1 2 3 4 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 44 45 46 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/*****************************************************************************
* caopengllayer.m: CAOpenGLLayer (Mac OS X) video output
*****************************************************************************
* Copyright (C) 2014 VLC authors and VideoLAN
* $Id: 9ee27b97c130a686cc9c7b04f8a503dcb3e169ed $
*
* 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 */
#include "opengl.h"
/*****************************************************************************
* 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
@interface VLCCAOpenGLLayer : CAOpenGLLayer {
vout_display_t *_vd;
}
- (void)setVoutDisplay:(vout_display_t *)aVd;
@end
struct vout_display_sys_t {
picture_pool_t *pool;
picture_resource_t resource;
CALayer <VLCCoreAnimationVideoLayerEmbedding> *container;
vout_window_t *embed;
VLCCAOpenGLLayer *cgLayer;
CGLContextObj glContext;
vlc_gl_t gl;
vout_display_opengl_t *vgl;
vout_display_place_t place;
bool b_frame_available;
};
/*****************************************************************************
* 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;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id container = var_CreateGetAddress(vd, "drawable-nsobject");
if (container)
vout_display_DeleteWindow(vd, NULL);
else {
vout_window_cfg_t wnd_cfg;
memset(&wnd_cfg, 0, sizeof(wnd_cfg));
wnd_cfg.type = VOUT_WINDOW_TYPE_NSOBJECT;
wnd_cfg.x = var_InheritInteger(vd, "video-x");
wnd_cfg.y = var_InheritInteger(vd, "video-y");
wnd_cfg.height = vd->cfg->display.height;
wnd_cfg.width = vd->cfg->display.width;
sys->embed = vout_display_NewWindow(vd, &wnd_cfg);
if (sys->embed)
container = sys->embed->handle.nsobject;
if (!container) {
msg_Err(vd, "No drawable-nsobject found!");
goto bailout;
}
}
/* 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];
if (!sys->cgLayer)
goto bailout;
if (!sys->glContext)
msg_Warn(vd, "we might not have an OpenGL context yet");
/* Initialize common OpenGL video display */
sys->gl.lock = OpenglLock;
sys->gl.unlock = OpenglUnlock;
sys->gl.swap = OpenglSwap;
sys->gl.getProcAddress = OurGetProcAddress;
sys->gl.sys = sys;
const vlc_fourcc_t *subpicture_chromas;
video_format_t fmt = vd->fmt;
sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas, &sys->gl);
if (!sys->vgl) {
msg_Err(vd, "Error while initializing opengl display.");
sys->gl.sys = NULL;
goto bailout;
}
/* setup vout display */
vout_display_info_t info = vd->info;
info.subpicture_chromas = subpicture_chromas;
info.has_hide_mouse = true;
vd->info = info;
vd->pool = Pool;
vd->prepare = PictureRender;
vd->display = PictureDisplay;
vd->control = Control;
/* setup initial state */
CGSize outputSize;
if ([container respondsToSelector:@selector(currentOutputSize)])
outputSize = [container currentOutputSize];
else
outputSize = [sys->container visibleRect].size;
vout_display_SendEventFullscreen(vd, false);
vout_display_SendEventDisplaySize(vd, (int)outputSize.width, (int)outputSize.height, false);
[pool release];
return VLC_SUCCESS;
bailout:
[pool release];
Close(p_this);
return VLC_EGENERIC;
}
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);
if (sys->gl.sys != NULL)
vout_display_opengl_Delete(sys->vgl);
if (sys->glContext)
CGLReleaseContext(sys->glContext);
free(sys);
}
static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
{
vout_display_sys_t *sys = vd->sys;
if (!sys->pool)
sys->pool = vout_display_opengl_GetPool(sys->vgl, count);
assert(sys->pool);
return sys->pool;
}
static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
{
vout_display_sys_t *sys = vd->sys;
if (pic == NULL) {
msg_Warn(vd, "invalid pic, skipping frame");
return;
}
@synchronized (sys->cgLayer) {
vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
}
}
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:
case VOUT_DISPLAY_CHANGE_FULLSCREEN:
{
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;
[CATransaction lock];
CGRect bounds = [sys->cgLayer bounds];
[CATransaction unlock];
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);
sys->place = place;
return VLC_SUCCESS;
}
case VOUT_DISPLAY_HIDE_MOUSE:
{
[NSCursor setHiddenUntilMouseMoves: YES];
return VLC_SUCCESS;
}
case VOUT_DISPLAY_GET_OPENGL:
{
vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
*gl = &sys->gl;
return VLC_SUCCESS;
}
case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
{
return VLC_SUCCESS;
}
case VOUT_DISPLAY_RESET_PICTURES:
assert (0);
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)
{
vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
if(!sys->glContext) {
return 1;
}
CGLError err = CGLLockContext(sys->glContext);
if (kCGLNoError == err) {
CGLSetCurrentContext(sys->glContext);
return 0;
}
return 1;
}
static void OpenglUnlock (vlc_gl_t *gl)
{
vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
if (!sys->glContext) {
return;
}
CGLUnlockContext(sys->glContext);
}
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) {
[CATransaction lock];
[self setAutoresizingMask: kCALayerWidthSizable | kCALayerHeightSizable];
self.asynchronous = NO;
[CATransaction unlock];
}
return self;
}
- (void)setVoutDisplay:(vout_display_t *)aVd
{
_vd = aVd;
}
- (void)resizeWithOldSuperlayerSize:(CGSize)size
{
[super resizeWithOldSuperlayerSize: size];
CGSize boundsSize = self.bounds.size;
if (_vd)
vout_display_SendEventDisplaySize(_vd, boundsSize.width, boundsSize.height, _vd->cfg->is_fullscreen);
}
- (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 */
if (!_vd)
return false;
return _vd->sys->b_frame_available;
}
- (void)drawInCGLContext:(CGLContextObj)glContext pixelFormat:(CGLPixelFormatObj)pixelFormat forLayerTime:(CFTimeInterval)timeInterval displayTime:(const CVTimeStamp *)timeStamp
{
if (!_vd)
return;
vout_display_sys_t *sys = _vd->sys;
if (!sys->vgl)
return;
CGRect bounds = [self bounds];
// 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
vout_display_opengl_Display (sys->vgl, &_vd->source);
sys->b_frame_available = NO;
}
- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat
{
// Only one opengl context is allowed for the module lifetime
if(_vd->sys->glContext) {
msg_Dbg(_vd, "Return existing context: %p", _vd->sys->glContext);
return _vd->sys->glContext;
}
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,
¶ms );
@synchronized (self) {
_vd->sys->glContext = context;
}
return context;
}
- (void)releaseCGLContext:(CGLContextObj)glContext
{
// do not release anything here, we do that when closing the module
}
- (void)mouseButtonDown:(int)buttonNumber
{
@synchronized (self) {
if (_vd) {
if (buttonNumber == 0)
vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_LEFT);
else if (buttonNumber == 1)
vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_RIGHT);
else
vout_display_SendEventMousePressed (_vd, MOUSE_BUTTON_CENTER);
}
}
}
- (void)mouseButtonUp:(int)buttonNumber
{
@synchronized (self) {
if (_vd) {
if (buttonNumber == 0)
vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_LEFT);
else if (buttonNumber == 1)
vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_RIGHT);
else
vout_display_SendEventMouseReleased (_vd, MOUSE_BUTTON_CENTER);
}
}
}
- (void)mouseMovedToX:(double)xValue Y:(double)yValue
{
@synchronized (self) {
if (_vd) {
vout_display_SendMouseMovedDisplayCoordinates (_vd,
ORIENT_NORMAL,
xValue,
yValue,
&_vd->sys->place);
}
}
}
@end
|