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;
63 using boost::optional;
64 #if BOOST_VERSION >= 106100
65 using namespace boost::placeholders;
67 using namespace dcpomatic;
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 border
199 * type = 1: draw XYZ image
200 * type = 2: draw RGB image
202 "uniform int type = 0;\n"
203 "uniform vec4 border_colour;\n"
204 "uniform mat4 colour_conversion;\n"
206 "out vec4 FragColor;\n"
208 "vec4 cubic(float x)\n"
210 "#define IN_GAMMA 2.2\n"
211 "#define OUT_GAMMA 0.384615385\n" // 1 / 2.6
212 "#define DCI_COEFFICIENT 0.91655528\n" // 48 / 53.37
215 " float x2 = x * x;\n"
216 " float x3 = x2 * x;\n"
218 " w.x = -x3 + 3 * x2 - 3 * x + 1;\n"
219 " w.y = 3 * x3 - 6 * x2 + 4;\n"
220 " w.z = -3 * x3 + 3 * x2 + 3 * x + 1;\n"
225 "vec4 texture_bicubic(sampler2D sampler, vec2 tex_coords)\n"
227 " vec2 tex_size = textureSize(sampler, 0);\n"
228 " vec2 inv_tex_size = 1.0 / tex_size;\n"
230 " tex_coords = tex_coords * tex_size - 0.5;\n"
232 " vec2 fxy = fract(tex_coords);\n"
233 " tex_coords -= fxy;\n"
235 " vec4 xcubic = cubic(fxy.x);\n"
236 " vec4 ycubic = cubic(fxy.y);\n"
238 " vec4 c = tex_coords.xxyy + vec2 (-0.5, +1.5).xyxy;\n"
240 " vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw);\n"
241 " vec4 offset = c + vec4 (xcubic.yw, ycubic.yw) / s;\n"
243 " offset *= inv_tex_size.xxyy;\n"
245 " vec4 sample0 = texture(sampler, offset.xz);\n"
246 " vec4 sample1 = texture(sampler, offset.yz);\n"
247 " vec4 sample2 = texture(sampler, offset.xw);\n"
248 " vec4 sample3 = texture(sampler, offset.yw);\n"
250 " float sx = s.x / (s.x + s.y);\n"
251 " float sy = s.z / (s.z + s.w);\n"
254 " mix(sample3, sample2, sx), mix(sample1, sample0, sx)\n"
262 " FragColor = border_colour;\n"
265 " FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
266 " FragColor.x = pow(FragColor.x, IN_GAMMA) / DCI_COEFFICIENT;\n"
267 " FragColor.y = pow(FragColor.y, IN_GAMMA) / DCI_COEFFICIENT;\n"
268 " FragColor.z = pow(FragColor.z, IN_GAMMA) / DCI_COEFFICIENT;\n"
269 " FragColor = colour_conversion * FragColor;\n"
270 " FragColor.x = pow(FragColor.x, OUT_GAMMA);\n"
271 " FragColor.y = pow(FragColor.y, OUT_GAMMA);\n"
272 " FragColor.z = pow(FragColor.z, OUT_GAMMA);\n"
275 " FragColor = texture_bicubic(texture_sampler, TexCoord);\n"
282 GLVideoView::ensure_context ()
285 wxGLContextAttrs attrs;
286 attrs.PlatformDefaults().CoreProfile().OGLVersion(4, 1).EndList();
287 _context = new wxGLContext (_canvas, nullptr, &attrs);
288 if (!_context->IsOK()) {
289 throw GLError ("Making GL context", -1);
295 /* Offset of video texture triangles in indices */
296 static constexpr int indices_video_texture = 0;
297 /* Offset of subtitle texture triangles in indices */
298 static constexpr int indices_subtitle_texture = 6;
299 /* Offset of border lines in indices */
300 static constexpr int indices_border = 12;
302 static constexpr unsigned int indices[] = {
303 0, 1, 3, // video texture triangle #1
304 1, 2, 3, // video texture triangle #2
305 4, 5, 7, // subtitle texture triangle #1
306 5, 6, 7, // subtitle texture triangle #2
307 8, 9, // border line #1
308 9, 10, // border line #2
309 10, 11, // border line #3
310 11, 8, // border line #4
315 GLVideoView::setup_shaders ()
317 DCPOMATIC_ASSERT (_canvas);
318 DCPOMATIC_ASSERT (_context);
319 auto r = _canvas->SetCurrent (*_context);
320 DCPOMATIC_ASSERT (r);
322 #ifdef DCPOMATIC_WINDOWS
325 throw GLError(reinterpret_cast<char const*>(glewGetErrorString(r)));
329 auto get_information = [this](GLenum name) {
330 auto s = glGetString (name);
332 _information[name] = std::string (reinterpret_cast<char const *>(s));
336 get_information (GL_VENDOR);
337 get_information (GL_RENDERER);
338 get_information (GL_VERSION);
339 get_information (GL_SHADING_LANGUAGE_VERSION);
341 glGenVertexArrays(1, &_vao);
342 check_gl_error ("glGenVertexArrays");
344 glGenBuffers(1, &vbo);
345 check_gl_error ("glGenBuffers");
347 glGenBuffers(1, &ebo);
348 check_gl_error ("glGenBuffers");
350 glBindVertexArray(_vao);
351 check_gl_error ("glBindVertexArray");
353 glBindBuffer(GL_ARRAY_BUFFER, vbo);
354 check_gl_error ("glBindBuffer");
356 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
357 check_gl_error ("glBindBuffer");
358 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
359 check_gl_error ("glBufferData");
361 /* position attribute to vertex shader (location = 0) */
362 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
363 glEnableVertexAttribArray(0);
364 /* texture coord attribute to vertex shader (location = 1) */
365 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
366 glEnableVertexAttribArray(1);
367 check_gl_error ("glEnableVertexAttribArray");
369 auto compile = [](GLenum type, char const* source) -> GLuint {
370 auto shader = glCreateShader(type);
371 DCPOMATIC_ASSERT (shader);
372 GLchar const * src[] = { static_cast<GLchar const *>(source) };
373 glShaderSource(shader, 1, src, nullptr);
374 check_gl_error ("glShaderSource");
375 glCompileShader(shader);
376 check_gl_error ("glCompileShader");
378 glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
381 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
383 if (log_length > 0) {
384 std::vector<char> log_char(log_length);
385 glGetShaderInfoLog(shader, log_length, nullptr, log_char.data());
386 log = string(log_char.data());
388 glDeleteShader(shader);
389 throw GLError(String::compose("Could not compile shader (%1)", log).c_str(), -1);
394 auto vertex_shader = compile (GL_VERTEX_SHADER, vertex_source);
395 auto fragment_shader = compile (GL_FRAGMENT_SHADER, fragment_source);
397 auto program = glCreateProgram();
398 check_gl_error ("glCreateProgram");
399 glAttachShader (program, vertex_shader);
400 check_gl_error ("glAttachShader");
401 glAttachShader (program, fragment_shader);
402 check_gl_error ("glAttachShader");
403 glLinkProgram (program);
404 check_gl_error ("glLinkProgram");
406 glGetProgramiv (program, GL_LINK_STATUS, &ok);
409 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
411 if (log_length > 0) {
412 std::vector<char> log_char(log_length);
413 glGetProgramInfoLog(program, log_length, nullptr, log_char.data());
414 log = string(log_char.data());
416 glDeleteProgram (program);
417 throw GLError(String::compose("Could not link shader (%1)", log).c_str(), -1);
419 glDeleteShader (vertex_shader);
420 glDeleteShader (fragment_shader);
422 glUseProgram (program);
424 _fragment_type = glGetUniformLocation (program, "type");
425 check_gl_error ("glGetUniformLocation");
426 set_border_colour (program);
428 auto conversion = dcp::ColourConversion::rec709_to_xyz();
429 boost::numeric::ublas::matrix<double> matrix = conversion.xyz_to_rgb ();
430 GLfloat gl_matrix[] = {
431 static_cast<float>(matrix(0, 0)), static_cast<float>(matrix(0, 1)), static_cast<float>(matrix(0, 2)), 0.0f,
432 static_cast<float>(matrix(1, 0)), static_cast<float>(matrix(1, 1)), static_cast<float>(matrix(1, 2)), 0.0f,
433 static_cast<float>(matrix(2, 0)), static_cast<float>(matrix(2, 1)), static_cast<float>(matrix(2, 2)), 0.0f,
434 0.0f, 0.0f, 0.0f, 1.0f
437 auto colour_conversion = glGetUniformLocation (program, "colour_conversion");
438 check_gl_error ("glGetUniformLocation");
439 glUniformMatrix4fv (colour_conversion, 1, GL_TRUE, gl_matrix);
442 check_gl_error ("glLineWidth");
444 check_gl_error ("glEnable");
445 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
446 check_gl_error ("glBlendFunc");
448 /* Reserve space for the GL_ARRAY_BUFFER */
449 glBufferData(GL_ARRAY_BUFFER, 12 * 5 * sizeof(float), nullptr, GL_STATIC_DRAW);
450 check_gl_error ("glBufferData");
455 GLVideoView::set_border_colour (GLuint program)
457 auto uniform = glGetUniformLocation (program, "border_colour");
458 check_gl_error ("glGetUniformLocation");
459 auto colour = outline_content_colour ();
460 glUniform4f (uniform, colour.Red() / 255.0f, colour.Green() / 255.0f, colour.Blue() / 255.0f, 1.0f);
461 check_gl_error ("glUniform4f");
468 auto pad = pad_colour();
469 glClearColor(pad.Red() / 255.0, pad.Green() / 255.0, pad.Blue() / 255.0, 1.0);
470 glClear (GL_COLOR_BUFFER_BIT);
471 check_gl_error ("glClear");
473 auto const size = _canvas_size.load();
474 int const width = size.GetWidth();
475 int const height = size.GetHeight();
477 if (width < 64 || height < 0) {
481 glViewport (0, 0, width, height);
482 check_gl_error ("glViewport");
484 glBindVertexArray(_vao);
485 check_gl_error ("glBindVertexArray");
486 glUniform1i(_fragment_type, _optimise_for_j2k ? 1 : 2);
487 _video_texture->bind();
488 glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_video_texture * sizeof(int)));
489 if (_have_subtitle_to_render) {
490 glUniform1i(_fragment_type, 2);
491 _subtitle_texture->bind();
492 glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_subtitle_texture * sizeof(int)));
494 if (_viewer->outline_content()) {
495 glUniform1i(_fragment_type, 0);
496 glDrawElements (GL_LINES, 8, GL_UNSIGNED_INT, reinterpret_cast<void*>(indices_border * sizeof(int)));
497 check_gl_error ("glDrawElements");
501 check_gl_error ("glFlush");
503 _canvas->SwapBuffers();
508 GLVideoView::set_image (shared_ptr<const PlayerVideo> pv)
510 shared_ptr<const Image> video = _optimise_for_j2k ? pv->raw_image() : pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
512 /* Only the player's black frames should be aligned at this stage, so this should
513 * almost always have no work to do.
515 video = Image::ensure_alignment (video, Image::Alignment::COMPACT);
517 /** If _optimise_for_j2k is true we render a XYZ image, doing the colourspace
518 * conversion, scaling and video range conversion in the GL shader.
519 * Othewise we render a RGB image without any shader-side processing.
522 /* XXX: video range conversion */
524 _video_texture->set (video);
526 auto const text = pv->text();
527 _have_subtitle_to_render = static_cast<bool>(text) && _optimise_for_j2k;
528 if (_have_subtitle_to_render) {
529 /* opt: only do this if it's a new subtitle? */
530 DCPOMATIC_ASSERT (text->image->alignment() == Image::Alignment::COMPACT);
531 _subtitle_texture->set (text->image);
535 auto const canvas_size = _canvas_size.load();
536 int const canvas_width = canvas_size.GetWidth();
537 int const canvas_height = canvas_size.GetHeight();
538 auto const inter_position = player_video().first->inter_position();
539 auto const inter_size = player_video().first->inter_size();
540 auto const out_size = player_video().first->out_size();
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);
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);
554 Rectangle (wxSize canvas_size, float x, float y, dcp::Size size)
555 : _canvas_size (canvas_size)
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);
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.
584 _vertices[12] = 0.0f;
585 _vertices[13] = 0.0f;
586 _vertices[14] = 0.0f;
591 _vertices[17] = 0.0f;
592 _vertices[18] = 0.0f;
593 _vertices[19] = 1.0f;
596 float const * vertices () const {
600 int const size () const {
601 return sizeof(_vertices);
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;
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());
619 if (_last_canvas_size.changed() || _last_inter_position.changed() || _last_inter_size.changed() || _last_out_size.changed()) {
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);
625 glBufferSubData (GL_ARRAY_BUFFER, 0, video.size(), video.vertices());
626 check_gl_error ("glBufferSubData (video)");
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)");
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)");
639 /* opt: where should these go? */
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");
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");
652 GLVideoView::start ()
656 boost::mutex::scoped_lock lm (_playing_mutex);
658 _thread_work_condition.notify_all ();
664 boost::mutex::scoped_lock lm (_playing_mutex);
670 GLVideoView::thread_playing ()
672 if (length() != dcpomatic::DCPTime()) {
673 auto const next = position() + one_video_frame();
675 if (next >= length()) {
676 _viewer->finished ();
680 get_next_frame (false);
681 set_image_and_draw ();
685 optional<int> n = time_until_next_frame();
689 get_next_frame (true);
696 GLVideoView::set_image_and_draw ()
698 auto pv = player_video().first;
706 _viewer->image_changed (pv);
712 GLVideoView::thread ()
715 start_of_thread ("GLVideoView");
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]
721 WXGLSetCurrentContext (_context->GetWXGLContext());
723 if (!_setup_shaders_done) {
725 _setup_shaders_done = true;
729 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
730 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
732 Display* dpy = wxGetX11Display();
733 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
734 _vsync_enabled = true;
738 #ifdef DCPOMATIC_WINDOWS
739 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
741 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
744 _vsync_enabled = true;
752 GLint swapInterval = 1;
753 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
754 _vsync_enabled = true;
757 _video_texture.reset(new Texture(_optimise_for_j2k ? 2 : 1));
758 _subtitle_texture.reset(new Texture(1));
761 boost::mutex::scoped_lock lm (_playing_mutex);
762 while (!_playing && !_one_shot) {
763 _thread_work_condition.wait (lm);
769 } else if (_one_shot) {
771 set_image_and_draw ();
774 boost::this_thread::interruption_point ();
775 dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
778 /* XXX: leaks _context, but that seems preferable to deleting it here
779 * without also deleting the wxGLCanvas.
788 VideoView::NextFrameResult
789 GLVideoView::display_next_frame (bool non_blocking)
791 NextFrameResult const r = get_next_frame (non_blocking);
798 GLVideoView::request_one_shot ()
800 boost::mutex::scoped_lock lm (_playing_mutex);
802 _thread_work_condition.notify_all ();
806 Texture::Texture (GLint unpack_alignment)
807 : _unpack_alignment (unpack_alignment)
809 glGenTextures (1, &_name);
810 check_gl_error ("glGenTextures");
816 glDeleteTextures (1, &_name);
823 glBindTexture(GL_TEXTURE_2D, _name);
824 check_gl_error ("glBindTexture");
829 Texture::set (shared_ptr<const Image> image)
831 auto const create = !_size || image->size() != _size;
832 _size = image->size();
834 glPixelStorei (GL_UNPACK_ALIGNMENT, _unpack_alignment);
835 check_gl_error ("glPixelStorei");
837 DCPOMATIC_ASSERT (image->alignment() == Image::Alignment::COMPACT);
839 GLint internal_format;
843 switch (image->pixel_format()) {
844 case AV_PIX_FMT_BGRA:
845 internal_format = GL_RGBA8;
847 type = GL_UNSIGNED_BYTE;
849 case AV_PIX_FMT_RGBA:
850 internal_format = GL_RGBA8;
852 type = GL_UNSIGNED_BYTE;
854 case AV_PIX_FMT_RGB24:
855 internal_format = GL_RGBA8;
857 type = GL_UNSIGNED_BYTE;
859 case AV_PIX_FMT_XYZ12:
860 internal_format = GL_RGBA12;
862 type = GL_UNSIGNED_SHORT;
865 throw PixelFormatError ("Texture::set", image->pixel_format());
871 glTexImage2D (GL_TEXTURE_2D, 0, internal_format, _size->width, _size->height, 0, format, type, image->data()[0]);
872 check_gl_error ("glTexImage2D");
874 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, format, type, image->data()[0]);
875 check_gl_error ("glTexSubImage2D");