fefc08849233c3ddea162d5a7c24a9246cbef77a
[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         _canvas->SetCurrent (*_context);
73         wxPaintDC dc (_canvas);
74         draw ();
75 }
76
77 void
78 GLVideoView::update ()
79 {
80         if (!_canvas->IsShownOnScreen()) {
81                 return;
82         }
83         wxClientDC dc (_canvas);
84         draw ();
85 }
86
87 void
88 GLVideoView::draw ()
89 {
90         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
91         check_gl_error ("glClear");
92
93         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
94         check_gl_error ("glClearColor");
95         glEnable (GL_TEXTURE_2D);
96         check_gl_error ("glEnable GL_TEXTURE_2D");
97         glEnable (GL_BLEND);
98         check_gl_error ("glEnable GL_BLEND");
99         glDisable (GL_DEPTH_TEST);
100         check_gl_error ("glDisable GL_DEPTH_TEST");
101         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102
103         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
104         glMatrixMode (GL_PROJECTION);
105         glLoadIdentity ();
106
107         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
108         glMatrixMode (GL_MODELVIEW);
109         glLoadIdentity ();
110
111         glTranslatef (0, 0, 0);
112
113         if (_size) {
114                 glBegin (GL_QUADS);
115
116                 glTexCoord2f (0, 1);
117                 glVertex2f (0, _size->height);
118                 glTexCoord2f (1, 1);
119                 glVertex2f (_size->width, _size->height);
120                 glTexCoord2f (1, 0);
121                 glVertex2f (_size->width, 0);
122                 glTexCoord2f (0, 0);
123                 glVertex2f (0, 0);
124
125                 glEnd ();
126         }
127
128         glDisable (GL_TEXTURE_2D);
129         check_gl_error ("glDisable GL_TEXTURE_2D");
130
131         dcp::Size const out_size = _viewer->out_size ();
132         wxSize const canvas_size = _canvas->GetSize ();
133
134         if (!_viewer->pad_black() && out_size.width < canvas_size.GetWidth()) {
135                 glBegin (GL_QUADS);
136                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
137                 glColor3ub (240, 240, 240);
138                 glVertex2f (out_size.width, 0);
139                 glVertex2f (canvas_size.GetWidth(), 0);
140                 glVertex2f (canvas_size.GetWidth(), canvas_size.GetHeight());
141                 glVertex2f (out_size.width, canvas_size.GetHeight());
142                 glEnd ();
143                 glColor3ub (255, 255, 255);
144         }
145
146         if (!_viewer->pad_black() && out_size.height < canvas_size.GetHeight()) {
147                 glColor3ub (240, 240, 240);
148                 int const gap = (canvas_size.GetHeight() - out_size.height) / 2;
149                 glBegin (GL_QUADS);
150                 glVertex2f (0, 0);
151                 glVertex2f (canvas_size.GetWidth(), 0);
152                 glVertex2f (canvas_size.GetWidth(), gap);
153                 glVertex2f (0, gap);
154                 glEnd ();
155                 glBegin (GL_QUADS);
156                 glVertex2f (0, gap + out_size.height + 1);
157                 glVertex2f (canvas_size.GetWidth(), gap + out_size.height + 1);
158                 glVertex2f (canvas_size.GetWidth(), 2 * gap + out_size.height + 2);
159                 glVertex2f (0, 2 * gap + out_size.height + 2);
160                 glEnd ();
161                 glColor3ub (255, 255, 255);
162         }
163
164         if (_viewer->outline_content()) {
165                 glColor3ub (255, 0, 0);
166                 glBegin (GL_LINE_LOOP);
167                 Position<int> inter_position = _viewer->inter_position ();
168                 dcp::Size inter_size = _viewer->inter_size ();
169                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
170                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2);
171                 glVertex2f (inter_position.x + inter_size.width, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
172                 glVertex2f (inter_position.x, inter_position.y + (canvas_size.GetHeight() - out_size.height) / 2 + inter_size.height);
173                 glEnd ();
174                 glColor3ub (255, 255, 255);
175         }
176
177         glFlush();
178         _canvas->SwapBuffers();
179 }
180
181 void
182 GLVideoView::set_image (shared_ptr<const Image> image)
183 {
184         if (!image) {
185                 _size = optional<dcp::Size>();
186                 return;
187         }
188
189         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
190         DCPOMATIC_ASSERT (!image->aligned());
191
192         _size = image->size ();
193         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
194         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
195         check_gl_error ("glTexImage2D");
196
197         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
198         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
199         check_gl_error ("glTexParameteri");
200
201         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
202         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
203         check_gl_error ("glTexParameterf");
204 }