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_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, subpicture_t *subpicture);
56 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
57 static int Control (vout_display_t *vd, int query, va_list ap);
59 static int OpenglLock(vlc_gl_t *gl);
60 static void OpenglUnlock(vlc_gl_t *gl);
61 static void OpenglSwap(vlc_gl_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;
101 vout_window_t *embed;
103 vout_display_opengl_t *vgl;
105 picture_pool_t *pool;
107 bool has_first_frame;
110 static int Open(vlc_object_t *this)
112 vout_display_t *vd = (vout_display_t *)this;
113 vout_display_sys_t *sys = calloc(1, sizeof(*sys));
114 NSAutoreleasePool *nsPool = nil;
124 /* Get the drawable object */
125 id container = var_CreateGetAddress(vd, "drawable-nsobject");
128 vout_display_DeleteWindow(vd, NULL);
132 vout_window_cfg_t wnd_cfg;
134 memset (&wnd_cfg, 0, sizeof (wnd_cfg));
135 wnd_cfg.type = VOUT_WINDOW_TYPE_NSOBJECT;
136 wnd_cfg.x = var_InheritInteger (vd, "video-x");
137 wnd_cfg.y = var_InheritInteger (vd, "video-y");
138 wnd_cfg.width = vd->cfg->display.width;
139 wnd_cfg.height = vd->cfg->display.height;
141 sys->embed = vout_display_NewWindow (vd, &wnd_cfg);
143 container = sys->embed->handle.nsobject;
147 msg_Dbg(vd, "No drawable-nsobject nor vout_window_t found, passing over.");
152 /* This will be released in Close(), on
153 * main thread, after we are done using it. */
154 sys->container = [container retain];
156 /* Get our main view*/
157 nsPool = [[NSAutoreleasePool alloc] init];
159 [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
163 [sys->glView setVoutDisplay:vd];
165 /* We don't wait, that means that we'll have to be careful about releasing
167 * That's why we'll release on main thread in Close(). */
168 if ([(id)container respondsToSelector:@selector(addVoutSubview:)])
169 [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
170 else if ([container isKindOfClass:[NSView class]])
172 NSView *parentView = container;
173 [parentView performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
174 [sys->glView performSelectorOnMainThread:@selector(setFrameWithValue:) withObject:[NSValue valueWithRect:[parentView bounds]] waitUntilDone:NO];
178 msg_Err(vd, "Invalid drawable-nsobject object. drawable-nsobject must either be an NSView or comply to the @protocol VLCOpenGLVideoViewEmbedding.");
186 /* Initialize common OpenGL video display */
187 sys->gl.lock = OpenglLock;
188 sys->gl.unlock = OpenglUnlock;
189 sys->gl.swap = OpenglSwap;
190 sys->gl.getProcAddress = NULL;
193 sys->vgl = vout_display_opengl_New(&vd->fmt, &sys->gl);
201 vout_display_info_t info = vd->info;
202 info.has_pictures_invalid = false;
204 /* Setup vout_display_t once everything is fine */
208 vd->prepare = PictureRender;
209 vd->display = PictureDisplay;
210 vd->control = Control;
213 vout_display_SendEventFullscreen (vd, false);
214 vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
224 void Close(vlc_object_t *this)
226 vout_display_t *vd = (vout_display_t *)this;
227 vout_display_sys_t *sys = vd->sys;
229 [sys->glView setVoutDisplay:nil];
231 var_Destroy(vd, "drawable-nsobject");
232 if ([(id)sys->container respondsToSelector:@selector(removeVoutSubview:)])
234 /* This will retain sys->glView */
235 [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
237 /* release on main thread as explained in Open() */
238 [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
239 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
241 [sys->glView release];
243 if (sys->gl.sys != NULL)
244 vout_display_opengl_Delete(sys->vgl);
247 vout_display_DeleteWindow(vd, sys->embed);
251 /*****************************************************************************
252 * vout display callbacks
253 *****************************************************************************/
255 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
257 vout_display_sys_t *sys = vd->sys;
258 VLC_UNUSED(requested_count);
261 sys->pool = vout_display_opengl_GetPool (sys->vgl);
266 static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
269 vout_display_sys_t *sys = vd->sys;
271 vout_display_opengl_Prepare( sys->vgl, pic );
275 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
277 vout_display_sys_t *sys = vd->sys;
278 [sys->glView setVoutFlushing:YES];
279 vout_display_opengl_Display(sys->vgl, &vd->fmt );
280 [sys->glView setVoutFlushing:NO];
281 picture_Release (pic);
282 sys->has_first_frame = true;
286 static int Control (vout_display_t *vd, int query, va_list ap)
288 vout_display_sys_t *sys = vd->sys;
292 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
293 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
294 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
295 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
296 case VOUT_DISPLAY_CHANGE_ZOOM:
297 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
298 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
303 case VOUT_DISPLAY_HIDE_MOUSE:
306 case VOUT_DISPLAY_GET_OPENGL:
308 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
313 case VOUT_DISPLAY_RESET_PICTURES:
316 msg_Err (vd, "Unknown request in Mac OS X vout display");
321 /*****************************************************************************
322 * vout opengl callbacks
323 *****************************************************************************/
324 static int OpenglLock(vlc_gl_t *gl)
326 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
327 NSOpenGLContext *context = [sys->glView openGLContext];
328 CGLError err = CGLLockContext([context CGLContextObj]);
329 if (kCGLNoError == err)
331 [context makeCurrentContext];
337 static void OpenglUnlock(vlc_gl_t *gl)
339 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
340 CGLUnlockContext([[sys->glView openGLContext] CGLContextObj]);
343 static void OpenglSwap(vlc_gl_t *gl)
345 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
346 [[sys->glView openGLContext] flushBuffer];
349 /*****************************************************************************
351 *****************************************************************************/
352 @implementation VLCOpenGLVideoView
354 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
356 + (void)getNewView:(NSValue *)value
358 id *ret = [value pointerValue];
359 *ret = [[self alloc] init];
363 * Gets called by the Open() method.
367 VLCAssertMainThread();
369 /* Warning - this may be called on non main thread */
371 NSOpenGLPixelFormatAttribute attribs[] =
373 NSOpenGLPFADoubleBuffer,
374 NSOpenGLPFAAccelerated,
375 NSOpenGLPFANoRecovery,
376 NSOpenGLPFAColorSize, 24,
377 NSOpenGLPFAAlphaSize, 8,
378 NSOpenGLPFADepthSize, 24,
383 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
388 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
394 /* Swap buffers only during the vertical retrace of the monitor.
395 http://developer.apple.com/documentation/GraphicsImaging/
396 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
397 GLint params[] = { 1 };
398 CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
400 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
405 * Gets called by the Open() method.
407 - (void)setFrameWithValue:(NSValue *)value
409 [self setFrame:[value rectValue]];
413 * Gets called by the Close and Open methods.
416 - (void)setVoutDisplay:(vout_display_t *)aVd
418 @synchronized(self) {
425 * Gets called when the vout will aquire the lock and flush.
428 - (void)setVoutFlushing:(BOOL)flushing
432 @synchronized(self) {
433 _hasPendingReshape = NO;
438 * Can -drawRect skip rendering?.
440 - (BOOL)canSkipRendering
442 VLCAssertMainThread();
444 @synchronized(self) {
445 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
446 return !_hasPendingReshape && hasFirstFrame;
452 * Local method that locks the gl context.
456 VLCAssertMainThread();
457 NSOpenGLContext *context = [self openGLContext];
458 CGLError err = CGLLockContext([context CGLContextObj]);
459 if (err == kCGLNoError)
460 [context makeCurrentContext];
461 return err == kCGLNoError;
465 * Local method that unlocks the gl context.
469 VLCAssertMainThread();
470 CGLUnlockContext([[self openGLContext] CGLContextObj]);
474 * Local method that force a rendering of a frame.
475 * This will get called if Cocoa forces us to redraw (via -drawRect).
479 VLCAssertMainThread();
481 // We may have taken some times to take the opengl Lock.
482 // Check here to see if we can just skip the frame as well.
483 if ([self canSkipRendering])
487 @synchronized(self) { // vd can be accessed from multiple threads
488 hasFirstFrame = vd && vd->sys->has_first_frame;
492 // This will lock gl.
493 vout_display_opengl_Display( vd->sys->vgl, &vd->source );
496 glClear(GL_COLOR_BUFFER_BIT);
500 * Method called by Cocoa when the view is resized.
504 VLCAssertMainThread();
506 NSRect bounds = [self bounds];
508 CGFloat height = bounds.size.height;
509 CGFloat width = bounds.size.width;
511 GLint x = width, y = height;
513 @synchronized(self) {
515 CGFloat videoHeight = vd->source.i_visible_height;
516 CGFloat videoWidth = vd->source.i_visible_width;
518 GLint sarNum = vd->source.i_sar_num;
519 GLint sarDen = vd->source.i_sar_den;
521 if (height * videoWidth * sarNum < width * videoHeight * sarDen)
523 x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
529 y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
535 glViewport((width - x) / 2, (height - y) / 2, x, y);
537 @synchronized(self) {
538 // This may be cleared before -drawRect is being called,
539 // in this case we'll skip the rendering.
540 // This will save us for rendering two frames (or more) for nothing
541 // (one by the vout, one (or more) by drawRect)
542 _hasPendingReshape = YES;
552 * Method called by Cocoa when the view is resized or the location has changed.
553 * We just need to make sure we are locking here.
557 VLCAssertMainThread();
558 BOOL success = [self lockgl];
568 * Method called by Cocoa to force redraw.
570 - (void)drawRect:(NSRect) rect
572 VLCAssertMainThread();
574 if ([self canSkipRendering])
577 BOOL success = [self lockgl];
588 NSWindow *window = [self window];
590 // Remove flashes with splitter view.
591 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
592 [window disableScreenUpdatesUntilFlush];
597 - (BOOL)mouseDownCanMoveWindow