GPUImageRawDataInput.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #import "GPUImageRawDataInput.h"
  2. @interface GPUImageRawDataInput()
  3. - (void)uploadBytes:(GLubyte *)bytesToUpload;
  4. @end
  5. @implementation GPUImageRawDataInput
  6. @synthesize pixelFormat = _pixelFormat;
  7. @synthesize pixelType = _pixelType;
  8. #pragma mark -
  9. #pragma mark Initialization and teardown
  10. - (id)initWithBytes:(GLubyte *)bytesToUpload size:(CGSize)imageSize;
  11. {
  12. if (!(self = [self initWithBytes:bytesToUpload size:imageSize pixelFormat:GPUPixelFormatBGRA type:GPUPixelTypeUByte]))
  13. {
  14. return nil;
  15. }
  16. return self;
  17. }
  18. - (id)initWithBytes:(GLubyte *)bytesToUpload size:(CGSize)imageSize pixelFormat:(GPUPixelFormat)pixelFormat;
  19. {
  20. if (!(self = [self initWithBytes:bytesToUpload size:imageSize pixelFormat:pixelFormat type:GPUPixelTypeUByte]))
  21. {
  22. return nil;
  23. }
  24. return self;
  25. }
  26. - (id)initWithBytes:(GLubyte *)bytesToUpload size:(CGSize)imageSize pixelFormat:(GPUPixelFormat)pixelFormat type:(GPUPixelType)pixelType;
  27. {
  28. if (!(self = [super init]))
  29. {
  30. return nil;
  31. }
  32. dataUpdateSemaphore = dispatch_semaphore_create(1);
  33. uploadedImageSize = imageSize;
  34. self.pixelFormat = pixelFormat;
  35. self.pixelType = pixelType;
  36. [self uploadBytes:bytesToUpload];
  37. return self;
  38. }
  39. // ARC forbids explicit message send of 'release'; since iOS 6 even for dispatch_release() calls: stripping it out in that case is required.
  40. - (void)dealloc;
  41. {
  42. #if ( (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0) || (!defined(__IPHONE_6_0)) )
  43. if (dataUpdateSemaphore != NULL)
  44. {
  45. dispatch_release(dataUpdateSemaphore);
  46. }
  47. #endif
  48. }
  49. #pragma mark -
  50. #pragma mark Image rendering
  51. - (void)uploadBytes:(GLubyte *)bytesToUpload;
  52. {
  53. [GPUImageContext useImageProcessingContext];
  54. // TODO: This probably isn't right, and will need to be corrected
  55. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:uploadedImageSize textureOptions:self.outputTextureOptions onlyTexture:YES];
  56. glBindTexture(GL_TEXTURE_2D, [outputFramebuffer texture]);
  57. glTexImage2D(GL_TEXTURE_2D, 0, _pixelFormat==GPUPixelFormatRGB ? GL_RGB : GL_RGBA, (int)uploadedImageSize.width, (int)uploadedImageSize.height, 0, (GLint)_pixelFormat, (GLenum)_pixelType, bytesToUpload);
  58. }
  59. - (void)updateDataFromBytes:(GLubyte *)bytesToUpload size:(CGSize)imageSize;
  60. {
  61. uploadedImageSize = imageSize;
  62. [self uploadBytes:bytesToUpload];
  63. }
  64. - (void)processData;
  65. {
  66. if (dispatch_semaphore_wait(dataUpdateSemaphore, DISPATCH_TIME_NOW) != 0)
  67. {
  68. return;
  69. }
  70. runAsynchronouslyOnVideoProcessingQueue(^{
  71. CGSize pixelSizeOfImage = [self outputImageSize];
  72. for (id<GPUImageInput> currentTarget in targets)
  73. {
  74. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  75. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  76. [currentTarget setInputSize:pixelSizeOfImage atIndex:textureIndexOfTarget];
  77. [currentTarget setInputFramebuffer:outputFramebuffer atIndex:textureIndexOfTarget];
  78. [currentTarget newFrameReadyAtTime:kCMTimeInvalid atIndex:textureIndexOfTarget];
  79. }
  80. dispatch_semaphore_signal(dataUpdateSemaphore);
  81. });
  82. }
  83. - (void)processDataForTimestamp:(CMTime)frameTime;
  84. {
  85. if (dispatch_semaphore_wait(dataUpdateSemaphore, DISPATCH_TIME_NOW) != 0)
  86. {
  87. return;
  88. }
  89. runAsynchronouslyOnVideoProcessingQueue(^{
  90. CGSize pixelSizeOfImage = [self outputImageSize];
  91. for (id<GPUImageInput> currentTarget in targets)
  92. {
  93. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  94. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  95. [currentTarget setInputSize:pixelSizeOfImage atIndex:textureIndexOfTarget];
  96. [currentTarget newFrameReadyAtTime:frameTime atIndex:textureIndexOfTarget];
  97. }
  98. dispatch_semaphore_signal(dataUpdateSemaphore);
  99. });
  100. }
  101. - (CGSize)outputImageSize;
  102. {
  103. return uploadedImageSize;
  104. }
  105. @end