GPUImageView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #import "GPUImageView.h"
  2. #import <OpenGLES/EAGLDrawable.h>
  3. #import <QuartzCore/QuartzCore.h>
  4. #import "GPUImageContext.h"
  5. #import "GPUImageFilter.h"
  6. #import <AVFoundation/AVFoundation.h>
  7. #pragma mark -
  8. #pragma mark Private methods and instance variables
  9. @interface GPUImageView ()
  10. {
  11. GPUImageFramebuffer *inputFramebufferForDisplay;
  12. GLuint displayRenderbuffer, displayFramebuffer;
  13. GLProgram *displayProgram;
  14. GLint displayPositionAttribute, displayTextureCoordinateAttribute;
  15. GLint displayInputTextureUniform;
  16. CGSize inputImageSize;
  17. GLfloat imageVertices[8];
  18. GLfloat backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha;
  19. CGSize boundsSizeAtFrameBufferEpoch;
  20. }
  21. @property (assign, nonatomic) NSUInteger aspectRatio;
  22. // Initialization and teardown
  23. - (void)commonInit;
  24. // Managing the display FBOs
  25. - (void)createDisplayFramebuffer;
  26. - (void)destroyDisplayFramebuffer;
  27. // Handling fill mode
  28. - (void)recalculateViewGeometry;
  29. @end
  30. @implementation GPUImageView
  31. @synthesize aspectRatio;
  32. @synthesize sizeInPixels = _sizeInPixels;
  33. @synthesize fillMode = _fillMode;
  34. @synthesize enabled;
  35. #pragma mark -
  36. #pragma mark Initialization and teardown
  37. + (Class)layerClass
  38. {
  39. return [CAEAGLLayer class];
  40. }
  41. - (id)initWithFrame:(CGRect)frame
  42. {
  43. if (!(self = [super initWithFrame:frame]))
  44. {
  45. return nil;
  46. }
  47. [self commonInit];
  48. return self;
  49. }
  50. -(id)initWithCoder:(NSCoder *)coder
  51. {
  52. if (!(self = [super initWithCoder:coder]))
  53. {
  54. return nil;
  55. }
  56. [self commonInit];
  57. return self;
  58. }
  59. - (void)commonInit;
  60. {
  61. // Set scaling to account for Retina display
  62. if ([self respondsToSelector:@selector(setContentScaleFactor:)])
  63. {
  64. self.contentScaleFactor = [[UIScreen mainScreen] scale];
  65. }
  66. inputRotation = kGPUImageNoRotation;
  67. self.opaque = YES;
  68. self.hidden = NO;
  69. CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
  70. eaglLayer.opaque = YES;
  71. eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  72. self.enabled = YES;
  73. runSynchronouslyOnVideoProcessingQueue(^{
  74. [GPUImageContext useImageProcessingContext];
  75. displayProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:kGPUImageVertexShaderString fragmentShaderString:kGPUImagePassthroughFragmentShaderString];
  76. if (!displayProgram.initialized)
  77. {
  78. [displayProgram addAttribute:@"position"];
  79. [displayProgram addAttribute:@"inputTextureCoordinate"];
  80. if (![displayProgram link])
  81. {
  82. NSString *progLog = [displayProgram programLog];
  83. NSLog(@"Program link log: %@", progLog);
  84. NSString *fragLog = [displayProgram fragmentShaderLog];
  85. NSLog(@"Fragment shader compile log: %@", fragLog);
  86. NSString *vertLog = [displayProgram vertexShaderLog];
  87. NSLog(@"Vertex shader compile log: %@", vertLog);
  88. displayProgram = nil;
  89. NSAssert(NO, @"Filter shader link failed");
  90. }
  91. }
  92. displayPositionAttribute = [displayProgram attributeIndex:@"position"];
  93. displayTextureCoordinateAttribute = [displayProgram attributeIndex:@"inputTextureCoordinate"];
  94. displayInputTextureUniform = [displayProgram uniformIndex:@"inputImageTexture"]; // This does assume a name of "inputTexture" for the fragment shader
  95. [GPUImageContext setActiveShaderProgram:displayProgram];
  96. glEnableVertexAttribArray(displayPositionAttribute);
  97. glEnableVertexAttribArray(displayTextureCoordinateAttribute);
  98. [self setBackgroundColorRed:0.0 green:0.0 blue:0.0 alpha:1.0];
  99. _fillMode = kGPUImageFillModePreserveAspectRatio;
  100. [self createDisplayFramebuffer];
  101. });
  102. }
  103. - (void)layoutSubviews {
  104. [super layoutSubviews];
  105. // The frame buffer needs to be trashed and re-created when the view size changes.
  106. if (!CGSizeEqualToSize(self.bounds.size, boundsSizeAtFrameBufferEpoch) &&
  107. !CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
  108. runSynchronouslyOnVideoProcessingQueue(^{
  109. [self destroyDisplayFramebuffer];
  110. [self createDisplayFramebuffer];
  111. [self recalculateViewGeometry];
  112. });
  113. }
  114. }
  115. - (void)dealloc
  116. {
  117. runSynchronouslyOnVideoProcessingQueue(^{
  118. [self destroyDisplayFramebuffer];
  119. });
  120. }
  121. #pragma mark -
  122. #pragma mark Managing the display FBOs
  123. - (void)createDisplayFramebuffer;
  124. {
  125. [GPUImageContext useImageProcessingContext];
  126. glGenFramebuffers(1, &displayFramebuffer);
  127. glBindFramebuffer(GL_FRAMEBUFFER, displayFramebuffer);
  128. glGenRenderbuffers(1, &displayRenderbuffer);
  129. glBindRenderbuffer(GL_RENDERBUFFER, displayRenderbuffer);
  130. [[[GPUImageContext sharedImageProcessingContext] context] renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
  131. GLint backingWidth, backingHeight;
  132. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
  133. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
  134. if ( (backingWidth == 0) || (backingHeight == 0) )
  135. {
  136. [self destroyDisplayFramebuffer];
  137. return;
  138. }
  139. _sizeInPixels.width = (CGFloat)backingWidth;
  140. _sizeInPixels.height = (CGFloat)backingHeight;
  141. // NSLog(@"Backing width: %d, height: %d", backingWidth, backingHeight);
  142. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, displayRenderbuffer);
  143. GLuint framebufferCreationStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  144. NSAssert(framebufferCreationStatus == GL_FRAMEBUFFER_COMPLETE, @"Failure with display framebuffer generation for display of size: %f, %f", self.bounds.size.width, self.bounds.size.height);
  145. boundsSizeAtFrameBufferEpoch = self.bounds.size;
  146. }
  147. - (void)destroyDisplayFramebuffer;
  148. {
  149. [GPUImageContext useImageProcessingContext];
  150. if (displayFramebuffer)
  151. {
  152. glDeleteFramebuffers(1, &displayFramebuffer);
  153. displayFramebuffer = 0;
  154. }
  155. if (displayRenderbuffer)
  156. {
  157. glDeleteRenderbuffers(1, &displayRenderbuffer);
  158. displayRenderbuffer = 0;
  159. }
  160. }
  161. - (void)setDisplayFramebuffer;
  162. {
  163. if (!displayFramebuffer)
  164. {
  165. [self createDisplayFramebuffer];
  166. }
  167. glBindFramebuffer(GL_FRAMEBUFFER, displayFramebuffer);
  168. glViewport(0, 0, (GLint)_sizeInPixels.width, (GLint)_sizeInPixels.height);
  169. }
  170. - (void)presentFramebuffer;
  171. {
  172. glBindRenderbuffer(GL_RENDERBUFFER, displayRenderbuffer);
  173. [[GPUImageContext sharedImageProcessingContext] presentBufferForDisplay];
  174. }
  175. #pragma mark -
  176. #pragma mark Handling fill mode
  177. - (void)recalculateViewGeometry;
  178. {
  179. runSynchronouslyOnVideoProcessingQueue(^{
  180. CGFloat heightScaling, widthScaling;
  181. CGSize currentViewSize = self.bounds.size;
  182. // CGFloat imageAspectRatio = inputImageSize.width / inputImageSize.height;
  183. // CGFloat viewAspectRatio = currentViewSize.width / currentViewSize.height;
  184. CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(inputImageSize, self.bounds);
  185. switch(_fillMode)
  186. {
  187. case kGPUImageFillModeStretch:
  188. {
  189. widthScaling = 1.0;
  190. heightScaling = 1.0;
  191. }; break;
  192. case kGPUImageFillModePreserveAspectRatio:
  193. {
  194. widthScaling = insetRect.size.width / currentViewSize.width;
  195. heightScaling = insetRect.size.height / currentViewSize.height;
  196. }; break;
  197. case kGPUImageFillModePreserveAspectRatioAndFill:
  198. {
  199. // CGFloat widthHolder = insetRect.size.width / currentViewSize.width;
  200. widthScaling = currentViewSize.height / insetRect.size.height;
  201. heightScaling = currentViewSize.width / insetRect.size.width;
  202. }; break;
  203. }
  204. imageVertices[0] = -widthScaling;
  205. imageVertices[1] = -heightScaling;
  206. imageVertices[2] = widthScaling;
  207. imageVertices[3] = -heightScaling;
  208. imageVertices[4] = -widthScaling;
  209. imageVertices[5] = heightScaling;
  210. imageVertices[6] = widthScaling;
  211. imageVertices[7] = heightScaling;
  212. });
  213. // static const GLfloat imageVertices[] = {
  214. // -1.0f, -1.0f,
  215. // 1.0f, -1.0f,
  216. // -1.0f, 1.0f,
  217. // 1.0f, 1.0f,
  218. // };
  219. }
  220. - (void)setBackgroundColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent;
  221. {
  222. backgroundColorRed = redComponent;
  223. backgroundColorGreen = greenComponent;
  224. backgroundColorBlue = blueComponent;
  225. backgroundColorAlpha = alphaComponent;
  226. }
  227. + (const GLfloat *)textureCoordinatesForRotation:(GPUImageRotationMode)rotationMode;
  228. {
  229. // static const GLfloat noRotationTextureCoordinates[] = {
  230. // 0.0f, 0.0f,
  231. // 1.0f, 0.0f,
  232. // 0.0f, 1.0f,
  233. // 1.0f, 1.0f,
  234. // };
  235. static const GLfloat noRotationTextureCoordinates[] = {
  236. 0.0f, 1.0f,
  237. 1.0f, 1.0f,
  238. 0.0f, 0.0f,
  239. 1.0f, 0.0f,
  240. };
  241. static const GLfloat rotateRightTextureCoordinates[] = {
  242. 1.0f, 1.0f,
  243. 1.0f, 0.0f,
  244. 0.0f, 1.0f,
  245. 0.0f, 0.0f,
  246. };
  247. static const GLfloat rotateLeftTextureCoordinates[] = {
  248. 0.0f, 0.0f,
  249. 0.0f, 1.0f,
  250. 1.0f, 0.0f,
  251. 1.0f, 1.0f,
  252. };
  253. static const GLfloat verticalFlipTextureCoordinates[] = {
  254. 0.0f, 0.0f,
  255. 1.0f, 0.0f,
  256. 0.0f, 1.0f,
  257. 1.0f, 1.0f,
  258. };
  259. static const GLfloat horizontalFlipTextureCoordinates[] = {
  260. 1.0f, 1.0f,
  261. 0.0f, 1.0f,
  262. 1.0f, 0.0f,
  263. 0.0f, 0.0f,
  264. };
  265. static const GLfloat rotateRightVerticalFlipTextureCoordinates[] = {
  266. 1.0f, 0.0f,
  267. 1.0f, 1.0f,
  268. 0.0f, 0.0f,
  269. 0.0f, 1.0f,
  270. };
  271. static const GLfloat rotateRightHorizontalFlipTextureCoordinates[] = {
  272. 1.0f, 1.0f,
  273. 1.0f, 0.0f,
  274. 0.0f, 1.0f,
  275. 0.0f, 0.0f,
  276. };
  277. static const GLfloat rotate180TextureCoordinates[] = {
  278. 1.0f, 0.0f,
  279. 0.0f, 0.0f,
  280. 1.0f, 1.0f,
  281. 0.0f, 1.0f,
  282. };
  283. switch(rotationMode)
  284. {
  285. case kGPUImageNoRotation: return noRotationTextureCoordinates;
  286. case kGPUImageRotateLeft: return rotateLeftTextureCoordinates;
  287. case kGPUImageRotateRight: return rotateRightTextureCoordinates;
  288. case kGPUImageFlipVertical: return verticalFlipTextureCoordinates;
  289. case kGPUImageFlipHorizonal: return horizontalFlipTextureCoordinates;
  290. case kGPUImageRotateRightFlipVertical: return rotateRightVerticalFlipTextureCoordinates;
  291. case kGPUImageRotateRightFlipHorizontal: return rotateRightHorizontalFlipTextureCoordinates;
  292. case kGPUImageRotate180: return rotate180TextureCoordinates;
  293. }
  294. }
  295. #pragma mark -
  296. #pragma mark GPUInput protocol
  297. - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
  298. {
  299. runSynchronouslyOnVideoProcessingQueue(^{
  300. [GPUImageContext setActiveShaderProgram:displayProgram];
  301. [self setDisplayFramebuffer];
  302. glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);
  303. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  304. glActiveTexture(GL_TEXTURE4);
  305. glBindTexture(GL_TEXTURE_2D, [inputFramebufferForDisplay texture]);
  306. glUniform1i(displayInputTextureUniform, 4);
  307. glVertexAttribPointer(displayPositionAttribute, 2, GL_FLOAT, 0, 0, imageVertices);
  308. glVertexAttribPointer(displayTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [GPUImageView textureCoordinatesForRotation:inputRotation]);
  309. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  310. [self presentFramebuffer];
  311. [inputFramebufferForDisplay unlock];
  312. inputFramebufferForDisplay = nil;
  313. });
  314. }
  315. - (NSInteger)nextAvailableTextureIndex;
  316. {
  317. return 0;
  318. }
  319. - (void)setInputFramebuffer:(GPUImageFramebuffer *)newInputFramebuffer atIndex:(NSInteger)textureIndex;
  320. {
  321. inputFramebufferForDisplay = newInputFramebuffer;
  322. [inputFramebufferForDisplay lock];
  323. }
  324. - (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
  325. {
  326. inputRotation = newInputRotation;
  327. }
  328. - (void)setInputSize:(CGSize)newSize atIndex:(NSInteger)textureIndex;
  329. {
  330. runSynchronouslyOnVideoProcessingQueue(^{
  331. CGSize rotatedSize = newSize;
  332. if (GPUImageRotationSwapsWidthAndHeight(inputRotation))
  333. {
  334. rotatedSize.width = newSize.height;
  335. rotatedSize.height = newSize.width;
  336. }
  337. if (!CGSizeEqualToSize(inputImageSize, rotatedSize))
  338. {
  339. inputImageSize = rotatedSize;
  340. [self recalculateViewGeometry];
  341. }
  342. });
  343. }
  344. - (CGSize)maximumOutputSize;
  345. {
  346. if ([self respondsToSelector:@selector(setContentScaleFactor:)])
  347. {
  348. CGSize pointSize = self.bounds.size;
  349. return CGSizeMake(self.contentScaleFactor * pointSize.width, self.contentScaleFactor * pointSize.height);
  350. }
  351. else
  352. {
  353. return self.bounds.size;
  354. }
  355. }
  356. - (void)endProcessing
  357. {
  358. }
  359. - (BOOL)shouldIgnoreUpdatesToThisTarget;
  360. {
  361. return NO;
  362. }
  363. - (BOOL)wantsMonochromeInput;
  364. {
  365. return NO;
  366. }
  367. - (void)setCurrentlyReceivingMonochromeInput:(BOOL)newValue;
  368. {
  369. }
  370. #pragma mark -
  371. #pragma mark Accessors
  372. - (CGSize)sizeInPixels;
  373. {
  374. if (CGSizeEqualToSize(_sizeInPixels, CGSizeZero))
  375. {
  376. return [self maximumOutputSize];
  377. }
  378. else
  379. {
  380. return _sizeInPixels;
  381. }
  382. }
  383. - (void)setFillMode:(GPUImageFillModeType)newValue;
  384. {
  385. _fillMode = newValue;
  386. [self recalculateViewGeometry];
  387. }
  388. @end