1 /*****************************************************************************
2 * voutgl.m: MacOS X OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2009 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>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
29 *****************************************************************************/
31 /*****************************************************************************
33 *****************************************************************************/
35 #import <Cocoa/Cocoa.h>
36 #import <OpenGL/OpenGL.h>
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_vout_display.h>
45 #include <vlc_vout_opengl.h>
49 * Forward declarations
51 static int Open(vlc_object_t *);
52 static void Close(vlc_object_t *);
54 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count);
55 static void PictureRender(vout_display_t *vd, picture_t *pic);
56 static void PictureDisplay(vout_display_t *vd, picture_t *pic);
57 static int Control (vout_display_t *vd, int query, va_list ap);
59 static int OpenglLock(vout_opengl_t *gl);
60 static void OpenglUnlock(vout_opengl_t *gl);
61 static void OpenglSwap(vout_opengl_t *gl);
67 /* Will be loaded even without interface module. see voutgl.m */
68 set_shortname("Mac OS X")
69 set_description( N_("Mac OS X OpenGL video output (requires drawable-nsobject)"))
70 set_category(CAT_VIDEO)
71 set_subcategory(SUBCAT_VIDEO_VOUT )
72 set_capability("vout display", 300)
73 set_callbacks(Open, Close)
75 add_shortcut("macosx", "vout_macosx")
79 * Obj-C protocol declaration that drawable-nsobject should follow
81 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
82 - (void)addVoutSubview:(NSView *)view;
83 - (void)removeVoutSubview:(NSView *)view;
86 @interface VLCOpenGLVideoView : NSOpenGLView
89 BOOL _hasPendingReshape;
91 - (void)setVoutDisplay:(vout_display_t *)vd;
92 - (void)setVoutFlushing:(BOOL)flushing;
96 struct vout_display_sys_t
98 VLCOpenGLVideoView *glView;
99 id<VLCOpenGLVideoViewEmbedding> container;
102 vout_display_opengl_t vgl;
104 picture_pool_t *pool;
106 bool has_first_frame;
109 static int Open(vlc_object_t *this)
111 vout_display_t *vd = (vout_display_t *)this;
112 vout_display_sys_t *sys = calloc(1, sizeof(*sys));
113 NSAutoreleasePool *nsPool = nil;
122 /* Get the drawable object */
123 id container = var_CreateGetAddress(vd, "drawable-nsobject");
126 msg_Dbg(vd, "No drawable-nsobject, passing over.");
129 vout_display_DeleteWindow(vd, NULL);
131 /* This will be released in Close(), on
132 * main thread, after we are done using it. */
133 sys->container = [container retain];
135 /* Get our main view*/
136 nsPool = [[NSAutoreleasePool alloc] init];
138 [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
142 [sys->glView setVoutDisplay:vd];
144 /* We don't wait, that means that we'll have to be careful about releasing
146 * That's why we'll release on main thread in Close(). */
147 if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
148 [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
149 else if ([container isKindOfClass:[NSView class]])
151 NSView *parentView = container;
152 [parentView performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
153 [sys->glView performSelectorOnMainThread:@selector(setFrame:) withObject:[NSValue valueWithRect:[parentView bounds]] waitUntilDone:NO];
157 msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
165 /* Initialize common OpenGL video display */
166 sys->gl.lock = OpenglLock;
167 sys->gl.unlock = OpenglUnlock;
168 sys->gl.swap = OpenglSwap;
171 if (vout_display_opengl_Init(&sys->vgl, &vd->fmt, &sys->gl))
178 vout_display_info_t info = vd->info;
179 info.has_pictures_invalid = false;
181 /* Setup vout_display_t once everything is fine */
185 vd->prepare = PictureRender;
186 vd->display = PictureDisplay;
187 vd->control = Control;
190 vout_display_SendEventFullscreen (vd, false);
191 vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
201 void Close(vlc_object_t *this)
203 vout_display_t *vd = (vout_display_t *)this;
204 vout_display_sys_t *sys = vd->sys;
206 [sys->glView setVoutDisplay:nil];
208 var_Destroy(vd, "drawable-nsobject");
209 if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
211 /* This will retain sys->glView */
212 [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
214 /* release on main thread as explained in Open() */
215 [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
216 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
218 [sys->glView release];
220 if (sys->gl.sys != NULL)
221 vout_display_opengl_Clean(&sys->vgl);
226 /*****************************************************************************
227 * vout display callbacks
228 *****************************************************************************/
230 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
232 vout_display_sys_t *sys = vd->sys;
233 VLC_UNUSED(requested_count);
236 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
241 static void PictureRender(vout_display_t *vd, picture_t *pic)
244 vout_display_sys_t *sys = vd->sys;
246 vout_display_opengl_Prepare( &sys->vgl, pic );
249 static void PictureDisplay(vout_display_t *vd, picture_t *pic)
251 vout_display_sys_t *sys = vd->sys;
252 [sys->glView setVoutFlushing:YES];
253 vout_display_opengl_Display(&sys->vgl, &vd->fmt );
254 [sys->glView setVoutFlushing:NO];
255 picture_Release (pic);
256 sys->has_first_frame = true;
259 static int Control (vout_display_t *vd, int query, va_list ap)
261 vout_display_sys_t *sys = vd->sys;
265 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
266 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
267 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
268 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
269 case VOUT_DISPLAY_CHANGE_ZOOM:
270 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
271 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
276 case VOUT_DISPLAY_HIDE_MOUSE:
279 case VOUT_DISPLAY_GET_OPENGL:
281 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
286 case VOUT_DISPLAY_RESET_PICTURES:
289 msg_Err (vd, "Unknown request in Mac OS X vout display");
294 /*****************************************************************************
295 * vout opengl callbacks
296 *****************************************************************************/
297 static int OpenglLock(vout_opengl_t *gl)
299 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
300 NSOpenGLContext *context = [sys->glView openGLContext];
301 CGLError err = CGLLockContext([context CGLContextObj]);
302 if (kCGLNoError == err)
304 [context makeCurrentContext];
310 static void OpenglUnlock(vout_opengl_t *gl)
312 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
313 CGLUnlockContext([[sys->glView openGLContext] CGLContextObj]);
316 static void OpenglSwap(vout_opengl_t *gl)
318 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
319 [[sys->glView openGLContext] flushBuffer];
322 /*****************************************************************************
324 *****************************************************************************/
325 @implementation VLCOpenGLVideoView
327 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
329 + (void)getNewView:(NSValue *)value
331 id *ret = [value pointerValue];
332 *ret = [[self alloc] init];
336 * Gets called by the Open() method.
340 VLCAssertMainThread();
342 /* Warning - this may be called on non main thread */
344 NSOpenGLPixelFormatAttribute attribs[] =
346 NSOpenGLPFADoubleBuffer,
347 NSOpenGLPFAAccelerated,
348 NSOpenGLPFANoRecovery,
349 NSOpenGLPFAColorSize, 24,
350 NSOpenGLPFAAlphaSize, 8,
351 NSOpenGLPFADepthSize, 24,
356 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
361 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
367 /* Swap buffers only during the vertical retrace of the monitor.
368 http://developer.apple.com/documentation/GraphicsImaging/
369 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
370 GLint params[] = { 1 };
371 CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
373 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
378 * Gets called by the Close and Open methods.
381 - (void)setVoutDisplay:(vout_display_t *)aVd
383 @synchronized(self) {
390 * Gets called when the vout will aquire the lock and flush.
393 - (void)setVoutFlushing:(BOOL)flushing
397 @synchronized(self) {
398 _hasPendingReshape = NO;
403 * Can -drawRect skip rendering?.
405 - (BOOL)canSkipRendering
407 VLCAssertMainThread();
409 @synchronized(self) {
410 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
411 return !_hasPendingReshape && hasFirstFrame;
417 * Local method that locks the gl context.
421 VLCAssertMainThread();
422 NSOpenGLContext *context = [self openGLContext];
423 CGLError err = CGLLockContext([context CGLContextObj]);
424 if (err == kCGLNoError)
425 [context makeCurrentContext];
426 return err == kCGLNoError;
430 * Local method that unlocks the gl context.
434 VLCAssertMainThread();
435 CGLUnlockContext([[self openGLContext] CGLContextObj]);
439 * Local method that force a rendering of a frame.
440 * This will get called if Cocoa forces us to redraw (via -drawRect).
444 VLCAssertMainThread();
446 // We may have taken some times to take the opengl Lock.
447 // Check here to see if we can just skip the frame as well.
448 if ([self canSkipRendering])
452 @synchronized(self) { // vd can be accessed from multiple threads
453 hasFirstFrame = vd && vd->sys->has_first_frame;
457 // This will lock gl.
458 vout_display_opengl_Display( &vd->sys->vgl, &vd->source );
461 glClear(GL_COLOR_BUFFER_BIT);
465 * Method called by Cocoa when the view is resized.
469 VLCAssertMainThread();
471 NSRect bounds = [self bounds];
473 CGFloat height = bounds.size.height;
474 CGFloat width = bounds.size.width;
476 GLint x = width, y = height;
478 @synchronized(self) {
480 CGFloat videoHeight = vd->source.i_visible_height;
481 CGFloat videoWidth = vd->source.i_visible_width;
483 GLint sarNum = vd->source.i_sar_num;
484 GLint sarDen = vd->source.i_sar_den;
486 if (height * videoWidth * sarNum < width * videoHeight * sarDen)
488 x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
494 y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
500 glViewport((width - x) / 2, (height - y) / 2, x, y);
502 @synchronized(self) {
503 // This may be cleared before -drawRect is being called,
504 // in this case we'll skip the rendering.
505 // This will save us for rendering two frames (or more) for nothing
506 // (one by the vout, one (or more) by drawRect)
507 _hasPendingReshape = YES;
517 * Method called by Cocoa when the view is resized or the location has changed.
518 * We just need to make sure we are locking here.
522 VLCAssertMainThread();
523 BOOL success = [self lockgl];
533 * Method called by Cocoa to force redraw.
535 - (void)drawRect:(NSRect) rect
537 VLCAssertMainThread();
539 if ([self canSkipRendering])
542 BOOL success = [self lockgl];
553 NSWindow *window = [self window];
555 // Remove flashes with splitter view.
556 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
557 [window disableScreenUpdatesUntilFlush];
562 - (BOOL)mouseDownCanMoveWindow