GPUImageView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 green:0 blue:0 alpha:1];
  99. #pragma mark - 不要再找了,录制视频全屏显示就改这个 _fillMode
  100. _fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
  101. [self createDisplayFramebuffer];
  102. });
  103. }
  104. - (void)layoutSubviews {
  105. [super layoutSubviews];
  106. // The frame buffer needs to be trashed and re-created when the view size changes.
  107. if (!CGSizeEqualToSize(self.bounds.size, boundsSizeAtFrameBufferEpoch) &&
  108. !CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
  109. runSynchronouslyOnVideoProcessingQueue(^{
  110. [self destroyDisplayFramebuffer];
  111. [self createDisplayFramebuffer];
  112. [self recalculateViewGeometry];
  113. });
  114. }
  115. }
  116. - (void)dealloc
  117. {
  118. runSynchronouslyOnVideoProcessingQueue(^{
  119. [self destroyDisplayFramebuffer];
  120. });
  121. }
  122. #pragma mark -
  123. #pragma mark Managing the display FBOs
  124. - (void)createDisplayFramebuffer;
  125. {
  126. [GPUImageContext useImageProcessingContext];
  127. glGenFramebuffers(1, &displayFramebuffer);
  128. glBindFramebuffer(GL_FRAMEBUFFER, displayFramebuffer);
  129. glGenRenderbuffers(1, &displayRenderbuffer);
  130. glBindRenderbuffer(GL_RENDERBUFFER, displayRenderbuffer);
  131. [[[GPUImageContext sharedImageProcessingContext] context] renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)self.layer];
  132. GLint backingWidth, backingHeight;
  133. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
  134. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
  135. if ( (backingWidth == 0) || (backingHeight == 0) )
  136. {
  137. [self destroyDisplayFramebuffer];
  138. return;
  139. }
  140. #pragma mark - 调整视频录制中央显示部位的size
  141. _sizeInPixels.width = (CGFloat)backingWidth;
  142. _sizeInPixels.height = (CGFloat)backingHeight;
  143. // NSLog(@"Backing width: %d, height: %d", backingWidth, backingHeight);
  144. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, displayRenderbuffer);
  145. GLuint framebufferCreationStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  146. NSAssert(framebufferCreationStatus == GL_FRAMEBUFFER_COMPLETE, @"Failure with display framebuffer generation for display of size: %f, %f", self.bounds.size.width, self.bounds.size.height);
  147. boundsSizeAtFrameBufferEpoch = self.bounds.size;
  148. }
  149. - (void)destroyDisplayFramebuffer;
  150. {
  151. [GPUImageContext useImageProcessingContext];
  152. if (displayFramebuffer)
  153. {
  154. glDeleteFramebuffers(1, &displayFramebuffer);
  155. displayFramebuffer = 0;
  156. }
  157. if (displayRenderbuffer)
  158. {
  159. glDeleteRenderbuffers(1, &displayRenderbuffer);
  160. displayRenderbuffer = 0;
  161. }
  162. }
  163. - (void)setDisplayFramebuffer;
  164. {
  165. if (!displayFramebuffer)
  166. {
  167. [self createDisplayFramebuffer];
  168. }
  169. glBindFramebuffer(GL_FRAMEBUFFER, displayFramebuffer);
  170. glViewport(0, 0, (GLint)_sizeInPixels.width, (GLint)_sizeInPixels.height);
  171. }
  172. - (void)presentFramebuffer;
  173. {
  174. glBindRenderbuffer(GL_RENDERBUFFER, displayRenderbuffer);
  175. [[GPUImageContext sharedImageProcessingContext] presentBufferForDisplay];
  176. }
  177. #pragma mark -
  178. #pragma mark Handling fill mode
  179. - (void)recalculateViewGeometry;
  180. {
  181. runSynchronouslyOnVideoProcessingQueue(^{
  182. dispatch_async(dispatch_get_main_queue(), ^{
  183. CGFloat heightScaling, widthScaling;
  184. CGSize currentViewSize = self.bounds.size;
  185. // CGFloat imageAspectRatio = inputImageSize.width / inputImageSize.height;
  186. // CGFloat viewAspectRatio = currentViewSize.width / currentViewSize.height;
  187. CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(inputImageSize, self.bounds);
  188. switch(_fillMode)
  189. {
  190. case kGPUImageFillModeStretch:
  191. {
  192. widthScaling = 1.0;
  193. heightScaling = 1.0;
  194. }; break;
  195. case kGPUImageFillModePreserveAspectRatio:
  196. {
  197. widthScaling = insetRect.size.width / currentViewSize.width;
  198. heightScaling = insetRect.size.height / currentViewSize.height;
  199. }; break;
  200. case kGPUImageFillModePreserveAspectRatioAndFill:
  201. {
  202. // CGFloat widthHolder = insetRect.size.width / currentViewSize.width;
  203. widthScaling = currentViewSize.height / insetRect.size.height;
  204. heightScaling = currentViewSize.width / insetRect.size.width;
  205. }; break;
  206. }
  207. imageVertices[0] = -widthScaling;
  208. imageVertices[1] = -heightScaling;
  209. imageVertices[2] = widthScaling;
  210. imageVertices[3] = -heightScaling;
  211. imageVertices[4] = -widthScaling;
  212. imageVertices[5] = heightScaling;
  213. imageVertices[6] = widthScaling;
  214. imageVertices[7] = heightScaling;
  215. });
  216. });
  217. // static const GLfloat imageVertices[] = {
  218. // -1.0f, -1.0f,
  219. // 1.0f, -1.0f,
  220. // -1.0f, 1.0f,
  221. // 1.0f, 1.0f,
  222. // };
  223. }
  224. - (void)setBackgroundColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent;
  225. {
  226. backgroundColorRed = redComponent;
  227. backgroundColorGreen = greenComponent;
  228. backgroundColorBlue = blueComponent;
  229. backgroundColorAlpha = alphaComponent;
  230. }
  231. + (const GLfloat *)textureCoordinatesForRotation:(GPUImageRotationMode)rotationMode;
  232. {
  233. // static const GLfloat noRotationTextureCoordinates[] = {
  234. // 0.0f, 0.0f,
  235. // 1.0f, 0.0f,
  236. // 0.0f, 1.0f,
  237. // 1.0f, 1.0f,
  238. // };
  239. static const GLfloat noRotationTextureCoordinates[] = {
  240. 0.0f, 1.0f,
  241. 1.0f, 1.0f,
  242. 0.0f, 0.0f,
  243. 1.0f, 0.0f,
  244. };
  245. static const GLfloat rotateRightTextureCoordinates[] = {
  246. 1.0f, 1.0f,
  247. 1.0f, 0.0f,
  248. 0.0f, 1.0f,
  249. 0.0f, 0.0f,
  250. };
  251. static const GLfloat rotateLeftTextureCoordinates[] = {
  252. 0.0f, 0.0f,
  253. 0.0f, 1.0f,
  254. 1.0f, 0.0f,
  255. 1.0f, 1.0f,
  256. };
  257. static const GLfloat verticalFlipTextureCoordinates[] = {
  258. 0.0f, 0.0f,
  259. 1.0f, 0.0f,
  260. 0.0f, 1.0f,
  261. 1.0f, 1.0f,
  262. };
  263. static const GLfloat horizontalFlipTextureCoordinates[] = {
  264. 1.0f, 1.0f,
  265. 0.0f, 1.0f,
  266. 1.0f, 0.0f,
  267. 0.0f, 0.0f,
  268. };
  269. static const GLfloat rotateRightVerticalFlipTextureCoordinates[] = {
  270. 1.0f, 0.0f,
  271. 1.0f, 1.0f,
  272. 0.0f, 0.0f,
  273. 0.0f, 1.0f,
  274. };
  275. static const GLfloat rotateRightHorizontalFlipTextureCoordinates[] = {
  276. 1.0f, 1.0f,
  277. 1.0f, 0.0f,
  278. 0.0f, 1.0f,
  279. 0.0f, 0.0f,
  280. };
  281. static const GLfloat rotate180TextureCoordinates[] = {
  282. 1.0f, 0.0f,
  283. 0.0f, 0.0f,
  284. 1.0f, 1.0f,
  285. 0.0f, 1.0f,
  286. };
  287. switch(rotationMode)
  288. {
  289. case kGPUImageNoRotation: return noRotationTextureCoordinates;
  290. case kGPUImageRotateLeft: return rotateLeftTextureCoordinates;
  291. case kGPUImageRotateRight: return rotateRightTextureCoordinates;
  292. case kGPUImageFlipVertical: return verticalFlipTextureCoordinates;
  293. case kGPUImageFlipHorizonal: return horizontalFlipTextureCoordinates;
  294. case kGPUImageRotateRightFlipVertical: return rotateRightVerticalFlipTextureCoordinates;
  295. case kGPUImageRotateRightFlipHorizontal: return rotateRightHorizontalFlipTextureCoordinates;
  296. case kGPUImageRotate180: return rotate180TextureCoordinates;
  297. }
  298. }
  299. #pragma mark -
  300. #pragma mark GPUInput protocol
  301. - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
  302. {
  303. runSynchronouslyOnVideoProcessingQueue(^{
  304. [GPUImageContext setActiveShaderProgram:displayProgram];
  305. [self setDisplayFramebuffer];
  306. glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);
  307. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  308. glActiveTexture(GL_TEXTURE4);
  309. glBindTexture(GL_TEXTURE_2D, [inputFramebufferForDisplay texture]);
  310. glUniform1i(displayInputTextureUniform, 4);
  311. glVertexAttribPointer(displayPositionAttribute, 2, GL_FLOAT, 0, 0, imageVertices);
  312. glVertexAttribPointer(displayTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, [GPUImageView textureCoordinatesForRotation:inputRotation]);
  313. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  314. [self presentFramebuffer];
  315. [inputFramebufferForDisplay unlock];
  316. inputFramebufferForDisplay = nil;
  317. });
  318. }
  319. - (NSInteger)nextAvailableTextureIndex;
  320. {
  321. return 0;
  322. }
  323. - (void)setInputFramebuffer:(GPUImageFramebuffer *)newInputFramebuffer atIndex:(NSInteger)textureIndex;
  324. {
  325. inputFramebufferForDisplay = newInputFramebuffer;
  326. [inputFramebufferForDisplay lock];
  327. }
  328. - (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
  329. {
  330. inputRotation = newInputRotation;
  331. }
  332. - (void)setInputSize:(CGSize)newSize atIndex:(NSInteger)textureIndex;
  333. {
  334. runSynchronouslyOnVideoProcessingQueue(^{
  335. CGSize rotatedSize = newSize;
  336. if (GPUImageRotationSwapsWidthAndHeight(inputRotation))
  337. {
  338. rotatedSize.width = newSize.height;
  339. rotatedSize.height = newSize.width;
  340. }
  341. if (!CGSizeEqualToSize(inputImageSize, rotatedSize))
  342. {
  343. inputImageSize = rotatedSize;
  344. [self recalculateViewGeometry];
  345. }
  346. });
  347. }
  348. - (CGSize)maximumOutputSize;
  349. {
  350. if ([self respondsToSelector:@selector(setContentScaleFactor:)])
  351. {
  352. CGSize pointSize = self.bounds.size;
  353. return CGSizeMake(self.contentScaleFactor * pointSize.width, self.contentScaleFactor * pointSize.height);
  354. }
  355. else
  356. {
  357. return self.bounds.size;
  358. }
  359. }
  360. - (void)endProcessing
  361. {
  362. }
  363. - (BOOL)shouldIgnoreUpdatesToThisTarget;
  364. {
  365. return NO;
  366. }
  367. - (BOOL)wantsMonochromeInput;
  368. {
  369. return NO;
  370. }
  371. - (void)setCurrentlyReceivingMonochromeInput:(BOOL)newValue;
  372. {
  373. }
  374. #pragma mark -
  375. #pragma mark Accessors
  376. - (CGSize)sizeInPixels;
  377. {
  378. if (CGSizeEqualToSize(_sizeInPixels, CGSizeZero))
  379. {
  380. return [self maximumOutputSize];
  381. }
  382. else
  383. {
  384. return _sizeInPixels;
  385. }
  386. }
  387. - (void)setFillMode:(GPUImageFillModeType)newValue;
  388. {
  389. _fillMode = newValue;
  390. [self recalculateViewGeometry];
  391. }
  392. @end