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