blob: 0dcbb3c78d3b48b0e0f2dbf311912f8ffaeeccb6 [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
55void LayerHierarchy::traverse(const Visitor& visitor,
56 LayerHierarchy::TraversalPath& traversalPath) const {
57 if (mLayer) {
58 bool breakTraversal = !visitor(*this, traversalPath);
59 if (breakTraversal) {
60 return;
61 }
62 }
Vishnu Nair606d9d02023-08-19 14:20:18 -070063
64 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d",
65 traversalPath.invalidRelativeRootId);
Vishnu Nair04f89692022-11-16 23:21:05 +000066 for (auto& [child, childVariant] : mChildren) {
67 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
68 childVariant);
69 child->traverse(visitor, traversalPath);
70 }
71}
72
73void LayerHierarchy::traverseInZOrder(const Visitor& visitor,
74 LayerHierarchy::TraversalPath& traversalPath) const {
75 bool traverseThisLayer = (mLayer != nullptr);
76 for (auto it = mChildren.begin(); it < mChildren.end(); it++) {
77 auto& [child, childVariant] = *it;
78 if (traverseThisLayer && child->getLayer()->z >= 0) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000079 traverseThisLayer = false;
Vishnu Nair04f89692022-11-16 23:21:05 +000080 bool breakTraversal = !visitor(*this, traversalPath);
81 if (breakTraversal) {
82 return;
83 }
Vishnu Nair04f89692022-11-16 23:21:05 +000084 }
85 if (childVariant == LayerHierarchy::Variant::Detached) {
86 continue;
87 }
88 ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id,
89 childVariant);
90 child->traverseInZOrder(visitor, traversalPath);
91 }
92
93 if (traverseThisLayer) {
94 visitor(*this, traversalPath);
95 }
96}
97
98void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) {
99 insertSorted(mChildren, {child, variant});
100}
101
102void LayerHierarchy::removeChild(LayerHierarchy* child) {
103 auto it = std::find_if(mChildren.begin(), mChildren.end(),
104 [child](const std::pair<LayerHierarchy*, Variant>& x) {
105 return x.first == child;
106 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700107 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
Vishnu Nair04f89692022-11-16 23:21:05 +0000108 mChildren.erase(it);
109}
110
111void LayerHierarchy::sortChildrenByZOrder() {
112 std::sort(mChildren.begin(), mChildren.end(), layerZCompare);
113}
114
115void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) {
116 auto it = std::find_if(mChildren.begin(), mChildren.end(),
117 [hierarchy](std::pair<LayerHierarchy*, Variant>& child) {
118 return child.first == hierarchy;
119 });
Vishnu Nair606d9d02023-08-19 14:20:18 -0700120 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!");
121 it->second = variant;
Vishnu Nair04f89692022-11-16 23:21:05 +0000122}
123
124const RequestedLayerState* LayerHierarchy::getLayer() const {
125 return mLayer;
126}
127
Vishnu Nairea6ff812023-02-27 17:41:39 +0000128const LayerHierarchy* LayerHierarchy::getRelativeParent() const {
129 return mRelativeParent;
130}
131
132const LayerHierarchy* LayerHierarchy::getParent() const {
133 return mParent;
134}
135
Vishnu Nair04f89692022-11-16 23:21:05 +0000136std::string LayerHierarchy::getDebugStringShort() const {
137 std::string debug = "LayerHierarchy{";
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000138 debug += ((mLayer) ? mLayer->getDebugString() : "root") + " ";
Vishnu Nair04f89692022-11-16 23:21:05 +0000139 if (mChildren.empty()) {
140 debug += "no children";
141 } else {
142 debug += std::to_string(mChildren.size()) + " children";
143 }
144 return debug + "}";
145}
146
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000147void LayerHierarchy::dump(std::ostream& out, const std::string& prefix,
Vishnu Nair6f878312023-09-08 11:05:01 -0700148 LayerHierarchy::Variant variant, bool isLastChild,
149 bool includeMirroredHierarchy) const {
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000150 if (!mLayer) {
151 out << " ROOT";
152 } else {
153 out << prefix + (isLastChild ? "└─ " : "├─ ");
154 if (variant == LayerHierarchy::Variant::Relative) {
155 out << "(Relative) ";
Vishnu Nair491827d2024-04-29 23:43:26 +0000156 } else if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700157 if (!includeMirroredHierarchy) {
158 out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ...";
159 return;
160 }
161 out << "(Mirroring) ";
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000162 }
163 out << *mLayer;
Vishnu Nair04f89692022-11-16 23:21:05 +0000164 }
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000165
166 for (size_t i = 0; i < mChildren.size(); i++) {
167 auto& [child, childVariant] = mChildren[i];
168 if (childVariant == LayerHierarchy::Variant::Detached) continue;
169 const bool lastChild = i == (mChildren.size() - 1);
170 std::string childPrefix = prefix;
171 if (mLayer) {
172 childPrefix += (isLastChild ? " " : "│ ");
173 }
174 out << "\n";
Vishnu Nair6f878312023-09-08 11:05:01 -0700175 child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000176 }
177 return;
Vishnu Nair04f89692022-11-16 23:21:05 +0000178}
179
180bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const {
181 outInvalidRelativeRoot = UNASSIGNED_LAYER_ID;
182 traverse([&outInvalidRelativeRoot](const LayerHierarchy&,
183 const LayerHierarchy::TraversalPath& traversalPath) -> bool {
184 if (traversalPath.hasRelZLoop()) {
185 outInvalidRelativeRoot = traversalPath.invalidRelativeRootId;
186 return false;
187 }
188 return true;
189 });
190 return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID;
191}
192
Vishnu Naira0292282023-12-16 14:32:00 -0800193void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) {
194 mLayerIdToHierarchy.clear();
195 mHierarchies.clear();
196 mRoot = nullptr;
197 mOffscreenRoot = nullptr;
198
Vishnu Nair04f89692022-11-16 23:21:05 +0000199 mHierarchies.reserve(layers.size());
200 mLayerIdToHierarchy.reserve(layers.size());
201 for (auto& layer : layers) {
202 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
203 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
204 }
205 for (const auto& layer : layers) {
206 onLayerAdded(layer.get());
207 }
208 detachHierarchyFromRelativeParent(&mOffscreenRoot);
Vishnu Naira0292282023-12-16 14:32:00 -0800209 mInitialized = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000210}
211
212void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) {
213 auto layer = hierarchy->mLayer;
214 LayerHierarchy::Variant type = layer->hasValidRelativeParent()
215 ? LayerHierarchy::Variant::Detached
216 : LayerHierarchy::Variant::Attached;
217
218 LayerHierarchy* parent;
219
220 if (layer->parentId != UNASSIGNED_LAYER_ID) {
221 parent = getHierarchyFromId(layer->parentId);
222 } else if (layer->canBeRoot) {
223 parent = &mRoot;
224 } else {
225 parent = &mOffscreenRoot;
226 }
227 parent->addChild(hierarchy, type);
228 hierarchy->mParent = parent;
229}
230
231void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) {
232 hierarchy->mParent->removeChild(hierarchy);
233 hierarchy->mParent = nullptr;
234}
235
236void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) {
237 auto layer = hierarchy->mLayer;
238 if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) {
239 return;
240 }
241
242 if (layer->relativeParentId != UNASSIGNED_LAYER_ID) {
243 hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId);
244 } else {
245 hierarchy->mRelativeParent = &mOffscreenRoot;
246 }
247 hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative);
248 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached);
249}
250
251void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) {
252 if (hierarchy->mRelativeParent) {
253 hierarchy->mRelativeParent->removeChild(hierarchy);
254 }
255 hierarchy->mRelativeParent = nullptr;
256 hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached);
257}
258
259void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) {
260 if (root->mLayer) {
261 attachToRelativeParent(root);
262 }
263 for (auto& [child, childVariant] : root->mChildren) {
264 if (childVariant == LayerHierarchy::Variant::Detached ||
265 childVariant == LayerHierarchy::Variant::Attached) {
266 attachHierarchyToRelativeParent(child);
267 }
268 }
269}
270
271void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) {
272 if (root->mLayer) {
273 detachFromRelativeParent(root);
274 }
275 for (auto& [child, childVariant] : root->mChildren) {
276 if (childVariant == LayerHierarchy::Variant::Detached ||
277 childVariant == LayerHierarchy::Variant::Attached) {
278 detachHierarchyFromRelativeParent(child);
279 }
280 }
281}
282
283void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) {
284 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
285 attachToParent(hierarchy);
286 attachToRelativeParent(hierarchy);
287
Vishnu Naira9c43762023-01-27 19:10:25 +0000288 for (uint32_t mirrorId : layer->mirrorIds) {
289 LayerHierarchy* mirror = getHierarchyFromId(mirrorId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000290 hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror);
291 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000292 if (FlagManager::getInstance().detached_mirror()) {
293 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
294 LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror);
295 hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror);
296 }
297 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000298}
299
300void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000301 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000302 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
303 if (!hierarchy) {
304 // Layer was never part of the hierarchy if it was created and destroyed in the same
305 // transaction.
306 return;
307 }
308 // detach from parent
309 detachFromRelativeParent(hierarchy);
310 detachFromParent(hierarchy);
311
312 // detach children
313 for (auto& [child, variant] : hierarchy->mChildren) {
314 if (variant == LayerHierarchy::Variant::Attached ||
315 variant == LayerHierarchy::Variant::Detached) {
316 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
317 child->mParent = &mOffscreenRoot;
318 } else if (variant == LayerHierarchy::Variant::Relative) {
319 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
320 child->mRelativeParent = &mOffscreenRoot;
321 }
322 }
323
324 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
325 return layerHierarchy.get() == hierarchy;
326 });
327 mLayerIdToHierarchy.erase(layer->id);
328}
329
330void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
331 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
332 auto it = hierarchy->mChildren.begin();
333 while (it != hierarchy->mChildren.end()) {
Vishnu Nair491827d2024-04-29 23:43:26 +0000334 if (LayerHierarchy::isMirror(it->second)) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000335 it = hierarchy->mChildren.erase(it);
336 } else {
337 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000338 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000339 }
340
Vishnu Naira9c43762023-01-27 19:10:25 +0000341 for (uint32_t mirrorId : layer->mirrorIds) {
342 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000343 }
Vishnu Nair491827d2024-04-29 23:43:26 +0000344 if (FlagManager::getInstance().detached_mirror()) {
345 if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) {
346 hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror),
347 LayerHierarchy::Variant::Detached_Mirror);
348 }
349 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000350}
351
Vishnu Naira0292282023-12-16 14:32:00 -0800352void LayerHierarchyBuilder::doUpdate(
Vishnu Nair04f89692022-11-16 23:21:05 +0000353 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
354 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
355 // rebuild map
356 for (auto& layer : layers) {
357 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
358 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
359 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
360 }
361 }
362
363 for (auto& layer : layers) {
364 if (layer->changes.get() == 0) {
365 continue;
366 }
367 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
368 onLayerAdded(layer.get());
369 continue;
370 }
371 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
372 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
373 detachFromParent(hierarchy);
374 attachToParent(hierarchy);
375 }
376 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
377 detachFromRelativeParent(hierarchy);
378 attachToRelativeParent(hierarchy);
379 }
380 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
381 hierarchy->mParent->sortChildrenByZOrder();
382 if (hierarchy->mRelativeParent) {
383 hierarchy->mRelativeParent->sortChildrenByZOrder();
384 }
385 }
386 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
387 updateMirrorLayer(layer.get());
388 }
389 }
390
391 for (auto& layer : destroyedLayers) {
392 onLayerDestroyed(layer.get());
393 }
394 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
395 // from our relative parents. This walks down both trees to do so. We can optimize this
396 // further by tracking onscreen, offscreen state in LayerHierarchy.
397 detachHierarchyFromRelativeParent(&mOffscreenRoot);
398 attachHierarchyToRelativeParent(&mRoot);
399}
400
Vishnu Naira0292282023-12-16 14:32:00 -0800401void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
402 if (!mInitialized) {
403 ATRACE_NAME("LayerHierarchyBuilder:init");
404 init(layerLifecycleManager.getLayers());
405 } else if (layerLifecycleManager.getGlobalChanges().test(
406 RequestedLayerState::Changes::Hierarchy)) {
407 ATRACE_NAME("LayerHierarchyBuilder:update");
408 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
409 } else {
410 return; // nothing to do
411 }
412
413 uint32_t invalidRelativeRoot;
414 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
415 while (hasRelZLoop) {
416 ATRACE_NAME("FixRelZLoop");
417 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
418 /*overwrite=*/false);
419 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
420 // reinitialize the hierarchy with the updated layer data
421 init(layerLifecycleManager.getLayers());
422 // check if we have any remaining loops
423 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
424 }
425}
426
Vishnu Nair04f89692022-11-16 23:21:05 +0000427const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
428 return mRoot;
429}
430
431const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
432 return mOffscreenRoot;
433}
434
435std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
436 if (depth > 10) return "too deep, loop?";
437 if (layerId == UNASSIGNED_LAYER_ID) return "";
438 auto it = mLayerIdToHierarchy.find(layerId);
439 if (it == mLayerIdToHierarchy.end()) return "not found";
440
441 LayerHierarchy* hierarchy = it->second;
442 if (!hierarchy->mLayer) return "none";
443
444 std::string debug =
445 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
446 if (hierarchy->mRelativeParent) {
447 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
448 }
449 if (hierarchy->mParent) {
450 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
451 }
452 return debug;
453}
454
455LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
456 bool childrenOnly) const {
457 auto it = mLayerIdToHierarchy.find(layerId);
458 if (it == mLayerIdToHierarchy.end()) return {nullptr};
459
460 LayerHierarchy hierarchy(*it->second, childrenOnly);
461 return hierarchy;
462}
463
464LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
465 auto it = mLayerIdToHierarchy.find(layerId);
466 if (it == mLayerIdToHierarchy.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700467 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
468 layerId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000469 return nullptr;
470 };
471
472 return it->second;
473}
474
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000475const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000476 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
477
478std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000479 if (id == UNASSIGNED_LAYER_ID) {
480 return "TraversalPath{ROOT}";
481 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000482 std::stringstream ss;
483 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000484
Vishnu Nair6f878312023-09-08 11:05:01 -0700485 if (!mirrorRootIds.empty()) {
486 ss << ", .mirrorRootIds=";
487 for (auto rootId : mirrorRootIds) {
488 ss << rootId << ",";
489 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000490 }
491
492 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000493 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000494 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000495 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000496 }
497 }
498
499 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000500 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000501 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000502 ss << "}";
503 return ss.str();
504}
Vishnu Nair04f89692022-11-16 23:21:05 +0000505
Vishnu Nair04f89692022-11-16 23:21:05 +0000506// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
507// of scope the TraversalPath is reset to its original state.
508LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
509 uint32_t layerId,
510 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000511 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000512 // Update the traversal id with the child layer id and variant. Parent id and variant are
513 // stored to reset the id upon destruction.
514 traversalPath.id = layerId;
515 traversalPath.variant = variant;
Vishnu Nair491827d2024-04-29 23:43:26 +0000516 if (LayerHierarchy::isMirror(variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700517 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000518 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000519 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
520 layerId) != traversalPath.relativeRootIds.end()) {
521 traversalPath.invalidRelativeRootId = layerId;
522 }
523 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000524 } else if (variant == LayerHierarchy::Variant::Detached) {
525 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000526 }
527}
528LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
529 // Reset the traversal id to its original parent state using the state that was saved in
530 // the constructor.
Vishnu Nair491827d2024-04-29 23:43:26 +0000531 if (LayerHierarchy::isMirror(mTraversalPath.variant)) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700532 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000533 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000534 mTraversalPath.relativeRootIds.pop_back();
535 }
536 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
537 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
538 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000539 mTraversalPath.id = mParentPath.id;
540 mTraversalPath.variant = mParentPath.variant;
541 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000542}
543
544} // namespace android::surfaceflinger::frontend