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