GPUImageContext类,提供OpenGL ES基本环境,我们一般不会用到,所以讲的很简单。

  属性

  @property(readonly, nonatomic) dispatch_queue_t contextQueue

  说明:创建一个context线程

  描述:_contextQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.openGLESContextQueue", NULL);

 

  @property(readwrite, retain, nonatomic) GLProgram *currentShaderProgram

  说明:当前着色器program

 

  @property(readonly, retain, nonatomic) EAGLContext *context

  说明:opengl es绘制环境,管理上下文

  方法

    + (void *)contextKey

    说明:设置当前context的线程标识符,全局静态变量。

  

    + (GPUImageContext *)sharedImageProcessingContext

    说明:创建一个全局的GPUImageContext对象的单例。

    + (dispatch_queue_t)sharedContextQueue

    说明:创建一个context queue单例

    + (GPUImageFramebufferCache *)sharedFramebufferCache

    说明:创建一个GPUFramebufferCache单例

     

    - (void)useAsCurrentContext

    说明:使用当前context

    + (void)setActiveShaderProgram:(GLProgram *)shaderProgram;

    - (void)setContextShaderProgram:(GLProgram *)shaderProgram;

    + (GLint)maximumTextureSizeForThisDevice;

    + (GLint)maximumTextureUnitsForThisDevice;

    + (GLint)maximumVaryingVectorsForThisDevice;

    + (BOOL)deviceSupportsOpenGLESExtension:(NSString *)extension;

    + (BOOL)deviceSupportsRedTextures;

    + (BOOL)deviceSupportsFramebufferReads;

    + (CGSize)sizeThatFitsWithinATextureForSize:(CGSize)inputSize;

    - (void)presentBufferForDisplay;

    - (GLProgram *)programForVertexShaderString:(NSString *)vertexShaderString fragmentShaderString:(NSString *)fragmentShaderString;

    - (void)useSharegroup:(EAGLSharegroup *)sharegroup;

    + (BOOL)supportsFastTextureUpload;

完整代码

#import "GLProgram.h"
#import "GPUImageFramebuffer.h"
#import "GPUImageFramebufferCache.h" #define GPUImageRotationSwapsWidthAndHeight(rotation) ((rotation) == kGPUImageRotateLeft || (rotation) == kGPUImageRotateRight || (rotation) == kGPUImageRotateRightFlipVertical || (rotation) == kGPUImageRotateRightFlipHorizontal) typedef enum { kGPUImageNoRotation, kGPUImageRotateLeft, kGPUImageRotateRight, kGPUImageFlipVertical, kGPUImageFlipHorizonal, kGPUImageRotateRightFlipVertical, kGPUImageRotateRightFlipHorizontal, kGPUImageRotate180 } GPUImageRotationMode; @interface GPUImageContext : NSObject @property(readonly, nonatomic) dispatch_queue_t contextQueue;
@property(readwrite, retain, nonatomic) GLProgram *currentShaderProgram;
@property(readonly, retain, nonatomic) EAGLContext *context;
@property(readonly) CVOpenGLESTextureCacheRef coreVideoTextureCache;
@property(readonly) GPUImageFramebufferCache *framebufferCache; + (void *)contextKey;
+ (GPUImageContext *)sharedImageProcessingContext;
+ (dispatch_queue_t)sharedContextQueue;
+ (GPUImageFramebufferCache *)sharedFramebufferCache;
+ (void)useImageProcessingContext;
- (void)useAsCurrentContext;
+ (void)setActiveShaderProgram:(GLProgram *)shaderProgram;
- (void)setContextShaderProgram:(GLProgram *)shaderProgram;
+ (GLint)maximumTextureSizeForThisDevice;
+ (GLint)maximumTextureUnitsForThisDevice;
+ (GLint)maximumVaryingVectorsForThisDevice;
+ (BOOL)deviceSupportsOpenGLESExtension:(NSString *)extension;
+ (BOOL)deviceSupportsRedTextures;
+ (BOOL)deviceSupportsFramebufferReads;
+ (CGSize)sizeThatFitsWithinATextureForSize:(CGSize)inputSize; - (void)presentBufferForDisplay;
- (GLProgram *)programForVertexShaderString:(NSString *)vertexShaderString fragmentShaderString:(NSString *)fragmentShaderString; - (void)useSharegroup:(EAGLSharegroup *)sharegroup; // Manage fast texture upload
+ (BOOL)supportsFastTextureUpload; @end @protocol GPUImageInput <NSObject>
- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
- (void)setInputFramebuffer:(GPUImageFramebuffer *)newInputFramebuffer atIndex:(NSInteger)textureIndex;
- (NSInteger)nextAvailableTextureIndex;
- (void)setInputSize:(CGSize)newSize atIndex:(NSInteger)textureIndex;
- (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
- (CGSize)maximumOutputSize;
- (void)endProcessing;
- (BOOL)shouldIgnoreUpdatesToThisTarget;
- (BOOL)enabled;
- (BOOL)wantsMonochromeInput;
- (void)setCurrentlyReceivingMonochromeInput:(BOOL)newValue;
@end
#import "GPUImageContext.h"
#import <OpenGLES/EAGLDrawable.h>
#import <AVFoundation/AVFoundation.h> #define MAXSHADERPROGRAMSALLOWEDINCACHE 40 @interface GPUImageContext()
{
NSMutableDictionary *shaderProgramCache;
NSMutableArray *shaderProgramUsageHistory;
EAGLSharegroup *_sharegroup;
} @end @implementation GPUImageContext @synthesize context = _context;
@synthesize currentShaderProgram = _currentShaderProgram;
@synthesize contextQueue = _contextQueue;
@synthesize coreVideoTextureCache = _coreVideoTextureCache;
@synthesize framebufferCache = _framebufferCache; static void *openGLESContextQueueKey; - (id)init;
{
if (!(self = [super init]))
{
return nil;
} openGLESContextQueueKey = &openGLESContextQueueKey;
_contextQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.openGLESContextQueue", NULL); #if OS_OBJECT_USE_OBJC
dispatch_queue_set_specific(_contextQueue, openGLESContextQueueKey, (__bridge void *)self, NULL);
#endif
shaderProgramCache = [[NSMutableDictionary alloc] init];
shaderProgramUsageHistory = [[NSMutableArray alloc] init]; return self;
} + (void *)contextKey {
return openGLESContextQueueKey;
} // Based on Colin Wheeler's example here: http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html
+ (GPUImageContext *)sharedImageProcessingContext;
{
static dispatch_once_t pred;
static GPUImageContext *sharedImageProcessingContext = nil; dispatch_once(&pred, ^{
sharedImageProcessingContext = [[[self class] alloc] init];
});
return sharedImageProcessingContext;
} + (dispatch_queue_t)sharedContextQueue;
{
return [[self sharedImageProcessingContext] contextQueue];
} + (GPUImageFramebufferCache *)sharedFramebufferCache;
{
return [[self sharedImageProcessingContext] framebufferCache];
} + (void)useImageProcessingContext;
{
[[GPUImageContext sharedImageProcessingContext] useAsCurrentContext];
} - (void)useAsCurrentContext;
{
EAGLContext *imageProcessingContext = [self context];
if ([EAGLContext currentContext] != imageProcessingContext)
{
[EAGLContext setCurrentContext:imageProcessingContext];
}
} + (void)setActiveShaderProgram:(GLProgram *)shaderProgram;
{
GPUImageContext *sharedContext = [GPUImageContext sharedImageProcessingContext];
[sharedContext setContextShaderProgram:shaderProgram];
} - (void)setContextShaderProgram:(GLProgram *)shaderProgram;
{
EAGLContext *imageProcessingContext = [self context];
if ([EAGLContext currentContext] != imageProcessingContext)
{
[EAGLContext setCurrentContext:imageProcessingContext];
} if (self.currentShaderProgram != shaderProgram)
{
self.currentShaderProgram = shaderProgram;
[shaderProgram use];
}
} + (GLint)maximumTextureSizeForThisDevice;
{
static dispatch_once_t pred;
static GLint maxTextureSize = ; dispatch_once(&pred, ^{
[self useImageProcessingContext];
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
}); return maxTextureSize;
} + (GLint)maximumTextureUnitsForThisDevice;
{
static dispatch_once_t pred;
static GLint maxTextureUnits = ; dispatch_once(&pred, ^{
[self useImageProcessingContext];
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
}); return maxTextureUnits;
} + (GLint)maximumVaryingVectorsForThisDevice;
{
static dispatch_once_t pred;
static GLint maxVaryingVectors = ; dispatch_once(&pred, ^{
[self useImageProcessingContext];
glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryingVectors);
}); return maxVaryingVectors;
} + (BOOL)deviceSupportsOpenGLESExtension:(NSString *)extension;
{
static dispatch_once_t pred;
static NSArray *extensionNames = nil; // Cache extensions for later quick reference, since this won't change for a given device
dispatch_once(&pred, ^{
[GPUImageContext useImageProcessingContext];
NSString *extensionsString = [NSString stringWithCString:(const char *)glGetString(GL_EXTENSIONS) encoding:NSASCIIStringEncoding];
extensionNames = [extensionsString componentsSeparatedByString:@" "];
}); return [extensionNames containsObject:extension];
} // http://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_rg.txt + (BOOL)deviceSupportsRedTextures;
{
static dispatch_once_t pred;
static BOOL supportsRedTextures = NO; dispatch_once(&pred, ^{
supportsRedTextures = [GPUImageContext deviceSupportsOpenGLESExtension:@"GL_EXT_texture_rg"];
}); return supportsRedTextures;
} + (BOOL)deviceSupportsFramebufferReads;
{
static dispatch_once_t pred;
static BOOL supportsFramebufferReads = NO; dispatch_once(&pred, ^{
supportsFramebufferReads = [GPUImageContext deviceSupportsOpenGLESExtension:@"GL_EXT_shader_framebuffer_fetch"];
}); return supportsFramebufferReads;
} + (CGSize)sizeThatFitsWithinATextureForSize:(CGSize)inputSize;
{
GLint maxTextureSize = [self maximumTextureSizeForThisDevice];
if ( (inputSize.width < maxTextureSize) && (inputSize.height < maxTextureSize) )
{
return inputSize;
} CGSize adjustedSize;
if (inputSize.width > inputSize.height)
{
adjustedSize.width = (CGFloat)maxTextureSize;
adjustedSize.height = ((CGFloat)maxTextureSize / inputSize.width) * inputSize.height;
}
else
{
adjustedSize.height = (CGFloat)maxTextureSize;
adjustedSize.width = ((CGFloat)maxTextureSize / inputSize.height) * inputSize.width;
} return adjustedSize;
} - (void)presentBufferForDisplay;
{
[self.context presentRenderbuffer:GL_RENDERBUFFER];
} - (GLProgram *)programForVertexShaderString:(NSString *)vertexShaderString fragmentShaderString:(NSString *)fragmentShaderString;
{
NSString *lookupKeyForShaderProgram = [NSString stringWithFormat:@"V: %@ - F: %@", vertexShaderString, fragmentShaderString];
GLProgram *programFromCache = [shaderProgramCache objectForKey:lookupKeyForShaderProgram]; if (programFromCache == nil)
{
programFromCache = [[GLProgram alloc] initWithVertexShaderString:vertexShaderString fragmentShaderString:fragmentShaderString];
[shaderProgramCache setObject:programFromCache forKey:lookupKeyForShaderProgram];
// [shaderProgramUsageHistory addObject:lookupKeyForShaderProgram];
// if ([shaderProgramUsageHistory count] >= MAXSHADERPROGRAMSALLOWEDINCACHE)
// {
// for (NSUInteger currentShaderProgramRemovedFromCache = 0; currentShaderProgramRemovedFromCache < 10; currentShaderProgramRemovedFromCache++)
// {
// NSString *shaderProgramToRemoveFromCache = [shaderProgramUsageHistory objectAtIndex:0];
// [shaderProgramUsageHistory removeObjectAtIndex:0];
// [shaderProgramCache removeObjectForKey:shaderProgramToRemoveFromCache];
// }
// }
} return programFromCache;
} - (void)useSharegroup:(EAGLSharegroup *)sharegroup;
{
NSAssert(_context == nil, @"Unable to use a share group when the context has already been created. Call this method before you use the context for the first time."); _sharegroup = sharegroup;
} - (EAGLContext *)createContext;
{
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:_sharegroup];
NSAssert(context != nil, @"Unable to create an OpenGL ES 2.0 context. The GPUImage framework requires OpenGL ES 2.0 support to work.");
return context;
} #pragma mark -
#pragma mark Manage fast texture upload + (BOOL)supportsFastTextureUpload;
{
#if TARGET_IPHONE_SIMULATOR
return NO;
#else #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-pointer-compare"
return (CVOpenGLESTextureCacheCreate != NULL);
#pragma clang diagnostic pop #endif
} #pragma mark -
#pragma mark Accessors - (EAGLContext *)context;
{
if (_context == nil)
{
_context = [self createContext];
[EAGLContext setCurrentContext:_context]; // Set up a few global settings for the image processing pipeline
glDisable(GL_DEPTH_TEST);
} return _context;
} - (CVOpenGLESTextureCacheRef)coreVideoTextureCache;
{
if (_coreVideoTextureCache == NULL)
{
#if defined(__IPHONE_6_0)
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, [self context], NULL, &_coreVideoTextureCache);
#else
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[self context], NULL, &_coreVideoTextureCache);
#endif if (err)
{
NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d", err);
} } return _coreVideoTextureCache;
} - (GPUImageFramebufferCache *)framebufferCache;
{
if (_framebufferCache == nil)
{
_framebufferCache = [[GPUImageFramebufferCache alloc] init];
} return _framebufferCache;
} @end

GPUImage API文档之GPUImageContext类的更多相关文章

  1. GPUImage API 文档之GPUImageFilter类

    GPUImageFilter类 方法 - (id)initWithVertexShaderFromString:(NSString *)vertexShaderString fragmentShade ...

  2. GPUImage API 文档之GPUImageOutput类

    GPUImageOutput类将静态图像纹理上传到OpenGL ES中,然后使用这些纹理去处理进程链中的下一个对象.它的子类可以获得滤镜处理后的图片功能.[本文讲的很少,由于有许多地方不清楚,以后会更 ...

  3. GPUImage API文档之GPUImageFramebufferCache类

    GPUImageFramebufferCache类负责管理GPUImageFramebuffer对象,是一个GPUImageFramebuffer对象的缓存. 方法 - (GPUImageFrameb ...

  4. GPUImage API 文档之GPUImagePicture类

    GPUImagePicture类静态图像处理操作,它可以是需要处理的静态图像,也可以是一张作为纹理使用的图片,调用向它发送processImage消息,进行图像滤镜处理. 方法 - (id)initW ...

  5. GPUImage API文档之GPUImageFramebuffer类

    GPUImageFramebuffer类用于管理帧缓冲对象,负责帧缓冲对象的创建和销毁,读取帧缓冲内容 属性 @property(readonly) CGSize size 说明:只读属性,在实现中, ...

  6. GPUImage API文档之GLProgram类

    GLProgram是GPUImage中代表openGL ES 中的program,具有glprogram功能. 属性 @property(readwrite, nonatomic) BOOL init ...

  7. GPUImage API文档之GPUImageInput协议

    GPUImageInput协议主要包含一些输入需要渲染目标的操作. - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)t ...

  8. 通过API文档查询Math类的方法,打印出近似圆,只要给定不同半径,圆的大小就会随之发生改变

    package question; import java.util.Scanner; import java.lang.Math; public class MathTest { /** * 未搞懂 ...

  9. Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源,BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 各种后台管理系统

    Java,面试题,简历,Linux,大数据,常用开发工具类,API文档,电子书,各种思维导图资源,百度网盘资源BBS论坛系统 ERP管理系统 OA办公自动化管理系统 车辆管理系统 家庭理财系统 各种后 ...

随机推荐

  1. Keil debugging techniques and alternative printf (SWO function)

    One of the basic needs of the embedded software development through the terminal to output debugging ...

  2. AES CBC/CTR 加解密原理

    So, lets look at how CBC works first. The following picture shows the encryption when using CBC (in ...

  3. [web.config]如何灵活使用配置文件

    摘要 在实际项目中,经常遇到比较多的环境,比如开发环境,测试环境,生产环境.对于这些环境,可能会有不同接口调用,不同的数据库连接字符串等等.那么该如何实现不同环境的参数快速切换呢?当然,最笨的方式就是 ...

  4. EditPlus(4.0.0.395)中文免激活绿色版

    EditPlus一套功能强大,可取代记事本的文字编辑器,拥有无限制的撤消与重做.英文拼字检查.自动换行.列数标记.搜寻取代.同时编辑多文件.全屏幕浏览功能.而它还有一个好用的功能,就是它有监视剪贴板的 ...

  5. 如何快速分析一款ios软件或需求的大流程,然后在业务层实现,不牵扯到界面?

    如何快速分析一款ios软件或需求的大流程,然后在业务层实现,不牵扯到界面?

  6. sqlite数据库实现字符串查找的方法(instr,substring,charindex替代方案)

    sqlite数据库是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,资源占用低,执行效率高,可以跨平台使用,已被广泛使用.作为一款轻量级的数据库,功能自然会有所欠缺,比如数据库加密,用户权限设 ...

  7. C#编程(六十二)---------LINQ标准的查询操作符

    LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作”类型”进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. ...

  8. hdu2444The Accomodation of Students (最大匹配+推断是否为二分图)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  9. git 两个中心仓库上的分支 merge

    首先在一个中心仓库里面添加另外一个仓库的所有分支. 命令: git remote add Cangku2 https://github.com/abc/abc.git git fetch 这之后在使用 ...

  10. Java 公历转农历,然后农历减一年(或者几天或者任意天),再把这个日期转成公历

    由于系统的提醒有可能是农历的今天或指定时间要用quartz 实现定时任务 公历转农历,然后农历减一年(或者几天或者任意天),再把这个日期转成公历. 网上很多农历转公历的程序都有问题,QQ.百度的也有 ...