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)
64 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
65 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
66 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
67 _canvas->Bind (wxEVT_CREATE, boost::bind(&GLVideoView::create, this));
69 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
70 _timer.reset (new wxTimer(_canvas));
73 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
74 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
76 Display* dpy = wxGetX11Display();
77 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
78 _vsync_enabled = true;
82 #ifdef DCPOMATIC_WINDOWS
83 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
85 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
88 _vsync_enabled = true;
96 GLint swapInterval = 1;
97 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
98 _vsync_enabled = true;
101 glGenTextures (1, &_id);
102 glBindTexture (GL_TEXTURE_2D, _id);
103 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
106 GLVideoView::~GLVideoView ()
108 _thread->interrupt ();
112 glDeleteTextures (1, &_id);
116 GLVideoView::check_for_butler_errors ()
119 _viewer->butler()->rethrow ();
120 } catch (DecodeError& e) {
121 error_dialog (get(), e.what());
126 check_gl_error (char const * last)
128 GLenum const e = glGetError ();
129 if (e != GL_NO_ERROR) {
130 throw GLError (last, e);
135 GLVideoView::update ()
138 boost::mutex::scoped_lock lm (_canvas_mutex);
139 if (!_canvas->IsShownOnScreen()) {
147 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
149 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
150 check_gl_error ("glClear");
152 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
153 check_gl_error ("glClearColor");
154 glEnable (GL_TEXTURE_2D);
155 check_gl_error ("glEnable GL_TEXTURE_2D");
157 check_gl_error ("glEnable GL_BLEND");
158 glDisable (GL_DEPTH_TEST);
159 check_gl_error ("glDisable GL_DEPTH_TEST");
160 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
164 boost::mutex::scoped_lock lm (_canvas_mutex);
165 canvas_size = _canvas->GetSize ();
168 if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
172 glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
173 check_gl_error ("glViewport");
174 glMatrixMode (GL_PROJECTION);
177 gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
178 check_gl_error ("gluOrtho2d");
179 glMatrixMode (GL_MODELVIEW);
182 glTranslatef (0, 0, 0);
184 dcp::Size const out_size = _viewer->out_size ();
187 /* Render our image (texture) */
190 glVertex2f (0, _size->height);
192 glVertex2f (_size->width, _size->height);
194 glVertex2f (_size->width, 0);
199 /* No image, so just fill with black */
201 glColor3ub (0, 0, 0);
203 glVertex2f (out_size.width, 0);
204 glVertex2f (out_size.width, out_size.height);
205 glVertex2f (0, out_size.height);
210 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
212 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
213 glColor3ub (240, 240, 240);
214 glVertex2f (out_size.width, 0);
215 glVertex2f (canvas_size.GetWidth(), 0);
216 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
217 glVertex2f (out_size.width, canvas_size.GetHeight());
219 glColor3ub (255, 255, 255);
222 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
223 glColor3ub (240, 240, 240);
224 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
227 glVertex2f (canvas_size.GetWidth(), 0);
228 glVertex2f (canvas_size.GetWidth(), gap);
232 glVertex2f (0, gap + out_size.height + 1);
233 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
234 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
235 glVertex2f (0, 2 * gap + out_size.height + 2);
237 glColor3ub (255, 255, 255);
240 if (_viewer->outline_content()) {
241 glColor3ub (255, 0, 0);
242 glBegin (GL_LINE_LOOP);
243 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
244 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
245 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
246 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
248 glColor3ub (255, 255, 255);
253 boost::mutex::scoped_lock lm (_canvas_mutex);
254 _canvas->SwapBuffers();
258 GLVideoView::set_image (shared_ptr<const Image> image)
261 _size = optional<dcp::Size>();
265 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
266 DCPOMATIC_ASSERT (!image->aligned());
268 if (image->size() != _size) {
269 _have_storage = false;
272 _size = image->size ();
273 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
274 check_gl_error ("glPixelStorei");
276 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
277 check_gl_error ("glTexSubImage2D");
279 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
280 _have_storage = true;
281 check_gl_error ("glTexImage2D");
284 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
285 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
286 check_gl_error ("glTexParameteri");
288 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
289 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
290 check_gl_error ("glTexParameterf");
294 GLVideoView::start ()
298 boost::mutex::scoped_lock lm (_playing_mutex);
300 _playing_condition.notify_all ();
306 boost::mutex::scoped_lock lm (_playing_mutex);
311 GLVideoView::thread ()
315 boost::mutex::scoped_lock lm (_canvas_mutex);
316 _context = new wxGLContext (_canvas); //local
317 _canvas->SetCurrent (*_context);
321 boost::mutex::scoped_lock lm (_playing_mutex);
322 while (!_playing && !_one_shot) {
323 _playing_condition.wait (lm);
328 Position<int> inter_position;
329 dcp::Size inter_size;
330 if (length() != dcpomatic::DCPTime()) {
331 dcpomatic::DCPTime const next = position() + one_video_frame();
333 if (next >= length()) {
334 _viewer->finished ();
338 get_next_frame (false);
339 shared_ptr<PlayerVideo> pv = player_video().first;
341 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
342 inter_position = pv->inter_position();
343 inter_size = pv->inter_size();
346 draw (inter_position, inter_size);
349 optional<int> n = time_until_next_frame();
353 get_next_frame (true);
357 boost::this_thread::interruption_point ();
358 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
361 /* XXX: leaks _context, but that seems preferable to deleting it here
362 * without also deleting the wxGLCanvas.
365 catch (boost::thread_interrupted& e)
371 GLVideoView::display_next_frame (bool non_blocking)
373 bool const r = get_next_frame (non_blocking);
379 GLVideoView::request_one_shot ()
381 boost::mutex::scoped_lock lm (_playing_mutex);
383 _playing_condition.notify_all ();
387 GLVideoView::create ()
390 _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));