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