]> git.sesse.net Git - casparcg/blob - modules/ogl/consumer/ogl_consumer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / ogl / consumer / ogl_consumer.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 \r
21 #define NOMINMAX\r
22 \r
23 #include <windows.h>\r
24 #include <Glee.h>\r
25 #include <SFML/Window.hpp>\r
26 \r
27 #include "ogl_consumer.h"\r
28 \r
29 #include <core/video_format.h>\r
30 #include <core/consumer/frame/read_frame.h>\r
31 \r
32 #include <common/gl/gl_check.h>\r
33 #include <common/concurrency/executor.h>\r
34 #include <common/memory/safe_ptr.h>\r
35 #include <common/diagnostics/graph.h>\r
36 #include <common/utility/timer.h>\r
37 \r
38 #include <boost/thread.hpp>\r
39 \r
40 #include <algorithm>\r
41 #include <array>\r
42 \r
43 namespace caspar {\r
44 \r
45 struct ogl_consumer::implementation : boost::noncopyable\r
46 {               \r
47         timer clock_;\r
48         printer parent_printer_;\r
49         boost::unique_future<void> active_;\r
50 \r
51         float wratio_;\r
52         float hratio_;\r
53         \r
54         float wSize_;\r
55         float hSize_;\r
56 \r
57         GLuint                            texture_;\r
58         std::array<GLuint, 2> pbos_;\r
59 \r
60         const bool windowed_;\r
61         unsigned int screen_width_;\r
62         unsigned int screen_height_;\r
63         unsigned int screen_x_;\r
64         unsigned int screen_y_;\r
65         const unsigned int screen_index_;\r
66                                 \r
67         const stretch stretch_;\r
68         core::video_format_desc format_desc_;\r
69         \r
70         sf::Window window_;\r
71         \r
72         safe_ptr<diagnostics::graph> graph_;\r
73         timer perf_timer_;\r
74 \r
75         executor executor_;\r
76 public:\r
77         implementation(unsigned int screen_index, stretch stretch, bool windowed) \r
78                 : stretch_(stretch)\r
79                 , windowed_(windowed)\r
80                 , texture_(0)\r
81                 , screen_x_(0)\r
82                 , screen_y_(0)\r
83                 , screen_index_(screen_index)\r
84                 , graph_(diagnostics::create_graph(narrow(print())))\r
85                 , executor_(print())\r
86         {               \r
87                 graph_->add_guide("frame-time", 0.5);\r
88                 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
89         }\r
90 \r
91         ~implementation()\r
92         {\r
93                 CASPAR_LOG(info) << print() << L" Shutting down.";      \r
94         }\r
95 \r
96         void initialize(const core::video_format_desc& format_desc, const printer& parent_printer)\r
97         {\r
98                 if(!GLEE_VERSION_2_1)\r
99                         BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));\r
100                 \r
101                 format_desc_ = format_desc;\r
102                 parent_printer_ = parent_printer;\r
103 \r
104                 screen_width_ = format_desc.width;\r
105                 screen_height_ = format_desc.height;\r
106 #ifdef _WIN32\r
107                 DISPLAY_DEVICE d_device;                        \r
108                 memset(&d_device, 0, sizeof(d_device));\r
109                 d_device.cb = sizeof(d_device);\r
110 \r
111                 std::vector<DISPLAY_DEVICE> displayDevices;\r
112                 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)\r
113                 {\r
114                         displayDevices.push_back(d_device);\r
115                         memset(&d_device, 0, sizeof(d_device));\r
116                         d_device.cb = sizeof(d_device);\r
117                 }\r
118 \r
119                 if(screen_index_ >= displayDevices.size())\r
120                         BOOST_THROW_EXCEPTION(out_of_range() << arg_name_info("screen_index_") << msg_info(narrow(print())));\r
121                 \r
122                 DEVMODE devmode;\r
123                 memset(&devmode, 0, sizeof(devmode));\r
124                 \r
125                 if(!EnumDisplaySettings(displayDevices[screen_index_].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))\r
126                         BOOST_THROW_EXCEPTION(invalid_operation() << arg_name_info("screen_index") << msg_info(narrow(print()) + " EnumDisplaySettings"));\r
127                 \r
128                 screen_width_ = windowed_ ? format_desc_.width : devmode.dmPelsWidth;\r
129                 screen_height_ = windowed_ ? format_desc_.height : devmode.dmPelsHeight;\r
130                 screen_x_ = devmode.dmPosition.x;\r
131                 screen_y_ = devmode.dmPosition.y;\r
132 #else\r
133                 if(!windowed)\r
134                         BOOST_THROW_EXCEPTION(not_supported() << msg_info(narrow(print() + " doesn't support non-Win32 fullscreen"));\r
135 \r
136                 if(screen_index != 0)\r
137                         CASPAR_LOG(warning) << print() << " only supports screen_index=0 for non-Win32";\r
138 #endif          \r
139                 executor_.start();\r
140                 executor_.invoke([=]\r
141                 {\r
142                         window_.Create(sf::VideoMode(screen_width_, screen_height_, 32), narrow(print()), windowed_ ? sf::Style::Titlebar : sf::Style::Fullscreen);\r
143                         window_.ShowMouseCursor(false);\r
144                         window_.SetPosition(screen_x_, screen_y_);\r
145                         window_.SetSize(screen_width_, screen_height_);\r
146                         window_.SetActive();\r
147                         GL(glEnable(GL_TEXTURE_2D));\r
148                         GL(glDisable(GL_DEPTH_TEST));           \r
149                         GL(glClearColor(0.0, 0.0, 0.0, 0.0));\r
150                         GL(glViewport(0, 0, format_desc_.width, format_desc_.height));\r
151                         glLoadIdentity();\r
152                                 \r
153                         wratio_ = static_cast<float>(format_desc_.width)/static_cast<float>(format_desc_.width);\r
154                         hratio_ = static_cast<float>(format_desc_.height)/static_cast<float>(format_desc_.height);\r
155 \r
156                         std::pair<float, float> target_ratio = None();\r
157                         if(stretch_ == fill)\r
158                                 target_ratio = Fill();\r
159                         else if(stretch_ == uniform)\r
160                                 target_ratio = Uniform();\r
161                         else if(stretch_ == uniform_to_fill)\r
162                                 target_ratio = UniformToFill();\r
163 \r
164                         wSize_ = target_ratio.first;\r
165                         hSize_ = target_ratio.second;\r
166                         \r
167                         glGenTextures(1, &texture_);\r
168                         glBindTexture(GL_TEXTURE_2D, texture_);\r
169                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);\r
170                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);\r
171                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);\r
172                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);\r
173                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);\r
174                         glBindTexture(GL_TEXTURE_2D, 0);\r
175                         \r
176                         GL(glGenBuffers(2, pbos_.data()));\r
177                         \r
178                         glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[0]);\r
179                         glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);\r
180                         glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[1]);\r
181                         glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);\r
182                         glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);\r
183                 });\r
184                 active_ = executor_.begin_invoke([]{});\r
185                 CASPAR_LOG(info) << print() << " Sucessfully initialized.";\r
186         }\r
187                 \r
188         std::pair<float, float> None()\r
189         {\r
190                 float width = static_cast<float>(format_desc_.width)/static_cast<float>(screen_width_);\r
191                 float height = static_cast<float>(format_desc_.height)/static_cast<float>(screen_height_);\r
192 \r
193                 return std::make_pair(width, height);\r
194         }\r
195 \r
196         std::pair<float, float> Uniform()\r
197         {\r
198                 float aspect = static_cast<float>(format_desc_.width)/static_cast<float>(format_desc_.height);\r
199                 float width = std::min(1.0f, static_cast<float>(screen_height_)*aspect/static_cast<float>(screen_width_));\r
200                 float height = static_cast<float>(screen_width_*width)/static_cast<float>(screen_height_*aspect);\r
201 \r
202                 return std::make_pair(width, height);\r
203         }\r
204 \r
205         std::pair<float, float> Fill()\r
206         {\r
207                 return std::make_pair(1.0f, 1.0f);\r
208         }\r
209 \r
210         std::pair<float, float> UniformToFill()\r
211         {\r
212                 float wr = static_cast<float>(format_desc_.width)/static_cast<float>(screen_width_);\r
213                 float hr = static_cast<float>(format_desc_.height)/static_cast<float>(screen_height_);\r
214                 float r_inv = 1.0f/std::min(wr, hr);\r
215 \r
216                 float width = wr*r_inv;\r
217                 float height = hr*r_inv;\r
218 \r
219                 return std::make_pair(width, height);\r
220         }\r
221 \r
222         void render(const safe_ptr<const core::read_frame>& frame)\r
223         {                       \r
224                 glBindTexture(GL_TEXTURE_2D, texture_);\r
225                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]);\r
226 \r
227                 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);\r
228 \r
229                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]);\r
230 \r
231                 glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW);\r
232 \r
233                 auto ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);\r
234                 if(ptr)\r
235                 {\r
236                         std::copy(frame->image_data().begin(), frame->image_data().end(), reinterpret_cast<char*>(ptr));\r
237                         glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); // release the mapped buffer\r
238                 }\r
239 \r
240                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);\r
241                                 \r
242                 GL(glClear(GL_COLOR_BUFFER_BIT));                       \r
243                 glBegin(GL_QUADS);\r
244                                 glTexCoord2f(0.0f,        hratio_);     glVertex2f(-wSize_, -hSize_);\r
245                                 glTexCoord2f(wratio_, hratio_); glVertex2f( wSize_, -hSize_);\r
246                                 glTexCoord2f(wratio_, 0.0f);    glVertex2f( wSize_,  hSize_);\r
247                                 glTexCoord2f(0.0f,        0.0f);        glVertex2f(-wSize_,  hSize_);\r
248                 glEnd();\r
249                 \r
250                 glBindTexture(GL_TEXTURE_2D, 0);\r
251 \r
252                 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());\r
253 \r
254                 clock_.tick(1.0/format_desc_.fps);\r
255         }\r
256                 \r
257         void send(const safe_ptr<const core::read_frame>& frame)\r
258         {\r
259                 active_.get();\r
260                 active_ = executor_.begin_invoke([=]\r
261                 {\r
262                         perf_timer_.reset();\r
263                         sf::Event e;\r
264                         while(window_.GetEvent(e)){}\r
265                         render(frame);\r
266                         window_.Display();\r
267                         graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/format_desc_.interval*0.5));\r
268                 });\r
269         }\r
270 \r
271         std::wstring print() const\r
272         {\r
273                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ogl[" + boost::lexical_cast<std::wstring>(screen_index_) + L"]";\r
274         }\r
275 \r
276         size_t buffer_depth() const{return 2;}\r
277 };\r
278 \r
279 ogl_consumer::ogl_consumer(ogl_consumer&& other) : impl_(std::move(other.impl_)){}\r
280 ogl_consumer::ogl_consumer(unsigned int screen_index, stretch stretch, bool windowed) : impl_(new implementation(screen_index, stretch, windowed)){}\r
281 void ogl_consumer::send(const safe_ptr<const core::read_frame>& frame){impl_->send(frame);}\r
282 size_t ogl_consumer::buffer_depth() const{return impl_->buffer_depth();}\r
283 void ogl_consumer::initialize(const core::video_format_desc& format_desc, const printer& parent_printer){impl_->initialize(format_desc, parent_printer);}\r
284 std::wstring ogl_consumer::print() const {return impl_->print();}\r
285 \r
286 safe_ptr<core::frame_consumer> create_ogl_consumer(const std::vector<std::wstring>& params)\r
287 {\r
288         if(params.size() < 1 || params[0] != L"OGL")\r
289                 return core::frame_consumer::empty();\r
290         \r
291         unsigned int screen_index = 0;\r
292         stretch stretch = stretch::fill;\r
293         bool windowed = true;\r
294         \r
295         if(params.size() > 1) \r
296                 screen_index = lexical_cast_or_default<int>(params[2], screen_index);\r
297 \r
298         if(params.size() > 2) \r
299                 windowed = lexical_cast_or_default<bool>(params[3], windowed);\r
300 \r
301         return make_safe<ogl_consumer>(screen_index, stretch, windowed);\r
302 }\r
303 \r
304 }