mTraversalScheduled是个布尔值,防止重复调用,在一次 vsync 信号期间多次调用是没有意义的- 利用 Handler 的同步屏障机制,优先处理异步消息
- Choreographer 登场
到这里,鼎鼎大名的 编舞者 —— Choreographer [ˌkɔːriˈɑːɡrəfər] 就该出场了(为了避免面试中出现不会读单词的尴尬,掌握一下发音还是必须的)。
通过 mChoreographer 发送了一个任务 mTraversalRunnable ,最终会在某个时刻被执行。在看源码之前,先抛出来几个问题:
mChoreographer是在什么时候初始化的?mTraversalRunnable是个什么鬼?mChoreographer是如何发送任务以及任务是如何被调度执行的?
围绕这三个问题,我们再回到源码中。
先来看第一个问题,这就得回到上一节介绍过的 WindowManagerGlobal.addView() 方法。
WindowManagerGlobal.java
// 参数 view 就是 DecorView
public void addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) {
…
ViewRootImpl root;
// 1. 初始化 ViewRootImpl
root = new ViewRootImpl(view.getContext(), display);
mViews.add(view);
mRoots.add(root);
root.setView(view, wparams, panelParentView);
…
}
注释 1 处 新建了 ViewRootImpl 对象,跟进 ViewRootImpl 的构造函数。
ViewRootImpl.java
public ViewRootImpl(Context context, Display display) {
mContext = context;
// 1. IWindowSession 代理对象,与 WMS 进行 Binder 通信
mWindowSession = WindowManagerGlobal.getWindowSession();
…
mThread = Thread.currentThread();
…
// IWindow Binder 对象
mWindow = new W(this);
…
// 2. 初始化 mAttachInfo
mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this,
context);
…
// 3. 初始化 Choreographer,通过 Threadlocal 存储
mChoreographer = Choreographer.getInstance();
…
}
在 ViewRootImpl 的构造函数中,注释 3 处初始化了 mChoreographer,调用的是 Choreographer.getInstance() 方法。
Choreographer.java
public static Choreographer getInstance() {
return sThreadInstance.get();
}
sThreadInstance 是一个 ThreadLocal<Choreographer> 对象。
Choreographer.java
private static final ThreadLocal sThreadInstance =
new ThreadLocal() {
@Override
protected Choreographer initialValue() {
Looper looper = Looper.myLooper();
if (looper == null) {
throw new IllegalStateException(“The current thread must have a looper!”);
}
// 新建 Choreographer 对象
Choreographer choreographer = new Choreographer(looper, VSYNC_SOURCE

本文详细解析了Android系统中Choreographer的角色和工作原理,介绍了如何初始化Choreographer,以及如何通过Choreographer检测应用的FPS。通过分析ViewRootImpl和Choreographer的源码,揭示了主线程 Looper与FPS的关系,探讨了帧同步信号VSYNC的使用,以及在遇到FPS问题时如何进行优化。
最低0.47元/天 解锁文章
2024

被折叠的 条评论
为什么被折叠?



