Skip to content

Commit

Permalink
vout ios: correctly handle scenarios with multiple OpenGL contexts in…
Browse files Browse the repository at this point in the history
… a sharegroup
  • Loading branch information
fkuehne committed Sep 30, 2015
1 parent 4c0c0d3 commit c052d25
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions modules/video_output/ios2.m
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ void Close (vlc_object_t *this)

[sys->glESView release];

/* when using the traditional pipeline, the cross-platform code will free the the pool */
if (sys->zero_copy) {
if (sys->picturePool)
picture_pool_Release(sys->picturePool);
Expand Down Expand Up @@ -615,13 +616,11 @@ - (id)initWithFrame:(CGRect)frame zeroCopy:(bool)zero_copy voutDisplay:(vout_dis
/* a client app may have already created a rendering context,
* so re-use it if it is valid */
EAGLContext *existingContext = [EAGLContext currentContext];
if (existingContext) {
if ([existingContext API] == kEAGLRenderingAPIOpenGLES2)
_eaglContext = [EAGLContext currentContext];
}

if (!_eaglContext)
if (existingContext != nil)
_eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup: existingContext.sharegroup];
else
_eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

if (unlikely(!_eaglContext))
return nil;
if (unlikely(![EAGLContext setCurrentContext:_eaglContext]))
Expand Down Expand Up @@ -691,6 +690,7 @@ - (void)createBuffers
- (void)destroyBuffers
{
/* re-set current context */
EAGLContext *previousContext = [EAGLContext currentContext];
[EAGLContext setCurrentContext:_eaglContext];

/* clear frame buffer */
Expand All @@ -700,15 +700,21 @@ - (void)destroyBuffers
/* clear render buffer */
glDeleteRenderbuffers(1, &_renderBuffer);
_renderBuffer = 0;
[EAGLContext setCurrentContext:previousContext];
}

- (void)resetBuffers
{
EAGLContext *previousContext = [EAGLContext currentContext];
[EAGLContext setCurrentContext:_eaglContext];

if (_bufferNeedReset) {
[self destroyBuffers];
[self createBuffers];
_bufferNeedReset = NO;
}

[EAGLContext setCurrentContext:previousContext];
}

- (void)layoutSubviews
Expand All @@ -729,6 +735,7 @@ - (void)reshape
{
assert([[NSThread currentThread] isMainThread]);

EAGLContext *previousContext = [EAGLContext currentContext];
[EAGLContext setCurrentContext:_eaglContext];

CGSize viewSize = [self bounds].size;
Expand All @@ -752,6 +759,7 @@ - (void)reshape

// x / y are top left corner, but we need the lower left one
glViewport(place.x, place.y, place.width, place.height);
[EAGLContext setCurrentContext:previousContext];
}

- (void)tapRecognized:(UITapGestureRecognizer *)tapRecognizer
Expand Down Expand Up @@ -797,6 +805,10 @@ - (BOOL)acceptsFirstResponder

- (void)displayPixelBuffer:(CVPixelBufferRef)pixelBuffer
{
/* the currently current context may not be ours, so cache it and restore it later */
EAGLContext *previousContext = [EAGLContext currentContext];
[EAGLContext setCurrentContext:_eaglContext];

CVReturn err;
if (pixelBuffer != NULL) {
int frameWidth = (int)CVPixelBufferGetWidth(pixelBuffer);
Expand All @@ -805,7 +817,7 @@ - (void)displayPixelBuffer:(CVPixelBufferRef)pixelBuffer
if (!_videoTextureCache) {
if (_voutDisplay)
msg_Err(_voutDisplay, "No video texture cache");
return;
goto done;
}

[self cleanUpTextures];
Expand Down Expand Up @@ -934,10 +946,17 @@ Vertex data formed using (-1,-1) and (1,1) as the bottom left and top right coor

glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
[_eaglContext presentRenderbuffer:GL_RENDERBUFFER];

glFlush();

done:
/* restore previous eagl context which we cached on entry */
[EAGLContext setCurrentContext:previousContext];
}

- (void)setupZeroCopyGL
{
EAGLContext *previousContext = [EAGLContext currentContext];
[EAGLContext setCurrentContext:_eaglContext];
[self createBuffers];
[self loadShaders];
Expand All @@ -959,9 +978,9 @@ - (void)setupZeroCopyGL
if (err != noErr) {
if (_voutDisplay)
msg_Err(_voutDisplay, "Error at CVOpenGLESTextureCacheCreate %d", err);
return;
}
}
[EAGLContext setCurrentContext:previousContext];
}

- (void)cleanUpTextures
Expand Down

0 comments on commit c052d25

Please sign in to comment.