2 Copyright (C) 2018-2019 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/>.
21 #include "gl_video_view.h"
22 #include "film_viewer.h"
23 #include "lib/image.h"
24 #include "lib/dcpomatic_assert.h"
25 #include "lib/exceptions.h"
26 #include "lib/cross.h"
27 #include "lib/player_video.h"
28 #include <boost/bind.hpp>
32 #include <OpenGL/glu.h>
33 #include <OpenGL/glext.h>
34 #include <OpenGL/CGLTypes.h>
35 #include <OpenGL/OpenGL.h>
38 #ifdef DCPOMATIC_LINUX
41 #include <GL/glxext.h>
44 #ifdef DCPOMATIC_WINDOWS
47 #include <GL/wglext.h>
51 using boost::shared_ptr;
52 using boost::optional;
54 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
56 , _vsync_enabled (false)
59 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
60 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
61 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
63 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
64 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
66 Display* dpy = wxGetX11Display();
67 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
68 _vsync_enabled = true;
72 #ifdef DCPOMATIC_WINDOWS
73 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
75 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
78 _vsync_enabled = true;
86 GLint swapInterval = 1;
87 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
88 _vsync_enabled = true;
91 glGenTextures (1, &_id);
92 glBindTexture (GL_TEXTURE_2D, _id);
93 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
96 GLVideoView::~GLVideoView ()
99 _thread->interrupt ();
104 glDeleteTextures (1, &_id);
108 check_gl_error (char const * last)
110 GLenum const e = glGetError ();
111 if (e != GL_NO_ERROR) {
112 throw GLError (last, e);
117 GLVideoView::paint ()
119 /* XXX_b: can't do this yet */
121 _viewer->state_timer().set("paint-panel");
122 _canvas->SetCurrent (*_context);
123 wxPaintDC dc (_canvas);
125 _viewer->state_timer().unset();
130 GLVideoView::update ()
132 if (!_canvas->IsShownOnScreen()) {
136 // wxClientDC dc (_canvas);
143 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144 check_gl_error ("glClear");
146 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
147 check_gl_error ("glClearColor");
148 glEnable (GL_TEXTURE_2D);
149 check_gl_error ("glEnable GL_TEXTURE_2D");
151 check_gl_error ("glEnable GL_BLEND");
152 glDisable (GL_DEPTH_TEST);
153 check_gl_error ("glDisable GL_DEPTH_TEST");
154 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
156 if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
160 glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
161 check_gl_error ("glViewport");
162 glMatrixMode (GL_PROJECTION);
165 gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
166 check_gl_error ("gluOrtho2d");
167 glMatrixMode (GL_MODELVIEW);
170 glTranslatef (0, 0, 0);
176 glVertex2f (0, _size->height);
178 glVertex2f (_size->width, _size->height);
180 glVertex2f (_size->width, 0);
187 dcp::Size const out_size = _viewer->out_size ();
188 wxSize const canvas_size = _canvas->GetSize ();
190 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
192 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
193 glColor3ub (240, 240, 240);
194 glVertex2f (out_size.width, 0);
195 glVertex2f (canvas_size.GetWidth(), 0);
196 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
197 glVertex2f (out_size.width, canvas_size.GetHeight());
199 glColor3ub (255, 255, 255);
202 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
203 glColor3ub (240, 240, 240);
204 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
207 glVertex2f (canvas_size.GetWidth(), 0);
208 glVertex2f (canvas_size.GetWidth(), gap);
212 glVertex2f (0, gap + out_size.height + 1);
213 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
214 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
215 glVertex2f (0, 2 * gap + out_size.height + 2);
217 glColor3ub (255, 255, 255);
220 if (_viewer->outline_content()) {
221 glColor3ub (255, 0, 0);
222 glBegin (GL_LINE_LOOP);
223 Position<int> inter_position = _viewer->inter_position ();
224 dcp::Size inter_size = _viewer->inter_size ();
225 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
226 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
227 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
228 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
230 glColor3ub (255, 255, 255);
234 _canvas->SwapBuffers();
238 GLVideoView::set_image (shared_ptr<const Image> image)
241 _size = optional<dcp::Size>();
245 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
246 DCPOMATIC_ASSERT (!image->aligned());
248 _size = image->size ();
249 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
250 check_gl_error ("glPixelStorei");
251 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
252 check_gl_error ("glTexImage2D");
254 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
255 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
256 check_gl_error ("glTexParameteri");
258 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
259 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
260 check_gl_error ("glTexParameterf");
264 GLVideoView::start ()
266 _thread = new boost::thread (boost::bind(&GLVideoView::thread, this));
273 _thread->interrupt ();
281 GLVideoView::thread ()
284 /* XXX_b: check all calls and signal emissions in this method & protect them if necessary */
285 _context = new wxGLContext (_canvas);
286 _canvas->SetCurrent (*_context);
288 std::cout << "Here we go " << video_frame_rate() << " " << to_string(length()) << "\n";
291 dcpomatic::DCPTime const next = position() + one_video_frame();
293 if (next >= length()) {
295 _viewer->emit_finished ();
299 get_next_frame (false);
301 boost::mutex::scoped_lock lm (_mutex);
302 set_image (_player_video.first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
306 while (time_until_next_frame() < 5) {
307 get_next_frame (true);
310 boost::this_thread::interruption_point ();
311 dcpomatic_sleep_milliseconds (time_until_next_frame());
316 catch (boost::thread_interrupted& e)
318 /* XXX_b: store exceptions here */
324 GLVideoView::display_next_frame (bool non_blocking)
326 return get_next_frame (non_blocking);