Fix incorrect scaling of the simple view with hidpi screens (#2128).
[dcpomatic.git] / src / wx / simple_video_view.cc
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 #include "closed_captions_dialog.h"
23 #include "film_viewer.h"
24 #include "simple_video_view.h"
25 #include "wx_util.h"
26 #include "lib/butler.h"
27 #include "lib/dcpomatic_log.h"
28 #include "lib/image.h"
29 #include <dcp/util.h>
30 #include <wx/wx.h>
31 #include <boost/bind/bind.hpp>
32
33
34 using std::max;
35 using std::shared_ptr;
36 using std::string;
37 using boost::optional;
38 #if BOOST_VERSION >= 106100
39 using namespace boost::placeholders;
40 #endif
41 using namespace dcpomatic;
42
43
44 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
45         : VideoView (viewer)
46 {
47         _panel = new wxPanel (parent);
48
49 #ifndef __WXOSX__
50         _panel->SetDoubleBuffered (true);
51 #endif
52
53         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
54         _panel->SetBackgroundColour (*wxBLACK);
55
56         _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
57         _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
58
59         _timer.Bind (wxEVT_TIMER, boost::bind(&SimpleVideoView::timer, this));
60 }
61
62
63 void
64 SimpleVideoView::paint ()
65 {
66         _state_timer.set("paint-panel");
67         wxPaintDC dc (_panel);
68         auto scale = 1 / dpi_scale_factor (_panel);
69         dc.SetLogicalScale (scale, scale);
70
71         auto const panel_size = _panel->GetSize ();
72
73         dcp::Size out_size;
74         if (!_image) {
75                 dc.Clear ();
76         } else {
77                 DCPOMATIC_ASSERT (_image->alignment() == Image::Alignment::COMPACT);
78                 out_size = _image->size();
79                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
80                 wxBitmap frame_bitmap (frame);
81                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
82         }
83
84         auto pad = pad_colour();
85
86         if (out_size.width < panel_size.GetWidth()) {
87                 wxPen   p (pad);
88                 wxBrush b (pad);
89                 dc.SetPen (p);
90                 dc.SetBrush (b);
91                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
92         }
93
94         if (out_size.height < panel_size.GetHeight()) {
95                 wxPen   p (pad);
96                 wxBrush b (pad);
97                 dc.SetPen (p);
98                 dc.SetBrush (b);
99                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
100                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
101                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
102         }
103
104         if (_viewer->outline_content()) {
105                 wxPen p (outline_content_colour(), 2);
106                 dc.SetPen (p);
107                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
108                 dc.DrawRectangle (_inter_position.x, _inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, _inter_size.width, _inter_size.height);
109         }
110
111         auto subs = _viewer->outline_subtitles();
112         if (subs) {
113                 wxPen p (outline_subtitles_colour(), 2);
114                 dc.SetPen (p);
115                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
116                 dc.DrawRectangle (subs->x * out_size.width, subs->y * out_size.height, subs->width * out_size.width, subs->height * out_size.height);
117         }
118
119         _state_timer.unset();
120 }
121
122
123 void
124 SimpleVideoView::refresh_panel ()
125 {
126         _state_timer.set ("refresh-panel");
127         _panel->Refresh ();
128         _panel->Update ();
129         _state_timer.unset ();
130 }
131
132
133 void
134 SimpleVideoView::timer ()
135 {
136         if (!_viewer->playing()) {
137                 return;
138         }
139
140         display_next_frame (false);
141         auto const next = position() + _viewer->one_video_frame();
142
143         if (next >= length()) {
144                 _viewer->finished ();
145                 return;
146         }
147
148         LOG_DEBUG_VIDEO_VIEW("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
149         _timer.Start (max(1, time_until_next_frame().get_value_or(0)), wxTIMER_ONE_SHOT);
150
151         if (_viewer->butler()) {
152                 _viewer->butler()->rethrow ();
153         }
154 }
155
156
157 void
158 SimpleVideoView::start ()
159 {
160         VideoView::start ();
161         timer ();
162 }
163
164
165 /** Try to get a frame from the butler and display it.
166  *  @param non_blocking true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
167  *  false to ask the butler to block until it has video (unless it is suspended).
168  *  @return true on success, false if we did nothing because it would have taken too long.
169  */
170 VideoView::NextFrameResult
171 SimpleVideoView::display_next_frame (bool non_blocking)
172 {
173         auto const r = get_next_frame (non_blocking);
174         if (r != SUCCESS) {
175                 return r;
176         }
177
178         update ();
179
180         try {
181                 _viewer->butler()->rethrow ();
182         } catch (DecodeError& e) {
183                 error_dialog (get(), e.what());
184         }
185
186         return SUCCESS;
187 }
188
189
190 void
191 SimpleVideoView::update ()
192 {
193         if (!player_video().first) {
194                 _image.reset ();
195                 refresh_panel ();
196                 return;
197         }
198
199         if (_viewer->playing() && (_viewer->time() - player_video().second) > one_video_frame()) {
200                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
201                    part if this frame is J2K).
202                 */
203                 add_dropped ();
204                 return;
205         }
206
207         /* In an ideal world, what we would do here is:
208          *
209          * 1. convert to XYZ exactly as we do in the DCP creation path.
210          * 2. convert back to RGB for the preview display, compensating
211          *    for the monitor etc. etc.
212          *
213          * but this is inefficient if the source is RGB.  Since we don't
214          * (currently) care too much about the precise accuracy of the preview's
215          * colour mapping (and we care more about its speed) we try to short-
216          * circuit this "ideal" situation in some cases.
217          *
218          * The content's specified colour conversion indicates the colourspace
219          * which the content is in (according to the user).
220          *
221          * PlayerVideo::image (bound to PlayerVideo::force) will take the source
222          * image and convert it (from whatever the user has said it is) to RGB.
223          */
224
225         _state_timer.set ("get image");
226
227         _image = player_video().first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), VideoRange::FULL, true);
228
229         _state_timer.set ("ImageChanged");
230         _viewer->image_changed (player_video().first);
231         _state_timer.unset ();
232
233         _inter_position = player_video().first->inter_position ();
234         _inter_size = player_video().first->inter_size ();
235
236         refresh_panel ();
237 }