Android性能优化第(四)篇—Android渲染机制说过,Android系统每隔16ms发出VSYNC信号,触发对UI的渲染,16ms没完成绘制就会卡顿。VSync机制就像是一台转速固定的发动机(60转/s)。每一转会带动着去做一些UI相关的事情,但不是每一转都会有工作去做(就像有时在空挡,有时在D档)。有时候因为各种阻力某一圈工作量比较重超过了16.6ms,那么这台发动机这秒内就不是60转了,当然也有可能被其他因素影响,比如给油不足(主线程里干的活太多)等等,就会出现转速降低的状况。我们把这个转速叫做流畅度。当流畅度越小的时候说明当前程序越卡顿。
二、Choreographer帧率检测原理
我们有时候会看到这样的log,系统帮助我们打印出了跳帧数。
02-07 19:47:04.333 17601-17604/zhangwan.wj.com.choreographertest D/dalvikvm: GC_CONCURRENT freed 143K, 3% free 9105K/9384K, paused 2ms+0ms, total 6ms
02-07 19:47:04.337 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 60 frames! The application may be doing too much work on its main thread.
02-07 19:47:11.685 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 85 frames! The application may be doing too much work on its main thread.
02-07 19:47:12.545 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
02-07 19:47:14.893 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
02-07 19:47:23.049 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
02-07 19:47:23.929 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.
02-07 19:47:24.961 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 61 frames! The application may be doing too much work on its main thread.
02-07 19:47:25.817 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
02-07 19:47:26.433 17601-17601/zhangwan.wj.com.choreographertest I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
这个log就出自于Choreographer中(英[ˌkɒrɪ’ɒɡrəfə®] 美[ˌkɒrɪ’ɒɡrəfə®])。
void doFrame(long frameTimeNanos, int frame) {
final long startNanos;
synchronized (mLock) {
if (!mFrameScheduled) {
return; // no work to do
}
if (DEBUG_JANK && mDebugPrintNextFrameTimeDelta) {
mDebugPrintNextFrameTimeDelta = false;
Log.d(TAG, "Frame time delta: "
+ ((frameTimeNanos - mLastFrameTimeNanos) * 0.000001f) + " ms");
}
long intendedFrameTimeNanos = frameTimeNanos;
startNanos = System.nanoTime();
final long jitterNanos = startNanos - frameTimeNanos;
if (jitterNanos >= mFrameIntervalNanos) {
final long skippedFrames = jitterNanos / mFrameIntervalNanos;
if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) {
Log.i(TAG, "Skipped " + skippedFrames + " frames! "
+ "The application may be doing too much work on its main thread.");
}
final long lastFrameOffset = jitterNanos % mFrameIntervalNanos;
if (DEBUG_JANK) {
Log.d(TAG, "Missed vsync by " + (jitterNanos * 0.000001f) + " ms "
+ "which is more than the frame interval of "
+ (mFrameIntervalNanos * 0.000001f) + " ms! "
+ "Skipping " + skippedFrames + " frames and setting frame "
+ "time to " + (lastFrameOffset * 0.000001f) + " ms in the past.");
}
frameTimeNanos = startNanos - lastFrameOffset;
}
}
}
其中SKIPPED_FRAME_WARNING_LIMIT是Choreographer的成员变量。
// Set a limit to warn about skipped frames.
// Skipped frames imply jank.
private static final int SKIPPED_FRAME_WARNING_LIMIT =SystemProperties.getInt( "debug.choreographer.skipwarning", 30);
也就是当跳帧数大于设置的SKIPPED_FRAME_WARNING_LIMIT 值时会在当前进程输出这个log。由于 SKIPPED_FRAME_WARNING_LIMIT 的值默认为 30,所以上面的log并不是经常看到,如果我们用反射的方法把SKIPPED_FRAME_WARNING_LIMIT的值设置成1,这样可以保证只要有丢帧,就会有上面的log输出来。
static {
try {
Field field = Choreographer.class.getDeclaredField("SKIPPED_FRAME_WARNING_LIMIT");
field.setAccessible(true);
field.set(Choreographer.class,1);
} catch (Throwable e) {
e.printStackTrace();
}
}
注意,这个方案是 API 16 以上才支持。Choreographer就是一个消息处理器,根据vsync 信号 来计算frame,而计算frame的方式就是处理三种回调,包括事件回调、动画回调、绘制回调。这三种事件在消息输入、加入动画、准备绘图layout 等动作时均会发给Choreographer。一句话,我们只要捕获这个log提取出skippedFrames 就可以知道界面是否卡顿。
三、如何检测
采用上面的方式就可以在App内部观测当前App的流畅度了。并且在丢帧的地方打印,就可以知道丢帧的大概原因,大概位置,定位代码问题。
在Choreographer中有个回调接口,FrameCallback。
public interface FrameCallback {
//当新的一帧被绘制的时候被调用。
public void doFrame(long frameTimeNanos);
}
根据上面的代码,重写doFrame方法,所以照葫芦画瓢,自定义FrameCallback。我们可以在每一帧被渲染的时候记录下它开始渲染的时间,这样在下一帧被处理时,判断上一帧在渲染过程中是否出现掉帧。
public class SMFrameCallback implements Choreographer.FrameCallback {
public static SMFrameCallback sInstance;
private String TAG="SMFrameCallback";
public static final float deviceRefreshRateMs=16.6f;
public static long lastFrameTimeNanos=0;//纳秒为单位
public static long currentFrameTimeNanos=0;
public void start() {
Choreographer.getInstance().postFrameCallback(SMFrameCallback.getInstance());
}
public static SMFrameCallback getInstance() {
if (sInstance == null) {
sInstance = new SMFrameCallback();
}
return sInstance;
}
@Override
public void doFrame(long frameTimeNanos) {
if(lastFrameTimeNanos==0){
lastFrameTimeNanos=frameTimeNanos;
Choreographer.getInstance().postFrameCallback(this);
return;
}
currentFrameTimeNanos=frameTimeNanos;
float value=(currentFrameTimeNanos-lastFrameTimeNanos)/1000000.0f;
final int skipFrameCount = skipFrameCount(lastFrameTimeNanos, currentFrameTimeNanos, deviceRefreshRateMs);
Log.e(TAG,"两次绘制时间间隔value="+value+" frameTimeNanos="+frameTimeNanos+" currentFrameTimeNanos="+currentFrameTimeNanos+" skipFrameCount="+skipFrameCount+"");
lastFrameTimeNanos=currentFrameTimeNanos;
Choreographer.getInstance().postFrameCallback(this);
}
/**