blob: 2b20de38d7b486eba773701b90ae178d48055eb3 [file] [log] [blame]
Vishnu Nair04f89692022-11-16 23:21:05 +00001/*
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
Vishnu Naira02943f2023-06-03 13:44:46 -070019#define LOG_TAG "SurfaceFlinger"
Vishnu Nair04f89692022-11-16 23:21:05 +000020
21#include "LayerHierarchy.h"
Vishnu Nair80a5a702023-02-11 01:21:51 +000022#include "LayerLog.h"
Vishnu Nair04f89692022-11-16 23:21:05 +000023#include "SwapErase.h"
24
25namespace android::surfaceflinger::frontend {
26
27namespace {
28auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs,
29 const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) {
30 auto lhsLayer = lhs.first->getLayer();
31 auto rhsLayer = rhs.first->getLayer();
Vishnu Naircfb2d252023-01-19 04:44:02 +000032 if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) {
Vishnu Nair444f3952023-04-11 13:01:02 -070033 return lhsLayer->layerStack.id < rhsLayer->layerStack.id;
Vishnu Nair04f89692022-11-16 23:21:05 +000034 }
35 if (lhsLayer->z != rhsLayer->z) {
36 return lhsLayer->z < rhsLayer->z;
37 }
38 return lhsLayer->id < rhsLayer->id;
39};
40
41void insertSorted(std::vector<std::pair<LayerHierarchy*, LayerHierarchy::Variant>>& vec,
42 std::pair<LayerHierarchy*, LayerHierarchy::Variant> value) {
43 auto it = std::upper_bound(vec.begin(), vec.end(), value, layerZCompare);
44 vec.insert(it, std::move(value));
45}
46} // namespace
47
48LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {}
49
50LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) {
51 mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer;
52 mChildren = hierarchy.mChildren;
53}
54
Melody Hsubb2ec6a2024-05-27 22:18:05 +000055void LayerHierarchy::traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& traversalPath,
56 uint32_t depth) const {
57 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50,
58 "Cycle detected in LayerHierarchy::traverse. See "
59 "traverse_stack_overflow_transactions.winscope");
60
Vishnu Nair04f89692022-11-16 23:21:05 +000061 if (mLayer) {
62 bool breakTraversal = !visitor(*this, traversalPath);
63 if (breakTraversal) {
64 return;
65 }
66 }
Vishnu Nair606d9d02023-08-19 14:20:18 -070067
68 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
69 traversalPath.invalidRelativeRootId);
Vishnu Nair04f89692022-11-16 23:21:05 +000070 for (auto& [child, childVariant] : mChildren) {
71 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
72 childVariant);
Melody Hsubb2ec6a2024-05-27 22:18:05 +000073 child->traverse(visitor, traversalPath, depth + 1);
Vishnu Nair04f89692022-11-16 23:21:05 +000074 }
75}
76
77void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
78 LayerHierarchy::TraversalPath& traversalPath) const {
79 bool traverseThisLayer = (mLayer != nullptr);
80 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
81 auto& [child, childVariant] = *it;
82 if (traverseThisLayer && child->getLayer()->z >= 0) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000083 traverseThisLayer = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000084 bool breakTraversal = !visitor(*this, traversalPath);
85 if (breakTraversal) {
86 return;
87 }
Vishnu Nair04f89692022-11-16 23:21:05 +000088 }
89 if (childVariant == LayerHierarchy::Variant::Detached) {
90 continue;
91 }
92 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
93 childVariant);
94 child->traverseInZOrder(visitor, traversalPath);
95 }
96
97 if (traverseThisLayer) {
98 visitor(*this, traversalPath);
99 }
100}
101
102void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
103 insertSorted(mChildren, {child, variant});
104}
105
106void 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 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700111 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
Vishnu Nair04f89692022-11-16 23:21:05 +0000112 mChildren.erase(it);
113}
114
115void LayerHierarchy::sortChildrenByZOrder() {
116 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
117}
118
119void 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 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700124 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
125 it->second = variant;
Vishnu Nair04f89692022-11-16 23:21:05 +0000126}
127
128const RequestedLayerState* LayerHierarchy::getLayer() const {
129 return mLayer;
130}
131
Vishnu Nairea6ff812023-02-27 17:41:39 +0000132const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
133 return mRelativeParent;
134}
135
136const LayerHierarchy* LayerHierarchy::getParent() const {
137 return mParent;
138}
139
Vishnu Nair04f89692022-11-16 23:21:05 +0000140std::string LayerHierarchy::getDebugStringShort() const {
141 std::string debug = "LayerHierarchy{";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000142 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
Vishnu Nair04f89692022-11-16 23:21:05 +0000143 if (mChildren.empty()) {
144 debug += "no children";
145 } else {
146 debug += std::to_string(mChildren.size()) + " children";
147 }
148 return debug + "}";
149}
150
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000151void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
Vishnu Nair6f878312023-09-08 11:05:01 -0700152 LayerHierarchy::Variant variant, bool isLastChild,
153 bool includeMirroredHierarchy) const {
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000154 if (!mLayer) {
155 out << " ROOT";
156 } else {
157 out << prefix + (isLastChild ? "└─ " : "├─ ");
158 if (variant == LayerHierarchy::Variant::Relative) {
159 out << "(Relative) ";
Vishnu Nair491827d2024-04-29 23:43:26 +0000160 } else if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700161 if (!includeMirroredHierarchy) {
162 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
163 return;
164 }
165 out << "(Mirroring) ";
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000166 }
167 out << *mLayer;
Vishnu Nair04f89692022-11-16 23:21:05 +0000168 }
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000169
170 for (size_t i = 0; i < mChildren.size(); i++) {
171 auto& [child, childVariant] = mChildren[i];
172 if (childVariant == LayerHierarchy::Variant::Detached) continue;
173 const bool lastChild = i == (mChildren.size() - 1);
174 std::string childPrefix = prefix;
175 if (mLayer) {
176 childPrefix += (isLastChild ? " " : "│ ");
177 }
178 out << "\n";
Vishnu Nair6f878312023-09-08 11:05:01 -0700179 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000180 }
181 return;
Vishnu Nair04f89692022-11-16 23:21:05 +0000182}
183
184bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
185 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
186 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
187 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
188 if (traversalPath.hasRelZLoop()) {
189 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
190 return false;
191 }
192 return true;
193 });
194 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
195}
196
Vishnu Naira0292282023-12-16 14:32:00 -0800197void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
198 mLayerIdToHierarchy.clear();
199 mHierarchies.clear();
200 mRoot = nullptr;
201 mOffscreenRoot = nullptr;
202
Vishnu Nair04f89692022-11-16 23:21:05 +0000203 mHierarchies.reserve(layers.size());
204 mLayerIdToHierarchy.reserve(layers.size());
205 for (auto& layer : layers) {
206 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
207 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
208 }
209 for (const auto& layer : layers) {
210 onLayerAdded(layer.get());
211 }
212 detachHierarchyFromRelativeParent(&mOffscreenRoot);
Vishnu Naira0292282023-12-16 14:32:00 -0800213 mInitialized = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000214}
215
216void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
217 auto layer = hierarchy->mLayer;
218 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
219 ? LayerHierarchy::Variant::Detached
220 : LayerHierarchy::Variant::Attached;
221
222 LayerHierarchy* parent;
223
224 if (layer->parentId != UNASSIGNED_LAYER_ID) {
225 parent = getHierarchyFromId(layer->parentId);
226 } else if (layer->canBeRoot) {
227 parent = &mRoot;
228 } else {
229 parent = &mOffscreenRoot;
230 }
231 parent->addChild(hierarchy, type);
232 hierarchy->mParent = parent;
233}
234
235void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
236 hierarchy->mParent->removeChild(hierarchy);
237 hierarchy->mParent = nullptr;
238}
239
240void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
241 auto layer = hierarchy->mLayer;
242 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
243 return;
244 }
245
246 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
247 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
248 } else {
249 hierarchy->mRelativeParent = &mOffscreenRoot;
250 }
251 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
252 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
253}
254
255void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
256 if (hierarchy->mRelativeParent) {
257 hierarchy->mRelativeParent->removeChild(hierarchy);
258 }
259 hierarchy->mRelativeParent = nullptr;
260 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
261}
262
263void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
264 if (root->mLayer) {
265 attachToRelativeParent(root);
266 }
267 for (auto& [child, childVariant] : root->mChildren) {
268 if (childVariant == LayerHierarchy::Variant::Detached ||
269 childVariant == LayerHierarchy::Variant::Attached) {
270 attachHierarchyToRelativeParent(child);
271 }
272 }
273}
274
275void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
276 if (root->mLayer) {
277 detachFromRelativeParent(root);
278 }
279 for (auto& [child, childVariant] : root->mChildren) {
280 if (childVariant == LayerHierarchy::Variant::Detached ||
281 childVariant == LayerHierarchy::Variant::Attached) {
282 detachHierarchyFromRelativeParent(child);
283 }
284 }
285}
286
287void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
288 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
289 attachToParent(hierarchy);
290 attachToRelativeParent(hierarchy);
291
Vishnu Naira9c43762023-01-27 19:10:25 +0000292 for (uint32_t mirrorId : layer->mirrorIds) {
293 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000294 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
295 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000296 if (FlagManager::getInstance().detached_mirror()) {
297 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
298 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
299 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
300 }
301 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000302}
303
304void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000305 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000306 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
307 if (!hierarchy) {
308 // Layer was never part of the hierarchy if it was created and destroyed in the same
309 // transaction.
310 return;
311 }
312 // detach from parent
313 detachFromRelativeParent(hierarchy);
314 detachFromParent(hierarchy);
315
316 // detach children
317 for (auto& [child, variant] : hierarchy->mChildren) {
318 if (variant == LayerHierarchy::Variant::Attached ||
319 variant == LayerHierarchy::Variant::Detached) {
320 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
321 child->mParent = &mOffscreenRoot;
322 } else if (variant == LayerHierarchy::Variant::Relative) {
323 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
324 child->mRelativeParent = &mOffscreenRoot;
325 }
326 }
327
328 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
329 return layerHierarchy.get() == hierarchy;
330 });
331 mLayerIdToHierarchy.erase(layer->id);
332}
333
334void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
335 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
336 auto it = hierarchy->mChildren.begin();
337 while (it != hierarchy->mChildren.end()) {
Vishnu Nair491827d2024-04-29 23:43:26 +0000338 if (LayerHierarchy::isMirror(it->second)) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000339 it = hierarchy->mChildren.erase(it);
340 } else {
341 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000342 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000343 }
344
Vishnu Naira9c43762023-01-27 19:10:25 +0000345 for (uint32_t mirrorId : layer->mirrorIds) {
346 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000347 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000348 if (FlagManager::getInstance().detached_mirror()) {
349 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
350 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
351 LayerHierarchy::Variant::Detached_Mirror);
352 }
353 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000354}
355
Vishnu Naira0292282023-12-16 14:32:00 -0800356void LayerHierarchyBuilder::doUpdate(
Vishnu Nair04f89692022-11-16 23:21:05 +0000357 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
358 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
359 // rebuild map
360 for (auto& layer : layers) {
361 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
362 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
363 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
364 }
365 }
366
367 for (auto& layer : layers) {
368 if (layer->changes.get() == 0) {
369 continue;
370 }
371 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
372 onLayerAdded(layer.get());
373 continue;
374 }
375 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
376 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
377 detachFromParent(hierarchy);
378 attachToParent(hierarchy);
379 }
380 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
381 detachFromRelativeParent(hierarchy);
382 attachToRelativeParent(hierarchy);
383 }
384 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
385 hierarchy->mParent->sortChildrenByZOrder();
386 if (hierarchy->mRelativeParent) {
387 hierarchy->mRelativeParent->sortChildrenByZOrder();
388 }
389 }
390 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
391 updateMirrorLayer(layer.get());
392 }
393 }
394
395 for (auto& layer : destroyedLayers) {
396 onLayerDestroyed(layer.get());
397 }
398 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
399 // from our relative parents. This walks down both trees to do so. We can optimize this
400 // further by tracking onscreen, offscreen state in LayerHierarchy.
401 detachHierarchyFromRelativeParent(&mOffscreenRoot);
402 attachHierarchyToRelativeParent(&mRoot);
403}
404
Vishnu Naira0292282023-12-16 14:32:00 -0800405void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
406 if (!mInitialized) {
407 ATRACE_NAME("LayerHierarchyBuilder:init");
408 init(layerLifecycleManager.getLayers());
409 } else if (layerLifecycleManager.getGlobalChanges().test(
410 RequestedLayerState::Changes::Hierarchy)) {
411 ATRACE_NAME("LayerHierarchyBuilder:update");
412 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
413 } else {
414 return; // nothing to do
415 }
416
417 uint32_t invalidRelativeRoot;
418 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
419 while (hasRelZLoop) {
420 ATRACE_NAME("FixRelZLoop");
421 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
422 /*overwrite=*/false);
423 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
424 // reinitialize the hierarchy with the updated layer data
425 init(layerLifecycleManager.getLayers());
426 // check if we have any remaining loops
427 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
428 }
429}
430
Vishnu Nair04f89692022-11-16 23:21:05 +0000431const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
432 return mRoot;
433}
434
435const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
436 return mOffscreenRoot;
437}
438
439std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
440 if (depth > 10) return "too deep, loop?";
441 if (layerId == UNASSIGNED_LAYER_ID) return "";
442 auto it = mLayerIdToHierarchy.find(layerId);
443 if (it == mLayerIdToHierarchy.end()) return "not found";
444
445 LayerHierarchy* hierarchy = it->second;
446 if (!hierarchy->mLayer) return "none";
447
448 std::string debug =
449 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
450 if (hierarchy->mRelativeParent) {
451 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
452 }
453 if (hierarchy->mParent) {
454 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
455 }
456 return debug;
457}
458
459LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
460 bool childrenOnly) const {
461 auto it = mLayerIdToHierarchy.find(layerId);
462 if (it == mLayerIdToHierarchy.end()) return {nullptr};
463
464 LayerHierarchy hierarchy(*it->second, childrenOnly);
465 return hierarchy;
466}
467
468LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
469 auto it = mLayerIdToHierarchy.find(layerId);
470 if (it == mLayerIdToHierarchy.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700471 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
472 layerId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000473 return nullptr;
474 };
475
476 return it->second;
477}
478
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000479const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000480 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
481
482std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000483 if (id == UNASSIGNED_LAYER_ID) {
484 return "TraversalPath{ROOT}";
485 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000486 std::stringstream ss;
487 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000488
Vishnu Nair6f878312023-09-08 11:05:01 -0700489 if (!mirrorRootIds.empty()) {
490 ss << ", .mirrorRootIds=";
491 for (auto rootId : mirrorRootIds) {
492 ss << rootId << ",";
493 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000494 }
495
496 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000497 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000498 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000499 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000500 }
501 }
502
503 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000504 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000505 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000506 ss << "}";
507 return ss.str();
508}
Vishnu Nair04f89692022-11-16 23:21:05 +0000509
Vishnu Nair04f89692022-11-16 23:21:05 +0000510// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
511// of scope the TraversalPath is reset to its original state.
512LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
513 uint32_t layerId,
514 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000515 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000516 // Update the traversal id with the child layer id and variant. Parent id and variant are
517 // stored to reset the id upon destruction.
518 traversalPath.id = layerId;
519 traversalPath.variant = variant;
Vishnu Nair491827d2024-04-29 23:43:26 +0000520 if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700521 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000522 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000523 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
524 layerId) != traversalPath.relativeRootIds.end()) {
525 traversalPath.invalidRelativeRootId = layerId;
526 }
527 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000528 } else if (variant == LayerHierarchy::Variant::Detached) {
529 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000530 }
531}
532LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
533 // Reset the traversal id to its original parent state using the state that was saved in
534 // the constructor.
Vishnu Nair491827d2024-04-29 23:43:26 +0000535 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700536 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000537 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000538 mTraversalPath.relativeRootIds.pop_back();
539 }
540 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
541 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
542 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000543 mTraversalPath.id = mParentPath.id;
544 mTraversalPath.variant = mParentPath.variant;
545 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000546}
547
548} // namespace android::surfaceflinger::frontend