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