在SurfaceFlinger.cpp里可以看到很多 traverseInZOrder的调用,比如dumpsys的调用,
mCurrentState.traverseInZOrder([&](Layer* layer) {
layer->miniDump(result, hwcId);
});
还有
status_t SurfaceFlinger::getLayerDebugInfo(std::vector<LayerDebugInfo>* outLayers) const
mCurrentState.traverseInZOrder([&](Layer* layer) {
outLayers->push_back(layer->getLayerDebugInfo());
});
void SurfaceFlinger::preComposition(nsecs_t refreshStartTime)
{
ATRACE_CALL();
ALOGV("preComposition");
bool needExtraInvalidate = false;
mDrawingState.traverseInZOrder([&](Layer* layer) {
if (layer->onPreComposition(refreshStartTime)) {
needExtraInvalidate = true;
}
});
if (needExtraInvalidate) {
signalLayerUpdate();
}
}
以这个为例,
mCurrentState.traverseInZOrder([&](Layer* layer) {
layer->miniDump(result, hwcId);
});
[&](Layer* layer) {
layer->miniDump(result, hwcId);
} 是一个Lambda语句块,
不熟悉Lambda时看上去会很难理解,
mCurrentState.traverseInZOrder 会遍历获取到的Layer列表,并且把layer作为参数传给语句块
[&](Layer* layer) {
layer->miniDump(result, hwcId);
}
进而来调用每个layer的miniDump 方法,这种Lambda的写法方便了编程实现。
现在我们来看看traverseInZOrder的调用流程
764 State mCurrentState{LayerVector::StateSet::Current};
frameworks/native/services/surfaceflinger/SurfaceFlinger.h
370 class State {
371 public:
372 explicit State(LayerVector::StateSet set) : stateSet(set), layersSortedByZ(set) {}
373 State& operator=(const State& other) {
374 // We explicitly don't copy stateSet so that, e.g., mDrawingState
375 // always uses the Drawing StateSet.
376 layersSortedByZ = other.layersSortedByZ;
377 displays = other.displays;
378 colorMatrixChanged = other.colorMatrixChanged;
379 if (colorMatrixChanged) {
380 colorMatrix = other.colorMatrix;
381 }
382 return *this;
383 }
384
385 const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid;
386 LayerVector layersSortedByZ;
387 DefaultKeyedVector< wp<IBinder>, DisplayDeviceState> displays;
388
389 bool colorMatrixChanged = true;
390 mat4 colorMatrix;
391
392 void traverseInZOrder(const LayerVector::Visitor& visitor) const;
393 void traverseInReverseZOrder(const LayerVector::Visitor& visitor) const;
394 };
实现在SurfaceFlinger.cpp
void SurfaceFlinger::State::traverseInZOrder(const LayerVector::Visitor& visitor) const {
layersSortedByZ.traverseInZOrder(stateSet, visitor);
}
layersSortedByZ 是LayerVector 类型
搜索关键词layersSortedByZ.add, (由于class LayerVector : public SortedVector,add操作的时候进行了排序)
layersSortedByZ的数据获取是在
status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
const sp<IBinder>& handle,
const sp<IGraphicBufferProducer>& gbc,
const sp<Layer>& lbc,
const sp<Layer>& parent)
{
// add this layer to the current state list
{
Mutex::Autolock _l(mStateLock);
if (mNumLayers >= MAX_LAYERS) {
ALOGE("AddClientLayer failed, mNumLayers (%zu) >= MAX_LAYERS (%zu)", mNumLayers,
MAX_LAYERS);
return NO_MEMORY;
}
if (parent == nullptr) {
mCurrentState.layersSortedByZ.add(lbc);
uint32_t SurfaceFlinger::setClientStateLocked(const ComposerState& composerState) {
...
if (what & layer_state_t::eLayerChanged) {
// NOTE: index needs to be calculated before we update the state
const auto& p = layer->getParent();
if (p == nullptr) {
ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
if (layer->setLayer(s.z) && idx >= 0) {
mCurrentState.layersSortedByZ.removeAt(idx);
mCurrentState.layersSortedByZ.add(layer);
// we need traversal (state changed)
// AND transaction (list changed)
flags |= eTransactionNeeded|eTraversalNeeded;
}
数据获取有了,再看方法
33class LayerVector : public SortedVector<sp<Layer>> {
这里实现了遍历
62void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
63 for (size_t i = 0; i < size(); i++) {
64 const auto& layer = (*this)[i];
65 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
66 : layer->getDrawingState();
67 if (state.zOrderRelativeOf != nullptr) {
68 continue;
69 }
70 layer->traverseInZOrder(stateSet, visitor);
71 }
72}
又调用到了Layer的方法
1702void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
1703 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1704 // produce a new list for traversing, including our relatives, and not including our children
1705 // who are relatives of another surface. In the case that there are no relative Z,
1706 // makeTraversalList returns our children directly to avoid significant overhead.
1707 // However in this case we need to take the responsibility for filtering children which
1708 // are relatives of another surface here.
1709 bool skipRelativeZUsers = false;
1710 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
1711
1712 size_t i = 0;
1713 for (; i < list.size(); i++) {
1714 const auto& relative = list[i];
1715 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1716 continue;
1717 }
1718
1719 if (relative->getZ() >= 0) {
1720 break;
1721 }
1722 relative->traverseInZOrder(stateSet, visitor);
1723 }
1724
1725 visitor(this);
在visitor(this);里,把this作为参数,传给了最最开始的块语句
如
[&](Layer* layer) {
outLayers->push_back(layer->getLayerDebugInfo());
}
[&](Layer* layer) {
if (layer->onPreComposition(refreshStartTime)) {
needExtraInvalidate = true;
}
}