GPUImageFramebufferCache class for GPUImage API documentation,

Source: Internet
Author: User

GPUImageFramebufferCache class for GPUImage API documentation,

The GPUImageFramebufferCache class is used to manage GPUImageFramebuffer objects. It is the cache of A GPUImageFramebuffer object.

  Method

-(GPUImageFramebuffer *) fetchFramebufferForSize :( CGSize) framebufferSize textureOptions :( GPUTextureOptions) textureOptions onlyTexture :( BOOL) onlyTexture

Note: obtain a GPUImageFramebuffer object from the cache. If the collection fails, create a new GPUImageFramebuffer object.

Parameter: framebufferSize indicates the object size, textureOptions indicates the texture configuration information, and onlyTexture indicates whether the texture is used only and whether the framebuffer object is included.

Return: A GPUImageFramebuffer object

Implementation

 

- (GPUImageFramebuffer *)fetchFramebufferForSize:(CGSize)framebufferSize textureOptions:(GPUTextureOptions)textureOptions onlyTexture:(BOOL)onlyTexture;{    __block GPUImageFramebuffer *framebufferFromCache = nil;//    dispatch_sync(framebufferCacheQueue, ^{    runSynchronouslyOnVideoProcessingQueue(^{        NSString *lookupHash = [self hashForSize:framebufferSize textureOptions:textureOptions onlyTexture:onlyTexture];        NSNumber *numberOfMatchingTexturesInCache = [framebufferTypeCounts objectForKey:lookupHash];        NSInteger numberOfMatchingTextures = [numberOfMatchingTexturesInCache integerValue];                if ([numberOfMatchingTexturesInCache integerValue] < 1)        {            // Nothing in the cache, create a new framebuffer to use            framebufferFromCache = [[GPUImageFramebuffer alloc] initWithSize:framebufferSize textureOptions:textureOptions onlyTexture:onlyTexture];        }        else        {            // Something found, pull the old framebuffer and decrement the count            NSInteger currentTextureID = (numberOfMatchingTextures - 1);            while ((framebufferFromCache == nil) && (currentTextureID >= 0))            {                NSString *textureHash = [NSString stringWithFormat:@"%@-%ld", lookupHash, (long)currentTextureID];                framebufferFromCache = [framebufferCache objectForKey:textureHash];                // Test the values in the cache first, to see if they got invalidated behind our back                if (framebufferFromCache != nil)                {                    // Withdraw this from the cache while it's in use                    [framebufferCache removeObjectForKey:textureHash];                }                currentTextureID--;            }                        currentTextureID++;                        [framebufferTypeCounts setObject:[NSNumber numberWithInteger:currentTextureID] forKey:lookupHash];                        if (framebufferFromCache == nil)            {                framebufferFromCache = [[GPUImageFramebuffer alloc] initWithSize:framebufferSize textureOptions:textureOptions onlyTexture:onlyTexture];            }        }    });    [framebufferFromCache lock];    return framebufferFromCache;}

 

 

 

-(GPUImageFramebuffer *) fetchFramebufferForSize :( CGSize) framebufferSize onlyTexture :( BOOL) onlyTexture

Note: Same as above

Implementation

 

- (GPUImageFramebuffer *)fetchFramebufferForSize:(CGSize)framebufferSize onlyTexture:(BOOL)onlyTexture;{    GPUTextureOptions defaultTextureOptions;    defaultTextureOptions.minFilter = GL_LINEAR;    defaultTextureOptions.magFilter = GL_LINEAR;    defaultTextureOptions.wrapS = GL_CLAMP_TO_EDGE;    defaultTextureOptions.wrapT = GL_CLAMP_TO_EDGE;    defaultTextureOptions.internalFormat = GL_RGBA;    defaultTextureOptions.format = GL_BGRA;    defaultTextureOptions.type = GL_UNSIGNED_BYTE;        return [self fetchFramebufferForSize:framebufferSize textureOptions:defaultTextureOptions onlyTexture:onlyTexture];}

 

-(Void) returnFramebufferToCache :( GPUImageFramebuffer *) framebuffer

Note: Add a GPUImageFramebuffer object to the GPUImageFramebuffer cache.

Parameter: The GPUImageFramebuffer object to be added.

Implementation

 

- (void)returnFramebufferToCache:(GPUImageFramebuffer *)framebuffer;{    [framebuffer clearAllLocks];    //    dispatch_async(framebufferCacheQueue, ^{    runAsynchronouslyOnVideoProcessingQueue(^{        CGSize framebufferSize = framebuffer.size;        GPUTextureOptions framebufferTextureOptions = framebuffer.textureOptions;        NSString *lookupHash = [self hashForSize:framebufferSize textureOptions:framebufferTextureOptions onlyTexture:framebuffer.missingFramebuffer];        NSNumber *numberOfMatchingTexturesInCache = [framebufferTypeCounts objectForKey:lookupHash];        NSInteger numberOfMatchingTextures = [numberOfMatchingTexturesInCache integerValue];                NSString *textureHash = [NSString stringWithFormat:@"%@-%ld", lookupHash, (long)numberOfMatchingTextures];        //        [framebufferCache setObject:framebuffer forKey:textureHash cost:round(framebufferSize.width * framebufferSize.height * 4.0)];        [framebufferCache setObject:framebuffer forKey:textureHash];        [framebufferTypeCounts setObject:[NSNumber numberWithInteger:(numberOfMatchingTextures + 1)] forKey:lookupHash];    });}

 

-(Void) purgeAllUnassignedFramebuffers

Description: clears the texture cache in the cache and clears all objects in the framebufferCache.

Implementation

 

- (void)purgeAllUnassignedFramebuffers;{    runAsynchronouslyOnVideoProcessingQueue(^{//    dispatch_async(framebufferCacheQueue, ^{        [framebufferCache removeAllObjects];        [framebufferTypeCounts removeAllObjects];#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE        CVOpenGLESTextureCacheFlush([[GPUImageContext sharedImageProcessingContext] coreVideoTextureCache], 0);#else#endif    });}

 

 

 

-(Void) addFramebufferToActiveImageCaptureList :( GPUImageFramebuffer *) framebuffer

Note:

Implementation

 

- (void)addFramebufferToActiveImageCaptureList:(GPUImageFramebuffer *)framebuffer;{    runAsynchronouslyOnVideoProcessingQueue(^{//    dispatch_async(framebufferCacheQueue, ^{        [activeImageCaptureList addObject:framebuffer];    });}

 

-(Void) removeFramebufferFromActiveImageCaptureList :( GPUImageFramebuffer *) framebuffer

Note:

Implementation

 

- (void)removeFramebufferFromActiveImageCaptureList:(GPUImageFramebuffer *)framebuffer;{    runAsynchronouslyOnVideoProcessingQueue(^{//  dispatch_async(framebufferCacheQueue, ^{        [activeImageCaptureList removeObject:framebuffer];    });}

 

 

 

Complete code

#import <Foundation/Foundation.h>#import <QuartzCore/QuartzCore.h>#import "GPUImageFramebuffer.h"@interface GPUImageFramebufferCache : NSObject// Framebuffer management- (GPUImageFramebuffer *)fetchFramebufferForSize:(CGSize)framebufferSize textureOptions:(GPUTextureOptions)textureOptions onlyTexture:(BOOL)onlyTexture;- (GPUImageFramebuffer *)fetchFramebufferForSize:(CGSize)framebufferSize onlyTexture:(BOOL)onlyTexture;- (void)returnFramebufferToCache:(GPUImageFramebuffer *)framebuffer;- (void)purgeAllUnassignedFramebuffers;- (void)addFramebufferToActiveImageCaptureList:(GPUImageFramebuffer *)framebuffer;- (void)removeFramebufferFromActiveImageCaptureList:(GPUImageFramebuffer *)framebuffer;@end

 

#import "GPUImageFramebufferCache.h"#import "GPUImageContext.h"#import "GPUImageOutput.h"#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE#import <UIKit/UIKit.h>#else#endif@interface GPUImageFramebufferCache(){//    NSCache *framebufferCache;    NSMutableDictionary *framebufferCache;    NSMutableDictionary *framebufferTypeCounts;    NSMutableArray *activeImageCaptureList; // Where framebuffers that may be lost by a filter, but which are still needed for a UIImage, etc., are stored    id memoryWarningObserver;    dispatch_queue_t framebufferCacheQueue;}- (NSString *)hashForSize:(CGSize)size textureOptions:(GPUTextureOptions)textureOptions onlyTexture:(BOOL)onlyTexture;@end@implementation GPUImageFramebufferCache#pragma mark -#pragma mark Initialization and teardown- (id)init;{    if (!(self = [super init]))    {        return nil;    }    #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE    memoryWarningObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(NSNotification *note) {                [self purgeAllUnassignedFramebuffers];    }];#else#endif//    framebufferCache = [[NSCache alloc] init];    framebufferCache = [[NSMutableDictionary alloc] init];    framebufferTypeCounts = [[NSMutableDictionary alloc] init];    activeImageCaptureList = [[NSMutableArray alloc] init];    framebufferCacheQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.framebufferCacheQueue", NULL);        return self;}#pragma mark -#pragma mark Framebuffer management- (NSString *)hashForSize:(CGSize)size textureOptions:(GPUTextureOptions)textureOptions onlyTexture:(BOOL)onlyTexture;{    if (onlyTexture)    {        return [NSString stringWithFormat:@"%.1fx%.1f-%d:%d:%d:%d:%d:%d:%d-NOFB", size.width, size.height, textureOptions.minFilter, textureOptions.magFilter, textureOptions.wrapS, textureOptions.wrapT, textureOptions.internalFormat, textureOptions.format, textureOptions.type];    }    else    {        return [NSString stringWithFormat:@"%.1fx%.1f-%d:%d:%d:%d:%d:%d:%d", size.width, size.height, textureOptions.minFilter, textureOptions.magFilter, textureOptions.wrapS, textureOptions.wrapT, textureOptions.internalFormat, textureOptions.format, textureOptions.type];    }}- (GPUImageFramebuffer *)fetchFramebufferForSize:(CGSize)framebufferSize textureOptions:(GPUTextureOptions)textureOptions onlyTexture:(BOOL)onlyTexture;{    __block GPUImageFramebuffer *framebufferFromCache = nil;//    dispatch_sync(framebufferCacheQueue, ^{    runSynchronouslyOnVideoProcessingQueue(^{        NSString *lookupHash = [self hashForSize:framebufferSize textureOptions:textureOptions onlyTexture:onlyTexture];        NSNumber *numberOfMatchingTexturesInCache = [framebufferTypeCounts objectForKey:lookupHash];        NSInteger numberOfMatchingTextures = [numberOfMatchingTexturesInCache integerValue];                if ([numberOfMatchingTexturesInCache integerValue] < 1)        {            // Nothing in the cache, create a new framebuffer to use            framebufferFromCache = [[GPUImageFramebuffer alloc] initWithSize:framebufferSize textureOptions:textureOptions onlyTexture:onlyTexture];        }        else        {            // Something found, pull the old framebuffer and decrement the count            NSInteger currentTextureID = (numberOfMatchingTextures - 1);            while ((framebufferFromCache == nil) && (currentTextureID >= 0))            {                NSString *textureHash = [NSString stringWithFormat:@"%@-%ld", lookupHash, (long)currentTextureID];                framebufferFromCache = [framebufferCache objectForKey:textureHash];                // Test the values in the cache first, to see if they got invalidated behind our back                if (framebufferFromCache != nil)                {                    // Withdraw this from the cache while it's in use                    [framebufferCache removeObjectForKey:textureHash];                }                currentTextureID--;            }                        currentTextureID++;                        [framebufferTypeCounts setObject:[NSNumber numberWithInteger:currentTextureID] forKey:lookupHash];                        if (framebufferFromCache == nil)            {                framebufferFromCache = [[GPUImageFramebuffer alloc] initWithSize:framebufferSize textureOptions:textureOptions onlyTexture:onlyTexture];            }        }    });    [framebufferFromCache lock];    return framebufferFromCache;}- (GPUImageFramebuffer *)fetchFramebufferForSize:(CGSize)framebufferSize onlyTexture:(BOOL)onlyTexture;{    GPUTextureOptions defaultTextureOptions;    defaultTextureOptions.minFilter = GL_LINEAR;    defaultTextureOptions.magFilter = GL_LINEAR;    defaultTextureOptions.wrapS = GL_CLAMP_TO_EDGE;    defaultTextureOptions.wrapT = GL_CLAMP_TO_EDGE;    defaultTextureOptions.internalFormat = GL_RGBA;    defaultTextureOptions.format = GL_BGRA;    defaultTextureOptions.type = GL_UNSIGNED_BYTE;        return [self fetchFramebufferForSize:framebufferSize textureOptions:defaultTextureOptions onlyTexture:onlyTexture];}- (void)returnFramebufferToCache:(GPUImageFramebuffer *)framebuffer;{    [framebuffer clearAllLocks];    //    dispatch_async(framebufferCacheQueue, ^{    runAsynchronouslyOnVideoProcessingQueue(^{        CGSize framebufferSize = framebuffer.size;        GPUTextureOptions framebufferTextureOptions = framebuffer.textureOptions;        NSString *lookupHash = [self hashForSize:framebufferSize textureOptions:framebufferTextureOptions onlyTexture:framebuffer.missingFramebuffer];        NSNumber *numberOfMatchingTexturesInCache = [framebufferTypeCounts objectForKey:lookupHash];        NSInteger numberOfMatchingTextures = [numberOfMatchingTexturesInCache integerValue];                NSString *textureHash = [NSString stringWithFormat:@"%@-%ld", lookupHash, (long)numberOfMatchingTextures];        //        [framebufferCache setObject:framebuffer forKey:textureHash cost:round(framebufferSize.width * framebufferSize.height * 4.0)];        [framebufferCache setObject:framebuffer forKey:textureHash];        [framebufferTypeCounts setObject:[NSNumber numberWithInteger:(numberOfMatchingTextures + 1)] forKey:lookupHash];    });}- (void)purgeAllUnassignedFramebuffers;{    runAsynchronouslyOnVideoProcessingQueue(^{//    dispatch_async(framebufferCacheQueue, ^{        [framebufferCache removeAllObjects];        [framebufferTypeCounts removeAllObjects];#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE        CVOpenGLESTextureCacheFlush([[GPUImageContext sharedImageProcessingContext] coreVideoTextureCache], 0);#else#endif    });}- (void)addFramebufferToActiveImageCaptureList:(GPUImageFramebuffer *)framebuffer;{    runAsynchronouslyOnVideoProcessingQueue(^{//    dispatch_async(framebufferCacheQueue, ^{        [activeImageCaptureList addObject:framebuffer];    });}- (void)removeFramebufferFromActiveImageCaptureList:(GPUImageFramebuffer *)framebuffer;{    runAsynchronouslyOnVideoProcessingQueue(^{//  dispatch_async(framebufferCacheQueue, ^{        [activeImageCaptureList removeObject:framebuffer];    });}@end

 

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.