GPUImageContext.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #import "GPUImageContext.h"
  2. #import <OpenGLES/EAGLDrawable.h>
  3. #import <AVFoundation/AVFoundation.h>
  4. #define MAXSHADERPROGRAMSALLOWEDINCACHE 40
  5. @interface GPUImageContext()
  6. {
  7. NSMutableDictionary *shaderProgramCache;
  8. NSMutableArray *shaderProgramUsageHistory;
  9. EAGLSharegroup *_sharegroup;
  10. }
  11. @end
  12. @implementation GPUImageContext
  13. @synthesize context = _context;
  14. @synthesize currentShaderProgram = _currentShaderProgram;
  15. @synthesize contextQueue = _contextQueue;
  16. @synthesize coreVideoTextureCache = _coreVideoTextureCache;
  17. @synthesize framebufferCache = _framebufferCache;
  18. static void *openGLESContextQueueKey;
  19. - (id)init;
  20. {
  21. if (!(self = [super init]))
  22. {
  23. return nil;
  24. }
  25. openGLESContextQueueKey = &openGLESContextQueueKey;
  26. _contextQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.openGLESContextQueue", NULL);
  27. #if (!defined(__IPHONE_6_0) || (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0))
  28. #else
  29. dispatch_queue_set_specific(_contextQueue, openGLESContextQueueKey, (__bridge void *)self, NULL);
  30. #endif
  31. shaderProgramCache = [[NSMutableDictionary alloc] init];
  32. shaderProgramUsageHistory = [[NSMutableArray alloc] init];
  33. return self;
  34. }
  35. + (void *)contextKey {
  36. return openGLESContextQueueKey;
  37. }
  38. // Based on Colin Wheeler's example here: http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html
  39. + (GPUImageContext *)sharedImageProcessingContext;
  40. {
  41. static dispatch_once_t pred;
  42. static GPUImageContext *sharedImageProcessingContext = nil;
  43. dispatch_once(&pred, ^{
  44. sharedImageProcessingContext = [[[self class] alloc] init];
  45. });
  46. return sharedImageProcessingContext;
  47. }
  48. + (dispatch_queue_t)sharedContextQueue;
  49. {
  50. return [[self sharedImageProcessingContext] contextQueue];
  51. }
  52. + (GPUImageFramebufferCache *)sharedFramebufferCache;
  53. {
  54. return [[self sharedImageProcessingContext] framebufferCache];
  55. }
  56. + (void)useImageProcessingContext;
  57. {
  58. [[GPUImageContext sharedImageProcessingContext] useAsCurrentContext];
  59. }
  60. - (void)useAsCurrentContext;
  61. {
  62. EAGLContext *imageProcessingContext = [self context];
  63. if ([EAGLContext currentContext] != imageProcessingContext)
  64. {
  65. [EAGLContext setCurrentContext:imageProcessingContext];
  66. }
  67. }
  68. + (void)setActiveShaderProgram:(GLProgram *)shaderProgram;
  69. {
  70. GPUImageContext *sharedContext = [GPUImageContext sharedImageProcessingContext];
  71. [sharedContext setContextShaderProgram:shaderProgram];
  72. }
  73. - (void)setContextShaderProgram:(GLProgram *)shaderProgram;
  74. {
  75. EAGLContext *imageProcessingContext = [self context];
  76. if ([EAGLContext currentContext] != imageProcessingContext)
  77. {
  78. [EAGLContext setCurrentContext:imageProcessingContext];
  79. }
  80. if (self.currentShaderProgram != shaderProgram)
  81. {
  82. self.currentShaderProgram = shaderProgram;
  83. [shaderProgram use];
  84. }
  85. }
  86. + (GLint)maximumTextureSizeForThisDevice;
  87. {
  88. static dispatch_once_t pred;
  89. static GLint maxTextureSize = 0;
  90. dispatch_once(&pred, ^{
  91. [self useImageProcessingContext];
  92. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
  93. });
  94. return maxTextureSize;
  95. }
  96. + (GLint)maximumTextureUnitsForThisDevice;
  97. {
  98. static dispatch_once_t pred;
  99. static GLint maxTextureUnits = 0;
  100. dispatch_once(&pred, ^{
  101. [self useImageProcessingContext];
  102. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
  103. });
  104. return maxTextureUnits;
  105. }
  106. + (GLint)maximumVaryingVectorsForThisDevice;
  107. {
  108. static dispatch_once_t pred;
  109. static GLint maxVaryingVectors = 0;
  110. dispatch_once(&pred, ^{
  111. [self useImageProcessingContext];
  112. glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryingVectors);
  113. });
  114. return maxVaryingVectors;
  115. }
  116. + (BOOL)deviceSupportsOpenGLESExtension:(NSString *)extension;
  117. {
  118. static dispatch_once_t pred;
  119. static NSArray *extensionNames = nil;
  120. // Cache extensions for later quick reference, since this won't change for a given device
  121. dispatch_once(&pred, ^{
  122. [GPUImageContext useImageProcessingContext];
  123. NSString *extensionsString = [NSString stringWithCString:(const char *)glGetString(GL_EXTENSIONS) encoding:NSASCIIStringEncoding];
  124. extensionNames = [extensionsString componentsSeparatedByString:@" "];
  125. });
  126. return [extensionNames containsObject:extension];
  127. }
  128. // http://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_rg.txt
  129. + (BOOL)deviceSupportsRedTextures;
  130. {
  131. static dispatch_once_t pred;
  132. static BOOL supportsRedTextures = NO;
  133. dispatch_once(&pred, ^{
  134. supportsRedTextures = [GPUImageContext deviceSupportsOpenGLESExtension:@"GL_EXT_texture_rg"];
  135. });
  136. return supportsRedTextures;
  137. }
  138. + (BOOL)deviceSupportsFramebufferReads;
  139. {
  140. static dispatch_once_t pred;
  141. static BOOL supportsFramebufferReads = NO;
  142. dispatch_once(&pred, ^{
  143. supportsFramebufferReads = [GPUImageContext deviceSupportsOpenGLESExtension:@"GL_EXT_shader_framebuffer_fetch"];
  144. });
  145. return supportsFramebufferReads;
  146. }
  147. + (CGSize)sizeThatFitsWithinATextureForSize:(CGSize)inputSize;
  148. {
  149. GLint maxTextureSize = [self maximumTextureSizeForThisDevice];
  150. if ( (inputSize.width < maxTextureSize) && (inputSize.height < maxTextureSize) )
  151. {
  152. return inputSize;
  153. }
  154. CGSize adjustedSize;
  155. if (inputSize.width > inputSize.height)
  156. {
  157. adjustedSize.width = (CGFloat)maxTextureSize;
  158. adjustedSize.height = ((CGFloat)maxTextureSize / inputSize.width) * inputSize.height;
  159. }
  160. else
  161. {
  162. adjustedSize.height = (CGFloat)maxTextureSize;
  163. adjustedSize.width = ((CGFloat)maxTextureSize / inputSize.height) * inputSize.width;
  164. }
  165. return adjustedSize;
  166. }
  167. - (void)presentBufferForDisplay;
  168. {
  169. [self.context presentRenderbuffer:GL_RENDERBUFFER];
  170. }
  171. - (GLProgram *)programForVertexShaderString:(NSString *)vertexShaderString fragmentShaderString:(NSString *)fragmentShaderString;
  172. {
  173. NSString *lookupKeyForShaderProgram = [NSString stringWithFormat:@"V: %@ - F: %@", vertexShaderString, fragmentShaderString];
  174. GLProgram *programFromCache = [shaderProgramCache objectForKey:lookupKeyForShaderProgram];
  175. if (programFromCache == nil)
  176. {
  177. programFromCache = [[GLProgram alloc] initWithVertexShaderString:vertexShaderString fragmentShaderString:fragmentShaderString];
  178. [shaderProgramCache setObject:programFromCache forKey:lookupKeyForShaderProgram];
  179. // [shaderProgramUsageHistory addObject:lookupKeyForShaderProgram];
  180. // if ([shaderProgramUsageHistory count] >= MAXSHADERPROGRAMSALLOWEDINCACHE)
  181. // {
  182. // for (NSUInteger currentShaderProgramRemovedFromCache = 0; currentShaderProgramRemovedFromCache < 10; currentShaderProgramRemovedFromCache++)
  183. // {
  184. // NSString *shaderProgramToRemoveFromCache = [shaderProgramUsageHistory objectAtIndex:0];
  185. // [shaderProgramUsageHistory removeObjectAtIndex:0];
  186. // [shaderProgramCache removeObjectForKey:shaderProgramToRemoveFromCache];
  187. // }
  188. // }
  189. }
  190. return programFromCache;
  191. }
  192. - (void)useSharegroup:(EAGLSharegroup *)sharegroup;
  193. {
  194. 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.");
  195. _sharegroup = sharegroup;
  196. }
  197. - (EAGLContext *)createContext;
  198. {
  199. EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:_sharegroup];
  200. NSAssert(context != nil, @"Unable to create an OpenGL ES 2.0 context. The GPUImage framework requires OpenGL ES 2.0 support to work.");
  201. return context;
  202. }
  203. #pragma mark -
  204. #pragma mark Manage fast texture upload
  205. + (BOOL)supportsFastTextureUpload;
  206. {
  207. #if TARGET_IPHONE_SIMULATOR
  208. return NO;
  209. #else
  210. return (CVOpenGLESTextureCacheCreate != NULL);
  211. #endif
  212. }
  213. #pragma mark -
  214. #pragma mark Accessors
  215. - (EAGLContext *)context;
  216. {
  217. if (_context == nil)
  218. {
  219. _context = [self createContext];
  220. [EAGLContext setCurrentContext:_context];
  221. // Set up a few global settings for the image processing pipeline
  222. glDisable(GL_DEPTH_TEST);
  223. }
  224. return _context;
  225. }
  226. - (CVOpenGLESTextureCacheRef)coreVideoTextureCache;
  227. {
  228. if (_coreVideoTextureCache == NULL)
  229. {
  230. #if defined(__IPHONE_6_0)
  231. CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, [self context], NULL, &_coreVideoTextureCache);
  232. #else
  233. CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[self context], NULL, &_coreVideoTextureCache);
  234. #endif
  235. if (err)
  236. {
  237. NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d", err);
  238. }
  239. }
  240. return _coreVideoTextureCache;
  241. }
  242. - (GPUImageFramebufferCache *)framebufferCache;
  243. {
  244. if (_framebufferCache == nil)
  245. {
  246. _framebufferCache = [[GPUImageFramebufferCache alloc] init];
  247. }
  248. return _framebufferCache;
  249. }
  250. @end