ebf4098e4c814693718e25be61a3b631776b7560
[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         try {
121                 _viewer->butler()->rethrow ();
122         } catch (DecodeError& e) {
123                 error_dialog (get(), e.what());
124         }
125 }
126
127 static void
128 check_gl_error (char const * last)
129 {
130         GLenum const e = glGetError ();
131         if (e != GL_NO_ERROR) {
132                 throw GLError (last, e);
133         }
134 }
135
136 void
137 GLVideoView::update ()
138 {
139         {
140                 boost::mutex::scoped_lock lm (_canvas_mutex);
141                 if (!_canvas->IsShownOnScreen()) {
142                         return;
143                 }
144         }
145         request_one_shot ();
146 }
147
148 void
149 GLVideoView::draw (Position<int> inter_position, dcp::Size inter_size)
150 {
151         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152         check_gl_error ("glClear");
153
154         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
155         check_gl_error ("glClearColor");
156         glEnable (GL_TEXTURE_2D);
157         check_gl_error ("glEnable GL_TEXTURE_2D");
158         glEnable (GL_BLEND);
159         check_gl_error ("glEnable GL_BLEND");
160         glDisable (GL_DEPTH_TEST);
161         check_gl_error ("glDisable GL_DEPTH_TEST");
162         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163
164         wxSize canvas_size;
165         {
166                 boost::mutex::scoped_lock lm (_canvas_mutex);
167                 canvas_size = _canvas->GetSize ();
168         }
169
170         if (canvas_size.GetWidth() < 64 || canvas_size.GetHeight() < 0) {
171                 return;
172         }
173
174         glViewport (0, 0, canvas_size.GetWidth(), canvas_size.GetHeight());
175         check_gl_error ("glViewport");
176         glMatrixMode (GL_PROJECTION);
177         glLoadIdentity ();
178
179         gluOrtho2D (0, canvas_size.GetWidth(), canvas_size.GetHeight(), 0);
180         check_gl_error ("gluOrtho2d");
181         glMatrixMode (GL_MODELVIEW);
182         glLoadIdentity ();
183
184         glTranslatef (0, 0, 0);
185
186         dcp::Size const out_size = _viewer->out_size ();
187
188         if (_size) {
189                 /* Render our image (texture) */
190                 glBegin (GL_QUADS);
191                 glTexCoord2f (0, 1);
192                 glVertex2f (0, _size->height);
193                 glTexCoord2f (1, 1);
194                 glVertex2f (_size->width, _size->height);
195                 glTexCoord2f (1, 0);
196                 glVertex2f (_size->width, 0);
197                 glTexCoord2f (0, 0);
198                 glVertex2f (0, 0);
199                 glEnd ();
200         } else {
201                 /* No image, so just fill with black */
202                 glBegin (GL_QUADS);
203                 glColor3ub (0, 0, 0);
204                 glVertex2f (0, 0);
205                 glVertex2f (out_size.width, 0);
206                 glVertex2f (out_size.width, out_size.height);
207                 glVertex2f (0, out_size.height);
208                 glVertex2f (0, 0);
209                 glEnd ();
210         }
211
212         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
213                 glBegin (GL_QUADS);
214                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
215                 glColor3ub (240, 240, 240);
216                 glVertex2f (out_size.width, 0);
217                 glVertex2f (canvas_size.GetWidth(), 0);
218                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
219                 glVertex2f (out_size.width, canvas_size.GetHeight());
220                 glEnd ();
221                 glColor3ub (255, 255, 255);
222         }
223
224         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
225                 glColor3ub (240, 240, 240);
226                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
227                 glBegin (GL_QUADS);
228                 glVertex2f (0, 0);
229                 glVertex2f (canvas_size.GetWidth(), 0);
230                 glVertex2f (canvas_size.GetWidth(), gap);
231                 glVertex2f (0, gap);
232                 glEnd ();
233                 glBegin (GL_QUADS);
234                 glVertex2f (0, gap + out_size.height + 1);
235                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
236                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
237                 glVertex2f (0, 2 * gap + out_size.height + 2);
238                 glEnd ();
239                 glColor3ub (255, 255, 255);
240         }
241
242         if (_viewer->outline_content()) {
243                 glColor3ub (255, 0, 0);
244                 glBegin (GL_LINE_LOOP);
245                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
246                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
247                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
248                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
249                 glEnd ();
250                 glColor3ub (255, 255, 255);
251         }
252
253         glFlush();
254
255         boost::mutex::scoped_lock lm (_canvas_mutex);
256         _canvas->SwapBuffers();
257 }
258
259 void
260 GLVideoView::set_image (shared_ptr<const Image> image)
261 {
262         if (!image) {
263                 _size = optional<dcp::Size>();
264                 return;
265         }
266
267         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
268         DCPOMATIC_ASSERT (!image->aligned());
269
270         if (image->size() != _size) {
271                 _have_storage = false;
272         }
273
274         _size = image->size ();
275         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
276         check_gl_error ("glPixelStorei");
277         if (_have_storage) {
278                 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
279                 check_gl_error ("glTexSubImage2D");
280         } else {
281                 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
282                 _have_storage = true;
283                 check_gl_error ("glTexImage2D");
284         }
285
286         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
287         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
288         check_gl_error ("glTexParameteri");
289
290         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
291         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
292         check_gl_error ("glTexParameterf");
293 }
294
295 void
296 GLVideoView::start ()
297 {
298         VideoView::start ();
299
300         boost::mutex::scoped_lock lm (_playing_mutex);
301         _playing = true;
302         _playing_condition.notify_all ();
303 }
304
305 void
306 GLVideoView::stop ()
307 {
308         boost::mutex::scoped_lock lm (_playing_mutex);
309         _playing = false;
310 }
311
312 void
313 GLVideoView::thread ()
314 try
315 {
316         {
317                 boost::mutex::scoped_lock lm (_canvas_mutex);
318                 _context = new wxGLContext (_canvas); //local
319                 _canvas->SetCurrent (*_context);
320         }
321
322         while (true) {
323                 boost::mutex::scoped_lock lm (_playing_mutex);
324                 while (!_playing && !_one_shot) {
325                         _playing_condition.wait (lm);
326                 }
327                 _one_shot = false;
328                 lm.unlock ();
329
330                 Position<int> inter_position;
331                 dcp::Size inter_size;
332                 if (length() != dcpomatic::DCPTime()) {
333                         dcpomatic::DCPTime const next = position() + one_video_frame();
334
335                         if (next >= length()) {
336                                 _viewer->finished ();
337                                 continue;
338                         }
339
340                         get_next_frame (false);
341                         shared_ptr<PlayerVideo> pv = player_video().first;
342                         if (pv) {
343                                 set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
344                                 inter_position = pv->inter_position();
345                                 inter_size = pv->inter_size();
346                         }
347                 }
348                 draw (inter_position, inter_size);
349
350                 while (true) {
351                         optional<int> n = time_until_next_frame();
352                         if (!n || *n > 5) {
353                                 break;
354                         }
355                         get_next_frame (true);
356                         add_dropped ();
357                 }
358
359                 boost::this_thread::interruption_point ();
360                 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
361         }
362
363         /* XXX: leaks _context, but that seems preferable to deleting it here
364          * without also deleting the wxGLCanvas.
365          */
366 }
367 catch (boost::thread_interrupted& e)
368 {
369         store_current ();
370 }
371
372 bool
373 GLVideoView::display_next_frame (bool non_blocking)
374 {
375         bool const r = get_next_frame (non_blocking);
376         request_one_shot ();
377         return r;
378 }
379
380 void
381 GLVideoView::request_one_shot ()
382 {
383         boost::mutex::scoped_lock lm (_playing_mutex);
384         _one_shot = true;
385         _playing_condition.notify_all ();
386 }
387
388 void
389 GLVideoView::create ()
390 {
391         if (!_thread.joinable()) {
392                 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
393         }
394 }