Skip to content

Commit

Permalink
Attempting to protect against improper deallocation of resources outs…
Browse files Browse the repository at this point in the history
…ide of the image processing OpenGL ES context.
  • Loading branch information
BradLarson committed May 24, 2013
1 parent 26e3988 commit 4db09e5
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 108 deletions.
38 changes: 24 additions & 14 deletions framework/Source/GPUImageBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,36 @@ - (GLuint)textureForOutput;

- (GLuint)generateTexture;
{
GLuint newTextureName = 0;
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &newTextureName);
glBindTexture(GL_TEXTURE_2D, newTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

CGSize currentFBOSize = [self sizeOfFBO];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFBOSize.width, (int)currentFBOSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);
__block GLuint newTextureName = 0;

runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &newTextureName);
glBindTexture(GL_TEXTURE_2D, newTextureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// This is necessary for non-power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

CGSize currentFBOSize = [self sizeOfFBO];
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)currentFBOSize.width, (int)currentFBOSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

});

return newTextureName;
}

- (void)removeTexture:(GLuint)textureToRemove;
{
glDeleteTextures(1, &textureToRemove);
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

glDeleteTextures(1, &textureToRemove);
});
}

#pragma mark -
Expand Down
16 changes: 10 additions & 6 deletions framework/Source/GPUImageFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -941,14 +941,18 @@ - (void)cleanupOutputImage;

- (void)deleteOutputTexture;
{
if (!([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage))
{
if (outputTexture)
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (!([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage))
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
}
}
});
}

- (CGSize)maximumOutputSize;
Expand Down
14 changes: 9 additions & 5 deletions framework/Source/GPUImageOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,15 @@ - (void)initializeOutputTextureIfNeeded;

- (void)deleteOutputTexture;
{
if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
});
}

- (void)forceProcessingAtSize:(CGSize)frameSize;
Expand Down
28 changes: 16 additions & 12 deletions framework/Source/GPUImagePoissonBlendFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,24 @@ - (void)initializeSecondOutputTextureIfNeeded;

- (void)deleteOutputTexture;
{
if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}

if (!([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage))
{
if (secondFilterOutputTexture)
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (outputTexture)
{
glDeleteTextures(1, &secondFilterOutputTexture);
secondFilterOutputTexture = 0;
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
}

if (!([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage))
{
if (secondFilterOutputTexture)
{
glDeleteTextures(1, &secondFilterOutputTexture);
secondFilterOutputTexture = 0;
}
}
});
}

- (void)createFilterFBOofSize:(CGSize)currentFBOSize
Expand Down
40 changes: 21 additions & 19 deletions framework/Source/GPUImageRawDataOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,27 @@ - (void)createDataFBO;

- (void)destroyDataFBO;
{
[GPUImageContext useImageProcessingContext];

if (renderTexture)
{
CFRelease(renderTexture);
renderTexture = NULL;
}

if (dataFramebuffer)
{
glDeleteFramebuffers(1, &dataFramebuffer);
dataFramebuffer = 0;
}

if (dataRenderbuffer)
{
glDeleteRenderbuffers(1, &dataRenderbuffer);
dataRenderbuffer = 0;
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (renderTexture)
{
CFRelease(renderTexture);
renderTexture = NULL;
}

if (dataFramebuffer)
{
glDeleteFramebuffers(1, &dataFramebuffer);
dataFramebuffer = 0;
}

if (dataRenderbuffer)
{
glDeleteRenderbuffers(1, &dataRenderbuffer);
dataRenderbuffer = 0;
}
});
}

- (void)setFilterFBO;
Expand Down
16 changes: 10 additions & 6 deletions framework/Source/GPUImageToneCurveFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,16 @@ - (void)setPointsWithACVURL:(NSURL*)curveFileURL

- (void)dealloc
{
if (toneCurveTexture)
{
glDeleteTextures(1, &toneCurveTexture);
toneCurveTexture = 0;
free(toneCurveByteArray);
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (toneCurveTexture)
{
glDeleteTextures(1, &toneCurveTexture);
toneCurveTexture = 0;
free(toneCurveByteArray);
}
});
}

#pragma mark -
Expand Down
26 changes: 15 additions & 11 deletions framework/Source/GPUImageTwoPassFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,24 @@ - (void)initializeSecondOutputTextureIfNeeded;

- (void)deleteOutputTexture;
{
if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (!([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage))
{
if (secondFilterOutputTexture)
if (outputTexture)
{
glDeleteTextures(1, &secondFilterOutputTexture);
secondFilterOutputTexture = 0;
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
}

if (!([GPUImageContext supportsFastTextureUpload] && preparedToCaptureImage))
{
if (secondFilterOutputTexture)
{
glDeleteTextures(1, &secondFilterOutputTexture);
secondFilterOutputTexture = 0;
}
}
});
}

#pragma mark -
Expand Down
26 changes: 15 additions & 11 deletions framework/Source/GPUImageVideoCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -784,17 +784,21 @@ - (void)createYUVConversionFBO;

- (void)destroyYUVConversionFBO;
{
if (yuvConversionFramebuffer)
{
glDeleteFramebuffers(1, &yuvConversionFramebuffer);
yuvConversionFramebuffer = 0;
}

if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (yuvConversionFramebuffer)
{
glDeleteFramebuffers(1, &yuvConversionFramebuffer);
yuvConversionFramebuffer = 0;
}

if (outputTexture)
{
glDeleteTextures(1, &outputTexture);
outputTexture = 0;
}
});
}


Expand Down
50 changes: 26 additions & 24 deletions framework/Source/iOS/GPUImageMovieWriter.m
Original file line number Diff line number Diff line change
Expand Up @@ -402,37 +402,39 @@ - (void)createDataFBO;

- (void)destroyDataFBO;
{
[GPUImageContext useImageProcessingContext];
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];

if (movieFramebuffer)
{
glDeleteFramebuffers(1, &movieFramebuffer);
movieFramebuffer = 0;
}

if (movieRenderbuffer)
{
glDeleteRenderbuffers(1, &movieRenderbuffer);
movieRenderbuffer = 0;
}

if ([GPUImageContext supportsFastTextureUpload])
{
if (coreVideoTextureCache)
if (movieFramebuffer)
{
CFRelease(coreVideoTextureCache);
glDeleteFramebuffers(1, &movieFramebuffer);
movieFramebuffer = 0;
}

if (renderTexture)
if (movieRenderbuffer)
{
CFRelease(renderTexture);
glDeleteRenderbuffers(1, &movieRenderbuffer);
movieRenderbuffer = 0;
}
if (renderTarget)

if ([GPUImageContext supportsFastTextureUpload])
{
CVPixelBufferRelease(renderTarget);
if (coreVideoTextureCache)
{
CFRelease(coreVideoTextureCache);
}

if (renderTexture)
{
CFRelease(renderTexture);
}
if (renderTarget)
{
CVPixelBufferRelease(renderTarget);
}

}

}
});
}

- (void)setFilterFBO;
Expand Down

0 comments on commit 4db09e5

Please sign in to comment.