Rename image -> video.
[dcpomatic.git] / src / wx / gl_video_view.cc
1 /*
2     Copyright (C) 2018-2021 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
22 #ifdef DCPOMATIC_WINDOWS
23 #include <GL/glew.h>
24 #endif
25
26 #include "gl_video_view.h"
27 #include "film_viewer.h"
28 #include "wx_util.h"
29 #include "lib/image.h"
30 #include "lib/dcpomatic_assert.h"
31 #include "lib/exceptions.h"
32 #include "lib/cross.h"
33 #include "lib/player_video.h"
34 #include "lib/butler.h"
35 #include <boost/bind/bind.hpp>
36 #include <iostream>
37
38 #ifdef DCPOMATIC_OSX
39 #define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
40 #include <OpenGL/OpenGL.h>
41 #include <OpenGL/gl3.h>
42 #endif
43
44 #ifdef DCPOMATIC_LINUX
45 #include <GL/glu.h>
46 #include <GL/glext.h>
47 #endif
48
49 #ifdef DCPOMATIC_WINDOWS
50 #include <GL/glu.h>
51 #include <GL/wglext.h>
52 #endif
53
54
55 using std::cout;
56 using std::shared_ptr;
57 using std::string;
58 using boost::optional;
59 #if BOOST_VERSION >= 106100
60 using namespace boost::placeholders;
61 #endif
62
63
64 static void
65 check_gl_error (char const * last)
66 {
67         GLenum const e = glGetError ();
68         if (e != GL_NO_ERROR) {
69                 throw GLError (last, e);
70         }
71 }
72
73
74 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
75         : VideoView (viewer)
76         , _context (nullptr)
77         , _vsync_enabled (false)
78         , _playing (false)
79         , _one_shot (false)
80 {
81         wxGLAttributes attributes;
82         /* We don't need a depth buffer, and indeed there is apparently a bug with Windows/Intel HD 630
83          * which puts green lines over the OpenGL display if you have a non-zero depth buffer size.
84          * https://community.intel.com/t5/Graphics/Request-for-details-on-Intel-HD-630-green-lines-in-OpenGL-apps/m-p/1202179
85          */
86         attributes.PlatformDefaults().MinRGBA(8, 8, 8, 8).DoubleBuffer().Depth(0).EndList();
87         _canvas = new wxGLCanvas (
88                 parent, attributes, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE
89         );
90         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
91         _canvas->Bind (wxEVT_SIZE, boost::bind(&GLVideoView::size_changed, this, _1));
92
93         _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
94         _timer.reset (new wxTimer(_canvas));
95         _timer->Start (2000);
96 }
97
98
99 void
100 GLVideoView::size_changed (wxSizeEvent const& ev)
101 {
102         _canvas_size = ev.GetSize ();
103         Sized ();
104 }
105
106
107
108 GLVideoView::~GLVideoView ()
109 {
110         boost::this_thread::disable_interruption dis;
111
112         try {
113                 _thread.interrupt ();
114                 _thread.join ();
115         } catch (...) {}
116 }
117
118 void
119 GLVideoView::check_for_butler_errors ()
120 {
121         if (!_viewer->butler()) {
122                 return;
123         }
124
125         try {
126                 _viewer->butler()->rethrow ();
127         } catch (DecodeError& e) {
128                 error_dialog (get(), e.what());
129         } catch (dcp::ReadError& e) {
130                 error_dialog (get(), wxString::Format(_("Could not read DCP: %s"), std_to_wx(e.what())));
131         }
132 }
133
134
135 /** Called from the UI thread */
136 void
137 GLVideoView::update ()
138 {
139         if (!_canvas->IsShownOnScreen()) {
140                 return;
141         }
142
143         /* It appears important to do this from the GUI thread; if we do it from the GL thread
144          * on Linux we get strange failures to create the context for any version of GL higher
145          * than 3.2.
146          */
147         ensure_context ();
148
149 #ifdef DCPOMATIC_OSX
150         /* macOS gives errors if we don't do this (and therefore [NSOpenGLContext setView:]) from the main thread */
151         if (!_setup_shaders_done) {
152                 setup_shaders ();
153                 _setup_shaders_done = true;
154         }
155 #endif
156
157         if (!_thread.joinable()) {
158                 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
159         }
160
161         request_one_shot ();
162
163         rethrow ();
164 }
165
166
167 static constexpr char vertex_source[] =
168 "#version 330 core\n"
169 "\n"
170 "layout (location = 0) in vec3 in_pos;\n"
171 "layout (location = 1) in vec2 in_tex_coord;\n"
172 "\n"
173 "out vec2 TexCoord;\n"
174 "\n"
175 "void main()\n"
176 "{\n"
177 "       gl_Position = vec4(in_pos, 1.0);\n"
178 "       TexCoord = in_tex_coord;\n"
179 "}\n";
180
181
182 /* Bicubic interpolation stolen from https://stackoverflow.com/questions/13501081/efficient-bicubic-filtering-code-in-glsl */
183 static constexpr char fragment_source[] =
184 "#version 330 core\n"
185 "\n"
186 "in vec2 TexCoord;\n"
187 "\n"
188 "uniform sampler2D texture_sampler;\n"
189 /* type = 0: draw border
190  * type = 1: draw XYZ image
191  * type = 2: draw RGB image
192  */
193 "uniform int type = 0;\n"
194 "uniform vec4 border_colour;\n"
195 "uniform mat4 colour_conversion;\n"
196 "\n"
197 "out vec4 FragColor;\n"
198 "\n"
199 "vec4 cubic(float x)\n"
200 "\n"
201 "#define IN_GAMMA 2.2\n"
202 "#define OUT_GAMMA 0.384615385\n"       //  1 /  2.6
203 "#define DCI_COEFFICIENT 0.91655528\n"  // 48 / 53.37
204 "\n"
205 "{\n"
206 "    float x2 = x * x;\n"
207 "    float x3 = x2 * x;\n"
208 "    vec4 w;\n"
209 "    w.x =     -x3 + 3 * x2 - 3 * x + 1;\n"
210 "    w.y =  3 * x3 - 6 * x2         + 4;\n"
211 "    w.z = -3 * x3 + 3 * x2 + 3 * x + 1;\n"
212 "    w.w =  x3;\n"
213 "    return w / 6.f;\n"
214 "}\n"
215 "\n"
216 "vec4 texture_bicubic(sampler2D sampler, vec2 tex_coords)\n"
217 "{\n"
218 "   vec2 tex_size = textureSize(sampler, 0);\n"
219 "   vec2 inv_tex_size = 1.0 / tex_size;\n"
220 "\n"
221 "   tex_coords = tex_coords * tex_size - 0.5;\n"
222 "\n"
223 "   vec2 fxy = fract(tex_coords);\n"
224 "   tex_coords -= fxy;\n"
225 "\n"
226 "   vec4 xcubic = cubic(fxy.x);\n"
227 "   vec4 ycubic = cubic(fxy.y);\n"
228 "\n"
229 "   vec4 c = tex_coords.xxyy + vec2 (-0.5, +1.5).xyxy;\n"
230 "\n"
231 "   vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw);\n"
232 "   vec4 offset = c + vec4 (xcubic.yw, ycubic.yw) / s;\n"
233 "\n"
234 "   offset *= inv_tex_size.xxyy;\n"
235 "\n"
236 "   vec4 sample0 = texture(sampler, offset.xz);\n"
237 "   vec4 sample1 = texture(sampler, offset.yz);\n"
238 "   vec4 sample2 = texture(sampler, offset.xw);\n"
239 "   vec4 sample3 = texture(sampler, offset.yw);\n"
240 "\n"
241 "   float sx = s.x / (s.x + s.y);\n"
242 "   float sy = s.z / (s.z + s.w);\n"
243 "\n"
244 "   return mix(\n"
245 "          mix(sample3, sample2, sx), mix(sample1, sample0, sx)\n"
246 "          , sy);\n"
247 "}\n"
248 "\n"
249 "void main()\n"
250 "{\n"
251 "       switch (type) {\n"
252 "               case 0:\n"
253 "                       FragColor = border_colour;\n"
254 "                       break;\n"
255 "               case 1:\n"
256 "                       FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
257 "                       FragColor.x = pow(FragColor.x, IN_GAMMA) / DCI_COEFFICIENT;\n"
258 "                       FragColor.y = pow(FragColor.y, IN_GAMMA) / DCI_COEFFICIENT;\n"
259 "                       FragColor.z = pow(FragColor.z, IN_GAMMA) / DCI_COEFFICIENT;\n"
260 "                       FragColor = colour_conversion * FragColor;\n"
261 "                       FragColor.x = pow(FragColor.x, OUT_GAMMA);\n"
262 "                       FragColor.y = pow(FragColor.y, OUT_GAMMA);\n"
263 "                       FragColor.z = pow(FragColor.z, OUT_GAMMA);\n"
264 "                       break;\n"
265 "               case 2:\n"
266 "                       FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
267 "                       break;\n"
268 "       }\n"
269 "}\n";
270
271
272 void
273 GLVideoView::ensure_context ()
274 {
275         if (!_context) {
276                 wxGLContextAttrs attrs;
277                 attrs.PlatformDefaults().CoreProfile().OGLVersion(4, 1).EndList();
278                 _context = new wxGLContext (_canvas, nullptr, &attrs);
279                 if (!_context->IsOK()) {
280                         throw GLError ("Making GL context", -1);
281                 }
282         }
283 }
284
285
286 /* Offset of video texture triangles in indices */
287 static constexpr int indices_video_texture = 0;
288 /* Offset of border lines in indices */
289 static constexpr int indices_border = 6;
290
291 static constexpr unsigned int indices[] = {
292         0, 1, 3, // video texture triangle #1
293         1, 2, 3, // video texture triangle #2
294         4, 5,    // border line #1
295         5, 6,    // border line #2
296         6, 7,    // border line #3
297         7, 4,    // border line #4
298 };
299
300
301 void
302 GLVideoView::setup_shaders ()
303 {
304         DCPOMATIC_ASSERT (_canvas);
305         DCPOMATIC_ASSERT (_context);
306         auto r = _canvas->SetCurrent (*_context);
307         DCPOMATIC_ASSERT (r);
308
309 #ifdef DCPOMATIC_WINDOWS
310         r = glewInit();
311         if (r != GLEW_OK) {
312                 throw GLError(reinterpret_cast<char const*>(glewGetErrorString(r)));
313         }
314 #endif
315
316         auto get_information = [this](GLenum name) {
317                 auto s = glGetString (name);
318                 if (s) {
319                         _information[name] = std::string (reinterpret_cast<char const *>(s));
320                 }
321         };
322
323         get_information (GL_VENDOR);
324         get_information (GL_RENDERER);
325         get_information (GL_VERSION);
326         get_information (GL_SHADING_LANGUAGE_VERSION);
327
328         glGenVertexArrays(1, &_vao);
329         check_gl_error ("glGenVertexArrays");
330         GLuint vbo;
331         glGenBuffers(1, &vbo);
332         check_gl_error ("glGenBuffers");
333         GLuint ebo;
334         glGenBuffers(1, &ebo);
335         check_gl_error ("glGenBuffers");
336
337         glBindVertexArray(_vao);
338         check_gl_error ("glBindVertexArray");
339
340         glBindBuffer(GL_ARRAY_BUFFER, vbo);
341         check_gl_error ("glBindBuffer");
342
343         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
344         check_gl_error ("glBindBuffer");
345         glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
346         check_gl_error ("glBufferData");
347
348         /* position attribute to vertex shader (location = 0) */
349         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
350         glEnableVertexAttribArray(0);
351         /* texture coord attribute to vertex shader (location = 1) */
352         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
353         glEnableVertexAttribArray(1);
354         check_gl_error ("glEnableVertexAttribArray");
355
356         auto compile = [](GLenum type, char const* source) -> GLuint {
357                 auto shader = glCreateShader(type);
358                 DCPOMATIC_ASSERT (shader);
359                 GLchar const * src[] = { static_cast<GLchar const *>(source) };
360                 glShaderSource(shader, 1, src, nullptr);
361                 check_gl_error ("glShaderSource");
362                 glCompileShader(shader);
363                 check_gl_error ("glCompileShader");
364                 GLint ok;
365                 glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
366                 if (!ok) {
367                         GLint log_length;
368                         glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
369                         string log;
370                         if (log_length > 0) {
371                                 char* log_char = new char[log_length];
372                                 glGetShaderInfoLog(shader, log_length, nullptr, log_char);
373                                 log = string(log_char);
374                                 delete[] log_char;
375                         }
376                         glDeleteShader(shader);
377                         throw GLError(String::compose("Could not compile shader (%1)", log).c_str(), -1);
378                 }
379                 return shader;
380         };
381
382         auto vertex_shader = compile (GL_VERTEX_SHADER, vertex_source);
383         auto fragment_shader = compile (GL_FRAGMENT_SHADER, fragment_source);
384
385         auto program = glCreateProgram();
386         check_gl_error ("glCreateProgram");
387         glAttachShader (program, vertex_shader);
388         check_gl_error ("glAttachShader");
389         glAttachShader (program, fragment_shader);
390         check_gl_error ("glAttachShader");
391         glLinkProgram (program);
392         check_gl_error ("glLinkProgram");
393         GLint ok;
394         glGetProgramiv (program, GL_LINK_STATUS, &ok);
395         if (!ok) {
396                 GLint log_length;
397                 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
398                 string log;
399                 if (log_length > 0) {
400                         char* log_char = new char[log_length];
401                         glGetProgramInfoLog(program, log_length, nullptr, log_char);
402                         log = string(log_char);
403                         delete[] log_char;
404                 }
405                 glDeleteProgram (program);
406                 throw GLError(String::compose("Could not link shader (%1)", log).c_str(), -1);
407         }
408         glDeleteShader (vertex_shader);
409         glDeleteShader (fragment_shader);
410
411         glUseProgram (program);
412
413         _fragment_type = glGetUniformLocation (program, "type");
414         check_gl_error ("glGetUniformLocation");
415         set_border_colour (program);
416
417         auto conversion = dcp::ColourConversion::rec709_to_xyz();
418         boost::numeric::ublas::matrix<double> matrix = conversion.xyz_to_rgb ();
419         GLfloat gl_matrix[] = {
420                 static_cast<float>(matrix(0, 0)), static_cast<float>(matrix(0, 1)), static_cast<float>(matrix(0, 2)), 0.0f,
421                 static_cast<float>(matrix(1, 0)), static_cast<float>(matrix(1, 1)), static_cast<float>(matrix(1, 2)), 0.0f,
422                 static_cast<float>(matrix(2, 0)), static_cast<float>(matrix(2, 1)), static_cast<float>(matrix(2, 2)), 0.0f,
423                 0.0f, 0.0f, 0.0f, 1.0f
424                 };
425
426         auto colour_conversion = glGetUniformLocation (program, "colour_conversion");
427         check_gl_error ("glGetUniformLocation");
428         glUniformMatrix4fv (colour_conversion, 1, GL_TRUE, gl_matrix);
429
430         glLineWidth (2.0f);
431 }
432
433
434 void
435 GLVideoView::set_border_colour (GLuint program)
436 {
437         auto uniform = glGetUniformLocation (program, "border_colour");
438         check_gl_error ("glGetUniformLocation");
439         auto colour = outline_content_colour ();
440         glUniform4f (uniform, colour.Red() / 255.0f, colour.Green() / 255.0f, colour.Blue() / 255.0f, 1.0f);
441         check_gl_error ("glUniform4f");
442 }
443
444
445 void
446 GLVideoView::draw (Position<int>, dcp::Size)
447 {
448         auto pad = pad_colour();
449         glClearColor(pad.Red() / 255.0, pad.Green() / 255.0, pad.Blue() / 255.0, 1.0);
450         glClear (GL_COLOR_BUFFER_BIT);
451         check_gl_error ("glClear");
452
453         auto const size = _canvas_size.load();
454         int const width = size.GetWidth();
455         int const height = size.GetHeight();
456
457         if (width < 64 || height < 0) {
458                 return;
459         }
460
461         glViewport (0, 0, width, height);
462         check_gl_error ("glViewport");
463
464         glBindVertexArray(_vao);
465         check_gl_error ("glBindVertexArray");
466         glUniform1i(_fragment_type, _optimise_for_j2k ? 1 : 2);
467         glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_video_texture));
468         if (_viewer->outline_content()) {
469                 glUniform1i(_fragment_type, 1);
470                 glDrawElements (GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_border * sizeof(int)));
471                 check_gl_error ("glDrawElements");
472         }
473
474         glFlush();
475         check_gl_error ("glFlush");
476
477         _canvas->SwapBuffers();
478 }
479
480
481 void
482 GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
483 {
484         auto video = _optimise_for_j2k ? pv->raw_image() : pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true);
485
486         DCPOMATIC_ASSERT (!video->aligned());
487
488         /** If _optimise_for_j2k is true we render a XYZ image, doing the colourspace
489          *  conversion, scaling and video range conversion in the GL shader.
490          *  Othewise we render a RGB image without any shader-side processing.
491          */
492
493         /* XXX: video range conversion */
494         /* XXX: subs */
495
496         auto const changed = _video_texture->set (video);
497
498         if (changed) {
499                 auto const canvas_size = _canvas_size.load();
500                 int const canvas_width = canvas_size.GetWidth();
501                 int const canvas_height = canvas_size.GetHeight();
502
503                 float const video_x = float(video->size().width) / canvas_width;
504                 float const video_y = float(video->size().height) / canvas_height;
505
506                 auto x_pixels_to_gl = [canvas_width](int x) {
507                         return (x * 2.0f / canvas_width) - 1.0f;
508                 };
509
510                 auto y_pixels_to_gl = [canvas_height](int y) {
511                         return (y * 2.0f / canvas_height) - 1.0f;
512                 };
513
514                 auto inter_position = player_video().first->inter_position();
515                 auto inter_size = player_video().first->inter_size();
516
517                 float const border_x1 = x_pixels_to_gl (inter_position.x) + 1.0f - video_x;
518                 float const border_y1 = y_pixels_to_gl (inter_position.y) + 1.0f - video_y;
519                 float const border_x2 = x_pixels_to_gl (inter_position.x + inter_size.width) + 1.0f - video_x;
520                 float const border_y2 = y_pixels_to_gl (inter_position.y + inter_size.height) + 1.0f - video_y;
521
522                 float vertices[] = {
523                         // positions                  // texture coords
524                          video_x,   video_y,   0.0f,  1.0f, 0.0f,   // video texture top right    (index 0)
525                          video_x,  -video_y,   0.0f,  1.0f, 1.0f,   // video texture bottom right (index 1)
526                         -video_x,  -video_y,   0.0f,  0.0f, 1.0f,   // video texture bottom left  (index 2)
527                         -video_x,   video_y,   0.0f,  0.0f, 0.0f,   // video texture top left     (index 3)
528                          border_x1, border_y1, 0.0f,  0.0f, 0.0f,   // border bottom left         (index 4)
529                          border_x1, border_y2, 0.0f,  0.0f, 0.0f,   // border top left            (index 5)
530                          border_x2, border_y2, 0.0f,  0.0f, 0.0f,   // border top right           (index 6)
531                          border_x2, border_y1, 0.0f,  0.0f, 0.0f,   // border bottom right        (index 7)
532                 };
533
534                 /* Set the vertex shader's input data (GL_ARRAY_BUFFER) */
535                 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
536                 check_gl_error ("glBufferData");
537         }
538
539         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
540         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
541         check_gl_error ("glTexParameteri");
542
543         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
544         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
545         check_gl_error ("glTexParameterf");
546 }
547
548
549 void
550 GLVideoView::start ()
551 {
552         VideoView::start ();
553
554         boost::mutex::scoped_lock lm (_playing_mutex);
555         _playing = true;
556         _thread_work_condition.notify_all ();
557 }
558
559 void
560 GLVideoView::stop ()
561 {
562         boost::mutex::scoped_lock lm (_playing_mutex);
563         _playing = false;
564 }
565
566
567 void
568 GLVideoView::thread_playing ()
569 {
570         if (length() != dcpomatic::DCPTime()) {
571                 auto const next = position() + one_video_frame();
572
573                 if (next >= length()) {
574                         _viewer->finished ();
575                         return;
576                 }
577
578                 get_next_frame (false);
579                 set_image_and_draw ();
580         }
581
582         while (true) {
583                 optional<int> n = time_until_next_frame();
584                 if (!n || *n > 5) {
585                         break;
586                 }
587                 get_next_frame (true);
588                 add_dropped ();
589         }
590 }
591
592
593 void
594 GLVideoView::set_image_and_draw ()
595 {
596         auto pv = player_video().first;
597         if (pv) {
598                 set_image (pv);
599                 draw (pv->inter_position(), pv->inter_size());
600                 _viewer->image_changed (pv);
601         }
602 }
603
604
605 void
606 GLVideoView::thread ()
607 try
608 {
609         start_of_thread ("GLVideoView");
610
611 #if defined(DCPOMATIC_OSX)
612         /* Without this we see errors like
613          * ../src/osx/cocoa/glcanvas.mm(194): assert ""context"" failed in SwapBuffers(): should have current context [in thread 700006970000]
614          */
615         WXGLSetCurrentContext (_context->GetWXGLContext());
616 #else
617         if (!_setup_shaders_done) {
618                 setup_shaders ();
619                 _setup_shaders_done = true;
620         }
621 #endif
622
623 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
624         if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
625                 /* Enable vsync */
626                 Display* dpy = wxGetX11Display();
627                 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
628                 _vsync_enabled = true;
629         }
630 #endif
631
632 #ifdef DCPOMATIC_WINDOWS
633         if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
634                 /* Enable vsync */
635                 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
636                 if (swap) {
637                         swap (1);
638                         _vsync_enabled = true;
639                 }
640         }
641
642 #endif
643
644 #ifdef DCPOMATIC_OSX
645         /* Enable vsync */
646         GLint swapInterval = 1;
647         CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
648         _vsync_enabled = true;
649 #endif
650
651         _video_texture.reset(new Texture(_optimise_for_j2k ? 2 : 1));
652         _video_texture->bind();
653
654         while (true) {
655                 boost::mutex::scoped_lock lm (_playing_mutex);
656                 while (!_playing && !_one_shot) {
657                         _thread_work_condition.wait (lm);
658                 }
659                 lm.unlock ();
660
661                 if (_playing) {
662                         thread_playing ();
663                 } else if (_one_shot) {
664                         _one_shot = false;
665                         set_image_and_draw ();
666                 }
667
668                 boost::this_thread::interruption_point ();
669                 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
670         }
671
672         /* XXX: leaks _context, but that seems preferable to deleting it here
673          * without also deleting the wxGLCanvas.
674          */
675 }
676 catch (...)
677 {
678         store_current ();
679 }
680
681
682 VideoView::NextFrameResult
683 GLVideoView::display_next_frame (bool non_blocking)
684 {
685         NextFrameResult const r = get_next_frame (non_blocking);
686         request_one_shot ();
687         return r;
688 }
689
690
691 void
692 GLVideoView::request_one_shot ()
693 {
694         boost::mutex::scoped_lock lm (_playing_mutex);
695         _one_shot = true;
696         _thread_work_condition.notify_all ();
697 }
698
699
700 Texture::Texture (GLint unpack_alignment)
701         : _unpack_alignment (unpack_alignment)
702 {
703         glGenTextures (1, &_name);
704         check_gl_error ("glGenTextures");
705 }
706
707
708 Texture::~Texture ()
709 {
710         glDeleteTextures (1, &_name);
711 }
712
713
714 void
715 Texture::bind ()
716 {
717         glBindTexture(GL_TEXTURE_2D, _name);
718         check_gl_error ("glBindTexture");
719 }
720
721
722 bool
723 Texture::set (shared_ptr<const Image> image)
724 {
725         auto const create = !_size || image->size() != _size;
726         _size = image->size();
727
728         glPixelStorei (GL_UNPACK_ALIGNMENT, _unpack_alignment);
729         check_gl_error ("glPixelStorei");
730
731         auto const format = image->pixel_format() == AV_PIX_FMT_RGB24 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
732         auto const internal_format = image->pixel_format() == AV_PIX_FMT_RGB24 ? GL_RGBA8 : GL_RGBA12;
733
734         if (create) {
735                 glTexImage2D (GL_TEXTURE_2D, 0, internal_format, _size->width, _size->height, 0, GL_RGB, format, image->data()[0]);
736                 check_gl_error ("glTexImage2D");
737         } else {
738                 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, GL_RGB, format, image->data()[0]);
739                 check_gl_error ("glTexSubImage2D");
740         }
741
742         return create;
743 }
744