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;
366 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
367 case VOUT_DISPLAY_CHANGE_ZOOM:
368 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
369 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
370 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
373 id o_window = [sys->glView window];
375 return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
378 NSSize windowMinSize = [o_window minSize];
380 const vout_display_cfg_t *cfg;
382 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
385 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
388 /* we always use our current frame here, because we have some size constraints
389 in the ui vout provider */
390 vout_display_cfg_t cfg_tmp = *cfg;
391 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
392 NSRect bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
393 cfg_tmp.display.width = bounds.size.width;
394 cfg_tmp.display.height = bounds.size.height;
396 /* Reverse vertical alignment as the GL tex are Y inverted */
397 if (cfg_tmp.align.vertical == VOUT_DISPLAY_ALIGN_TOP)
398 cfg_tmp.align.vertical = VOUT_DISPLAY_ALIGN_BOTTOM;
399 else if (cfg_tmp.align.vertical == VOUT_DISPLAY_ALIGN_BOTTOM)
400 cfg_tmp.align.vertical = VOUT_DISPLAY_ALIGN_TOP;
402 vout_display_place_t place;
403 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
404 @synchronized (sys->glView) {
408 if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
410 vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
412 /* For resize, we call glViewport in reshape and not here.
413 This has the positive side effect that we avoid erratic sizing as we animate every resize. */
414 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
415 // x / y are top left corner, but we need the lower left one
416 glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
417 vlc_gl_ReleaseCurrent (sys->gl);
422 case VOUT_DISPLAY_CHANGE_VIEWPOINT:
423 return vout_display_opengl_SetViewpoint (sys->vgl,
424 &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
426 case VOUT_DISPLAY_RESET_PICTURES:
427 vlc_assert_unreachable ();
429 msg_Err (vd, "Unknown request in Mac OS X vout display");
435 /*****************************************************************************
436 * vout opengl callbacks
437 *****************************************************************************/
438 static int OpenglLock (vlc_gl_t *gl)
440 struct gl_sys *sys = gl->sys;
441 if (![sys->glView respondsToSelector:@selector(openGLContext)])
444 assert(sys->locked_ctx == NULL);
446 NSOpenGLContext *context = [sys->glView openGLContext];
447 CGLContextObj cglcntx = [context CGLContextObj];
449 CGLError err = CGLLockContext (cglcntx);
450 if (kCGLNoError == err) {
451 sys->locked_ctx = cglcntx;
452 [context makeCurrentContext];
458 static void OpenglUnlock (vlc_gl_t *gl)
460 struct gl_sys *sys = gl->sys;
461 CGLUnlockContext (sys->locked_ctx);
462 sys->locked_ctx = NULL;
465 static void OpenglSwap (vlc_gl_t *gl)
467 struct gl_sys *sys = gl->sys;
468 [[sys->glView openGLContext] flushBuffer];
471 /*****************************************************************************
473 *****************************************************************************/
474 @implementation VLCOpenGLVideoView
476 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
479 + (void)getNewView:(NSValue *)value
481 id *ret = [value pointerValue];
482 *ret = [[self alloc] init];
486 * Gets called by the Open() method.
490 VLCAssertMainThread();
492 /* Warning - this may be called on non main thread */
494 NSOpenGLPixelFormatAttribute attribs[] =
496 NSOpenGLPFADoubleBuffer,
497 NSOpenGLPFAAccelerated,
498 NSOpenGLPFANoRecovery,
499 NSOpenGLPFAColorSize, 24,
500 NSOpenGLPFAAlphaSize, 8,
501 NSOpenGLPFADepthSize, 24,
503 NSOpenGLPFAAllowOfflineRenderers,
507 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
512 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
518 /* enable HiDPI support */
519 [self setWantsBestResolutionOpenGLSurface:YES];
521 /* request our screen's HDR mode (introduced in OS X 10.11) */
522 #pragma clang diagnostic push
523 #pragma clang diagnostic ignored "-Wpartial-availability"
524 if ([self respondsToSelector:@selector(setWantsExtendedDynamicRangeOpenGLSurface:)]) {
525 [self setWantsExtendedDynamicRangeOpenGLSurface:YES];
527 #pragma clang diagnostic pop
529 /* Swap buffers only during the vertical retrace of the monitor.
530 http://developer.apple.com/documentation/GraphicsImaging/
531 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
532 GLint params[] = { 1 };
533 CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
535 [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
536 object:[NSApplication sharedApplication]
538 usingBlock:^(NSNotification *notification) {
539 [self performSelectorOnMainThread:@selector(reshape)
544 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
550 [[NSNotificationCenter defaultCenter] removeObserver:self];
555 * Gets called by the Open() method.
557 - (void)setFrameToBoundsOfView:(NSValue *)value
559 NSView *parentView = [value pointerValue];
560 [self setFrame:[parentView bounds]];
564 * Gets called by the Close and Open methods.
567 - (void)setVoutDisplay:(vout_display_t *)aVd
569 @synchronized(self) {
575 * Gets called when the vout will aquire the lock and flush.
578 - (void)setVoutFlushing:(BOOL)flushing
582 @synchronized(self) {
583 _hasPendingReshape = NO;
588 * Can -drawRect skip rendering?.
590 - (BOOL)canSkipRendering
592 VLCAssertMainThread();
594 @synchronized(self) {
595 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
596 return !_hasPendingReshape && hasFirstFrame;
602 * Local method that locks the gl context.
606 VLCAssertMainThread();
607 NSOpenGLContext *context = [self openGLContext];
608 CGLError err = CGLLockContext ([context CGLContextObj]);
609 if (err == kCGLNoError)
610 [context makeCurrentContext];
611 return err == kCGLNoError;
615 * Local method that unlocks the gl context.
619 VLCAssertMainThread();
620 CGLUnlockContext ([[self openGLContext] CGLContextObj]);
624 * Local method that force a rendering of a frame.
625 * This will get called if Cocoa forces us to redraw (via -drawRect).
629 VLCAssertMainThread();
631 // We may have taken some times to take the opengl Lock.
632 // Check here to see if we can just skip the frame as well.
633 if ([self canSkipRendering])
637 @synchronized(self) { // vd can be accessed from multiple threads
638 hasFirstFrame = vd && vd->sys->has_first_frame;
642 // This will lock gl.
643 vout_display_opengl_Display (vd->sys->vgl, &vd->source);
645 glClear (GL_COLOR_BUFFER_BIT);
649 * Method called by Cocoa when the view is resized.
653 VLCAssertMainThread();
655 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
656 NSRect bounds = [self convertRectToBacking:[self bounds]];
657 vout_display_place_t place;
659 @synchronized(self) {
661 vout_display_cfg_t cfg_tmp = *(vd->cfg);
662 cfg_tmp.display.width = bounds.size.width;
663 cfg_tmp.display.height = bounds.size.height;
665 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
666 vd->sys->place = place;
667 vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height);
672 // x / y are top left corner, but we need the lower left one
673 glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
675 @synchronized(self) {
676 // This may be cleared before -drawRect is being called,
677 // in this case we'll skip the rendering.
678 // This will save us for rendering two frames (or more) for nothing
679 // (one by the vout, one (or more) by drawRect)
680 _hasPendingReshape = YES;
690 * Method called by Cocoa when the view is resized or the location has changed.
691 * We just need to make sure we are locking here.
695 VLCAssertMainThread();
696 BOOL success = [self lockgl];
706 * Method called by Cocoa to force redraw.
708 - (void)drawRect:(NSRect) rect
710 VLCAssertMainThread();
712 if ([self canSkipRendering])
715 BOOL success = [self lockgl];
726 NSWindow *window = [self window];
728 // Remove flashes with splitter view.
729 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
730 [window disableScreenUpdatesUntilFlush];
741 #pragma mark Mouse handling
743 - (void)mouseDown:(NSEvent *)o_event
745 @synchronized (self) {
747 if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] & NSControlKeyMask)) {
748 if ([o_event clickCount] <= 1)
749 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
754 [super mouseDown:o_event];
757 - (void)otherMouseDown:(NSEvent *)o_event
759 @synchronized (self) {
761 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
764 [super otherMouseDown: o_event];
767 - (void)mouseUp:(NSEvent *)o_event
769 @synchronized (self) {
771 if ([o_event type] == NSLeftMouseUp)
772 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
776 [super mouseUp: o_event];
779 - (void)otherMouseUp:(NSEvent *)o_event
781 @synchronized (self) {
783 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
786 [super otherMouseUp: o_event];
789 - (void)mouseMoved:(NSEvent *)o_event
791 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
792 NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
793 NSRect videoRect = [self bounds];
794 BOOL b_inside = [self mouse: ml inRect: videoRect];
796 ml = [self convertPointToBacking: ml];
797 videoRect = [self convertRectToBacking: videoRect];
800 @synchronized (self) {
802 vout_display_SendMouseMovedDisplayCoordinates(vd, ORIENT_NORMAL,
803 (int)ml.x, videoRect.size.height - (int)ml.y,
809 [super mouseMoved: o_event];
812 - (void)mouseDragged:(NSEvent *)o_event
814 [self mouseMoved: o_event];
815 [super mouseDragged: o_event];
818 - (void)otherMouseDragged:(NSEvent *)o_event
820 [self mouseMoved: o_event];
821 [super otherMouseDragged: o_event];
824 - (void)rightMouseDragged:(NSEvent *)o_event
826 [self mouseMoved: o_event];
827 [super rightMouseDragged: o_event];
830 - (BOOL)acceptsFirstResponder
835 - (BOOL)mouseDownCanMoveWindow