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 <boost/bind.hpp>
30 #include <OpenGL/glu.h>
31 #include <OpenGL/glext.h>
32 #include <OpenGL/CGLTypes.h>
33 #include <OpenGL/OpenGL.h>
36 #ifdef DCPOMATIC_LINUX
39 #include <GL/glxext.h>
42 #ifdef DCPOMATIC_WINDOWS
48 using boost::shared_ptr;
49 using boost::optional;
51 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
54 _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
55 _context = new wxGLContext (_canvas);
56 _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
57 _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
59 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
60 if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
62 Display* dpy = wxGetX11Display();
63 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
67 #ifdef DCPOMATIC_WINDOWS
68 if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
70 wglSwapIntervalEXT (1);
77 GLint swapInterval = 1;
78 CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
81 glGenTextures (1, &_id);
82 glBindTexture (GL_TEXTURE_2D, _id);
83 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
86 GLVideoView::~GLVideoView ()
88 glDeleteTextures (1, &_id);
93 check_gl_error (char const * last)
95 GLenum const e = glGetError ();
96 if (e != GL_NO_ERROR) {
97 throw GLError (last, e);
102 GLVideoView::paint ()
104 _viewer->state_timer().set("paint-panel");
105 _canvas->SetCurrent (*_context);
106 wxPaintDC dc (_canvas);
108 _viewer->state_timer().unset();
112 GLVideoView::update ()
114 if (!_canvas->IsShownOnScreen()) {
117 wxClientDC dc (_canvas);
124 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
125 check_gl_error ("glClear");
127 glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
128 check_gl_error ("glClearColor");
129 glEnable (GL_TEXTURE_2D);
130 check_gl_error ("glEnable GL_TEXTURE_2D");
132 check_gl_error ("glEnable GL_BLEND");
133 glDisable (GL_DEPTH_TEST);
134 check_gl_error ("glDisable GL_DEPTH_TEST");
135 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
141 glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
142 check_gl_error ("glViewport");
143 glMatrixMode (GL_PROJECTION);
146 gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
147 check_gl_error ("gluOrtho2d");
148 glMatrixMode (GL_MODELVIEW);
151 glTranslatef (0, 0, 0);
157 glVertex2f (0, _size->height);
159 glVertex2f (_size->width, _size->height);
161 glVertex2f (_size->width, 0);
168 dcp::Size const out_size = _viewer->out_size ();
169 wxSize const canvas_size = _canvas->GetSize ();
171 if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
173 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
174 glColor3ub (240, 240, 240);
175 glVertex2f (out_size.width, 0);
176 glVertex2f (canvas_size.GetWidth(), 0);
177 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
178 glVertex2f (out_size.width, canvas_size.GetHeight());
180 glColor3ub (255, 255, 255);
183 if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
184 glColor3ub (240, 240, 240);
185 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
188 glVertex2f (canvas_size.GetWidth(), 0);
189 glVertex2f (canvas_size.GetWidth(), gap);
193 glVertex2f (0, gap + out_size.height + 1);
194 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
195 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
196 glVertex2f (0, 2 * gap + out_size.height + 2);
198 glColor3ub (255, 255, 255);
201 if (_viewer->outline_content()) {
202 glColor3ub (255, 0, 0);
203 glBegin (GL_LINE_LOOP);
204 Position<int> inter_position = _viewer->inter_position ();
205 dcp::Size inter_size = _viewer->inter_size ();
206 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
207 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
208 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
209 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
211 glColor3ub (255, 255, 255);
215 _canvas->SwapBuffers();
219 GLVideoView::set_image (shared_ptr<const Image> image)
222 _size = optional<dcp::Size>();
226 DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
227 DCPOMATIC_ASSERT (!image->aligned());
229 _size = image->size ();
230 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
231 check_gl_error ("glPixelStorei");
232 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
233 check_gl_error ("glTexImage2D");
235 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
236 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
237 check_gl_error ("glTexParameteri");
239 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
240 glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
241 check_gl_error ("glTexParameterf");