GPUImageContext class for GPUImage API documentation, gpuimage

Source: Internet
Author: User

GPUImageContext class for GPUImage API documentation, gpuimage

The GPUImageContext class provides the basic environment of OpenGL ES, which is generally not used.

  Attribute

@ Property (readonly, nonatomic) dispatch_queue_t contextQueue

Creates a context thread.

Description: _ contextQueue = dispatch_queue_create ("com. sunsetlakesoftware. GPUImage. openGLESContextQueue", NULL );

 

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

Note: The current coloring tool program

 

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

Note: opengl es draws the environment and manages the context.

 

  Method

   + (Void *) contextKey

Description: sets the thread identifier of the current context, a global static variable.

  

+ (GPUImageContext *) nvidimageprocessingcontext

Note: create a global GPUImageContext object Singleton.

 

+ (Dispatch_queue_t) sharedContextQueue

Creates a context queue Singleton.

 

+ (GPUImageFramebufferCache *) sharedFramebufferCache

Creates a GPUFramebufferCache Singleton.

     

-(Void) useAsCurrentContext

Note: use the current 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;

 

Complete code

# Import "GLProgram. h "# import" GPUImageFramebuffer. h "# import" GPUImageFramebufferCache. h "# define rotate (rotation) = kGPUImageRotateLeft | (rotation) = kGPUImageRotateRight | (rotation) = rotate) typedef enum {kGPUImageNoRotation, rotate} 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) implements coreVideoTextureCache; @ property (readonly) optional * framebufferCache; + (void *) contextKey; + (GPUImageContext *) includimageprocessingcontext; + (optional) sharedContextQueue; + (optional *) sharedFramebufferCache; + (void) useImageProcessingContext; -(void) useAsCurrentContext; + (void) setActiveShaderProgram :( GLProgram *) shaderProgram;-(void) implements :( GLProgram *) shaderProgram; + (GLint) implements; + (GLint) extensions; + (BOOL) deviceSupportsOpenGLESExtension :( NSString *) extension; + (BOOL) deviceSupportsRedTextures; + (BOOL) extensions; + (CGSize) Extensions :( CGSize) inputSize; -(void) presentBufferForDisplay;-(GLProgram *) implements :( NSString *) vertexShaderString fragmentShaderString :( NSString *) fragmentShaderString;-(void) useSharegroup :( optional *) sharegroup; // Manage fast texture upload + (BOOL) upload; @ end @ protocol GPUImageInput <NSObject>-(void) newFrameReadyAtTime :( CMTime) frameTime atIndex :( NSInteger) textureIndex;-(void) setInputFramebuffer :( GPUImageFramebuffer *) newInputFramebuffer atIndex :( NSInteger) textureIndex;-(NSInteger) handle;-(void) setInputSize :( CGSize) newSize atIndex :( NSInteger) textureIndex;-(void) setInputRotation :( invoke) newInputRotation atIndex :( NSInteger) textureIndex;-(CGSize) maximumOutputSize;-(void) endProcessing;-(BOOL) Disabled;-(BOOL) enabled;-(BOOL) wantsMonochromeInput;-(void) setCurrentlyReceivingMonochromeInput :( BOOL) newValue; @ endView Code

 

# Import "GPUImageContext. h "# import <OpenGLES/EAGLDrawable. h> # import <AVFoundation/AVFoundation. h ># define generation 40 @ interface GPUImageContext () {NSMutableDictionary * shaderProgramCache; NSMutableArray * Rule; Role * _ sharegroup;} @ end @ implementation GPUImageContext @ synthesize context = _ context; @ synthesize currentShaderProgram = _ currentShaderProg Ram; @ synthesize contextQueue = _ contextQueue; @ synthesize coreVideoTextureCache = _ cached; @ synthesize framebufferCache = _ framebufferCache; static void * cached;-(id) init; {if (! (Self = [super init]) {return nil;} openGLESContextQueueKey = & openGLESContextQueueKey; _ contextQueue = dispatch_queue_create ("com. sunsetlakesoftware. GPUImage. openGLESContextQueue ", NULL); # if OS _OBJECT_USE_OBJC encode (_ contextQueue, encode, (_ 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 *) extends dimageprocessingcontext; {static dispatch_once_t pred; static GPUImageContext * extends dimageprocessingcontext = nil; dispatch_once (& pred, ^ {nvidimagepr OcessingContext = [[self class] alloc] init];}); return implements dimageprocessingcontext;} + (dispatch_queue_t) sharedContextQueue; {return [self implements dimageprocessingcontext] contextQueue];} + (GPUImageFramebufferCache *) sharedFramebufferCache; {return [[self calculated dimageprocessingcontext] framebufferCache];} + (void) useImageProcessingContext; {[[GPUImageContext implements dimageprocessingcontext] useAsCurre NtContext];}-(void) useAsCurrentContext; {EAGLContext * imageProcessingContext = [self context]; if ([EAGLContext currentContext]! = ImageProcessingContext) {[EAGLContext setCurrentContext: imageProcessingContext] ;}+ (void) setActiveShaderProgram :( GLProgram *) shaderProgram; {GPUImageContext * sharedContext = [GPUImageContext context example: 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) gradient; {static encrypted pred; static GLint maxTextureSize = 0; dispatch_once (& pred, ^ {[self useImageProcessingContext]; glGetIntegerv (GL_MAX_TEXTURE_SIZE, & maxTextureSize) ;}); return maxTextureSize ;}+ (GLint) maximumTextureUnitsForThisDevice; {static dispatch_once_t pred; Static GLint maxTextureUnits = 0; dispatch_once (& pred, ^ {[self useImageProcessingContext]; glGetIntegerv (response, & Response) ;}); return maxTextureUnits ;}+ (GLint) response; {static dispatch_once_t pred; static GLint maxVaryingVectors = 0; dispatch_once (& pred, ^ {[self useImageProcessingContext]; glGetIntegerv (GL_MAX_VARYING_VECTORS Ectors) ;}); 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: Topology]; extensionNames = [extensionsString componentsSeparatedByString: @ ""] ;}); return [extensionNames containsObject: extension];} // http://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_rg.txt+ (BOOL) topology; {static dispatch_once_t pred; static BOOL supportsRedTextures = NO; dispatch_once (& pred, ^ {supportsRedTextu Res = [GPUImageContext scheme: @ "GL_EXT_texture_rg"];}); return supportsRedTextures;} + (BOOL) scheme; {static dispatch_once_t pred; static BOOL supportsFramebufferReads = NO; dispatch_once (& pred, ^ {supportsFramebufferReads = [GPUImageContext deviceSupportsOpenGLESExtension: @ "prop"] ;}); 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 * scheme = [NSString stringWithFormat: @ "V: % @-F: % @", vertexShaderString, fragmentShaderString]; GLProgram * programFromCache = [shaderProgramCache objectForKey: Scheme]; if (programFromCache = nil) {programFromCache = [[GLProgram alloc] initWithVertexShaderString: vertexShaderString fragmentShaderString: fragmentShaderString]; [shaderProgramCache s EtObject: programFromCache forKey: lookupKeyForShaderProgram]; // [shaderProgramUsageHistory addObject: lookupKeyForShaderProgram]; // if ([shaderProgramUsageHistory count]> = register) // {// for (NSUInteger vertex = 0; currentShaderProgramRemovedFromCache <10; currentShaderProgramRemovedFromCache ++) // {// NSString * shaderProgramToRemoveFromCache = [Incluobjectatindex: 0]; // [shaderProgramUsageHistory removeObjectAtIndex: 0]; // [shaderProgramCache removeObjectForKey: Secret]; //} 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 conte Xt for the first time. "); _ sharegroup = sharegroup;}-(EAGLContext *) createContext; {EAGLContext * context = [[EAGLContext alloc] initWithAPI: sharesharegroup: _ 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;}-(response) coreVideoTextureCache; {if (_ coreVideoTextureCache = NULL) {# if defined (_ IPHONE_6_0) CVReturn err = partition (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 failed % d", err);} return _ coreVideoTextureCache;}-(GPUImageFramebufferCache *) framebufferCache; {if (_ framebufferCache = nil) {_ framebufferCache = [[GPUImageFramebufferCache alloc] init];} return _ framebufferCache;} @ endView Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.