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