]> git.sesse.net Git - vlc/blob - modules/video_output/caopengllayer.m
caopengllayer: add implicit API to handle mouse events
[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         vout_window_cfg_t wnd_cfg;
124
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;
131
132         sys->embed = vout_display_NewWindow(vd, &wnd_cfg);
133         if (sys->embed)
134             container = sys->embed->handle.nsobject;
135
136         if (!container) {
137             msg_Err(vd, "No drawable-nsobject found!");
138             goto bailout;
139         }
140     }
141
142     /* store for later, released in Close() */
143     sys->container = [container retain];
144
145         dispatch_sync(dispatch_get_main_queue(), ^{
146                 [CATransaction begin];
147                 sys->cgLayer = [[VLCCAOpenGLLayer alloc] init];
148                 [sys->cgLayer setVoutDisplay:vd];
149                 [sys->cgLayer display];         // TODO: Find a better way to wait until we get a context
150             if ([container respondsToSelector:@selector(addVoutLayer:)]) {
151                 msg_Dbg(vd, "container implements implicit protocol");
152                 [container addVoutLayer:sys->cgLayer];
153             } else if ([container respondsToSelector:@selector(addSublayer:)] || [container isKindOfClass:[CALayer class]]) {
154                 msg_Dbg(vd, "container doesn't implement implicit protocol, fallback mode used");
155                 [container addSublayer:sys->cgLayer];
156             } else {
157                 msg_Err(vd, "Provided NSObject container isn't compatible");
158                         [sys->cgLayer release];
159                         sys->cgLayer = nil;
160             }
161                 [CATransaction commit];
162         });
163
164     if (!sys->cgLayer)
165         goto bailout;
166
167     if (!sys->glContext) {
168         msg_Err(vd, "Have no gl context");
169         goto bailout;
170     }
171
172     /* Initialize common OpenGL video display */
173     sys->gl.lock = OpenglLock;
174     sys->gl.unlock = OpenglUnlock;
175     sys->gl.swap = OpenglSwap;
176     sys->gl.getProcAddress = OurGetProcAddress;
177     sys->gl.sys = sys;
178
179     const vlc_fourcc_t *subpicture_chromas;
180     video_format_t fmt = vd->fmt;
181     sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas, &sys->gl);
182     if (!sys->vgl) {
183         msg_Err(vd, "Error while initializing opengl display.");
184         sys->gl.sys = NULL;
185         goto bailout;
186     }
187
188     /* setup vout display */
189     vout_display_info_t info = vd->info;
190     info.subpicture_chromas = subpicture_chromas;
191     info.has_hide_mouse = false;
192     vd->info = info;
193
194     vd->pool    = Pool;
195     vd->prepare = PictureRender;
196     vd->display = PictureDisplay;
197     vd->control = Control;
198
199     /* setup initial state */
200     CGSize outputSize;
201     if ([container respondsToSelector:@selector(currentOutputSize)])
202         outputSize = [container currentOutputSize];
203     else
204         outputSize = [sys->container visibleRect].size;
205     vout_display_SendEventFullscreen(vd, false);
206     vout_display_SendEventDisplaySize(vd, (int)outputSize.width, (int)outputSize.height, false);
207
208     [pool release];
209     return VLC_SUCCESS;
210
211 bailout:
212     [pool release];
213     Close(p_this);
214     return VLC_EGENERIC;
215 }
216
217 static void Close (vlc_object_t *p_this)
218 {
219     vout_display_t *vd = (vout_display_t *)p_this;
220     vout_display_sys_t *sys = vd->sys;
221
222     if (sys->cgLayer) {
223         if ([sys->container respondsToSelector:@selector(removeVoutLayer:)])
224             [sys->container removeVoutLayer:sys->cgLayer];
225         else
226             [sys->cgLayer removeFromSuperlayer];
227         [sys->cgLayer release];
228     }
229
230     if (sys->container)
231         [sys->container release];
232
233     if (sys->embed)
234         vout_display_DeleteWindow(vd, sys->embed);
235
236     if (sys->gl.sys != NULL)
237         vout_display_opengl_Delete(sys->vgl);
238
239     if (sys->glContext)
240         CGLReleaseContext(sys->glContext);
241
242     free(sys);
243 }
244
245 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
246 {
247     vout_display_sys_t *sys = vd->sys;
248
249     if (!sys->pool)
250         sys->pool = vout_display_opengl_GetPool(sys->vgl, count);
251     assert(sys->pool);
252     return sys->pool;
253 }
254
255 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
256 {
257     vout_display_sys_t *sys = vd->sys;
258
259     @synchronized (sys->cgLayer) {
260         vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
261     }
262 }
263
264 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
265 {
266     vout_display_sys_t *sys = vd->sys;
267
268     @synchronized (sys->cgLayer) {
269         sys->b_frame_available = YES;
270
271         /* Calling display on the non-main thread is not officially supported, but
272          * its suggested at several places and works fine here. Flush is thread-safe
273          * and makes sure the picture is actually displayed. */
274         [sys->cgLayer display];
275         [CATransaction flush];
276     }
277
278     picture_Release(pic);
279
280     if (subpicture)
281         subpicture_Delete(subpicture);
282 }
283
284 static int Control (vout_display_t *vd, int query, va_list ap)
285 {
286     vout_display_sys_t *sys = vd->sys;
287
288     if (!vd->sys)
289         return VLC_EGENERIC;
290
291     switch (query)
292     {
293         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
294         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
295         case VOUT_DISPLAY_CHANGE_ZOOM:
296         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
297         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
298         case VOUT_DISPLAY_CHANGE_FULLSCREEN:
299         {
300             const vout_display_cfg_t *cfg;
301             const video_format_t *source;
302
303             if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
304                 source = (const video_format_t *)va_arg (ap, const video_format_t *);
305                 cfg = vd->cfg;
306             } else {
307                 source = &vd->source;
308                 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
309             }
310
311             /* we always use our current frame here */
312             vout_display_cfg_t cfg_tmp = *cfg;
313             [CATransaction lock];
314             CGRect bounds = [sys->cgLayer bounds];
315             [CATransaction unlock];
316             cfg_tmp.display.width = bounds.size.width;
317             cfg_tmp.display.height = bounds.size.height;
318
319             vout_display_place_t place;
320             vout_display_PlacePicture (&place, source, &cfg_tmp, false);
321             sys->place = place;
322
323             return VLC_SUCCESS;
324         }
325
326         case VOUT_DISPLAY_GET_OPENGL:
327         {
328             vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
329             *gl = &sys->gl;
330             return VLC_SUCCESS;
331         }
332
333         case VOUT_DISPLAY_RESET_PICTURES:
334             assert (0);
335         default:
336             msg_Err (vd, "Unhandled request %d", query);
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 setAutoresizingMask: kCALayerWidthSizable | kCALayerHeightSizable];
399         self.asynchronous = NO;
400         [CATransaction unlock];
401     }
402
403     return self;
404 }
405
406 - (void)setVoutDisplay:(vout_display_t *)aVd
407 {
408     _vd = aVd;
409 }
410
411 - (void)resizeWithOldSuperlayerSize:(CGSize)size
412 {
413     [super resizeWithOldSuperlayerSize: size];
414
415     CGRect bounds = self.bounds;
416     if (_vd)
417         vout_display_SendEventDisplaySize(_vd, bounds.size.width, bounds.size.height, _vd->cfg->is_fullscreen);
418 }
419
420 - (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
421 {
422     /* Only draw the frame if we have a frame that was previously rendered */
423     if (!_vd)
424         return false;
425
426     return _vd->sys->b_frame_available;
427 }
428
429 - (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
430 {
431     if (!_vd)
432         return;
433     vout_display_sys_t *sys = _vd->sys;
434
435     if (!sys->vgl)
436         return;
437
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);
441
442     // flush is also done by this method, no need to call super
443     vout_display_opengl_Display (sys->vgl, &_vd->source);
444     sys->b_frame_available = NO;
445 }
446
447 - (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
448 {
449     // Only one opengl context is allowed for the module lifetime
450     if(_vd->sys->glContext) {
451         msg_Dbg(_vd, "Return existing context: %p", _vd->sys->glContext);
452         return _vd->sys->glContext;
453     }
454
455     CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];
456
457     // Swap buffers only during the vertical retrace of the monitor.
458     //http://developer.apple.com/documentation/GraphicsImaging/
459     //Conceptual/OpenGL/chap5/chapter_5_section_44.html /
460
461     GLint params = 1;
462     CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
463                      &params );
464
465     @synchronized (self) {
466         _vd->sys->glContext = context;
467     }
468
469     return context;
470 }
471
472 - (void)releaseCGLContext:(CGLContextObj)glContext
473 {
474     // do not release anything here, we do that when closing the module
475 }
476
477 - (void)mouseButtonDown:(int)buttonNumber
478 {
479     @synchronized (self) {
480         if (_vd) {
481             if (buttonNumber == 0)
482                 vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_LEFT);
483             else if (buttonNumber == 1)
484                 vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_RIGHT);
485             else
486                 vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_CENTER);
487         }
488     }
489 }
490
491 - (void)mouseButtonUp:(int)buttonNumber
492 {
493     @synchronized (self) {
494         if (_vd) {
495             if (buttonNumber == 0)
496                 vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_LEFT);
497             else if (buttonNumber == 1)
498                 vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_RIGHT);
499             else
500                 vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_CENTER);
501         }
502     }
503 }
504
505 - (void)mouseMovedToX:(double)xValue Y:(double)yValue
506 {
507     @synchronized (self) {
508         if (_vd) {
509             vout_display_SendMouseMovedDisplayCoordinates (_vd,
510                                                            ORIENT_NORMAL,
511                                                            xValue,
512                                                            yValue,
513                                                            &_vd->sys->place);
514         }
515     }
516 }
517
518 @end