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/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 std::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)
72 , _have_storage (false)
73 , _vsync_enabled (false)
77 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
78 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
79 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
81 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
82 _timer.reset (new wxTimer(_canvas));
86 GLVideoView::~GLVideoView ()
88 boost::this_thread::disable_interruption dis;
95 glDeleteTextures (1, &_id);
99 GLVideoView::check_for_butler_errors ()
101 if (!_viewer->butler()) {
106 _viewer->butler()->rethrow ();
107 } catch (DecodeError& e) {
108 error_dialog (get(), e.what());
109 } catch (dcp::ReadError& e) {
110 error_dialog (get(), wxString::Format(_("Could not read DCP: %s"), std_to_wx(e.what())));
116 GLVideoView::update ()
119 boost::mutex::scoped_lock lm (_canvas_mutex);
120 if (!_canvas->IsShownOnScreen()) {
125 /* macOS gives errors if we don't do this (and therefore [NSOpenGLContext setView:]) from the main thread */
130 if (!_thread.joinable()) {
131 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
139 GLVideoView::ensure_context ()
142 _context = new wxGLContext (_canvas);
143 _canvas->SetCurrent (*_context);
148 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
150 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
151 check_gl_error ("glClear");
153 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
154 check_gl_error ("glClearColor");
155 glEnable (GL_TEXTURE_2D);
156 check_gl_error ("glEnable GL_TEXTURE_2D");
158 check_gl_error ("glEnable GL_BLEND");
159 glDisable (GL_DEPTH_TEST);
160 check_gl_error ("glDisable GL_DEPTH_TEST");
161 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165 boost::mutex::scoped_lock lm (_canvas_mutex);
167 canvas_size = _canvas->GetSize ();
171 if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
175 glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
176 check_gl_error ("glViewport");
177 glMatrixMode (GL_PROJECTION);
180 DCPOMATIC_DISABLE_WARNINGS
181 gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
182 DCPOMATIC_ENABLE_WARNINGS
183 check_gl_error ("gluOrtho2d");
184 glMatrixMode (GL_MODELVIEW);
187 glTranslatef (0, 0, 0);
189 dcp::Size const out_size = _viewer->out_size ();
192 /* Render our image (texture) */
195 glVertex2f (0, _size->height);
197 glVertex2f (_size->width, _size->height);
199 glVertex2f (_size->width, 0);
204 /* No image, so just fill with black */
206 glColor3ub (0, 0, 0);
208 glVertex2f (out_size.width, 0);
209 glVertex2f (out_size.width, out_size.height);
210 glVertex2f (0, out_size.height);
215 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
217 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
218 glColor3ub (240, 240, 240);
219 glVertex2f (out_size.width, 0);
220 glVertex2f (canvas_size.GetWidth(), 0);
221 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
222 glVertex2f (out_size.width, canvas_size.GetHeight());
224 glColor3ub (255, 255, 255);
227 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
228 glColor3ub (240, 240, 240);
229 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
232 glVertex2f (canvas_size.GetWidth(), 0);
233 glVertex2f (canvas_size.GetWidth(), gap);
237 glVertex2f (0, gap + out_size.height + 1);
238 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
239 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
240 glVertex2f (0, 2 * gap + out_size.height + 2);
242 glColor3ub (255, 255, 255);
245 if (_viewer->outline_content()) {
246 glColor3ub (255, 0, 0);
247 glBegin (GL_LINE_LOOP);
248 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
249 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
250 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
251 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
253 glColor3ub (255, 255, 255);
257 check_gl_error ("glFlush");
259 boost::mutex::scoped_lock lm (_canvas_mutex);
260 _canvas->SwapBuffers();
264 GLVideoView::set_image (shared_ptr<const Image> image)
267 _size = optional<dcp::Size>();
271 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
272 DCPOMATIC_ASSERT (!image->aligned());
274 if (image->size() != _size) {
275 _have_storage = false;
278 _size = image->size ();
279 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
280 check_gl_error ("glPixelStorei");
282 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
283 check_gl_error ("glTexSubImage2D");
285 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
286 _have_storage = true;
287 check_gl_error ("glTexImage2D");
290 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
291 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
292 check_gl_error ("glTexParameteri");
294 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
295 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
296 check_gl_error ("glTexParameterf");
300 GLVideoView::start ()
304 boost::mutex::scoped_lock lm (_playing_mutex);
306 _thread_work_condition.notify_all ();
312 boost::mutex::scoped_lock lm (_playing_mutex);
318 GLVideoView::thread_playing ()
320 if (length() != dcpomatic::DCPTime()) {
321 dcpomatic::DCPTime const next = position() + one_video_frame();
323 if (next >= length()) {
324 _viewer->finished ();
328 get_next_frame (false);
329 set_image_and_draw ();
333 optional<int> n = time_until_next_frame();
337 get_next_frame (true);
344 GLVideoView::set_image_and_draw ()
346 shared_ptr<PlayerVideo> pv = player_video().first;
348 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
349 draw (pv->inter_position(), pv->inter_size());
350 _viewer->image_changed (pv);
356 GLVideoView::thread ()
360 boost::mutex::scoped_lock lm (_canvas_mutex);
362 #if defined(DCPOMATIC_OSX)
363 /* Without this we see errors like
364 * ../src/osx/cocoa/glcanvas.mm(194): assert ""context"" failed in SwapBuffers(): should have current context [in thread 700006970000]
366 WXGLSetCurrentContext (_context->GetWXGLContext());
368 /* We must call this here on Linux otherwise we get no image (for reasons
369 * that aren't clear). However, doing ensure_context() from this thread
371 * "[NSOpenGLContext setView:] must be called from the main thread".
377 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
378 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
380 Display* dpy = wxGetX11Display();
381 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
382 _vsync_enabled = true;
386 #ifdef DCPOMATIC_WINDOWS
387 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
389 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
392 _vsync_enabled = true;
400 GLint swapInterval = 1;
401 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
402 _vsync_enabled = true;
405 glGenTextures (1, &_id);
406 check_gl_error ("glGenTextures");
407 glBindTexture (GL_TEXTURE_2D, _id);
408 check_gl_error ("glBindTexture");
409 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
410 check_gl_error ("glPixelStorei");
413 boost::mutex::scoped_lock lm (_playing_mutex);
414 while (!_playing && !_one_shot) {
415 _thread_work_condition.wait (lm);
421 } else if (_one_shot) {
423 set_image_and_draw ();
426 boost::this_thread::interruption_point ();
427 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
430 /* XXX: leaks _context, but that seems preferable to deleting it here
431 * without also deleting the wxGLCanvas.
434 catch (boost::thread_interrupted& e)
440 VideoView::NextFrameResult
441 GLVideoView::display_next_frame (bool non_blocking)
443 NextFrameResult const r = get_next_frame (non_blocking);
450 GLVideoView::request_one_shot ()
452 boost::mutex::scoped_lock lm (_playing_mutex);
454 _thread_work_condition.notify_all ();