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