发布网友 发布时间:2022-04-23 17:55
共1个回答
热心网友 时间:2023-09-03 16:59
在Android4.1之后增加了Choreographer机制,用于同Vsync机制配合,实现统一调度界面绘图.Choreographer构造过程frameworks\base\core\java\android\view\Choreographer.java
[java] view plaincopy
public static Choreographer getInstance() {
return sThreadInstance.get();
}
[java] view plaincopy
private static final ThreadLocal<Choreographer> sThreadInstance =
new ThreadLocal<Choreographer>() {
@Override
protected Choreographer initialValue() {
Looper looper = Looper.myLooper();
if (looper == null) {
throw new IllegalStateException("The current thread must have a looper!");
}
return new Choreographer(looper);
}
};
为调用线程创建一个Choreographer实例,调用线程必须具备消息循环功能,因为ViewRootImpl对象的构造是在应用程序进程的UI主线程中执行的,因此创建的Choreographer对象将使用UI线程消息队列。
[java] view plaincopy
private Choreographer(Looper looper) {
mLooper = looper;
//创建消息处理Handler
mHandler = new FrameHandler(looper);
//如果系统使用了Vsync机制,则注册一个FrameDisplayEventReceiver接收器
mDisplayEventReceiver = USE_VSYNC ? new FrameDisplayEventReceiver(looper) : null;
mLastFrameTimeNanos = Long.MIN_VALUE;
//屏幕刷新周期
mFrameIntervalNanos = (long)(1000000000 /
new Display(Display.DEFAULT_DISPLAY, null).getRefreshRate());
//创建回调数组
mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1];
//初始化数组
for (int i = 0; i <= CALLBACK_LAST; i++) {
mCallbackQueues[i] = new CallbackQueue();
}
}
变量USE_VSYNC用于表示系统是否是用了Vsync同步机制,该值是通过读取系统属性debug.choreographer.vsync来获取的。如果系统使用了Vsync同步机制,则创建一个FrameDisplayEventReceiver对象用于请求并接收Vsync事件,最后Choreographer创建了一个大小为3的CallbackQueue队列数组,用于保存不同类型的Callback。