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")
76 add_shortcut("vout_macosx")
80 * Obj-C protocol declaration that drawable-nsobject should follow
82 @protocol VLCOpenGLVideoViewEmbedding <NSObject>
83 - (void)addVoutSubview:(NSView *)view;
84 - (void)removeVoutSubview:(NSView *)view;
87 @interface VLCOpenGLVideoView : NSOpenGLView
91 - (void)setVoutDisplay:(vout_display_t *)vd;
95 struct vout_display_sys_t
97 VLCOpenGLVideoView *glView;
98 id<VLCOpenGLVideoViewEmbedding> container;
101 vout_display_opengl_t vgl;
103 picture_pool_t *pool;
105 bool has_first_frame;
108 static int Open(vlc_object_t *this)
110 vout_display_t *vd = (vout_display_t *)this;
111 vout_display_sys_t *sys = calloc(1, sizeof(*sys));
112 NSAutoreleasePool *nsPool = nil;
121 /* Get the drawable object */
122 id container = var_CreateGetAddress(vd, "drawable-nsobject");
125 msg_Dbg(vd, "No drawable-nsobject, passing over.");
129 /* This will be released in Close(), on
130 * main thread, after we are done using it. */
131 sys->container = [container retain];
133 /* Get our main view*/
134 nsPool = [[NSAutoreleasePool alloc] init];
136 [VLCOpenGLVideoView performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->glView] waitUntilDone:YES];
140 [sys->glView setVoutDisplay:vd];
142 /* We don't wait, that means that we'll have to be careful about releasing
144 * That's why we'll release on main thread in Close(). */
145 [(id)container performSelectorOnMainThread:@selector(addVoutSubview:) withObject:sys->glView waitUntilDone:NO];
149 /* Initialize common OpenGL video display */
150 sys->gl.lock = OpenglLock;
151 sys->gl.unlock = OpenglUnlock;
152 sys->gl.swap = OpenglSwap;
155 if (vout_display_opengl_Init(&sys->vgl, &vd->fmt, &sys->gl))
162 vout_display_info_t info = vd->info;
163 info.has_pictures_invalid = false;
165 /* Setup vout_display_t once everything is fine */
169 vd->prepare = PictureRender;
170 vd->display = PictureDisplay;
171 vd->control = Control;
174 vout_display_SendEventFullscreen (vd, false);
175 vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
185 void Close(vlc_object_t *this)
187 vout_display_t *vd = (vout_display_t *)this;
188 vout_display_sys_t *sys = vd->sys;
190 [sys->glView setVoutDisplay:nil];
192 var_Destroy(vd, "drawable-nsobject");
193 /* This will retain sys->glView */
194 [(id)sys->container performSelectorOnMainThread:@selector(removeVoutSubview:) withObject:sys->glView waitUntilDone:NO];
195 /* release on main thread as explained in Open() */
196 [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
197 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
199 [sys->glView release];
201 if (sys->gl.sys != NULL)
202 vout_display_opengl_Clean(&sys->vgl);
207 /*****************************************************************************
208 * vout display callbacks
209 *****************************************************************************/
211 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
213 vout_display_sys_t *sys = vd->sys;
214 VLC_UNUSED(requested_count);
217 sys->pool = vout_display_opengl_GetPool (&sys->vgl);
222 static void PictureRender(vout_display_t *vd, picture_t *pic)
225 vout_display_sys_t *sys = vd->sys;
227 vout_display_opengl_Prepare( &sys->vgl, pic );
230 static void PictureDisplay(vout_display_t *vd, picture_t *pic)
232 vout_display_sys_t *sys = vd->sys;
233 vout_display_opengl_Display(&sys->vgl, &vd->fmt );
234 picture_Release (pic);
235 sys->has_first_frame = true;
238 static int Control (vout_display_t *vd, int query, va_list ap)
240 vout_display_sys_t *sys = vd->sys;
244 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
245 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
246 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
247 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
248 case VOUT_DISPLAY_CHANGE_ZOOM:
249 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
250 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
255 case VOUT_DISPLAY_HIDE_MOUSE:
258 case VOUT_DISPLAY_GET_OPENGL:
260 vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
265 case VOUT_DISPLAY_RESET_PICTURES:
268 msg_Err (vd, "Unknown request in Mac OS X vout display");
271 printf("query %d\n", query);
274 /*****************************************************************************
275 * vout opengl callbacks
276 *****************************************************************************/
277 static int OpenglLock(vout_opengl_t *gl)
279 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
280 NSOpenGLContext *context = [sys->glView openGLContext];
281 CGLError err = CGLLockContext([context CGLContextObj]);
282 if (kCGLNoError == err)
284 [context makeCurrentContext];
290 static void OpenglUnlock(vout_opengl_t *gl)
292 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
293 CGLUnlockContext([[sys->glView openGLContext] CGLContextObj]);
296 static void OpenglSwap(vout_opengl_t *gl)
298 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
299 [[sys->glView openGLContext] flushBuffer];
302 /*****************************************************************************
304 *****************************************************************************/
305 @implementation VLCOpenGLVideoView
307 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
309 + (void)getNewView:(NSValue *)value
311 id *ret = [value pointerValue];
312 *ret = [[self alloc] init];
316 * Gets called by the Open() method.
320 VLCAssertMainThread();
322 /* Warning - this may be called on non main thread */
324 NSOpenGLPixelFormatAttribute attribs[] =
326 NSOpenGLPFADoubleBuffer,
327 NSOpenGLPFAAccelerated,
328 NSOpenGLPFANoRecovery,
329 NSOpenGLPFAColorSize, 24,
330 NSOpenGLPFAAlphaSize, 8,
331 NSOpenGLPFADepthSize, 24,
336 NSOpenGLPixelFormat *fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
341 self = [super initWithFrame:NSMakeRect(0,0,10,10) pixelFormat:fmt];
345 /* Swap buffers only during the vertical retrace of the monitor.
346 http://developer.apple.com/documentation/GraphicsImaging/
347 Conceptual/OpenGL/chap5/chapter_5_section_44.html */
348 GLint params[] = { 1 };
349 CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSwapInterval, params);
355 * Gets called by the Close and Open methods.
358 - (void)setVoutDisplay:(vout_display_t *)aVd
360 @synchronized(self) {
366 * Local method that locks the gl context.
370 VLCAssertMainThread();
371 CGLError err = CGLLockContext([[self openGLContext] CGLContextObj]);
372 return err == kCGLNoError;
376 * Local method that unlocks the gl context.
380 VLCAssertMainThread();
381 CGLUnlockContext([[self openGLContext] CGLContextObj]);
385 * Local method that force a rendering of a frame.
386 * This will get called if Cocoa forces us to redraw (via -drawRect).
390 VLCAssertMainThread();
392 @synchronized(self) { // vd can be accessed from multiple threads
393 if (vd && vd->sys->has_first_frame)
395 // This will lock gl.
396 vout_display_opengl_Display( &vd->sys->vgl, &vd->source );
399 glClear(GL_COLOR_BUFFER_BIT);
406 * Method called by Cocoa when the view is resized.
410 VLCAssertMainThread();
412 NSRect bounds = [self bounds];
414 CGFloat height = bounds.size.height;
415 CGFloat width = bounds.size.width;
417 GLint x = width, y = height;
419 @synchronized(self) {
421 CGFloat videoHeight = vd->source.i_visible_height;
422 CGFloat videoWidth = vd->source.i_visible_width;
424 GLint sarNum = vd->source.i_sar_num;
425 GLint sarDen = vd->source.i_sar_den;
427 if (height * videoWidth * sarNum < width * videoHeight * sarDen)
429 x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
435 y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
441 glClearColor(0, 0, 0, 1);
442 glViewport((width - x) / 2, (height - y) / 2, x, y);
449 * Method called by Cocoa when the view is resized or the location has changed.
450 * We just need to make sure we are locking here.
454 VLCAssertMainThread();
455 BOOL success = [self lockgl];
465 * Method called by Cocoa to force redraw.
467 - (void)drawRect:(NSRect) rect
469 VLCAssertMainThread();
470 BOOL success = [self lockgl];
479 - (BOOL)mouseDownCanMoveWindow