Layer Tree 创建

本文详细解析了Blink中Graphics Layer Tree的创建过程,以及如何通过AddChild等函数建立与CC Layer Tree的对应关系。在Graphics Layer Tree创建后,通过SetNeedsFullTreeSync同步到CC Layer Tree,由Blink控制整个过程。分析了根节点的创建,以及在不同场景下启用硬件加速渲染的条件。最终,讲解了LayerTreeHost在多线程环境下的初始化,创建了用于管理CC Layer Tree的LayerTreeHostImpl对象和调度器Scheduler。
摘要由CSDN通过智能技术生成

站在老罗的肩膀上:https://blog.csdn.net/luoshengyang/article/details/50941980

网页的Graphics Layer Tree是根据Paint Layer Tree创建的,而Graphics Layer Tree与CC模块创建的Layer Tree的节点是一一对应的关系,如图1所示:

图1 Graphics Layer Tree与CC Layer Tree的关系

     也就是说,每一个Graphics Layer都对应有一个CC Layer。不过,Graphics Layer与CC Layer不是直接的一一对应的,它们是透过另外两个Layer才对应起来的,如图2所示:

图2 Graphics Layer与CC Layer的对应关系

       中间的两个Layer分别是WebContentLayerImpl和WebLayerImpl,它们是属于Content层的对象。Graphics Layer与CC Layer的对应关系,是在Graphics Layer的创建过程中建立起来的,接下来我们就通过源码分析这种对应关系的建立过程。

GraphicsLayer::GraphicsLayer(GraphicsLayerClient& client)
    : client_(client),
      ... {
  ...
  layer_ = cc::PictureLayer::Create(this);
  layer_->SetIsDrawable(draws_content_ && contents_visible_);
  layer_->SetLayerClient(weak_ptr_factory_.GetWeakPtr());
}                                                    

Graphics Layer通过GraphicsLayer::AddChild形成父子关系的(从而形成Graphics Layer Tree),如下所示:

void GraphicsLayer::AddChild(GraphicsLayer* child_layer) {
  AddChildInternal(child_layer);
  UpdateChildList();
}

首先调用成员函数AddChildInternal将参数childLayer描述的一个Graphics Layer作为当前正在处理的Graphics Layer的子Graphics Layer,如下所示: 

void GraphicsLayer::AddChildInternal(GraphicsLayer* child_layer) {
  ...
  if (child_layer->Parent())
    child_layer->RemoveFromParent();

  child_layer->SetParent(this);
  children_.push_back(child_layer);
}

这一步执行完成后,Graphics Layer之间就建立了父子关系。回到GraphicsLayer类的成员函数AddChild中,它接下来还会调用另外一个成员函数UpdateChildList,用来在CC Layer之间建立父子关系,从而形CC Layer Tree

void GraphicsLayer::UpdateChildList() {
  // child_host point to PictureLayer
  cc::Layer* child_host = layer_.get();
  child_host->RemoveAllChildren();

  ...

  for (size_t i = 0; i < children_.size(); ++i)
    child_host->AddChild(children_[i]->CcLayer());

  for (size_t i = 0; i < link_highlights_.size(); ++i)
    child_host->AddChild(link_highlights_[i]->Layer());
}

其中child_host指向的是当前Graphics Layer对应的一个PictureLayer对象,并把它所有的Child Graphics Layer对应的PictureLayer调用AddChild添加到它的子节点,AddChild调用InsertChild实现如下

void Layer::AddChild(scoped_refptr<Layer> child) {
  InsertChild(child, inputs_.children.size());
}


void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
  DCHECK(IsPropertyChangeAllowed());
  child->RemoveFromParent();
  AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
                         (child->DrawsContent() ? 1 : 0));
  child->SetParent(this);
  child->SetSubtreePropertyChanged();

  index = std::min(index, inputs_.children.size());
  inputs_.children.insert(inputs_.children.begin() + index, child);
  SetNeedsFullTreeSync();
}

Layer类的成员函数InsertChild所做的第一件事情是将当前正在处理的Picture Layer设置为参数child描述的Pictrue Layer的父Picture Layer,并且将参数child描述的Pictrue Layer保存在当前正在处理的Picture Layer的子Picture Layer列表中。Layer类的成员函数InsertChild所做的第二件事情是调用另外一个成员函数SetNeedsFullTreeSync发出一个通知,要在CC Layer Tree与CC Pending Layer Tree之间做一个Tree结构同步。

      Layer类的成员函数SetNeedsFullTreeSync的实现如下所示:

void Layer::SetNeeds
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值