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