caopengllayer.m 17.6 KB
Newer Older
1 2 3
/*****************************************************************************
 * caopengllayer.m: CAOpenGLLayer (Mac OS X) video output
 *****************************************************************************
4
 * Copyright (C) 2014-2017 VLC authors and VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 * $Id$
 *
 * Authors: David Fuhrmann <david dot fuhrmann at googlemail dot com>
 *          Felix Paul Kühne <fkuehne at videolan dot org>
 *          Pierre d'Herbemont <pdherbemont at videolan dot org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>
#include <vlc_opengl.h>

#import <QuartzCore/QuartzCore.h>
#import <Cocoa/Cocoa.h>
#import <OpenGL/OpenGL.h>
#import <dlfcn.h>               /* dlsym */

Thomas Guillem's avatar
Thomas Guillem committed
44
#include "opengl/vout_helper.h"
45

46
#define OSX_SIERRA_AND_HIGHER (NSAppKitVersionNumber >= 1485)
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*****************************************************************************
 * Vout interface
 *****************************************************************************/
static int  Open   (vlc_object_t *);
static void Close  (vlc_object_t *);

vlc_module_begin()
    set_description(N_("Core Animation OpenGL Layer (Mac OS X)"))
    set_capability("vout display", 0)
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT)
    set_callbacks(Open, Close)
vlc_module_end()

static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count);
static void PictureRender   (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
static void PictureDisplay  (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
static int Control          (vout_display_t *vd, int query, va_list ap);

static void *OurGetProcAddress (vlc_gl_t *gl, const char *name);
static int OpenglLock         (vlc_gl_t *gl);
static void OpenglUnlock       (vlc_gl_t *gl);
static void OpenglSwap         (vlc_gl_t *gl);

@protocol VLCCoreAnimationVideoLayerEmbedding <NSObject>
- (void)addVoutLayer:(CALayer *)aLayer;
- (void)removeVoutLayer:(CALayer *)aLayer;
- (CGSize)currentOutputSize;
@end

Felix Paul Kühne's avatar
Felix Paul Kühne committed
78 79 80
@interface VLCCAOpenGLLayer : CAOpenGLLayer

@property (nonatomic, readwrite) vout_display_t* voutDisplay;
81
@property (nonatomic, readwrite) CGLContextObj glContext;
82 83 84 85 86 87 88 89 90 91 92 93 94

@end


struct vout_display_sys_t {

    picture_pool_t *pool;
    picture_resource_t resource;

    CALayer <VLCCoreAnimationVideoLayerEmbedding> *container;
    vout_window_t *embed;
    VLCCAOpenGLLayer *cgLayer;

95
    vlc_gl_t *gl;
96 97 98 99 100 101 102
    vout_display_opengl_t *vgl;

    vout_display_place_t place;

    bool  b_frame_available;
};

103 104 105 106 107 108
struct gl_sys
{
    CGLContextObj locked_ctx;
    VLCCAOpenGLLayer *cgLayer;
};

109 110 111 112 113 114 115 116 117 118 119 120 121
/*****************************************************************************
 * Open: This function allocates and initializes the OpenGL vout method.
 *****************************************************************************/
static int Open (vlc_object_t *p_this)
{
    vout_display_t *vd = (vout_display_t *)p_this;
    vout_display_sys_t *sys;

    /* Allocate structure */
    vd->sys = sys = calloc(1, sizeof(vout_display_sys_t));
    if (sys == NULL)
        return VLC_EGENERIC;

122 123 124 125 126 127 128 129 130 131 132 133 134
    @autoreleasepool {
        id container = var_CreateGetAddress(vd, "drawable-nsobject");
        if (container)
            vout_display_DeleteWindow(vd, NULL);
        else {
            sys->embed = vout_display_NewWindow(vd, VOUT_WINDOW_TYPE_NSOBJECT);
            if (sys->embed)
                container = sys->embed->handle.nsobject;

            if (!container) {
                msg_Err(vd, "No drawable-nsobject found!");
                goto bailout;
            }
135 136
        }

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        /* store for later, released in Close() */
        sys->container = [container retain];

        [CATransaction begin];
        sys->cgLayer = [[VLCCAOpenGLLayer alloc] init];
        [sys->cgLayer setVoutDisplay:vd];

        [sys->cgLayer performSelectorOnMainThread:@selector(display)
                                       withObject:nil
                                    waitUntilDone:YES];

        if ([container respondsToSelector:@selector(addVoutLayer:)]) {
            msg_Dbg(vd, "container implements implicit protocol");
            [container addVoutLayer:sys->cgLayer];
        } else if ([container respondsToSelector:@selector(addSublayer:)] ||
                   [container isKindOfClass:[CALayer class]]) {
            msg_Dbg(vd, "container doesn't implement implicit protocol, fallback mode used");
            [container addSublayer:sys->cgLayer];
        } else {
            msg_Err(vd, "Provided NSObject container isn't compatible");
            [sys->cgLayer release];
            sys->cgLayer = nil;
            [CATransaction commit];
            goto bailout;
        }
        [CATransaction commit];
163

164 165
        if (!sys->cgLayer)
            goto bailout;
166

167
        if (![sys->cgLayer glContext])
168 169 170
            msg_Warn(vd, "we might not have an OpenGL context yet");

        /* Initialize common OpenGL video display */
171
        sys->gl = vlc_object_create(vd, sizeof(*sys->gl));
172 173 174 175 176 177
        if (unlikely(!sys->gl))
            goto bailout;
        sys->gl->makeCurrent = OpenglLock;
        sys->gl->releaseCurrent = OpenglUnlock;
        sys->gl->swap = OpenglSwap;
        sys->gl->getProcAddress = OurGetProcAddress;
178 179 180 181 182 183

        struct gl_sys *glsys = sys->gl->sys = malloc(sizeof(*glsys));
        if (!sys->gl->sys)
            goto bailout;
        glsys->locked_ctx = NULL;
        glsys->cgLayer = sys->cgLayer;
184 185 186

        const vlc_fourcc_t *subpicture_chromas;
        video_format_t fmt = vd->fmt;
187 188 189 190 191 192
        if (!OpenglLock(sys->gl)) {
            sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas,
                                               sys->gl, &vd->cfg->viewpoint);
            OpenglUnlock(sys->gl);
        } else
            sys->vgl = NULL;
193 194 195 196
        if (!sys->vgl) {
            msg_Err(vd, "Error while initializing opengl display.");
            goto bailout;
        }
197

198 199 200 201 202 203 204 205 206 207
        /* setup vout display */
        vout_display_info_t info = vd->info;
        info.subpicture_chromas = subpicture_chromas;
        vd->info = info;

        vd->pool    = Pool;
        vd->prepare = PictureRender;
        vd->display = PictureDisplay;
        vd->control = Control;

208 209 210 211 212
        if (OSX_SIERRA_AND_HIGHER) {
            /* request our screen's HDR mode (introduced in OS X 10.11, but correctly supported in 10.12 only) */
            if ([sys->cgLayer respondsToSelector:@selector(setWantsExtendedDynamicRangeContent:)]) {
                [sys->cgLayer setWantsExtendedDynamicRangeContent:YES];
            }
213 214
        }

215 216 217 218 219 220 221
        /* setup initial state */
        CGSize outputSize;
        if ([container respondsToSelector:@selector(currentOutputSize)])
            outputSize = [container currentOutputSize];
        else
            outputSize = [sys->container visibleRect].size;
        vout_display_SendEventDisplaySize(vd, (int)outputSize.width, (int)outputSize.height);
222

223
        return VLC_SUCCESS;
224

225 226 227
    bailout:
        Close(p_this);
        return VLC_EGENERIC;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    }
}

static void Close (vlc_object_t *p_this)
{
    vout_display_t *vd = (vout_display_t *)p_this;
    vout_display_sys_t *sys = vd->sys;

    if (sys->cgLayer) {
        if ([sys->container respondsToSelector:@selector(removeVoutLayer:)])
            [sys->container removeVoutLayer:sys->cgLayer];
        else
            [sys->cgLayer removeFromSuperlayer];
        [sys->cgLayer release];
    }

    if (sys->container)
        [sys->container release];

    if (sys->embed)
        vout_display_DeleteWindow(vd, sys->embed);

250
    if (sys->vgl != NULL && !OpenglLock(sys->gl)) {
251
        vout_display_opengl_Delete(sys->vgl);
252 253
        OpenglUnlock(sys->gl);
    }
254

255
    if (sys->gl != NULL)
256 257 258 259 260 261
    {
        if (sys->gl->sys != NULL)
        {
            assert(((struct gl_sys *)sys->gl->sys)->locked_ctx == NULL);
            free(sys->gl->sys);
        }
262
        vlc_object_release(sys->gl);
263
    }
264

265 266
    if ([sys->cgLayer glContext])
        CGLReleaseContext([sys->cgLayer glContext]);
267

268 269 270 271 272 273 274
    free(sys);
}

static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
{
    vout_display_sys_t *sys = vd->sys;

275
    if (!sys->pool && !OpenglLock(sys->gl)) {
276
        sys->pool = vout_display_opengl_GetPool(sys->vgl, count);
277 278 279
        OpenglUnlock(sys->gl);
        assert(sys->pool);
    }
280 281 282 283 284 285 286
    return sys->pool;
}

static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
{
    vout_display_sys_t *sys = vd->sys;

287 288 289 290 291
    if (pic == NULL) {
        msg_Warn(vd, "invalid pic, skipping frame");
        return;
    }

292
    @synchronized (sys->cgLayer) {
293 294 295 296
        if (!OpenglLock(sys->gl)) {
            vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
            OpenglUnlock(sys->gl);
        }
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    }
}

static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
{
    vout_display_sys_t *sys = vd->sys;

    @synchronized (sys->cgLayer) {
        sys->b_frame_available = YES;

        /* Calling display on the non-main thread is not officially supported, but
         * its suggested at several places and works fine here. Flush is thread-safe
         * and makes sure the picture is actually displayed. */
        [sys->cgLayer display];
        [CATransaction flush];
    }

    picture_Release(pic);

    if (subpicture)
        subpicture_Delete(subpicture);
}

static int Control (vout_display_t *vd, int query, va_list ap)
{
    vout_display_sys_t *sys = vd->sys;

    if (!vd->sys)
        return VLC_EGENERIC;

    switch (query)
    {
        case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
        case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
        case VOUT_DISPLAY_CHANGE_ZOOM:
        case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
        case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
        {
            const vout_display_cfg_t *cfg;
            const video_format_t *source;

            if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
                source = (const video_format_t *)va_arg (ap, const video_format_t *);
                cfg = vd->cfg;
            } else {
                source = &vd->source;
                cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
            }

            /* we always use our current frame here */
            vout_display_cfg_t cfg_tmp = *cfg;
348
            [CATransaction lock];
349
            CGRect bounds = [sys->cgLayer visibleRect];
350
            [CATransaction unlock];
351 352 353 354 355
            cfg_tmp.display.width = bounds.size.width;
            cfg_tmp.display.height = bounds.size.height;

            vout_display_place_t place;
            vout_display_PlacePicture (&place, source, &cfg_tmp, false);
356 357 358
            if (OpenglLock(sys->gl))
                return VLC_EGENERIC;

359
            vout_display_opengl_SetWindowAspectRatio(sys->vgl, (float)place.width / place.height);
360 361
            OpenglUnlock(sys->gl);

362 363 364 365 366
            sys->place = place;

            return VLC_SUCCESS;
        }

367 368 369 370 371 372
        case VOUT_DISPLAY_HIDE_MOUSE:
        {
            [NSCursor setHiddenUntilMouseMoves: YES];
            return VLC_SUCCESS;
        }

373
        case VOUT_DISPLAY_CHANGE_VIEWPOINT:
374 375 376 377 378 379 380
        {
            int ret;

            if (OpenglLock(sys->gl))
                return VLC_EGENERIC;

            ret = vout_display_opengl_SetViewpoint(sys->vgl,
381
                &va_arg (ap, const vout_display_cfg_t* )->viewpoint);
382 383 384
            OpenglUnlock(sys->gl);
            return ret;
        }
385

386
        case VOUT_DISPLAY_RESET_PICTURES:
387
            vlc_assert_unreachable ();
388 389 390 391 392 393 394 395 396 397 398 399 400
        default:
            msg_Err (vd, "Unhandled request %d", query);
            return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

#pragma mark -
#pragma mark OpenGL callbacks

static int OpenglLock (vlc_gl_t *gl)
{
401 402
    struct gl_sys *sys = gl->sys;
    assert(sys->locked_ctx == NULL);
403

404 405
    CGLContextObj ctx = [sys->cgLayer glContext];
    if(!ctx) {
406 407 408
        return 1;
    }

409
    CGLError err = CGLLockContext(ctx);
410
    if (kCGLNoError == err) {
411 412
        sys->locked_ctx = ctx;
        CGLSetCurrentContext(ctx);
413 414 415 416 417 418 419
        return 0;
    }
    return 1;
}

static void OpenglUnlock (vlc_gl_t *gl)
{
420 421 422
    struct gl_sys *sys = gl->sys;
    CGLUnlockContext(sys->locked_ctx);
    sys->locked_ctx = NULL;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
}

static void OpenglSwap (vlc_gl_t *gl)
{
    glFlush();
}

static void *OurGetProcAddress (vlc_gl_t *gl, const char *name)
{
    VLC_UNUSED(gl);

    return dlsym(RTLD_DEFAULT, name);
}

#pragma mark -
#pragma mark CA layer

/*****************************************************************************
 * @implementation VLCCAOpenGLLayer
 *****************************************************************************/
@implementation VLCCAOpenGLLayer

- (id)init {

    self = [super init];
    if (self) {
449
        [CATransaction lock];
450 451
        self.needsDisplayOnBoundsChange = YES;
        self.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
452 453
        self.asynchronous = NO;
        [CATransaction unlock];
454 455 456 457 458 459 460
    }

    return self;
}

- (void)setVoutDisplay:(vout_display_t *)aVd
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
461
    _voutDisplay = aVd;
462 463 464 465 466 467
}

- (void)resizeWithOldSuperlayerSize:(CGSize)size
{
    [super resizeWithOldSuperlayerSize: size];

468 469
    CGSize boundsSize = self.visibleRect.size;

Felix Paul Kühne's avatar
Felix Paul Kühne committed
470 471
    if (_voutDisplay)
        vout_display_SendEventDisplaySize(_voutDisplay, boundsSize.width, boundsSize.height);
472 473 474 475 476
}

- (BOOL)canDrawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
{
    /* Only draw the frame if we have a frame that was previously rendered */
Felix Paul Kühne's avatar
Felix Paul Kühne committed
477
    if (!_voutDisplay)
478 479
        return false;

Felix Paul Kühne's avatar
Felix Paul Kühne committed
480
    return _voutDisplay->sys->b_frame_available;
481 482 483 484
}

- (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
485
    if (!_voutDisplay)
486
        return;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
487
    vout_display_sys_t *sys = _voutDisplay->sys;
488 489 490 491

    if (!sys->vgl)
        return;

492 493
    CGRect bounds = [self visibleRect];

494 495 496 497
    // x / y are top left corner, but we need the lower left one
    glViewport (sys->place.x, bounds.size.height - (sys->place.y + sys->place.height), sys->place.width, sys->place.height);

    // flush is also done by this method, no need to call super
Felix Paul Kühne's avatar
Felix Paul Kühne committed
498
    vout_display_opengl_Display (sys->vgl, &_voutDisplay->source);
499
    sys->b_frame_available = NO;
500 501
}

502 503 504 505 506 507
-(CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
{
    // The default is fine for this demonstration.
    return [super copyCGLPixelFormatForDisplayMask:mask];
}

508 509
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
{
510
    // Only one opengl context is allowed for the module lifetime
511 512 513
    if(_glContext) {
        msg_Dbg(_voutDisplay, "Return existing context: %p", _glContext);
        return _glContext;
514 515
    }

516 517 518 519 520 521 522 523 524 525 526
    CGLContextObj context = [super copyCGLContextForPixelFormat:pixelFormat];

    // Swap buffers only during the vertical retrace of the monitor.
    //http://developer.apple.com/documentation/GraphicsImaging/
    //Conceptual/OpenGL/chap5/chapter_5_section_44.html /

    GLint params = 1;
    CGLSetParameter( CGLGetCurrentContext(), kCGLCPSwapInterval,
                     &params );

    @synchronized (self) {
527
        _glContext = context;
528 529 530 531 532 533 534
    }

    return context;
}

- (void)releaseCGLContext:(CGLContextObj)glContext
{
535
    // do not release anything here, we do that when closing the module
536 537
}

538 539 540
- (void)mouseButtonDown:(int)buttonNumber
{
    @synchronized (self) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
541
        if (_voutDisplay) {
542
            if (buttonNumber == 0)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
543
                vout_display_SendEventMousePressed (_voutDisplay, MOUSE_BUTTON_LEFT);
544
            else if (buttonNumber == 1)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
545
                vout_display_SendEventMousePressed (_voutDisplay, MOUSE_BUTTON_RIGHT);
546
            else
Felix Paul Kühne's avatar
Felix Paul Kühne committed
547
                vout_display_SendEventMousePressed (_voutDisplay, MOUSE_BUTTON_CENTER);
548 549 550 551 552 553 554
        }
    }
}

- (void)mouseButtonUp:(int)buttonNumber
{
    @synchronized (self) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
555
        if (_voutDisplay) {
556
            if (buttonNumber == 0)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
557
                vout_display_SendEventMouseReleased (_voutDisplay, MOUSE_BUTTON_LEFT);
558
            else if (buttonNumber == 1)
Felix Paul Kühne's avatar
Felix Paul Kühne committed
559
                vout_display_SendEventMouseReleased (_voutDisplay, MOUSE_BUTTON_RIGHT);
560
            else
Felix Paul Kühne's avatar
Felix Paul Kühne committed
561
                vout_display_SendEventMouseReleased (_voutDisplay, MOUSE_BUTTON_CENTER);
562 563 564 565 566 567 568
        }
    }
}

- (void)mouseMovedToX:(double)xValue Y:(double)yValue
{
    @synchronized (self) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
569 570
        if (_voutDisplay) {
            vout_display_SendMouseMovedDisplayCoordinates (_voutDisplay,
571 572 573
                                                           ORIENT_NORMAL,
                                                           xValue,
                                                           yValue,
Felix Paul Kühne's avatar
Felix Paul Kühne committed
574
                                                           &_voutDisplay->sys->place);
575 576 577 578
        }
    }
}

579
@end