2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
29 #include <common/except.h>
30 #include <common/gl/gl_check.h>
31 #include <common/timer.h>
35 #include <tbb/atomic.h>
37 namespace caspar { namespace accelerator { namespace ogl {
39 static tbb::atomic<int> g_w_total_count;
40 static tbb::atomic<int> g_r_total_count;
42 struct buffer::impl : boost::noncopyable
45 const std::size_t size_;
46 tbb::atomic<uint8_t*> data_;
51 impl(std::size_t size, buffer::usage usage)
53 , target_(usage == buffer::usage::write_only ? GL_PIXEL_UNPACK_BUFFER : GL_PIXEL_PACK_BUFFER)
54 , usage_(usage == buffer::usage::write_only ? GL_STREAM_DRAW : GL_STREAM_READ)
59 GL(glGenBuffers(1, &pbo_));
61 GL(glBufferData(target_, size_, NULL, usage_));
62 if(usage_ == GL_STREAM_DRAW)
64 auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
65 data_ = reinterpret_cast<uint8_t*>(result);
70 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to allocate buffer."));
72 if(timer.elapsed() > 0.02)
73 CASPAR_LOG(warning) << L"[buffer] Performance warning. Buffer allocation blocked: " << timer.elapsed();
75 //CASPAR_LOG(trace) << "[buffer] [" << ++(usage_ == buffer::usage::write_only ? g_w_total_count : g_r_total_count) << L"] allocated size:" << size_ << " usage: " << (usage == buffer::usage::write_only ? "write_only" : "read_only");
80 glDeleteBuffers(1, &pbo_);
90 GL(glBindBuffer(target_, pbo_));
91 if(usage_ == GL_STREAM_DRAW)
92 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
94 auto result = GL2(glMapBuffer(target_, usage_ == GL_STREAM_DRAW ? GL_WRITE_ONLY : GL_READ_ONLY));
95 data_ = (uint8_t*) result;
97 if(timer.elapsed() > 0.02)
98 CASPAR_LOG(warning) << L"[buffer] Performance warning. Buffer mapping blocked: " << timer.elapsed();
100 GL(glBindBuffer(target_, 0));
102 CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to map target OpenGL Pixel Buffer Object."));
112 GL(glBindBuffer(target_, pbo_));
113 GL(glUnmapBuffer(target_));
114 if(usage_ == GL_STREAM_READ)
115 GL(glBufferData(target_, size_, NULL, usage_)); // Notify OpenGL that we don't care about previous data.
117 GL(glBindBuffer(target_, 0));
122 GL(glBindBuffer(target_, pbo_));
127 GL(glBindBuffer(target_, 0));
131 buffer::buffer(std::size_t size, usage usage) : impl_(new impl(size, usage)){}
132 buffer::buffer(buffer&& other) : impl_(std::move(other.impl_)){}
134 buffer& buffer::operator=(buffer&& other){impl_ = std::move(other.impl_); return *this;}
135 uint8_t* buffer::data(){return impl_->data_;}
136 void buffer::map(){impl_->map();}
137 void buffer::unmap(){impl_->unmap();}
138 void buffer::bind() const{impl_->bind();}
139 void buffer::unbind() const{impl_->unbind();}
140 std::size_t buffer::size() const { return impl_->size_; }
141 int buffer::id() const {return impl_->pbo_;}