A little thread safety.
[dcpomatic.git] / src / wx / gl_video_view.cc
index ce17c03297c14b6c694a6232e17a95077a79da61..c3a6112831750641cedc226733010ba83b70fb94 100644 (file)
 */
 
 #include "gl_video_view.h"
+#include "film_viewer.h"
 #include "lib/image.h"
 #include "lib/dcpomatic_assert.h"
 #include "lib/exceptions.h"
+#include "lib/cross.h"
+#include "lib/player_video.h"
 #include <boost/bind.hpp>
 #include <iostream>
 
 #ifdef DCPOMATIC_OSX
 #include <OpenGL/glu.h>
 #include <OpenGL/glext.h>
-#else
+#include <OpenGL/CGLTypes.h>
+#include <OpenGL/OpenGL.h>
+#endif
+
+#ifdef DCPOMATIC_LINUX
+#include <GL/glu.h>
+#include <GL/glext.h>
+#include <GL/glxext.h>
+#endif
+
+#ifdef DCPOMATIC_WINDOWS
 #include <GL/glu.h>
 #include <GL/glext.h>
+#include <GL/wglext.h>
 #endif
 
 using std::cout;
 using boost::shared_ptr;
+using boost::optional;
 
-GLVideoView::GLVideoView (wxWindow *parent)
+GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
+       : VideoView (viewer)
+       , _vsync_enabled (false)
+       , _thread (0)
+       , _one_shot (false)
 {
        _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
-       _context = new wxGLContext (_canvas);
-       _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this, _1));
+       _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
+       _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
+
+#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);
        glBindTexture (GL_TEXTURE_2D, _id);
@@ -49,9 +96,13 @@ GLVideoView::GLVideoView (wxWindow *parent)
 
 GLVideoView::~GLVideoView ()
 {
+       if (_thread) {
+               _thread->interrupt ();
+               _thread->join ();
+       }
+       delete _thread;
+
        glDeleteTextures (1, &_id);
-       delete _context;
-       delete _canvas;
 }
 
 static void
@@ -64,10 +115,32 @@ check_gl_error (char const * last)
 }
 
 void
-GLVideoView::paint (wxPaintEvent &)
+GLVideoView::paint ()
 {
+       /* XXX_b: can't do this yet */
+#if 0
+        _viewer->state_timer().set("paint-panel");
        _canvas->SetCurrent (*_context);
        wxPaintDC dc (_canvas);
+       draw ();
+       _viewer->state_timer().unset();
+#endif
+}
+
+void
+GLVideoView::update ()
+{
+       if (!_canvas->IsShownOnScreen()) {
+               return;
+       }
+       /* XXX_b */
+//     wxClientDC dc (_canvas);
+//     draw ();
+}
+
+void
+GLVideoView::draw ()
+{
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        check_gl_error ("glClear");
 
@@ -75,19 +148,23 @@ GLVideoView::paint (wxPaintEvent &)
        check_gl_error ("glClearColor");
        glEnable (GL_TEXTURE_2D);
        check_gl_error ("glEnable GL_TEXTURE_2D");
-       glEnable (GL_COLOR_MATERIAL);
-       check_gl_error ("glEnable GL_COLOR_MATERIAL");
        glEnable (GL_BLEND);
        check_gl_error ("glEnable GL_BLEND");
        glDisable (GL_DEPTH_TEST);
        check_gl_error ("glDisable GL_DEPTH_TEST");
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
+       if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
+               return;
+       }
+
        glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
+       check_gl_error ("glViewport");
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
 
        gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
+       check_gl_error ("gluOrtho2d");
        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();
 
@@ -108,6 +185,52 @@ GLVideoView::paint (wxPaintEvent &)
                glEnd ();
        }
 
+       dcp::Size const out_size = _viewer->out_size ();
+       wxSize const canvas_size = _canvas->GetSize ();
+
+       if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
+               glBegin (GL_QUADS);
+               /* XXX: these colours are right for GNOME; may need adjusting for other OS */
+               glColor3ub (240, 240, 240);
+               glVertex2f (out_size.width, 0);
+               glVertex2f (canvas_size.GetWidth(), 0);
+               glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
+               glVertex2f (out_size.width, canvas_size.GetHeight());
+               glEnd ();
+               glColor3ub (255, 255, 255);
+       }
+
+       if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
+               glColor3ub (240, 240, 240);
+               int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
+               glBegin (GL_QUADS);
+               glVertex2f (0, 0);
+               glVertex2f (canvas_size.GetWidth(), 0);
+               glVertex2f (canvas_size.GetWidth(), gap);
+               glVertex2f (0, gap);
+               glEnd ();
+               glBegin (GL_QUADS);
+               glVertex2f (0, gap + out_size.height + 1);
+               glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
+               glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
+               glVertex2f (0, 2 * gap + out_size.height + 2);
+               glEnd ();
+               glColor3ub (255, 255, 255);
+       }
+
+       if (_viewer->outline_content()) {
+               glColor3ub (255, 0, 0);
+               glBegin (GL_LINE_LOOP);
+               Position<int> inter_position = _viewer->inter_position ();
+               dcp::Size inter_size = _viewer->inter_size ();
+               glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
+               glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
+               glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
+               glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
+               glEnd ();
+               glColor3ub (255, 255, 255);
+       }
+
        glFlush();
        _canvas->SwapBuffers();
 }
@@ -115,14 +238,17 @@ GLVideoView::paint (wxPaintEvent &)
 void
 GLVideoView::set_image (shared_ptr<const Image> image)
 {
-       /* XXX: put this somewhere sensible */
-       cout << glGetString (GL_VERSION) << "\n";
+       if (!image) {
+               _size = optional<dcp::Size>();
+               return;
+       }
 
        DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
        DCPOMATIC_ASSERT (!image->aligned());
 
        _size = image->size ();
        glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
+       check_gl_error ("glPixelStorei");
        glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
        check_gl_error ("glTexImage2D");
 
@@ -133,6 +259,115 @@ GLVideoView::set_image (shared_ptr<const Image> image)
        glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        check_gl_error ("glTexParameterf");
+}
+
+void
+GLVideoView::start ()
+{
+       _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));
+}
 
-       _canvas->Refresh ();
+void
+GLVideoView::stop ()
+{
+       if (_thread) {
+               _thread->interrupt ();
+               _thread->join ();
+       }
+       delete _thread;
+       _thread = 0;
+}
+
+bool
+GLVideoView::one_shot () const
+{
+       boost::mutex::scoped_lock lm (_one_shot_mutex);
+       return _one_shot;
 }
+
+void
+GLVideoView::set_one_shot (bool s)
+{
+       boost::mutex::scoped_lock lm (_one_shot_mutex);
+       _one_shot = s;
+}
+
+void
+GLVideoView::thread ()
+try
+{
+       /* XXX_b: check all calls and signal emissions in this method & protect them if necessary */
+       {
+               boost::mutex::scoped_lock lm (_context_mutex);
+               _context = new wxGLContext (_canvas);
+               _canvas->SetCurrent (*_context);
+       }
+
+       while (true) {
+               if (!film() && !one_shot()) {
+                       /* XXX: this should be an indefinite wait until
+                          one of our conditions becomes true.
+                        */
+                       dcpomatic_sleep_milliseconds (40);
+                       continue;
+               }
+
+               set_one_shot (false);
+
+               dcpomatic::DCPTime const next = position() + one_video_frame();
+
+               if (next >= film()->length()) {
+                       _viewer->stop ();
+                       _viewer->emit_finished ();
+                       continue;
+               }
+
+               get_next_frame (false);
+               {
+                       boost::mutex::scoped_lock lm (_mutex);
+                       set_image (_player_video.first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
+               }
+               draw ();
+
+               while (time_until_next_frame() < 5) {
+                       get_next_frame (true);
+               }
+
+               boost::this_thread::interruption_point ();
+               dcpomatic_sleep_milliseconds (time_until_next_frame());
+       }
+
+       {
+               boost::mutex::scoped_lock lm (_context_mutex);
+               delete _context;
+       }
+}
+catch (boost::thread_interrupted& e)
+{
+       /* XXX_b: store exceptions here */
+       delete _context;
+       return;
+}
+
+wxGLContext *
+GLVideoView::context () const
+{
+       boost::mutex::scoped_lock lm (_context_mutex);
+       return _context;
+}
+
+bool
+GLVideoView::display_next_frame (bool non_blocking)
+{
+       bool const g = get_next_frame (non_blocking);
+       set_one_shot (true);
+       return g;
+}
+
+dcpomatic::DCPTime
+GLVideoView::one_video_frame () const
+{
+       return dcpomatic::DCPTime::from_frames (1, film()->video_frame_rate());
+}
+
+