2015-07-28 55 views
0

我使用GPUImageFilterGroup将一些过滤器应用于图像。所有过滤器稳定(所有参数不变),但最后过滤可变(一些参数改变)。更新GPUImageFilterGroup中的一个过滤器而不重绘所有过滤器

我需要在最后一次过滤器更改后重新绘制图像。

现在我打电话给processImage来源GPUImagePicture,但是这个调用重画了所有的过滤器,速度太慢了。

如何重绘组中的最后一个过滤器?我认为,我应该在最后一个过滤器绘制之前保存一个帧缓冲区的副本,并且当我更改了最后一个过滤器中的某个参数时,我应该使用保存的帧缓冲区来重绘最后一个过滤器。但我找不到如何保存帧缓冲区的副本。

回答

1

我解决了这个,通过继承GPUImageFilter和GPUImageFilterGroup。 在GPUImageFilter我重载方法

- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex 
<...> 

[self renderToTextureWithVertices:imageVertices textureCoordinates:[[self class] textureCoordinatesForRotation:inputRotation]]; 
_bufferCallback(self); 
[self informTargetsAboutNewFrameAtTime:frameTime]; 
<...> 

在GPUImageFilterGroup我重载的方法:从preLast滤波器

- (void)addFilter:(GPUImageOutput<GPUImageInput> *)newFilter 
{ 
    NSParameterAssert([newFilter isKindOfClass: [FAEShiftFilterWithBackOutputBuffer class]]); 
    if ([newFilter isKindOfClass:[FAEShiftFilterWithBackOutputBuffer class]]) 
    { 
     __weak typeof(self) selfWeak = self; 
     [(FAEShiftFilterWithBackOutputBuffer*)newFilter setOutputBufferCallback:^(FAEShiftFilterWithBackOutputBuffer *sender) { 
     __strong typeof(selfWeak) selfStrong = selfWeak; 
     if (selfStrong) 
     { 
      if (!selfStrong.lastFramebuffer) 
      { 
       if ([selfStrong isPreLastFilter:sender]) 
       { 
        selfStrong.lastFramebuffer = [sender framebufferForOutput]; 
        [selfStrong.lastFramebuffer lock]; 
       } 
      } 
     } 
    }]; 
} 
[super addFilter:newFilter]; 
} 

这种方法存储outputFrameBuffer。 和方法:在dealloc中和forceProcessingAtSize和forceProcessingAtSizeRespectingAspectRatio方法

- (void)newFrameReadyAtTime:(CMTime)frameTime atIndex:(NSInteger)textureIndex 
{ 
    if (self.filterCount > 1) 
    { 
    if (self.lastFramebuffer) 
    { 
     GPUImageFilter* lastFilter = (GPUImageFilter*)self.terminalFilter; 
     [lastFilter setInputFramebuffer:self.lastFramebuffer atIndex:0]; 
     [lastFilter newFrameReadyAtTime:frameTime atIndex:textureIndex]; 
    } 
    else 
    { 
     [super newFrameReadyAtTime:frameTime atIndex:textureIndex]; 
    } 
} 
else 
{ 
    [super newFrameReadyAtTime:frameTime atIndex:textureIndex]; 
} 
} 

我也复位保存帧缓冲。

- (void)_clearLastFrameBuffer 
{ 
if (_lastFramebuffer) 
{ 
    [_lastFramebuffer unlock]; 
    _lastFramebuffer = nil; 
} 
}