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/exception/exceptions.h>
\r
25 #include <common/utility/assert.h>
\r
26 #include <common/gl/gl_check.h>
\r
28 #include <boost/foreach.hpp>
\r
30 #include <gl/glew.h>
\r
32 namespace caspar { namespace core {
\r
34 ogl_device::ogl_device() : executor_(L"ogl_device")
\r
38 context_.reset(new sf::Context());
\r
39 context_->SetActive(true);
\r
41 if (glewInit() != GLEW_OK)
\r
42 BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
\r
44 if(!GLEW_VERSION_3_2)
\r
45 CASPAR_LOG(warning) << "Missing OpenGL 3.2 support.";
\r
47 GL(glGenFramebuffers(1, &fbo_));
\r
48 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));
\r
49 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));
\r
53 ogl_device::~ogl_device()
\r
57 BOOST_FOREACH(auto& pool, device_pools_)
\r
59 BOOST_FOREACH(auto& pool, host_pools_)
\r
61 glDeleteFramebuffersEXT(1, &fbo_);
\r
65 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)
\r
67 CASPAR_VERIFY(stride > 0 && stride < 5);
\r
68 CASPAR_VERIFY(width > 0 && height > 0);
\r
69 auto pool = device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
\r
70 std::shared_ptr<device_buffer> buffer;
\r
71 if(!pool->try_pop(buffer))
\r
73 executor_.invoke([&]
\r
77 buffer.reset(new device_buffer(width, height, stride));
\r
84 BOOST_FOREACH(auto& pools, device_pools_)
\r
86 BOOST_FOREACH(auto& pool, pools)
\r
87 pool.second->clear();
\r
91 buffer.reset(new device_buffer(width, height, stride));
\r
95 CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";
\r
100 }, high_priority);
\r
103 return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*)
\r
105 pool->push(buffer);
\r
109 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)
\r
111 CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);
\r
112 CASPAR_VERIFY(size > 0);
\r
113 auto pool = host_pools_[usage][size];
\r
114 std::shared_ptr<host_buffer> buffer;
\r
115 if(!pool->try_pop(buffer))
\r
117 executor_.invoke([&]
\r
121 buffer.reset(new host_buffer(size, usage));
\r
122 if(usage == host_buffer::write_only)
\r
132 BOOST_FOREACH(auto& pools, host_pools_)
\r
134 BOOST_FOREACH(auto& pool, pools)
\r
135 pool.second->clear();
\r
139 buffer.reset(new host_buffer(size, usage));
\r
140 if(usage == host_buffer::write_only)
\r
147 CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";
\r
152 }, high_priority);
\r
155 return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)
\r
157 executor_.begin_invoke([=]
\r
159 if(usage == host_buffer::write_only)
\r
164 pool->push(buffer);
\r
170 void ogl_device::yield()
\r
175 boost::unique_future<void> ogl_device::gc()
\r
177 return begin_invoke([=]
\r
179 CASPAR_LOG(info) << " ogl: Running GC.";
\r
183 BOOST_FOREACH(auto& pools, device_pools_)
\r
185 BOOST_FOREACH(auto& pool, pools)
\r
186 pool.second->clear();
\r
188 BOOST_FOREACH(auto& pools, host_pools_)
\r
190 BOOST_FOREACH(auto& pool, pools)
\r
191 pool.second->clear();
\r
196 CASPAR_LOG_CURRENT_EXCEPTION();
\r
201 std::wstring ogl_device::get_version()
\r
203 static std::wstring ver = L"Not found";
\r
207 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})
\r
208 + " " + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));
\r