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