]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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/exception/exceptions.h>\r
25 #include <common/utility/assert.h>\r
26 #include <common/gl/gl_check.h>\r
27 \r
28 #include <boost/foreach.hpp>\r
29 \r
30 #include <gl/glew.h>\r
31 \r
32 namespace caspar { namespace core {\r
33 \r
34 ogl_device::ogl_device() : executor_(L"ogl_device")\r
35 {\r
36         invoke([=]\r
37         {\r
38                 context_.reset(new sf::Context());\r
39                 context_->SetActive(true);\r
40                 \r
41                 if (glewInit() != GLEW_OK)\r
42                         BOOST_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));\r
43                                 \r
44                 if(!GLEW_VERSION_3_2)\r
45                         CASPAR_LOG(warning) << "Missing OpenGL 3.2 support.";\r
46         \r
47                 GL(glGenFramebuffers(1, &fbo_));                \r
48                 GL(glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo_));\r
49                 GL(glReadBuffer(GL_COLOR_ATTACHMENT0_EXT));\r
50         });\r
51 }\r
52 \r
53 ogl_device::~ogl_device()\r
54 {\r
55         invoke([=]\r
56         {\r
57                 BOOST_FOREACH(auto& pool, device_pools_)\r
58                         pool.clear();\r
59                 BOOST_FOREACH(auto& pool, host_pools_)\r
60                         pool.clear();\r
61                 glDeleteFramebuffersEXT(1, &fbo_);\r
62         });\r
63 }\r
64                                 \r
65 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
66 {\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
72         {\r
73                 executor_.invoke([&]\r
74                 {       \r
75                         try\r
76                         {\r
77                                 buffer.reset(new device_buffer(width, height, stride));\r
78                         }\r
79                         catch(...)\r
80                         {\r
81                                 try\r
82                                 {\r
83                                         yield();\r
84                                         BOOST_FOREACH(auto& pools, device_pools_)\r
85                                         {\r
86                                                 BOOST_FOREACH(auto& pool, pools)\r
87                                                         pool.second->clear();\r
88                                         }\r
89                                         \r
90                                         // Try again\r
91                                         buffer.reset(new device_buffer(width, height, stride));\r
92                                 }\r
93                                 catch(...)\r
94                                 {\r
95                                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
96                                         throw;\r
97                                 }\r
98                         }\r
99 \r
100                 }, high_priority);      \r
101         }\r
102                         \r
103         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*)\r
104         {\r
105                 pool->push(buffer);\r
106         });\r
107 }\r
108         \r
109 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
110 {\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
116         {\r
117                 executor_.invoke([&]\r
118                 {\r
119                         try\r
120                         {\r
121                                 buffer.reset(new host_buffer(size, usage));\r
122                                 if(usage == host_buffer::write_only)\r
123                                         buffer->map();\r
124                                 else\r
125                                         buffer->unmap();                        \r
126                         }\r
127                         catch(...)\r
128                         {\r
129                                 try\r
130                                 {\r
131                                         yield();\r
132                                         BOOST_FOREACH(auto& pools, host_pools_)\r
133                                         {\r
134                                                 BOOST_FOREACH(auto& pool, pools)\r
135                                                         pool.second->clear();\r
136                                         }\r
137 \r
138                                         // Try again\r
139                                         buffer.reset(new host_buffer(size, usage));\r
140                                         if(usage == host_buffer::write_only)\r
141                                                 buffer->map();\r
142                                         else\r
143                                                 buffer->unmap();        \r
144                                 }\r
145                                 catch(...)\r
146                                 {\r
147                                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
148                                         throw;          \r
149                                 }\r
150                         }\r
151 \r
152                 }, high_priority);      \r
153         }\r
154         \r
155         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
156         {\r
157                 executor_.begin_invoke([=]\r
158                 {\r
159                         if(usage == host_buffer::write_only)\r
160                                 buffer->map();\r
161                         else\r
162                                 buffer->unmap();\r
163                         \r
164                         pool->push(buffer);\r
165 \r
166                 }, high_priority);\r
167         });\r
168 }\r
169 \r
170 void ogl_device::yield()\r
171 {\r
172         executor_.yield();\r
173 }\r
174 \r
175 boost::unique_future<void> ogl_device::gc()\r
176 {       \r
177         return begin_invoke([=]\r
178         {\r
179                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
180         \r
181                 try\r
182                 {\r
183                         BOOST_FOREACH(auto& pools, device_pools_)\r
184                         {\r
185                                 BOOST_FOREACH(auto& pool, pools)\r
186                                         pool.second->clear();\r
187                         }\r
188                         BOOST_FOREACH(auto& pools, host_pools_)\r
189                         {\r
190                                 BOOST_FOREACH(auto& pool, pools)\r
191                                         pool.second->clear();\r
192                         }\r
193                 }\r
194                 catch(...)\r
195                 {\r
196                         CASPAR_LOG_CURRENT_EXCEPTION();\r
197                 }\r
198         }, high_priority);\r
199 }\r
200 \r
201 std::wstring ogl_device::get_version()\r
202 {       \r
203         static std::wstring ver = L"Not found";\r
204         try\r
205         {\r
206                 ogl_device tmp;\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
209         }\r
210         catch(...){}\r
211 \r
212         return ver;\r
213 }\r
214 \r
215 }}