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