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
23 #include <windows.h>
\r
25 #include <SFML/Window.hpp>
\r
27 #include "ogl_consumer.h"
\r
29 #include <core/video_format.h>
\r
30 #include <core/consumer/frame/read_frame.h>
\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
38 #include <boost/thread.hpp>
\r
40 #include <algorithm>
\r
45 struct ogl_consumer::implementation : boost::noncopyable
\r
48 printer parent_printer_;
\r
49 boost::unique_future<void> active_;
\r
58 std::array<GLuint, 2> pbos_;
\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
67 const stretch stretch_;
\r
68 core::video_format_desc format_desc_;
\r
72 safe_ptr<diagnostics::graph> graph_;
\r
77 implementation(unsigned int screen_index, stretch stretch, bool windowed)
\r
79 , windowed_(windowed)
\r
83 , screen_index_(screen_index)
\r
84 , graph_(diagnostics::create_graph(narrow(print())))
\r
85 , executor_(print())
\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
93 CASPAR_LOG(info) << print() << L" Shutting down.";
\r
96 void initialize(const core::video_format_desc& format_desc, const printer& parent_printer)
\r
98 if(!GLEE_VERSION_2_1)
\r
99 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));
\r
101 format_desc_ = format_desc;
\r
102 parent_printer_ = parent_printer;
\r
104 screen_width_ = format_desc.width;
\r
105 screen_height_ = format_desc.height;
\r
107 DISPLAY_DEVICE d_device;
\r
108 memset(&d_device, 0, sizeof(d_device));
\r
109 d_device.cb = sizeof(d_device);
\r
111 std::vector<DISPLAY_DEVICE> displayDevices;
\r
112 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
\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
119 if(screen_index_ >= displayDevices.size())
\r
120 BOOST_THROW_EXCEPTION(out_of_range() << arg_name_info("screen_index_") << msg_info(narrow(print())));
\r
123 memset(&devmode, 0, sizeof(devmode));
\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
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
134 BOOST_THROW_EXCEPTION(not_supported() << msg_info(narrow(print() + " doesn't support non-Win32 fullscreen"));
\r
136 if(screen_index != 0)
\r
137 CASPAR_LOG(warning) << print() << " only supports screen_index=0 for non-Win32";
\r
140 executor_.invoke([=]
\r
142 window_.Create(sf::VideoMode(format_desc_.width, format_desc_.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
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
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
164 wSize_ = target_ratio.first;
\r
165 hSize_ = target_ratio.second;
\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
176 GL(glGenBuffers(2, pbos_.data()));
\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
184 active_ = executor_.begin_invoke([]{});
\r
185 CASPAR_LOG(info) << print() << " Sucessfully initialized.";
\r
188 std::pair<float, float> None()
\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
193 return std::make_pair(width, height);
\r
196 std::pair<float, float> Uniform()
\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
202 return std::make_pair(width, height);
\r
205 std::pair<float, float> Fill()
\r
207 return std::make_pair(1.0f, 1.0f);
\r
210 std::pair<float, float> UniformToFill()
\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
216 float width = wr*r_inv;
\r
217 float height = hr*r_inv;
\r
219 return std::make_pair(width, height);
\r
222 void render(const safe_ptr<const core::read_frame>& frame)
\r
224 glBindTexture(GL_TEXTURE_2D, texture_);
\r
225 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]);
\r
227 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
\r
229 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]);
\r
231 glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW);
\r
233 auto ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
\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
240 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
\r
242 GL(glClear(GL_COLOR_BUFFER_BIT));
\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
250 glBindTexture(GL_TEXTURE_2D, 0);
\r
252 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());
\r
254 clock_.tick(1.0/format_desc_.fps);
\r
257 void send(const safe_ptr<const core::read_frame>& frame)
\r
260 active_ = executor_.begin_invoke([=]
\r
262 perf_timer_.reset();
\r
264 while(window_.GetEvent(e)){}
\r
267 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/format_desc_.interval*0.5));
\r
271 std::wstring print() const
\r
273 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ogl[" + boost::lexical_cast<std::wstring>(screen_index_) + L"]";
\r
276 size_t buffer_depth() const{return 2;}
\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
286 safe_ptr<core::frame_consumer> create_ogl_consumer(const std::vector<std::wstring>& params)
\r
288 if(params.size() < 1 || params[0] != L"OGL")
\r
289 return core::frame_consumer::empty();
\r
291 unsigned int screen_index = 0;
\r
292 stretch stretch = stretch::fill;
\r
293 bool windowed = true;
\r
295 if(params.size() > 1)
\r
296 screen_index = lexical_cast_or_default<int>(params[2], screen_index);
\r
298 if(params.size() > 2)
\r
299 windowed = lexical_cast_or_default<bool>(params[3], windowed);
\r
301 return make_safe<ogl_consumer>(screen_index, stretch, windowed);
\r