2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
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
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
21 #include "ogl_consumer.h"
\r
23 #include <GL/glew.h>
\r
24 #include <SFML/Window.hpp>
\r
26 #include <common/diagnostics/graph.h>
\r
27 #include <common/gl/gl_check.h>
\r
28 #include <common/log/log.h>
\r
29 #include <common/memory/safe_ptr.h>
\r
30 #include <common/memory/memcpy.h>
\r
31 #include <common/utility/timer.h>
\r
32 #include <common/utility/string.h>
\r
34 #include <core/video_format.h>
\r
35 #include <core/mixer/read_frame.h>
\r
36 #include <core/consumer/frame_consumer.h>
\r
38 #include <boost/timer.hpp>
\r
39 #include <boost/circular_buffer.hpp>
\r
40 #include <boost/foreach.hpp>
\r
41 #include <boost/thread.hpp>
\r
43 #include <tbb/atomic.h>
\r
44 #include <tbb/concurrent_queue.h>
\r
46 #include <algorithm>
\r
59 struct ogl_consumer : boost::noncopyable
\r
61 core::video_format_desc format_desc_;
\r
68 const stretch stretch_;
\r
70 const bool windowed_;
\r
71 unsigned int screen_x_;
\r
72 unsigned int screen_y_;
\r
73 unsigned int screen_width_;
\r
74 unsigned int screen_height_;
\r
75 const unsigned int screen_index_;
\r
77 size_t square_width_;
\r
78 size_t square_height_;
\r
82 safe_ptr<diagnostics::graph> graph_;
\r
83 boost::timer perf_timer_;
\r
85 boost::circular_buffer<safe_ptr<core::read_frame>> input_buffer_;
\r
86 tbb::concurrent_bounded_queue<safe_ptr<core::read_frame>> frame_buffer_;
\r
88 boost::thread thread_;
\r
89 tbb::atomic<bool> is_running_;
\r
91 ogl_consumer(unsigned int screen_index, stretch stretch, bool windowed, const core::video_format_desc& format_desc)
\r
92 : format_desc_(format_desc)
\r
95 , windowed_(windowed)
\r
96 , screen_index_(screen_index)
\r
97 , screen_width_(format_desc.width)
\r
98 , screen_height_(format_desc.height)
\r
99 , square_width_(format_desc.width)
\r
100 , square_height_(format_desc.height)
\r
101 , graph_(diagnostics::create_graph(narrow(print())))
\r
102 , input_buffer_(core::consumer_buffer_depth()-1)
\r
104 frame_buffer_.set_capacity(2);
\r
106 graph_->add_guide("frame-time", 0.5);
\r
107 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
108 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
\r
110 if(format_desc_.format == core::video_format::pal)
\r
112 square_width_ = 768;
\r
113 square_height_ = 576;
\r
115 else if(format_desc_.format == core::video_format::ntsc)
\r
117 square_width_ = 720;
\r
118 square_height_ = 547;
\r
121 DISPLAY_DEVICE d_device;
\r
122 memset(&d_device, 0, sizeof(d_device));
\r
123 d_device.cb = sizeof(d_device);
\r
125 std::vector<DISPLAY_DEVICE> displayDevices;
\r
126 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
\r
128 displayDevices.push_back(d_device);
\r
129 memset(&d_device, 0, sizeof(d_device));
\r
130 d_device.cb = sizeof(d_device);
\r
133 if(screen_index_ >= displayDevices.size())
\r
134 BOOST_THROW_EXCEPTION(out_of_range() << arg_name_info("screen_index_") << msg_info(narrow(print())));
\r
137 memset(&devmode, 0, sizeof(devmode));
\r
139 if(!EnumDisplaySettings(displayDevices[screen_index_].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))
\r
140 BOOST_THROW_EXCEPTION(invalid_operation() << arg_name_info("screen_index") << msg_info(narrow(print()) + " EnumDisplaySettings"));
\r
142 screen_x_ = devmode.dmPosition.x;
\r
143 screen_y_ = devmode.dmPosition.y;
\r
144 screen_width_ = windowed_ ? square_width_ : devmode.dmPelsWidth;
\r
145 screen_height_ = windowed_ ? square_height_ : devmode.dmPelsHeight;
\r
147 is_running_ = true;
\r
148 thread_ = boost::thread([this]{run();});
\r
153 is_running_ = false;
\r
154 frame_buffer_.try_push(make_safe<core::read_frame>());
\r
160 if(!GLEW_VERSION_2_1)
\r
161 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));
\r
163 window_.Create(sf::VideoMode(screen_width_, screen_height_, 32), narrow(print()), windowed_ ? sf::Style::Resize : sf::Style::Fullscreen);
\r
164 window_.ShowMouseCursor(false);
\r
165 window_.SetPosition(screen_x_, screen_y_);
\r
166 window_.SetSize(screen_width_, screen_height_);
\r
167 window_.SetActive();
\r
168 GL(glEnable(GL_TEXTURE_2D));
\r
169 GL(glDisable(GL_DEPTH_TEST));
\r
170 GL(glClearColor(0.0, 0.0, 0.0, 0.0));
\r
171 GL(glViewport(0, 0, format_desc_.width, format_desc_.height));
\r
172 GL(glLoadIdentity());
\r
174 calculate_aspect();
\r
176 GL(glGenTextures(1, &texture_));
\r
177 GL(glBindTexture(GL_TEXTURE_2D, texture_));
\r
178 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
\r
179 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
\r
180 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
\r
181 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
\r
182 GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0));
\r
183 GL(glBindTexture(GL_TEXTURE_2D, 0));
\r
185 CASPAR_LOG(info) << print() << " Sucessfully Initialized.";
\r
191 glDeleteTextures(1, &texture_);
\r
193 CASPAR_LOG(info) << print() << " Sucessfully Uninitialized.";
\r
206 perf_timer_.restart();
\r
209 while(window_.GetEvent(e))
\r
211 if(e.Type == sf::Event::Resized)
\r
212 calculate_aspect();
\r
215 safe_ptr<core::read_frame> frame;
\r
216 frame_buffer_.pop(frame);
\r
221 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()*format_desc_.fps*0.5));
\r
225 CASPAR_LOG_CURRENT_EXCEPTION();
\r
226 is_running_ = false;
\r
234 CASPAR_LOG_CURRENT_EXCEPTION();
\r
238 const core::video_format_desc& get_video_format_desc() const
\r
240 return format_desc_;
\r
243 void render(const safe_ptr<core::read_frame>& frame)
\r
245 if(frame->image_data().empty())
\r
248 GL(glBindTexture(GL_TEXTURE_2D, texture_));
\r
250 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, frame->image_data().begin()));
\r
252 GL(glClear(GL_COLOR_BUFFER_BIT));
\r
254 glTexCoord2f(0.0f, 1.0f); glVertex2f(-width_, -height_);
\r
255 glTexCoord2f(1.0f, 1.0f); glVertex2f( width_, -height_);
\r
256 glTexCoord2f(1.0f, 0.0f); glVertex2f( width_, height_);
\r
257 glTexCoord2f(0.0f, 0.0f); glVertex2f(-width_, height_);
\r
260 glBindTexture(GL_TEXTURE_2D, 0);
\r
263 void send(const safe_ptr<core::read_frame>& frame)
\r
265 input_buffer_.push_back(frame);
\r
267 if(input_buffer_.full())
\r
269 if(!frame_buffer_.try_push(input_buffer_.front()))
\r
270 graph_->add_tag("dropped-frame");
\r
274 std::wstring print() const
\r
276 return L"ogl[" + boost::lexical_cast<std::wstring>(screen_index_) + L"|" + format_desc_.name + L"]";
\r
279 void calculate_aspect()
\r
283 screen_height_ = window_.GetHeight();
\r
284 screen_width_ = window_.GetWidth();
\r
287 GL(glViewport(0, 0, screen_width_, screen_height_));
\r
289 std::pair<float, float> target_ratio = None();
\r
290 if(stretch_ == fill)
\r
291 target_ratio = Fill();
\r
292 else if(stretch_ == uniform)
\r
293 target_ratio = Uniform();
\r
294 else if(stretch_ == uniform_to_fill)
\r
295 target_ratio = UniformToFill();
\r
297 width_ = target_ratio.first;
\r
298 height_ = target_ratio.second;
\r
301 std::pair<float, float> None()
\r
303 float width = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
\r
304 float height = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
\r
306 return std::make_pair(width, height);
\r
309 std::pair<float, float> Uniform()
\r
311 float aspect = static_cast<float>(square_width_)/static_cast<float>(square_height_);
\r
312 float width = std::min(1.0f, static_cast<float>(screen_height_)*aspect/static_cast<float>(screen_width_));
\r
313 float height = static_cast<float>(screen_width_*width)/static_cast<float>(screen_height_*aspect);
\r
315 return std::make_pair(width, height);
\r
318 std::pair<float, float> Fill()
\r
320 return std::make_pair(1.0f, 1.0f);
\r
323 std::pair<float, float> UniformToFill()
\r
325 float wr = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
\r
326 float hr = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
\r
327 float r_inv = 1.0f/std::min(wr, hr);
\r
329 float width = wr*r_inv;
\r
330 float height = hr*r_inv;
\r
332 return std::make_pair(width, height);
\r
337 struct ogl_consumer_proxy : public core::frame_consumer
\r
339 size_t screen_index_;
\r
340 caspar::stretch stretch_;
\r
344 std::unique_ptr<ogl_consumer> consumer_;
\r
348 ogl_consumer_proxy(size_t screen_index, stretch stretch, bool windowed, bool key_only)
\r
349 : screen_index_(screen_index)
\r
350 , stretch_(stretch)
\r
351 , windowed_(windowed)
\r
352 , key_only_(key_only){}
\r
354 virtual void initialize(const core::video_format_desc& format_desc)
\r
356 consumer_.reset(new ogl_consumer(screen_index_, stretch_, windowed_, format_desc));
\r
359 virtual bool send(const safe_ptr<core::read_frame>& frame)
\r
361 consumer_->send(frame);
\r
365 virtual std::wstring print() const
\r
367 return consumer_->print();
\r
370 virtual bool key_only() const
\r
375 virtual bool has_synchronization_clock() const
\r
380 virtual const core::video_format_desc& get_video_format_desc() const
\r
382 return consumer_->get_video_format_desc();
\r
386 safe_ptr<core::frame_consumer> create_ogl_consumer(const std::vector<std::wstring>& params)
\r
388 if(params.size() < 1 || params[0] != L"SCREEN")
\r
389 return core::frame_consumer::empty();
\r
391 size_t screen_index = 0;
\r
392 stretch stretch = stretch::fill;
\r
393 bool windowed = true;
\r
395 if(params.size() > 1)
\r
396 screen_index = lexical_cast_or_default<int>(params[2], screen_index);
\r
398 if(params.size() > 2)
\r
399 windowed = lexical_cast_or_default<bool>(params[3], windowed);
\r
401 bool key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();
\r
403 return make_safe<ogl_consumer_proxy>(screen_index, stretch, windowed, key_only);
\r
406 safe_ptr<core::frame_consumer> create_ogl_consumer(const boost::property_tree::ptree& ptree)
\r
408 size_t screen_index = ptree.get("device", 0);
\r
409 bool windowed =ptree.get("windowed", true);
\r
411 stretch stretch = stretch::fill;
\r
412 auto key_str = ptree.get("stretch", "default");
\r
413 if(key_str == "uniform")
\r
414 stretch = stretch::uniform;
\r
415 else if(key_str == "uniform_to_fill")
\r
416 stretch = stretch::uniform_to_fill;
\r
418 bool key_only = ptree.get("key-only", false);
\r
420 return make_safe<ogl_consumer_proxy>(screen_index, stretch, windowed, key_only);
\r