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
43 #include <GL/glxext.h>
46 #ifdef DCPOMATIC_WINDOWS
49 #include <GL/wglext.h>
53 using boost::shared_ptr;
54 using boost::optional;
58 check_gl_error (char const * last)
60 GLenum const e = glGetError ();
61 if (e != GL_NO_ERROR) {
62 throw GLError (last, e);
67 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
69 , _have_storage (false)
70 , _vsync_enabled (false)
74 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
75 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
76 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
77 _canvas->Bind (wxEVT_CREATE, boost::bind(&GLVideoView::create, this));
79 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
80 _timer.reset (new wxTimer(_canvas));
83 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
84 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
86 Display* dpy = wxGetX11Display();
87 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
88 _vsync_enabled = true;
92 #ifdef DCPOMATIC_WINDOWS
93 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
95 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
98 _vsync_enabled = true;
106 GLint swapInterval = 1;
107 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
108 _vsync_enabled = true;
111 glGenTextures (1, &_id);
112 check_gl_error ("glGenTextures");
113 glBindTexture (GL_TEXTURE_2D, _id);
114 check_gl_error ("glBindTexture");
115 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
116 check_gl_error ("glPixelStorei");
119 GLVideoView::~GLVideoView ()
121 boost::this_thread::disable_interruption dis;
124 _thread.interrupt ();
128 glDeleteTextures (1, &_id);
132 GLVideoView::check_for_butler_errors ()
134 if (!_viewer->butler()) {
139 _viewer->butler()->rethrow ();
140 } catch (DecodeError& e) {
141 error_dialog (get(), e.what());
147 GLVideoView::update ()
150 boost::mutex::scoped_lock lm (_canvas_mutex);
151 if (!_canvas->IsShownOnScreen()) {
159 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
161 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162 check_gl_error ("glClear");
164 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
165 check_gl_error ("glClearColor");
166 glEnable (GL_TEXTURE_2D);
167 check_gl_error ("glEnable GL_TEXTURE_2D");
169 check_gl_error ("glEnable GL_BLEND");
170 glDisable (GL_DEPTH_TEST);
171 check_gl_error ("glDisable GL_DEPTH_TEST");
172 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
176 boost::mutex::scoped_lock lm (_canvas_mutex);
177 canvas_size = _canvas->GetSize ();
180 if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
184 glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
185 check_gl_error ("glViewport");
186 glMatrixMode (GL_PROJECTION);
189 DCPOMATIC_DISABLE_WARNINGS
190 gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
191 DCPOMATIC_ENABLE_WARNINGS
192 check_gl_error ("gluOrtho2d");
193 glMatrixMode (GL_MODELVIEW);
196 glTranslatef (0, 0, 0);
198 dcp::Size const out_size = _viewer->out_size ();
201 /* Render our image (texture) */
204 glVertex2f (0, _size->height);
206 glVertex2f (_size->width, _size->height);
208 glVertex2f (_size->width, 0);
213 /* No image, so just fill with black */
215 glColor3ub (0, 0, 0);
217 glVertex2f (out_size.width, 0);
218 glVertex2f (out_size.width, out_size.height);
219 glVertex2f (0, out_size.height);
224 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
226 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
227 glColor3ub (240, 240, 240);
228 glVertex2f (out_size.width, 0);
229 glVertex2f (canvas_size.GetWidth(), 0);
230 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
231 glVertex2f (out_size.width, canvas_size.GetHeight());
233 glColor3ub (255, 255, 255);
236 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
237 glColor3ub (240, 240, 240);
238 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
241 glVertex2f (canvas_size.GetWidth(), 0);
242 glVertex2f (canvas_size.GetWidth(), gap);
246 glVertex2f (0, gap + out_size.height + 1);
247 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
248 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
249 glVertex2f (0, 2 * gap + out_size.height + 2);
251 glColor3ub (255, 255, 255);
254 if (_viewer->outline_content()) {
255 glColor3ub (255, 0, 0);
256 glBegin (GL_LINE_LOOP);
257 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
258 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
259 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
260 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
262 glColor3ub (255, 255, 255);
266 check_gl_error ("glFlush");
268 boost::mutex::scoped_lock lm (_canvas_mutex);
269 _canvas->SwapBuffers();
273 GLVideoView::set_image (shared_ptr<const Image> image)
276 _size = optional<dcp::Size>();
280 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
281 DCPOMATIC_ASSERT (!image->aligned());
283 if (image->size() != _size) {
284 _have_storage = false;
287 _size = image->size ();
288 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
289 check_gl_error ("glPixelStorei");
291 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
292 check_gl_error ("glTexSubImage2D");
294 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
295 _have_storage = true;
296 check_gl_error ("glTexImage2D");
299 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
300 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
301 check_gl_error ("glTexParameteri");
303 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
304 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
305 check_gl_error ("glTexParameterf");
309 GLVideoView::start ()
313 boost::mutex::scoped_lock lm (_playing_mutex);
315 _thread_work_condition.notify_all ();
321 boost::mutex::scoped_lock lm (_playing_mutex);
327 GLVideoView::thread_playing ()
329 if (length() != dcpomatic::DCPTime()) {
330 dcpomatic::DCPTime const next = position() + one_video_frame();
332 if (next >= length()) {
333 _viewer->finished ();
337 get_next_frame (false);
338 set_image_and_draw ();
342 optional<int> n = time_until_next_frame();
346 get_next_frame (true);
353 GLVideoView::set_image_and_draw ()
355 shared_ptr<PlayerVideo> pv = player_video().first;
357 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
358 draw (pv->inter_position(), pv->inter_size());
364 GLVideoView::thread ()
368 boost::mutex::scoped_lock lm (_canvas_mutex);
369 _context = new wxGLContext (_canvas); //local
370 _canvas->SetCurrent (*_context);
374 boost::mutex::scoped_lock lm (_playing_mutex);
375 while (!_playing && !_one_shot) {
376 _thread_work_condition.wait (lm);
382 } else if (_one_shot) {
384 set_image_and_draw ();
387 boost::this_thread::interruption_point ();
388 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
391 /* XXX: leaks _context, but that seems preferable to deleting it here
392 * without also deleting the wxGLCanvas.
395 catch (boost::thread_interrupted& e)
401 GLVideoView::display_next_frame (bool non_blocking)
403 bool const r = get_next_frame (non_blocking);
409 GLVideoView::request_one_shot ()
411 boost::mutex::scoped_lock lm (_playing_mutex);
413 _thread_work_condition.notify_all ();
417 GLVideoView::create ()
419 if (!_thread.joinable()) {
420 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));