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,
__anonc5e575490202(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,const LayerHierarchy::TraversalPath & traversalPath,uint32_t depth) const57 void LayerHierarchy::traverse(const Visitor& visitor,
58 const LayerHierarchy::TraversalPath& traversalPath,
59 uint32_t depth) const {
60 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
61 "Cycle detected in LayerHierarchy::traverse. See "
62 "traverse_stack_overflow_transactions.winscope");
63
64 if (mLayer) {
65 bool breakTraversal = !visitor(*this, traversalPath);
66 if (breakTraversal) {
67 return;
68 }
69 }
70
71 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
72 traversalPath.invalidRelativeRootId);
73 for (auto& [child, childVariant] : mChildren) {
74 child->traverse(visitor, traversalPath.makeChild(child->mLayer->id, childVariant),
75 depth + 1);
76 }
77 }
78
traverseInZOrder(const Visitor & visitor,const LayerHierarchy::TraversalPath & traversalPath) const79 void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
80 const 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 child->traverseInZOrder(visitor, traversalPath.makeChild(child->mLayer->id, childVariant));
95 }
96
97 if (traverseThisLayer) {
98 visitor(*this, traversalPath);
99 }
100 }
101
addChild(LayerHierarchy * child,LayerHierarchy::Variant variant)102 void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
103 insertSorted(mChildren, {child, variant});
104 }
105
removeChild(LayerHierarchy * child)106 void LayerHierarchy::removeChild(LayerHierarchy* child) {
107 auto it = std::find_if(mChildren.begin(), mChildren.end(),
108 [child](const std::pair<LayerHierarchy*, Variant>& x) {
109 return x.first == child;
110 });
111 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
112 mChildren.erase(it);
113 }
114
sortChildrenByZOrder()115 void LayerHierarchy::sortChildrenByZOrder() {
116 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
117 }
118
updateChild(LayerHierarchy * hierarchy,LayerHierarchy::Variant variant)119 void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
120 auto it = std::find_if(mChildren.begin(), mChildren.end(),
121 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
122 return child.first == hierarchy;
123 });
124 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
125 it->second = variant;
126 }
127
getLayer() const128 const RequestedLayerState* LayerHierarchy::getLayer() const {
129 return mLayer;
130 }
131
getRelativeParent() const132 const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
133 return mRelativeParent;
134 }
135
getParent() const136 const LayerHierarchy* LayerHierarchy::getParent() const {
137 return mParent;
138 }
139
getDebugStringShort() const140 std::string LayerHierarchy::getDebugStringShort() const {
141 std::string debug = "LayerHierarchy{";
142 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
143 if (mChildren.empty()) {
144 debug += "no children";
145 } else {
146 debug += std::to_string(mChildren.size()) + " children";
147 }
148 return debug + "}";
149 }
150
dump(std::ostream & out,const std::string & prefix,LayerHierarchy::Variant variant,bool isLastChild,bool includeMirroredHierarchy) const151 void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
152 LayerHierarchy::Variant variant, bool isLastChild,
153 bool includeMirroredHierarchy) const {
154 if (!mLayer) {
155 out << " ROOT";
156 } else {
157 out << prefix + (isLastChild ? "└─ " : "├─ ");
158 if (variant == LayerHierarchy::Variant::Relative) {
159 out << "(Relative) ";
160 } else if (LayerHierarchy::isMirror(variant)) {
161 if (!includeMirroredHierarchy) {
162 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
163 return;
164 }
165 out << "(Mirroring) ";
166 }
167
168 out << *mLayer << " pid=" << mLayer->ownerPid.val() << " uid=" << mLayer->ownerUid.val();
169 }
170
171 for (size_t i = 0; i < mChildren.size(); i++) {
172 auto& [child, childVariant] = mChildren[i];
173 if (childVariant == LayerHierarchy::Variant::Detached) continue;
174 const bool lastChild = i == (mChildren.size() - 1);
175 std::string childPrefix = prefix;
176 if (mLayer) {
177 childPrefix += (isLastChild ? " " : "│ ");
178 }
179 out << "\n";
180 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
181 }
182 return;
183 }
184
hasRelZLoop(uint32_t & outInvalidRelativeRoot) const185 bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
186 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
187 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
188 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
189 if (traversalPath.hasRelZLoop()) {
190 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
191 return false;
192 }
193 return true;
194 });
195 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
196 }
197
init(const std::vector<std::unique_ptr<RequestedLayerState>> & layers)198 void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
199 mLayerIdToHierarchy.clear();
200 mHierarchies.clear();
201 mRoot = nullptr;
202 mOffscreenRoot = nullptr;
203
204 mHierarchies.reserve(layers.size());
205 mLayerIdToHierarchy.reserve(layers.size());
206 for (auto& layer : layers) {
207 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
208 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
209 }
210 for (const auto& layer : layers) {
211 onLayerAdded(layer.get());
212 }
213 detachHierarchyFromRelativeParent(&mOffscreenRoot);
214 mInitialized = true;
215 }
216
attachToParent(LayerHierarchy * hierarchy)217 void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
218 auto layer = hierarchy->mLayer;
219 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
220 ? LayerHierarchy::Variant::Detached
221 : LayerHierarchy::Variant::Attached;
222
223 LayerHierarchy* parent;
224
225 if (layer->parentId != UNASSIGNED_LAYER_ID) {
226 parent = getHierarchyFromId(layer->parentId);
227 } else if (layer->canBeRoot) {
228 parent = &mRoot;
229 } else {
230 parent = &mOffscreenRoot;
231 }
232 parent->addChild(hierarchy, type);
233 hierarchy->mParent = parent;
234 }
235
detachFromParent(LayerHierarchy * hierarchy)236 void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
237 hierarchy->mParent->removeChild(hierarchy);
238 hierarchy->mParent = nullptr;
239 }
240
attachToRelativeParent(LayerHierarchy * hierarchy)241 void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
242 auto layer = hierarchy->mLayer;
243 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
244 return;
245 }
246
247 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
248 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
249 } else {
250 hierarchy->mRelativeParent = &mOffscreenRoot;
251 }
252 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
253 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
254 }
255
detachFromRelativeParent(LayerHierarchy * hierarchy)256 void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
257 if (hierarchy->mRelativeParent) {
258 hierarchy->mRelativeParent->removeChild(hierarchy);
259 }
260 hierarchy->mRelativeParent = nullptr;
261 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
262 }
263
getDescendants(LayerHierarchy * root)264 std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) {
265 std::vector<LayerHierarchy*> hierarchies;
266 hierarchies.push_back(root);
267 std::vector<LayerHierarchy*> descendants;
268 for (size_t i = 0; i < hierarchies.size(); i++) {
269 LayerHierarchy* hierarchy = hierarchies[i];
270 if (hierarchy->mLayer) {
271 descendants.push_back(hierarchy);
272 }
273 for (auto& [child, childVariant] : hierarchy->mChildren) {
274 if (childVariant == LayerHierarchy::Variant::Detached ||
275 childVariant == LayerHierarchy::Variant::Attached) {
276 hierarchies.push_back(child);
277 }
278 }
279 }
280 return descendants;
281 }
282
attachHierarchyToRelativeParent(LayerHierarchy * root)283 void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
284 std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root);
285 for (LayerHierarchy* hierarchy : hierarchiesToAttach) {
286 attachToRelativeParent(hierarchy);
287 }
288 }
289
detachHierarchyFromRelativeParent(LayerHierarchy * root)290 void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
291 std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root);
292 for (LayerHierarchy* hierarchy : hierarchiesToDetach) {
293 detachFromRelativeParent(hierarchy);
294 }
295 }
296
onLayerAdded(RequestedLayerState * layer)297 void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
298 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
299 attachToParent(hierarchy);
300 attachToRelativeParent(hierarchy);
301
302 for (uint32_t mirrorId : layer->mirrorIds) {
303 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
304 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
305 }
306 if (FlagManager::getInstance().detached_mirror()) {
307 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
308 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
309 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
310 }
311 }
312 }
313
onLayerDestroyed(RequestedLayerState * layer)314 void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
315 LLOGV(layer->id, "");
316 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
317 if (!hierarchy) {
318 // Layer was never part of the hierarchy if it was created and destroyed in the same
319 // transaction.
320 return;
321 }
322 // detach from parent
323 detachFromRelativeParent(hierarchy);
324 detachFromParent(hierarchy);
325
326 // detach children
327 for (auto& [child, variant] : hierarchy->mChildren) {
328 if (variant == LayerHierarchy::Variant::Attached ||
329 variant == LayerHierarchy::Variant::Detached) {
330 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
331 child->mParent = &mOffscreenRoot;
332 } else if (variant == LayerHierarchy::Variant::Relative) {
333 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
334 child->mRelativeParent = &mOffscreenRoot;
335 }
336 }
337
338 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
339 return layerHierarchy.get() == hierarchy;
340 });
341 mLayerIdToHierarchy.erase(layer->id);
342 }
343
updateMirrorLayer(RequestedLayerState * layer)344 void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
345 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
346 auto it = hierarchy->mChildren.begin();
347 while (it != hierarchy->mChildren.end()) {
348 if (LayerHierarchy::isMirror(it->second)) {
349 it = hierarchy->mChildren.erase(it);
350 } else {
351 it++;
352 }
353 }
354
355 for (uint32_t mirrorId : layer->mirrorIds) {
356 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
357 }
358 if (FlagManager::getInstance().detached_mirror()) {
359 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
360 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
361 LayerHierarchy::Variant::Detached_Mirror);
362 }
363 }
364 }
365
doUpdate(const std::vector<std::unique_ptr<RequestedLayerState>> & layers,const std::vector<std::unique_ptr<RequestedLayerState>> & destroyedLayers)366 void LayerHierarchyBuilder::doUpdate(
367 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
368 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
369 // rebuild map
370 for (auto& layer : layers) {
371 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
372 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
373 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
374 }
375 }
376
377 for (auto& layer : layers) {
378 if (layer->changes.get() == 0) {
379 continue;
380 }
381 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
382 onLayerAdded(layer.get());
383 continue;
384 }
385 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
386 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
387 detachFromParent(hierarchy);
388 attachToParent(hierarchy);
389 }
390 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
391 detachFromRelativeParent(hierarchy);
392 attachToRelativeParent(hierarchy);
393 }
394 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
395 hierarchy->mParent->sortChildrenByZOrder();
396 if (hierarchy->mRelativeParent) {
397 hierarchy->mRelativeParent->sortChildrenByZOrder();
398 }
399 }
400 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
401 updateMirrorLayer(layer.get());
402 }
403 }
404
405 for (auto& layer : destroyedLayers) {
406 onLayerDestroyed(layer.get());
407 }
408 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
409 // from our relative parents. This walks down both trees to do so. We can optimize this
410 // further by tracking onscreen, offscreen state in LayerHierarchy.
411 detachHierarchyFromRelativeParent(&mOffscreenRoot);
412 attachHierarchyToRelativeParent(&mRoot);
413 }
414
update(LayerLifecycleManager & layerLifecycleManager)415 void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
416 if (!mInitialized) {
417 SFTRACE_NAME("LayerHierarchyBuilder:init");
418 init(layerLifecycleManager.getLayers());
419 } else if (layerLifecycleManager.getGlobalChanges().test(
420 RequestedLayerState::Changes::Hierarchy)) {
421 SFTRACE_NAME("LayerHierarchyBuilder:update");
422 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
423 } else {
424 return; // nothing to do
425 }
426
427 uint32_t invalidRelativeRoot;
428 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
429 while (hasRelZLoop) {
430 SFTRACE_NAME("FixRelZLoop");
431 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
432 /*overwrite=*/false);
433 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
434 // reinitialize the hierarchy with the updated layer data
435 init(layerLifecycleManager.getLayers());
436 // check if we have any remaining loops
437 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
438 }
439 }
440
getHierarchy() const441 const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
442 return mRoot;
443 }
444
getOffscreenHierarchy() const445 const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
446 return mOffscreenRoot;
447 }
448
getDebugString(uint32_t layerId,uint32_t depth) const449 std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
450 if (depth > 10) return "too deep, loop?";
451 if (layerId == UNASSIGNED_LAYER_ID) return "";
452 auto it = mLayerIdToHierarchy.find(layerId);
453 if (it == mLayerIdToHierarchy.end()) return "not found";
454
455 LayerHierarchy* hierarchy = it->second;
456 if (!hierarchy->mLayer) return "none";
457
458 std::string debug =
459 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
460 if (hierarchy->mRelativeParent) {
461 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
462 }
463 if (hierarchy->mParent) {
464 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
465 }
466 return debug;
467 }
468
getPartialHierarchy(uint32_t layerId,bool childrenOnly) const469 LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
470 bool childrenOnly) const {
471 auto it = mLayerIdToHierarchy.find(layerId);
472 if (it == mLayerIdToHierarchy.end()) return {nullptr};
473
474 LayerHierarchy hierarchy(*it->second, childrenOnly);
475 return hierarchy;
476 }
477
getHierarchyFromId(uint32_t layerId,bool crashOnFailure)478 LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
479 auto it = mLayerIdToHierarchy.find(layerId);
480 if (it == mLayerIdToHierarchy.end()) {
481 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
482 layerId);
483 return nullptr;
484 };
485
486 return it->second;
487 }
488
logSampledChildren(const LayerHierarchy & hierarchy) const489 void LayerHierarchyBuilder::logSampledChildren(const LayerHierarchy& hierarchy) const {
490 LOG(ERROR) << "Dumping random sampling of child layers.";
491 int sampleSize = static_cast<int>(hierarchy.mChildren.size() / 100 + 1);
492 for (const auto& [child, variant] : hierarchy.mChildren) {
493 if (rand() % sampleSize == 0) {
494 LOG(ERROR) << "Child Layer: " << *(child->mLayer);
495 }
496 }
497 }
498
dumpLayerSample(const LayerHierarchy & root) const499 void LayerHierarchyBuilder::dumpLayerSample(const LayerHierarchy& root) const {
500 LOG(ERROR) << "Dumping layer keeping > 20 children alive:";
501 // If mLayer is nullptr, it will be skipped while traversing.
502 if (!root.mLayer && root.mChildren.size() > 20) {
503 LOG(ERROR) << "ROOT has " << root.mChildren.size() << " children";
504 logSampledChildren(root);
505 }
506 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
507 if (hierarchy.mChildren.size() <= 20) {
508 return true;
509 }
510 // mLayer is ensured to be non-null. See LayerHierarchy::traverse.
511 const auto* layer = hierarchy.mLayer;
512 const auto childrenCount = hierarchy.mChildren.size();
513 LOG(ERROR) << "Layer " << *layer << " has " << childrenCount << " children";
514
515 const auto* parent = hierarchy.mParent;
516 while (parent != nullptr) {
517 if (!parent->mLayer) break;
518 LOG(ERROR) << "Parent Layer: " << *(parent->mLayer);
519 parent = parent->mParent;
520 }
521
522 logSampledChildren(hierarchy);
523 // Stop traversing.
524 return false;
525 });
526 LOG(ERROR) << "Dumping random sampled layers.";
527 size_t numLayers = 0;
528 root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool {
529 if (hierarchy.mLayer) numLayers++;
530 if ((rand() % 20 == 13) && hierarchy.mLayer) {
531 LOG(ERROR) << "Layer: " << *(hierarchy.mLayer);
532 }
533 return true;
534 });
535 LOG(ERROR) << "Total layer count: " << numLayers;
536 }
537
538 const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
539 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
540
toString() const541 std::string LayerHierarchy::TraversalPath::toString() const {
542 if (id == UNASSIGNED_LAYER_ID) {
543 return "TraversalPath{ROOT}";
544 }
545 std::stringstream ss;
546 ss << "TraversalPath{.id = " << id;
547
548 if (!mirrorRootIds.empty()) {
549 ss << ", .mirrorRootIds=";
550 for (auto rootId : mirrorRootIds) {
551 ss << rootId << ",";
552 }
553 }
554
555 if (!relativeRootIds.empty()) {
556 ss << ", .relativeRootIds=";
557 for (auto rootId : relativeRootIds) {
558 ss << rootId << ",";
559 }
560 }
561
562 if (hasRelZLoop()) {
563 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
564 }
565 ss << "}";
566 return ss.str();
567 }
568
makeChild(uint32_t layerId,LayerHierarchy::Variant variant) const569 LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::makeChild(
570 uint32_t layerId, LayerHierarchy::Variant variant) const {
571 TraversalPath child{*this};
572 child.id = layerId;
573 child.variant = variant;
574 if (LayerHierarchy::isMirror(variant)) {
575 child.mirrorRootIds.emplace_back(id);
576 } else if (variant == LayerHierarchy::Variant::Relative) {
577 if (std::find(relativeRootIds.begin(), relativeRootIds.end(), layerId) !=
578 relativeRootIds.end()) {
579 child.invalidRelativeRootId = layerId;
580 }
581 child.relativeRootIds.emplace_back(layerId);
582 } else if (variant == LayerHierarchy::Variant::Detached) {
583 child.detached = true;
584 }
585 return child;
586 }
587
588 } // namespace android::surfaceflinger::frontend
589