+
+
+void
+GLVideoView::start ()
+{
+ VideoView::start ();
+
+ boost::mutex::scoped_lock lm (_playing_mutex);
+ _playing = true;
+ _thread_work_condition.notify_all ();
+}
+
+void
+GLVideoView::stop ()
+{
+ boost::mutex::scoped_lock lm (_playing_mutex);
+ _playing = false;
+}
+
+
+void
+GLVideoView::thread_playing ()
+{
+ if (length() != dcpomatic::DCPTime()) {
+ auto const next = position() + one_video_frame();
+
+ if (next >= length()) {
+ _viewer->finished ();
+ return;
+ }
+
+ get_next_frame (false);
+ set_image_and_draw ();
+ }
+
+ while (true) {
+ optional<int> n = time_until_next_frame();
+ if (!n || *n > 5) {
+ break;
+ }
+ get_next_frame (true);
+ add_dropped ();
+ }
+}
+
+
+void
+GLVideoView::set_image_and_draw ()
+{
+ auto pv = player_video().first;
+ if (pv) {
+ set_image (pv);
+ }
+
+ draw ();
+
+ if (pv) {
+ _viewer->image_changed (pv);
+ }
+}
+
+
+void
+GLVideoView::thread ()
+try
+{
+ start_of_thread ("GLVideoView");
+
+#if defined(DCPOMATIC_OSX)
+ /* Without this we see errors like
+ * ../src/osx/cocoa/glcanvas.mm(194): assert ""context"" failed in SwapBuffers(): should have current context [in thread 700006970000]
+ */
+ WXGLSetCurrentContext (_context->GetWXGLContext());
+#else
+ if (!_setup_shaders_done) {
+ setup_shaders ();
+ _setup_shaders_done = true;
+ }
+#endif
+
+#if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
+ if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
+ /* Enable vsync */
+ Display* dpy = wxGetX11Display();
+ glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
+ _vsync_enabled = true;
+ }
+#endif
+
+#ifdef DCPOMATIC_WINDOWS
+ if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
+ /* Enable vsync */
+ PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
+ if (swap) {
+ swap (1);
+ _vsync_enabled = true;
+ }
+ }
+
+#endif
+
+#ifdef DCPOMATIC_OSX
+ /* Enable vsync */
+ GLint swapInterval = 1;
+ CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
+ _vsync_enabled = true;
+#endif
+
+ _video_texture.reset(new Texture(_optimise_for_j2k ? 2 : 1));
+ _subtitle_texture.reset(new Texture(1));
+
+ while (true) {
+ boost::mutex::scoped_lock lm (_playing_mutex);
+ while (!_playing && !_one_shot) {
+ _thread_work_condition.wait (lm);
+ }
+ lm.unlock ();
+
+ if (_playing) {
+ thread_playing ();
+ } else if (_one_shot) {
+ _one_shot = false;
+ set_image_and_draw ();
+ }
+
+ boost::this_thread::interruption_point ();
+ dcpomatic_sleep_milliseconds (time_until_next_frame().get_value_or(0));
+ }
+
+ /* XXX: leaks _context, but that seems preferable to deleting it here
+ * without also deleting the wxGLCanvas.
+ */
+}
+catch (...)
+{
+ store_current ();
+}
+
+
+VideoView::NextFrameResult
+GLVideoView::display_next_frame (bool non_blocking)
+{
+ NextFrameResult const r = get_next_frame (non_blocking);
+ request_one_shot ();
+ return r;
+}
+
+
+void
+GLVideoView::request_one_shot ()
+{
+ boost::mutex::scoped_lock lm (_playing_mutex);
+ _one_shot = true;
+ _thread_work_condition.notify_all ();
+}
+
+
+Texture::Texture (GLint unpack_alignment)
+ : _unpack_alignment (unpack_alignment)
+{
+ glGenTextures (1, &_name);
+ check_gl_error ("glGenTextures");
+}
+
+
+Texture::~Texture ()
+{
+ glDeleteTextures (1, &_name);
+}
+
+
+void
+Texture::bind ()
+{
+ glBindTexture(GL_TEXTURE_2D, _name);
+ check_gl_error ("glBindTexture");
+}
+
+
+void
+Texture::set (shared_ptr<const Image> image)
+{
+ auto const create = !_size || image->size() != _size;
+ _size = image->size();
+
+ glPixelStorei (GL_UNPACK_ALIGNMENT, _unpack_alignment);
+ check_gl_error ("glPixelStorei");
+
+ DCPOMATIC_ASSERT (image->alignment() == Image::Alignment::COMPACT);
+
+ GLint internal_format;
+ GLenum format;
+ GLenum type;
+
+ switch (image->pixel_format()) {
+ case AV_PIX_FMT_BGRA:
+ internal_format = GL_RGBA8;
+ format = GL_BGRA;
+ type = GL_UNSIGNED_BYTE;
+ break;
+ case AV_PIX_FMT_RGBA:
+ internal_format = GL_RGBA8;
+ format = GL_RGBA;
+ type = GL_UNSIGNED_BYTE;
+ break;
+ case AV_PIX_FMT_RGB24:
+ internal_format = GL_RGBA8;
+ format = GL_RGB;
+ type = GL_UNSIGNED_BYTE;
+ break;
+ case AV_PIX_FMT_XYZ12:
+ internal_format = GL_RGBA12;
+ format = GL_RGB;
+ type = GL_UNSIGNED_SHORT;
+ break;
+ default:
+ throw PixelFormatError ("Texture::set", image->pixel_format());
+ }
+
+ bind ();
+
+ if (create) {
+ glTexImage2D (GL_TEXTURE_2D, 0, internal_format, _size->width, _size->height, 0, format, type, image->data()[0]);
+ check_gl_error ("glTexImage2D");
+ } else {
+ glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, _size->width, _size->height, format, type, image->data()[0]);
+ check_gl_error ("glTexSubImage2D");
+ }
+}
+
+#endif