2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "../../stdafx.h"
\r
22 #include "ogl_device.h"
\r
24 #include <common/utility/assert.h>
\r
25 #include <common/gl/gl_check.h>
\r
27 #include <boost/foreach.hpp>
\r
29 namespace caspar { namespace core {
\r
31 ogl_device::ogl_device() : executor_(L"ogl_device")
\r
35 context_.reset(new sf::Context());
\r
36 context_->SetActive(true);
\r
38 GL(glGenFramebuffers(1, &fbo_));
\r
39 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));
\r
40 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
\r
44 ogl_device::~ogl_device()
\r
48 BOOST_FOREACH(auto& pool, device_pools_)
\r
50 BOOST_FOREACH(auto& pool, host_pools_)
\r
52 glDeleteFramebuffersEXT(1, &fbo_);
\r
56 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
58 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
59 CASPAR_VERIFY(width > 0 && height > 0);
\r
60 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
61 std::shared_ptr<device_buffer> buffer;
\r
62 if(!pool->try_pop(buffer))
\r
64 executor_.invoke([&]
\r
68 buffer = std::make_shared<device_buffer>(width, height, stride);
\r
72 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
78 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*){pool->push(buffer);});
\r
81 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
83 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
84 CASPAR_VERIFY(size > 0);
\r
85 auto pool = &host_pools_[usage][size];
\r
86 std::shared_ptr<host_buffer> buffer;
\r
87 if(!pool->try_pop(buffer))
\r
89 executor_.invoke([&]
\r
93 buffer = std::make_shared<host_buffer>(size, usage);
\r
94 if(usage == host_buffer::write_only)
\r
101 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
105 }, high_priority);
\r
108 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)
\r
110 executor_.begin_invoke([=]
\r
112 if(usage == host_buffer::write_only)
\r
117 pool->push(buffer);
\r
123 void ogl_device::yield()
\r
128 boost::unique_future<void> ogl_device::gc()
\r
130 CASPAR_LOG(info) << " ogl: Running GC.";
\r
132 return begin_invoke([=]
\r
134 BOOST_FOREACH(auto& pool, device_pools_)
\r
136 BOOST_FOREACH(auto& pool, host_pools_)
\r
141 std::wstring ogl_device::get_version()
\r
143 static std::wstring ver;
\r
147 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
148 + " " + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r