Re-work OpenGL video view to use modern shaders / APIs.
[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 class GLVideoView : public VideoView
38 {
39 public:
40         GLVideoView (FilmViewer* viewer, wxWindow* parent);
41         ~GLVideoView ();
42
43         wxWindow* get () const {
44                 return _canvas;
45         }
46         void update ();
47         void start ();
48         void stop ();
49
50         NextFrameResult display_next_frame (bool);
51
52         bool vsync_enabled () const {
53                 return _vsync_enabled;
54         }
55
56 private:
57         void set_image (std::shared_ptr<const Image> image);
58         void set_image_and_draw ();
59         void draw (Position<int> inter_position, dcp::Size inter_size);
60         void thread ();
61         void thread_playing ();
62         void request_one_shot ();
63         void check_for_butler_errors ();
64         void ensure_context ();
65         void size_changed (wxSizeEvent const &);
66         void setup_shaders ();
67         void set_border_colour (GLuint program);
68
69         wxGLCanvas* _canvas;
70         wxGLContext* _context;
71
72         boost::atomic<wxSize> _canvas_size;
73         GLuint _texture;
74         boost::optional<dcp::Size> _size;
75         bool _have_storage;
76         bool _vsync_enabled;
77         boost::thread _thread;
78
79         boost::mutex _playing_mutex;
80         boost::condition _thread_work_condition;
81         boost::atomic<bool> _playing;
82         boost::atomic<bool> _one_shot;
83
84         GLuint _vao;
85         GLint _draw_border;
86         bool _setup_shaders_done = false;
87
88         std::shared_ptr<wxTimer> _timer;
89 };