Add basic Texture wrapper for a GL texture.
[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 ();
42         ~Texture ();
43
44         Texture (Texture const&) = delete;
45         Texture& operator= (Texture const&) = delete;
46
47         void bind ();
48
49 private:
50         GLuint _name;
51 };
52
53
54 class GLVideoView : public VideoView
55 {
56 public:
57         GLVideoView (FilmViewer* viewer, wxWindow* parent);
58         ~GLVideoView ();
59
60         wxWindow* get () const override {
61                 return _canvas;
62         }
63         void update () override;
64         void start () override;
65         void stop () override;
66
67         NextFrameResult display_next_frame (bool) override;
68
69         bool vsync_enabled () const {
70                 return _vsync_enabled;
71         }
72
73         std::map<GLenum, std::string> information () const {
74                 return _information;
75         }
76
77 private:
78         void set_image (std::shared_ptr<const PlayerVideo> pv);
79         void set_image_and_draw ();
80         void draw (Position<int> inter_position, dcp::Size inter_size);
81         void thread ();
82         void thread_playing ();
83         void request_one_shot ();
84         void check_for_butler_errors ();
85         void ensure_context ();
86         void size_changed (wxSizeEvent const &);
87         void setup_shaders ();
88         void set_border_colour (GLuint program);
89
90         wxGLCanvas* _canvas;
91         wxGLContext* _context;
92
93         boost::atomic<wxSize> _canvas_size;
94         std::unique_ptr<Texture> _video_texture;
95         boost::optional<dcp::Size> _video_size;
96         bool _have_storage;
97         bool _vsync_enabled;
98         boost::thread _thread;
99
100         boost::mutex _playing_mutex;
101         boost::condition _thread_work_condition;
102         boost::atomic<bool> _playing;
103         boost::atomic<bool> _one_shot;
104
105         GLuint _vao;
106         GLint _fragment_type;
107         bool _setup_shaders_done = false;
108
109         std::shared_ptr<wxTimer> _timer;
110
111         std::map<GLenum, std::string> _information;
112 };