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>
52 /* compilation support for 10.5 and 10.6 */
53 #define OSX_LION NSAppKitVersionNumber >= 1115.2
54 #ifndef MAC_OS_X_VERSION_10_7
56 @interface NSView (IntroducedInLion)
57 - (NSRect)convertRectToBacking:(NSRect)aRect;
58 - (void)setWantsBestResolutionOpenGLSurface:(BOOL)aBool;
64 * Forward declarations
66 static int Open (vlc_object_t *);
67 static void Close (vlc_object_t *);
69 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
70 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
71 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
72 static int Control (vout_display_t *vd, int query, va_list ap);
74 static void *OurGetProcAddress(vlc_gl_t *, const char *);
76 static int OpenglLock (vlc_gl_t *gl);
77 static void OpenglUnlock (vlc_gl_t *gl);
78 static void OpenglSwap (vlc_gl_t *gl);
84 /* Will be loaded even without interface module. see voutgl.m */
85 set_shortname ("Mac OS X")
86 set_description (N_("Mac OS X OpenGL video output"))
87 set_category (CAT_VIDEO)
88 set_subcategory (SUBCAT_VIDEO_VOUT)
89 set_capability ("vout display", 300)
90 set_callbacks (Open, Close)
92 add_shortcut ("macosx", "vout_macosx")
96 * Obj-C protocol declaration that drawable-nsobject should follow
98 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
99 - (void)addVoutSubview:(NSView *)view;
100 - (void)removeVoutSubview:(NSView *)view;
103 @interface VLCOpenGLVideoView : NSOpenGLView
106 BOOL _hasPendingReshape;
108 - (void)setVoutDisplay:(vout_display_t *)vd;
109 - (void)setVoutFlushing:(BOOL)flushing;
113 struct vout_display_sys_t
115 VLCOpenGLVideoView *glView;
116 id<VLCOpenGLVideoViewEmbedding> container;
118 vout_window_t *embed;
120 vout_display_opengl_t *vgl;
122 picture_pool_t *pool;
124 bool has_first_frame;
126 vout_display_place_t place;
130 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
134 return dlsym(RTLD_DEFAULT, name);
137 static int Open (vlc_object_t *this)
139 vout_display_t *vd = (vout_display_t *)this;
140 vout_display_sys_t *sys = calloc (1, sizeof(*sys));
141 NSAutoreleasePool *nsPool = nil;
146 if (!CGDisplayUsesOpenGLAcceleration (kCGDirectMainDisplay))
147 msg_Err (this, "no OpenGL hardware acceleration found. this can lead to slow output and unexpected results");
154 /* Get the drawable object */
155 id container = var_CreateGetAddress (vd, "drawable-nsobject");
157 vout_display_DeleteWindow (vd, NULL);
159 vout_window_cfg_t wnd_cfg;
161 memset (&wnd_cfg, 0, sizeof (wnd_cfg));
162 wnd_cfg.type = VOUT_WINDOW_TYPE_NSOBJECT;
163 wnd_cfg.x = var_InheritInteger (vd, "video-x");
164 wnd_cfg.y = var_InheritInteger (vd, "video-y");
165 wnd_cfg.width = vd->cfg->display.width;
166 wnd_cfg.height = vd->cfg->display.height;
168 sys->embed = vout_display_NewWindow (vd, &wnd_cfg);
170 container = sys->embed->handle.nsobject;
173 msg_Err(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
178 /* This will be released in Close(), on
179 * main thread, after we are done using it. */
180 sys->container = [container retain];
182 /* Get our main view*/
183 nsPool = [[NSAutoreleasePool alloc] init];
185 [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
187 msg_Err(vd, "Initialization of open gl view failed");
191 [sys->glView setVoutDisplay:vd];
193 /* We don't wait, that means that we'll have to be careful about releasing
195 * That's why we'll release on main thread in Close(). */
196 if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
197 [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
198 else if ([container isKindOfClass:[NSView class]]) {
199 NSView *parentView = container;
200 [parentView performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
201 [sys->glView performSelectorOnMainThread:@selector(setFrameToBoundsOfView:) withObject:[NSValue valueWithPointer:parentView] waitUntilDone:NO];
203 msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
211 /* Initialize common OpenGL video display */
212 sys->gl.lock = OpenglLock;
213 sys->gl.unlock = OpenglUnlock;
214 sys->gl.swap = OpenglSwap;
215 sys->gl.getProcAddress = OurGetProcAddress;
217 const vlc_fourcc_t *subpicture_chromas;
219 sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, &sys->gl);
221 msg_Err(vd, "Error while initializing opengl display.");
227 vout_display_info_t info = vd->info;
228 info.has_pictures_invalid = false;
229 info.has_event_thread = true;
230 info.subpicture_chromas = subpicture_chromas;
231 info.has_hide_mouse = true;
233 /* Setup vout_display_t once everything is fine */
237 vd->prepare = PictureRender;
238 vd->display = PictureDisplay;
239 vd->control = Control;
242 vout_display_SendEventDisplaySize (vd, vd->fmt.i_visible_width, vd->fmt.i_visible_height, false);
252 void Close (vlc_object_t *this)
254 vout_display_t *vd = (vout_display_t *)this;
255 vout_display_sys_t *sys = vd->sys;
257 [sys->glView setVoutDisplay:nil];
259 var_Destroy (vd, "drawable-nsobject");
260 if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
261 /* This will retain sys->glView */
262 [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
264 /* release on main thread as explained in Open() */
265 [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
266 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
268 if (sys->gl.sys != NULL)
269 vout_display_opengl_Delete (sys->vgl);
271 [sys->glView release];
274 vout_display_DeleteWindow (vd, sys->embed);
278 /*****************************************************************************
279 * vout display callbacks
280 *****************************************************************************/
282 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
284 vout_display_sys_t *sys = vd->sys;
287 sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
292 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
295 vout_display_sys_t *sys = vd->sys;
297 vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
300 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
302 vout_display_sys_t *sys = vd->sys;
303 [sys->glView setVoutFlushing:YES];
304 vout_display_opengl_Display (sys->vgl, &vd->source);
305 [sys->glView setVoutFlushing:NO];
306 picture_Release (pic);
307 sys->has_first_frame = true;
310 subpicture_Delete(subpicture);
313 static int Control (vout_display_t *vd, int query, va_list ap)
315 vout_display_sys_t *sys = vd->sys;
325 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
327 const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
328 if (vout_window_SetFullScreen (sys->embed, cfg->is_fullscreen))
333 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
335 unsigned state = va_arg (ap, unsigned);
336 return vout_window_SetState (sys->embed, state);
338 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
339 case VOUT_DISPLAY_CHANGE_ZOOM:
340 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
341 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
342 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
344 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
346 id o_window = [sys->glView window];
349 return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
352 NSSize windowMinSize = [o_window minSize];
354 const vout_display_cfg_t *cfg;
355 const video_format_t *source;
356 bool is_forced = false;
358 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
359 source = (const video_format_t *)va_arg (ap, const video_format_t *);
362 source = &vd->source;
363 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
364 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
365 is_forced = (bool)va_arg (ap, int);
368 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced
369 && vout_window_SetSize (sys->embed, cfg->display.width, cfg->display.height)) {
374 /* we always use our current frame here, because we have some size constraints
375 in the ui vout provider */
376 vout_display_cfg_t cfg_tmp = *cfg;
378 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
380 bounds = [sys->glView convertRectToBacking:[sys->glView bounds]];
382 bounds = [sys->glView bounds];
383 cfg_tmp.display.width = bounds.size.width;
384 cfg_tmp.display.height = bounds.size.height;
386 vout_display_place_t place;
387 vout_display_PlacePicture (&place, source, &cfg_tmp, false);
388 @synchronized (sys->glView) {
392 /* For resize, we call glViewport in reshape and not here.
393 This has the positive side effect that we avoid erratic sizing as we animate every resize. */
394 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
395 // x / y are top left corner, but we need the lower left one
396 glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
403 case VOUT_DISPLAY_HIDE_MOUSE:
405 [NSCursor setHiddenUntilMouseMoves: YES];
409 case VOUT_DISPLAY_GET_OPENGL:
411 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
416 case VOUT_DISPLAY_RESET_PICTURES:
419 msg_Err (vd, "Unknown request in Mac OS X vout display");
424 /*****************************************************************************
425 * vout opengl callbacks
426 *****************************************************************************/
427 static int OpenglLock (vlc_gl_t *gl)
429 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
430 if (!sys->glView || ![sys->glView respondsToSelector:@selector(openGLContext)])
433 NSOpenGLContext *context = [sys->glView openGLContext];
434 CGLError err = CGLLockContext ([context CGLContextObj]);
435 if (kCGLNoError == err) {
436 [context makeCurrentContext];
442 static void OpenglUnlock (vlc_gl_t *gl)
444 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
445 CGLUnlockContext ([[sys->glView openGLContext] CGLContextObj]);
448 static void OpenglSwap (vlc_gl_t *gl)
450 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
451 [[sys->glView openGLContext] flushBuffer];
454 /*****************************************************************************
456 *****************************************************************************/
457 @implementation VLCOpenGLVideoView
459 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
462 + (void)getNewView:(NSValue *)value
464 id *ret = [value pointerValue];
465 *ret = [[self alloc] init];
469 * Gets called by the Open() method.
473 VLCAssertMainThread();
475 /* Warning - this may be called on non main thread */
477 NSOpenGLPixelFormatAttribute attribs[] =
479 NSOpenGLPFADoubleBuffer,
480 NSOpenGLPFAAccelerated,
481 NSOpenGLPFANoRecovery,
482 NSOpenGLPFAColorSize, 24,
483 NSOpenGLPFAAlphaSize, 8,
484 NSOpenGLPFADepthSize, 24,
486 NSOpenGLPFAAllowOfflineRenderers,
490 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
495 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
501 /* enable HiDPI support on OS X 10.7 and later */
503 [self setWantsBestResolutionOpenGLSurface:YES];
505 /* Swap buffers only during the vertical retrace of the monitor.
506 http://developer.apple.com/documentation/GraphicsImaging/
507 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
508 GLint params[] = { 1 };
509 CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
511 [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidChangeScreenParametersNotification
512 object:[NSApplication sharedApplication]
514 usingBlock:^(NSNotification *notification) {
515 [self performSelectorOnMainThread:@selector(reshape)
520 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
526 [[NSNotificationCenter defaultCenter] removeObserver:self];
531 * Gets called by the Open() method.
533 - (void)setFrameToBoundsOfView:(NSValue *)value
535 NSView *parentView = [value pointerValue];
536 [self setFrame:[parentView bounds]];
540 * Gets called by the Close and Open methods.
543 - (void)setVoutDisplay:(vout_display_t *)aVd
545 @synchronized(self) {
551 * Gets called when the vout will aquire the lock and flush.
554 - (void)setVoutFlushing:(BOOL)flushing
558 @synchronized(self) {
559 _hasPendingReshape = NO;
564 * Can -drawRect skip rendering?.
566 - (BOOL)canSkipRendering
568 VLCAssertMainThread();
570 @synchronized(self) {
571 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
572 return !_hasPendingReshape && hasFirstFrame;
578 * Local method that locks the gl context.
582 VLCAssertMainThread();
583 NSOpenGLContext *context = [self openGLContext];
584 CGLError err = CGLLockContext ([context CGLContextObj]);
585 if (err == kCGLNoError)
586 [context makeCurrentContext];
587 return err == kCGLNoError;
591 * Local method that unlocks the gl context.
595 VLCAssertMainThread();
596 CGLUnlockContext ([[self openGLContext] CGLContextObj]);
600 * Local method that force a rendering of a frame.
601 * This will get called if Cocoa forces us to redraw (via -drawRect).
605 VLCAssertMainThread();
607 // We may have taken some times to take the opengl Lock.
608 // Check here to see if we can just skip the frame as well.
609 if ([self canSkipRendering])
613 @synchronized(self) { // vd can be accessed from multiple threads
614 hasFirstFrame = vd && vd->sys->has_first_frame;
618 // This will lock gl.
619 vout_display_opengl_Display (vd->sys->vgl, &vd->source);
621 glClear (GL_COLOR_BUFFER_BIT);
625 * Method called by Cocoa when the view is resized.
629 VLCAssertMainThread();
632 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
634 bounds = [self convertRectToBacking:[self bounds]];
636 bounds = [self bounds];
637 vout_display_place_t place;
639 @synchronized(self) {
641 vout_display_cfg_t cfg_tmp = *(vd->cfg);
642 cfg_tmp.display.width = bounds.size.width;
643 cfg_tmp.display.height = bounds.size.height;
645 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
646 vd->sys->place = place;
647 vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height, vd->cfg->is_fullscreen);
652 // x / y are top left corner, but we need the lower left one
653 glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
655 @synchronized(self) {
656 // This may be cleared before -drawRect is being called,
657 // in this case we'll skip the rendering.
658 // This will save us for rendering two frames (or more) for nothing
659 // (one by the vout, one (or more) by drawRect)
660 _hasPendingReshape = YES;
670 * Method called by Cocoa when the view is resized or the location has changed.
671 * We just need to make sure we are locking here.
675 VLCAssertMainThread();
676 BOOL success = [self lockgl];
686 * Method called by Cocoa to force redraw.
688 - (void)drawRect:(NSRect) rect
690 VLCAssertMainThread();
692 if ([self canSkipRendering])
695 BOOL success = [self lockgl];
706 NSWindow *window = [self window];
708 // Remove flashes with splitter view.
709 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
710 [window disableScreenUpdatesUntilFlush];
721 #pragma mark Mouse handling
723 - (void)mouseDown:(NSEvent *)o_event
725 @synchronized (self) {
727 if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] & NSControlKeyMask)) {
728 if ([o_event clickCount] <= 1)
729 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
734 [super mouseDown:o_event];
737 - (void)otherMouseDown:(NSEvent *)o_event
739 @synchronized (self) {
741 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
744 [super otherMouseDown: o_event];
747 - (void)mouseUp:(NSEvent *)o_event
749 @synchronized (self) {
751 if ([o_event type] == NSLeftMouseUp)
752 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
756 [super mouseUp: o_event];
759 - (void)otherMouseUp:(NSEvent *)o_event
761 @synchronized (self) {
763 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
766 [super otherMouseUp: o_event];
769 - (void)mouseMoved:(NSEvent *)o_event
771 /* on HiDPI displays, the point bounds don't equal the actual pixel based bounds */
772 NSPoint ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
773 NSRect videoRect = [self bounds];
774 BOOL b_inside = [self mouse: ml inRect: videoRect];
777 ml = [self convertPointToBacking: ml];
778 videoRect = [self convertRectToBacking: videoRect];
782 @synchronized (self) {
784 vout_display_SendMouseMovedDisplayCoordinates(vd, ORIENT_NORMAL,
785 (int)ml.x, videoRect.size.height - (int)ml.y,
791 [super mouseMoved: o_event];
794 - (void)mouseDragged:(NSEvent *)o_event
796 [self mouseMoved: o_event];
797 [super mouseDragged: o_event];
800 - (void)otherMouseDragged:(NSEvent *)o_event
802 [self mouseMoved: o_event];
803 [super otherMouseDragged: o_event];
806 - (void)rightMouseDragged:(NSEvent *)o_event
808 [self mouseMoved: o_event];
809 [super rightMouseDragged: o_event];
812 - (BOOL)acceptsFirstResponder
817 - (BOOL)mouseDownCanMoveWindow