eac9e3dbfd67ee22b765edbd3e01255714287d3c
[dcpomatic.git] / src / wx / gl_video_view.cc
1 /*
2     Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "gl_video_view.h"
22 #include "film_viewer.h"
23 #include "wx_util.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>
31 #include <iostream>
32
33 #ifdef DCPOMATIC_OSX
34 #include <OpenGL/glu.h>
35 #include <OpenGL/glext.h>
36 #include <OpenGL/CGLTypes.h>
37 #include <OpenGL/OpenGL.h>
38 #endif
39
40 #ifdef DCPOMATIC_LINUX
41 #include <GL/glu.h>
42 #include <GL/glext.h>
43 #include <GL/glxext.h>
44 #endif
45
46 #ifdef DCPOMATIC_WINDOWS
47 #include <GL/glu.h>
48 #include <GL/glext.h>
49 #include <GL/wglext.h>
50 #endif
51
52 using std::cout;
53 using boost::shared_ptr;
54 using boost::optional;
55
56
57 static void
58 check_gl_error (char const * last)
59 {
60         GLenum const e = glGetError ();
61         if (e != GL_NO_ERROR) {
62                 throw GLError (last, e);
63         }
64 }
65
66
67 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
68         : VideoView (viewer)
69         , _have_storage (false)
70         , _vsync_enabled (false)
71         , _playing (false)
72         , _one_shot (false)
73 {
74         check_gl_error ("GLVideoView constructor");
75         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
76         check_gl_error ("wxGLCanvas constructor");
77         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
78         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
79         _canvas->Bind (wxEVT_CREATE, boost::bind(&GLVideoView::create, this));
80
81         _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
82         _timer.reset (new wxTimer(_canvas));
83         _timer->Start (2000);
84
85 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
86         if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
87                 /* Enable vsync */
88                 Display* dpy = wxGetX11Display();
89                 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
90                 _vsync_enabled = true;
91         }
92 #endif
93
94 #ifdef DCPOMATIC_WINDOWS
95         if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
96                 /* Enable vsync */
97                 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
98                 if (swap) {
99                         swap (1);
100                         _vsync_enabled = true;
101                 }
102         }
103
104 #endif
105
106 #ifdef DCPOMATIC_OSX
107         /* Enable vsync */
108         GLint swapInterval = 1;
109         CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
110         _vsync_enabled = true;
111 #endif
112
113         check_gl_error ("pre-glGenTextures");
114         glGenTextures (1, &_id);
115         check_gl_error ("glGenTextures");
116         glBindTexture (GL_TEXTURE_2D, _id);
117         check_gl_error ("glBindTexture");
118         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
119         check_gl_error ("glPixelStorei");
120 }
121
122 GLVideoView::~GLVideoView ()
123 {
124         boost::this_thread::disable_interruption dis;
125
126         try {
127                 _thread.interrupt ();
128                 _thread.join ();
129         } catch (...) {}
130
131         glDeleteTextures (1, &_id);
132 }
133
134 void
135 GLVideoView::check_for_butler_errors ()
136 {
137         if (!_viewer->butler()) {
138                 return;
139         }
140
141         try {
142                 _viewer->butler()->rethrow ();
143         } catch (DecodeError& e) {
144                 error_dialog (get(), e.what());
145         }
146 }
147
148
149 void
150 GLVideoView::update ()
151 {
152         {
153                 boost::mutex::scoped_lock lm (_canvas_mutex);
154                 if (!_canvas->IsShownOnScreen()) {
155                         return;
156                 }
157         }
158         request_one_shot ();
159 }
160
161 void
162 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
163 {
164         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
165         check_gl_error ("glClear");
166
167         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
168         check_gl_error ("glClearColor");
169         glEnable (GL_TEXTURE_2D);
170         check_gl_error ("glEnable GL_TEXTURE_2D");
171         glEnable (GL_BLEND);
172         check_gl_error ("glEnable GL_BLEND");
173         glDisable (GL_DEPTH_TEST);
174         check_gl_error ("glDisable GL_DEPTH_TEST");
175         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
176
177         wxSize canvas_size;
178         {
179                 boost::mutex::scoped_lock lm (_canvas_mutex);
180                 canvas_size = _canvas->GetSize ();
181         }
182
183         if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
184                 return;
185         }
186
187         glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
188         check_gl_error ("glViewport");
189         glMatrixMode (GL_PROJECTION);
190         glLoadIdentity ();
191
192 DCPOMATIC_DISABLE_WARNINGS
193         gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
194 DCPOMATIC_ENABLE_WARNINGS
195         check_gl_error ("gluOrtho2d");
196         glMatrixMode (GL_MODELVIEW);
197         glLoadIdentity ();
198
199         glTranslatef (0, 0, 0);
200
201         dcp::Size const out_size = _viewer->out_size ();
202
203         if (_size) {
204                 /* Render our image (texture) */
205                 glBegin (GL_QUADS);
206                 glTexCoord2f (0, 1);
207                 glVertex2f (0, _size->height);
208                 glTexCoord2f (1, 1);
209                 glVertex2f (_size->width, _size->height);
210                 glTexCoord2f (1, 0);
211                 glVertex2f (_size->width, 0);
212                 glTexCoord2f (0, 0);
213                 glVertex2f (0, 0);
214                 glEnd ();
215         } else {
216                 /* No image, so just fill with black */
217                 glBegin (GL_QUADS);
218                 glColor3ub (0, 0, 0);
219                 glVertex2f (0, 0);
220                 glVertex2f (out_size.width, 0);
221                 glVertex2f (out_size.width, out_size.height);
222                 glVertex2f (0, out_size.height);
223                 glVertex2f (0, 0);
224                 glEnd ();
225         }
226
227         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
228                 glBegin (GL_QUADS);
229                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
230                 glColor3ub (240, 240, 240);
231                 glVertex2f (out_size.width, 0);
232                 glVertex2f (canvas_size.GetWidth(), 0);
233                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
234                 glVertex2f (out_size.width, canvas_size.GetHeight());
235                 glEnd ();
236                 glColor3ub (255, 255, 255);
237         }
238
239         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
240                 glColor3ub (240, 240, 240);
241                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
242                 glBegin (GL_QUADS);
243                 glVertex2f (0, 0);
244                 glVertex2f (canvas_size.GetWidth(), 0);
245                 glVertex2f (canvas_size.GetWidth(), gap);
246                 glVertex2f (0, gap);
247                 glEnd ();
248                 glBegin (GL_QUADS);
249                 glVertex2f (0, gap + out_size.height + 1);
250                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
251                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
252                 glVertex2f (0, 2 * gap + out_size.height + 2);
253                 glEnd ();
254                 glColor3ub (255, 255, 255);
255         }
256
257         if (_viewer->outline_content()) {
258                 glColor3ub (255, 0, 0);
259                 glBegin (GL_LINE_LOOP);
260                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
261                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
262                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
263                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
264                 glEnd ();
265                 glColor3ub (255, 255, 255);
266         }
267
268         glFlush();
269         check_gl_error ("glFlush");
270
271         boost::mutex::scoped_lock lm (_canvas_mutex);
272         _canvas->SwapBuffers();
273 }
274
275 void
276 GLVideoView::set_image (shared_ptr<const Image> image)
277 {
278         if (!image) {
279                 _size = optional<dcp::Size>();
280                 return;
281         }
282
283         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
284         DCPOMATIC_ASSERT (!image->aligned());
285
286         if (image->size() != _size) {
287                 _have_storage = false;
288         }
289
290         _size = image->size ();
291         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
292         check_gl_error ("glPixelStorei");
293         if (_have_storage) {
294                 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
295                 check_gl_error ("glTexSubImage2D");
296         } else {
297                 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
298                 _have_storage = true;
299                 check_gl_error ("glTexImage2D");
300         }
301
302         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
303         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
304         check_gl_error ("glTexParameteri");
305
306         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
307         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
308         check_gl_error ("glTexParameterf");
309 }
310
311 void
312 GLVideoView::start ()
313 {
314         VideoView::start ();
315
316         boost::mutex::scoped_lock lm (_playing_mutex);
317         _playing = true;
318         _thread_work_condition.notify_all ();
319 }
320
321 void
322 GLVideoView::stop ()
323 {
324         boost::mutex::scoped_lock lm (_playing_mutex);
325         _playing = false;
326 }
327
328
329 void
330 GLVideoView::thread_playing ()
331 {
332         if (length() != dcpomatic::DCPTime()) {
333                 dcpomatic::DCPTime const next = position() + one_video_frame();
334
335                 if (next >= length()) {
336                         _viewer->finished ();
337                         return;
338                 }
339
340                 get_next_frame (false);
341                 set_image_and_draw ();
342         }
343
344         while (true) {
345                 optional<int> n = time_until_next_frame();
346                 if (!n || *n > 5) {
347                         break;
348                 }
349                 get_next_frame (true);
350                 add_dropped ();
351         }
352 }
353
354
355 void
356 GLVideoView::set_image_and_draw ()
357 {
358         shared_ptr<PlayerVideo> pv = player_video().first;
359         if (pv) {
360                 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
361                 draw (pv->inter_position(), pv->inter_size());
362         }
363 }
364
365
366 void
367 GLVideoView::thread ()
368 try
369 {
370         {
371                 boost::mutex::scoped_lock lm (_canvas_mutex);
372                 _context = new wxGLContext (_canvas); //local
373                 DCPOMATIC_ASSERT (_context->IsOK());
374                 check_gl_error ("wxGLContext constuctor");
375                 _canvas->SetCurrent (*_context);
376         }
377
378         while (true) {
379                 boost::mutex::scoped_lock lm (_playing_mutex);
380                 while (!_playing && !_one_shot) {
381                         _thread_work_condition.wait (lm);
382                 }
383                 lm.unlock ();
384
385                 if (_playing) {
386                         thread_playing ();
387                 } else if (_one_shot) {
388                         _one_shot = false;
389                         set_image_and_draw ();
390                 }
391
392                 boost::this_thread::interruption_point ();
393                 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
394         }
395
396         /* XXX: leaks _context, but that seems preferable to deleting it here
397          * without also deleting the wxGLCanvas.
398          */
399 }
400 catch (boost::thread_interrupted& e)
401 {
402         store_current ();
403 }
404
405 bool
406 GLVideoView::display_next_frame (bool non_blocking)
407 {
408         bool const r = get_next_frame (non_blocking);
409         request_one_shot ();
410         return r;
411 }
412
413 void
414 GLVideoView::request_one_shot ()
415 {
416         boost::mutex::scoped_lock lm (_playing_mutex);
417         _one_shot = true;
418         _thread_work_condition.notify_all ();
419 }
420
421 void
422 GLVideoView::create ()
423 {
424         if (!_thread.joinable()) {
425                 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
426         }
427 }