vout/macosx: Ensure context exists when calling vout_display_opengl_New
[vlc.git] / modules / video_output / macosx.m
1 /*****************************************************************************
2  * macosx.m: MacOS X OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          Eric Petit <titer@m0k.org>
9  *          Benjamin Pracht <bigben at videolan dot org>
10  *          Damien Fouilleul <damienf at videolan dot org>
11  *          Pierre d'Herbemont <pdherbemont at videolan dot org>
12  *          Felix Paul Kühne <fkuehne at videolan dot org>
13  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
14  *          Rémi Denis-Courmont
15  *          Juho Vähä-Herttua <juhovh at iki dot fi>
16  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
17  *
18  * This program is free software; you can redistribute it and/or modify it
19  * under the terms of the GNU Lesser General Public License as published by
20  * the Free Software Foundation; either version 2.1 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with this program; if not, write to the Free Software Foundation,
30  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
31  *****************************************************************************/
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36
37 #import <Cocoa/Cocoa.h>
38 #import <OpenGL/OpenGL.h>
39 #import <dlfcn.h>
40
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47 #include <vlc_vout_display.h>
48 #include <vlc_opengl.h>
49 #include <vlc_dialog.h>
50 #include "opengl/vout_helper.h"
51
52 #define OSX_EL_CAPITAN (NSAppKitVersionNumber >= 1404)
53
54 #if MAC_OS_X_VERSION_MIN_ALLOWED <= MAC_OS_X_VERSION_10_11
55 const CFStringRef kCGColorSpaceDCIP3 = CFSTR("kCGColorSpaceDCIP3");
56 const CFStringRef kCGColorSpaceITUR_709 = CFSTR("kCGColorSpaceITUR_709");
57 const CFStringRef kCGColorSpaceITUR_2020 = CFSTR("kCGColorSpaceITUR_2020");
58 #endif
59
60 /**
61  * Forward declarations
62  */
63 static int Open (vlc_object_t *);
64 static void Close (vlc_object_t *);
65
66 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
67 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
68 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
69 static int Control (vout_display_t *vd, int query, va_list ap);
70
71 static void *OurGetProcAddress(vlc_gl_t *, const char *);
72
73 static int OpenglLock (vlc_gl_t *gl);
74 static void OpenglUnlock (vlc_gl_t *gl);
75 static void OpenglSwap (vlc_gl_t *gl);
76
77 /**
78  * Module declaration
79  */
80 vlc_module_begin ()
81     /* Will be loaded even without interface module. see voutgl.m */
82     set_shortname ("Mac OS X")
83     set_description (N_("Mac OS X OpenGL video output"))
84     set_category (CAT_VIDEO)
85     set_subcategory (SUBCAT_VIDEO_VOUT)
86     set_capability ("vout display", 300)
87     set_callbacks (Open, Close)
88
89     add_shortcut ("macosx", "vout_macosx")
90 vlc_module_end ()
91
92 /**
93  * Obj-C protocol declaration that drawable-nsobject should follow
94  */
95 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
96 - (void)addVoutSubview:(NSView *)view;
97 - (void)removeVoutSubview:(NSView *)view;
98 @end
99
100 @interface VLCOpenGLVideoView : NSOpenGLView
101 {
102     vout_display_t *vd;
103     BOOL _hasPendingReshape;
104 }
105 - (void)setVoutDisplay:(vout_display_t *)vd;
106 - (void)setVoutFlushing:(BOOL)flushing;
107 @end
108
109
110 struct vout_display_sys_t
111 {
112     VLCOpenGLVideoView *glView;
113     id<VLCOpenGLVideoViewEmbedding> container;
114
115     CGColorSpaceRef cgColorSpace;
116     NSColorSpace *nsColorSpace;
117
118     vout_window_t *embed;
119     vlc_gl_t *gl;
120     vout_display_opengl_t *vgl;
121
122     picture_pool_t *pool;
123     picture_t *current;
124     bool has_first_frame;
125
126     vout_display_place_t place;
127 };
128
129
130 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
131 {
132     VLC_UNUSED(gl);
133
134     return dlsym(RTLD_DEFAULT, name);
135 }
136
137 static int Open (vlc_object_t *this)
138 {
139     vout_display_t *vd = (vout_display_t *)this;
140     vout_display_sys_t *sys = calloc (1, sizeof(*sys));
141
142     if (!sys)
143         return VLC_ENOMEM;
144
145     @autoreleasepool {
146         if (!CGDisplayUsesOpenGLAcceleration (kCGDirectMainDisplay))
147             msg_Err (this, "no OpenGL hardware acceleration found. this can lead to slow output and unexpected results");
148
149         vd->sys = sys;
150         sys->pool = NULL;
151         sys->embed = NULL;
152         sys->vgl = NULL;
153         sys->gl = NULL;
154
155         /* Get the drawable object */
156         id container = var_CreateGetAddress (vd, "drawable-nsobject");
157         if (container)
158             vout_display_DeleteWindow (vd, NULL);
159         else {
160             sys->embed = vout_display_NewWindow (vd, VOUT_WINDOW_TYPE_NSOBJECT);
161             if (sys->embed)
162                 container = sys->embed->handle.nsobject;
163
164             if (!container) {
165                 msg_Err(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
166                 goto error;
167             }
168         }
169
170         /* This will be released in Close(), on
171          * main thread, after we are done using it. */
172         sys->container = [container retain];
173
174         /* support for BT.709 and BT.2020 color spaces was introduced with OS X 10.11
175          * on older OS versions, we can't show correct colors, so we fallback on linear RGB */
176         if (OSX_EL_CAPITAN) {
177             switch (vd->fmt.primaries) {
178                 case COLOR_PRIMARIES_BT601_525:
179                 case COLOR_PRIMARIES_BT601_625:
180                 {
181                     msg_Dbg(vd, "Using BT.601 color space");
182                     sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
183                     break;
184                 }
185                 case COLOR_PRIMARIES_BT709:
186                 {
187                     msg_Dbg(vd, "Using BT.709 color space");
188                     sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
189                     break;
190                 }
191                 case COLOR_PRIMARIES_BT2020:
192                 {
193                     msg_Dbg(vd, "Using BT.2020 color space");
194                     sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
195                     break;
196                 }
197                 case COLOR_PRIMARIES_DCI_P3:
198                 {
199                     msg_Dbg(vd, "Using DCI P3 color space");
200                     sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceDCIP3);
201                     break;
202                 }
203                 default:
204                 {
205                     msg_Dbg(vd, "Guessing color space based on video dimensions (%ix%i)", vd->fmt.i_visible_width, vd->fmt.i_visible_height);
206                     if (vd->fmt.i_visible_height >= 2000 || vd->fmt.i_visible_width >= 3800) {
207                         msg_Dbg(vd, "Using BT.2020 color space");
208                         sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_2020);
209                     } else if (vd->fmt.i_height > 576) {
210                         msg_Dbg(vd, "Using BT.709 color space");
211                         sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceITUR_709);
212                     } else {
213                         msg_Dbg(vd, "SD content, using linear RGB color space");
214                         sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
215                     }
216                     break;
217                 }
218             }
219         } else {
220             msg_Dbg(vd, "OS does not support BT.709 or BT.2020 color spaces, output may vary");
221             sys->cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
222         }
223         sys->nsColorSpace = [[NSColorSpace alloc] initWithCGColorSpace:sys->cgColorSpace];
224
225         /* Get our main view*/
226         [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:)
227                                              withObject:[NSValue valueWithPointer:&sys->glView]
228                                           waitUntilDone:YES];
229         if (!sys->glView) {
230             msg_Err(vd, "Initialization of open gl view failed");
231             goto error;
232         }
233
234         [sys->glView setVoutDisplay:vd];
235
236         /* We don't wait, that means that we'll have to be careful about releasing
237          * container.
238          * That's why we'll release on main thread in Close(). */
239         if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
240             [(id)container performSelectorOnMainThread:@selector(addVoutSubview:)
241                                             withObject:sys->glView
242                                          waitUntilDone:NO];
243         else if ([container isKindOfClass:[NSView class]]) {
244             NSView *parentView = container;
245             [parentView performSelectorOnMainThread:@selector(addSubview:)
246                                          withObject:sys->glView
247                                       waitUntilDone:NO];
248             [sys->glView performSelectorOnMainThread:@selector(setFrameToBoundsOfView:)
249                                           withObject:[NSValue valueWithPointer:parentView]
250                                        waitUntilDone:NO];
251         } else {
252             msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
253             goto error;
254         }
255
256         /* Initialize common OpenGL video display */
257         sys->gl = vlc_object_create(this, sizeof(*sys->gl));
258
259         if( unlikely( !sys->gl ) )
260             goto error;
261
262         sys->gl->makeCurrent = OpenglLock;
263         sys->gl->releaseCurrent = OpenglUnlock;
264         sys->gl->swap = OpenglSwap;
265         sys->gl->getProcAddress = OurGetProcAddress;
266         sys->gl->sys = sys;
267
268         const vlc_fourcc_t *subpicture_chromas;
269
270         vlc_gl_MakeCurrent(sys->gl);
271         sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, sys->gl,
272                                             &vd->cfg->viewpoint);
273         vlc_gl_ReleaseCurrent(sys->gl);
274         if (!sys->vgl) {
275             msg_Err(vd, "Error while initializing opengl display.");
276             goto error;
277         }
278
279         /* */
280         vout_display_info_t info = vd->info;
281         info.has_pictures_invalid = false;
282         info.subpicture_chromas = subpicture_chromas;
283         info.has_hide_mouse = true;
284
285         /* Setup vout_display_t once everything is fine */
286         vd->info = info;
287         
288         vd->pool = Pool;
289         vd->prepare = PictureRender;
290         vd->display = PictureDisplay;
291         vd->control = Control;
292         
293         /* */
294         vout_display_SendEventDisplaySize (vd, vd->fmt.i_visible_width, vd->fmt.i_visible_height);
295         
296         return VLC_SUCCESS;
297         
298     error:
299         Close(this);
300         return VLC_EGENERIC;
301     }
302 }
303
304 void Close (vlc_object_t *this)
305 {
306     vout_display_t *vd = (vout_display_t *)this;
307     vout_display_sys_t *sys = vd->sys;
308
309     @autoreleasepool {
310         [sys->glView setVoutDisplay:nil];
311
312         var_Destroy (vd, "drawable-nsobject");
313         if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
314         /* This will retain sys->glView */
315             [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:)
316                                                  withObject:sys->glView
317                                               waitUntilDone:NO];
318
319         /* release on main thread as explained in Open() */
320         [(id)sys->container performSelectorOnMainThread:@selector(release)
321                                              withObject:nil
322                                           waitUntilDone:NO];
323         [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview)
324                                       withObject:nil
325                                    waitUntilDone:NO];
326
327         if (sys->vgl != NULL)
328             vout_display_opengl_Delete (sys->vgl);
329
330         if (sys->gl != NULL)
331             vlc_object_release(sys->gl);
332
333         [sys->glView release];
334
335         if (sys->cgColorSpace != nil)
336             CGColorSpaceRelease(sys->cgColorSpace);
337
338         if (sys->nsColorSpace != nil)
339             [sys->nsColorSpace release];
340
341         if (sys->embed)
342             vout_display_DeleteWindow (vd, sys->embed);
343         free (sys);
344     }
345 }
346
347 /*****************************************************************************
348  * vout display callbacks
349  *****************************************************************************/
350
351 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
352 {
353     vout_display_sys_t *sys = vd->sys;
354
355     if (!sys->pool)
356         sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
357     assert(sys->pool);
358     return sys->pool;
359 }
360
361 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
362 {
363
364     vout_display_sys_t *sys = vd->sys;
365
366     vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
367 }
368
369 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
370 {
371     vout_display_sys_t *sys = vd->sys;
372     [sys->glView setVoutFlushing:YES];
373     vout_display_opengl_Display (sys->vgl, &vd->source);
374     [sys->glView setVoutFlushing:NO];
375     picture_Release (pic);
376     sys->has_first_frame = true;
377
378     if (subpicture)
379         subpicture_Delete(subpicture);
380 }
381
382 static int Control (vout_display_t *vd, int query, va_list ap)
383 {
384     vout_display_sys_t *sys = vd->sys;
385
386     if (!vd->sys)
387         return VLC_EGENERIC;
388
389     if (!sys->embed)
390         return VLC_EGENERIC;
391
392     @autoreleasepool {
393         switch (query)
394         {
395             case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
396             case VOUT_DISPLAY_CHANGE_ZOOM:
397             case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
398             case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
399             case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
400             {
401
402                 id o_window = [sys->glView window];
403                 if (!o_window) {
404                     return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
405                 }
406
407                 NSSize windowMinSize = [o_window minSize];
408
409                 const vout_display_cfg_t *cfg;
410                 const video_format_t *source;
411
412                 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
413                     source = (const video_format_t *)va_arg (ap, const video_format_t *);
414                     cfg = vd->cfg;
415                 } else {
416                     source = &vd->source;
417                     cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
418                 }
419
420                 /* we always use our current frame here, because we have some size constraints
421                  in the ui vout provider */
422                 vout_display_cfg_t cfg_tmp = *cfg;
423                 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
424                 NSRect bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
425                 cfg_tmp.display.width = bounds.size.width;
426                 cfg_tmp.display.height = bounds.size.height;
427
428                 vout_display_place_t place;
429                 vout_display_PlacePicture (&place, source, &cfg_tmp, false);
430                 @synchronized (sys->glView) {
431                     sys->place = place;
432                 }
433
434                 vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
435
436                 /* For resize, we call glViewport in reshape and not here.
437                  This has the positive side effect that we avoid erratic sizing as we animate every resize. */
438                 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
439                     // x / y are top left corner, but we need the lower left one
440                     glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
441
442                 return VLC_SUCCESS;
443             }
444
445             case VOUT_DISPLAY_HIDE_MOUSE:
446             {
447                 [NSCursor setHiddenUntilMouseMoves: YES];
448                 return VLC_SUCCESS;
449             }
450
451             case VOUT_DISPLAY_CHANGE_VIEWPOINT:
452                 return vout_display_opengl_SetViewpoint (sys->vgl,
453                     &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
454
455             case VOUT_DISPLAY_RESET_PICTURES:
456                 vlc_assert_unreachable ();
457             default:
458                 msg_Err (vd, "Unknown request in Mac OS X vout display");
459                 return VLC_EGENERIC;
460         }
461     }
462 }
463
464 /*****************************************************************************
465  * vout opengl callbacks
466  *****************************************************************************/
467 static int OpenglLock (vlc_gl_t *gl)
468 {
469     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
470     if (!sys->glView || ![sys->glView respondsToSelector:@selector(openGLContext)])
471         return 1;
472
473     NSOpenGLContext *context = [sys->glView openGLContext];
474     CGLError err = CGLLockContext ([context CGLContextObj]);
475     if (kCGLNoError == err) {
476         [context makeCurrentContext];
477         return 0;
478     }
479     return 1;
480 }
481
482 static void OpenglUnlock (vlc_gl_t *gl)
483 {
484     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
485     CGLUnlockContext ([[sys->glView openGLContext] CGLContextObj]);
486 }
487
488 static void OpenglSwap (vlc_gl_t *gl)
489 {
490     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
491     [[sys->glView openGLContext] flushBuffer];
492 }
493
494 /*****************************************************************************
495  * Our NSView object
496  *****************************************************************************/
497 @implementation VLCOpenGLVideoView
498
499 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
500
501
502 + (void)getNewView:(NSValue *)value
503 {
504     id *ret = [value pointerValue];
505     *ret = [[self alloc] init];
506 }
507
508 /**
509  * Gets called by the Open() method.
510  */
511 - (id)init
512 {
513     VLCAssertMainThread();
514
515     /* Warning - this may be called on non main thread */
516
517     NSOpenGLPixelFormatAttribute attribs[] =
518     {
519         NSOpenGLPFADoubleBuffer,
520         NSOpenGLPFAAccelerated,
521         NSOpenGLPFANoRecovery,
522         NSOpenGLPFAColorSize, 24,
523         NSOpenGLPFAAlphaSize, 8,
524         NSOpenGLPFADepthSize, 24,
525         NSOpenGLPFAWindow,
526         NSOpenGLPFAAllowOfflineRenderers,
527         0
528     };
529
530     NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
531
532     if (!fmt)
533         return nil;
534
535     self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
536     [fmt release];
537
538     if (!self)
539         return nil;
540
541     /* enable HiDPI support */
542     [self setWantsBestResolutionOpenGLSurface:YES];
543
544     /* request our screen's HDR mode (introduced in OS X 10.11) */
545 #pragma clang diagnostic push
546 #pragma clang diagnostic ignored "-Wpartial-availability"
547     if ([self respondsToSelector:@selector(setWantsExtendedDynamicRangeOpenGLSurface:)]) {
548         [self setWantsExtendedDynamicRangeOpenGLSurface:YES];
549     }
550 #pragma clang diagnostic pop
551
552     /* Swap buffers only during the vertical retrace of the monitor.
553      http://developer.apple.com/documentation/GraphicsImaging/
554      Conceptual/OpenGL/chap5/chapter_5_section_44.html */
555     GLint params[] = { 1 };
556     CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
557
558     [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
559                                                       object:[NSApplication sharedApplication]
560                                                        queue:nil
561                                                   usingBlock:^(NSNotification *notification) {
562                                                       [self performSelectorOnMainThread:@selector(reshape)
563                                                                              withObject:nil
564                                                                           waitUntilDone:NO];
565                                                   }];
566
567     [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
568     return self;
569 }
570
571 - (void)dealloc
572 {
573     [[NSNotificationCenter defaultCenter] removeObserver:self];
574     [super dealloc];
575 }
576
577 /**
578  * Gets called by the Open() method.
579  */
580 - (void)setFrameToBoundsOfView:(NSValue *)value
581 {
582     NSView *parentView = [value pointerValue];
583     [self setFrame:[parentView bounds]];
584 }
585
586 /**
587  * Gets called by the Close and Open methods.
588  * (Non main thread).
589  */
590 - (void)setVoutDisplay:(vout_display_t *)aVd
591 {
592     @synchronized(self) {
593         vd = aVd;
594     }
595 }
596
597 /**
598  * Gets called when the vout will aquire the lock and flush.
599  * (Non main thread).
600  */
601 - (void)setVoutFlushing:(BOOL)flushing
602 {
603     if (!flushing)
604         return;
605     @synchronized(self) {
606         _hasPendingReshape = NO;
607     }
608 }
609
610 /**
611  * Can -drawRect skip rendering?.
612  */
613 - (BOOL)canSkipRendering
614 {
615     VLCAssertMainThread();
616
617     @synchronized(self) {
618         BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
619         return !_hasPendingReshape && hasFirstFrame;
620     }
621 }
622
623
624 /**
625  * Local method that locks the gl context.
626  */
627 - (BOOL)lockgl
628 {
629     VLCAssertMainThread();
630     NSOpenGLContext *context = [self openGLContext];
631     CGLError err = CGLLockContext ([context CGLContextObj]);
632     if (err == kCGLNoError)
633         [context makeCurrentContext];
634     return err == kCGLNoError;
635 }
636
637 /**
638  * Local method that unlocks the gl context.
639  */
640 - (void)unlockgl
641 {
642     VLCAssertMainThread();
643     CGLUnlockContext ([[self openGLContext] CGLContextObj]);
644 }
645
646 /**
647  * Local method that force a rendering of a frame.
648  * This will get called if Cocoa forces us to redraw (via -drawRect).
649  */
650 - (void)render
651 {
652     VLCAssertMainThread();
653
654     // We may have taken some times to take the opengl Lock.
655     // Check here to see if we can just skip the frame as well.
656     if ([self canSkipRendering])
657         return;
658
659     BOOL hasFirstFrame;
660     @synchronized(self) { // vd can be accessed from multiple threads
661         hasFirstFrame = vd && vd->sys->has_first_frame;
662     }
663
664     if (hasFirstFrame)
665         // This will lock gl.
666         vout_display_opengl_Display (vd->sys->vgl, &vd->source);
667     else
668         glClear (GL_COLOR_BUFFER_BIT);
669 }
670
671 /**
672  * Method called by Cocoa when the view is resized.
673  */
674 - (void)reshape
675 {
676     VLCAssertMainThread();
677
678     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
679     NSRect bounds = [self convertRectToBacking:[self bounds]];
680     vout_display_place_t place;
681
682     @synchronized(self) {
683         if (vd) {
684             vout_display_cfg_t cfg_tmp = *(vd->cfg);
685             cfg_tmp.display.width  = bounds.size.width;
686             cfg_tmp.display.height = bounds.size.height;
687
688             vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
689             vd->sys->place = place;
690             vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height);
691         }
692     }
693
694     if ([self lockgl]) {
695         // x / y are top left corner, but we need the lower left one
696         glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
697
698         @synchronized(self) {
699             // This may be cleared before -drawRect is being called,
700             // in this case we'll skip the rendering.
701             // This will save us for rendering two frames (or more) for nothing
702             // (one by the vout, one (or more) by drawRect)
703             _hasPendingReshape = YES;
704         }
705
706         [self unlockgl];
707
708         [super reshape];
709     }
710 }
711
712 /**
713  * Method called by Cocoa when the view is resized or the location has changed.
714  * We just need to make sure we are locking here.
715  */
716 - (void)update
717 {
718     VLCAssertMainThread();
719     BOOL success = [self lockgl];
720     if (!success)
721         return;
722
723     [super update];
724
725     [self unlockgl];
726 }
727
728 /**
729  * Method called by Cocoa to force redraw.
730  */
731 - (void)drawRect:(NSRect) rect
732 {
733     VLCAssertMainThread();
734
735     if ([self canSkipRendering])
736         return;
737
738     BOOL success = [self lockgl];
739     if (!success)
740         return;
741
742     [self render];
743
744     [self unlockgl];
745 }
746
747 - (void)renewGState
748 {
749     NSWindow *window = [self window];
750
751     // Remove flashes with splitter view.
752     if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
753         [window disableScreenUpdatesUntilFlush];
754
755     [super renewGState];
756 }
757
758 - (BOOL)isOpaque
759 {
760     return YES;
761 }
762
763 #pragma mark -
764 #pragma mark Mouse handling
765
766 - (void)mouseDown:(NSEvent *)o_event
767 {
768     @synchronized (self) {
769         if (vd) {
770             if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] &  NSControlKeyMask)) {
771                 if ([o_event clickCount] <= 1)
772                     vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
773             }
774         }
775     }
776
777     [super mouseDown:o_event];
778 }
779
780 - (void)otherMouseDown:(NSEvent *)o_event
781 {
782     @synchronized (self) {
783         if (vd)
784             vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
785     }
786
787     [super otherMouseDown: o_event];
788 }
789
790 - (void)mouseUp:(NSEvent *)o_event
791 {
792     @synchronized (self) {
793         if (vd) {
794             if ([o_event type] == NSLeftMouseUp)
795                 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
796         }
797     }
798
799     [super mouseUp: o_event];
800 }
801
802 - (void)otherMouseUp:(NSEvent *)o_event
803 {
804     @synchronized (self) {
805         if (vd)
806             vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
807     }
808
809     [super otherMouseUp: o_event];
810 }
811
812 - (void)mouseMoved:(NSEvent *)o_event
813 {
814     /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
815     NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
816     NSRect videoRect = [self bounds];
817     BOOL b_inside = [self mouse: ml inRect: videoRect];
818
819     ml = [self convertPointToBacking: ml];
820     videoRect = [self convertRectToBacking: videoRect];
821
822     if (b_inside) {
823         @synchronized (self) {
824             if (vd) {
825                 vout_display_SendMouseMovedDisplayCoordinates(vd, ORIENT_NORMAL,
826                                                               (int)ml.x, videoRect.size.height - (int)ml.y,
827                                                               &vd->sys->place);
828             }
829         }
830     }
831
832     [super mouseMoved: o_event];
833 }
834
835 - (void)mouseDragged:(NSEvent *)o_event
836 {
837     [self mouseMoved: o_event];
838     [super mouseDragged: o_event];
839 }
840
841 - (void)otherMouseDragged:(NSEvent *)o_event
842 {
843     [self mouseMoved: o_event];
844     [super otherMouseDragged: o_event];
845 }
846
847 - (void)rightMouseDragged:(NSEvent *)o_event
848 {
849     [self mouseMoved: o_event];
850     [super rightMouseDragged: o_event];
851 }
852
853 - (BOOL)acceptsFirstResponder
854 {
855     return YES;
856 }
857
858 - (BOOL)mouseDownCanMoveWindow
859 {
860     return YES;
861 }
862
863 - (void)viewWillMoveToWindow:(nullable NSWindow *)newWindow
864 {
865     [super viewWillMoveToWindow:newWindow];
866
867     if (newWindow == nil)
868         return;
869
870     @synchronized (self) {
871         @try {
872             if (vd) [newWindow setColorSpace:vd->sys->nsColorSpace];
873         }
874         @catch (NSException *exception) {
875             msg_Warn(vd, "Setting the window color space failed due to an Obj-C exception (%s, %s", [exception.name UTF8String], [exception.reason UTF8String]);
876         }
877
878     }
879
880 }
881
882 @end