2 Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "gl_video_view.h"
22 #include "film_viewer.h"
24 #include "lib/image.h"
25 #include "lib/dcpomatic_assert.h"
26 #include "lib/exceptions.h"
27 #include "lib/cross.h"
28 #include "lib/player_video.h"
29 #include "lib/butler.h"
30 #include <boost/bind.hpp>
34 #include <OpenGL/glu.h>
35 #include <OpenGL/glext.h>
36 #include <OpenGL/CGLTypes.h>
37 #include <OpenGL/OpenGL.h>
40 #ifdef DCPOMATIC_LINUX
45 #ifdef DCPOMATIC_WINDOWS
48 #include <GL/wglext.h>
52 using boost::shared_ptr;
53 using boost::optional;
54 #if BOOST_VERSION >= 106100
55 using namespace boost::placeholders;
60 check_gl_error (char const * last)
62 GLenum const e = glGetError ();
63 if (e != GL_NO_ERROR) {
64 throw GLError (last, e);
69 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
71 , _have_storage (false)
72 , _vsync_enabled (false)
76 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
77 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
78 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
80 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
81 _timer.reset (new wxTimer(_canvas));
85 GLVideoView::~GLVideoView ()
87 boost::this_thread::disable_interruption dis;
94 glDeleteTextures (1, &_id);
98 GLVideoView::check_for_butler_errors ()
100 if (!_viewer->butler()) {
105 _viewer->butler()->rethrow ();
106 } catch (DecodeError& e) {
107 error_dialog (get(), e.what());
113 GLVideoView::update ()
115 if (!_thread.joinable()) {
116 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
120 boost::mutex::scoped_lock lm (_canvas_mutex);
121 if (!_canvas->IsShownOnScreen()) {
129 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
131 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132 check_gl_error ("glClear");
134 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
135 check_gl_error ("glClearColor");
136 glEnable (GL_TEXTURE_2D);
137 check_gl_error ("glEnable GL_TEXTURE_2D");
139 check_gl_error ("glEnable GL_BLEND");
140 glDisable (GL_DEPTH_TEST);
141 check_gl_error ("glDisable GL_DEPTH_TEST");
142 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
146 boost::mutex::scoped_lock lm (_canvas_mutex);
147 canvas_size = _canvas->GetSize ();
150 if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
154 glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
155 check_gl_error ("glViewport");
156 glMatrixMode (GL_PROJECTION);
159 DCPOMATIC_DISABLE_WARNINGS
160 gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
161 DCPOMATIC_ENABLE_WARNINGS
162 check_gl_error ("gluOrtho2d");
163 glMatrixMode (GL_MODELVIEW);
166 glTranslatef (0, 0, 0);
168 dcp::Size const out_size = _viewer->out_size ();
171 /* Render our image (texture) */
174 glVertex2f (0, _size->height);
176 glVertex2f (_size->width, _size->height);
178 glVertex2f (_size->width, 0);
183 /* No image, so just fill with black */
185 glColor3ub (0, 0, 0);
187 glVertex2f (out_size.width, 0);
188 glVertex2f (out_size.width, out_size.height);
189 glVertex2f (0, out_size.height);
194 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
196 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
197 glColor3ub (240, 240, 240);
198 glVertex2f (out_size.width, 0);
199 glVertex2f (canvas_size.GetWidth(), 0);
200 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
201 glVertex2f (out_size.width, canvas_size.GetHeight());
203 glColor3ub (255, 255, 255);
206 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
207 glColor3ub (240, 240, 240);
208 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
211 glVertex2f (canvas_size.GetWidth(), 0);
212 glVertex2f (canvas_size.GetWidth(), gap);
216 glVertex2f (0, gap + out_size.height + 1);
217 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
218 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
219 glVertex2f (0, 2 * gap + out_size.height + 2);
221 glColor3ub (255, 255, 255);
224 if (_viewer->outline_content()) {
225 glColor3ub (255, 0, 0);
226 glBegin (GL_LINE_LOOP);
227 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
228 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
229 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
230 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
232 glColor3ub (255, 255, 255);
236 check_gl_error ("glFlush");
238 boost::mutex::scoped_lock lm (_canvas_mutex);
239 _canvas->SwapBuffers();
243 GLVideoView::set_image (shared_ptr<const Image> image)
246 _size = optional<dcp::Size>();
250 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
251 DCPOMATIC_ASSERT (!image->aligned());
253 if (image->size() != _size) {
254 _have_storage = false;
257 _size = image->size ();
258 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
259 check_gl_error ("glPixelStorei");
261 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
262 check_gl_error ("glTexSubImage2D");
264 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
265 _have_storage = true;
266 check_gl_error ("glTexImage2D");
269 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
270 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
271 check_gl_error ("glTexParameteri");
273 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
274 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
275 check_gl_error ("glTexParameterf");
279 GLVideoView::start ()
283 boost::mutex::scoped_lock lm (_playing_mutex);
285 _thread_work_condition.notify_all ();
291 boost::mutex::scoped_lock lm (_playing_mutex);
297 GLVideoView::thread_playing ()
299 if (length() != dcpomatic::DCPTime()) {
300 dcpomatic::DCPTime const next = position() + one_video_frame();
302 if (next >= length()) {
303 _viewer->finished ();
307 get_next_frame (false);
308 set_image_and_draw ();
312 optional<int> n = time_until_next_frame();
316 get_next_frame (true);
323 GLVideoView::set_image_and_draw ()
325 shared_ptr<PlayerVideo> pv = player_video().first;
327 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
328 draw (pv->inter_position(), pv->inter_size());
334 GLVideoView::thread ()
338 boost::mutex::scoped_lock lm (_canvas_mutex);
339 _context = new wxGLContext (_canvas);
340 _canvas->SetCurrent (*_context);
344 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
345 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
347 Display* dpy = wxGetX11Display();
348 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
349 _vsync_enabled = true;
353 #ifdef DCPOMATIC_WINDOWS
354 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
356 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
359 _vsync_enabled = true;
367 GLint swapInterval = 1;
368 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
369 _vsync_enabled = true;
372 glGenTextures (1, &_id);
373 check_gl_error ("glGenTextures");
374 glBindTexture (GL_TEXTURE_2D, _id);
375 check_gl_error ("glBindTexture");
376 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
377 check_gl_error ("glPixelStorei");
380 boost::mutex::scoped_lock lm (_playing_mutex);
381 while (!_playing && !_one_shot) {
382 _thread_work_condition.wait (lm);
388 } else if (_one_shot) {
390 set_image_and_draw ();
393 boost::this_thread::interruption_point ();
394 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
397 /* XXX: leaks _context, but that seems preferable to deleting it here
398 * without also deleting the wxGLCanvas.
401 catch (boost::thread_interrupted& e)
407 GLVideoView::display_next_frame (bool non_blocking)
409 bool const r = get_next_frame (non_blocking);
415 GLVideoView::request_one_shot ()
417 boost::mutex::scoped_lock lm (_playing_mutex);
419 _thread_work_condition.notify_all ();