1 /*****************************************************************************
2 * caopengllayer.m: CAOpenGLLayer (Mac OS X) video output
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
7 * Authors: David Fuhrmann <david dot fuhrmann at googlemail dot com>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * Pierre d'Herbemont <pdherbemont at videolan dot org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_opengl.h>
39 #import <QuartzCore/QuartzCore.h>
40 #import <Cocoa/Cocoa.h>
41 #import <OpenGL/OpenGL.h>
42 #import <dlfcn.h> /* dlsym */
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open (vlc_object_t *);
50 static void Close (vlc_object_t *);
53 set_description(N_("Core Animation OpenGL Layer (Mac OS X)"))
54 set_capability("vout display", 0)
55 set_category(CAT_VIDEO)
56 set_subcategory(SUBCAT_VIDEO_VOUT)
57 set_callbacks(Open, Close)
61 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
62 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
63 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
64 static int Control (vout_display_t *vd, int query, va_list ap);
66 static void *OurGetProcAddress (vlc_gl_t *gl, const char *name);
67 static int OpenglLock (vlc_gl_t *gl);
68 static void OpenglUnlock (vlc_gl_t *gl);
69 static void OpenglSwap (vlc_gl_t *gl);
71 @protocol VLCCoreAnimationVideoLayerEmbedding <NSObject>
72 - (void)addVoutLayer:(CALayer *)aLayer;
73 - (void)removeVoutLayer:(CALayer *)aLayer;
74 - (CGSize)currentOutputSize;
77 @interface VLCCAOpenGLLayer : CAOpenGLLayer {
81 - (void)setVoutDisplay:(vout_display_t *)aVd;
85 struct vout_display_sys_t {
88 picture_resource_t resource;
90 CALayer <VLCCoreAnimationVideoLayerEmbedding> *container;
92 VLCCAOpenGLLayer *cgLayer;
94 CGLContextObj glContext;
97 vout_display_opengl_t *vgl;
99 vout_display_place_t place;
101 bool b_frame_available;
104 /*****************************************************************************
105 * Open: This function allocates and initializes the OpenGL vout method.
106 *****************************************************************************/
107 static int Open (vlc_object_t *p_this)
109 vout_display_t *vd = (vout_display_t *)p_this;
110 vout_display_sys_t *sys;
112 /* Allocate structure */
113 vd->sys = sys = calloc(1, sizeof(vout_display_sys_t));
117 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
119 id container = var_CreateGetAddress(vd, "drawable-nsobject");
121 vout_display_DeleteWindow(vd, NULL);
123 vout_window_cfg_t wnd_cfg;
125 memset(&wnd_cfg, 0, sizeof(wnd_cfg));
126 wnd_cfg.type = VOUT_WINDOW_TYPE_NSOBJECT;
127 wnd_cfg.x = var_InheritInteger(vd, "video-x");
128 wnd_cfg.y = var_InheritInteger(vd, "video-y");
129 wnd_cfg.height = vd->cfg->display.height;
130 wnd_cfg.width = vd->cfg->display.width;
132 sys->embed = vout_display_NewWindow(vd, &wnd_cfg);
134 container = sys->embed->handle.nsobject;
137 msg_Err(vd, "No drawable-nsobject found!");
142 /* store for later, released in Close() */
143 sys->container = [container retain];
145 [VLCCAOpenGLLayer performSelectorOnMainThread:@selector(getNewView:) withObject:[NSValue valueWithPointer:&sys->cgLayer] waitUntilDone:YES];
149 [sys->cgLayer setVoutDisplay:vd];
151 // TODO: Find a better way to wait until we get a context
152 [sys->cgLayer performSelectorOnMainThread:@selector(display)
153 withObject:nil waitUntilDone:YES];
154 assert(sys->glContext);
156 if ([container respondsToSelector:@selector(addVoutLayer:)]) {
157 msg_Dbg(vd, "container implements implicit protocol");
158 [container addVoutLayer:sys->cgLayer];
159 } else if ([container respondsToSelector:@selector(addSublayer:)] || [container isKindOfClass:[CALayer class]]) {
160 msg_Dbg(vd, "container doesn't implement implicit protocol, fallback mode used");
161 [container addSublayer:sys->cgLayer];
163 msg_Err(vd, "Provided NSObject container isn't compatible");
167 /* Initialize common OpenGL video display */
168 sys->gl.lock = OpenglLock;
169 sys->gl.unlock = OpenglUnlock;
170 sys->gl.swap = OpenglSwap;
171 sys->gl.getProcAddress = OurGetProcAddress;
174 const vlc_fourcc_t *subpicture_chromas;
175 video_format_t fmt = vd->fmt;
176 sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas, &sys->gl);
178 msg_Err(vd, "Error while initializing opengl display.");
183 /* setup vout display */
184 vout_display_info_t info = vd->info;
185 info.subpicture_chromas = subpicture_chromas;
186 info.has_hide_mouse = false;
190 vd->prepare = PictureRender;
191 vd->display = PictureDisplay;
192 vd->control = Control;
194 /* setup initial state */
196 if ([container respondsToSelector:@selector(currentOutputSize)])
197 outputSize = [container currentOutputSize];
199 outputSize = [sys->container visibleRect].size;
200 vout_display_SendEventFullscreen(vd, false);
201 vout_display_SendEventDisplaySize(vd, (int)outputSize.width, (int)outputSize.height, false);
213 static void Close (vlc_object_t *p_this)
215 vout_display_t *vd = (vout_display_t *)p_this;
216 vout_display_sys_t *sys = vd->sys;
219 if ([sys->container respondsToSelector:@selector(removeVoutLayer:)])
220 [sys->container removeVoutLayer:sys->cgLayer];
222 [sys->cgLayer removeFromSuperlayer];
223 [sys->cgLayer release];
227 [sys->container release];
230 vout_display_DeleteWindow(vd, sys->embed);
232 if (sys->gl.sys != NULL)
233 vout_display_opengl_Delete(sys->vgl);
236 CGLReleaseContext(sys->glContext);
241 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
243 vout_display_sys_t *sys = vd->sys;
246 sys->pool = vout_display_opengl_GetPool(sys->vgl, count);
251 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
253 vout_display_sys_t *sys = vd->sys;
255 @synchronized (sys->cgLayer) {
256 vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
260 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
262 vout_display_sys_t *sys = vd->sys;
264 @synchronized (sys->cgLayer) {
265 sys->b_frame_available = YES;
267 /* Calling display on the non-main thread is not officially supported, but
268 * its suggested at several places and works fine here. Flush is thread-safe
269 * and makes sure the picture is actually displayed. */
270 [sys->cgLayer display];
271 [CATransaction flush];
272 sys->b_frame_available = NO;
275 picture_Release(pic);
278 subpicture_Delete(subpicture);
281 static int Control (vout_display_t *vd, int query, va_list ap)
283 vout_display_sys_t *sys = vd->sys;
290 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
291 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
292 case VOUT_DISPLAY_CHANGE_ZOOM:
293 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
294 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
295 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
297 const vout_display_cfg_t *cfg;
298 const video_format_t *source;
300 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
301 source = (const video_format_t *)va_arg (ap, const video_format_t *);
304 source = &vd->source;
305 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
308 /* we always use our current frame here */
309 vout_display_cfg_t cfg_tmp = *cfg;
310 CGRect bounds = [sys->cgLayer bounds];
311 cfg_tmp.display.width = bounds.size.width;
312 cfg_tmp.display.height = bounds.size.height;
314 vout_display_place_t place;
315 vout_display_PlacePicture (&place, source, &cfg_tmp, false);
321 case VOUT_DISPLAY_GET_OPENGL:
323 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
328 case VOUT_DISPLAY_RESET_PICTURES:
331 msg_Err (vd, "Unhandled request %d", query);
339 #pragma mark OpenGL callbacks
341 static int OpenglLock (vlc_gl_t *gl)
343 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
345 if(!sys->glContext) {
349 CGLError err = CGLLockContext(sys->glContext);
350 if (kCGLNoError == err) {
351 CGLSetCurrentContext(sys->glContext);
357 static void OpenglUnlock (vlc_gl_t *gl)
359 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
361 if (!sys->glContext) {
365 CGLUnlockContext(sys->glContext);
368 static void OpenglSwap (vlc_gl_t *gl)
373 static void *OurGetProcAddress (vlc_gl_t *gl, const char *name)
377 return dlsym(RTLD_DEFAULT, name);
381 #pragma mark CA layer
383 /*****************************************************************************
384 * @implementation VLCCAOpenGLLayer
385 *****************************************************************************/
386 @implementation VLCCAOpenGLLayer
388 + (void)getNewView:(NSValue *)value
390 id *ret = [value pointerValue];
391 *ret = [[self alloc] init];
398 [CATransaction begin];
399 [self setAutoresizingMask: kCALayerWidthSizable | kCALayerHeightSizable];
400 [self setAsynchronous: NO];
401 [CATransaction commit];
407 - (void)setVoutDisplay:(vout_display_t *)aVd
412 - (void)resizeWithOldSuperlayerSize:(CGSize)size
414 [super resizeWithOldSuperlayerSize: size];
417 vout_display_SendEventDisplaySize(_vd, self.bounds.size.width, self.bounds.size.height, _vd->cfg->is_fullscreen);
420 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
422 /* Only draw the frame if we have a frame that was previously rendered */
426 return _vd->sys->b_frame_available;
429 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
433 vout_display_sys_t *sys = _vd->sys;
438 CGRect bounds = [self bounds];
439 // x / y are top left corner, but we need the lower left one
440 glViewport (sys->place.x, bounds.size.height - (sys->place.y + sys->place.height), sys->place.width, sys->place.height);
442 // flush is also done by this method, no need to call super
443 vout_display_opengl_Display (sys->vgl, &_vd->source);
446 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
448 // Only one opengl context is allowed for the module lifetime
449 if(_vd->sys->glContext) {
450 msg_Dbg(_vd, "Return existing context: %p", _vd->sys->glContext);
451 return _vd->sys->glContext;
454 CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
456 // Swap buffers only during the vertical retrace of the monitor.
457 //http://developer.apple.com/documentation/GraphicsImaging/
458 //Conceptual/OpenGL/chap5/chapter_5_section_44.html /
461 CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
464 @synchronized (self) {
465 _vd->sys->glContext = context;
471 - (void)releaseCGLContext:(CGLContextObj)glContext
473 // do not release anything here, we do that when closing the module