]> git.sesse.net Git - casparcg/blob - accelerator/ogl/util/device.cpp
Refactored to use range based for instead of BOOST_FOREACH
[casparcg] / accelerator / ogl / util / device.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 // TODO: Smart GC
23
24 #include "../../stdafx.h"
25
26 #include "device.h"
27
28 #include "buffer.h"
29 #include "texture.h"
30 #include "shader.h"
31
32 #include <common/assert.h>
33 #include <common/except.h>
34 #include <common/future.h>
35 #include <common/array.h>
36 #include <common/gl/gl_check.h>
37 #include <common/os/windows/windows.h>
38
39 #include <gl/glew.h>
40
41 #include <SFML/Window/Context.hpp>
42
43 #include <tbb/concurrent_unordered_map.h>
44 #include <tbb/concurrent_hash_map.h>
45 #include <tbb/concurrent_queue.h>
46
47 #include <boost/utility/declval.hpp>
48
49 #include <array>
50 #include <unordered_map>
51
52 #include <asmlib.h>
53 #include <tbb/parallel_for.h>
54
55 namespace caspar { namespace accelerator { namespace ogl {
56                 
57 struct device::impl : public std::enable_shared_from_this<impl>
58 {       
59         static_assert(std::is_same<decltype(boost::declval<device>().impl_), spl::shared_ptr<impl>>::value, "impl_ must be shared_ptr");
60
61         tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>> texture_cache_;
62
63         std::unique_ptr<sf::Context> device_;
64         
65         std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<texture>>>, 4>      device_pools_;
66         std::array<tbb::concurrent_unordered_map<std::size_t, tbb::concurrent_bounded_queue<std::shared_ptr<buffer>>>, 2>       host_pools_;
67         
68         GLuint fbo_;
69
70         executor& executor_;
71                                 
72         impl(executor& executor) 
73                 : executor_(executor)
74         {
75                 executor_.set_capacity(256);
76
77                 CASPAR_LOG(info) << L"Initializing OpenGL Device.";
78                 
79                 executor_.invoke([=]
80                 {
81                         device_.reset(new sf::Context());
82                         device_->setActive(true);               
83                                                 
84                         if (glewInit() != GLEW_OK)
85                                 CASPAR_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
86                 
87                         if(!GLEW_VERSION_3_0)
88                                 CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Your graphics card does not meet the minimum hardware requirements since it does not support OpenGL 3.0 or higher."));
89         
90                         glGenFramebuffers(1, &fbo_);                            
91                         glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
92                 });
93                                 
94                 CASPAR_LOG(info) << L"Successfully initialized OpenGL " << version();
95         }
96
97         ~impl()
98         {
99                 executor_.invoke([=]
100                 {
101                         texture_cache_.clear();
102
103                         for (auto& pool : host_pools_)
104                                 pool.clear();
105
106                         for (auto& pool : device_pools_)
107                                 pool.clear();
108
109                         glDeleteFramebuffers(1, &fbo_);
110
111                         device_.reset();
112                 });
113         }
114                 
115         std::wstring version()
116         {       
117                 try
118                 {
119                         return executor_.invoke([]
120                         {
121                                 return u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VERSION)))) + L" " + u16(reinterpret_cast<const char*>(GL2(glGetString(GL_VENDOR))));
122                         });     
123                 }
124                 catch(...)
125                 {
126                         return L"Not found";;
127                 }
128         }
129                                                         
130         spl::shared_ptr<texture> create_texture(int width, int height, int stride, bool clear = false)
131         {
132                 CASPAR_VERIFY(stride > 0 && stride < 5);
133                 CASPAR_VERIFY(width > 0 && height > 0);
134
135                 if(!executor_.is_current())
136                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));
137                                         
138                 auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];
139                 
140                 std::shared_ptr<texture> tex;
141                 if(!pool->try_pop(tex))         
142                         tex = spl::make_shared<texture>(width, height, stride);
143         
144                 if(clear)
145                         tex->clear();
146
147                 return spl::shared_ptr<texture>(tex.get(), [tex, pool](texture*) mutable
148                 {               
149                         pool->push(tex);        
150                 });
151         }
152                 
153         spl::shared_ptr<buffer> create_buffer(std::size_t size, buffer::usage usage)
154         {
155                 CASPAR_VERIFY(size > 0);
156                 
157                 auto pool = &host_pools_[usage.value()][size];
158                 
159                 std::shared_ptr<buffer> buf;
160                 if(!pool->try_pop(buf)) 
161                 {
162                         boost::timer timer;
163
164                         buf = executor_.invoke([&]
165                         {
166                                 return std::make_shared<buffer>(size, usage);
167                         }, task_priority::high_priority);
168                         
169                         if(timer.elapsed() > 0.02)
170                                 CASPAR_LOG(debug) << L"[ogl-device] Performance warning. Buffer allocation blocked: " << timer.elapsed();
171                 }
172                 
173                 auto self = shared_from_this(); // buffers can leave the device context, take a hold on life-time.
174                 return spl::shared_ptr<buffer>(buf.get(), [=](buffer*) mutable
175                 {       
176                         texture_cache_.erase(buf.get());
177                         pool->push(buf);
178                 });
179         }
180
181         array<std::uint8_t> create_array(std::size_t size)
182         {               
183                 auto buf = create_buffer(size, buffer::usage::write_only);
184                 return array<std::uint8_t>(buf->data(), buf->size(), false, buf);
185         }
186
187         template<typename T>
188         std::shared_ptr<buffer> copy_to_buf(const T& source)
189         {
190                 std::shared_ptr<buffer> buf;
191
192                 auto tmp = source.storage<spl::shared_ptr<buffer>>();
193                 if(tmp)
194                         buf = *tmp;
195                 else
196                 {                       
197                         buf = create_buffer(source.size(), buffer::usage::write_only);
198                         tbb::parallel_for(tbb::blocked_range<std::size_t>(0, source.size()), [&](const tbb::blocked_range<std::size_t>& r)
199                         {
200                                 A_memcpy(buf->data() + r.begin(), source.data() + r.begin(), r.size());
201                         });
202                 }
203
204                 return buf;
205         }
206
207         // TODO: Since the returned texture is cached it SHOULD NOT be modified.
208         std::future<std::shared_ptr<texture>> copy_async(const array<const std::uint8_t>& source, int width, int height, int stride)
209         {
210                 std::shared_ptr<buffer> buf = copy_to_buf(source);
211                                 
212                 return executor_.begin_invoke([=]() -> std::shared_ptr<texture>
213                 {
214                         tbb::concurrent_hash_map<buffer*, std::shared_ptr<texture>>::const_accessor a;
215                         if(texture_cache_.find(a, buf.get()))
216                                 return spl::make_shared_ptr(a->second);
217
218                         auto texture = create_texture(width, height, stride);
219                         texture->copy_from(*buf);       
220
221                         texture_cache_.insert(std::make_pair(buf.get(), texture));
222                         
223                         return texture;
224                 }, task_priority::high_priority);
225         }
226         
227         std::future<std::shared_ptr<texture>> copy_async(const array<std::uint8_t>& source, int width, int height, int stride)
228         {
229                 std::shared_ptr<buffer> buf = copy_to_buf(source);
230
231                 return executor_.begin_invoke([=]() -> std::shared_ptr<texture>
232                 {
233                         auto texture = create_texture(width, height, stride, false);
234                         texture->copy_from(*buf);       
235                         
236                         return texture;
237                 }, task_priority::high_priority);
238         }
239
240         std::future<array<const std::uint8_t>> copy_async(const spl::shared_ptr<texture>& source)
241         {
242                 if(!executor_.is_current())
243                         CASPAR_THROW_EXCEPTION(invalid_operation() << msg_info("Operation only valid in an OpenGL Context."));
244
245                 auto buffer = create_buffer(source->size(), buffer::usage::read_only); 
246                 source->copy_to(*buffer);       
247
248                 auto self = shared_from_this();
249                 auto cmd = [self, buffer]() mutable -> array<const std::uint8_t>
250                 {
251                         self->executor_.invoke(std::bind(&buffer::map, std::ref(buffer))); // Defer blocking "map" call until data is needed.
252                         return array<const std::uint8_t>(buffer->data(), buffer->size(), true, buffer);
253                 };
254                 return std::async(std::launch::deferred, std::move(cmd));
255         }
256 };
257
258 device::device() 
259         : executor_(L"OpenGL Rendering Context")
260         , impl_(new impl(executor_)){}
261 device::~device(){}
262 spl::shared_ptr<texture>                                        device::create_texture(int width, int height, int stride){return impl_->create_texture(width, height, stride, true);}
263 array<std::uint8_t>                                                     device::create_array(int size){return impl_->create_array(size);}
264 std::future<std::shared_ptr<texture>>           device::copy_async(const array<const std::uint8_t>& source, int width, int height, int stride){return impl_->copy_async(source, width, height, stride);}
265 std::future<std::shared_ptr<texture>>           device::copy_async(const array<std::uint8_t>& source, int width, int height, int stride){return impl_->copy_async(source, width, height, stride);}
266 std::future<array<const std::uint8_t>>          device::copy_async(const spl::shared_ptr<texture>& source){return impl_->copy_async(source);}
267 std::wstring                                                            device::version() const{return impl_->version();}
268
269
270 }}}
271
272