1 /*****************************************************************************
2 * ios.m: iOS X OpenGLES provider
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Romain Goyet <romain.goyet at likid dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #import <UIKit/UIKit.h>
29 #import <OpenGLES/ES1/gl.h>
30 #import <OpenGLES/ES1/glext.h>
31 #include <QuartzCore/QuartzCore.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout_display.h>
40 #include <vlc_opengl.h>
42 #define USE_OPENGL_ES 1
47 * Forward declarations
49 static int Open(vlc_object_t *);
50 static void Close(vlc_object_t *);
52 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count);
53 static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
54 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture);
55 static int Control (vout_display_t *vd, int query, va_list ap);
57 static int OpenglClean(vlc_gl_t *gl);
58 static void OpenglSwap(vlc_gl_t *gl);
64 /* Will be loaded even without interface module. see voutgl.m */
66 set_description( N_("iOS OpenGL ES video output (requires UIView)"))
67 set_category(CAT_VIDEO)
68 set_subcategory(SUBCAT_VIDEO_VOUT )
69 set_capability("vout display", 300)
70 set_callbacks(Open, Close)
72 add_shortcut("ios", "vout_ios")
75 @interface VLCOpenGLESVideoView : UIView
78 EAGLContext * _context;
79 GLuint _defaultFramebuffer;
80 GLuint _colorRenderbuffer;
81 BOOL _framebufferDirty;
83 - (id)initWithFrame:(CGRect)frame andVOutDisplay:(vout_display_t *)vd;
84 @property (readonly) EAGLContext * context;
85 @property (readonly) GLuint colorRenderbuffer;
86 - (void)setVoutDisplay:(vout_display_t *)vd;
87 - (void)cleanFramebuffer;
91 struct vout_display_sys_t
93 VLCOpenGLESVideoView *glView;
97 vout_display_opengl_t *vgl;
101 bool has_first_frame;
104 // Called from vout thread
105 static int Open(vlc_object_t *this)
107 vout_display_t *vd = (vout_display_t *)this;
108 vout_display_sys_t *sys = calloc(1, sizeof(*sys));
109 NSAutoreleasePool *nsPool = nil;
118 /* Get the drawable object */
119 UIView * container = (UIView *)var_CreateGetAddress(vd, "drawable-nsobject");
121 if (![container isKindOfClass:[UIView class]]) {
122 msg_Dbg(vd, "Container isn't an UIView, passing over.");
125 vout_display_DeleteWindow(vd, NULL);
127 /* This will be released in Close(), on
128 * main thread, after we are done using it. */
129 sys->container = [container retain];
131 /* Get our main view*/
132 nsPool = [[NSAutoreleasePool alloc] init];
134 msg_Dbg(vd, "Creating VLCOpenGLESVideoView");
135 sys->glView = [[VLCOpenGLESVideoView alloc] initWithFrame:[container bounds] andVOutDisplay:vd];
139 /* We don't wait, that means that we'll have to be careful about releasing
141 * That's why we'll release on main thread in Close(). */
142 [container performSelectorOnMainThread:@selector(addSubview:) withObject:sys->glView waitUntilDone:NO];
147 /* Initialize common OpenGL video display */
148 sys->gl.lock = OpenglClean; // We don't do locking, but sometimes we need to cleanup the framebuffer
149 sys->gl.unlock = NULL;
150 sys->gl.swap = OpenglSwap;
151 sys->gl.getProcAddress = NULL;
154 sys->vgl = vout_display_opengl_New(&vd->fmt, NULL, &sys->gl);
162 vout_display_info_t info = vd->info;
163 info.has_pictures_invalid = false;
165 /* Setup vout_display_t once everything is fine */
169 vd->prepare = PictureRender;
170 vd->display = PictureDisplay;
171 vd->control = Control;
174 vout_display_SendEventFullscreen (vd, false);
175 vout_display_SendEventDisplaySize (vd, vd->source.i_visible_width, vd->source.i_visible_height, false);
185 // Called from vout thread as well
186 void Close(vlc_object_t *this)
188 vout_display_t *vd = (vout_display_t *)this;
189 vout_display_sys_t *sys = vd->sys;
191 [sys->glView setVoutDisplay:nil];
193 var_Destroy(vd, "drawable-nsobject");
194 /* release on main thread as explained in Open() */
195 [(id)sys->container performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
196 [sys->glView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
198 [sys->glView release];
200 if (sys->gl.sys != NULL)
201 vout_display_opengl_Delete(sys->vgl);
206 /*****************************************************************************
207 * vout display callbacks
208 *****************************************************************************/
210 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
212 vout_display_sys_t *sys = vd->sys;
215 sys->pool = vout_display_opengl_GetPool (sys->vgl, requested_count);
220 static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
222 vout_display_sys_t *sys = vd->sys;
224 vout_display_opengl_Prepare( sys->vgl, pic, subpicture );
227 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
229 vout_display_sys_t *sys = vd->sys;
230 vout_display_opengl_Display(sys->vgl, &vd->fmt );
231 picture_Release (pic);
232 sys->has_first_frame = true;
236 static int Control (vout_display_t *vd, int query, va_list ap)
238 vout_display_sys_t *sys = vd->sys;
242 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
243 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
244 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
245 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
246 case VOUT_DISPLAY_CHANGE_ZOOM:
247 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
248 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
252 case VOUT_DISPLAY_HIDE_MOUSE:
255 case VOUT_DISPLAY_GET_OPENGL:
257 vlc_gl_t **gl = va_arg (ap, vlc_gl_t **);
262 case VOUT_DISPLAY_RESET_PICTURES:
265 msg_Err (vd, "Unknown request in iOS vout display");
270 /*****************************************************************************
271 * vout opengl callbacks
272 *****************************************************************************/
274 static int OpenglClean(vlc_gl_t *gl) {
275 vout_display_sys_t *sys = gl->sys;
276 [sys->glView cleanFramebuffer];
280 static void OpenglSwap(vlc_gl_t *gl)
282 vout_display_sys_t *sys = gl->sys;
283 EAGLContext *context = [sys->glView context];
284 [context presentRenderbuffer:GL_RENDERBUFFER_OES];
287 /*****************************************************************************
289 * *ALL* OpenGL calls should happen in the render thread
290 *****************************************************************************/
292 @interface VLCOpenGLESVideoView (Private)
293 - (void)_createFramebuffer;
294 - (void)_updateViewportWithBackingWitdh:(GLuint)backingWidth andBackingHeight:(GLuint)backingHeight;
295 - (void)_destroyFramebuffer;
298 @implementation VLCOpenGLESVideoView
299 @synthesize context=_context, colorRenderbuffer=_colorRenderbuffer;
300 #define VLCAssertMainThread() assert([[NSThread currentThread] isMainThread])
302 + (Class)layerClass {
303 return [CAEAGLLayer class];
307 * Gets called by the Open() method.
310 - (id)initWithFrame:(CGRect)frame andVOutDisplay:(vout_display_t *)vd {
311 if (self = [super initWithFrame:frame]) {
313 CAEAGLLayer * eaglLayer = (CAEAGLLayer *)self.layer;
315 eaglLayer.opaque = TRUE;
316 eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
317 // [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
318 kEAGLColorFormatRGB565, kEAGLDrawablePropertyColorFormat,
321 _context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
322 NSAssert(_context && [EAGLContext setCurrentContext:_context], @"Creating context");
324 // This shouldn't need to be done on the main thread.
325 // Indeed, it works just fine from the render thread on iOS 3.2 to 4.1
326 // However, if you don't call it from the main thread, it doesn't work on iOS 4.2 beta 1
327 [self performSelectorOnMainThread:@selector(_createFramebuffer) withObject:nil waitUntilDone:YES];
329 _framebufferDirty = NO;
331 [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
343 * Gets called by the Close and Open methods.
346 - (void)setVoutDisplay:(vout_display_t *)aVd
348 @synchronized(self) {
355 * Method called by UIKit when we have been resized
357 - (void)layoutSubviews {
358 // CAUTION : This is called from the main thread
359 _framebufferDirty = YES;
362 - (void)cleanFramebuffer {
363 if (_framebufferDirty) {
364 [self _destroyFramebuffer];
365 [self _createFramebuffer];
366 _framebufferDirty = NO;
372 @implementation VLCOpenGLESVideoView (Private)
373 - (void)_createFramebuffer {
374 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);
375 [EAGLContext setCurrentContext:_context];
376 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
377 glGenFramebuffersOES(1, &_defaultFramebuffer); // Generate one framebuffer, store it in _defaultFrameBuffer
378 glGenRenderbuffersOES(1, &_colorRenderbuffer);
379 glBindFramebufferOES(GL_FRAMEBUFFER_OES, _defaultFramebuffer);
380 glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);
382 // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
383 // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
384 [_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
385 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, _colorRenderbuffer);
387 GLuint backingWidth, backingHeight;
388 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
389 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
391 if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
392 msg_Err(_vd, "Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
394 [self _updateViewportWithBackingWitdh:backingWidth andBackingHeight:backingHeight];
397 - (void)_updateViewportWithBackingWitdh:(GLuint)backingWidth andBackingHeight:(GLuint)backingHeight {
398 msg_Dbg(_vd, "Reshaping to %dx%d", backingWidth, backingHeight);
400 CGFloat width = (CGFloat)backingWidth;
401 CGFloat height = (CGFloat)backingHeight;
403 GLint x = width, y = height;
406 CGFloat videoHeight = _vd->source.i_visible_height;
407 CGFloat videoWidth = _vd->source.i_visible_width;
409 GLint sarNum = _vd->source.i_sar_num;
410 GLint sarDen = _vd->source.i_sar_den;
412 if (height * videoWidth * sarNum < width * videoHeight * sarDen)
414 x = (height * videoWidth * sarNum) / (videoHeight * sarDen);
420 y = (width * videoHeight * sarDen) / (videoWidth * sarNum);
424 [EAGLContext setCurrentContext:_context];
425 glViewport((width - x) / 2, (height - y) / 2, x, y);
428 - (void)_destroyFramebuffer {
429 [EAGLContext setCurrentContext:_context];
430 glDeleteFramebuffersOES(1, &_defaultFramebuffer);
431 _defaultFramebuffer = 0;
432 glDeleteRenderbuffersOES(1, &_colorRenderbuffer);
433 _colorRenderbuffer = 0;