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;
260 sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
265 static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
268 vout_display_sys_t *sys = vd->sys;
270 vout_display_opengl_Prepare( sys->vgl, pic, subpicture );
273 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
275 vout_display_sys_t *sys = vd->sys;
276 [sys->glView setVoutFlushing:YES];
277 vout_display_opengl_Display(sys->vgl, &vd->fmt );
278 [sys->glView setVoutFlushing:NO];
279 picture_Release (pic);
280 sys->has_first_frame = true;
284 static int Control (vout_display_t *vd, int query, va_list ap)
286 vout_display_sys_t *sys = vd->sys;
290 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
291 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
292 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
293 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
294 case VOUT_DISPLAY_CHANGE_ZOOM:
295 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
296 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
301 case VOUT_DISPLAY_HIDE_MOUSE:
304 case VOUT_DISPLAY_GET_OPENGL:
306 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
311 case VOUT_DISPLAY_RESET_PICTURES:
314 msg_Err (vd, "Unknown request in Mac OS X vout display");
319 /*****************************************************************************
320 * vout opengl callbacks
321 *****************************************************************************/
322 static int OpenglLock(vlc_gl_t *gl)
324 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
325 NSOpenGLContext *context = [sys->glView openGLContext];
326 CGLError err = CGLLockContext([context CGLContextObj]);
327 if (kCGLNoError == err)
329 [context makeCurrentContext];
335 static void OpenglUnlock(vlc_gl_t *gl)
337 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
338 CGLUnlockContext([[sys->glView openGLContext] CGLContextObj]);
341 static void OpenglSwap(vlc_gl_t *gl)
343 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
344 [[sys->glView openGLContext] flushBuffer];
347 /*****************************************************************************
349 *****************************************************************************/
350 @implementation VLCOpenGLVideoView
352 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
354 + (void)getNewView:(NSValue *)value
356 id *ret = [value pointerValue];
357 *ret = [[self alloc] init];
361 * Gets called by the Open() method.
365 VLCAssertMainThread();
367 /* Warning - this may be called on non main thread */
369 NSOpenGLPixelFormatAttribute attribs[] =
371 NSOpenGLPFADoubleBuffer,
372 NSOpenGLPFAAccelerated,
373 NSOpenGLPFANoRecovery,
374 NSOpenGLPFAColorSize, 24,
375 NSOpenGLPFAAlphaSize, 8,
376 NSOpenGLPFADepthSize, 24,
381 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
386 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
392 /* Swap buffers only during the vertical retrace of the monitor.
393 http://developer.apple.com/documentation/GraphicsImaging/
394 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
395 GLint params[] = { 1 };
396 CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
398 [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
403 * Gets called by the Open() method.
405 - (void)setFrameWithValue:(NSValue *)value
407 [self setFrame:[value rectValue]];
411 * Gets called by the Close and Open methods.
414 - (void)setVoutDisplay:(vout_display_t *)aVd
416 @synchronized(self) {
423 * Gets called when the vout will aquire the lock and flush.
426 - (void)setVoutFlushing:(BOOL)flushing
430 @synchronized(self) {
431 _hasPendingReshape = NO;
436 * Can -drawRect skip rendering?.
438 - (BOOL)canSkipRendering
440 VLCAssertMainThread();
442 @synchronized(self) {
443 BOOL hasFirstFrame = vd && vd->sys->has_first_frame;
444 return !_hasPendingReshape && hasFirstFrame;
450 * Local method that locks the gl context.
454 VLCAssertMainThread();
455 NSOpenGLContext *context = [self openGLContext];
456 CGLError err = CGLLockContext([context CGLContextObj]);
457 if (err == kCGLNoError)
458 [context makeCurrentContext];
459 return err == kCGLNoError;
463 * Local method that unlocks the gl context.
467 VLCAssertMainThread();
468 CGLUnlockContext([[self openGLContext] CGLContextObj]);
472 * Local method that force a rendering of a frame.
473 * This will get called if Cocoa forces us to redraw (via -drawRect).
477 VLCAssertMainThread();
479 // We may have taken some times to take the opengl Lock.
480 // Check here to see if we can just skip the frame as well.
481 if ([self canSkipRendering])
485 @synchronized(self) { // vd can be accessed from multiple threads
486 hasFirstFrame = vd && vd->sys->has_first_frame;
490 // This will lock gl.
491 vout_display_opengl_Display( vd->sys->vgl, &vd->source );
494 glClear(GL_COLOR_BUFFER_BIT);
498 * Method called by Cocoa when the view is resized.
502 VLCAssertMainThread();
504 NSRect bounds = [self bounds];
506 CGFloat height = bounds.size.height;
507 CGFloat width = bounds.size.width;
509 GLint x = width, y = height;
511 @synchronized(self) {
513 CGFloat videoHeight = vd->source.i_visible_height;
514 CGFloat videoWidth = vd->source.i_visible_width;
516 GLint sarNum = vd->source.i_sar_num;
517 GLint sarDen = vd->source.i_sar_den;
519 if (height * videoWidth * sarNum < width * videoHeight * sarDen)
521 x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
527 y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
533 glViewport((width - x) / 2, (height - y) / 2, x, y);
535 @synchronized(self) {
536 // This may be cleared before -drawRect is being called,
537 // in this case we'll skip the rendering.
538 // This will save us for rendering two frames (or more) for nothing
539 // (one by the vout, one (or more) by drawRect)
540 _hasPendingReshape = YES;
550 * Method called by Cocoa when the view is resized or the location has changed.
551 * We just need to make sure we are locking here.
555 VLCAssertMainThread();
556 BOOL success = [self lockgl];
566 * Method called by Cocoa to force redraw.
568 - (void)drawRect:(NSRect) rect
570 VLCAssertMainThread();
572 if ([self canSkipRendering])
575 BOOL success = [self lockgl];
586 NSWindow *window = [self window];
588 // Remove flashes with splitter view.
589 if ([window respondsToSelector:@selector(disableScreenUpdatesUntilFlush)])
590 [window disableScreenUpdatesUntilFlush];
595 - (BOOL)mouseDownCanMoveWindow