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