1 /*****************************************************************************
2 * macosx.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2012 the VideoLAN team
7 * Authors: Colin Delacroix <colin@zoy.org>
8 * Florian G. Pflug <fgp@phlo.org>
9 * Jon Lech Johansen <jon-vl@nanocrew.net>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Eric Petit <titer@m0k.org>
12 * Benjamin Pracht <bigben at videolan dot org>
13 * Damien Fouilleul <damienf at videolan dot org>
14 * Pierre d'Herbemont <pdherbemont at videolan dot org>
15 * Felix Paul Kühne <fkuehne at videolan dot org>
16 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 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 General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, 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 @interface NSWindow (VLCCustomCode)
57 * Forward declarations
59 static int Open (vlc_object_t *);
60 static void Close (vlc_object_t *);
62 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
63 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
64 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
65 static int Control (vout_display_t *vd, int query, va_list ap);
67 static void *OurGetProcAddress(vlc_gl_t *, const char *);
69 static int OpenglLock (vlc_gl_t *gl);
70 static void OpenglUnlock (vlc_gl_t *gl);
71 static void OpenglSwap (vlc_gl_t *gl);
77 /* Will be loaded even without interface module. see voutgl.m */
78 set_shortname ("Mac OS X")
79 set_description (N_("Mac OS X OpenGL video output (requires drawable-nsobject)"))
80 set_category (CAT_VIDEO)
81 set_subcategory (SUBCAT_VIDEO_VOUT )
82 set_capability ("vout display", 300)
83 set_callbacks (Open, Close)
85 add_shortcut ("macosx", "vout_macosx")
89 * Obj-C protocol declaration that drawable-nsobject should follow
91 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
92 - (void)addVoutSubview:(NSView *)view;
93 - (void)removeVoutSubview:(NSView *)view;
96 @interface VLCOpenGLVideoView : NSOpenGLView
99 BOOL _hasPendingReshape;
101 - (void)setVoutDisplay:(vout_display_t *)vd;
102 - (vout_display_t *)voutDisplay;
103 - (void)setVoutFlushing:(BOOL)flushing;
107 struct vout_display_sys_t
109 VLCOpenGLVideoView *glView;
110 id<VLCOpenGLVideoViewEmbedding> container;
112 vout_window_t *embed;
114 vout_display_opengl_t *vgl;
116 picture_pool_t *pool;
118 bool has_first_frame;
122 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
126 return dlsym(RTLD_DEFAULT, name);
129 static int Open (vlc_object_t *this)
131 vout_display_t *vd = (vout_display_t *)this;
132 vout_display_sys_t *sys = calloc (1, sizeof(*sys));
133 NSAutoreleasePool *nsPool = nil;
138 if (!CGDisplayUsesOpenGLAcceleration (kCGDirectMainDisplay))
140 msg_Err (this, "no OpenGL hardware acceleration found. this can lead to slow output and unexpected results");
141 dialog_Fatal (this, _("OpenGL acceleration is not supported on your Mac"), _("Your Mac lacks Quartz Extreme acceleration, which is required for video output. It will still work, but much slower and with possibly unexpected results."));
144 msg_Dbg (this, "Quartz Extreme acceleration is active");
151 /* Get the drawable object */
152 id container = var_CreateGetAddress (vd, "drawable-nsobject");
155 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;
174 msg_Dbg(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
179 /* This will be released in Close(), on
180 * main thread, after we are done using it. */
181 sys->container = [container retain];
183 /* Get our main view*/
184 nsPool = [[NSAutoreleasePool alloc] init];
186 [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
190 [sys->glView setVoutDisplay:vd];
192 /* We don't wait, that means that we'll have to be careful about releasing
194 * That's why we'll release on main thread in Close(). */
195 if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
196 [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
197 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(setFrameWithValue:) withObject:[NSValue valueWithRect:[parentView bounds]] waitUntilDone:NO];
205 msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
213 /* Initialize common OpenGL video display */
214 sys->gl.lock = OpenglLock;
215 sys->gl.unlock = OpenglUnlock;
216 sys->gl.swap = OpenglSwap;
217 sys->gl.getProcAddress = OurGetProcAddress;
219 const vlc_fourcc_t *subpicture_chromas;
220 video_format_t fmt = vd->fmt;
222 sys->vgl = vout_display_opengl_New (&vd->fmt, &subpicture_chromas, &sys->gl);
230 vout_display_info_t info = vd->info;
231 info.has_pictures_invalid = false;
232 info.has_event_thread = true;
233 info.subpicture_chromas = subpicture_chromas;
235 /* Setup vout_display_t once everything is fine */
239 vd->prepare = PictureRender;
240 vd->display = PictureDisplay;
241 vd->control = Control;
244 vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
254 void Close (vlc_object_t *this)
256 vout_display_t *vd = (vout_display_t *)this;
257 vout_display_sys_t *sys = vd->sys;
259 if ([[sys->glView window] level] != NSNormalWindowLevel)
260 [[sys->glView window] setLevel: NSNormalWindowLevel];
262 [sys->glView setVoutDisplay:nil];
264 var_Destroy (vd, "drawable-nsobject");
265 if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
267 /* This will retain sys->glView */
268 [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
270 /* release on main thread as explained in Open() */
271 [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
272 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
274 [sys->glView release];
276 if (sys->gl.sys != NULL)
277 vout_display_opengl_Delete (sys->vgl);
280 vout_display_DeleteWindow (vd, sys->embed);
284 /*****************************************************************************
285 * vout display callbacks
286 *****************************************************************************/
288 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
290 vout_display_sys_t *sys = vd->sys;
293 sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
298 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
301 vout_display_sys_t *sys = vd->sys;
303 vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
306 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
308 vout_display_sys_t *sys = vd->sys;
309 [sys->glView setVoutFlushing:YES];
310 vout_display_opengl_Display (sys->vgl, &vd->source);
311 [sys->glView setVoutFlushing:NO];
312 picture_Release (pic);
313 sys->has_first_frame = true;
316 subpicture_Delete(subpicture);
319 static int Control (vout_display_t *vd, int query, va_list ap)
321 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 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
336 unsigned state = va_arg (ap, unsigned);
337 [sys->glView performSelectorOnMainThread:@selector(setWindowLevel:) withObject:[NSNumber numberWithUnsignedInt:state] waitUntilDone:NO];
341 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
342 case VOUT_DISPLAY_CHANGE_ZOOM:
343 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
344 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
345 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
350 NSAutoreleasePool * o_pool = [[NSAutoreleasePool alloc] init];
352 id o_window = [sys->glView window];
354 return VLC_SUCCESS; // this is okay, since the event will occur again when we have a window
356 NSSize windowMinSize = [o_window minSize];
358 const vout_display_cfg_t *cfg;
359 const video_format_t *source;
360 bool is_forced = false;
362 vout_display_place_t place;
364 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
366 source = (const video_format_t *)va_arg (ap, const video_format_t *);
371 source = &vd->source;
372 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
373 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
374 is_forced = (bool)va_arg (ap, int);
377 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced
378 && (cfg->display.width != vd->cfg->display.width
379 || cfg->display.height != vd->cfg->display.height)
380 && vout_window_SetSize (sys->embed, cfg->display.width, cfg->display.height))
383 /* for the case that the core wants to resize below minimum window size we correct the size here
384 to ensure a centered picture */
385 vout_display_cfg_t cfg_tmp = *cfg;
386 if (cfg_tmp.display.width < windowMinSize.width)
387 cfg_tmp.display.width = windowMinSize.width;
388 if (cfg_tmp.display.height < 70)
389 cfg_tmp.display.height = 70;
391 if (!config_GetInt(vd, "macosx-video-autoresize"))
393 NSRect bounds = [sys->glView bounds];
394 cfg_tmp.display.width = bounds.size.width;
395 cfg_tmp.display.height = bounds.size.height;
398 vout_display_PlacePicture (&place, source, &cfg_tmp, false);
400 /* For resize, we call glViewport in reshape and not here.
401 This has the positive side effect that we avoid erratic sizing as we animate every resize. */
402 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
404 // x / y are top left corner, but we need the lower left one
405 glViewport (place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
412 case VOUT_DISPLAY_HIDE_MOUSE:
414 [NSCursor setHiddenUntilMouseMoves: YES];
418 case VOUT_DISPLAY_GET_OPENGL:
420 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
425 case VOUT_DISPLAY_RESET_PICTURES:
428 msg_Err (vd, "Unknown request in Mac OS X vout display");
433 /*****************************************************************************
434 * vout opengl callbacks
435 *****************************************************************************/
436 static int OpenglLock (vlc_gl_t *gl)
438 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
439 NSOpenGLContext *context = [sys->glView openGLContext];
440 CGLError err = CGLLockContext ([context CGLContextObj]);
441 if (kCGLNoError == err)
443 [context makeCurrentContext];
449 static void OpenglUnlock (vlc_gl_t *gl)
451 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
452 CGLUnlockContext ([[sys->glView openGLContext] CGLContextObj]);
455 static void OpenglSwap (vlc_gl_t *gl)
457 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
458 [[sys->glView openGLContext] flushBuffer];
461 /*****************************************************************************
463 *****************************************************************************/
464 @implementation VLCOpenGLVideoView
466 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
469 + (void)getNewView:(NSValue *)value
471 id *ret = [value pointerValue];
472 *ret = [[self alloc] init];
476 * Gets called by the Open() method.
480 VLCAssertMainThread();
482 /* Warning - this may be called on non main thread */
484 NSOpenGLPixelFormatAttribute attribs[] =
486 NSOpenGLPFADoubleBuffer,
487 NSOpenGLPFAAccelerated,
488 NSOpenGLPFANoRecovery,
489 NSOpenGLPFAColorSize, 24,
490 NSOpenGLPFAAlphaSize, 8,
491 NSOpenGLPFADepthSize, 24,
496 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
501 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
507 /* Swap buffers only during the vertical retrace of the monitor.
508 http://developer.apple.com/documentation/GraphicsImaging/
509 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
510 GLint params[] = { 1 };
511 CGLSetParameter ([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
513 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
518 * Gets called by the Open() method.
520 - (void)setFrameWithValue:(NSValue *)value
522 [self setFrame:[value rectValue]];
526 * Gets called by the Close and Open methods.
529 - (void)setVoutDisplay:(vout_display_t *)aVd
531 @synchronized(self) {
536 - (vout_display_t *)voutDisplay
542 * Gets called when the vout will aquire the lock and flush.
545 - (void)setVoutFlushing:(BOOL)flushing
549 @synchronized(self) {
550 _hasPendingReshape = NO;
555 * Can -drawRect skip rendering?.
557 - (BOOL)canSkipRendering
559 VLCAssertMainThread();
561 @synchronized(self) {
562 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
563 return !_hasPendingReshape && hasFirstFrame;
569 * Local method that locks the gl context.
573 VLCAssertMainThread();
574 NSOpenGLContext *context = [self openGLContext];
575 CGLError err = CGLLockContext ([context CGLContextObj]);
576 if (err == kCGLNoError)
577 [context makeCurrentContext];
578 return err == kCGLNoError;
582 * Local method that unlocks the gl context.
586 VLCAssertMainThread();
587 CGLUnlockContext ([[self openGLContext] CGLContextObj]);
591 * Local method that force a rendering of a frame.
592 * This will get called if Cocoa forces us to redraw (via -drawRect).
596 VLCAssertMainThread();
598 // We may have taken some times to take the opengl Lock.
599 // Check here to see if we can just skip the frame as well.
600 if ([self canSkipRendering])
604 @synchronized(self) { // vd can be accessed from multiple threads
605 hasFirstFrame = vd && vd->sys->has_first_frame;
609 // This will lock gl.
610 vout_display_opengl_Display (vd->sys->vgl, &vd->source);
613 glClear (GL_COLOR_BUFFER_BIT);
617 * Method called by Cocoa when the view is resized.
621 VLCAssertMainThread();
623 NSRect bounds = [self bounds];
624 vout_display_place_t place;
626 @synchronized(self) {
628 vout_display_cfg_t cfg_tmp = *(vd->cfg);
629 cfg_tmp.display.width = bounds.size.width;
630 cfg_tmp.display.height = bounds.size.height;
632 vout_display_PlacePicture (&place, &vd->source, &cfg_tmp, false);
633 vout_display_SendEventDisplaySize (vd, bounds.size.width, bounds.size.height, vd->cfg->is_fullscreen);
638 // x / y are top left corner, but we need the lower left one
639 glViewport (place.x, bounds.size.height - (place.y + place.height), place.width, place.height);
641 @synchronized(self) {
642 // This may be cleared before -drawRect is being called,
643 // in this case we'll skip the rendering.
644 // This will save us for rendering two frames (or more) for nothing
645 // (one by the vout, one (or more) by drawRect)
646 _hasPendingReshape = YES;
656 * Method called by Cocoa when the view is resized or the location has changed.
657 * We just need to make sure we are locking here.
661 VLCAssertMainThread();
662 BOOL success = [self lockgl];
672 * Method called by Cocoa to force redraw.
674 - (void)drawRect:(NSRect) rect
676 VLCAssertMainThread();
678 if ([self canSkipRendering])
681 BOOL success = [self lockgl];
692 NSWindow *window = [self window];
694 // Remove flashes with splitter view.
695 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
696 [window disableScreenUpdatesUntilFlush];
706 - (void)setWindowLevel:(NSNumber*)state
708 if ([state unsignedIntValue] & VOUT_WINDOW_STATE_ABOVE)
709 [[self window] setLevel: NSStatusWindowLevel];
711 [[self window] setLevel: NSNormalWindowLevel];
715 #pragma mark Mouse handling
717 - (void)mouseDown:(NSEvent *)o_event
719 if ([o_event type] == NSLeftMouseDown && !([o_event modifierFlags] & NSControlKeyMask))
721 if ([o_event clickCount] <= 1)
722 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_LEFT);
725 [super mouseDown:o_event];
728 - (void)otherMouseDown:(NSEvent *)o_event
730 vout_display_SendEventMousePressed (vd, MOUSE_BUTTON_CENTER);
732 [super otherMouseDown: o_event];
735 - (void)mouseUp:(NSEvent *)o_event
737 if ([o_event type] == NSLeftMouseUp)
738 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_LEFT);
740 [super mouseUp: o_event];
743 - (void)otherMouseUp:(NSEvent *)o_event
745 vout_display_SendEventMouseReleased (vd, MOUSE_BUTTON_CENTER);
747 [super otherMouseUp: o_event];
750 - (void)mouseMoved:(NSEvent *)o_event
756 s_rect = [self bounds];
757 ml = [self convertPoint: [o_event locationInWindow] fromView: nil];
758 b_inside = [self mouse: ml inRect: s_rect];
762 vout_display_place_t place;
763 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
765 if (place.width > 0 && place.height > 0)
767 const int x = vd->source.i_x_offset +
768 (int64_t)(ml.x - place.x) * vd->source.i_visible_width / place.width;
769 const int y = vd->source.i_y_offset +
770 (int64_t)((int)s_rect.size.height - (int)ml.y - place.y) * vd->source.i_visible_height / place.height;
772 vout_display_SendEventMouseMoved (vd, x, y);
776 [super mouseMoved: o_event];
779 - (void)mouseDragged:(NSEvent *)o_event
781 [self mouseMoved: o_event];
782 [super mouseDragged: o_event];
785 - (void)otherMouseDragged:(NSEvent *)o_event
787 [self mouseMoved: o_event];
788 [super otherMouseDragged: o_event];
791 - (void)rightMouseDragged:(NSEvent *)o_event
793 [self mouseMoved: o_event];
794 [super rightMouseDragged: o_event];
797 - (BOOL)acceptsFirstResponder
802 - (BOOL)mouseDownCanMoveWindow