]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
7cbe5a9ed7a7f46952fa947f0edbe267d21c815e
[casparcg] / core / mixer / gpu / ogl_device.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "ogl_device.h"\r
23 \r
24 #include <common/utility/assert.h>\r
25 #include <common/gl/gl_check.h>\r
26 \r
27 #include <boost/foreach.hpp>\r
28 \r
29 namespace caspar { namespace core {\r
30 \r
31 ogl_device::ogl_device() : executor_(L"ogl_device")\r
32 {\r
33         invoke([=]\r
34         {\r
35                 context_.reset(new sf::Context());\r
36                 context_->SetActive(true);\r
37                                                 \r
38                 GL(glGenFramebuffers(1, &fbo_));                \r
39                 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
40                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
41         });\r
42 }\r
43 \r
44 ogl_device::~ogl_device()\r
45 {\r
46         invoke([=]\r
47         {\r
48                 BOOST_FOREACH(auto& pool, device_pools_)\r
49                         pool.clear();\r
50                 BOOST_FOREACH(auto& pool, host_pools_)\r
51                         pool.clear();\r
52                 glDeleteFramebuffersEXT(1, &fbo_);\r
53         });\r
54 }\r
55                                 \r
56 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
57 {\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
63         {\r
64                 executor_.invoke([&]\r
65                 {       \r
66                         try\r
67                         {\r
68                                 buffer.reset(new device_buffer(width, height, stride));\r
69                         }\r
70                         catch(...)\r
71                         {\r
72                                 try\r
73                                 {\r
74                                         yield();\r
75                                         gc().get();\r
76                                         \r
77                                         // Try again\r
78                                         buffer.reset(new device_buffer(width, height, stride));\r
79                                 }\r
80                                 catch(...)\r
81                                 {\r
82                                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
83                                         throw;\r
84                                 }\r
85                         }\r
86 \r
87                 }, high_priority);      \r
88         }\r
89                         \r
90         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*)\r
91         {\r
92                 pool->push(buffer);\r
93         });\r
94 }\r
95         \r
96 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
97 {\r
98         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
99         CASPAR_VERIFY(size > 0);\r
100         auto pool = host_pools_[usage][size];\r
101         std::shared_ptr<host_buffer> buffer;\r
102         if(!pool->try_pop(buffer))\r
103         {\r
104                 executor_.invoke([&]\r
105                 {\r
106                         try\r
107                         {\r
108                                 buffer.reset(new host_buffer(size, usage));\r
109                                 if(usage == host_buffer::write_only)\r
110                                         buffer->map();\r
111                                 else\r
112                                         buffer->unmap();                        \r
113                         }\r
114                         catch(...)\r
115                         {\r
116                                 try\r
117                                 {\r
118                                         yield();\r
119                                         gc().get();\r
120 \r
121                                         // Try again\r
122                                         buffer.reset(new host_buffer(size, usage));\r
123                                         if(usage == host_buffer::write_only)\r
124                                                 buffer->map();\r
125                                         else\r
126                                                 buffer->unmap();        \r
127                                 }\r
128                                 catch(...)\r
129                                 {\r
130                                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
131                                         throw;          \r
132                                 }\r
133                         }\r
134 \r
135                 }, high_priority);      \r
136         }\r
137         \r
138         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
139         {\r
140                 executor_.begin_invoke([=]\r
141                 {\r
142                         if(usage == host_buffer::write_only)\r
143                                 buffer->map();\r
144                         else\r
145                                 buffer->unmap();\r
146                         \r
147                         pool->push(buffer);\r
148 \r
149                 }, high_priority);\r
150         });\r
151 }\r
152 \r
153 void ogl_device::yield()\r
154 {\r
155         executor_.yield();\r
156 }\r
157 \r
158 boost::unique_future<void> ogl_device::gc()\r
159 {       \r
160         return begin_invoke([=]\r
161         {\r
162                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
163         \r
164                 try\r
165                 {\r
166                         BOOST_FOREACH(auto& pools, device_pools_)\r
167                         {\r
168                                 BOOST_FOREACH(auto& pool, pools)\r
169                                         pool.second->clear();\r
170                         }\r
171                         BOOST_FOREACH(auto& pools, host_pools_)\r
172                         {\r
173                                 BOOST_FOREACH(auto& pool, pools)\r
174                                         pool.second->clear();\r
175                         }\r
176                 }\r
177                 catch(...)\r
178                 {\r
179                         CASPAR_LOG_CURRENT_EXCEPTION();\r
180                 }\r
181         }, high_priority);\r
182 }\r
183 \r
184 std::wstring ogl_device::get_version()\r
185 {       \r
186         static std::wstring ver = L"Not found";\r
187         try\r
188         {\r
189                 ogl_device tmp;\r
190                 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
191                 + " "   + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                  \r
192         }\r
193         catch(...){}\r
194 \r
195         return ver;\r
196 }\r
197 \r
198 }}