GPUImageContext.m 8.9 KB

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