Barely-functioning GL playback with new arrangement.
[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 {
59         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
60         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
61         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
62
63 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
64         if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
65                 /* Enable vsync */
66                 Display* dpy = wxGetX11Display();
67                 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
68                 _vsync_enabled = true;
69         }
70 #endif
71
72 #ifdef DCPOMATIC_WINDOWS
73         if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
74                 /* Enable vsync */
75                 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
76                 if (swap) {
77                         swap (1);
78                         _vsync_enabled = true;
79                 }
80         }
81
82 #endif
83
84 #ifdef DCPOMATIC_OSX
85         /* Enable vsync */
86         GLint swapInterval = 1;
87         CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
88         _vsync_enabled = true;
89 #endif
90
91         glGenTextures (1, &_id);
92         glBindTexture (GL_TEXTURE_2D, _id);
93         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
94 }
95
96 GLVideoView::~GLVideoView ()
97 {
98         if (_thread) {
99                 _thread->interrupt ();
100                 _thread->join ();
101         }
102         delete _thread;
103
104         glDeleteTextures (1, &_id);
105 }
106
107 static void
108        check_gl_error (char const * last)
109 {
110         GLenum const e = glGetError ();
111         if (e != GL_NO_ERROR) {
112                 throw GLError (last, e);
113         }
114 }
115
116 void
117 GLVideoView::paint ()
118 {
119         /* XXX_b: can't do this yet */
120 #if 0
121         _viewer->state_timer().set("paint-panel");
122         _canvas->SetCurrent (*_context);
123         wxPaintDC dc (_canvas);
124         draw ();
125         _viewer->state_timer().unset();
126 #endif
127 }
128
129 void
130 GLVideoView::update ()
131 {
132         if (!_canvas->IsShownOnScreen()) {
133                 return;
134         }
135         /* XXX_b */
136 //      wxClientDC dc (_canvas);
137 //      draw ();
138 }
139
140 void
141 GLVideoView::draw ()
142 {
143         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144         check_gl_error ("glClear");
145
146         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
147         check_gl_error ("glClearColor");
148         glEnable (GL_TEXTURE_2D);
149         check_gl_error ("glEnable GL_TEXTURE_2D");
150         glEnable (GL_BLEND);
151         check_gl_error ("glEnable GL_BLEND");
152         glDisable (GL_DEPTH_TEST);
153         check_gl_error ("glDisable GL_DEPTH_TEST");
154         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
155
156         if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
157                 return;
158         }
159
160         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
161         check_gl_error ("glViewport");
162         glMatrixMode (GL_PROJECTION);
163         glLoadIdentity ();
164
165         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
166         check_gl_error ("gluOrtho2d");
167         glMatrixMode (GL_MODELVIEW);
168         glLoadIdentity ();
169
170         glTranslatef (0, 0, 0);
171
172         if (_size) {
173                 glBegin (GL_QUADS);
174
175                 glTexCoord2f (0, 1);
176                 glVertex2f (0, _size->height);
177                 glTexCoord2f (1, 1);
178                 glVertex2f (_size->width, _size->height);
179                 glTexCoord2f (1, 0);
180                 glVertex2f (_size->width, 0);
181                 glTexCoord2f (0, 0);
182                 glVertex2f (0, 0);
183
184                 glEnd ();
185         }
186
187         dcp::Size const out_size = _viewer->out_size ();
188         wxSize const canvas_size = _canvas->GetSize ();
189
190         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
191                 glBegin (GL_QUADS);
192                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
193                 glColor3ub (240, 240, 240);
194                 glVertex2f (out_size.width, 0);
195                 glVertex2f (canvas_size.GetWidth(), 0);
196                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
197                 glVertex2f (out_size.width, canvas_size.GetHeight());
198                 glEnd ();
199                 glColor3ub (255, 255, 255);
200         }
201
202         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
203                 glColor3ub (240, 240, 240);
204                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
205                 glBegin (GL_QUADS);
206                 glVertex2f (0, 0);
207                 glVertex2f (canvas_size.GetWidth(), 0);
208                 glVertex2f (canvas_size.GetWidth(), gap);
209                 glVertex2f (0, gap);
210                 glEnd ();
211                 glBegin (GL_QUADS);
212                 glVertex2f (0, gap + out_size.height + 1);
213                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
214                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
215                 glVertex2f (0, 2 * gap + out_size.height + 2);
216                 glEnd ();
217                 glColor3ub (255, 255, 255);
218         }
219
220         if (_viewer->outline_content()) {
221                 glColor3ub (255, 0, 0);
222                 glBegin (GL_LINE_LOOP);
223                 Position<int> inter_position = _viewer->inter_position ();
224                 dcp::Size inter_size = _viewer->inter_size ();
225                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
226                 glVertex2f (inter_position.x + inter_size.width, 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 + inter_size.height);
228                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
229                 glEnd ();
230                 glColor3ub (255, 255, 255);
231         }
232
233         glFlush();
234         _canvas->SwapBuffers();
235 }
236
237 void
238 GLVideoView::set_image (shared_ptr<const Image> image)
239 {
240         if (!image) {
241                 _size = optional<dcp::Size>();
242                 return;
243         }
244
245         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
246         DCPOMATIC_ASSERT (!image->aligned());
247
248         _size = image->size ();
249         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
250         check_gl_error ("glPixelStorei");
251         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
252         check_gl_error ("glTexImage2D");
253
254         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
256         check_gl_error ("glTexParameteri");
257
258         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
259         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
260         check_gl_error ("glTexParameterf");
261 }
262
263 void
264 GLVideoView::start ()
265 {
266         _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));
267 }
268
269 void
270 GLVideoView::thread ()
271 try
272 {
273         /* XXX_b: check all calls and signal emissions in this method & protect them if necessary */
274         {
275                 boost::mutex::scoped_lock lm (_context_mutex);
276                 _context = new wxGLContext (_canvas);
277                 _canvas->SetCurrent (*_context);
278         }
279
280         while (true) {
281                 if (!_viewer->film() || !_viewer->playing()) {
282                         dcpomatic_sleep_milliseconds (40);
283                         continue;
284                 }
285
286                 dcpomatic::DCPTime const next = _viewer->position() + _viewer->one_video_frame();
287
288                 if (next >= _viewer->film()->length()) {
289                         _viewer->stop ();
290                         _viewer->Finished ();
291                         return;
292                 }
293
294                 get_next_frame (false);
295                 set_image (_player_video.first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
296                 draw ();
297                 _viewer->_video_position = _player_video.second;
298
299                 std::cout << "sleep " << _viewer->time_until_next_frame() << "\n";
300                 dcpomatic_sleep_milliseconds (_viewer->time_until_next_frame());
301         }
302
303         {
304                 boost::mutex::scoped_lock lm (_context_mutex);
305                 delete _context;
306         }
307 }
308 catch (boost::thread_interrupted& e)
309 {
310         /* XXX_b: store exceptions here */
311         delete _context;
312         return;
313 }
314
315 wxGLContext *
316 GLVideoView::context () const
317 {
318         boost::mutex::scoped_lock lm (_context_mutex);
319         return _context;
320 }