d7f8429af78c705faff83d831d1cd888be16fa42
[dcpomatic.git] / src / wx / gl_video_view.h
1 /*
2     Copyright (C) 2020 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 "video_view.h"
22 #include "lib/signaller.h"
23 #include "lib/position.h"
24 #include "lib/warnings.h"
25 DCPOMATIC_DISABLE_WARNINGS
26 #include <wx/glcanvas.h>
27 #include <wx/wx.h>
28 DCPOMATIC_ENABLE_WARNINGS
29 #include <dcp/util.h>
30 #include <boost/atomic.hpp>
31 #include <boost/thread.hpp>
32 #include <boost/thread/condition.hpp>
33 #undef None
34 #undef Success
35 #undef Status
36
37
38 class Texture
39 {
40 public:
41         Texture (GLint unpack_alignment);
42         ~Texture ();
43
44         Texture (Texture const&) = delete;
45         Texture& operator= (Texture const&) = delete;
46
47         void bind ();
48         void set (std::shared_ptr<const Image> image);
49
50 private:
51         GLuint _name;
52         GLint _unpack_alignment;
53         boost::optional<dcp::Size> _size;
54 };
55
56
57 class GLVideoView : public VideoView
58 {
59 public:
60         GLVideoView (FilmViewer* viewer, wxWindow* parent);
61         ~GLVideoView ();
62
63         wxWindow* get () const override {
64                 return _canvas;
65         }
66         void update () override;
67         void start () override;
68         void stop () override;
69
70         NextFrameResult display_next_frame (bool) override;
71
72         bool vsync_enabled () const {
73                 return _vsync_enabled;
74         }
75
76         std::map<GLenum, std::string> information () const {
77                 return _information;
78         }
79
80 private:
81         void set_image (std::shared_ptr<const PlayerVideo> pv);
82         void set_image_and_draw ();
83         void draw (Position<int> inter_position, dcp::Size inter_size);
84         void thread ();
85         void thread_playing ();
86         void request_one_shot ();
87         void check_for_butler_errors ();
88         void ensure_context ();
89         void size_changed (wxSizeEvent const &);
90         void setup_shaders ();
91         void set_border_colour (GLuint program);
92
93         wxGLCanvas* _canvas;
94         wxGLContext* _context;
95
96         template <class T>
97         class Last
98         {
99         public:
100                 void set_next (T const& next) {
101                         _next = next;
102                 }
103
104                 bool changed () const {
105                         return !_value || *_value != _next;
106                 }
107
108                 void update () {
109                         _value = _next;
110                 }
111
112         private:
113                 boost::optional<T> _value;
114                 T _next;
115         };
116
117         Last<wxSize> _last_canvas_size;
118         Last<dcp::Size> _last_video_size;
119         Last<Position<int>> _last_inter_position;
120         Last<dcp::Size> _last_inter_size;
121         Last<dcp::Size> _last_out_size;
122
123         boost::atomic<wxSize> _canvas_size;
124         std::unique_ptr<Texture> _video_texture;
125         std::unique_ptr<Texture> _subtitle_texture;
126         bool _have_subtitle_to_render = false;
127         bool _vsync_enabled;
128         boost::thread _thread;
129
130         boost::mutex _playing_mutex;
131         boost::condition _thread_work_condition;
132         boost::atomic<bool> _playing;
133         boost::atomic<bool> _one_shot;
134
135         GLuint _vao;
136         GLint _fragment_type;
137         bool _setup_shaders_done = false;
138
139         std::shared_ptr<wxTimer> _timer;
140
141         std::map<GLenum, std::string> _information;
142 };