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