1 /*****************************************************************************
2 * ios2.m: iOS OpenGL ES 2 provider
3 *****************************************************************************
4 * Copyright (C) 2001-2014 VLC authors and VideoLAN
7 * Authors: Pierre d'Herbemont <pdherbemont at videolan dot org>
8 * Felix Paul Kühne <fkuehne at videolan dot org>
9 * David Fuhrmann <david dot fuhrmann at googlemail dot com>
11 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
12 * Eric Petit <titer@m0k.org>
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation; either version 2.1 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this program; if not, write to the Free Software Foundation,
27 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
34 #import <UIKit/UIKit.h>
35 #import <OpenGLES/EAGL.h>
36 #import <OpenGLES/ES2/gl.h>
37 #import <QuartzCore/QuartzCore.h>
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_vout_display.h>
47 #include <vlc_opengl.h>
48 #include <vlc_dialog.h>
52 * Forward declarations
54 static int Open(vlc_object_t *);
55 static void Close(vlc_object_t *);
57 static picture_pool_t* PicturePool(vout_display_t *vd, unsigned requested_count);
58 static void PictureRender(vout_display_t* vd, picture_t *pic, subpicture_t *subpicture);
59 static void PictureDisplay(vout_display_t* vd, picture_t *pic, subpicture_t *subpicture);
60 static int Control(vout_display_t* vd, int query, va_list ap);
62 static void *OurGetProcAddress(vlc_gl_t *, const char *);
64 static int OpenglESClean(vlc_gl_t* gl);
65 static void OpenglESSwap(vlc_gl_t* gl);
71 set_shortname("iOS vout")
72 set_description(N_("iOS OpenGL video output"))
73 set_category(CAT_VIDEO)
74 set_subcategory(SUBCAT_VIDEO_VOUT)
75 set_capability("vout display", 300)
76 set_callbacks(Open, Close)
78 add_shortcut("vout_ios2")
81 @interface VLCOpenGLES2VideoView : UIView {
82 vout_display_t *_voutDisplay;
83 EAGLContext *_eaglContext;
87 BOOL _bufferNeedReset;
90 @property (readwrite) vout_display_t* voutDisplay;
91 @property (readonly) EAGLContext* eaglContext;
92 @property (readonly) BOOL isAppActive;
94 - (void)createBuffers;
95 - (void)destroyBuffers;
99 struct vout_display_sys_t
101 VLCOpenGLES2VideoView *glESView;
102 UIView* viewContainer;
103 UITapGestureRecognizer *tapRecognizer;
106 vout_display_opengl_t *vgl;
108 picture_pool_t *picturePool;
109 bool has_first_frame;
111 vout_display_place_t place;
114 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
118 return dlsym(RTLD_DEFAULT, name);
121 static int Open(vlc_object_t *this)
123 vout_display_t *vd = (vout_display_t *)this;
125 if (vout_display_IsWindowed(vd))
128 vout_display_sys_t *sys = calloc (1, sizeof(*sys));
129 NSAutoreleasePool *autoreleasePool = nil;
135 sys->picturePool = NULL;
138 autoreleasePool = [[NSAutoreleasePool alloc] init];
140 /* get the object we will draw into */
141 UIView* viewContainer = var_CreateGetAddress (vd, "drawable-nsobject");
142 if (!viewContainer || ![viewContainer isKindOfClass:[UIView class]])
145 /* This will be released in Close(), on
146 * main thread, after we are done using it. */
147 sys->viewContainer = [viewContainer retain];
149 /* setup the actual OpenGL ES view */
150 sys->glESView = [[VLCOpenGLES2VideoView alloc] initWithFrame:[viewContainer bounds]];
155 [sys->glESView setVoutDisplay:vd];
157 [sys->viewContainer performSelectorOnMainThread:@selector(addSubview:)
158 withObject:sys->glESView
161 /* add tap gesture recognizer for DVD menus and stuff */
162 sys->tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:sys->glESView
163 action:@selector(tapRecognized:)];
164 if (sys->viewContainer.window) {
165 if (sys->viewContainer.window.rootViewController) {
166 if (sys->viewContainer.window.rootViewController.view)
167 [sys->viewContainer.superview addGestureRecognizer:sys->tapRecognizer];
170 sys->tapRecognizer.cancelsTouchesInView = NO;
172 /* Initialize common OpenGL video display */
173 sys->gl.lock = OpenglESClean;
174 sys->gl.unlock = nil;
175 sys->gl.swap = OpenglESSwap;
176 sys->gl.getProcAddress = OurGetProcAddress;
178 const vlc_fourcc_t *subpicture_chromas;
179 video_format_t fmt = vd->fmt;
181 sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas, &sys->gl);
188 vout_display_info_t info = vd->info;
189 info.has_pictures_invalid = false;
190 info.has_event_thread = true;
191 info.subpicture_chromas = subpicture_chromas;
192 info.has_hide_mouse = false;
194 /* Setup vout_display_t once everything is fine */
197 vd->pool = PicturePool;
198 vd->prepare = PictureRender;
199 vd->display = PictureDisplay;
200 vd->control = Control;
202 /* forward our dimensions to the vout core */
203 CGSize viewSize = sys->viewContainer.frame.size;
204 vout_display_SendEventFullscreen(vd, false);
205 vout_display_SendEventDisplaySize(vd, (int)viewSize.width, (int)viewSize.height);
208 [[NSNotificationCenter defaultCenter] addObserver:sys->glESView
209 selector:@selector(applicationStateChanged:)
210 name:UIApplicationWillResignActiveNotification
212 [[NSNotificationCenter defaultCenter] addObserver:sys->glESView
213 selector:@selector(applicationStateChanged:)
214 name:UIApplicationDidBecomeActiveNotification
216 [sys->glESView performSelectorOnMainThread:@selector(reshape)
220 [autoreleasePool release];
224 [autoreleasePool release];
229 void Close (vlc_object_t *this)
231 vout_display_t *vd = (vout_display_t *)this;
232 vout_display_sys_t *sys = vd->sys;
234 if (sys->tapRecognizer) {
235 [sys->tapRecognizer.view removeGestureRecognizer:sys->tapRecognizer];
236 [sys->tapRecognizer release];
239 [sys->glESView setVoutDisplay:nil];
241 var_Destroy (vd, "drawable-nsobject");
242 [sys->viewContainer performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
243 [sys->glESView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
245 if (sys->gl.sys != NULL) {
246 msg_Dbg(this, "deleting display");
247 vout_display_opengl_Delete(sys->vgl);
250 [sys->glESView release];
255 /*****************************************************************************
256 * vout display callbacks
257 *****************************************************************************/
259 static int Control(vout_display_t *vd, int query, va_list ap)
261 vout_display_sys_t *sys = vd->sys;
264 case VOUT_DISPLAY_HIDE_MOUSE:
267 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
268 case VOUT_DISPLAY_CHANGE_ZOOM:
269 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
270 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
271 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
276 NSAutoreleasePool * autoreleasePool = [[NSAutoreleasePool alloc] init];
278 const vout_display_cfg_t *cfg;
279 const video_format_t *source;
281 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
282 source = (const video_format_t *)va_arg(ap, const video_format_t *);
285 source = &vd->source;
286 cfg = (const vout_display_cfg_t*)va_arg(ap, const vout_display_cfg_t *);
289 /* we don't adapt anything here regardless of what the vout core
290 * wants since we are not in a traditional desktop window */
294 vout_display_cfg_t cfg_tmp = *cfg;
296 viewSize = [sys->glESView bounds].size;
298 /* on HiDPI displays, the point bounds don't equal the actual pixels */
299 CGFloat scaleFactor = sys->glESView.contentScaleFactor;
300 cfg_tmp.display.width = viewSize.width * scaleFactor;
301 cfg_tmp.display.height = viewSize.height * scaleFactor;
303 vout_display_place_t place;
304 vout_display_PlacePicture(&place, source, &cfg_tmp, false);
305 @synchronized (sys->glESView) {
309 // x / y are top left corner, but we need the lower left one
310 if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
311 glViewport(place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
313 [autoreleasePool release];
317 case VOUT_DISPLAY_RESET_PICTURES:
318 vlc_assert_unreachable ();
320 msg_Err(vd, "Unknown request %d", query);
321 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
326 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
328 vout_display_sys_t *sys = vd->sys;
329 sys->has_first_frame = true;
330 if (likely([sys->glESView isAppActive]))
331 vout_display_opengl_Display(sys->vgl, &vd->source);
333 picture_Release(pic);
336 subpicture_Delete(subpicture);
339 static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
341 vout_display_sys_t *sys = vd->sys;
343 if (likely([sys->glESView isAppActive]))
344 vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
347 static picture_pool_t *PicturePool(vout_display_t *vd, unsigned requested_count)
349 vout_display_sys_t *sys = vd->sys;
351 if (!sys->picturePool)
352 sys->picturePool = vout_display_opengl_GetPool(sys->vgl, requested_count);
353 assert(sys->picturePool);
354 return sys->picturePool;
357 /*****************************************************************************
358 * vout opengl callbacks
359 *****************************************************************************/
360 static int OpenglESClean(vlc_gl_t *gl)
362 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
363 if (likely([sys->glESView isAppActive]))
364 [sys->glESView resetBuffers];
368 static void OpenglESSwap(vlc_gl_t *gl)
370 vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
371 if (likely([sys->glESView isAppActive]))
372 [[sys->glESView eaglContext] presentRenderbuffer:GL_RENDERBUFFER];
375 /*****************************************************************************
377 *****************************************************************************/
378 @implementation VLCOpenGLES2VideoView
379 @synthesize voutDisplay = _voutDisplay, eaglContext = _eaglContext, isAppActive = _appActive;
383 return [CAEAGLLayer class];
386 - (id)initWithFrame:(CGRect)frame
388 self = [super initWithFrame:frame];
393 CAEAGLLayer * layer = (CAEAGLLayer *)self.layer;
394 layer.drawableProperties = [NSDictionary dictionaryWithObject:kEAGLColorFormatRGBA8 forKey: kEAGLDrawablePropertyColorFormat];
397 _eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
400 [EAGLContext setCurrentContext:_eaglContext];
402 [self performSelectorOnMainThread:@selector(createBuffers) withObject:nil waitUntilDone:YES];
403 [self performSelectorOnMainThread:@selector(reshape) withObject:nil waitUntilDone:NO];
404 [self setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
406 _appActive = ([UIApplication sharedApplication].applicationState == UIApplicationStateActive);
413 [[NSNotificationCenter defaultCenter] removeObserver:self];
414 [_eaglContext release];
418 - (void)didMoveToWindow
420 self.contentScaleFactor = self.window.screen.scale;
421 _bufferNeedReset = YES;
424 - (void)createBuffers
426 /* make sure the current context is us */
427 [EAGLContext setCurrentContext:_eaglContext];
429 /* create render buffer */
430 glGenRenderbuffers(1, &_renderBuffer);
431 glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
433 /* create frame buffer */
434 glGenFramebuffers(1, &_frameBuffer);
435 glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
437 /* allocate storage for the pixels we are going to to draw to */
438 [_eaglContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:(id<EAGLDrawable>)self.layer];
440 /* bind render buffer to frame buffer */
441 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderBuffer);
443 /* make sure that our shape is ok */
444 [self performSelectorOnMainThread:@selector(reshape) withObject:nil waitUntilDone:NO];
447 - (void)destroyBuffers
449 /* re-set current context */
450 [EAGLContext setCurrentContext:_eaglContext];
452 /* clear frame buffer */
453 glDeleteFramebuffers(1, &_frameBuffer);
456 /* clear render buffer */
457 glDeleteRenderbuffers(1, &_renderBuffer);
463 if (_bufferNeedReset) {
464 [self destroyBuffers];
465 [self createBuffers];
466 _bufferNeedReset = NO;
470 - (void)layoutSubviews
472 /* this method is called as soon as we are resized.
473 * so set a variable to re-create our buffers on the next clean event */
474 _bufferNeedReset = YES;
479 assert([[NSThread currentThread] isMainThread]);
481 [EAGLContext setCurrentContext:_eaglContext];
483 CGSize viewSize = [self bounds].size;
485 vout_display_place_t place;
487 @synchronized(self) {
489 vout_display_cfg_t cfg_tmp = *(_voutDisplay->cfg);
490 CGFloat scaleFactor = self.contentScaleFactor;
492 cfg_tmp.display.width = viewSize.width * scaleFactor;
493 cfg_tmp.display.height = viewSize.height * scaleFactor;
495 vout_display_PlacePicture(&place, &_voutDisplay->source, &cfg_tmp, false);
496 _voutDisplay->sys->place = place;
497 vout_display_SendEventDisplaySize(_voutDisplay, viewSize.width * scaleFactor,
498 viewSize.height * scaleFactor);
502 // x / y are top left corner, but we need the lower left one
503 glViewport(place.x, place.y, place.width, place.height);
506 - (void)tapRecognized:(UITapGestureRecognizer *)tapRecognizer
508 UIGestureRecognizerState state = [tapRecognizer state];
509 CGPoint touchPoint = [tapRecognizer locationInView:self];
510 CGFloat scaleFactor = self.contentScaleFactor;
511 vout_display_SendMouseMovedDisplayCoordinates(_voutDisplay, ORIENT_NORMAL,
512 (int)touchPoint.x * scaleFactor, (int)touchPoint.y * scaleFactor,
513 &_voutDisplay->sys->place);
515 vout_display_SendEventMousePressed(_voutDisplay, MOUSE_BUTTON_LEFT);
516 vout_display_SendEventMouseReleased(_voutDisplay, MOUSE_BUTTON_LEFT);
519 - (void)applicationStateChanged:(NSNotification *)notification
521 if ([[notification name] isEqualToString:UIApplicationWillResignActiveNotification]
522 || [[notification name] isEqualToString:UIApplicationDidEnterBackgroundNotification]
523 || [[notification name] isEqualToString:UIApplicationWillTerminateNotification])
529 - (void)updateConstraints
532 [super updateConstraints];
540 - (BOOL)acceptsFirstResponder