站在老罗的肩膀上:https://blog.youkuaiyun.com/luoshengyang/article/details/50648792
目前Render Layer已经更新为Paint Layer, 其最终通过PaintLayer::InsertOnlyThisLayerAfterStyleChange创建,其函数调用关系如下,通过之前创建Layout Object Tree的LayoutTreeBuilderForElement::CreateLayoutObject调入:
LayoutTreeBuilderForElement::CreateLayoutObject实现如下:
DISABLE_CFI_PERF
void LayoutTreeBuilderForElement::CreateLayoutObject() {
...
LayoutObject* new_layout_object = node_->CreateLayoutObject(style);
...
new_layout_object->SetStyle(
&style); // SetStyle() can depend on LayoutObject() already being set
...
}
其调用SetStyle设置LayoutObject的CSS属性:
DISABLE_CFI_PERF
void LayoutObject::SetStyle(scoped_refptr<ComputedStyle> style) {
DCHECK(style);
if (style_ == style)
return;
StyleDifference diff;
...
diff = AdjustStyleDifference(diff);
StyleWillChange(diff, *style);
scoped_refptr<ComputedStyle> old_style = std::move(style_);
SetStyleInternal(std::move(style));
...
StyleDidChange(diff, old_style.get());
...
}
其中style_ type is ComputedStyle, describe the CSS attribute of this layout object. And it calculte the CSS difference between current and previous one, and calling SetStyleInternal to store new CSS into style_. Then calling LayoutBlockFlow::StyleDidChange. LayoutBlockFlow类是间接地从LayoutObject类继承下来的,并且重写了成员函数StyleDidChange。因此,接下来RenderBlockFlow类的成员函数StyleDidChange就会被调用。在调用的过程中,就会检查是否需要创建一个Paint Layer。LayoutBlockFlow类的成员函数StyleDidChange的实现如下所示:
void LayoutBlock::StyleDidChange(StyleDifference diff,
const ComputedStyle* old_style) {
LayoutBox::StyleDidChange(diff, old_style);
...
}
void LayoutBlock::StyleDidChange(StyleDifference diff,
const ComputedStyle* old_style) {
LayoutBox::StyleDidChange(diff, old_style);
...
}
void LayoutBox::StyleDidChange(StyleDifference diff,
const ComputedStyle* old_style) {
...
LayoutBoxModelObject::StyleDidChange(diff, old_style);
...
}
并且一步步回调基类的StyleDidChange,直到LayoutBoxModelObject