PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Dependencies/GPUImage/Source/GPUImageFilter.m

https://gitlab.com/Mr.Tomato/VideoEffects
Objective C | 755 lines | 598 code | 129 blank | 28 comment | 38 complexity | 3c6eeadbda31c05e4960fc394780a3f4 MD5 | raw file
  1. #import "GPUImageFilter.h"
  2. #import "GPUImagePicture.h"
  3. #import <AVFoundation/AVFoundation.h>
  4. // Hardcode the vertex shader for standard filters, but this can be overridden
  5. NSString *const kGPUImageVertexShaderString = SHADER_STRING
  6. (
  7. attribute vec4 position;
  8. attribute vec4 inputTextureCoordinate;
  9. varying vec2 textureCoordinate;
  10. void main()
  11. {
  12. gl_Position = position;
  13. textureCoordinate = inputTextureCoordinate.xy;
  14. }
  15. );
  16. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  17. NSString *const kGPUImagePassthroughFragmentShaderString = SHADER_STRING
  18. (
  19. varying highp vec2 textureCoordinate;
  20. uniform sampler2D inputImageTexture;
  21. void main()
  22. {
  23. gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
  24. }
  25. );
  26. #else
  27. NSString *const kGPUImagePassthroughFragmentShaderString = SHADER_STRING
  28. (
  29. varying vec2 textureCoordinate;
  30. uniform sampler2D inputImageTexture;
  31. void main()
  32. {
  33. gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
  34. }
  35. );
  36. #endif
  37. @implementation GPUImageFilter
  38. @synthesize preventRendering = _preventRendering;
  39. @synthesize currentlyReceivingMonochromeInput;
  40. #pragma mark -
  41. #pragma mark Initialization and teardown
  42. - (id)initWithVertexShaderFromString:(NSString *)vertexShaderString fragmentShaderFromString:(NSString *)fragmentShaderString;
  43. {
  44. if (!(self = [super init]))
  45. {
  46. return nil;
  47. }
  48. uniformStateRestorationBlocks = [NSMutableDictionary dictionaryWithCapacity:10];
  49. _preventRendering = NO;
  50. currentlyReceivingMonochromeInput = NO;
  51. inputRotation = kGPUImageNoRotation;
  52. backgroundColorRed = 0.0;
  53. backgroundColorGreen = 0.0;
  54. backgroundColorBlue = 0.0;
  55. backgroundColorAlpha = 0.0;
  56. imageCaptureSemaphore = dispatch_semaphore_create(0);
  57. dispatch_semaphore_signal(imageCaptureSemaphore);
  58. runSynchronouslyOnVideoProcessingQueue(^{
  59. [GPUImageContext useImageProcessingContext];
  60. filterProgram = [[GPUImageContext sharedImageProcessingContext] programForVertexShaderString:vertexShaderString fragmentShaderString:fragmentShaderString];
  61. if (!filterProgram.initialized)
  62. {
  63. [self initializeAttributes];
  64. if (![filterProgram link])
  65. {
  66. NSString *progLog = [filterProgram programLog];
  67. NSLog(@"Program link log: %@", progLog);
  68. NSString *fragLog = [filterProgram fragmentShaderLog];
  69. NSLog(@"Fragment shader compile log: %@", fragLog);
  70. NSString *vertLog = [filterProgram vertexShaderLog];
  71. NSLog(@"Vertex shader compile log: %@", vertLog);
  72. filterProgram = nil;
  73. NSAssert(NO, @"Filter shader link failed");
  74. }
  75. }
  76. filterPositionAttribute = [filterProgram attributeIndex:@"position"];
  77. filterTextureCoordinateAttribute = [filterProgram attributeIndex:@"inputTextureCoordinate"];
  78. filterInputTextureUniform = [filterProgram uniformIndex:@"inputImageTexture"]; // This does assume a name of "inputImageTexture" for the fragment shader
  79. [GPUImageContext setActiveShaderProgram:filterProgram];
  80. glEnableVertexAttribArray(filterPositionAttribute);
  81. glEnableVertexAttribArray(filterTextureCoordinateAttribute);
  82. });
  83. return self;
  84. }
  85. - (id)initWithFragmentShaderFromString:(NSString *)fragmentShaderString;
  86. {
  87. if (!(self = [self initWithVertexShaderFromString:kGPUImageVertexShaderString fragmentShaderFromString:fragmentShaderString]))
  88. {
  89. return nil;
  90. }
  91. return self;
  92. }
  93. - (id)initWithFragmentShaderFromFile:(NSString *)fragmentShaderFilename;
  94. {
  95. NSString *fragmentShaderPathname = [[NSBundle mainBundle] pathForResource:fragmentShaderFilename ofType:@"fsh"];
  96. NSString *fragmentShaderString = [NSString stringWithContentsOfFile:fragmentShaderPathname encoding:NSUTF8StringEncoding error:nil];
  97. if (!(self = [self initWithFragmentShaderFromString:fragmentShaderString]))
  98. {
  99. return nil;
  100. }
  101. return self;
  102. }
  103. - (id)init;
  104. {
  105. if (!(self = [self initWithFragmentShaderFromString:kGPUImagePassthroughFragmentShaderString]))
  106. {
  107. return nil;
  108. }
  109. return self;
  110. }
  111. - (void)initializeAttributes;
  112. {
  113. [filterProgram addAttribute:@"position"];
  114. [filterProgram addAttribute:@"inputTextureCoordinate"];
  115. // Override this, calling back to this super method, in order to add new attributes to your vertex shader
  116. }
  117. - (void)setupFilterForSize:(CGSize)filterFrameSize;
  118. {
  119. // This is where you can override to provide some custom setup, if your filter has a size-dependent element
  120. }
  121. - (void)dealloc
  122. {
  123. #if ( (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0) || (!defined(__IPHONE_6_0)) )
  124. if (imageCaptureSemaphore != NULL)
  125. {
  126. dispatch_release(imageCaptureSemaphore);
  127. }
  128. #endif
  129. }
  130. #pragma mark -
  131. #pragma mark Still image processing
  132. - (void)useNextFrameForImageCapture;
  133. {
  134. usingNextFrameForImageCapture = YES;
  135. // Set the semaphore high, if it isn't already
  136. if (dispatch_semaphore_wait(imageCaptureSemaphore, DISPATCH_TIME_NOW) != 0)
  137. {
  138. return;
  139. }
  140. }
  141. - (CGImageRef)newCGImageFromCurrentlyProcessedOutput
  142. {
  143. // Give it three seconds to process, then abort if they forgot to set up the image capture properly
  144. double timeoutForImageCapture = 3.0;
  145. dispatch_time_t convertedTimeout = dispatch_time(DISPATCH_TIME_NOW, timeoutForImageCapture * NSEC_PER_SEC);
  146. if (dispatch_semaphore_wait(imageCaptureSemaphore, convertedTimeout) != 0)
  147. {
  148. return NULL;
  149. }
  150. GPUImageFramebuffer* framebuffer = [self framebufferForOutput];
  151. usingNextFrameForImageCapture = NO;
  152. dispatch_semaphore_signal(imageCaptureSemaphore);
  153. // All image output is now managed by the framebuffer itself
  154. // return [[self framebufferForOutput] newCGImageFromFramebufferContents];
  155. CGImageRef image = [framebuffer newCGImageFromFramebufferContents];
  156. return image;
  157. }
  158. #pragma mark -
  159. #pragma mark Managing the display FBOs
  160. - (CGSize)sizeOfFBO;
  161. {
  162. CGSize outputSize = [self maximumOutputSize];
  163. if ( (CGSizeEqualToSize(outputSize, CGSizeZero)) || (inputTextureSize.width < outputSize.width) )
  164. {
  165. return inputTextureSize;
  166. }
  167. else
  168. {
  169. return outputSize;
  170. }
  171. }
  172. #pragma mark -
  173. #pragma mark Rendering
  174. + (const GLfloat *)textureCoordinatesForRotation:(GPUImageRotationMode)rotationMode;
  175. {
  176. static const GLfloat noRotationTextureCoordinates[] = {
  177. 0.0f, 0.0f,
  178. 1.0f, 0.0f,
  179. 0.0f, 1.0f,
  180. 1.0f, 1.0f,
  181. };
  182. static const GLfloat rotateLeftTextureCoordinates[] = {
  183. 1.0f, 0.0f,
  184. 1.0f, 1.0f,
  185. 0.0f, 0.0f,
  186. 0.0f, 1.0f,
  187. };
  188. static const GLfloat rotateRightTextureCoordinates[] = {
  189. 0.0f, 1.0f,
  190. 0.0f, 0.0f,
  191. 1.0f, 1.0f,
  192. 1.0f, 0.0f,
  193. };
  194. static const GLfloat verticalFlipTextureCoordinates[] = {
  195. 0.0f, 1.0f,
  196. 1.0f, 1.0f,
  197. 0.0f, 0.0f,
  198. 1.0f, 0.0f,
  199. };
  200. static const GLfloat horizontalFlipTextureCoordinates[] = {
  201. 1.0f, 0.0f,
  202. 0.0f, 0.0f,
  203. 1.0f, 1.0f,
  204. 0.0f, 1.0f,
  205. };
  206. static const GLfloat rotateRightVerticalFlipTextureCoordinates[] = {
  207. 0.0f, 0.0f,
  208. 0.0f, 1.0f,
  209. 1.0f, 0.0f,
  210. 1.0f, 1.0f,
  211. };
  212. static const GLfloat rotateRightHorizontalFlipTextureCoordinates[] = {
  213. 1.0f, 1.0f,
  214. 1.0f, 0.0f,
  215. 0.0f, 1.0f,
  216. 0.0f, 0.0f,
  217. };
  218. static const GLfloat rotate180TextureCoordinates[] = {
  219. 1.0f, 1.0f,
  220. 0.0f, 1.0f,
  221. 1.0f, 0.0f,
  222. 0.0f, 0.0f,
  223. };
  224. switch(rotationMode)
  225. {
  226. case kGPUImageNoRotation: return noRotationTextureCoordinates;
  227. case kGPUImageRotateLeft: return rotateLeftTextureCoordinates;
  228. case kGPUImageRotateRight: return rotateRightTextureCoordinates;
  229. case kGPUImageFlipVertical: return verticalFlipTextureCoordinates;
  230. case kGPUImageFlipHorizonal: return horizontalFlipTextureCoordinates;
  231. case kGPUImageRotateRightFlipVertical: return rotateRightVerticalFlipTextureCoordinates;
  232. case kGPUImageRotateRightFlipHorizontal: return rotateRightHorizontalFlipTextureCoordinates;
  233. case kGPUImageRotate180: return rotate180TextureCoordinates;
  234. }
  235. }
  236. - (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
  237. {
  238. if (self.preventRendering)
  239. {
  240. [firstInputFramebuffer unlock];
  241. return;
  242. }
  243. [GPUImageContext setActiveShaderProgram:filterProgram];
  244. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO];
  245. [outputFramebuffer activateFramebuffer];
  246. if (usingNextFrameForImageCapture)
  247. {
  248. [outputFramebuffer lock];
  249. }
  250. [self setUniformsForProgramAtIndex:0];
  251. glClearColor(backgroundColorRed, backgroundColorGreen, backgroundColorBlue, backgroundColorAlpha);
  252. glClear(GL_COLOR_BUFFER_BIT);
  253. glActiveTexture(GL_TEXTURE2);
  254. glBindTexture(GL_TEXTURE_2D, [firstInputFramebuffer texture]);
  255. glUniform1i(filterInputTextureUniform, 2);
  256. glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, vertices);
  257. glVertexAttribPointer(filterTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, textureCoordinates);
  258. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  259. [firstInputFramebuffer unlock];
  260. if (usingNextFrameForImageCapture)
  261. {
  262. dispatch_semaphore_signal(imageCaptureSemaphore);
  263. }
  264. }
  265. - (void)informTargetsAboutNewFrameAtTime:(CMTime)frameTime;
  266. {
  267. if (self.frameProcessingCompletionBlock != NULL)
  268. {
  269. self.frameProcessingCompletionBlock(self, frameTime);
  270. }
  271. // Get all targets the framebuffer so they can grab a lock on it
  272. for (id<GPUImageInput> currentTarget in targets)
  273. {
  274. if (currentTarget != self.targetToIgnoreForUpdates)
  275. {
  276. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  277. NSInteger textureIndex = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  278. [self setInputFramebufferForTarget:currentTarget atIndex:textureIndex];
  279. [currentTarget setInputSize:[self outputFrameSize] atIndex:textureIndex];
  280. }
  281. }
  282. // Release our hold so it can return to the cache immediately upon processing
  283. [[self framebufferForOutput] unlock];
  284. if (usingNextFrameForImageCapture)
  285. {
  286. // usingNextFrameForImageCapture = NO;
  287. }
  288. else
  289. {
  290. [self removeOutputFramebuffer];
  291. }
  292. // Trigger processing last, so that our unlock comes first in serial execution, avoiding the need for a callback
  293. for (id<GPUImageInput> currentTarget in targets)
  294. {
  295. if (currentTarget != self.targetToIgnoreForUpdates)
  296. {
  297. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  298. NSInteger textureIndex = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  299. [currentTarget newFrameReadyAtTime:frameTime atIndex:textureIndex];
  300. }
  301. }
  302. }
  303. - (CGSize)outputFrameSize;
  304. {
  305. return inputTextureSize;
  306. }
  307. #pragma mark -
  308. #pragma mark Input parameters
  309. - (void)setBackgroundColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent;
  310. {
  311. backgroundColorRed = redComponent;
  312. backgroundColorGreen = greenComponent;
  313. backgroundColorBlue = blueComponent;
  314. backgroundColorAlpha = alphaComponent;
  315. }
  316. - (void)setInteger:(GLint)newInteger forUniformName:(NSString *)uniformName;
  317. {
  318. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  319. [self setInteger:newInteger forUniform:uniformIndex program:filterProgram];
  320. }
  321. - (void)setFloat:(GLfloat)newFloat forUniformName:(NSString *)uniformName;
  322. {
  323. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  324. [self setFloat:newFloat forUniform:uniformIndex program:filterProgram];
  325. }
  326. - (void)setSize:(CGSize)newSize forUniformName:(NSString *)uniformName;
  327. {
  328. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  329. [self setSize:newSize forUniform:uniformIndex program:filterProgram];
  330. }
  331. - (void)setPoint:(CGPoint)newPoint forUniformName:(NSString *)uniformName;
  332. {
  333. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  334. [self setPoint:newPoint forUniform:uniformIndex program:filterProgram];
  335. }
  336. - (void)setFloatVec3:(GPUVector3)newVec3 forUniformName:(NSString *)uniformName;
  337. {
  338. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  339. [self setVec3:newVec3 forUniform:uniformIndex program:filterProgram];
  340. }
  341. - (void)setFloatVec4:(GPUVector4)newVec4 forUniform:(NSString *)uniformName;
  342. {
  343. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  344. [self setVec4:newVec4 forUniform:uniformIndex program:filterProgram];
  345. }
  346. - (void)setFloatArray:(GLfloat *)array length:(GLsizei)count forUniform:(NSString*)uniformName
  347. {
  348. GLint uniformIndex = [filterProgram uniformIndex:uniformName];
  349. [self setFloatArray:array length:count forUniform:uniformIndex program:filterProgram];
  350. }
  351. - (void)setMatrix3f:(GPUMatrix3x3)matrix forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  352. {
  353. runAsynchronouslyOnVideoProcessingQueue(^{
  354. [GPUImageContext setActiveShaderProgram:shaderProgram];
  355. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  356. glUniformMatrix3fv(uniform, 1, GL_FALSE, (GLfloat *)&matrix);
  357. }];
  358. });
  359. }
  360. - (void)setMatrix4f:(GPUMatrix4x4)matrix forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  361. {
  362. runAsynchronouslyOnVideoProcessingQueue(^{
  363. [GPUImageContext setActiveShaderProgram:shaderProgram];
  364. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  365. glUniformMatrix4fv(uniform, 1, GL_FALSE, (GLfloat *)&matrix);
  366. }];
  367. });
  368. }
  369. - (void)setFloat:(GLfloat)floatValue forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  370. {
  371. runAsynchronouslyOnVideoProcessingQueue(^{
  372. [GPUImageContext setActiveShaderProgram:shaderProgram];
  373. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  374. glUniform1f(uniform, floatValue);
  375. }];
  376. });
  377. }
  378. - (void)setPoint:(CGPoint)pointValue forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  379. {
  380. runAsynchronouslyOnVideoProcessingQueue(^{
  381. [GPUImageContext setActiveShaderProgram:shaderProgram];
  382. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  383. GLfloat positionArray[2];
  384. positionArray[0] = pointValue.x;
  385. positionArray[1] = pointValue.y;
  386. glUniform2fv(uniform, 1, positionArray);
  387. }];
  388. });
  389. }
  390. - (void)setSize:(CGSize)sizeValue forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  391. {
  392. runAsynchronouslyOnVideoProcessingQueue(^{
  393. [GPUImageContext setActiveShaderProgram:shaderProgram];
  394. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  395. GLfloat sizeArray[2];
  396. sizeArray[0] = sizeValue.width;
  397. sizeArray[1] = sizeValue.height;
  398. glUniform2fv(uniform, 1, sizeArray);
  399. }];
  400. });
  401. }
  402. - (void)setVec3:(GPUVector3)vectorValue forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  403. {
  404. runAsynchronouslyOnVideoProcessingQueue(^{
  405. [GPUImageContext setActiveShaderProgram:shaderProgram];
  406. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  407. glUniform3fv(uniform, 1, (GLfloat *)&vectorValue);
  408. }];
  409. });
  410. }
  411. - (void)setVec4:(GPUVector4)vectorValue forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  412. {
  413. runAsynchronouslyOnVideoProcessingQueue(^{
  414. [GPUImageContext setActiveShaderProgram:shaderProgram];
  415. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  416. glUniform4fv(uniform, 1, (GLfloat *)&vectorValue);
  417. }];
  418. });
  419. }
  420. - (void)setFloatArray:(GLfloat *)arrayValue length:(GLsizei)arrayLength forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  421. {
  422. // Make a copy of the data, so it doesn't get overwritten before async call executes
  423. NSData* arrayData = [NSData dataWithBytes:arrayValue length:arrayLength * sizeof(arrayValue[0])];
  424. runAsynchronouslyOnVideoProcessingQueue(^{
  425. [GPUImageContext setActiveShaderProgram:shaderProgram];
  426. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  427. glUniform1fv(uniform, arrayLength, [arrayData bytes]);
  428. }];
  429. });
  430. }
  431. - (void)setInteger:(GLint)intValue forUniform:(GLint)uniform program:(GLProgram *)shaderProgram;
  432. {
  433. runAsynchronouslyOnVideoProcessingQueue(^{
  434. [GPUImageContext setActiveShaderProgram:shaderProgram];
  435. [self setAndExecuteUniformStateCallbackAtIndex:uniform forProgram:shaderProgram toBlock:^{
  436. glUniform1i(uniform, intValue);
  437. }];
  438. });
  439. }
  440. - (void)setAndExecuteUniformStateCallbackAtIndex:(GLint)uniform forProgram:(GLProgram *)shaderProgram toBlock:(dispatch_block_t)uniformStateBlock;
  441. {
  442. [uniformStateRestorationBlocks setObject:[uniformStateBlock copy] forKey:[NSNumber numberWithInt:uniform]];
  443. uniformStateBlock();
  444. }
  445. - (void)setUniformsForProgramAtIndex:(NSUInteger)programIndex;
  446. {
  447. [uniformStateRestorationBlocks enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
  448. dispatch_block_t currentBlock = obj;
  449. currentBlock();
  450. }];
  451. }
  452. #pragma mark -
  453. #pragma mark GPUImageInput
  454. - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
  455. {
  456. static const GLfloat imageVertices[] = {
  457. -1.0f, -1.0f,
  458. 1.0f, -1.0f,
  459. -1.0f, 1.0f,
  460. 1.0f, 1.0f,
  461. };
  462. [self renderToTextureWithVertices:imageVertices textureCoordinates:[[self class] textureCoordinatesForRotation:inputRotation]];
  463. [self informTargetsAboutNewFrameAtTime:frameTime];
  464. }
  465. - (NSInteger)nextAvailableTextureIndex;
  466. {
  467. return 0;
  468. }
  469. - (void)setInputFramebuffer:(GPUImageFramebuffer *)newInputFramebuffer atIndex:(NSInteger)textureIndex;
  470. {
  471. firstInputFramebuffer = newInputFramebuffer;
  472. [firstInputFramebuffer lock];
  473. }
  474. - (CGSize)rotatedSize:(CGSize)sizeToRotate forIndex:(NSInteger)textureIndex;
  475. {
  476. CGSize rotatedSize = sizeToRotate;
  477. if (GPUImageRotationSwapsWidthAndHeight(inputRotation))
  478. {
  479. rotatedSize.width = sizeToRotate.height;
  480. rotatedSize.height = sizeToRotate.width;
  481. }
  482. return rotatedSize;
  483. }
  484. - (CGPoint)rotatedPoint:(CGPoint)pointToRotate forRotation:(GPUImageRotationMode)rotation;
  485. {
  486. CGPoint rotatedPoint;
  487. switch(rotation)
  488. {
  489. case kGPUImageNoRotation: return pointToRotate; break;
  490. case kGPUImageFlipHorizonal:
  491. {
  492. rotatedPoint.x = 1.0 - pointToRotate.x;
  493. rotatedPoint.y = pointToRotate.y;
  494. }; break;
  495. case kGPUImageFlipVertical:
  496. {
  497. rotatedPoint.x = pointToRotate.x;
  498. rotatedPoint.y = 1.0 - pointToRotate.y;
  499. }; break;
  500. case kGPUImageRotateLeft:
  501. {
  502. rotatedPoint.x = 1.0 - pointToRotate.y;
  503. rotatedPoint.y = pointToRotate.x;
  504. }; break;
  505. case kGPUImageRotateRight:
  506. {
  507. rotatedPoint.x = pointToRotate.y;
  508. rotatedPoint.y = 1.0 - pointToRotate.x;
  509. }; break;
  510. case kGPUImageRotateRightFlipVertical:
  511. {
  512. rotatedPoint.x = pointToRotate.y;
  513. rotatedPoint.y = pointToRotate.x;
  514. }; break;
  515. case kGPUImageRotateRightFlipHorizontal:
  516. {
  517. rotatedPoint.x = 1.0 - pointToRotate.y;
  518. rotatedPoint.y = 1.0 - pointToRotate.x;
  519. }; break;
  520. case kGPUImageRotate180:
  521. {
  522. rotatedPoint.x = 1.0 - pointToRotate.x;
  523. rotatedPoint.y = 1.0 - pointToRotate.y;
  524. }; break;
  525. }
  526. return rotatedPoint;
  527. }
  528. - (void)setInputSize:(CGSize)newSize atIndex:(NSInteger)textureIndex;
  529. {
  530. if (self.preventRendering)
  531. {
  532. return;
  533. }
  534. if (overrideInputSize)
  535. {
  536. if (CGSizeEqualToSize(forcedMaximumSize, CGSizeZero))
  537. {
  538. }
  539. else
  540. {
  541. CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(newSize, CGRectMake(0.0, 0.0, forcedMaximumSize.width, forcedMaximumSize.height));
  542. inputTextureSize = insetRect.size;
  543. }
  544. }
  545. else
  546. {
  547. CGSize rotatedSize = [self rotatedSize:newSize forIndex:textureIndex];
  548. if (CGSizeEqualToSize(rotatedSize, CGSizeZero))
  549. {
  550. inputTextureSize = rotatedSize;
  551. }
  552. else if (!CGSizeEqualToSize(inputTextureSize, rotatedSize))
  553. {
  554. inputTextureSize = rotatedSize;
  555. }
  556. }
  557. [self setupFilterForSize:[self sizeOfFBO]];
  558. }
  559. - (void)setInputRotation:(GPUImageRotationMode)newInputRotation atIndex:(NSInteger)textureIndex;
  560. {
  561. inputRotation = newInputRotation;
  562. }
  563. - (void)forceProcessingAtSize:(CGSize)frameSize;
  564. {
  565. if (CGSizeEqualToSize(frameSize, CGSizeZero))
  566. {
  567. overrideInputSize = NO;
  568. }
  569. else
  570. {
  571. overrideInputSize = YES;
  572. inputTextureSize = frameSize;
  573. forcedMaximumSize = CGSizeZero;
  574. }
  575. }
  576. - (void)forceProcessingAtSizeRespectingAspectRatio:(CGSize)frameSize;
  577. {
  578. if (CGSizeEqualToSize(frameSize, CGSizeZero))
  579. {
  580. overrideInputSize = NO;
  581. inputTextureSize = CGSizeZero;
  582. forcedMaximumSize = CGSizeZero;
  583. }
  584. else
  585. {
  586. overrideInputSize = YES;
  587. forcedMaximumSize = frameSize;
  588. }
  589. }
  590. - (CGSize)maximumOutputSize;
  591. {
  592. // I'm temporarily disabling adjustments for smaller output sizes until I figure out how to make this work better
  593. return CGSizeZero;
  594. /*
  595. if (CGSizeEqualToSize(cachedMaximumOutputSize, CGSizeZero))
  596. {
  597. for (id<GPUImageInput> currentTarget in targets)
  598. {
  599. if ([currentTarget maximumOutputSize].width > cachedMaximumOutputSize.width)
  600. {
  601. cachedMaximumOutputSize = [currentTarget maximumOutputSize];
  602. }
  603. }
  604. }
  605. return cachedMaximumOutputSize;
  606. */
  607. }
  608. - (void)endProcessing
  609. {
  610. // if (!isEndProcessing) // Repeat apply a same filter. Johnny xu,2014/8/13
  611. {
  612. isEndProcessing = YES;
  613. for (id<GPUImageInput> currentTarget in targets)
  614. {
  615. [currentTarget endProcessing];
  616. }
  617. }
  618. }
  619. - (BOOL)wantsMonochromeInput;
  620. {
  621. return NO;
  622. }
  623. #pragma mark -
  624. #pragma mark Accessors
  625. @end