Tidy up sized emissions from VideoView.
[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 "lib/image.h"
23 #include "lib/dcpomatic_assert.h"
24 #include "lib/exceptions.h"
25 #include <boost/bind.hpp>
26 #include <iostream>
27
28 #ifdef DCPOMATIC_OSX
29 #include <OpenGL/glu.h>
30 #include <OpenGL/glext.h>
31 #else
32 #include <GL/glu.h>
33 #include <GL/glext.h>
34 #endif
35
36 using std::cout;
37 using boost::shared_ptr;
38
39 GLVideoView::GLVideoView (wxWindow *parent)
40 {
41         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
42         _context = new wxGLContext (_canvas);
43         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this, _1));
44         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
45
46         glGenTextures (1, &_id);
47         glBindTexture (GL_TEXTURE_2D, _id);
48         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
49 }
50
51 GLVideoView::~GLVideoView ()
52 {
53         glDeleteTextures (1, &_id);
54         delete _context;
55         /* XXX: should we delete this? */
56         delete _canvas;
57 }
58
59 static void
60 check_gl_error (char const * last)
61 {
62         GLenum const e = glGetError ();
63         if (e != GL_NO_ERROR) {
64                 throw GLError (last, e);
65         }
66 }
67
68 void
69 GLVideoView::paint (wxPaintEvent &)
70 {
71         _canvas->SetCurrent (*_context);
72         wxPaintDC dc (_canvas);
73         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74         check_gl_error ("glClear");
75
76         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
77         check_gl_error ("glClearColor");
78         glEnable (GL_TEXTURE_2D);
79         check_gl_error ("glEnable GL_TEXTURE_2D");
80         glEnable (GL_COLOR_MATERIAL);
81         check_gl_error ("glEnable GL_COLOR_MATERIAL");
82         glEnable (GL_BLEND);
83         check_gl_error ("glEnable GL_BLEND");
84         glDisable (GL_DEPTH_TEST);
85         check_gl_error ("glDisable GL_DEPTH_TEST");
86         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
87
88         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
89         glMatrixMode (GL_PROJECTION);
90         glLoadIdentity ();
91
92         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
93         glMatrixMode (GL_MODELVIEW);
94         glLoadIdentity ();
95
96         glTranslatef (0, 0, 0);
97
98         if (_size) {
99                 glBegin (GL_QUADS);
100
101                 glTexCoord2f (0, 1);
102                 glVertex2f (0, _size->height);
103                 glTexCoord2f (1, 1);
104                 glVertex2f (_size->width, _size->height);
105                 glTexCoord2f (1, 0);
106                 glVertex2f (_size->width, 0);
107                 glTexCoord2f (0, 0);
108                 glVertex2f (0, 0);
109
110                 glEnd ();
111         }
112
113         glFlush();
114         _canvas->SwapBuffers();
115 }
116
117 void
118 GLVideoView::set_image (shared_ptr<const Image> image)
119 {
120         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
121         DCPOMATIC_ASSERT (!image->aligned());
122
123         _size = image->size ();
124         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
125         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
126         check_gl_error ("glTexImage2D");
127
128         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
129         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130         check_gl_error ("glTexParameteri");
131
132         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
133         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
134         check_gl_error ("glTexParameterf");
135 }