Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 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 |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 19 | #define LOG_TAG "SurfaceFlinger" |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 20 | |
| 21 | #include "LayerHierarchy.h" |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 22 | #include "LayerLog.h" |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 23 | #include "SwapErase.h" |
| 24 | |
| 25 | namespace android::surfaceflinger::frontend { |
| 26 | |
| 27 | namespace { |
| 28 | auto 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 Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 32 | if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) { |
Vishnu Nair | 444f395 | 2023-04-11 13:01:02 -0700 | [diff] [blame] | 33 | return lhsLayer->layerStack.id < rhsLayer->layerStack.id; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 34 | } |
| 35 | if (lhsLayer->z != rhsLayer->z) { |
| 36 | return lhsLayer->z < rhsLayer->z; |
| 37 | } |
| 38 | return lhsLayer->id < rhsLayer->id; |
| 39 | }; |
| 40 | |
| 41 | void 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 | |
| 48 | LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {} |
| 49 | |
| 50 | LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) { |
| 51 | mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer; |
| 52 | mChildren = hierarchy.mChildren; |
| 53 | } |
| 54 | |
| 55 | void 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 Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 63 | |
| 64 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d", |
| 65 | traversalPath.invalidRelativeRootId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 66 | for (auto& [child, childVariant] : mChildren) { |
| 67 | ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id, |
| 68 | childVariant); |
| 69 | child->traverse(visitor, traversalPath); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void 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 Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 79 | traverseThisLayer = false; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 80 | bool breakTraversal = !visitor(*this, traversalPath); |
| 81 | if (breakTraversal) { |
| 82 | return; |
| 83 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 84 | } |
| 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 | |
| 98 | void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) { |
| 99 | insertSorted(mChildren, {child, variant}); |
| 100 | } |
| 101 | |
| 102 | void 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 Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 107 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!"); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 108 | mChildren.erase(it); |
| 109 | } |
| 110 | |
| 111 | void LayerHierarchy::sortChildrenByZOrder() { |
| 112 | std::sort(mChildren.begin(), mChildren.end(), layerZCompare); |
| 113 | } |
| 114 | |
| 115 | void 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 Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 120 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!"); |
| 121 | it->second = variant; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | const RequestedLayerState* LayerHierarchy::getLayer() const { |
| 125 | return mLayer; |
| 126 | } |
| 127 | |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 128 | const LayerHierarchy* LayerHierarchy::getRelativeParent() const { |
| 129 | return mRelativeParent; |
| 130 | } |
| 131 | |
| 132 | const LayerHierarchy* LayerHierarchy::getParent() const { |
| 133 | return mParent; |
| 134 | } |
| 135 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 136 | std::string LayerHierarchy::getDebugStringShort() const { |
| 137 | std::string debug = "LayerHierarchy{"; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 138 | debug += ((mLayer) ? mLayer->getDebugString() : "root") + " "; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 139 | if (mChildren.empty()) { |
| 140 | debug += "no children"; |
| 141 | } else { |
| 142 | debug += std::to_string(mChildren.size()) + " children"; |
| 143 | } |
| 144 | return debug + "}"; |
| 145 | } |
| 146 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 147 | void LayerHierarchy::dump(std::ostream& out, const std::string& prefix, |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame^] | 148 | LayerHierarchy::Variant variant, bool isLastChild, |
| 149 | bool includeMirroredHierarchy) const { |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 150 | 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 Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame^] | 157 | if (!includeMirroredHierarchy) { |
| 158 | out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ..."; |
| 159 | return; |
| 160 | } |
| 161 | out << "(Mirroring) "; |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 162 | } |
| 163 | out << *mLayer; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 164 | } |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 165 | |
| 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 Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame^] | 175 | child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 176 | } |
| 177 | return; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bool 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 | |
| 193 | LayerHierarchyBuilder::LayerHierarchyBuilder( |
| 194 | const std::vector<std::unique_ptr<RequestedLayerState>>& layers) { |
| 195 | mHierarchies.reserve(layers.size()); |
| 196 | mLayerIdToHierarchy.reserve(layers.size()); |
| 197 | for (auto& layer : layers) { |
| 198 | mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get())); |
| 199 | mLayerIdToHierarchy[layer->id] = mHierarchies.back().get(); |
| 200 | } |
| 201 | for (const auto& layer : layers) { |
| 202 | onLayerAdded(layer.get()); |
| 203 | } |
| 204 | detachHierarchyFromRelativeParent(&mOffscreenRoot); |
| 205 | } |
| 206 | |
| 207 | void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) { |
| 208 | auto layer = hierarchy->mLayer; |
| 209 | LayerHierarchy::Variant type = layer->hasValidRelativeParent() |
| 210 | ? LayerHierarchy::Variant::Detached |
| 211 | : LayerHierarchy::Variant::Attached; |
| 212 | |
| 213 | LayerHierarchy* parent; |
| 214 | |
| 215 | if (layer->parentId != UNASSIGNED_LAYER_ID) { |
| 216 | parent = getHierarchyFromId(layer->parentId); |
| 217 | } else if (layer->canBeRoot) { |
| 218 | parent = &mRoot; |
| 219 | } else { |
| 220 | parent = &mOffscreenRoot; |
| 221 | } |
| 222 | parent->addChild(hierarchy, type); |
| 223 | hierarchy->mParent = parent; |
| 224 | } |
| 225 | |
| 226 | void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) { |
| 227 | hierarchy->mParent->removeChild(hierarchy); |
| 228 | hierarchy->mParent = nullptr; |
| 229 | } |
| 230 | |
| 231 | void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) { |
| 232 | auto layer = hierarchy->mLayer; |
| 233 | if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | if (layer->relativeParentId != UNASSIGNED_LAYER_ID) { |
| 238 | hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId); |
| 239 | } else { |
| 240 | hierarchy->mRelativeParent = &mOffscreenRoot; |
| 241 | } |
| 242 | hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative); |
| 243 | hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached); |
| 244 | } |
| 245 | |
| 246 | void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) { |
| 247 | if (hierarchy->mRelativeParent) { |
| 248 | hierarchy->mRelativeParent->removeChild(hierarchy); |
| 249 | } |
| 250 | hierarchy->mRelativeParent = nullptr; |
| 251 | hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached); |
| 252 | } |
| 253 | |
| 254 | void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) { |
| 255 | if (root->mLayer) { |
| 256 | attachToRelativeParent(root); |
| 257 | } |
| 258 | for (auto& [child, childVariant] : root->mChildren) { |
| 259 | if (childVariant == LayerHierarchy::Variant::Detached || |
| 260 | childVariant == LayerHierarchy::Variant::Attached) { |
| 261 | attachHierarchyToRelativeParent(child); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) { |
| 267 | if (root->mLayer) { |
| 268 | detachFromRelativeParent(root); |
| 269 | } |
| 270 | for (auto& [child, childVariant] : root->mChildren) { |
| 271 | if (childVariant == LayerHierarchy::Variant::Detached || |
| 272 | childVariant == LayerHierarchy::Variant::Attached) { |
| 273 | detachHierarchyFromRelativeParent(child); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) { |
| 279 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 280 | attachToParent(hierarchy); |
| 281 | attachToRelativeParent(hierarchy); |
| 282 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 283 | for (uint32_t mirrorId : layer->mirrorIds) { |
| 284 | LayerHierarchy* mirror = getHierarchyFromId(mirrorId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 285 | hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 290 | LLOGV(layer->id, ""); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 291 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false); |
| 292 | if (!hierarchy) { |
| 293 | // Layer was never part of the hierarchy if it was created and destroyed in the same |
| 294 | // transaction. |
| 295 | return; |
| 296 | } |
| 297 | // detach from parent |
| 298 | detachFromRelativeParent(hierarchy); |
| 299 | detachFromParent(hierarchy); |
| 300 | |
| 301 | // detach children |
| 302 | for (auto& [child, variant] : hierarchy->mChildren) { |
| 303 | if (variant == LayerHierarchy::Variant::Attached || |
| 304 | variant == LayerHierarchy::Variant::Detached) { |
| 305 | mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached); |
| 306 | child->mParent = &mOffscreenRoot; |
| 307 | } else if (variant == LayerHierarchy::Variant::Relative) { |
| 308 | mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached); |
| 309 | child->mRelativeParent = &mOffscreenRoot; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) { |
| 314 | return layerHierarchy.get() == hierarchy; |
| 315 | }); |
| 316 | mLayerIdToHierarchy.erase(layer->id); |
| 317 | } |
| 318 | |
| 319 | void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) { |
| 320 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 321 | auto it = hierarchy->mChildren.begin(); |
| 322 | while (it != hierarchy->mChildren.end()) { |
| 323 | if (it->second == LayerHierarchy::Variant::Mirror) { |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 324 | it = hierarchy->mChildren.erase(it); |
| 325 | } else { |
| 326 | it++; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 327 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 330 | for (uint32_t mirrorId : layer->mirrorIds) { |
| 331 | hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | void LayerHierarchyBuilder::update( |
| 336 | const std::vector<std::unique_ptr<RequestedLayerState>>& layers, |
| 337 | const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) { |
| 338 | // rebuild map |
| 339 | for (auto& layer : layers) { |
| 340 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 341 | mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get())); |
| 342 | mLayerIdToHierarchy[layer->id] = mHierarchies.back().get(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | for (auto& layer : layers) { |
| 347 | if (layer->changes.get() == 0) { |
| 348 | continue; |
| 349 | } |
| 350 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 351 | onLayerAdded(layer.get()); |
| 352 | continue; |
| 353 | } |
| 354 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 355 | if (layer->changes.test(RequestedLayerState::Changes::Parent)) { |
| 356 | detachFromParent(hierarchy); |
| 357 | attachToParent(hierarchy); |
| 358 | } |
| 359 | if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) { |
| 360 | detachFromRelativeParent(hierarchy); |
| 361 | attachToRelativeParent(hierarchy); |
| 362 | } |
| 363 | if (layer->changes.test(RequestedLayerState::Changes::Z)) { |
| 364 | hierarchy->mParent->sortChildrenByZOrder(); |
| 365 | if (hierarchy->mRelativeParent) { |
| 366 | hierarchy->mRelativeParent->sortChildrenByZOrder(); |
| 367 | } |
| 368 | } |
| 369 | if (layer->changes.test(RequestedLayerState::Changes::Mirror)) { |
| 370 | updateMirrorLayer(layer.get()); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | for (auto& layer : destroyedLayers) { |
| 375 | onLayerDestroyed(layer.get()); |
| 376 | } |
| 377 | // When moving from onscreen to offscreen and vice versa, we need to attach and detach |
| 378 | // from our relative parents. This walks down both trees to do so. We can optimize this |
| 379 | // further by tracking onscreen, offscreen state in LayerHierarchy. |
| 380 | detachHierarchyFromRelativeParent(&mOffscreenRoot); |
| 381 | attachHierarchyToRelativeParent(&mRoot); |
| 382 | } |
| 383 | |
| 384 | const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const { |
| 385 | return mRoot; |
| 386 | } |
| 387 | |
| 388 | const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const { |
| 389 | return mOffscreenRoot; |
| 390 | } |
| 391 | |
| 392 | std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const { |
| 393 | if (depth > 10) return "too deep, loop?"; |
| 394 | if (layerId == UNASSIGNED_LAYER_ID) return ""; |
| 395 | auto it = mLayerIdToHierarchy.find(layerId); |
| 396 | if (it == mLayerIdToHierarchy.end()) return "not found"; |
| 397 | |
| 398 | LayerHierarchy* hierarchy = it->second; |
| 399 | if (!hierarchy->mLayer) return "none"; |
| 400 | |
| 401 | std::string debug = |
| 402 | "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name; |
| 403 | if (hierarchy->mRelativeParent) { |
| 404 | debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort(); |
| 405 | } |
| 406 | if (hierarchy->mParent) { |
| 407 | debug += " Parent:" + hierarchy->mParent->getDebugStringShort(); |
| 408 | } |
| 409 | return debug; |
| 410 | } |
| 411 | |
| 412 | LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId, |
| 413 | bool childrenOnly) const { |
| 414 | auto it = mLayerIdToHierarchy.find(layerId); |
| 415 | if (it == mLayerIdToHierarchy.end()) return {nullptr}; |
| 416 | |
| 417 | LayerHierarchy hierarchy(*it->second, childrenOnly); |
| 418 | return hierarchy; |
| 419 | } |
| 420 | |
| 421 | LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) { |
| 422 | auto it = mLayerIdToHierarchy.find(layerId); |
| 423 | if (it == mLayerIdToHierarchy.end()) { |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 424 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d", |
| 425 | layerId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 426 | return nullptr; |
| 427 | }; |
| 428 | |
| 429 | return it->second; |
| 430 | } |
| 431 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 432 | const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT = |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 433 | {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached}; |
| 434 | |
| 435 | std::string LayerHierarchy::TraversalPath::toString() const { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 436 | if (id == UNASSIGNED_LAYER_ID) { |
| 437 | return "TraversalPath{ROOT}"; |
| 438 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 439 | std::stringstream ss; |
| 440 | ss << "TraversalPath{.id = " << id; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 441 | |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame^] | 442 | if (!mirrorRootIds.empty()) { |
| 443 | ss << ", .mirrorRootIds="; |
| 444 | for (auto rootId : mirrorRootIds) { |
| 445 | ss << rootId << ","; |
| 446 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (!relativeRootIds.empty()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 450 | ss << ", .relativeRootIds="; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 451 | for (auto rootId : relativeRootIds) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 452 | ss << rootId << ","; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| 456 | if (hasRelZLoop()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 457 | ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ","; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 458 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 459 | ss << "}"; |
| 460 | return ss.str(); |
| 461 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 462 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 463 | // Helper class to update a passed in TraversalPath when visiting a child. When the object goes out |
| 464 | // of scope the TraversalPath is reset to its original state. |
| 465 | LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath, |
| 466 | uint32_t layerId, |
| 467 | LayerHierarchy::Variant variant) |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 468 | : mTraversalPath(traversalPath), mParentPath(traversalPath) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 469 | // Update the traversal id with the child layer id and variant. Parent id and variant are |
| 470 | // stored to reset the id upon destruction. |
| 471 | traversalPath.id = layerId; |
| 472 | traversalPath.variant = variant; |
| 473 | if (variant == LayerHierarchy::Variant::Mirror) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame^] | 474 | traversalPath.mirrorRootIds.emplace_back(mParentPath.id); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 475 | } else if (variant == LayerHierarchy::Variant::Relative) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 476 | if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(), |
| 477 | layerId) != traversalPath.relativeRootIds.end()) { |
| 478 | traversalPath.invalidRelativeRootId = layerId; |
| 479 | } |
| 480 | traversalPath.relativeRootIds.emplace_back(layerId); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 481 | } else if (variant == LayerHierarchy::Variant::Detached) { |
| 482 | traversalPath.detached = true; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() { |
| 486 | // Reset the traversal id to its original parent state using the state that was saved in |
| 487 | // the constructor. |
| 488 | if (mTraversalPath.variant == LayerHierarchy::Variant::Mirror) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame^] | 489 | mTraversalPath.mirrorRootIds.pop_back(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 490 | } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 491 | mTraversalPath.relativeRootIds.pop_back(); |
| 492 | } |
| 493 | if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) { |
| 494 | mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID; |
| 495 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 496 | mTraversalPath.id = mParentPath.id; |
| 497 | mTraversalPath.variant = mParentPath.variant; |
| 498 | mTraversalPath.detached = mParentPath.detached; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | } // namespace android::surfaceflinger::frontend |