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