1 /*****************************************************************************
2 * macosx.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2013 VLC authors and VideoLAN
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>
15 * Juho Vähä-Herttua <juhovh at iki dot fi>
16 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
37 #import <Cocoa/Cocoa.h>
38 #import <OpenGL/OpenGL.h>
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"
53 * Forward declarations
55 static int Open (vlc_object_t *);
56 static void Close (vlc_object_t *);
58 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
59 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
60 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
61 static int Control (vout_display_t *vd, int query, va_list ap);
63 static void *OurGetProcAddress(vlc_gl_t *, const char *);
65 static int OpenglLock (vlc_gl_t *gl);
66 static void OpenglUnlock (vlc_gl_t *gl);
67 static void OpenglSwap (vlc_gl_t *gl);
73 /* Will be loaded even without interface module. see voutgl.m */
74 set_shortname ("Mac OS X")
75 set_description (N_("Mac OS X OpenGL video output"))
76 set_category (CAT_VIDEO)
77 set_subcategory (SUBCAT_VIDEO_VOUT)
78 set_capability ("vout display", 300)
79 set_callbacks (Open, Close)
80 add_shortcut ("macosx", "vout_macosx")
85 * Obj-C protocol declaration that drawable-nsobject should follow
87 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
88 - (void)addVoutSubview:(NSView *)view;
89 - (void)removeVoutSubview:(NSView *)view;
92 @interface VLCOpenGLVideoView : NSOpenGLView
95 BOOL _hasPendingReshape;
97 - (void)setVoutDisplay:(vout_display_t *)vd;
98 - (void)setVoutFlushing:(BOOL)flushing;
102 struct vout_display_sys_t
104 VLCOpenGLVideoView *glView;
105 id<VLCOpenGLVideoViewEmbedding> container;
107 vout_window_t *embed;
109 vout_display_opengl_t *vgl;
111 picture_pool_t *pool;
113 bool has_first_frame;
115 vout_display_place_t place;
120 CGLContextObj locked_ctx;
121 VLCOpenGLVideoView *glView;
124 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
128 return dlsym(RTLD_DEFAULT, name);
131 static int Open (vlc_object_t *this)
133 vout_display_t *vd = (vout_display_t *)this;
134 vout_display_sys_t *sys = calloc (1, sizeof(*sys));
140 if (!CGDisplayUsesOpenGLAcceleration (kCGDirectMainDisplay))
141 msg_Err (this, "no OpenGL hardware acceleration found. this can lead to slow output and unexpected results");
149 var_Create(vd->obj.parent, "macosx-glcontext", VLC_VAR_ADDRESS);
151 /* Get the drawable object */
152 id container = var_CreateGetAddress (vd, "drawable-nsobject");
154 vout_display_DeleteWindow (vd, NULL);
156 sys->embed = vout_display_NewWindow (vd, VOUT_WINDOW_TYPE_NSOBJECT);
158 container = sys->embed->handle.nsobject;
161 msg_Err(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
166 /* This will be released in Close(), on
167 * main thread, after we are done using it. */
168 sys->container = [container retain];
170 /* Get our main view*/
171 [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:)
172 withObject:[NSValue valueWithPointer:&sys->glView]
175 msg_Err(vd, "Initialization of open gl view failed");
179 [sys->glView setVoutDisplay:vd];
181 /* We don't wait, that means that we'll have to be careful about releasing
183 * That's why we'll release on main thread in Close(). */
184 if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
185 [(id)container performSelectorOnMainThread:@selector(addVoutSubview:)
186 withObject:sys->glView
188 else if ([container isKindOfClass:[NSView class]]) {
189 NSView *parentView = container;
190 [parentView performSelectorOnMainThread:@selector(addSubview:)
191 withObject:sys->glView
193 [sys->glView performSelectorOnMainThread:@selector(setFrameToBoundsOfView:)
194 withObject:[NSValue valueWithPointer:parentView]
197 msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
201 /* Initialize common OpenGL video display */
202 sys->gl = vlc_object_create(this, sizeof(*sys->gl));
204 if( unlikely( !sys->gl ) )
207 struct gl_sys *glsys = sys->gl->sys = malloc(sizeof(struct gl_sys));
208 if( unlikely( !sys->gl->sys ) )
210 vlc_object_release(sys->gl);
213 glsys->locked_ctx = NULL;
214 glsys->glView = sys->glView;
215 sys->gl->makeCurrent = OpenglLock;
216 sys->gl->releaseCurrent = OpenglUnlock;
217 sys->gl->swap = OpenglSwap;
218 sys->gl->getProcAddress = OurGetProcAddress;
220 var_SetAddress(vd->obj.parent, "macosx-glcontext",
221 [[sys->glView openGLContext] CGLContextObj]);
223 const vlc_fourcc_t *subpicture_chromas;
225 if (vlc_gl_MakeCurrent(sys->gl) != VLC_SUCCESS)
227 msg_Err(vd, "Can't attach gl context");
230 sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, sys->gl,
231 &vd->cfg->viewpoint);
232 vlc_gl_ReleaseCurrent(sys->gl);
234 msg_Err(vd, "Error while initializing opengl display.");
239 vout_display_info_t info = vd->info;
240 info.has_pictures_invalid = false;
241 info.subpicture_chromas = subpicture_chromas;
243 /* Setup vout_display_t once everything is fine */
247 vd->prepare = PictureRender;
248 vd->display = PictureDisplay;
249 vd->control = Control;
252 vout_display_SendEventDisplaySize (vd, vd->fmt.i_visible_width, vd->fmt.i_visible_height);
262 void Close (vlc_object_t *this)
264 vout_display_t *vd = (vout_display_t *)this;
265 vout_display_sys_t *sys = vd->sys;
268 [sys->glView setVoutDisplay:nil];
270 var_Destroy (vd, "drawable-nsobject");
271 if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
272 /* This will retain sys->glView */
273 [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:)
274 withObject:sys->glView
277 /* release on main thread as explained in Open() */
278 [(id)sys->container performSelectorOnMainThread:@selector(release)
281 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview)
285 var_Destroy(vd->obj.parent, "macosx-glcontext");
286 if (sys->vgl != NULL)
288 vlc_gl_MakeCurrent(sys->gl);
289 vout_display_opengl_Delete (sys->vgl);
290 vlc_gl_ReleaseCurrent(sys->gl);
295 assert(((struct gl_sys *)sys->gl->sys)->locked_ctx == NULL);
297 vlc_object_release(sys->gl);
300 [sys->glView release];
303 vout_display_DeleteWindow (vd, sys->embed);
308 /*****************************************************************************
309 * vout display callbacks
310 *****************************************************************************/
312 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
314 vout_display_sys_t *sys = vd->sys;
316 if (!sys->pool && vlc_gl_MakeCurrent(sys->gl) == VLC_SUCCESS)
318 sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
319 vlc_gl_ReleaseCurrent(sys->gl);
324 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
327 vout_display_sys_t *sys = vd->sys;
329 if (vlc_gl_MakeCurrent(sys->gl) == VLC_SUCCESS)
331 vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
332 vlc_gl_ReleaseCurrent(sys->gl);
336 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
338 vout_display_sys_t *sys = vd->sys;
339 [sys->glView setVoutFlushing:YES];
340 if (vlc_gl_MakeCurrent(sys->gl) == VLC_SUCCESS)
342 vout_display_opengl_Display (sys->vgl, &vd->source);
343 vlc_gl_ReleaseCurrent(sys->gl);
345 [sys->glView setVoutFlushing:NO];
346 picture_Release (pic);
347 sys->has_first_frame = true;
350 subpicture_Delete(subpicture);
353 static int Control (vout_display_t *vd, int query, va_list ap)
355 vout_display_sys_t *sys = vd->sys;
363 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
364 case VOUT_DISPLAY_CHANGE_ZOOM:
365 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
366 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
367 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
370 id o_window = [sys->glView window];
372 return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
375 NSSize windowMinSize = [o_window minSize];
377 const vout_display_cfg_t *cfg;
379 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
382 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
385 /* we always use our current frame here, because we have some size constraints
386 in the ui vout provider */
387 vout_display_cfg_t cfg_tmp = *cfg;
388 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
389 NSRect bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
390 cfg_tmp.display.width = bounds.size.width;
391 cfg_tmp.display.height = bounds.size.height;
393 /* Reverse vertical alignment as the GL tex are Y inverted */
394 if (cfg_tmp.align.vertical == VOUT_DISPLAY_ALIGN_TOP)
395 cfg_tmp.align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
396 else if (cfg_tmp.align.vertical == VOUT_DISPLAY_ALIGN_BOTTOM)
397 cfg_tmp.align.vertical = VOUT_DISPLAY_ALIGN_TOP;
399 vout_display_place_t place;
400 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
401 @synchronized (sys->glView) {
405 if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
407 vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
409 /* For resize, we call glViewport in reshape and not here.
410 This has the positive side effect that we avoid erratic sizing as we animate every resize. */
411 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
412 // x / y are top left corner, but we need the lower left one
413 glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
414 vlc_gl_ReleaseCurrent (sys->gl);
419 case VOUT_DISPLAY_CHANGE_VIEWPOINT:
420 return vout_display_opengl_SetViewpoint (sys->vgl,
421 &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
423 case VOUT_DISPLAY_RESET_PICTURES:
424 vlc_assert_unreachable ();
426 msg_Err (vd, "Unknown request in Mac OS X vout display");
432 /*****************************************************************************
433 * vout opengl callbacks
434 *****************************************************************************/
435 static int OpenglLock (vlc_gl_t *gl)
437 struct gl_sys *sys = gl->sys;
438 if (![sys->glView respondsToSelector:@selector(openGLContext)])
441 assert(sys->locked_ctx == NULL);
443 NSOpenGLContext *context = [sys->glView openGLContext];
444 CGLContextObj cglcntx = [context CGLContextObj];
446 CGLError err = CGLLockContext (cglcntx);
447 if (kCGLNoError == err) {
448 sys->locked_ctx = cglcntx;
449 [context makeCurrentContext];
455 static void OpenglUnlock (vlc_gl_t *gl)
457 struct gl_sys *sys = gl->sys;
458 CGLUnlockContext (sys->locked_ctx);
459 sys->locked_ctx = NULL;
462 static void OpenglSwap (vlc_gl_t *gl)
464 struct gl_sys *sys = gl->sys;
465 [[sys->glView openGLContext] flushBuffer];
468 /*****************************************************************************
470 *****************************************************************************/
471 @implementation VLCOpenGLVideoView
473 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
476 + (void)getNewView:(NSValue *)value
478 id *ret = [value pointerValue];
479 *ret = [[self alloc] init];
483 * Gets called by the Open() method.
487 VLCAssertMainThread();
489 /* Warning - this may be called on non main thread */
491 NSOpenGLPixelFormatAttribute attribs[] =
493 NSOpenGLPFADoubleBuffer,
494 NSOpenGLPFAAccelerated,
495 NSOpenGLPFANoRecovery,
496 NSOpenGLPFAColorSize, 24,
497 NSOpenGLPFAAlphaSize, 8,
498 NSOpenGLPFADepthSize, 24,
500 NSOpenGLPFAAllowOfflineRenderers,
504 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
509 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
515 /* enable HiDPI support */
516 [self setWantsBestResolutionOpenGLSurface:YES];
518 /* request our screen's HDR mode (introduced in OS X 10.11) */
519 #pragma clang diagnostic push
520 #pragma clang diagnostic ignored "-Wpartial-availability"
521 if ([self respondsToSelector:@selector(setWantsExtendedDynamicRangeOpenGLSurface:)]) {
522 [self setWantsExtendedDynamicRangeOpenGLSurface:YES];
524 #pragma clang diagnostic pop
526 /* Swap buffers only during the vertical retrace of the monitor.
527 http://developer.apple.com/documentation/GraphicsImaging/
528 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
529 GLint params[] = { 1 };
530 CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
532 [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
533 object:[NSApplication sharedApplication]
535 usingBlock:^(NSNotification *notification) {
536 [self performSelectorOnMainThread:@selector(reshape)
541 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
547 [[NSNotificationCenter defaultCenter] removeObserver:self];
552 * Gets called by the Open() method.
554 - (void)setFrameToBoundsOfView:(NSValue *)value
556 NSView *parentView = [value pointerValue];
557 [self setFrame:[parentView bounds]];
561 * Gets called by the Close and Open methods.
564 - (void)setVoutDisplay:(vout_display_t *)aVd
566 @synchronized(self) {
572 * Gets called when the vout will aquire the lock and flush.
575 - (void)setVoutFlushing:(BOOL)flushing
579 @synchronized(self) {
580 _hasPendingReshape = NO;
585 * Can -drawRect skip rendering?.
587 - (BOOL)canSkipRendering
589 VLCAssertMainThread();
591 @synchronized(self) {
592 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
593 return !_hasPendingReshape && hasFirstFrame;
599 * Local method that locks the gl context.
603 VLCAssertMainThread();
604 NSOpenGLContext *context = [self openGLContext];
605 CGLError err = CGLLockContext ([context CGLContextObj]);
606 if (err == kCGLNoError)
607 [context makeCurrentContext];
608 return err == kCGLNoError;
612 * Local method that unlocks the gl context.
616 VLCAssertMainThread();
617 CGLUnlockContext ([[self openGLContext] CGLContextObj]);
621 * Local method that force a rendering of a frame.
622 * This will get called if Cocoa forces us to redraw (via -drawRect).
626 VLCAssertMainThread();
628 // We may have taken some times to take the opengl Lock.
629 // Check here to see if we can just skip the frame as well.
630 if ([self canSkipRendering])
634 @synchronized(self) { // vd can be accessed from multiple threads
635 hasFirstFrame = vd && vd->sys->has_first_frame;
639 // This will lock gl.
640 vout_display_opengl_Display (vd->sys->vgl, &vd->source);
642 glClear (GL_COLOR_BUFFER_BIT);
646 * Method called by Cocoa when the view is resized.
650 VLCAssertMainThread();
652 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
653 NSRect bounds = [self convertRectToBacking:[self bounds]];
654 vout_display_place_t place;
656 @synchronized(self) {
658 vout_display_cfg_t cfg_tmp = *(vd->cfg);
659 cfg_tmp.display.width = bounds.size.width;
660 cfg_tmp.display.height = bounds.size.height;
662 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
663 vd->sys->place = place;
664 vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height);
669 // x / y are top left corner, but we need the lower left one
670 glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
672 @synchronized(self) {
673 // This may be cleared before -drawRect is being called,
674 // in this case we'll skip the rendering.
675 // This will save us for rendering two frames (or more) for nothing
676 // (one by the vout, one (or more) by drawRect)
677 _hasPendingReshape = YES;
687 * Method called by Cocoa when the view is resized or the location has changed.
688 * We just need to make sure we are locking here.
692 VLCAssertMainThread();
693 BOOL success = [self lockgl];
703 * Method called by Cocoa to force redraw.
705 - (void)drawRect:(NSRect) rect
707 VLCAssertMainThread();
709 if ([self canSkipRendering])
712 BOOL success = [self lockgl];
723 NSWindow *window = [self window];
725 // Remove flashes with splitter view.
726 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
727 [window disableScreenUpdatesUntilFlush];
738 #pragma mark Mouse handling
740 - (void)mouseDown:(NSEvent *)o_event
742 @synchronized (self) {
744 if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] & NSControlKeyMask)) {
745 if ([o_event clickCount] <= 1)
746 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
751 [super mouseDown:o_event];
754 - (void)otherMouseDown:(NSEvent *)o_event
756 @synchronized (self) {
758 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
761 [super otherMouseDown: o_event];
764 - (void)mouseUp:(NSEvent *)o_event
766 @synchronized (self) {
768 if ([o_event type] == NSLeftMouseUp)
769 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
773 [super mouseUp: o_event];
776 - (void)otherMouseUp:(NSEvent *)o_event
778 @synchronized (self) {
780 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
783 [super otherMouseUp: o_event];
786 - (void)mouseMoved:(NSEvent *)o_event
788 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
789 NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
790 NSRect videoRect = [self bounds];
791 BOOL b_inside = [self mouse: ml inRect: videoRect];
793 ml = [self convertPointToBacking: ml];
794 videoRect = [self convertRectToBacking: videoRect];
797 @synchronized (self) {
799 vout_display_SendMouseMovedDisplayCoordinates(vd, ORIENT_NORMAL,
800 (int)ml.x, videoRect.size.height - (int)ml.y,
806 [super mouseMoved: o_event];
809 - (void)mouseDragged:(NSEvent *)o_event
811 [self mouseMoved: o_event];
812 [super mouseDragged: o_event];
815 - (void)otherMouseDragged:(NSEvent *)o_event
817 [self mouseMoved: o_event];
818 [super otherMouseDragged: o_event];
821 - (void)rightMouseDragged:(NSEvent *)o_event
823 [self mouseMoved: o_event];
824 [super rightMouseDragged: o_event];
827 - (BOOL)acceptsFirstResponder
832 - (BOOL)mouseDownCanMoveWindow