Remove storage of _frame in FilmViewer.
[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 using boost::optional;
39
40 GLVideoView::GLVideoView (wxWindow *parent)
41 {
42         _canvas = new wxGLCanvas (parent, wxID_ANY, 0, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE);
43         _context = new wxGLContext (_canvas);
44         _canvas->Bind (wxEVT_PAINT, boost::bind(&GLVideoView::paint, this, _1));
45         _canvas->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
46
47         glGenTextures (1, &_id);
48         glBindTexture (GL_TEXTURE_2D, _id);
49         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
50 }
51
52 GLVideoView::~GLVideoView ()
53 {
54         glDeleteTextures (1, &_id);
55         delete _context;
56         /* XXX: should we delete this? */
57         delete _canvas;
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 (wxPaintEvent &)
71 {
72         _canvas->SetCurrent (*_context);
73         wxPaintDC dc (_canvas);
74         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75         check_gl_error ("glClear");
76
77         glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
78         check_gl_error ("glClearColor");
79         glEnable (GL_TEXTURE_2D);
80         check_gl_error ("glEnable GL_TEXTURE_2D");
81         glEnable (GL_COLOR_MATERIAL);
82         check_gl_error ("glEnable GL_COLOR_MATERIAL");
83         glEnable (GL_BLEND);
84         check_gl_error ("glEnable GL_BLEND");
85         glDisable (GL_DEPTH_TEST);
86         check_gl_error ("glDisable GL_DEPTH_TEST");
87         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
88
89         glViewport (0, 0, _canvas->GetSize().x, _canvas->GetSize().y);
90         glMatrixMode (GL_PROJECTION);
91         glLoadIdentity ();
92
93         gluOrtho2D (0, _canvas->GetSize().x, _canvas->GetSize().y, 0);
94         glMatrixMode (GL_MODELVIEW);
95         glLoadIdentity ();
96
97         glTranslatef (0, 0, 0);
98
99         if (_size) {
100                 glBegin (GL_QUADS);
101
102                 glTexCoord2f (0, 1);
103                 glVertex2f (0, _size->height);
104                 glTexCoord2f (1, 1);
105                 glVertex2f (_size->width, _size->height);
106                 glTexCoord2f (1, 0);
107                 glVertex2f (_size->width, 0);
108                 glTexCoord2f (0, 0);
109                 glVertex2f (0, 0);
110
111                 glEnd ();
112         }
113
114         glFlush();
115         _canvas->SwapBuffers();
116 }
117
118 void
119 GLVideoView::set_image (shared_ptr<const Image> image)
120 {
121         if (!image) {
122                 _size = optional<dcp::Size>();
123                 return;
124         }
125
126         DCPOMATIC_ASSERT (image->pixel_format() == AV_PIX_FMT_RGB24);
127         DCPOMATIC_ASSERT (!image->aligned());
128
129         _size = image->size ();
130         glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
131         glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB8, _size->width, _size->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image->data()[0]);
132         check_gl_error ("glTexImage2D");
133
134         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
135         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
136         check_gl_error ("glTexParameteri");
137
138         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
139         glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
140         check_gl_error ("glTexParameterf");
141 }