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