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