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 , _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 ()
107 _thread->interrupt ();
111 glDeleteTextures (1, &_id);
115 GLVideoView::check_for_butler_errors ()
118 _viewer->butler()->rethrow ();
119 } catch (DecodeError& e) {
120 error_dialog (get(), e.what());
125 check_gl_error (char const * last)
127 GLenum const e = glGetError ();
128 if (e != GL_NO_ERROR) {
129 throw GLError (last, e);
134 GLVideoView::update ()
136 if (!_canvas->IsShownOnScreen()) {
145 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
146 check_gl_error ("glClear");
148 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
149 check_gl_error ("glClearColor");
150 glEnable (GL_TEXTURE_2D);
151 check_gl_error ("glEnable GL_TEXTURE_2D");
153 check_gl_error ("glEnable GL_BLEND");
154 glDisable (GL_DEPTH_TEST);
155 check_gl_error ("glDisable GL_DEPTH_TEST");
156 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158 if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
162 glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
163 check_gl_error ("glViewport");
164 glMatrixMode (GL_PROJECTION);
167 gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
168 check_gl_error ("gluOrtho2d");
169 glMatrixMode (GL_MODELVIEW);
172 glTranslatef (0, 0, 0);
174 dcp::Size const out_size = _viewer->out_size ();
177 /* Render our image (texture) */
180 glVertex2f (0, _size->height);
182 glVertex2f (_size->width, _size->height);
184 glVertex2f (_size->width, 0);
189 /* No image, so just fill with black */
191 glColor3ub (0, 0, 0);
193 glVertex2f (out_size.width, 0);
194 glVertex2f (out_size.width, out_size.height);
195 glVertex2f (0, out_size.height);
200 wxSize const canvas_size = _canvas->GetSize ();
202 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
204 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
205 glColor3ub (240, 240, 240);
206 glVertex2f (out_size.width, 0);
207 glVertex2f (canvas_size.GetWidth(), 0);
208 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
209 glVertex2f (out_size.width, canvas_size.GetHeight());
211 glColor3ub (255, 255, 255);
214 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
215 glColor3ub (240, 240, 240);
216 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
219 glVertex2f (canvas_size.GetWidth(), 0);
220 glVertex2f (canvas_size.GetWidth(), gap);
224 glVertex2f (0, gap + out_size.height + 1);
225 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
226 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
227 glVertex2f (0, 2 * gap + out_size.height + 2);
229 glColor3ub (255, 255, 255);
232 if (_viewer->outline_content()) {
233 glColor3ub (255, 0, 0);
234 glBegin (GL_LINE_LOOP);
235 Position<int> inter_position = _viewer->inter_position ();
236 dcp::Size inter_size = _viewer->inter_size ();
237 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
238 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
239 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
240 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
242 glColor3ub (255, 255, 255);
246 _canvas->SwapBuffers();
250 GLVideoView::set_image (shared_ptr<const Image> image)
253 _size = optional<dcp::Size>();
257 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
258 DCPOMATIC_ASSERT (!image->aligned());
260 _size = image->size ();
261 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
262 check_gl_error ("glPixelStorei");
263 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
264 check_gl_error ("glTexImage2D");
266 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
267 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
268 check_gl_error ("glTexParameteri");
270 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
271 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
272 check_gl_error ("glTexParameterf");
276 GLVideoView::start ()
278 boost::mutex::scoped_lock lm (_playing_mutex);
280 _playing_condition.notify_all ();
286 boost::mutex::scoped_lock lm (_playing_mutex);
291 GLVideoView::thread ()
294 /* XXX_b: check all calls and signal emissions in this method & protect them if necessary */
295 _context = new wxGLContext (_canvas);
296 _canvas->SetCurrent (*_context);
299 boost::mutex::scoped_lock lm (_playing_mutex);
300 while (!_playing && !_one_shot) {
301 _playing_condition.wait (lm);
306 if (length() != dcpomatic::DCPTime()) {
307 dcpomatic::DCPTime const next = position() + one_video_frame();
309 if (next >= length()) {
311 _viewer->emit_finished ();
315 get_next_frame (false);
316 set_image (player_video().first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
320 while (time_until_next_frame() < 5) {
321 get_next_frame (true);
324 boost::this_thread::interruption_point ();
325 dcpomatic_sleep_milliseconds (time_until_next_frame());
330 catch (boost::thread_interrupted& e)
332 /* XXX_b: store exceptions here */
338 GLVideoView::display_next_frame (bool non_blocking)
340 bool const r = get_next_frame (non_blocking);
346 GLVideoView::request_one_shot ()
348 boost::mutex::scoped_lock lm (_playing_mutex);
350 _playing_condition.notify_all ();
354 GLVideoView::create ()
357 _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));