GPUImage源码解读(四)
GPUImageFramebuffer类用于管理帧缓冲对象,负责帧缓冲对象的创建和销毁,读取帧缓冲内容,其中纹理附件涉及到了相关的纹理选项。因此,它提供的属性也是和帧缓存、纹理附件、纹理选项等相关。
属性
// 帧缓存大小
@property(readonly) CGSize size;
// 纹理选项
@property(readonly) GPUTextureOptions textureOptions;
// 纹理缓存
@property(readonly) GLuint texture;
// 是否仅有纹理没有帧缓存
@property(readonly) BOOL missingFramebuffer;初始化方法
// Initialization and teardown 初始化方法
- (id)initWithSize:(CGSize)framebufferSize;
- (id)initWithSize:(CGSize)framebufferSize textureOptions:(GPUTextureOptions)fboTextureOptions onlyTexture:(BOOL)onlyGenerateTexture;
- (id)initWithSize:(CGSize)framebufferSize overriddenTexture:(GLuint)inputTexture;其中 - (id)initWithSize:(CGSize)framebufferSize; 方法使用默认的纹理选项 GPUTextureOptions 然后调用[self initWithSize:framebufferSize textureOptions:defaultTextureOptions onlyTexture:NO] 方法. 纹理选项是给glTexParameteri 方法使用.
- (id)initWithSize:(CGSize)framebufferSize textureOptions:(GPUTextureOptions)fboTextureOptions onlyTexture:(BOOL)onlyGenerateTexture;
{
if (!(self = [super init]))
{
return nil;
}
// 纹理选项
_textureOptions = fboTextureOptions;
_size = framebufferSize;
framebufferReferenceCount = 0;
referenceCountingDisabled = NO;
// 是否只生成纹理缓存
_missingFramebuffer = onlyGenerateTexture;
// 如果只生成纹理缓存,则不生成帧缓存
if (_missingFramebuffer)
{
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext useImageProcessingContext];
[self generateTexture];
framebuffer = 0;
});
}
else
{
// 既生成纹理缓存又生成帧缓存
[self generateFramebuffer];
}
return self;
}也可以使用自定义SIZE和纹理ID初始化 生成帧缓存对象
其它方法定义
# 激活。在使用帧缓存的时候首先要激活(即绑定为当前帧缓存),激活之后才能在当前帧缓存上进行相关操作。
计数器和析构方法 注意 当创建GPUImageFramebuffer时给onlyTexture参数填NO(一般就是填NO的)时,会创建一个CVPixelBufferRef类型的变量renderTarget,当用CFRelease去释放这个变量时,它占用的内存并不会立即释放,而是要调用CVOpenGLESTextureCacheFlush([[GPUImageContext sharedImageProcessingContext] coreVideoTextureCache], 0); 之后,才会真正释放内存。而在GPUImageFramebufferCache的purgeAllUnassignedFramebuffers方法中,会帮我们清空OpenGLESTextureCache,真正释放GPUImageFramebuffer占用内存。purgeAllUnassignedFramebuffers方法会在收到memory warning时触发释放内存,一般情况下无需自行调用。
从帧缓存中生成图片。在读取图片数据的时候,根据设备是否支持 CoreVideo 框架,GPUImage 会选择使用 CVPixelBufferGetBaseAddress 或者 glReadPixels 读取帧缓存中的数据。最后通过 CGImageCreate,创建 CGImage 对象并返回该对象。
获取帧缓存原始数据相关的方法。如果设备支持 CoreVideo框架,获取纹理数据的相关操作会调用下面的这些方法。
Last updated