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