GPUImageBuffer.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #import "GPUImageBuffer.h"
  2. @interface GPUImageBuffer()
  3. @end
  4. @implementation GPUImageBuffer
  5. @synthesize bufferSize = _bufferSize;
  6. #pragma mark -
  7. #pragma mark Initialization and teardown
  8. - (id)init;
  9. {
  10. if (!(self = [self initWithFragmentShaderFromString:kGPUImagePassthroughFragmentShaderString]))
  11. {
  12. return nil;
  13. }
  14. bufferedFramebuffers = [[NSMutableArray alloc] init];
  15. // [bufferedTextures addObject:[NSNumber numberWithInt:outputTexture]];
  16. _bufferSize = 1;
  17. return self;
  18. }
  19. - (void)dealloc
  20. {
  21. for (GPUImageFramebuffer *currentFramebuffer in bufferedFramebuffers)
  22. {
  23. [currentFramebuffer unlock];
  24. }
  25. }
  26. #pragma mark -
  27. #pragma mark GPUImageInput
  28. - (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex;
  29. {
  30. if ([bufferedFramebuffers count] >= _bufferSize)
  31. {
  32. outputFramebuffer = [bufferedFramebuffers objectAtIndex:0];
  33. [bufferedFramebuffers removeObjectAtIndex:0];
  34. }
  35. else
  36. {
  37. // Nothing yet in the buffer, so don't process further until the buffer is full
  38. outputFramebuffer = firstInputFramebuffer;
  39. [firstInputFramebuffer lock];
  40. }
  41. [bufferedFramebuffers addObject:firstInputFramebuffer];
  42. // Need to pass along rotation information, as we're just holding on to buffered framebuffers and not rotating them ourselves
  43. for (id<GPUImageInput> currentTarget in targets)
  44. {
  45. if (currentTarget != self.targetToIgnoreForUpdates)
  46. {
  47. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  48. NSInteger textureIndex = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  49. [currentTarget setInputRotation:inputRotation atIndex:textureIndex];
  50. }
  51. }
  52. // Let the downstream video elements see the previous frame from the buffer before rendering a new one into place
  53. [self informTargetsAboutNewFrameAtTime:frameTime];
  54. // [self renderToTextureWithVertices:imageVertices textureCoordinates:[[self class] textureCoordinatesForRotation:inputRotation]];
  55. }
  56. - (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
  57. {
  58. // No need to render to another texture anymore, since we'll be hanging on to the textures in our buffer
  59. }
  60. #pragma mark -
  61. #pragma mark Accessors
  62. - (void)setBufferSize:(NSUInteger)newValue;
  63. {
  64. if ( (newValue == _bufferSize) || (newValue < 1) )
  65. {
  66. return;
  67. }
  68. if (newValue > _bufferSize)
  69. {
  70. NSUInteger texturesToAdd = newValue - _bufferSize;
  71. for (NSUInteger currentTextureIndex = 0; currentTextureIndex < texturesToAdd; currentTextureIndex++)
  72. {
  73. // TODO: Deal with the growth of the size of the buffer by rotating framebuffers, no textures
  74. }
  75. }
  76. else
  77. {
  78. NSUInteger texturesToRemove = _bufferSize - newValue;
  79. for (NSUInteger currentTextureIndex = 0; currentTextureIndex < texturesToRemove; currentTextureIndex++)
  80. {
  81. GPUImageFramebuffer *lastFramebuffer = [bufferedFramebuffers lastObject];
  82. [bufferedFramebuffers removeObjectAtIndex:([bufferedFramebuffers count] - 1)];
  83. [lastFramebuffer unlock];
  84. lastFramebuffer = nil;
  85. }
  86. }
  87. _bufferSize = newValue;
  88. }
  89. @end