Fix display when there is no film.
[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 "lib/image.h"
24 #include "lib/dcpomatic_assert.h"
25 #include "lib/exceptions.h"
26 #include "lib/cross.h"
27 #include "lib/player_video.h"
28 #include <boost/bind.hpp>
29 #include <iostream>
30
31 #ifdef DCPOMATIC_OSX
32 #include <OpenGL/glu.h>
33 #include <OpenGL/glext.h>
34 #include <OpenGL/CGLTypes.h>
35 #include <OpenGL/OpenGL.h>
36 #endif
37
38 #ifdef DCPOMATIC_LINUX
39 #include <GL/glu.h>
40 #include <GL/glext.h>
41 #include <GL/glxext.h>
42 #endif
43
44 #ifdef DCPOMATIC_WINDOWS
45 #include <GL/glu.h>
46 #include <GL/glext.h>
47 #include <GL/wglext.h>
48 #endif
49
50 using std::cout;
51 using boost::shared_ptr;
52 using boost::optional;
53
54 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
55         : VideoView (viewer)
56         , _vsync_enabled (false)
57         , _thread (0)
58         , _playing (false)
59         , _one_shot (false)
60 {
61         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
62         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
63         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
64         _canvas->Bind (wxEVT_CREATE, boost::bind(&GLVideoView::create, this));
65
66 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
67         if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
68                 /* Enable vsync */
69                 Display* dpy = wxGetX11Display();
70                 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
71                 _vsync_enabled = true;
72         }
73 #endif
74
75 #ifdef DCPOMATIC_WINDOWS
76         if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
77                 /* Enable vsync */
78                 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
79                 if (swap) {
80                         swap (1);
81                         _vsync_enabled = true;
82                 }
83         }
84
85 #endif
86
87 #ifdef DCPOMATIC_OSX
88         /* Enable vsync */
89         GLint swapInterval = 1;
90         CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
91         _vsync_enabled = true;
92 #endif
93
94         glGenTextures (1, &_id);
95         glBindTexture (GL_TEXTURE_2D, _id);
96         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
97 }
98
99 GLVideoView::~GLVideoView ()
100 {
101         _thread->interrupt ();
102         _thread->join ();
103         delete _thread;
104
105         glDeleteTextures (1, &_id);
106 }
107
108 static void
109 check_gl_error (char const * last)
110 {
111         GLenum const e = glGetError ();
112         if (e != GL_NO_ERROR) {
113                 throw GLError (last, e);
114         }
115 }
116
117 void
118 GLVideoView::paint ()
119 {
120         request_one_shot ();
121 }
122
123 void
124 GLVideoView::update ()
125 {
126         if (!_canvas->IsShownOnScreen()) {
127                 return;
128         }
129         /* XXX_b */
130 //      wxClientDC dc (_canvas);
131 //      draw ();
132 }
133
134 void
135 GLVideoView::draw ()
136 {
137         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
138         check_gl_error ("glClear");
139
140         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
141         check_gl_error ("glClearColor");
142         glEnable (GL_TEXTURE_2D);
143         check_gl_error ("glEnable GL_TEXTURE_2D");
144         glEnable (GL_BLEND);
145         check_gl_error ("glEnable GL_BLEND");
146         glDisable (GL_DEPTH_TEST);
147         check_gl_error ("glDisable GL_DEPTH_TEST");
148         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
149
150         if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
151                 return;
152         }
153
154         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
155         check_gl_error ("glViewport");
156         glMatrixMode (GL_PROJECTION);
157         glLoadIdentity ();
158
159         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
160         check_gl_error ("gluOrtho2d");
161         glMatrixMode (GL_MODELVIEW);
162         glLoadIdentity ();
163
164         glTranslatef (0, 0, 0);
165
166         dcp::Size const out_size = _viewer->out_size ();
167
168         if (_size) {
169                 /* Render our image (texture) */
170                 glBegin (GL_QUADS);
171                 glTexCoord2f (0, 1);
172                 glVertex2f (0, _size->height);
173                 glTexCoord2f (1, 1);
174                 glVertex2f (_size->width, _size->height);
175                 glTexCoord2f (1, 0);
176                 glVertex2f (_size->width, 0);
177                 glTexCoord2f (0, 0);
178                 glVertex2f (0, 0);
179                 glEnd ();
180         } else {
181                 /* No image, so just fill with black */
182                 glBegin (GL_QUADS);
183                 glColor3ub (0, 0, 0);
184                 glVertex2f (0, 0);
185                 glVertex2f (out_size.width, 0);
186                 glVertex2f (out_size.width, out_size.height);
187                 glVertex2f (0, out_size.height);
188                 glVertex2f (0, 0);
189                 glEnd ();
190         }
191
192         wxSize const canvas_size = _canvas->GetSize ();
193
194         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
195                 glBegin (GL_QUADS);
196                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
197                 glColor3ub (240, 240, 240);
198                 glVertex2f (out_size.width, 0);
199                 glVertex2f (canvas_size.GetWidth(), 0);
200                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
201                 glVertex2f (out_size.width, canvas_size.GetHeight());
202                 glEnd ();
203                 glColor3ub (255, 255, 255);
204         }
205
206         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
207                 glColor3ub (240, 240, 240);
208                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
209                 glBegin (GL_QUADS);
210                 glVertex2f (0, 0);
211                 glVertex2f (canvas_size.GetWidth(), 0);
212                 glVertex2f (canvas_size.GetWidth(), gap);
213                 glVertex2f (0, gap);
214                 glEnd ();
215                 glBegin (GL_QUADS);
216                 glVertex2f (0, gap + out_size.height + 1);
217                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
218                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
219                 glVertex2f (0, 2 * gap + out_size.height + 2);
220                 glEnd ();
221                 glColor3ub (255, 255, 255);
222         }
223
224         if (_viewer->outline_content()) {
225                 glColor3ub (255, 0, 0);
226                 glBegin (GL_LINE_LOOP);
227                 Position<int> inter_position = _viewer->inter_position ();
228                 dcp::Size inter_size = _viewer->inter_size ();
229                 glVertex2f (inter_position.x, 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);
231                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
232                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
233                 glEnd ();
234                 glColor3ub (255, 255, 255);
235         }
236
237         glFlush();
238         _canvas->SwapBuffers();
239 }
240
241 void
242 GLVideoView::set_image (shared_ptr<const Image> image)
243 {
244         if (!image) {
245                 _size = optional<dcp::Size>();
246                 return;
247         }
248
249         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
250         DCPOMATIC_ASSERT (!image->aligned());
251
252         _size = image->size ();
253         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
254         check_gl_error ("glPixelStorei");
255         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
256         check_gl_error ("glTexImage2D");
257
258         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
259         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
260         check_gl_error ("glTexParameteri");
261
262         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
263         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
264         check_gl_error ("glTexParameterf");
265 }
266
267 void
268 GLVideoView::start ()
269 {
270         boost::mutex::scoped_lock lm (_playing_mutex);
271         _playing = true;
272         _playing_condition.notify_all ();
273 }
274
275 void
276 GLVideoView::stop ()
277 {
278         boost::mutex::scoped_lock lm (_playing_mutex);
279         _playing = false;
280 }
281
282 void
283 GLVideoView::thread ()
284 try
285 {
286         /* XXX_b: check all calls and signal emissions in this method & protect them if necessary */
287         _context = new wxGLContext (_canvas);
288         _canvas->SetCurrent (*_context);
289
290         while (true) {
291                 boost::mutex::scoped_lock lm (_playing_mutex);
292                 while (!_playing && !_one_shot) {
293                         _playing_condition.wait (lm);
294                 }
295                 _one_shot = false;
296                 lm.unlock ();
297
298                 if (length() != dcpomatic::DCPTime()) {
299                         dcpomatic::DCPTime const next = position() + one_video_frame();
300
301                         if (next >= length()) {
302                                 _viewer->stop ();
303                                 _viewer->emit_finished ();
304                                 continue;
305                         }
306
307                         get_next_frame (false);
308                         set_image (player_video().first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
309                 }
310                 draw ();
311
312                 while (time_until_next_frame() < 5) {
313                         get_next_frame (true);
314                 }
315
316                 boost::this_thread::interruption_point ();
317                 dcpomatic_sleep_milliseconds (time_until_next_frame());
318         }
319
320         delete _context;
321 }
322 catch (boost::thread_interrupted& e)
323 {
324         /* XXX_b: store exceptions here */
325         delete _context;
326         return;
327 }
328
329 bool
330 GLVideoView::display_next_frame (bool non_blocking)
331 {
332         bool const r = get_next_frame (non_blocking);
333         request_one_shot ();
334         return r;
335 }
336
337 void
338 GLVideoView::request_one_shot ()
339 {
340         boost::mutex::scoped_lock lm (_playing_mutex);
341         _one_shot = true;
342         _playing_condition.notify_all ();
343 }
344
345 void
346 GLVideoView::create ()
347 {
348         if (!_thread) {
349                 _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));
350         }
351 }