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(&GLVideoView::size_changed, this, _1));
81 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
82 _timer.reset (new wxTimer(_canvas));
88 GLVideoView::size_changed (wxSizeEvent const& ev)
90 _canvas_size = ev.GetSize ();
96 GLVideoView::~GLVideoView ()
98 boost::this_thread::disable_interruption dis;
101 _thread.interrupt ();
105 glDeleteTextures (1, &_id);
109 GLVideoView::check_for_butler_errors ()
111 if (!_viewer->butler()) {
116 _viewer->butler()->rethrow ();
117 } catch (DecodeError& e) {
118 error_dialog (get(), e.what());
119 } catch (dcp::ReadError& e) {
120 error_dialog (get(), wxString::Format(_("Could not read DCP: %s"), std_to_wx(e.what())));
125 /** Called from the UI thread */
127 GLVideoView::update ()
129 if (!_canvas->IsShownOnScreen()) {
134 /* macOS gives errors if we don't do this (and therefore [NSOpenGLContext setView:]) from the main thread */
138 if (!_thread.joinable()) {
139 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
147 GLVideoView::ensure_context ()
150 _context = new wxGLContext (_canvas);
151 _canvas->SetCurrent (*_context);
156 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
158 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
159 check_gl_error ("glClear");
161 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
162 check_gl_error ("glClearColor");
163 glEnable (GL_TEXTURE_2D);
164 check_gl_error ("glEnable GL_TEXTURE_2D");
166 check_gl_error ("glEnable GL_BLEND");
167 glDisable (GL_DEPTH_TEST);
168 check_gl_error ("glDisable GL_DEPTH_TEST");
169 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
171 auto const size = _canvas_size.load();
172 int const width = size.GetWidth();
173 int const height = size.GetHeight();
175 if (width < 64 || height < 0) {
179 glViewport (0, 0, width, height);
180 check_gl_error ("glViewport");
181 glMatrixMode (GL_PROJECTION);
184 DCPOMATIC_DISABLE_WARNINGS
185 gluOrtho2D (0, width, height, 0);
186 DCPOMATIC_ENABLE_WARNINGS
187 check_gl_error ("gluOrtho2d");
188 glMatrixMode (GL_MODELVIEW);
191 glTranslatef (0, 0, 0);
193 dcp::Size const out_size = _viewer->out_size ();
196 /* Render our image (texture) */
199 glVertex2f (0, _size->height);
201 glVertex2f (_size->width, _size->height);
203 glVertex2f (_size->width, 0);
208 /* No image, so just fill with black */
210 glColor3ub (0, 0, 0);
212 glVertex2f (out_size.width, 0);
213 glVertex2f (out_size.width, out_size.height);
214 glVertex2f (0, out_size.height);
219 if (!_viewer->pad_black() && out_size.width < width) {
221 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
222 glColor3ub (240, 240, 240);
223 glVertex2f (out_size.width, 0);
224 glVertex2f (width, 0);
225 glVertex2f (width, height);
226 glVertex2f (out_size.width, height);
228 glColor3ub (255, 255, 255);
231 if (!_viewer->pad_black() && out_size.height < height) {
232 glColor3ub (240, 240, 240);
233 int const gap = (height - out_size.height) / 2;
236 glVertex2f (width, 0);
237 glVertex2f (width, gap);
241 glVertex2f (0, gap + out_size.height + 1);
242 glVertex2f (width, gap + out_size.height + 1);
243 glVertex2f (width, 2 * gap + out_size.height + 2);
244 glVertex2f (0, 2 * gap + out_size.height + 2);
246 glColor3ub (255, 255, 255);
249 if (_viewer->outline_content()) {
250 glColor3ub (255, 0, 0);
251 glBegin (GL_LINE_LOOP);
252 glVertex2f (inter_position.x, inter_position.y + (height - out_size.height) / 2);
253 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (height - out_size.height) / 2);
254 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (height - out_size.height) / 2 + inter_size.height);
255 glVertex2f (inter_position.x, inter_position.y + (height - out_size.height) / 2 + inter_size.height);
257 glColor3ub (255, 255, 255);
261 check_gl_error ("glFlush");
263 _canvas->SwapBuffers();
267 GLVideoView::set_image (shared_ptr<const Image> image)
270 _size = optional<dcp::Size>();
274 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
275 DCPOMATIC_ASSERT (!image->aligned());
277 if (image->size() != _size) {
278 _have_storage = false;
281 _size = image->size ();
282 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
283 check_gl_error ("glPixelStorei");
285 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
286 check_gl_error ("glTexSubImage2D");
288 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
289 _have_storage = true;
290 check_gl_error ("glTexImage2D");
293 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
294 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
295 check_gl_error ("glTexParameteri");
297 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
298 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
299 check_gl_error ("glTexParameterf");
303 GLVideoView::start ()
307 boost::mutex::scoped_lock lm (_playing_mutex);
309 _thread_work_condition.notify_all ();
315 boost::mutex::scoped_lock lm (_playing_mutex);
321 GLVideoView::thread_playing ()
323 if (length() != dcpomatic::DCPTime()) {
324 dcpomatic::DCPTime const next = position() + one_video_frame();
326 if (next >= length()) {
327 _viewer->finished ();
331 get_next_frame (false);
332 set_image_and_draw ();
336 optional<int> n = time_until_next_frame();
340 get_next_frame (true);
347 GLVideoView::set_image_and_draw ()
349 shared_ptr<PlayerVideo> pv = player_video().first;
351 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
352 draw (pv->inter_position(), pv->inter_size());
353 _viewer->image_changed (pv);
359 GLVideoView::thread ()
362 start_of_thread ("GLVideoView");
364 #if defined(DCPOMATIC_OSX)
365 /* Without this we see errors like
366 * ../src/osx/cocoa/glcanvas.mm(194): assert ""context"" failed in SwapBuffers(): should have current context [in thread 700006970000]
368 WXGLSetCurrentContext (_context->GetWXGLContext());
370 /* We must call this here on Linux otherwise we get no image (for reasons
371 * that aren't clear). However, doing ensure_context() from this thread
373 * "[NSOpenGLContext setView:] must be called from the main thread".
378 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
379 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
381 Display* dpy = wxGetX11Display();
382 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
383 _vsync_enabled = true;
387 #ifdef DCPOMATIC_WINDOWS
388 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
390 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
393 _vsync_enabled = true;
401 GLint swapInterval = 1;
402 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
403 _vsync_enabled = true;
406 glGenTextures (1, &_id);
407 check_gl_error ("glGenTextures");
408 glBindTexture (GL_TEXTURE_2D, _id);
409 check_gl_error ("glBindTexture");
410 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
411 check_gl_error ("glPixelStorei");
414 boost::mutex::scoped_lock lm (_playing_mutex);
415 while (!_playing && !_one_shot) {
416 _thread_work_condition.wait (lm);
422 } else if (_one_shot) {
424 set_image_and_draw ();
427 boost::this_thread::interruption_point ();
428 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
431 /* XXX: leaks _context, but that seems preferable to deleting it here
432 * without also deleting the wxGLCanvas.
435 catch (boost::thread_interrupted& e)
441 VideoView::NextFrameResult
442 GLVideoView::display_next_frame (bool non_blocking)
444 NextFrameResult const r = get_next_frame (non_blocking);
451 GLVideoView::request_one_shot ()
453 boost::mutex::scoped_lock lm (_playing_mutex);
455 _thread_work_condition.notify_all ();