GPUImageVideoCamera帧时间计算与GPU Rep
2017-01-13 本文已影响404人
熊皮皮
GPUImageViewCamera运行期间计算的帧时间,我此前略有疑惑:Current frame time与Xcode FPS面板显示的CPU和GPU时间并不相同。当然,必须说明的是,GPUImageViewCamera计算的是CPU时间,那么与Xcode显示的GPU时间不同是正常的。
经分析,GPUImage并没算错,是我理解有误。推论是,GPUImage只计算了processVideoSampleBuffer:方法的CPU耗时,而GPU Report可能通过GPU驱动统计了EAGLContext的presentRenderbuffer:方法进行系统前后帧缓冲区切换前,绘制后帧缓冲区所发出的所有OpenGL ES客户端函数的总耗时。
下面给出一段运行日志与GPU Report截图并作简单分析。
GPUImageVideoCamera运行期间Frame Time数据2017-01-13 13:58:20.532416 Average frame time : 6.184078 ms
2017-01-13 13:58:20.532625 Current frame time : 6.078005 ms
2017-01-13 13:58:20.564572 Average frame time : 6.184159 ms
2017-01-13 13:58:20.564781 Current frame time : 6.265998 ms
2017-01-13 13:58:20.601346 Average frame time : 6.187399 ms
2017-01-13 13:58:20.601621 Current frame time : 9.489000 ms
2017-01-13 13:58:20.638370 Average frame time : 6.194337 ms
2017-01-13 13:58:20.638593 Current frame time : 13.271034 ms
2017-01-13 13:58:20.663382 Average frame time : 6.192666 ms
2017-01-13 13:58:20.663914 Current frame time : 4.487038 ms
2017-01-13 13:58:20.697100 Average frame time : 6.191210 ms
2017-01-13 13:58:20.697925 Current frame time : 4.703045 ms
2017-01-13 13:58:20.729617 Average frame time : 6.189486 ms
2017-01-13 13:58:20.730434 Current frame time : 4.426003 ms
2017-01-13 13:58:20.768633 Average frame time : 6.193722 ms
2017-01-13 13:58:20.768898 Current frame time : 10.530949 ms
2017-01-13 13:58:20.799763 Average frame time : 6.195586 ms
2017-01-13 13:58:20.799968 Current frame time : 8.105993 ms
以第一、二条记录为例,使用NSLog输出的日志时间,第一帧的CPU处理时间frame time = 13:58:20.564572 - 13:58:20.532416 = 0.564572 - 0.532416 = 32.156 毫秒。接近FPS中GPU Report内Frame Time显示的时间。
然而,此值与GPUImage输出的Current frame time : 6.078005 ms有较大差异,结合GPUImageVideoCamera源码(如下所示),可知它只统计了processVideoSampleBuffer方法的CPU耗时。
- (void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer
{
if (capturePaused)
{
return;
}
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
// 上传纹理等操作
CFAbsoluteTime currentFrameTime = (CFAbsoluteTimeGetCurrent() - startTime);
totalFrameTimeDuringCapture += currentFrameTime;
NSLog(@"Average frame time : %f ms", [self averageFrameDurationDuringCapture]);
NSLog(@"Current frame time : %f ms", 1000.0 * currentFrameTime);
// ...
}