GPUImageUIElement.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #import "GPUImageUIElement.h"
  2. @interface GPUImageUIElement ()
  3. {
  4. UIView *view;
  5. CALayer *layer;
  6. CGSize previousLayerSizeInPixels;
  7. CMTime time;
  8. NSTimeInterval actualTimeOfLastUpdate;
  9. }
  10. @end
  11. @implementation GPUImageUIElement
  12. #pragma mark -
  13. #pragma mark Initialization and teardown
  14. - (id)initWithView:(UIView *)inputView;
  15. {
  16. if (!(self = [super init]))
  17. {
  18. return nil;
  19. }
  20. view = inputView;
  21. layer = inputView.layer;
  22. previousLayerSizeInPixels = CGSizeZero;
  23. [self update];
  24. return self;
  25. }
  26. - (id)initWithLayer:(CALayer *)inputLayer;
  27. {
  28. if (!(self = [super init]))
  29. {
  30. return nil;
  31. }
  32. view = nil;
  33. layer = inputLayer;
  34. previousLayerSizeInPixels = CGSizeZero;
  35. [self update];
  36. return self;
  37. }
  38. #pragma mark -
  39. #pragma mark Layer management
  40. - (CGSize)layerSizeInPixels;
  41. {
  42. CGSize pointSize = layer.bounds.size;
  43. return CGSizeMake(layer.contentsScale * pointSize.width, layer.contentsScale * pointSize.height);
  44. }
  45. - (void)update;
  46. {
  47. [self updateWithTimestamp:kCMTimeIndefinite];
  48. }
  49. - (void)updateUsingCurrentTime;
  50. {
  51. if(CMTIME_IS_INVALID(time)) {
  52. time = CMTimeMakeWithSeconds(0, 600);
  53. actualTimeOfLastUpdate = [NSDate timeIntervalSinceReferenceDate];
  54. } else {
  55. NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
  56. NSTimeInterval diff = now - actualTimeOfLastUpdate;
  57. time = CMTimeAdd(time, CMTimeMakeWithSeconds(diff, 600));
  58. actualTimeOfLastUpdate = now;
  59. }
  60. [self updateWithTimestamp:time];
  61. }
  62. - (void)updateWithTimestamp:(CMTime)frameTime;
  63. {
  64. [GPUImageContext useImageProcessingContext];
  65. CGSize layerPixelSize = [self layerSizeInPixels];
  66. GLubyte *imageData = (GLubyte *) calloc(1, (int)layerPixelSize.width * (int)layerPixelSize.height * 4);
  67. CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB();
  68. CGContextRef imageContext = CGBitmapContextCreate(imageData, (int)layerPixelSize.width, (int)layerPixelSize.height, 8, (int)layerPixelSize.width * 4, genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
  69. // CGContextRotateCTM(imageContext, M_PI_2);
  70. CGContextTranslateCTM(imageContext, 0.0f, layerPixelSize.height);
  71. CGContextScaleCTM(imageContext, layer.contentsScale, -layer.contentsScale);
  72. // CGContextSetBlendMode(imageContext, kCGBlendModeCopy); // From Technical Q&A QA1708: http://developer.apple.com/library/ios/#qa/qa1708/_index.html
  73. [layer renderInContext:imageContext];
  74. CGContextRelease(imageContext);
  75. CGColorSpaceRelease(genericRGBColorspace);
  76. // TODO: This may not work
  77. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:layerPixelSize textureOptions:self.outputTextureOptions onlyTexture:YES];
  78. glBindTexture(GL_TEXTURE_2D, [outputFramebuffer texture]);
  79. // no need to use self.outputTextureOptions here, we always need these texture options
  80. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)layerPixelSize.width, (int)layerPixelSize.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, imageData);
  81. free(imageData);
  82. for (id<GPUImageInput> currentTarget in targets)
  83. {
  84. if (currentTarget != self.targetToIgnoreForUpdates)
  85. {
  86. NSInteger indexOfObject = [targets indexOfObject:currentTarget];
  87. NSInteger textureIndexOfTarget = [[targetTextureIndices objectAtIndex:indexOfObject] integerValue];
  88. [currentTarget setInputSize:layerPixelSize atIndex:textureIndexOfTarget];
  89. [currentTarget newFrameReadyAtTime:frameTime atIndex:textureIndexOfTarget];
  90. }
  91. }
  92. }
  93. @end