资料:https://blog.youkuaiyun.com/luoshengyang/article/details/51638966
从前面Chromium网页渲染调度器(Scheduler)实现分析一文可以知道,当调度器调用SchedulerStateMachine类的成员函数NextAction询问状态机下一步要执行的操作时,SchedulerStateMachine类的成员函数NextAction会调用另外一个成员函数ShouldActivatePendingTree。当SchedulerStateMachine类的成员函数ShouldActivateSyncTree返回值等于true的时候,状态机就会提示调度器接下来需要执行ACTION_ACTIVATE_PENDING_TREE操作,也就是将CC Pending Layer Tree激活为CC Active Layer Tree,如下所示:
SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
if (ShouldActivateSyncTree())
return Action::ACTIVATE_SYNC_TREE;
...
return Action::NONE;
}
src/cc/scheduler/scheduler_state_machine.cc
接下来我们就继续分析SchedulerStateMachine类的成员函数ShouldActivateSyncTree什么情况下会返回true,它的实现如下所示:
bool SchedulerStateMachine::ShouldActivateSyncTree() const {
// There is nothing to activate.
if (!has_pending_tree_)
return false;
// We should not activate a second tree before drawing the first one.
// Even if we need to force activation of the pending tree, we should abort
// drawing the active tree first.
if (active_tree_needs_first_draw_)
return false;
if (ShouldAbortCurrentFrame())
return true;
// At this point, only activate if we are ready to activate.
return pending_tree_is_ready_for_activation_;
}
src/cc/scheduler/scheduler_state_machine.cc
SchedulerStateMachine类的成员函数ShouldActivatePendingTree的返回值为true有两个前提条件:
1. 当前存在CC Pending Layer Tree。这时候SchedulerStateMachine类的成员变量has_pending_tree_的值等于true。绘制CC Layer Tree时,仅仅是记录网页的绘制命令,那么调度器在请求Main线程在绘制CC Layer Tree之前,会将SchedulerStateMachine类的成员变量has_pending_tree_的值设置为true,表示在CC Layer Tree绘制完成后,会得到一个新的CC Pending Layer Tree。
2. 上一次激活得到的CC Active Layer Tree已经至少被渲染过一次。这时候SchedulerStateMachine类的成员变量active_tree_needs_first_draw_的值等于false。接下来我们会看到,当一个CC Pending Layer Tree激活为CC Active La