ios.m 13.6 KB
Newer Older
Romain Goyet's avatar
Romain Goyet committed
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
/*****************************************************************************
 * voutgl.m: iOS X OpenGLES provider
 *****************************************************************************
 * Copyright (C) 2001-2009 the VideoLAN team
 * $Id$
 *
 * Authors: Romain Goyet <romain.goyet at likid dot org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 *****************************************************************************/

#import <UIKit/UIKit.h>
#import <OpenGLES/ES1/gl.h>
Pierre's avatar
Pierre committed
30 31
#import <OpenGLES/ES1/glext.h>
#include <QuartzCore/QuartzCore.h>
Romain Goyet's avatar
Romain Goyet committed
32 33 34 35 36 37 38 39

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

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout_display.h>
40
#include <vlc_opengl.h>
Romain Goyet's avatar
Romain Goyet committed
41 42

#define USE_OPENGL_ES 1
Pierre's avatar
Pierre committed
43

Romain Goyet's avatar
Romain Goyet committed
44 45 46 47 48 49 50 51 52
#include "opengl.h"

/**
 * Forward declarations
 */
static int Open(vlc_object_t *);
static void Close(vlc_object_t *);

static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count);
53 54
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);
Romain Goyet's avatar
Romain Goyet committed
55 56
static int Control (vout_display_t *vd, int query, va_list ap);

57 58
static int OpenglClean(vlc_gl_t *gl);
static void OpenglSwap(vlc_gl_t *gl);
Romain Goyet's avatar
Romain Goyet committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/**
 * Module declaration
 */
vlc_module_begin ()
    /* Will be loaded even without interface module. see voutgl.m */
    set_shortname("iOS")
    set_description( N_("iOS OpenGL ES video output (requires UIView)"))
    set_category(CAT_VIDEO)
    set_subcategory(SUBCAT_VIDEO_VOUT )
    set_capability("vout display", 300)
    set_callbacks(Open, Close)

    add_shortcut("ios", "vout_ios")
vlc_module_end ()

@interface VLCOpenGLESVideoView : UIView
{
    vout_display_t * _vd;
78
    EAGLContext * _context;
Romain Goyet's avatar
Romain Goyet committed
79
    GLuint _defaultFramebuffer;
80 81
    GLuint _colorRenderbuffer;
    BOOL _framebufferDirty;
Romain Goyet's avatar
Romain Goyet committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
}
- (id)initWithFrame:(CGRect)frame andVOutDisplay:(vout_display_t *)vd;
@property (readonly) EAGLContext * context;
@property (readonly) GLuint colorRenderbuffer;
- (void)setVoutDisplay:(vout_display_t *)vd;
- (void)cleanFramebuffer;
@end


struct vout_display_sys_t
{
    VLCOpenGLESVideoView *glView;
    UIView * container;

96
    vlc_gl_t gl;
97
    vout_display_opengl_t *vgl;
Romain Goyet's avatar
Romain Goyet committed
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

    picture_pool_t *pool;
    picture_t *current;
    bool has_first_frame;
};

// Called from vout thread
static int Open(vlc_object_t *this)
{
    vout_display_t *vd = (vout_display_t *)this;
    vout_display_sys_t *sys = calloc(1, sizeof(*sys));
    NSAutoreleasePool *nsPool = nil;

    if (!sys)
        return VLC_ENOMEM;

    vd->sys = sys;
    sys->pool = NULL;
    sys->gl.sys = NULL;

    /* Get the drawable object */
    UIView * container = (UIView *)var_CreateGetAddress(vd, "drawable-nsobject");

    if (![container isKindOfClass:[UIView class]]) {
        msg_Dbg(vd, "Container isn't an UIView, passing over.");
        goto error;
    }
    vout_display_DeleteWindow(vd, NULL);

    /* This will be released in Close(), on
     * main thread, after we are done using it. */
    sys->container = [container retain];

    /* Get our main view*/
    nsPool = [[NSAutoreleasePool alloc] init];

134 135
    msg_Dbg(vd, "Creating VLCOpenGLESVideoView");
    sys->glView = [[VLCOpenGLESVideoView alloc] initWithFrame:[container bounds] andVOutDisplay:vd];
Romain Goyet's avatar
Romain Goyet committed
136 137 138 139 140 141
    if (!sys->glView)
        goto error;

    /* We don't wait, that means that we'll have to be careful about releasing
     * container.
     * That's why we'll release on main thread in Close(). */
142
    [container performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
Romain Goyet's avatar
Romain Goyet committed
143 144 145 146 147 148 149 150

    [nsPool drain];
    nsPool = nil;

    /* Initialize common OpenGL video display */
    sys->gl.lock = OpenglClean; // We don't do locking, but sometimes we need to cleanup the framebuffer
    sys->gl.unlock = NULL;
    sys->gl.swap = OpenglSwap;
151
	sys->gl.getProcAddress = NULL;
Romain Goyet's avatar
Romain Goyet committed
152 153
    sys->gl.sys = sys;

154
	sys->vgl = vout_display_opengl_New(&vd->fmt, NULL, &sys->gl);
155
	if (!sys->vgl)
Romain Goyet's avatar
Romain Goyet committed
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
    {
        sys->gl.sys = NULL;
        goto error;
    }

    /* */
    vout_display_info_t info = vd->info;
    info.has_pictures_invalid = false;

    /* Setup vout_display_t once everything is fine */
    vd->info = info;

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

    /* */
    vout_display_SendEventFullscreen (vd, false);
    vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);

    return VLC_SUCCESS;

error:
    [nsPool release];
    Close(this);
    return VLC_EGENERIC;
}

// Called from vout thread as well
void Close(vlc_object_t *this)
{
    vout_display_t *vd = (vout_display_t *)this;
    vout_display_sys_t *sys = vd->sys;

    [sys->glView setVoutDisplay:nil];

    var_Destroy(vd, "drawable-nsobject");
    /* release on main thread as explained in Open() */
    [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
    [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];

    [sys->glView release];

    if (sys->gl.sys != NULL)
201
        vout_display_opengl_Delete(sys->vgl);
Romain Goyet's avatar
Romain Goyet committed
202 203 204 205 206 207 208 209 210 211 212 213 214

    free (sys);
}

/*****************************************************************************
 * vout display callbacks
 *****************************************************************************/

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

    if (!sys->pool)
215
        sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
Romain Goyet's avatar
Romain Goyet committed
216 217 218 219
    assert(sys->pool);
    return sys->pool;
}

220
static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
Romain Goyet's avatar
Romain Goyet committed
221 222 223
{
    vout_display_sys_t *sys = vd->sys;

224
    vout_display_opengl_Prepare( sys->vgl, pic, subpicture );
Romain Goyet's avatar
Romain Goyet committed
225 226
}

227
static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
Romain Goyet's avatar
Romain Goyet committed
228 229
{
    vout_display_sys_t *sys = vd->sys;
230
    vout_display_opengl_Display(sys->vgl, &vd->fmt );
Romain Goyet's avatar
Romain Goyet committed
231 232
    picture_Release (pic);
    sys->has_first_frame = true;
233
	(void)subpicture;
Romain Goyet's avatar
Romain Goyet committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
}

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

    switch (query)
    {
        case VOUT_DISPLAY_CHANGE_FULLSCREEN:
        case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
        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:
        {
            return VLC_EGENERIC;
        }
        case VOUT_DISPLAY_HIDE_MOUSE:
            return VLC_SUCCESS;

        case VOUT_DISPLAY_GET_OPENGL:
        {
257
            vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
Romain Goyet's avatar
Romain Goyet committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            *gl = &sys->gl;
            return VLC_SUCCESS;
        }

        case VOUT_DISPLAY_RESET_PICTURES:
            assert (0);
        default:
            msg_Err (vd, "Unknown request in iOS vout display");
            return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * vout opengl callbacks
 *****************************************************************************/

274
static int OpenglClean(vlc_gl_t *gl) {
Romain Goyet's avatar
Romain Goyet committed
275
    vout_display_sys_t *sys = gl->sys;
276 277
    [sys->glView cleanFramebuffer];
    return 0;
Romain Goyet's avatar
Romain Goyet committed
278 279
}

280
static void OpenglSwap(vlc_gl_t *gl)
Romain Goyet's avatar
Romain Goyet committed
281 282 283
{
    vout_display_sys_t *sys = gl->sys;
    EAGLContext *context = [sys->glView context];
284
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
Romain Goyet's avatar
Romain Goyet committed
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
}

/*****************************************************************************
 * Our UIView object
 * *ALL* OpenGL calls should happen in the render thread
 *****************************************************************************/

@interface VLCOpenGLESVideoView (Private)
- (void)_createFramebuffer;
- (void)_updateViewportWithBackingWitdh:(GLuint)backingWidth andBackingHeight:(GLuint)backingHeight;
- (void)_destroyFramebuffer;
@end

@implementation VLCOpenGLESVideoView
@synthesize context=_context, colorRenderbuffer=_colorRenderbuffer;
#define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])

+ (Class)layerClass {
    return [CAEAGLLayer class];
}

/**
 * Gets called by the Open() method.
 */

- (id)initWithFrame:(CGRect)frame andVOutDisplay:(vout_display_t *)vd {
311 312 313
    if (self = [super initWithFrame:frame]) {
        _vd = vd;
        CAEAGLLayer * eaglLayer = (CAEAGLLayer *)self.layer;
Romain Goyet's avatar
Romain Goyet committed
314 315 316

        eaglLayer.opaque = TRUE;
        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
317 318 319
//                                        [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
                                        kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat,
                                        nil];
Romain Goyet's avatar
Romain Goyet committed
320

321 322
        _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
        NSAssert(_context && [EAGLContext setCurrentContext:_context], @"Creating context");
Romain Goyet's avatar
Romain Goyet committed
323 324 325 326 327 328

        // This shouldn't need to be done on the main thread.
        // Indeed, it works just fine from the render thread on iOS 3.2 to 4.1
        // However, if you don't call it from the main thread, it doesn't work on iOS 4.2 beta 1
        [self performSelectorOnMainThread:@selector(_createFramebuffer) withObject:nil waitUntilDone:YES];

329
        _framebufferDirty = NO;
Romain Goyet's avatar
Romain Goyet committed
330

331 332
        [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    }
Romain Goyet's avatar
Romain Goyet committed
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
    return self;
}

- (void) dealloc
{
    [_context release];
    [super dealloc];
}

/**
 * Gets called by the Close and Open methods.
 * (Non main thread).
 */
- (void)setVoutDisplay:(vout_display_t *)aVd
{
    @synchronized(self) {
        _vd = aVd;
    }
}


/**
 * Method called by UIKit when we have been resized
 */
- (void)layoutSubviews {
358 359
    // CAUTION : This is called from the main thread
    _framebufferDirty = YES;
Romain Goyet's avatar
Romain Goyet committed
360 361 362
}

- (void)cleanFramebuffer {
363 364 365 366 367
    if (_framebufferDirty) {
        [self _destroyFramebuffer];
        [self _createFramebuffer];
        _framebufferDirty = NO;
    }
Romain Goyet's avatar
Romain Goyet committed
368 369 370 371 372 373
}

@end

@implementation VLCOpenGLESVideoView (Private)
- (void)_createFramebuffer {
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    msg_Dbg(_vd, "Creating framebuffer for layer %p with bounds (%.1f,%.1f,%.1f,%.1f)", self.layer, self.layer.bounds.origin.x, self.layer.bounds.origin.y, self.layer.bounds.size.width, self.layer.bounds.size.height);
    [EAGLContext setCurrentContext:_context];
    // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
    glGenFramebuffersOES(1, &_defaultFramebuffer); // Generate one framebuffer, store it in _defaultFrameBuffer
    glGenRenderbuffersOES(1, &_colorRenderbuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, _defaultFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);

    // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
    // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
    [_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _colorRenderbuffer);

    GLuint backingWidth, backingHeight;
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
        msg_Err(_vd, "Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
    }
    [self _updateViewportWithBackingWitdh:backingWidth andBackingHeight:backingHeight];
Romain Goyet's avatar
Romain Goyet committed
395 396 397
}

- (void)_updateViewportWithBackingWitdh:(GLuint)backingWidth andBackingHeight:(GLuint)backingHeight {
398
    msg_Dbg(_vd, "Reshaping to %dx%d", backingWidth, backingHeight);
Romain Goyet's avatar
Romain Goyet committed
399

400 401
    CGFloat width = (CGFloat)backingWidth;
    CGFloat height = (CGFloat)backingHeight;
Romain Goyet's avatar
Romain Goyet committed
402 403 404

    GLint x = width, y = height;

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    if (_vd) {
        CGFloat videoHeight = _vd->source.i_visible_height;
        CGFloat videoWidth = _vd->source.i_visible_width;

        GLint sarNum = _vd->source.i_sar_num;
        GLint sarDen = _vd->source.i_sar_den;

        if (height * videoWidth * sarNum < width * videoHeight * sarDen)
        {
            x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
            y = height;
        }
        else
        {
            x = width;
            y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
        }
    }

    [EAGLContext setCurrentContext:_context];
    glViewport((width - x) / 2, (height - y) / 2, x, y);
Romain Goyet's avatar
Romain Goyet committed
426 427 428
}

- (void)_destroyFramebuffer {
429 430 431 432 433
    [EAGLContext setCurrentContext:_context];
    glDeleteFramebuffersOES(1, &_defaultFramebuffer);
    _defaultFramebuffer = 0;
    glDeleteRenderbuffersOES(1, &_colorRenderbuffer);
    _colorRenderbuffer = 0;
Romain Goyet's avatar
Romain Goyet committed
434 435 436
}
@end