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 "../../StdAfx.h"
\r
23 #include "ogl_consumer.h"
\r
25 #include <core/video_format.h>
\r
26 #include <mixer/frame/read_frame.h>
\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
34 #include <boost/thread.hpp>
\r
37 #include <SFML/Window.hpp>
\r
39 #include <windows.h>
\r
41 #include <algorithm>
\r
43 namespace caspar { namespace core {
\r
45 struct ogl_consumer::implementation : boost::noncopyable
\r
47 safe_ptr<diagnostics::graph> graph_;
\r
50 boost::unique_future<void> active_;
\r
59 std::array<GLuint, 2> pbos_;
\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
68 const stretch stretch_;
\r
69 video_format_desc format_desc_;
\r
75 implementation(unsigned int screen_index, stretch stretch, bool windowed)
\r
77 , windowed_(windowed)
\r
81 , screen_index_(screen_index)
\r
82 , graph_(diagnostics::create_graph("ogl_consumer"))
\r
84 graph_->line("frame_time_target", 0.5, 0.5f, 0.0f, 0.0f);
\r
85 graph_->color("frame_time", 1.0f, 0.0f, 0.0f);
\r
87 CASPAR_LOG(info) << "Sucessfully started ogl_consumer";
\r
92 CASPAR_LOG(info) << "Sucessfully ended ogl_consumer";
\r
95 void initialize(const video_format_desc& format_desc)
\r
97 format_desc_ = format_desc;
\r
99 screen_width_ = format_desc.width;
\r
100 screen_height_ = format_desc.height;
\r
102 DISPLAY_DEVICE d_device;
\r
103 memset(&d_device, 0, sizeof(d_device));
\r
104 d_device.cb = sizeof(d_device);
\r
106 std::vector<DISPLAY_DEVICE> displayDevices;
\r
107 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
\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
114 if(screen_index_ >= displayDevices.size())
\r
115 BOOST_THROW_EXCEPTION(out_of_range() << arg_name_info("screen_index_"));
\r
118 memset(&devmode, 0, sizeof(devmode));
\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
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
129 BOOST_THROW_EXCEPTION(not_supported() << msg_info("OGLConsumer doesn't support non-Win32 fullscreen"));
\r
131 if(screen_index != 0)
\r
132 CASPAR_LOG(warning) << "OGLConsumer only supports screen_index=0 for non-Win32";
\r
136 executor_.invoke([=]
\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
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
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
160 wSize_ = target_ratio.first;
\r
161 hSize_ = target_ratio.second;
\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
172 GL(glGenBuffers(2, pbos_.data()));
\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
180 active_ = executor_.begin_invoke([]{});
\r
183 std::pair<float, float> None()
\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
188 return std::make_pair(width, height);
\r
191 std::pair<float, float> Uniform()
\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
197 return std::make_pair(width, height);
\r
200 std::pair<float, float> Fill()
\r
202 return std::make_pair(1.0f, 1.0f);
\r
205 std::pair<float, float> UniformToFill()
\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
211 float width = wr*r_inv;
\r
212 float height = hr*r_inv;
\r
214 return std::make_pair(width, height);
\r
217 void render(const safe_ptr<const read_frame>& frame)
\r
219 glBindTexture(GL_TEXTURE_2D, texture_);
\r
220 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]);
\r
222 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0);
\r
224 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]);
\r
226 glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW);
\r
228 auto ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
\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
235 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
\r
237 GL(glClear(GL_COLOR_BUFFER_BIT));
\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
245 glBindTexture(GL_TEXTURE_2D, 0);
\r
247 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());
\r
250 void send(const safe_ptr<const read_frame>& frame)
\r
253 active_ = executor_.begin_invoke([=]
\r
255 perf_timer_.reset();
\r
257 while(window_.GetEvent(e)){}
\r
260 graph_->update("frame_time", static_cast<float>(perf_timer_.elapsed()/format_desc_.interval*0.5));
\r
264 size_t buffer_depth() const{return 2;}
\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
273 safe_ptr<frame_consumer> create_ogl_consumer(const std::vector<std::wstring>& params)
\r
275 if(params.size() < 1 || params[0] != L"OGL")
\r
276 return frame_consumer::empty();
\r
278 unsigned int screen_index = 0;
\r
279 stretch stretch = stretch::fill;
\r
280 bool windowed = true;
\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
287 return make_safe<ogl_consumer>(screen_index, stretch, windowed);
\r