1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #undef LOG_TAG
19 #define LOG_TAG "SurfaceFlinger"
20
21 #include <android-base/logging.h>
22
23 #include "LayerHierarchy.h"
24 #include "LayerLog.h"
25 #include "SwapErase.h"
26
27 namespace android::surfaceflinger::frontend {
28
29 namespace {
30 auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs,
__anon48b1cac90202(const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs, const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) 31 const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) {
32 auto lhsLayer = lhs.first->getLayer();
33 auto rhsLayer = rhs.first->getLayer();
34 if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) {
35 return lhsLayer->layerStack.id < rhsLayer->layerStack.id;
36 }
37 if (lhsLayer->z != rhsLayer->z) {
38 return lhsLayer->z < rhsLayer->z;
39 }
40 return lhsLayer->id < rhsLayer->id;
41 };
42
insertSorted(std::vector<std::pair<LayerHierarchy *,LayerHierarchy::Variant>> & vec,std::pair<LayerHierarchy *,LayerHierarchy::Variant> value)43 void insertSorted(std::vector<std::pair<LayerHierarchy*, LayerHierarchy::Variant>>& vec,
44 std::pair<LayerHierarchy*, LayerHierarchy::Variant> value) {
45 auto it = std::upper_bound(vec.begin(), vec.end(), value, layerZCompare);
46 vec.insert(it, std::move(value));
47 }
48 } // namespace
49
LayerHierarchy(RequestedLayerState * layer)50 LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {}
51
LayerHierarchy(const LayerHierarchy & hierarchy,bool childrenOnly)52 LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) {
53 mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer;
54 mChildren = hierarchy.mChildren;
55 }
56
traverse(const Visitor & visitor,LayerHierarchy::TraversalPath & traversalPath,uint32_t depth) const57 void LayerHierarchy::traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& traversalPath,
58 uint32_t depth) const {
59 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
60 "Cycle detected in LayerHierarchy::traverse. See "
61 "traverse_stack_overflow_transactions.winscope");
62
63 if (mLayer) {
64 bool breakTraversal = !visitor(*this, traversalPath);
65 if (breakTraversal) {
66 return;
67 }
68 }
69
70 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
71 traversalPath.invalidRelativeRootId);
72 for (auto& [child, childVariant] : mChildren) {
73 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
74 childVariant);
75 child->traverse(visitor, traversalPath, depth + 1);
76 }
77 }
78
traverseInZOrder(const Visitor & visitor,LayerHierarchy::TraversalPath & traversalPath) const79 void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
80 LayerHierarchy::TraversalPath& traversalPath) const {
81 bool traverseThisLayer = (mLayer != nullptr);
82 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
83 auto& [child, childVariant] = *it;
84 if (traverseThisLayer && child->getLayer()->z >= 0) {
85 traverseThisLayer = false;
86 bool breakTraversal = !visitor(*this, traversalPath);
87 if (breakTraversal) {
88 return;
89 }
90 }
91 if (childVariant == LayerHierarchy::Variant::Detached) {
92 continue;
93 }
94 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
95 childVariant);
96 child->traverseInZOrder(visitor, traversalPath);
97 }
98
99 if (traverseThisLayer) {
100 visitor(*this, traversalPath);
101 }
102 }
103
addChild(LayerHierarchy * child,LayerHierarchy::Variant variant)104 void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
105 insertSorted(mChildren, {child, variant});
106 }
107
removeChild(LayerHierarchy * child)108 void LayerHierarchy::removeChild(LayerHierarchy* child) {
109 auto it = std::find_if(mChildren.begin(), mChildren.end(),
110 [child](const std::pair<LayerHierarchy*, Variant>& x) {
111 return x.first == child;
112 });
113 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
114 mChildren.erase(it);
115 }
116
sortChildrenByZOrder()117 void LayerHierarchy::sortChildrenByZOrder() {
118 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
119 }
120
updateChild(LayerHierarchy * hierarchy,LayerHierarchy::Variant variant)121 void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
122 auto it = std::find_if(mChildren.begin(), mChildren.end(),
123 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
124 return child.first == hierarchy;
125 });
126 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
127 it->second = variant;
128 }
129
getLayer() const130 const RequestedLayerState* LayerHierarchy::getLayer() const {
131 return mLayer;
132 }
133
getRelativeParent() const134 const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
135 return mRelativeParent;
136 }
137
getParent() const138 const LayerHierarchy* LayerHierarchy::getParent() const {
139 return mParent;
140 }
141
getDebugStringShort() const142 std::string LayerHierarchy::getDebugStringShort() const {
143 std::string debug = "LayerHierarchy{";
144 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
145 if (mChildren.empty()) {
146 debug += "no children";
147 } else {
148 debug += std::to_string(mChildren.size()) + " children";
149 }
150 return debug + "}";
151 }
152
dump(std::ostream & out,const std::string & prefix,LayerHierarchy::Variant variant,bool isLastChild,bool includeMirroredHierarchy) const153 void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
154 LayerHierarchy::Variant variant, bool isLastChild,
155 bool includeMirroredHierarchy) const {
156 if (!mLayer) {
157 out << " ROOT";
158 } else {
159 out << prefix + (isLastChild ? "└─ " : "├─ ");
160 if (variant == LayerHierarchy::Variant::Relative) {
161 out << "(Relative) ";
162 } else if (LayerHierarchy::isMirror(variant)) {
163 if (!includeMirroredHierarchy) {
164 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
165 return;
166 }
167 out << "(Mirroring) ";
168 }
169
170 out << *mLayer << " pid=" << mLayer->ownerPid.val() << " uid=" << mLayer->ownerUid.val();
171 }
172
173 for (size_t i = 0; i < mChildren.size(); i++) {
174 auto& [child, childVariant] = mChildren[i];
175 if (childVariant == LayerHierarchy::Variant::Detached) continue;
176 const bool lastChild = i == (mChildren.size() - 1);
177 std::string childPrefix = prefix;
178 if (mLayer) {
179 childPrefix += (isLastChild ? " " : "│ ");
180 }
181 out << "\n";
182 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
183 }
184 return;
185 }
186
hasRelZLoop(uint32_t & outInvalidRelativeRoot) const187 bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
188 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
189 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
190 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
191 if (traversalPath.hasRelZLoop()) {
192 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
193 return false;
194 }
195 return true;
196 });
197 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
198 }
199
init(const std::vector<std::unique_ptr<RequestedLayerState>> & layers)200 void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
201 mLayerIdToHierarchy.clear();
202 mHierarchies.clear();
203 mRoot = nullptr;
204 mOffscreenRoot = nullptr;
205
206 mHierarchies.reserve(layers.size());
207 mLayerIdToHierarchy.reserve(layers.size());
208 for (auto& layer : layers) {
209 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
210 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
211 }
212 for (const auto& layer : layers) {
213 onLayerAdded(layer.get());
214 }
215 detachHierarchyFromRelativeParent(&mOffscreenRoot);
216 mInitialized = true;
217 }
218
attachToParent(LayerHierarchy * hierarchy)219 void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
220 auto layer = hierarchy->mLayer;
221 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
222 ? LayerHierarchy::Variant::Detached
223 : LayerHierarchy::Variant::Attached;
224
225 LayerHierarchy* parent;
226
227 if (layer->parentId != UNASSIGNED_LAYER_ID) {
228 parent = getHierarchyFromId(layer->parentId);
229 } else if (layer->canBeRoot) {
230 parent = &mRoot;
231 } else {
232 parent = &mOffscreenRoot;
233 }
234 parent->addChild(hierarchy, type);
235 hierarchy->mParent = parent;
236 }
237
detachFromParent(LayerHierarchy * hierarchy)238 void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
239 hierarchy->mParent->removeChild(hierarchy);
240 hierarchy->mParent = nullptr;
241 }
242
attachToRelativeParent(LayerHierarchy * hierarchy)243 void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
244 auto layer = hierarchy->mLayer;
245 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
246 return;
247 }
248
249 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
250 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
251 } else {
252 hierarchy->mRelativeParent = &mOffscreenRoot;
253 }
254 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
255 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
256 }
257
detachFromRelativeParent(LayerHierarchy * hierarchy)258 void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
259 if (hierarchy->mRelativeParent) {
260 hierarchy->mRelativeParent->removeChild(hierarchy);
261 }
262 hierarchy->mRelativeParent = nullptr;
263 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
264 }
265
getDescendants(LayerHierarchy * root)266 std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) {
267 std::vector<LayerHierarchy*> hierarchies;
268 hierarchies.push_back(root);
269 std::vector<LayerHierarchy*> descendants;
270 for (size_t i = 0; i < hierarchies.size(); i++) {
271 LayerHierarchy* hierarchy = hierarchies[i];
272 if (hierarchy->mLayer) {
273 descendants.push_back(hierarchy);
274 }
275 for (auto& [child, childVariant] : hierarchy->mChildren) {
276 if (childVariant == LayerHierarchy::Variant::Detached ||
277 childVariant == LayerHierarchy::Variant::Attached) {
278 hierarchies.push_back(child);
279 }
280 }
281 }
282 return descendants;
283 }
284
attachHierarchyToRelativeParent(LayerHierarchy * root)285 void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
286 std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root);
287 for (LayerHierarchy* hierarchy : hierarchiesToAttach) {
288 attachToRelativeParent(hierarchy);
289 }
290 }
291
detachHierarchyFromRelativeParent(LayerHierarchy * root)292 void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
293 std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root);
294 for (LayerHierarchy* hierarchy : hierarchiesToDetach) {
295 detachFromRelativeParent(hierarchy);
296 }
297 }
298
onLayerAdded(RequestedLayerState * layer)299 void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
300 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
301 attachToParent(hierarchy);
302 attachToRelativeParent(hierarchy);
303
304 for (uint32_t mirrorId : layer->mirrorIds) {
305 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
306 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
307 }
308 if (FlagManager::getInstance().detached_mirror()) {
309 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
310 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
311 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
312 }
313 }
314 }
315
onLayerDestroyed(RequestedLayerState * layer)316 void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
317 LLOGV(layer->id, "");
318 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
319 if (!hierarchy) {
320 // Layer was never part of the hierarchy if it was created and destroyed in the same
321 // transaction.
322 return;
323 }
324 // detach from parent
325 detachFromRelativeParent(hierarchy);
326 detachFromParent(hierarchy);
327
328 // detach children
329 for (auto& [child, variant] : hierarchy->mChildren) {
330 if (variant == LayerHierarchy::Variant::Attached ||
331 variant == LayerHierarchy::Variant::Detached) {
332 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
333 child->mParent = &mOffscreenRoot;
334 } else if (variant == LayerHierarchy::Variant::Relative) {
335 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
336 child->mRelativeParent = &mOffscreenRoot;
337 }
338 }
339
340 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
341 return layerHierarchy.get() == hierarchy;
342 });
343 mLayerIdToHierarchy.erase(layer->id);
344 }
345
updateMirrorLayer(RequestedLayerState * layer)346 void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
347 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
348 auto it = hierarchy->mChildren.begin();
349 while (it != hierarchy->mChildren.end()) {
350 if (LayerHierarchy::isMirror(it->second)) {
351 it = hierarchy->mChildren.erase(it);
352 } else {
353 it++;
354 }
355 }
356
357 for (uint32_t mirrorId : layer->mirrorIds) {
358 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
359 }
360 if (FlagManager::getInstance().detached_mirror()) {
361 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
362 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
363 LayerHierarchy::Variant::Detached_Mirror);
364 }
365 }
366 }
367
doUpdate(const std::vector<std::unique_ptr<RequestedLayerState>> & layers,const std::vector<std::unique_ptr<RequestedLayerState>> & destroyedLayers)368 void LayerHierarchyBuilder::doUpdate(
369 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
370 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
371 // rebuild map
372 for (auto& layer : layers) {
373 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
374 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
375 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
376 }
377 }
378
379 for (auto& layer : layers) {
380 if (layer->changes.get() == 0) {
381 continue;
382 }
383 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
384 onLayerAdded(layer.get());
385 continue;
386 }
387 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
388 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
389 detachFromParent(hierarchy);
390 attachToParent(hierarchy);
391 }
392 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
393 detachFromRelativeParent(hierarchy);
394 attachToRelativeParent(hierarchy);
395 }
396 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
397 hierarchy->mParent->sortChildrenByZOrder();
398 if (hierarchy->mRelativeParent) {
399 hierarchy->mRelativeParent->sortChildrenByZOrder();
400 }
401 }
402 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
403 updateMirrorLayer(layer.get());
404 }
405 }
406
407 for (auto& layer : destroyedLayers) {
408 onLayerDestroyed(layer.get());
409 }
410 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
411 // from our relative parents. This walks down both trees to do so. We can optimize this
412 // further by tracking onscreen, offscreen state in LayerHierarchy.
413 detachHierarchyFromRelativeParent(&mOffscreenRoot);
414 attachHierarchyToRelativeParent(&mRoot);
415 }
416
update(LayerLifecycleManager & layerLifecycleManager)417 void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
418 if (!mInitialized) {
419 SFTRACE_NAME("LayerHierarchyBuilder:init");
420 init(layerLifecycleManager.getLayers());
421 } else if (layerLifecycleManager.getGlobalChanges().test(
422 RequestedLayerState::Changes::Hierarchy)) {
423 SFTRACE_NAME("LayerHierarchyBuilder:update");
424 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
425 } else {
426 return; // nothing to do
427 }
428
429 uint32_t invalidRelativeRoot;
430 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
431 while (hasRelZLoop) {
432 SFTRACE_NAME("FixRelZLoop");
433 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
434 /*overwrite=*/false);
435 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
436 // reinitialize the hierarchy with the updated layer data
437 init(layerLifecycleManager.getLayers());
438 // check if we have any remaining loops
439 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
440 }
441 }
442
getHierarchy() const443 const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
444 return mRoot;
445 }
446
getOffscreenHierarchy() const447 const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
448 return mOffscreenRoot;
449 }
450
getDebugString(uint32_t layerId,uint32_t depth) const451 std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
452 if (depth > 10) return "too deep, loop?";
453 if (layerId == UNASSIGNED_LAYER_ID) return "";
454 auto it = mLayerIdToHierarchy.find(layerId);
455 if (it == mLayerIdToHierarchy.end()) return "not found";
456
457 LayerHierarchy* hierarchy = it->second;
458 if (!hierarchy->mLayer) return "none";
459
460 std::string debug =
461 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
462 if (hierarchy->mRelativeParent) {
463 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
464 }
465 if (hierarchy->mParent) {
466 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
467 }
468 return debug;
469 }
470
getPartialHierarchy(uint32_t layerId,bool childrenOnly) const471 LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
472 bool childrenOnly) const {
473 auto it = mLayerIdToHierarchy.find(layerId);
474 if (it == mLayerIdToHierarchy.end()) return {nullptr};
475
476 LayerHierarchy hierarchy(*it->second, childrenOnly);
477 return hierarchy;
478 }
479
getHierarchyFromId(uint32_t layerId,bool crashOnFailure)480 LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
481 auto it = mLayerIdToHierarchy.find(layerId);
482 if (it == mLayerIdToHierarchy.end()) {
483 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
484 layerId);
485 return nullptr;
486 };
487
488 return it->second;
489 }
490
logSampledChildren(const LayerHierarchy & hierarchy) const491 void LayerHierarchyBuilder::logSampledChildren(const LayerHierarchy& hierarchy) const {
492 LOG(ERROR) << "Dumping random sampling of child layers.";
493 int sampleSize = static_cast<int>(hierarchy.mChildren.size() / 100 + 1);
494 for (const auto& [child, variant] : hierarchy.mChildren) {
495 if (rand() % sampleSize == 0) {
496 LOG(ERROR) << "Child Layer: " << *(child->mLayer);
497 }
498 }
499 }
500
dumpLayerSample(const LayerHierarchy & root) const501 void LayerHierarchyBuilder::dumpLayerSample(const LayerHierarchy& root) const {
502 LOG(ERROR) << "Dumping layer keeping > 20 children alive:";
503 // If mLayer is nullptr, it will be skipped while traversing.
504 if (!root.mLayer && root.mChildren.size() > 20) {
505 LOG(ERROR) << "ROOT has " << root.mChildren.size() << " children";
506 logSampledChildren(root);
507 }
508 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
509 if (hierarchy.mChildren.size() <= 20) {
510 return true;
511 }
512 // mLayer is ensured to be non-null. See LayerHierarchy::traverse.
513 const auto* layer = hierarchy.mLayer;
514 const auto childrenCount = hierarchy.mChildren.size();
515 LOG(ERROR) << "Layer " << *layer << " has " << childrenCount << " children";
516
517 const auto* parent = hierarchy.mParent;
518 while (parent != nullptr) {
519 if (!parent->mLayer) break;
520 LOG(ERROR) << "Parent Layer: " << *(parent->mLayer);
521 parent = parent->mParent;
522 }
523
524 logSampledChildren(hierarchy);
525 // Stop traversing.
526 return false;
527 });
528 LOG(ERROR) << "Dumping random sampled layers.";
529 size_t numLayers = 0;
530 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
531 if (hierarchy.mLayer) numLayers++;
532 if ((rand() % 20 == 13) && hierarchy.mLayer) {
533 LOG(ERROR) << "Layer: " << *(hierarchy.mLayer);
534 }
535 return true;
536 });
537 LOG(ERROR) << "Total layer count: " << numLayers;
538 }
539
540 const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
541 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
542
toString() const543 std::string LayerHierarchy::TraversalPath::toString() const {
544 if (id == UNASSIGNED_LAYER_ID) {
545 return "TraversalPath{ROOT}";
546 }
547 std::stringstream ss;
548 ss << "TraversalPath{.id = " << id;
549
550 if (!mirrorRootIds.empty()) {
551 ss << ", .mirrorRootIds=";
552 for (auto rootId : mirrorRootIds) {
553 ss << rootId << ",";
554 }
555 }
556
557 if (!relativeRootIds.empty()) {
558 ss << ", .relativeRootIds=";
559 for (auto rootId : relativeRootIds) {
560 ss << rootId << ",";
561 }
562 }
563
564 if (hasRelZLoop()) {
565 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
566 }
567 ss << "}";
568 return ss.str();
569 }
570
571 // Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
572 // of scope the TraversalPath is reset to its original state.
ScopedAddToTraversalPath(TraversalPath & traversalPath,uint32_t layerId,LayerHierarchy::Variant variant)573 LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
574 uint32_t layerId,
575 LayerHierarchy::Variant variant)
576 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
577 // Update the traversal id with the child layer id and variant. Parent id and variant are
578 // stored to reset the id upon destruction.
579 traversalPath.id = layerId;
580 traversalPath.variant = variant;
581 if (LayerHierarchy::isMirror(variant)) {
582 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
583 } else if (variant == LayerHierarchy::Variant::Relative) {
584 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
585 layerId) != traversalPath.relativeRootIds.end()) {
586 traversalPath.invalidRelativeRootId = layerId;
587 }
588 traversalPath.relativeRootIds.emplace_back(layerId);
589 } else if (variant == LayerHierarchy::Variant::Detached) {
590 traversalPath.detached = true;
591 }
592 }
~ScopedAddToTraversalPath()593 LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
594 // Reset the traversal id to its original parent state using the state that was saved in
595 // the constructor.
596 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
597 mTraversalPath.mirrorRootIds.pop_back();
598 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
599 mTraversalPath.relativeRootIds.pop_back();
600 }
601 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
602 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
603 }
604 mTraversalPath.id = mParentPath.id;
605 mTraversalPath.variant = mParentPath.variant;
606 mTraversalPath.detached = mParentPath.detached;
607 }
608
609 } // namespace android::surfaceflinger::frontend
610