]> git.sesse.net Git - vlc/blob - modules/video_output/caopengllayer.m
decoder: fix data race in input_DecoderFrameNext()
[vlc] / modules / video_output / caopengllayer.m
1 /*****************************************************************************
2  * caopengllayer.m: CAOpenGLLayer (Mac OS X) video output
3  *****************************************************************************
4  * Copyright (C) 2014 VLC authors and VideoLAN
5  * $Id$
6  *
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>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_opengl.h>
38
39 #import <QuartzCore/QuartzCore.h>
40 #import <Cocoa/Cocoa.h>
41 #import <OpenGL/OpenGL.h>
42 #import <dlfcn.h>               /* dlsym */
43
44 #include "opengl.h"
45
46 /*****************************************************************************
47  * Vout interface
48  *****************************************************************************/
49 static int  Open   (vlc_object_t *);
50 static void Close  (vlc_object_t *);
51
52 vlc_module_begin()
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)
58 vlc_module_end()
59
60
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);
65
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);
70
71 @protocol VLCCoreAnimationVideoLayerEmbedding <NSObject>
72 - (void)addVoutLayer:(CALayer *)aLayer;
73 - (void)removeVoutLayer:(CALayer *)aLayer;
74 - (CGSize)currentOutputSize;
75 @end
76
77 @interface VLCCAOpenGLLayer : CAOpenGLLayer {
78     vout_display_t *_vd;
79 }
80
81 - (void)setVoutDisplay:(vout_display_t *)aVd;
82 @end
83
84
85 struct vout_display_sys_t {
86
87     picture_pool_t *pool;
88     picture_resource_t resource;
89
90     CALayer <VLCCoreAnimationVideoLayerEmbedding> *container;
91     vout_window_t *embed;
92     VLCCAOpenGLLayer *cgLayer;
93
94     CGLContextObj glContext;
95
96     vlc_gl_t gl;
97     vout_display_opengl_t *vgl;
98
99     vout_display_place_t place;
100
101     bool  b_frame_available;
102 };
103
104 /*****************************************************************************
105  * Open: This function allocates and initializes the OpenGL vout method.
106  *****************************************************************************/
107 static int Open (vlc_object_t *p_this)
108 {
109     vout_display_t *vd = (vout_display_t *)p_this;
110     vout_display_sys_t *sys;
111
112     /* Allocate structure */
113     vd->sys = sys = calloc(1, sizeof(vout_display_sys_t));
114     if (sys == NULL)
115         return VLC_EGENERIC;
116
117     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
118
119     id container = var_CreateGetAddress(vd, "drawable-nsobject");
120     if (container)
121         vout_display_DeleteWindow(vd, NULL);
122     else {
123         sys->embed = vout_display_NewWindow(vd, VOUT_WINDOW_TYPE_NSOBJECT);
124         if (sys->embed)
125             container = sys->embed->handle.nsobject;
126
127         if (!container) {
128             msg_Err(vd, "No drawable-nsobject found!");
129             goto bailout;
130         }
131     }
132
133     /* store for later, released in Close() */
134     sys->container = [container retain];
135
136     [CATransaction begin];
137     sys->cgLayer = [[VLCCAOpenGLLayer alloc] init];
138     [sys->cgLayer setVoutDisplay:vd];
139
140     [sys->cgLayer performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:YES];
141
142     if ([container respondsToSelector:@selector(addVoutLayer:)]) {
143         msg_Dbg(vd, "container implements implicit protocol");
144         [container addVoutLayer:sys->cgLayer];
145     } else if ([container respondsToSelector:@selector(addSublayer:)] || [container isKindOfClass:[CALayer class]]) {
146         msg_Dbg(vd, "container doesn't implement implicit protocol, fallback mode used");
147         [container addSublayer:sys->cgLayer];
148     } else {
149         msg_Err(vd, "Provided NSObject container isn't compatible");
150         [sys->cgLayer release];
151         sys->cgLayer = nil;
152         [CATransaction commit];
153         goto bailout;
154     }
155     [CATransaction commit];
156
157     if (!sys->cgLayer)
158         goto bailout;
159
160     if (!sys->glContext)
161         msg_Warn(vd, "we might not have an OpenGL context yet");
162
163     /* Initialize common OpenGL video display */
164     sys->gl.lock = OpenglLock;
165     sys->gl.unlock = OpenglUnlock;
166     sys->gl.swap = OpenglSwap;
167     sys->gl.getProcAddress = OurGetProcAddress;
168     sys->gl.sys = sys;
169
170     const vlc_fourcc_t *subpicture_chromas;
171     video_format_t fmt = vd->fmt;
172     sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas, &sys->gl);
173     if (!sys->vgl) {
174         msg_Err(vd, "Error while initializing opengl display.");
175         sys->gl.sys = NULL;
176         goto bailout;
177     }
178
179     /* setup vout display */
180     vout_display_info_t info = vd->info;
181     info.subpicture_chromas = subpicture_chromas;
182     info.has_hide_mouse = true;
183     vd->info = info;
184
185     vd->pool    = Pool;
186     vd->prepare = PictureRender;
187     vd->display = PictureDisplay;
188     vd->control = Control;
189
190     /* setup initial state */
191     CGSize outputSize;
192     if ([container respondsToSelector:@selector(currentOutputSize)])
193         outputSize = [container currentOutputSize];
194     else
195         outputSize = [sys->container visibleRect].size;
196     vout_display_SendEventFullscreen(vd, false);
197     vout_display_SendEventDisplaySize(vd, (int)outputSize.width, (int)outputSize.height);
198
199     [pool release];
200     return VLC_SUCCESS;
201
202 bailout:
203     [pool release];
204     Close(p_this);
205     return VLC_EGENERIC;
206 }
207
208 static void Close (vlc_object_t *p_this)
209 {
210     vout_display_t *vd = (vout_display_t *)p_this;
211     vout_display_sys_t *sys = vd->sys;
212
213     if (sys->cgLayer) {
214         if ([sys->container respondsToSelector:@selector(removeVoutLayer:)])
215             [sys->container removeVoutLayer:sys->cgLayer];
216         else
217             [sys->cgLayer removeFromSuperlayer];
218         [sys->cgLayer release];
219     }
220
221     if (sys->container)
222         [sys->container release];
223
224     if (sys->embed)
225         vout_display_DeleteWindow(vd, sys->embed);
226
227     if (sys->gl.sys != NULL)
228         vout_display_opengl_Delete(sys->vgl);
229
230     if (sys->glContext)
231         CGLReleaseContext(sys->glContext);
232
233     free(sys);
234 }
235
236 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
237 {
238     vout_display_sys_t *sys = vd->sys;
239
240     if (!sys->pool)
241         sys->pool = vout_display_opengl_GetPool(sys->vgl, count);
242     assert(sys->pool);
243     return sys->pool;
244 }
245
246 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
247 {
248     vout_display_sys_t *sys = vd->sys;
249
250     if (pic == NULL) {
251         msg_Warn(vd, "invalid pic, skipping frame");
252         return;
253     }
254
255     @synchronized (sys->cgLayer) {
256         vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
257     }
258 }
259
260 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
261 {
262     vout_display_sys_t *sys = vd->sys;
263
264     @synchronized (sys->cgLayer) {
265         sys->b_frame_available = YES;
266
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     }
273
274     picture_Release(pic);
275
276     if (subpicture)
277         subpicture_Delete(subpicture);
278 }
279
280 static int Control (vout_display_t *vd, int query, va_list ap)
281 {
282     vout_display_sys_t *sys = vd->sys;
283
284     if (!vd->sys)
285         return VLC_EGENERIC;
286
287     switch (query)
288     {
289         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
290         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
291         case VOUT_DISPLAY_CHANGE_ZOOM:
292         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
293         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
294         {
295             const vout_display_cfg_t *cfg;
296             const video_format_t *source;
297
298             if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
299                 source = (const video_format_t *)va_arg (ap, const video_format_t *);
300                 cfg = vd->cfg;
301             } else {
302                 source = &vd->source;
303                 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
304             }
305
306             /* we always use our current frame here */
307             vout_display_cfg_t cfg_tmp = *cfg;
308             [CATransaction lock];
309             CGRect bounds = [sys->cgLayer visibleRect];
310             [CATransaction unlock];
311             cfg_tmp.display.width = bounds.size.width;
312             cfg_tmp.display.height = bounds.size.height;
313
314             vout_display_place_t place;
315             vout_display_PlacePicture (&place, source, &cfg_tmp, false);
316             sys->place = place;
317
318             return VLC_SUCCESS;
319         }
320
321         case VOUT_DISPLAY_HIDE_MOUSE:
322         {
323             [NSCursor setHiddenUntilMouseMoves: YES];
324             return VLC_SUCCESS;
325         }
326
327         case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
328         {
329             return VLC_SUCCESS;
330         }
331
332         case VOUT_DISPLAY_RESET_PICTURES:
333             vlc_assert_unreachable ();
334         default:
335             msg_Err (vd, "Unhandled request %d", query);
336         case VOUT_DISPLAY_CHANGE_FULLSCREEN:
337             return VLC_EGENERIC;
338     }
339
340     return VLC_SUCCESS;
341 }
342
343 #pragma mark -
344 #pragma mark OpenGL callbacks
345
346 static int OpenglLock (vlc_gl_t *gl)
347 {
348     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
349
350     if(!sys->glContext) {
351         return 1;
352     }
353
354     CGLError err = CGLLockContext(sys->glContext);
355     if (kCGLNoError == err) {
356         CGLSetCurrentContext(sys->glContext);
357         return 0;
358     }
359     return 1;
360 }
361
362 static void OpenglUnlock (vlc_gl_t *gl)
363 {
364     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
365
366     if (!sys->glContext) {
367         return;
368     }
369
370     CGLUnlockContext(sys->glContext);
371 }
372
373 static void OpenglSwap (vlc_gl_t *gl)
374 {
375     glFlush();
376 }
377
378 static void *OurGetProcAddress (vlc_gl_t *gl, const char *name)
379 {
380     VLC_UNUSED(gl);
381
382     return dlsym(RTLD_DEFAULT, name);
383 }
384
385 #pragma mark -
386 #pragma mark CA layer
387
388 /*****************************************************************************
389  * @implementation VLCCAOpenGLLayer
390  *****************************************************************************/
391 @implementation VLCCAOpenGLLayer
392
393 - (id)init {
394
395     self = [super init];
396     if (self) {
397         [CATransaction lock];
398         self.needsDisplayOnBoundsChange = YES;
399         self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
400         self.asynchronous = NO;
401         [CATransaction unlock];
402     }
403
404     return self;
405 }
406
407 - (void)setVoutDisplay:(vout_display_t *)aVd
408 {
409     _vd = aVd;
410 }
411
412 - (void)resizeWithOldSuperlayerSize:(CGSize)size
413 {
414     [super resizeWithOldSuperlayerSize: size];
415
416     CGSize boundsSize = self.visibleRect.size;
417
418     if (_vd)
419         vout_display_SendEventDisplaySize(_vd, boundsSize.width, boundsSize.height);
420 }
421
422 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
423 {
424     /* Only draw the frame if we have a frame that was previously rendered */
425     if (!_vd)
426         return false;
427
428     return _vd->sys->b_frame_available;
429 }
430
431 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
432 {
433     if (!_vd)
434         return;
435     vout_display_sys_t *sys = _vd->sys;
436
437     if (!sys->vgl)
438         return;
439
440     CGRect bounds = [self visibleRect];
441
442     // x / y are top left corner, but we need the lower left one
443     glViewport (sys->place.x, bounds.size.height - (sys->place.y + sys->place.height), sys->place.width, sys->place.height);
444
445     // flush is also done by this method, no need to call super
446     vout_display_opengl_Display (sys->vgl, &_vd->source);
447     sys->b_frame_available = NO;
448 }
449
450 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
451 {
452     // Only one opengl context is allowed for the module lifetime
453     if(_vd->sys->glContext) {
454         msg_Dbg(_vd, "Return existing context: %p", _vd->sys->glContext);
455         return _vd->sys->glContext;
456     }
457
458     CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
459
460     // Swap buffers only during the vertical retrace of the monitor.
461     //http://developer.apple.com/documentation/GraphicsImaging/
462     //Conceptual/OpenGL/chap5/chapter_5_section_44.html /
463
464     GLint params = 1;
465     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
466                      &params );
467
468     @synchronized (self) {
469         _vd->sys->glContext = context;
470     }
471
472     return context;
473 }
474
475 - (void)releaseCGLContext:(CGLContextObj)glContext
476 {
477     // do not release anything here, we do that when closing the module
478 }
479
480 - (void)mouseButtonDown:(int)buttonNumber
481 {
482     @synchronized (self) {
483         if (_vd) {
484             if (buttonNumber == 0)
485                 vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_LEFT);
486             else if (buttonNumber == 1)
487                 vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_RIGHT);
488             else
489                 vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_CENTER);
490         }
491     }
492 }
493
494 - (void)mouseButtonUp:(int)buttonNumber
495 {
496     @synchronized (self) {
497         if (_vd) {
498             if (buttonNumber == 0)
499                 vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_LEFT);
500             else if (buttonNumber == 1)
501                 vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_RIGHT);
502             else
503                 vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_CENTER);
504         }
505     }
506 }
507
508 - (void)mouseMovedToX:(double)xValue Y:(double)yValue
509 {
510     @synchronized (self) {
511         if (_vd) {
512             vout_display_SendMouseMovedDisplayCoordinates (_vd,
513                                                            ORIENT_NORMAL,
514                                                            xValue,
515                                                            yValue,
516                                                            &_vd->sys->place);
517         }
518     }
519 }
520
521 @end