blob: 821ac0cf88c20d5ddf411a7ff98b39bc4a1d5e1c [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) ";
156 } else if (variant == LayerHierarchy::Variant::Mirror) {
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 }
292}
293
294void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000295 LLOGV(layer->id, "");
Vishnu Nair04f89692022-11-16 23:21:05 +0000296 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false);
297 if (!hierarchy) {
298 // Layer was never part of the hierarchy if it was created and destroyed in the same
299 // transaction.
300 return;
301 }
302 // detach from parent
303 detachFromRelativeParent(hierarchy);
304 detachFromParent(hierarchy);
305
306 // detach children
307 for (auto& [child, variant] : hierarchy->mChildren) {
308 if (variant == LayerHierarchy::Variant::Attached ||
309 variant == LayerHierarchy::Variant::Detached) {
310 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
311 child->mParent = &mOffscreenRoot;
312 } else if (variant == LayerHierarchy::Variant::Relative) {
313 mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached);
314 child->mRelativeParent = &mOffscreenRoot;
315 }
316 }
317
318 swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) {
319 return layerHierarchy.get() == hierarchy;
320 });
321 mLayerIdToHierarchy.erase(layer->id);
322}
323
324void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) {
325 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
326 auto it = hierarchy->mChildren.begin();
327 while (it != hierarchy->mChildren.end()) {
328 if (it->second == LayerHierarchy::Variant::Mirror) {
Vishnu Naira9c43762023-01-27 19:10:25 +0000329 it = hierarchy->mChildren.erase(it);
330 } else {
331 it++;
Vishnu Nair04f89692022-11-16 23:21:05 +0000332 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000333 }
334
Vishnu Naira9c43762023-01-27 19:10:25 +0000335 for (uint32_t mirrorId : layer->mirrorIds) {
336 hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror);
Vishnu Nair04f89692022-11-16 23:21:05 +0000337 }
338}
339
Vishnu Naira0292282023-12-16 14:32:00 -0800340void LayerHierarchyBuilder::doUpdate(
Vishnu Nair04f89692022-11-16 23:21:05 +0000341 const std::vector<std::unique_ptr<RequestedLayerState>>& layers,
342 const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) {
343 // rebuild map
344 for (auto& layer : layers) {
345 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
346 mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get()));
347 mLayerIdToHierarchy[layer->id] = mHierarchies.back().get();
348 }
349 }
350
351 for (auto& layer : layers) {
352 if (layer->changes.get() == 0) {
353 continue;
354 }
355 if (layer->changes.test(RequestedLayerState::Changes::Created)) {
356 onLayerAdded(layer.get());
357 continue;
358 }
359 LayerHierarchy* hierarchy = getHierarchyFromId(layer->id);
360 if (layer->changes.test(RequestedLayerState::Changes::Parent)) {
361 detachFromParent(hierarchy);
362 attachToParent(hierarchy);
363 }
364 if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) {
365 detachFromRelativeParent(hierarchy);
366 attachToRelativeParent(hierarchy);
367 }
368 if (layer->changes.test(RequestedLayerState::Changes::Z)) {
369 hierarchy->mParent->sortChildrenByZOrder();
370 if (hierarchy->mRelativeParent) {
371 hierarchy->mRelativeParent->sortChildrenByZOrder();
372 }
373 }
374 if (layer->changes.test(RequestedLayerState::Changes::Mirror)) {
375 updateMirrorLayer(layer.get());
376 }
377 }
378
379 for (auto& layer : destroyedLayers) {
380 onLayerDestroyed(layer.get());
381 }
382 // When moving from onscreen to offscreen and vice versa, we need to attach and detach
383 // from our relative parents. This walks down both trees to do so. We can optimize this
384 // further by tracking onscreen, offscreen state in LayerHierarchy.
385 detachHierarchyFromRelativeParent(&mOffscreenRoot);
386 attachHierarchyToRelativeParent(&mRoot);
387}
388
Vishnu Naira0292282023-12-16 14:32:00 -0800389void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) {
390 if (!mInitialized) {
391 ATRACE_NAME("LayerHierarchyBuilder:init");
392 init(layerLifecycleManager.getLayers());
393 } else if (layerLifecycleManager.getGlobalChanges().test(
394 RequestedLayerState::Changes::Hierarchy)) {
395 ATRACE_NAME("LayerHierarchyBuilder:update");
396 doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers());
397 } else {
398 return; // nothing to do
399 }
400
401 uint32_t invalidRelativeRoot;
402 bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
403 while (hasRelZLoop) {
404 ATRACE_NAME("FixRelZLoop");
405 TransactionTraceWriter::getInstance().invoke("relz_loop_detected",
406 /*overwrite=*/false);
407 layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot);
408 // reinitialize the hierarchy with the updated layer data
409 init(layerLifecycleManager.getLayers());
410 // check if we have any remaining loops
411 hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot);
412 }
413}
414
Vishnu Nair04f89692022-11-16 23:21:05 +0000415const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const {
416 return mRoot;
417}
418
419const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const {
420 return mOffscreenRoot;
421}
422
423std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const {
424 if (depth > 10) return "too deep, loop?";
425 if (layerId == UNASSIGNED_LAYER_ID) return "";
426 auto it = mLayerIdToHierarchy.find(layerId);
427 if (it == mLayerIdToHierarchy.end()) return "not found";
428
429 LayerHierarchy* hierarchy = it->second;
430 if (!hierarchy->mLayer) return "none";
431
432 std::string debug =
433 "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name;
434 if (hierarchy->mRelativeParent) {
435 debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort();
436 }
437 if (hierarchy->mParent) {
438 debug += " Parent:" + hierarchy->mParent->getDebugStringShort();
439 }
440 return debug;
441}
442
443LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId,
444 bool childrenOnly) const {
445 auto it = mLayerIdToHierarchy.find(layerId);
446 if (it == mLayerIdToHierarchy.end()) return {nullptr};
447
448 LayerHierarchy hierarchy(*it->second, childrenOnly);
449 return hierarchy;
450}
451
452LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) {
453 auto it = mLayerIdToHierarchy.find(layerId);
454 if (it == mLayerIdToHierarchy.end()) {
Vishnu Nair606d9d02023-08-19 14:20:18 -0700455 LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d",
456 layerId);
Vishnu Nair04f89692022-11-16 23:21:05 +0000457 return nullptr;
458 };
459
460 return it->second;
461}
462
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000463const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT =
Vishnu Nair04f89692022-11-16 23:21:05 +0000464 {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached};
465
466std::string LayerHierarchy::TraversalPath::toString() const {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000467 if (id == UNASSIGNED_LAYER_ID) {
468 return "TraversalPath{ROOT}";
469 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000470 std::stringstream ss;
471 ss << "TraversalPath{.id = " << id;
Vishnu Nair04f89692022-11-16 23:21:05 +0000472
Vishnu Nair6f878312023-09-08 11:05:01 -0700473 if (!mirrorRootIds.empty()) {
474 ss << ", .mirrorRootIds=";
475 for (auto rootId : mirrorRootIds) {
476 ss << rootId << ",";
477 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000478 }
479
480 if (!relativeRootIds.empty()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000481 ss << ", .relativeRootIds=";
Vishnu Nair04f89692022-11-16 23:21:05 +0000482 for (auto rootId : relativeRootIds) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000483 ss << rootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000484 }
485 }
486
487 if (hasRelZLoop()) {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000488 ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ",";
Vishnu Nair04f89692022-11-16 23:21:05 +0000489 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000490 ss << "}";
491 return ss.str();
492}
Vishnu Nair04f89692022-11-16 23:21:05 +0000493
Vishnu Nair04f89692022-11-16 23:21:05 +0000494// Helper class to update a passed in TraversalPath when visiting a child. When the object goes out
495// of scope the TraversalPath is reset to its original state.
496LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath,
497 uint32_t layerId,
498 LayerHierarchy::Variant variant)
Vishnu Nair80a5a702023-02-11 01:21:51 +0000499 : mTraversalPath(traversalPath), mParentPath(traversalPath) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000500 // Update the traversal id with the child layer id and variant. Parent id and variant are
501 // stored to reset the id upon destruction.
502 traversalPath.id = layerId;
503 traversalPath.variant = variant;
504 if (variant == LayerHierarchy::Variant::Mirror) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700505 traversalPath.mirrorRootIds.emplace_back(mParentPath.id);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000506 } else if (variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000507 if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(),
508 layerId) != traversalPath.relativeRootIds.end()) {
509 traversalPath.invalidRelativeRootId = layerId;
510 }
511 traversalPath.relativeRootIds.emplace_back(layerId);
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000512 } else if (variant == LayerHierarchy::Variant::Detached) {
513 traversalPath.detached = true;
Vishnu Nair04f89692022-11-16 23:21:05 +0000514 }
515}
516LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() {
517 // Reset the traversal id to its original parent state using the state that was saved in
518 // the constructor.
519 if (mTraversalPath.variant == LayerHierarchy::Variant::Mirror) {
Vishnu Nair6f878312023-09-08 11:05:01 -0700520 mTraversalPath.mirrorRootIds.pop_back();
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000521 } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) {
Vishnu Nair04f89692022-11-16 23:21:05 +0000522 mTraversalPath.relativeRootIds.pop_back();
523 }
524 if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) {
525 mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID;
526 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000527 mTraversalPath.id = mParentPath.id;
528 mTraversalPath.variant = mParentPath.variant;
529 mTraversalPath.detached = mParentPath.detached;
Vishnu Nair04f89692022-11-16 23:21:05 +0000530}
531
532} // namespace android::surfaceflinger::frontend