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