2 Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #ifdef DCPOMATIC_WINDOWS
26 #include "gl_video_view.h"
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)
31 #include "film_viewer.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>
44 #define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
45 #include <OpenGL/OpenGL.h>
46 #include <OpenGL/gl3.h>
49 #ifdef DCPOMATIC_LINUX
54 #ifdef DCPOMATIC_WINDOWS
56 #include <GL/wglext.h>
61 using std::shared_ptr;
64 using boost::optional;
65 #if BOOST_VERSION >= 106100
66 using namespace boost::placeholders;
71 check_gl_error (char const * last)
73 GLenum const e = glGetError ();
74 if (e != GL_NO_ERROR) {
75 throw GLError (last, e);
80 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
83 , _vsync_enabled (false)
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
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
96 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::update, this));
97 _canvas->Bind (wxEVT_SIZE, boost::bind(&GLVideoView::size_changed, this, _1));
99 _canvas->Bind (wxEVT_TIMER, boost::bind(&GLVideoView::check_for_butler_errors, this));
100 _timer.reset (new wxTimer(_canvas));
101 _timer->Start (2000);
106 GLVideoView::size_changed (wxSizeEvent const& ev)
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);
117 GLVideoView::~GLVideoView ()
119 boost::this_thread::disable_interruption dis;
122 _thread.interrupt ();
128 GLVideoView::check_for_butler_errors ()
130 if (!_viewer->butler()) {
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())));
144 /** Called from the UI thread */
146 GLVideoView::update ()
148 if (!_canvas->IsShownOnScreen()) {
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
159 /* macOS gives errors if we don't do this (and therefore [NSOpenGLContext setView:]) from the main thread */
160 if (!_setup_shaders_done) {
162 _setup_shaders_done = true;
166 if (!_thread.joinable()) {
167 _thread = boost::thread (boost::bind(&GLVideoView::thread, this));
176 static constexpr char vertex_source[] =
177 "#version 330 core\n"
179 "layout (location = 0) in vec3 in_pos;\n"
180 "layout (location = 1) in vec2 in_tex_coord;\n"
182 "out vec2 TexCoord;\n"
186 " gl_Position = vec4(in_pos, 1.0);\n"
187 " TexCoord = in_tex_coord;\n"
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"
195 "in vec2 TexCoord;\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
202 * See FragmentType enum below.
204 "uniform int type = 0;\n"
205 "uniform vec4 outline_content_colour;\n"
206 "uniform vec4 crop_guess_colour;\n"
207 "uniform mat4 colour_conversion;\n"
209 "out vec4 FragColor;\n"
211 "vec4 cubic(float x)\n"
213 "#define IN_GAMMA 2.2\n"
214 "#define OUT_GAMMA 0.384615385\n" // 1 / 2.6
215 "#define DCI_COEFFICIENT 0.91655528\n" // 48 / 53.37
218 " float x2 = x * x;\n"
219 " float x3 = x2 * x;\n"
221 " w.x = -x3 + 3 * x2 - 3 * x + 1;\n"
222 " w.y = 3 * x3 - 6 * x2 + 4;\n"
223 " w.z = -3 * x3 + 3 * x2 + 3 * x + 1;\n"
228 "vec4 texture_bicubic(sampler2D sampler, vec2 tex_coords)\n"
230 " vec2 tex_size = textureSize(sampler, 0);\n"
231 " vec2 inv_tex_size = 1.0 / tex_size;\n"
233 " tex_coords = tex_coords * tex_size - 0.5;\n"
235 " vec2 fxy = fract(tex_coords);\n"
236 " tex_coords -= fxy;\n"
238 " vec4 xcubic = cubic(fxy.x);\n"
239 " vec4 ycubic = cubic(fxy.y);\n"
241 " vec4 c = tex_coords.xxyy + vec2 (-0.5, +1.5).xyxy;\n"
243 " vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw);\n"
244 " vec4 offset = c + vec4 (xcubic.yw, ycubic.yw) / s;\n"
246 " offset *= inv_tex_size.xxyy;\n"
248 " vec4 sample0 = texture(sampler, offset.xz);\n"
249 " vec4 sample1 = texture(sampler, offset.yz);\n"
250 " vec4 sample2 = texture(sampler, offset.xw);\n"
251 " vec4 sample3 = texture(sampler, offset.yw);\n"
253 " float sx = s.x / (s.x + s.y);\n"
254 " float sy = s.z / (s.z + s.w);\n"
257 " mix(sample3, sample2, sx), mix(sample1, sample0, sx)\n"
265 " FragColor = outline_content_colour;\n"
268 " FragColor = crop_guess_colour;\n"
271 " FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
272 " FragColor.x = pow(FragColor.x, IN_GAMMA) / DCI_COEFFICIENT;\n"
273 " FragColor.y = pow(FragColor.y, IN_GAMMA) / DCI_COEFFICIENT;\n"
274 " FragColor.z = pow(FragColor.z, IN_GAMMA) / DCI_COEFFICIENT;\n"
275 " FragColor = colour_conversion * FragColor;\n"
276 " FragColor.x = pow(FragColor.x, OUT_GAMMA);\n"
277 " FragColor.y = pow(FragColor.y, OUT_GAMMA);\n"
278 " FragColor.z = pow(FragColor.z, OUT_GAMMA);\n"
281 " FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
287 enum class FragmentType
297 GLVideoView::ensure_context ()
300 wxGLContextAttrs attrs;
301 attrs.PlatformDefaults().CoreProfile().OGLVersion(4, 1).EndList();
302 _context = new wxGLContext (_canvas, nullptr, &attrs);
303 if (!_context->IsOK()) {
304 throw GLError ("Making GL context", -1);
310 /* Offset and number of indices for the things in the indices array below */
311 static constexpr int indices_video_texture_offset = 0;
312 static constexpr int indices_video_texture_number = 6;
313 static constexpr int indices_subtitle_texture_offset = indices_video_texture_offset + indices_video_texture_number;
314 static constexpr int indices_subtitle_texture_number = 6;
315 static constexpr int indices_outline_content_offset = indices_subtitle_texture_offset + indices_subtitle_texture_number;
316 static constexpr int indices_outline_content_number = 8;
317 static constexpr int indices_crop_guess_offset = indices_outline_content_offset + indices_outline_content_number;
318 static constexpr int indices_crop_guess_number = 8;
320 static constexpr unsigned int indices[] = {
321 0, 1, 3, // video texture triangle #1
322 1, 2, 3, // video texture triangle #2
323 4, 5, 7, // subtitle texture triangle #1
324 5, 6, 7, // subtitle texture triangle #2
325 8, 9, // outline content line #1
326 9, 10, // outline content line #2
327 10, 11, // outline content line #3
328 11, 8, // outline content line #4
329 12, 13, // crop guess line #1
330 13, 14, // crop guess line #2
331 14, 15, // crop guess line #3
332 15, 12, // crop guess line #4
335 /* Offsets of things in the GL_ARRAY_BUFFER */
336 static constexpr int array_buffer_video_offset = 0;
337 static constexpr int array_buffer_subtitle_offset = array_buffer_video_offset + 4 * 5 * sizeof(float);
338 static constexpr int array_buffer_outline_content_offset = array_buffer_subtitle_offset + 4 * 5 * sizeof(float);
339 static constexpr int array_buffer_crop_guess_offset = array_buffer_outline_content_offset + 4 * 5 * sizeof(float);
343 GLVideoView::setup_shaders ()
345 DCPOMATIC_ASSERT (_canvas);
346 DCPOMATIC_ASSERT (_context);
347 auto r = _canvas->SetCurrent (*_context);
348 DCPOMATIC_ASSERT (r);
350 #ifdef DCPOMATIC_WINDOWS
353 throw GLError(reinterpret_cast<char const*>(glewGetErrorString(r)));
357 auto get_information = [this](GLenum name) {
358 auto s = glGetString (name);
360 _information[name] = std::string (reinterpret_cast<char const *>(s));
364 get_information (GL_VENDOR);
365 get_information (GL_RENDERER);
366 get_information (GL_VERSION);
367 get_information (GL_SHADING_LANGUAGE_VERSION);
369 glGenVertexArrays(1, &_vao);
370 check_gl_error ("glGenVertexArrays");
372 glGenBuffers(1, &vbo);
373 check_gl_error ("glGenBuffers");
375 glGenBuffers(1, &ebo);
376 check_gl_error ("glGenBuffers");
378 glBindVertexArray(_vao);
379 check_gl_error ("glBindVertexArray");
381 glBindBuffer(GL_ARRAY_BUFFER, vbo);
382 check_gl_error ("glBindBuffer");
384 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
385 check_gl_error ("glBindBuffer");
386 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
387 check_gl_error ("glBufferData");
389 /* position attribute to vertex shader (location = 0) */
390 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
391 glEnableVertexAttribArray(0);
392 /* texture coord attribute to vertex shader (location = 1) */
393 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
394 glEnableVertexAttribArray(1);
395 check_gl_error ("glEnableVertexAttribArray");
397 auto compile = [](GLenum type, char const* source) -> GLuint {
398 auto shader = glCreateShader(type);
399 DCPOMATIC_ASSERT (shader);
400 GLchar const * src[] = { static_cast<GLchar const *>(source) };
401 glShaderSource(shader, 1, src, nullptr);
402 check_gl_error ("glShaderSource");
403 glCompileShader(shader);
404 check_gl_error ("glCompileShader");
406 glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
409 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
411 if (log_length > 0) {
412 std::vector<char> log_char(log_length);
413 glGetShaderInfoLog(shader, log_length, nullptr, log_char.data());
414 log = string(log_char.data());
416 glDeleteShader(shader);
417 throw GLError(String::compose("Could not compile shader (%1)", log).c_str(), -1);
422 auto vertex_shader = compile (GL_VERTEX_SHADER, vertex_source);
423 auto fragment_shader = compile (GL_FRAGMENT_SHADER, fragment_source);
425 auto program = glCreateProgram();
426 check_gl_error ("glCreateProgram");
427 glAttachShader (program, vertex_shader);
428 check_gl_error ("glAttachShader");
429 glAttachShader (program, fragment_shader);
430 check_gl_error ("glAttachShader");
431 glLinkProgram (program);
432 check_gl_error ("glLinkProgram");
434 glGetProgramiv (program, GL_LINK_STATUS, &ok);
437 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
439 if (log_length > 0) {
440 std::vector<char> log_char(log_length);
441 glGetProgramInfoLog(program, log_length, nullptr, log_char.data());
442 log = string(log_char.data());
444 glDeleteProgram (program);
445 throw GLError(String::compose("Could not link shader (%1)", log).c_str(), -1);
447 glDeleteShader (vertex_shader);
448 glDeleteShader (fragment_shader);
450 glUseProgram (program);
452 _fragment_type = glGetUniformLocation (program, "type");
453 check_gl_error ("glGetUniformLocation");
454 set_outline_content_colour (program);
455 set_crop_guess_colour (program);
457 auto conversion = dcp::ColourConversion::rec709_to_xyz();
458 boost::numeric::ublas::matrix<double> matrix = conversion.xyz_to_rgb ();
459 GLfloat gl_matrix[] = {
460 static_cast<float>(matrix(0, 0)), static_cast<float>(matrix(0, 1)), static_cast<float>(matrix(0, 2)), 0.0f,
461 static_cast<float>(matrix(1, 0)), static_cast<float>(matrix(1, 1)), static_cast<float>(matrix(1, 2)), 0.0f,
462 static_cast<float>(matrix(2, 0)), static_cast<float>(matrix(2, 1)), static_cast<float>(matrix(2, 2)), 0.0f,
463 0.0f, 0.0f, 0.0f, 1.0f
466 auto colour_conversion = glGetUniformLocation (program, "colour_conversion");
467 check_gl_error ("glGetUniformLocation");
468 glUniformMatrix4fv (colour_conversion, 1, GL_TRUE, gl_matrix);
471 check_gl_error ("glLineWidth");
473 check_gl_error ("glEnable");
474 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
475 check_gl_error ("glBlendFunc");
477 /* Reserve space for the GL_ARRAY_BUFFER */
478 glBufferData(GL_ARRAY_BUFFER, 16 * 5 * sizeof(float), nullptr, GL_STATIC_DRAW);
479 check_gl_error ("glBufferData");
484 GLVideoView::set_outline_content_colour (GLuint program)
486 auto uniform = glGetUniformLocation (program, "outline_content_colour");
487 check_gl_error ("glGetUniformLocation");
488 auto colour = outline_content_colour ();
489 glUniform4f (uniform, colour.Red() / 255.0f, colour.Green() / 255.0f, colour.Blue() / 255.0f, 1.0f);
490 check_gl_error ("glUniform4f");
495 GLVideoView::set_crop_guess_colour (GLuint program)
497 auto uniform = glGetUniformLocation (program, "crop_guess_colour");
498 check_gl_error ("glGetUniformLocation");
499 auto colour = crop_guess_colour ();
500 glUniform4f (uniform, colour.Red() / 255.0f, colour.Green() / 255.0f, colour.Blue() / 255.0f, 1.0f);
501 check_gl_error ("glUniform4f");
508 auto pad = pad_colour();
509 glClearColor(pad.Red() / 255.0, pad.Green() / 255.0, pad.Blue() / 255.0, 1.0);
510 glClear (GL_COLOR_BUFFER_BIT);
511 check_gl_error ("glClear");
513 auto const size = _canvas_size.load();
514 int const width = size.GetWidth();
515 int const height = size.GetHeight();
517 if (width < 64 || height < 0) {
521 glViewport (0, 0, width, height);
522 check_gl_error ("glViewport");
524 glBindVertexArray(_vao);
525 check_gl_error ("glBindVertexArray");
526 glUniform1i(_fragment_type, static_cast<GLint>(_optimise_for_j2k ? FragmentType::XYZ_IMAGE : FragmentType::RGB_IMAGE));
527 _video_texture->bind();
528 glDrawElements (GL_TRIANGLES, indices_video_texture_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_video_texture_offset * sizeof(int)));
529 if (_have_subtitle_to_render) {
530 glUniform1i(_fragment_type, static_cast<GLint>(FragmentType::RGB_IMAGE));
531 _subtitle_texture->bind();
532 glDrawElements (GL_TRIANGLES, indices_subtitle_texture_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_subtitle_texture_offset * sizeof(int)));
534 if (_viewer->outline_content()) {
535 glUniform1i(_fragment_type, static_cast<GLint>(FragmentType::OUTLINE_CONTENT));
536 glDrawElements (GL_LINES, indices_outline_content_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_outline_content_offset * sizeof(int)));
537 check_gl_error ("glDrawElements");
539 if (auto guess = _viewer->crop_guess()) {
540 glUniform1i(_fragment_type, 1);
541 glDrawElements (GL_LINES, indices_crop_guess_number, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_crop_guess_offset * sizeof(int)));
542 check_gl_error ("glDrawElements");
546 check_gl_error ("glFlush");
548 _canvas->SwapBuffers();
553 GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
555 shared_ptr<const Image> video = _optimise_for_j2k ? pv->raw_image() : pv->image(bind(&PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
557 /* Only the player's black frames should be aligned at this stage, so this should
558 * almost always have no work to do.
560 video = Image::ensure_alignment (video, Image::Alignment::COMPACT);
562 /** If _optimise_for_j2k is true we render a XYZ image, doing the colourspace
563 * conversion, scaling and video range conversion in the GL shader.
564 * Othewise we render a RGB image without any shader-side processing.
567 /* XXX: video range conversion */
569 _video_texture->set (video);
571 auto const text = pv->text();
572 _have_subtitle_to_render = static_cast<bool>(text) && _optimise_for_j2k;
573 if (_have_subtitle_to_render) {
574 /* opt: only do this if it's a new subtitle? */
575 DCPOMATIC_ASSERT (text->image->alignment() == Image::Alignment::COMPACT);
576 _subtitle_texture->set (text->image);
580 auto const canvas_size = _canvas_size.load();
581 int const canvas_width = canvas_size.GetWidth();
582 int const canvas_height = canvas_size.GetHeight();
583 auto const inter_position = player_video().first->inter_position();
584 auto const inter_size = player_video().first->inter_size();
585 auto const out_size = player_video().first->out_size();
586 auto const crop_guess = _viewer->crop_guess();
588 auto x_offset = std::max(0, (canvas_width - out_size.width) / 2);
589 auto y_offset = std::max(0, (canvas_height - out_size.height) / 2);
591 _last_canvas_size.set_next (canvas_size);
592 _last_video_size.set_next (video->size());
593 _last_inter_position.set_next (inter_position);
594 _last_inter_size.set_next (inter_size);
595 _last_out_size.set_next (out_size);
596 _last_crop_guess.set_next (crop_guess);
601 Rectangle (wxSize canvas_size, float x, float y, dcp::Size size)
602 : _canvas_size (canvas_size)
604 auto const x1 = x_pixels_to_gl(x);
605 auto const y1 = y_pixels_to_gl(y);
606 auto const x2 = x_pixels_to_gl(x + size.width);
607 auto const y2 = y_pixels_to_gl(y + size.height);
609 /* The texture coordinates here have to account for the fact that when we put images into the texture OpenGL
610 * expected us to start at the lower left but we actually started at the top left. So although the
611 * top of the texture is at 1.0 we pretend it's the other way round.
631 _vertices[12] = 0.0f;
632 _vertices[13] = 0.0f;
633 _vertices[14] = 0.0f;
638 _vertices[17] = 0.0f;
639 _vertices[18] = 0.0f;
640 _vertices[19] = 1.0f;
643 float const * vertices () const {
647 int const size () const {
648 return sizeof(_vertices);
652 /* @param x x position in pixels where 0 is left and canvas_width is right on screen */
653 float x_pixels_to_gl(int x) const {
654 return (x * 2.0f / _canvas_size.GetWidth()) - 1.0f;
657 /* @param y y position in pixels where 0 is top and canvas_height is bottom on screen */
658 float y_pixels_to_gl(int y) const {
659 return 1.0f - (y * 2.0f / _canvas_size.GetHeight());
666 auto const sizing_changed = _last_canvas_size.changed() || _last_inter_position.changed() || _last_inter_size.changed() || _last_out_size.changed();
668 if (sizing_changed) {
669 const auto video = _optimise_for_j2k ?
670 Rectangle(canvas_size, inter_position.x + x_offset, inter_position.y + y_offset, inter_size)
671 : Rectangle(canvas_size, x_offset, y_offset, out_size);
673 glBufferSubData (GL_ARRAY_BUFFER, array_buffer_video_offset, video.size(), video.vertices());
674 check_gl_error ("glBufferSubData (video)");
676 const auto outline_content = Rectangle(canvas_size, inter_position.x + x_offset, inter_position.y + y_offset, inter_size);
677 glBufferSubData (GL_ARRAY_BUFFER, array_buffer_outline_content_offset, outline_content.size(), outline_content.vertices());
678 check_gl_error ("glBufferSubData (outline_content)");
681 if ((sizing_changed || _last_crop_guess.changed()) && crop_guess) {
682 auto const crop_guess_rectangle = Rectangle(
684 inter_position.x + x_offset + inter_size.width * crop_guess->x,
685 inter_position.y + y_offset + inter_size.height * crop_guess->y,
686 dcp::Size(inter_size.width * crop_guess->width, inter_size.height * crop_guess->height)
688 glBufferSubData (GL_ARRAY_BUFFER, array_buffer_crop_guess_offset, crop_guess_rectangle.size(), crop_guess_rectangle.vertices());
689 check_gl_error ("glBufferSubData (crop_guess_rectangle)");
692 if (_have_subtitle_to_render) {
693 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());
694 glBufferSubData (GL_ARRAY_BUFFER, array_buffer_subtitle_offset, subtitle.size(), subtitle.vertices());
695 check_gl_error ("glBufferSubData (subtitle)");
698 /* opt: where should these go? */
700 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
701 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
702 check_gl_error ("glTexParameteri");
704 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
705 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
706 check_gl_error ("glTexParameterf");
711 GLVideoView::start ()
715 boost::mutex::scoped_lock lm (_playing_mutex);
717 _thread_work_condition.notify_all ();
723 boost::mutex::scoped_lock lm (_playing_mutex);
729 GLVideoView::thread_playing ()
731 if (length() != dcpomatic::DCPTime()) {
732 auto const next = position() + one_video_frame();
734 if (next >= length()) {
735 _viewer->finished ();
739 get_next_frame (false);
740 set_image_and_draw ();
744 optional<int> n = time_until_next_frame();
748 get_next_frame (true);
755 GLVideoView::set_image_and_draw ()
757 auto pv = player_video().first;
765 _viewer->image_changed (pv);
771 GLVideoView::thread ()
774 start_of_thread ("GLVideoView");
776 #if defined(DCPOMATIC_OSX)
777 /* Without this we see errors like
778 * ../src/osx/cocoa/glcanvas.mm(194): assert ""context"" failed in SwapBuffers(): should have current context [in thread 700006970000]
780 WXGLSetCurrentContext (_context->GetWXGLContext());
782 if (!_setup_shaders_done) {
784 _setup_shaders_done = true;
788 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
789 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
791 Display* dpy = wxGetX11Display();
792 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
793 _vsync_enabled = true;
797 #ifdef DCPOMATIC_WINDOWS
798 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
800 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
803 _vsync_enabled = true;
811 GLint swapInterval = 1;
812 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
813 _vsync_enabled = true;
816 _video_texture.reset(new Texture(_optimise_for_j2k ? 2 : 1));
817 _subtitle_texture.reset(new Texture(1));
820 boost::mutex::scoped_lock lm (_playing_mutex);
821 while (!_playing && !_one_shot) {
822 _thread_work_condition.wait (lm);
828 } else if (_one_shot) {
830 set_image_and_draw ();
833 boost::this_thread::interruption_point ();
834 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
837 /* XXX: leaks _context, but that seems preferable to deleting it here
838 * without also deleting the wxGLCanvas.
847 VideoView::NextFrameResult
848 GLVideoView::display_next_frame (bool non_blocking)
850 NextFrameResult const r = get_next_frame (non_blocking);
857 GLVideoView::request_one_shot ()
859 boost::mutex::scoped_lock lm (_playing_mutex);
861 _thread_work_condition.notify_all ();
865 Texture::Texture (GLint unpack_alignment)
866 : _unpack_alignment (unpack_alignment)
868 glGenTextures (1, &_name);
869 check_gl_error ("glGenTextures");
875 glDeleteTextures (1, &_name);
882 glBindTexture(GL_TEXTURE_2D, _name);
883 check_gl_error ("glBindTexture");
888 Texture::set (shared_ptr<const Image> image)
890 auto const create = !_size || image->size() != _size;
891 _size = image->size();
893 glPixelStorei (GL_UNPACK_ALIGNMENT, _unpack_alignment);
894 check_gl_error ("glPixelStorei");
896 DCPOMATIC_ASSERT (image->alignment() == Image::Alignment::COMPACT);
898 GLint internal_format;
902 switch (image->pixel_format()) {
903 case AV_PIX_FMT_BGRA:
904 internal_format = GL_RGBA8;
906 type = GL_UNSIGNED_BYTE;
908 case AV_PIX_FMT_RGBA:
909 internal_format = GL_RGBA8;
911 type = GL_UNSIGNED_BYTE;
913 case AV_PIX_FMT_RGB24:
914 internal_format = GL_RGBA8;
916 type = GL_UNSIGNED_BYTE;
918 case AV_PIX_FMT_XYZ12:
919 internal_format = GL_RGBA12;
921 type = GL_UNSIGNED_SHORT;
924 throw PixelFormatError ("Texture::set", image->pixel_format());
930 glTexImage2D (GL_TEXTURE_2D, 0, internal_format, _size->width, _size->height, 0, format, type, image->data()[0]);
931 check_gl_error ("glTexImage2D");
933 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, format, type, image->data()[0]);
934 check_gl_error ("glTexSubImage2D");