c3a6112831750641cedc226733010ba83b70fb94
[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         , _one_shot (false)
59 {
60         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
61         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
62         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
63
64 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
65         if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
66                 /* Enable vsync */
67                 Display* dpy = wxGetX11Display();
68                 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
69                 _vsync_enabled = true;
70         }
71 #endif
72
73 #ifdef DCPOMATIC_WINDOWS
74         if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
75                 /* Enable vsync */
76                 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
77                 if (swap) {
78                         swap (1);
79                         _vsync_enabled = true;
80                 }
81         }
82
83 #endif
84
85 #ifdef DCPOMATIC_OSX
86         /* Enable vsync */
87         GLint swapInterval = 1;
88         CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
89         _vsync_enabled = true;
90 #endif
91
92         glGenTextures (1, &_id);
93         glBindTexture (GL_TEXTURE_2D, _id);
94         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
95 }
96
97 GLVideoView::~GLVideoView ()
98 {
99         if (_thread) {
100                 _thread->interrupt ();
101                 _thread->join ();
102         }
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         /* XXX_b: can't do this yet */
121 #if 0
122         _viewer->state_timer().set("paint-panel");
123         _canvas->SetCurrent (*_context);
124         wxPaintDC dc (_canvas);
125         draw ();
126         _viewer->state_timer().unset();
127 #endif
128 }
129
130 void
131 GLVideoView::update ()
132 {
133         if (!_canvas->IsShownOnScreen()) {
134                 return;
135         }
136         /* XXX_b */
137 //      wxClientDC dc (_canvas);
138 //      draw ();
139 }
140
141 void
142 GLVideoView::draw ()
143 {
144         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
145         check_gl_error ("glClear");
146
147         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
148         check_gl_error ("glClearColor");
149         glEnable (GL_TEXTURE_2D);
150         check_gl_error ("glEnable GL_TEXTURE_2D");
151         glEnable (GL_BLEND);
152         check_gl_error ("glEnable GL_BLEND");
153         glDisable (GL_DEPTH_TEST);
154         check_gl_error ("glDisable GL_DEPTH_TEST");
155         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
156
157         if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
158                 return;
159         }
160
161         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
162         check_gl_error ("glViewport");
163         glMatrixMode (GL_PROJECTION);
164         glLoadIdentity ();
165
166         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
167         check_gl_error ("gluOrtho2d");
168         glMatrixMode (GL_MODELVIEW);
169         glLoadIdentity ();
170
171         glTranslatef (0, 0, 0);
172
173         if (_size) {
174                 glBegin (GL_QUADS);
175
176                 glTexCoord2f (0, 1);
177                 glVertex2f (0, _size->height);
178                 glTexCoord2f (1, 1);
179                 glVertex2f (_size->width, _size->height);
180                 glTexCoord2f (1, 0);
181                 glVertex2f (_size->width, 0);
182                 glTexCoord2f (0, 0);
183                 glVertex2f (0, 0);
184
185                 glEnd ();
186         }
187
188         dcp::Size const out_size = _viewer->out_size ();
189         wxSize const canvas_size = _canvas->GetSize ();
190
191         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
192                 glBegin (GL_QUADS);
193                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
194                 glColor3ub (240, 240, 240);
195                 glVertex2f (out_size.width, 0);
196                 glVertex2f (canvas_size.GetWidth(), 0);
197                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
198                 glVertex2f (out_size.width, canvas_size.GetHeight());
199                 glEnd ();
200                 glColor3ub (255, 255, 255);
201         }
202
203         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
204                 glColor3ub (240, 240, 240);
205                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
206                 glBegin (GL_QUADS);
207                 glVertex2f (0, 0);
208                 glVertex2f (canvas_size.GetWidth(), 0);
209                 glVertex2f (canvas_size.GetWidth(), gap);
210                 glVertex2f (0, gap);
211                 glEnd ();
212                 glBegin (GL_QUADS);
213                 glVertex2f (0, gap + out_size.height + 1);
214                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
215                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
216                 glVertex2f (0, 2 * gap + out_size.height + 2);
217                 glEnd ();
218                 glColor3ub (255, 255, 255);
219         }
220
221         if (_viewer->outline_content()) {
222                 glColor3ub (255, 0, 0);
223                 glBegin (GL_LINE_LOOP);
224                 Position<int> inter_position = _viewer->inter_position ();
225                 dcp::Size inter_size = _viewer->inter_size ();
226                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
227                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
228                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
229                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
230                 glEnd ();
231                 glColor3ub (255, 255, 255);
232         }
233
234         glFlush();
235         _canvas->SwapBuffers();
236 }
237
238 void
239 GLVideoView::set_image (shared_ptr<const Image> image)
240 {
241         if (!image) {
242                 _size = optional<dcp::Size>();
243                 return;
244         }
245
246         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
247         DCPOMATIC_ASSERT (!image->aligned());
248
249         _size = image->size ();
250         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
251         check_gl_error ("glPixelStorei");
252         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
253         check_gl_error ("glTexImage2D");
254
255         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
257         check_gl_error ("glTexParameteri");
258
259         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
260         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
261         check_gl_error ("glTexParameterf");
262 }
263
264 void
265 GLVideoView::start ()
266 {
267         _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));
268 }
269
270 void
271 GLVideoView::stop ()
272 {
273         if (_thread) {
274                 _thread->interrupt ();
275                 _thread->join ();
276         }
277         delete _thread;
278         _thread = 0;
279 }
280
281 bool
282 GLVideoView::one_shot () const
283 {
284         boost::mutex::scoped_lock lm (_one_shot_mutex);
285         return _one_shot;
286 }
287
288 void
289 GLVideoView::set_one_shot (bool s)
290 {
291         boost::mutex::scoped_lock lm (_one_shot_mutex);
292         _one_shot = s;
293 }
294
295 void
296 GLVideoView::thread ()
297 try
298 {
299         /* XXX_b: check all calls and signal emissions in this method & protect them if necessary */
300         {
301                 boost::mutex::scoped_lock lm (_context_mutex);
302                 _context = new wxGLContext (_canvas);
303                 _canvas->SetCurrent (*_context);
304         }
305
306         while (true) {
307                 if (!film() && !one_shot()) {
308                         /* XXX: this should be an indefinite wait until
309                            one of our conditions becomes true.
310                          */
311                         dcpomatic_sleep_milliseconds (40);
312                         continue;
313                 }
314
315                 set_one_shot (false);
316
317                 dcpomatic::DCPTime const next = position() + one_video_frame();
318
319                 if (next >= film()->length()) {
320                         _viewer->stop ();
321                         _viewer->emit_finished ();
322                         continue;
323                 }
324
325                 get_next_frame (false);
326                 {
327                         boost::mutex::scoped_lock lm (_mutex);
328                         set_image (_player_video.first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
329                 }
330                 draw ();
331
332                 while (time_until_next_frame() < 5) {
333                         get_next_frame (true);
334                 }
335
336                 boost::this_thread::interruption_point ();
337                 dcpomatic_sleep_milliseconds (time_until_next_frame());
338         }
339
340         {
341                 boost::mutex::scoped_lock lm (_context_mutex);
342                 delete _context;
343         }
344 }
345 catch (boost::thread_interrupted& e)
346 {
347         /* XXX_b: store exceptions here */
348         delete _context;
349         return;
350 }
351
352 wxGLContext *
353 GLVideoView::context () const
354 {
355         boost::mutex::scoped_lock lm (_context_mutex);
356         return _context;
357 }
358
359 bool
360 GLVideoView::display_next_frame (bool non_blocking)
361 {
362         bool const g = get_next_frame (non_blocking);
363         set_one_shot (true);
364         return g;
365 }
366
367 dcpomatic::DCPTime
368 GLVideoView::one_video_frame () const
369 {
370         return dcpomatic::DCPTime::from_frames (1, film()->video_frame_rate());
371 }
372
373