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