Fix typo.
[dcpomatic.git] / src / wx / gl_video_view.cc
1 /*
2     Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
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>
27 #include <iostream>
28
29 #ifdef DCPOMATIC_OSX
30 #include <OpenGL/glu.h>
31 #include <OpenGL/glext.h>
32 #include <OpenGL/CGLTypes.h>
33 #include <OpenGL/OpenGL.h>
34 #endif
35
36 #ifdef DCPOMATIC_LINUX
37 #include <GL/glu.h>
38 #include <GL/glext.h>
39 #include <GL/glxext.h>
40 #endif
41
42 #ifdef DCPOMATIC_WINDOWS
43 #include <GL/glu.h>
44 #include <GL/glext.h>
45 #include <GL/wglext.h>
46 #endif
47
48 using std::cout;
49 using boost::shared_ptr;
50 using boost::optional;
51
52 GLVideoView::GLVideoView (FilmViewer* viewer, wxWindow *parent)
53         : VideoView (viewer)
54         , _vsync_enabled (false)
55 {
56         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
57         _context = new wxGLContext (_canvas);
58         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this));
59         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
60
61 #if defined(DCPOMATIC_LINUX) && defined(DCPOMATIC_HAVE_GLX_SWAP_INTERVAL_EXT)
62         if (_canvas->IsExtensionSupported("GLX_EXT_swap_control")) {
63                 /* Enable vsync */
64                 Display* dpy = wxGetX11Display();
65                 glXSwapIntervalEXT (dpy, DefaultScreen(dpy), 1);
66                 _vsync_enabled = true;
67         }
68 #endif
69
70 #ifdef DCPOMATIC_WINDOWS
71         if (_canvas->IsExtensionSupported("WGL_EXT_swap_control")) {
72                 /* Enable vsync */
73                 PFNWGLSWAPINTERVALEXTPROC swap = (PFNWGLSWAPINTERVALEXTPROC) wglGetProcAddress("wglSwapIntervalEXT");
74                 if (swap) {
75                         swap (1);
76                         _vsync_enabled = true;
77                 }
78         }
79
80 #endif
81
82 #ifdef DCPOMATIC_OSX
83         /* Enable vsync */
84         GLint swapInterval = 1;
85         CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
86         _vsync_enabled = true;
87 #endif
88
89         glGenTextures (1, &_id);
90         glBindTexture (GL_TEXTURE_2D, _id);
91         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
92 }
93
94 GLVideoView::~GLVideoView ()
95 {
96         glDeleteTextures (1, &_id);
97         delete _context;
98 }
99
100 static void
101        check_gl_error (char const * last)
102 {
103         GLenum const e = glGetError ();
104         if (e != GL_NO_ERROR) {
105                 throw GLError (last, e);
106         }
107 }
108
109 void
110 GLVideoView::paint ()
111 {
112         _viewer->state_timer().set("paint-panel");
113         _canvas->SetCurrent (*_context);
114         wxPaintDC dc (_canvas);
115         draw ();
116         _viewer->state_timer().unset();
117 }
118
119 void
120 GLVideoView::update ()
121 {
122         if (!_canvas->IsShownOnScreen()) {
123                 return;
124         }
125         wxClientDC dc (_canvas);
126         draw ();
127 }
128
129 void
130 GLVideoView::draw ()
131 {
132         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133         check_gl_error ("glClear");
134
135         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
136         check_gl_error ("glClearColor");
137         glEnable (GL_TEXTURE_2D);
138         check_gl_error ("glEnable GL_TEXTURE_2D");
139         glEnable (GL_BLEND);
140         check_gl_error ("glEnable GL_BLEND");
141         glDisable (GL_DEPTH_TEST);
142         check_gl_error ("glDisable GL_DEPTH_TEST");
143         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
144
145         if (_canvas->GetSize().x < 64 || _canvas->GetSize().y < 0) {
146                 return;
147         }
148
149         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
150         check_gl_error ("glViewport");
151         glMatrixMode (GL_PROJECTION);
152         glLoadIdentity ();
153
154         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
155         check_gl_error ("gluOrtho2d");
156         glMatrixMode (GL_MODELVIEW);
157         glLoadIdentity ();
158
159         glTranslatef (0, 0, 0);
160
161         if (_size) {
162                 glBegin (GL_QUADS);
163
164                 glTexCoord2f (0, 1);
165                 glVertex2f (0, _size->height);
166                 glTexCoord2f (1, 1);
167                 glVertex2f (_size->width, _size->height);
168                 glTexCoord2f (1, 0);
169                 glVertex2f (_size->width, 0);
170                 glTexCoord2f (0, 0);
171                 glVertex2f (0, 0);
172
173                 glEnd ();
174         }
175
176         dcp::Size const out_size = _viewer->out_size ();
177         wxSize const canvas_size = _canvas->GetSize ();
178
179         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
180                 glBegin (GL_QUADS);
181                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
182                 glColor3ub (240, 240, 240);
183                 glVertex2f (out_size.width, 0);
184                 glVertex2f (canvas_size.GetWidth(), 0);
185                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
186                 glVertex2f (out_size.width, canvas_size.GetHeight());
187                 glEnd ();
188                 glColor3ub (255, 255, 255);
189         }
190
191         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
192                 glColor3ub (240, 240, 240);
193                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
194                 glBegin (GL_QUADS);
195                 glVertex2f (0, 0);
196                 glVertex2f (canvas_size.GetWidth(), 0);
197                 glVertex2f (canvas_size.GetWidth(), gap);
198                 glVertex2f (0, gap);
199                 glEnd ();
200                 glBegin (GL_QUADS);
201                 glVertex2f (0, gap + out_size.height + 1);
202                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
203                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
204                 glVertex2f (0, 2 * gap + out_size.height + 2);
205                 glEnd ();
206                 glColor3ub (255, 255, 255);
207         }
208
209         if (_viewer->outline_content()) {
210                 glColor3ub (255, 0, 0);
211                 glBegin (GL_LINE_LOOP);
212                 Position<int> inter_position = _viewer->inter_position ();
213                 dcp::Size inter_size = _viewer->inter_size ();
214                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
215                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
216                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
217                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
218                 glEnd ();
219                 glColor3ub (255, 255, 255);
220         }
221
222         glFlush();
223         _canvas->SwapBuffers();
224 }
225
226 void
227 GLVideoView::set_image (shared_ptr<const Image> image)
228 {
229         if (!image) {
230                 _size = optional<dcp::Size>();
231                 return;
232         }
233
234         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
235         DCPOMATIC_ASSERT (!image->aligned());
236
237         _size = image->size ();
238         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
239         check_gl_error ("glPixelStorei");
240         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
241         check_gl_error ("glTexImage2D");
242
243         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
244         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
245         check_gl_error ("glTexParameteri");
246
247         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
248         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
249         check_gl_error ("glTexParameterf");
250 }