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;
56 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
58 , _have_storage (false)
59 , _vsync_enabled (false)
63 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
64 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
65 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
66 _canvas->Bind (wxEVT_CREATE, boost::bind(&GLVideoView::create, this));
68 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
69 _timer.reset (new wxTimer(_canvas));
72 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
73 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
75 Display* dpy = wxGetX11Display();
76 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
77 _vsync_enabled = true;
81 #ifdef DCPOMATIC_WINDOWS
82 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
84 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
87 _vsync_enabled = true;
95 GLint swapInterval = 1;
96 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
97 _vsync_enabled = true;
100 glGenTextures (1, &_id);
101 glBindTexture (GL_TEXTURE_2D, _id);
102 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
105 GLVideoView::~GLVideoView ()
108 _thread.interrupt ();
114 glDeleteTextures (1, &_id);
118 GLVideoView::check_for_butler_errors ()
120 if (!_viewer->butler()) {
125 _viewer->butler()->rethrow ();
126 } catch (DecodeError& e) {
127 error_dialog (get(), e.what());
132 check_gl_error (char const * last)
134 GLenum const e = glGetError ();
135 if (e != GL_NO_ERROR) {
136 throw GLError (last, e);
141 GLVideoView::update ()
144 boost::mutex::scoped_lock lm (_canvas_mutex);
145 if (!_canvas->IsShownOnScreen()) {
153 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
155 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
156 check_gl_error ("glClear");
158 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
159 check_gl_error ("glClearColor");
160 glEnable (GL_TEXTURE_2D);
161 check_gl_error ("glEnable GL_TEXTURE_2D");
163 check_gl_error ("glEnable GL_BLEND");
164 glDisable (GL_DEPTH_TEST);
165 check_gl_error ("glDisable GL_DEPTH_TEST");
166 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
170 boost::mutex::scoped_lock lm (_canvas_mutex);
171 canvas_size = _canvas->GetSize ();
174 if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
178 glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
179 check_gl_error ("glViewport");
180 glMatrixMode (GL_PROJECTION);
183 gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
184 check_gl_error ("gluOrtho2d");
185 glMatrixMode (GL_MODELVIEW);
188 glTranslatef (0, 0, 0);
190 dcp::Size const out_size = _viewer->out_size ();
193 /* Render our image (texture) */
196 glVertex2f (0, _size->height);
198 glVertex2f (_size->width, _size->height);
200 glVertex2f (_size->width, 0);
205 /* No image, so just fill with black */
207 glColor3ub (0, 0, 0);
209 glVertex2f (out_size.width, 0);
210 glVertex2f (out_size.width, out_size.height);
211 glVertex2f (0, out_size.height);
216 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
218 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
219 glColor3ub (240, 240, 240);
220 glVertex2f (out_size.width, 0);
221 glVertex2f (canvas_size.GetWidth(), 0);
222 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
223 glVertex2f (out_size.width, canvas_size.GetHeight());
225 glColor3ub (255, 255, 255);
228 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
229 glColor3ub (240, 240, 240);
230 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
233 glVertex2f (canvas_size.GetWidth(), 0);
234 glVertex2f (canvas_size.GetWidth(), gap);
238 glVertex2f (0, gap + out_size.height + 1);
239 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
240 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
241 glVertex2f (0, 2 * gap + out_size.height + 2);
243 glColor3ub (255, 255, 255);
246 if (_viewer->outline_content()) {
247 glColor3ub (255, 0, 0);
248 glBegin (GL_LINE_LOOP);
249 glVertex2f (inter_position.x, 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);
251 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
252 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
254 glColor3ub (255, 255, 255);
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 _playing_condition.notify_all ();
312 boost::mutex::scoped_lock lm (_playing_mutex);
317 GLVideoView::thread ()
321 boost::mutex::scoped_lock lm (_canvas_mutex);
322 _context = new wxGLContext (_canvas); //local
323 _canvas->SetCurrent (*_context);
327 boost::mutex::scoped_lock lm (_playing_mutex);
328 while (!_playing && !_one_shot) {
329 _playing_condition.wait (lm);
334 Position<int> inter_position;
335 dcp::Size inter_size;
336 if (length() != dcpomatic::DCPTime()) {
337 dcpomatic::DCPTime const next = position() + one_video_frame();
339 if (next >= length()) {
340 _viewer->finished ();
344 get_next_frame (false);
345 shared_ptr<PlayerVideo> pv = player_video().first;
347 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
348 inter_position = pv->inter_position();
349 inter_size = pv->inter_size();
352 draw (inter_position, inter_size);
355 optional<int> n = time_until_next_frame();
359 get_next_frame (true);
363 boost::this_thread::interruption_point ();
364 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
367 /* XXX: leaks _context, but that seems preferable to deleting it here
368 * without also deleting the wxGLCanvas.
371 catch (boost::thread_interrupted& e)
377 GLVideoView::display_next_frame (bool non_blocking)
379 bool const r = get_next_frame (non_blocking);
385 GLVideoView::request_one_shot ()
387 boost::mutex::scoped_lock lm (_playing_mutex);
389 _playing_condition.notify_all ();
393 GLVideoView::create ()
395 if (!_thread.joinable()) {
396 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));