]> 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                                         gc().wait();\r
85                                         \r
86                                         // Try again\r
87                                         buffer.reset(new device_buffer(width, height, stride));\r
88                                 }\r
89                                 catch(...)\r
90                                 {\r
91                                         CASPAR_LOG(error) << L"ogl: create_device_buffer failed!";\r
92                                         throw;\r
93                                 }\r
94                         }\r
95 \r
96                 }, high_priority);      \r
97         }\r
98                         \r
99         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*)\r
100         {\r
101                 pool->push(buffer);\r
102         });\r
103 }\r
104         \r
105 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
106 {\r
107         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
108         CASPAR_VERIFY(size > 0);\r
109         auto pool = host_pools_[usage][size];\r
110         std::shared_ptr<host_buffer> buffer;\r
111         if(!pool->try_pop(buffer))\r
112         {\r
113                 executor_.invoke([&]\r
114                 {\r
115                         try\r
116                         {\r
117                                 buffer.reset(new host_buffer(size, usage));\r
118                                 if(usage == host_buffer::write_only)\r
119                                         buffer->map();\r
120                                 else\r
121                                         buffer->unmap();                        \r
122                         }\r
123                         catch(...)\r
124                         {\r
125                                 try\r
126                                 {\r
127                                         yield();\r
128                                         gc().wait();\r
129 \r
130                                         // Try again\r
131                                         buffer.reset(new host_buffer(size, usage));\r
132                                         if(usage == host_buffer::write_only)\r
133                                                 buffer->map();\r
134                                         else\r
135                                                 buffer->unmap();        \r
136                                 }\r
137                                 catch(...)\r
138                                 {\r
139                                         CASPAR_LOG(error) << L"ogl: create_host_buffer failed!";\r
140                                         throw;          \r
141                                 }\r
142                         }\r
143 \r
144                 }, high_priority);      \r
145         }\r
146         \r
147         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
148         {\r
149                 executor_.begin_invoke([=]\r
150                 {\r
151                         if(usage == host_buffer::write_only)\r
152                                 buffer->map();\r
153                         else\r
154                                 buffer->unmap();\r
155                         \r
156                         pool->push(buffer);\r
157 \r
158                 }, high_priority);\r
159         });\r
160 }\r
161 \r
162 void ogl_device::yield()\r
163 {\r
164         executor_.yield();\r
165 }\r
166 \r
167 boost::unique_future<void> ogl_device::gc()\r
168 {       \r
169         return begin_invoke([=]\r
170         {\r
171                 CASPAR_LOG(info) << " ogl: Running GC.";                \r
172         \r
173                 try\r
174                 {\r
175                         BOOST_FOREACH(auto& pools, device_pools_)\r
176                         {\r
177                                 BOOST_FOREACH(auto& pool, pools)\r
178                                         pool.second->clear();\r
179                         }\r
180                         BOOST_FOREACH(auto& pools, host_pools_)\r
181                         {\r
182                                 BOOST_FOREACH(auto& pool, pools)\r
183                                         pool.second->clear();\r
184                         }\r
185                 }\r
186                 catch(...)\r
187                 {\r
188                         CASPAR_LOG_CURRENT_EXCEPTION();\r
189                 }\r
190         }, high_priority);\r
191 }\r
192 \r
193 std::wstring ogl_device::get_version()\r
194 {       \r
195         static std::wstring ver = L"Not found";\r
196         try\r
197         {\r
198                 ogl_device tmp;\r
199                 ver = widen(tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VERSION)));})\r
200                 + " "   + tmp.invoke([]{return std::string(reinterpret_cast<const char*>(glGetString(GL_VENDOR)));}));                  \r
201         }\r
202         catch(...){}\r
203 \r
204         return ver;\r
205 }\r
206 \r
207 }}