Support optimised rendering of YUV420P in OpenGL.
[dcpomatic.git] / src / wx / video_view.h
1 /*
2     Copyright (C) 2019-2021 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
22 #ifndef DCPOMATIC_VIDEO_VIEW_H
23 #define DCPOMATIC_VIDEO_VIEW_H
24
25
26 #include "optimisation.h"
27 #include "lib/dcpomatic_time.h"
28 #include "lib/exception_store.h"
29 #include "lib/signaller.h"
30 #include "lib/timer.h"
31 #include "lib/types.h"
32 #include <dcp/warnings.h>
33 #include <dcp/types.h>
34 LIBDCP_DISABLE_WARNINGS
35 #include <wx/wx.h>
36 LIBDCP_ENABLE_WARNINGS
37 #include <boost/signals2.hpp>
38 #include <boost/thread.hpp>
39
40
41 class FilmViewer;
42 class Image;
43 class Player;
44 class PlayerVideo;
45 class wxWindow;
46
47
48 class VideoView : public ExceptionStore, public Signaller
49 {
50 public:
51         VideoView (FilmViewer* viewer);
52         virtual ~VideoView () {}
53
54         VideoView (VideoView const&) = delete;
55         VideoView& operator= (VideoView const&) = delete;
56
57         /** @return the thing displaying the image */
58         virtual wxWindow* get () const = 0;
59         /** Re-make and display the image from the current _player_video */
60         virtual void update () = 0;
61         /** Called when playback starts */
62         virtual void start ();
63         /** Called when playback stops */
64         virtual void stop () {}
65
66         enum NextFrameResult {
67                 FAIL,
68                 AGAIN,
69                 SUCCESS
70         };
71
72         /** Get the next frame and display it; used after seek */
73         virtual NextFrameResult display_next_frame (bool) = 0;
74
75         void clear ();
76         bool reset_metadata (std::shared_ptr<const Film> film, dcp::Size player_video_container_size);
77
78         /** Emitted from the GUI thread when our display changes in size */
79         boost::signals2::signal<void()> Sized;
80         /** Emitted from the GUI thread when a lot of frames are being dropped */
81         boost::signals2::signal<void()> TooManyDropped;
82
83
84         /* Accessors for FilmViewer */
85
86         int dropped () const {
87                 boost::mutex::scoped_lock lm (_mutex);
88                 return _dropped;
89         }
90
91         int errored () const {
92                 boost::mutex::scoped_lock lm (_mutex);
93                 return _errored;
94         }
95
96         int gets () const {
97                 boost::mutex::scoped_lock lm (_mutex);
98                 return _gets;
99         }
100
101         StateTimer const & state_timer () const {
102                 return _state_timer;
103         }
104
105         dcpomatic::DCPTime position () const {
106                 boost::mutex::scoped_lock lm (_mutex);
107                 return _player_video.second;
108         }
109
110
111         /* Setters for FilmViewer so it can tell us our state and
112          * we can then use (thread) safely.
113          */
114
115         void set_video_frame_rate (int r) {
116                 boost::mutex::scoped_lock lm (_mutex);
117                 _video_frame_rate = r;
118         }
119
120         void set_length (dcpomatic::DCPTime len) {
121                 boost::mutex::scoped_lock lm (_mutex);
122                 _length = len;
123         }
124
125         void set_eyes (Eyes eyes) {
126                 boost::mutex::scoped_lock lm (_mutex);
127                 _eyes = eyes;
128         }
129
130         void set_three_d (bool t) {
131                 boost::mutex::scoped_lock lm (_mutex);
132                 _three_d = t;
133         }
134
135         void set_optimisation(Optimisation o) {
136                 _optimisation = o;
137         }
138
139 protected:
140         NextFrameResult get_next_frame (bool non_blocking);
141         boost::optional<int> time_until_next_frame () const;
142         dcpomatic::DCPTime one_video_frame () const;
143
144         wxColour pad_colour () const;
145
146         wxColour outline_content_colour () const {
147                 return wxColour(255, 0, 0);
148         }
149
150         wxColour outline_subtitles_colour () const {
151                 return wxColour(0, 255, 0);
152         }
153
154         wxColour crop_guess_colour () const {
155                 return wxColour(0, 0, 255);
156         }
157
158         int video_frame_rate () const {
159                 boost::mutex::scoped_lock lm (_mutex);
160                 return _video_frame_rate;
161         }
162
163         dcpomatic::DCPTime length () const {
164                 boost::mutex::scoped_lock lm (_mutex);
165                 return _length;
166         }
167
168         std::pair<std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime> player_video () const {
169                 boost::mutex::scoped_lock lm (_mutex);
170                 return _player_video;
171         }
172
173         void add_dropped ();
174
175         void add_get () {
176                 boost::mutex::scoped_lock lm (_mutex);
177                 ++_gets;
178         }
179
180         FilmViewer* _viewer;
181
182         StateTimer _state_timer;
183
184         Optimisation _optimisation = Optimisation::NONE;
185
186 private:
187         /** Mutex protecting all the state in this class */
188         mutable boost::mutex _mutex;
189
190         std::pair<std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime> _player_video;
191         int _video_frame_rate = 0;
192         /** length of the film we are playing, or 0 if there is none */
193         dcpomatic::DCPTime _length;
194         Eyes _eyes = Eyes::LEFT;
195         bool _three_d = false;
196
197         int _dropped = 0;
198         struct timeval _dropped_check_period_start;
199         int _errored = 0;
200         int _gets = 0;
201 };
202
203
204 #endif