+
+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()) {
+ dcpomatic::DCPTime 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 ()
+{
+ shared_ptr<PlayerVideo> pv = player_video().first;
+ if (pv) {
+ set_image (pv->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, false, true));
+ draw (pv->inter_position(), pv->inter_size());
+ _viewer->image_changed (pv);
+ }
+}
+
+
+void
+GLVideoView::thread ()
+try
+{
+ {
+ boost::mutex::scoped_lock lm (_canvas_mutex);
+
+#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
+ /* We must call this here on Linux otherwise we get no image (for reasons
+ * that aren't clear). However, doing ensure_context() from this thread
+ * on macOS gives
+ * "[NSOpenGLContext setView:] must be called from the main thread".
+ */
+ ensure_context ();
+#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
+
+ glGenTextures (1, &_id);
+ check_gl_error ("glGenTextures");
+ glBindTexture (GL_TEXTURE_2D, _id);
+ check_gl_error ("glBindTexture");
+ glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+ check_gl_error ("glPixelStorei");
+
+ 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 (boost::thread_interrupted& e)
+{
+ 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 ();
+}
+