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 | |
Melody Hsu | bb2ec6a | 2024-05-27 22:18:05 +0000 | [diff] [blame] | 55 | void 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 Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 61 | if (mLayer) { |
| 62 | bool breakTraversal = !visitor(*this, traversalPath); |
| 63 | if (breakTraversal) { |
| 64 | return; |
| 65 | } |
| 66 | } |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 67 | |
| 68 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d", |
| 69 | traversalPath.invalidRelativeRootId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 70 | for (auto& [child, childVariant] : mChildren) { |
| 71 | ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id, |
| 72 | childVariant); |
Melody Hsu | bb2ec6a | 2024-05-27 22:18:05 +0000 | [diff] [blame] | 73 | child->traverse(visitor, traversalPath, depth + 1); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | void 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 Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 83 | traverseThisLayer = false; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 84 | bool breakTraversal = !visitor(*this, traversalPath); |
| 85 | if (breakTraversal) { |
| 86 | return; |
| 87 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 88 | } |
| 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 | |
| 102 | void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) { |
| 103 | insertSorted(mChildren, {child, variant}); |
| 104 | } |
| 105 | |
| 106 | void LayerHierarchy::removeChild(LayerHierarchy* child) { |
| 107 | auto it = std::find_if(mChildren.begin(), mChildren.end(), |
| 108 | [child](const std::pair<LayerHierarchy*, Variant>& x) { |
| 109 | return x.first == child; |
| 110 | }); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 111 | 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] | 112 | mChildren.erase(it); |
| 113 | } |
| 114 | |
| 115 | void LayerHierarchy::sortChildrenByZOrder() { |
| 116 | std::sort(mChildren.begin(), mChildren.end(), layerZCompare); |
| 117 | } |
| 118 | |
| 119 | void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) { |
| 120 | auto it = std::find_if(mChildren.begin(), mChildren.end(), |
| 121 | [hierarchy](std::pair<LayerHierarchy*, Variant>& child) { |
| 122 | return child.first == hierarchy; |
| 123 | }); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 124 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!"); |
| 125 | it->second = variant; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | const RequestedLayerState* LayerHierarchy::getLayer() const { |
| 129 | return mLayer; |
| 130 | } |
| 131 | |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 132 | const LayerHierarchy* LayerHierarchy::getRelativeParent() const { |
| 133 | return mRelativeParent; |
| 134 | } |
| 135 | |
| 136 | const LayerHierarchy* LayerHierarchy::getParent() const { |
| 137 | return mParent; |
| 138 | } |
| 139 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 140 | std::string LayerHierarchy::getDebugStringShort() const { |
| 141 | std::string debug = "LayerHierarchy{"; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 142 | debug += ((mLayer) ? mLayer->getDebugString() : "root") + " "; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 143 | if (mChildren.empty()) { |
| 144 | debug += "no children"; |
| 145 | } else { |
| 146 | debug += std::to_string(mChildren.size()) + " children"; |
| 147 | } |
| 148 | return debug + "}"; |
| 149 | } |
| 150 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 151 | void LayerHierarchy::dump(std::ostream& out, const std::string& prefix, |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 152 | LayerHierarchy::Variant variant, bool isLastChild, |
| 153 | bool includeMirroredHierarchy) const { |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 154 | if (!mLayer) { |
| 155 | out << " ROOT"; |
| 156 | } else { |
| 157 | out << prefix + (isLastChild ? "└─ " : "├─ "); |
| 158 | if (variant == LayerHierarchy::Variant::Relative) { |
| 159 | out << "(Relative) "; |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 160 | } else if (LayerHierarchy::isMirror(variant)) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 161 | if (!includeMirroredHierarchy) { |
| 162 | out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ..."; |
| 163 | return; |
| 164 | } |
| 165 | out << "(Mirroring) "; |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 166 | } |
| 167 | out << *mLayer; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 168 | } |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 169 | |
| 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 Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 179 | child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 180 | } |
| 181 | return; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool 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 Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 197 | void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) { |
| 198 | mLayerIdToHierarchy.clear(); |
| 199 | mHierarchies.clear(); |
| 200 | mRoot = nullptr; |
| 201 | mOffscreenRoot = nullptr; |
| 202 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 203 | 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 Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 213 | mInitialized = true; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void 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 | |
| 235 | void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) { |
| 236 | hierarchy->mParent->removeChild(hierarchy); |
| 237 | hierarchy->mParent = nullptr; |
| 238 | } |
| 239 | |
| 240 | void 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 | |
| 255 | void 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 | |
Vishnu Nair | f12a678 | 2024-06-03 23:08:15 +0000 | [diff] [blame] | 263 | std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) { |
| 264 | std::vector<LayerHierarchy*> hierarchies; |
| 265 | hierarchies.push_back(root); |
| 266 | std::vector<LayerHierarchy*> descendants; |
| 267 | for (size_t i = 0; i < hierarchies.size(); i++) { |
| 268 | LayerHierarchy* hierarchy = hierarchies[i]; |
| 269 | if (hierarchy->mLayer) { |
| 270 | descendants.push_back(hierarchy); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 271 | } |
Vishnu Nair | f12a678 | 2024-06-03 23:08:15 +0000 | [diff] [blame] | 272 | for (auto& [child, childVariant] : hierarchy->mChildren) { |
| 273 | if (childVariant == LayerHierarchy::Variant::Detached || |
| 274 | childVariant == LayerHierarchy::Variant::Attached) { |
| 275 | hierarchies.push_back(child); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return descendants; |
| 280 | } |
| 281 | |
| 282 | void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) { |
| 283 | std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root); |
| 284 | for (LayerHierarchy* hierarchy : hierarchiesToAttach) { |
| 285 | attachToRelativeParent(hierarchy); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) { |
Vishnu Nair | f12a678 | 2024-06-03 23:08:15 +0000 | [diff] [blame] | 290 | std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root); |
| 291 | for (LayerHierarchy* hierarchy : hierarchiesToDetach) { |
| 292 | detachFromRelativeParent(hierarchy); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) { |
| 297 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 298 | attachToParent(hierarchy); |
| 299 | attachToRelativeParent(hierarchy); |
| 300 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 301 | for (uint32_t mirrorId : layer->mirrorIds) { |
| 302 | LayerHierarchy* mirror = getHierarchyFromId(mirrorId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 303 | hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror); |
| 304 | } |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 305 | if (FlagManager::getInstance().detached_mirror()) { |
| 306 | if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) { |
| 307 | LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror); |
| 308 | hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror); |
| 309 | } |
| 310 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 314 | LLOGV(layer->id, ""); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 315 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false); |
| 316 | if (!hierarchy) { |
| 317 | // Layer was never part of the hierarchy if it was created and destroyed in the same |
| 318 | // transaction. |
| 319 | return; |
| 320 | } |
| 321 | // detach from parent |
| 322 | detachFromRelativeParent(hierarchy); |
| 323 | detachFromParent(hierarchy); |
| 324 | |
| 325 | // detach children |
| 326 | for (auto& [child, variant] : hierarchy->mChildren) { |
| 327 | if (variant == LayerHierarchy::Variant::Attached || |
| 328 | variant == LayerHierarchy::Variant::Detached) { |
| 329 | mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached); |
| 330 | child->mParent = &mOffscreenRoot; |
| 331 | } else if (variant == LayerHierarchy::Variant::Relative) { |
| 332 | mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached); |
| 333 | child->mRelativeParent = &mOffscreenRoot; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) { |
| 338 | return layerHierarchy.get() == hierarchy; |
| 339 | }); |
| 340 | mLayerIdToHierarchy.erase(layer->id); |
| 341 | } |
| 342 | |
| 343 | void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) { |
| 344 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 345 | auto it = hierarchy->mChildren.begin(); |
| 346 | while (it != hierarchy->mChildren.end()) { |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 347 | if (LayerHierarchy::isMirror(it->second)) { |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 348 | it = hierarchy->mChildren.erase(it); |
| 349 | } else { |
| 350 | it++; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 351 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 354 | for (uint32_t mirrorId : layer->mirrorIds) { |
| 355 | hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 356 | } |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 357 | if (FlagManager::getInstance().detached_mirror()) { |
| 358 | if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) { |
| 359 | hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror), |
| 360 | LayerHierarchy::Variant::Detached_Mirror); |
| 361 | } |
| 362 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 365 | void LayerHierarchyBuilder::doUpdate( |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 366 | const std::vector<std::unique_ptr<RequestedLayerState>>& layers, |
| 367 | const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) { |
| 368 | // rebuild map |
| 369 | for (auto& layer : layers) { |
| 370 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 371 | mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get())); |
| 372 | mLayerIdToHierarchy[layer->id] = mHierarchies.back().get(); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | for (auto& layer : layers) { |
| 377 | if (layer->changes.get() == 0) { |
| 378 | continue; |
| 379 | } |
| 380 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 381 | onLayerAdded(layer.get()); |
| 382 | continue; |
| 383 | } |
| 384 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 385 | if (layer->changes.test(RequestedLayerState::Changes::Parent)) { |
| 386 | detachFromParent(hierarchy); |
| 387 | attachToParent(hierarchy); |
| 388 | } |
| 389 | if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) { |
| 390 | detachFromRelativeParent(hierarchy); |
| 391 | attachToRelativeParent(hierarchy); |
| 392 | } |
| 393 | if (layer->changes.test(RequestedLayerState::Changes::Z)) { |
| 394 | hierarchy->mParent->sortChildrenByZOrder(); |
| 395 | if (hierarchy->mRelativeParent) { |
| 396 | hierarchy->mRelativeParent->sortChildrenByZOrder(); |
| 397 | } |
| 398 | } |
| 399 | if (layer->changes.test(RequestedLayerState::Changes::Mirror)) { |
| 400 | updateMirrorLayer(layer.get()); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | for (auto& layer : destroyedLayers) { |
| 405 | onLayerDestroyed(layer.get()); |
| 406 | } |
| 407 | // When moving from onscreen to offscreen and vice versa, we need to attach and detach |
| 408 | // from our relative parents. This walks down both trees to do so. We can optimize this |
| 409 | // further by tracking onscreen, offscreen state in LayerHierarchy. |
| 410 | detachHierarchyFromRelativeParent(&mOffscreenRoot); |
| 411 | attachHierarchyToRelativeParent(&mRoot); |
| 412 | } |
| 413 | |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 414 | void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) { |
| 415 | if (!mInitialized) { |
| 416 | ATRACE_NAME("LayerHierarchyBuilder:init"); |
| 417 | init(layerLifecycleManager.getLayers()); |
| 418 | } else if (layerLifecycleManager.getGlobalChanges().test( |
| 419 | RequestedLayerState::Changes::Hierarchy)) { |
| 420 | ATRACE_NAME("LayerHierarchyBuilder:update"); |
| 421 | doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers()); |
| 422 | } else { |
| 423 | return; // nothing to do |
| 424 | } |
| 425 | |
| 426 | uint32_t invalidRelativeRoot; |
| 427 | bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot); |
| 428 | while (hasRelZLoop) { |
| 429 | ATRACE_NAME("FixRelZLoop"); |
| 430 | TransactionTraceWriter::getInstance().invoke("relz_loop_detected", |
| 431 | /*overwrite=*/false); |
| 432 | layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot); |
| 433 | // reinitialize the hierarchy with the updated layer data |
| 434 | init(layerLifecycleManager.getLayers()); |
| 435 | // check if we have any remaining loops |
| 436 | hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot); |
| 437 | } |
| 438 | } |
| 439 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 440 | const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const { |
| 441 | return mRoot; |
| 442 | } |
| 443 | |
| 444 | const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const { |
| 445 | return mOffscreenRoot; |
| 446 | } |
| 447 | |
| 448 | std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const { |
| 449 | if (depth > 10) return "too deep, loop?"; |
| 450 | if (layerId == UNASSIGNED_LAYER_ID) return ""; |
| 451 | auto it = mLayerIdToHierarchy.find(layerId); |
| 452 | if (it == mLayerIdToHierarchy.end()) return "not found"; |
| 453 | |
| 454 | LayerHierarchy* hierarchy = it->second; |
| 455 | if (!hierarchy->mLayer) return "none"; |
| 456 | |
| 457 | std::string debug = |
| 458 | "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name; |
| 459 | if (hierarchy->mRelativeParent) { |
| 460 | debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort(); |
| 461 | } |
| 462 | if (hierarchy->mParent) { |
| 463 | debug += " Parent:" + hierarchy->mParent->getDebugStringShort(); |
| 464 | } |
| 465 | return debug; |
| 466 | } |
| 467 | |
| 468 | LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId, |
| 469 | bool childrenOnly) const { |
| 470 | auto it = mLayerIdToHierarchy.find(layerId); |
| 471 | if (it == mLayerIdToHierarchy.end()) return {nullptr}; |
| 472 | |
| 473 | LayerHierarchy hierarchy(*it->second, childrenOnly); |
| 474 | return hierarchy; |
| 475 | } |
| 476 | |
| 477 | LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) { |
| 478 | auto it = mLayerIdToHierarchy.find(layerId); |
| 479 | if (it == mLayerIdToHierarchy.end()) { |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 480 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d", |
| 481 | layerId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 482 | return nullptr; |
| 483 | }; |
| 484 | |
| 485 | return it->second; |
| 486 | } |
| 487 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 488 | const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT = |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 489 | {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached}; |
| 490 | |
| 491 | std::string LayerHierarchy::TraversalPath::toString() const { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 492 | if (id == UNASSIGNED_LAYER_ID) { |
| 493 | return "TraversalPath{ROOT}"; |
| 494 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 495 | std::stringstream ss; |
| 496 | ss << "TraversalPath{.id = " << id; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 497 | |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 498 | if (!mirrorRootIds.empty()) { |
| 499 | ss << ", .mirrorRootIds="; |
| 500 | for (auto rootId : mirrorRootIds) { |
| 501 | ss << rootId << ","; |
| 502 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (!relativeRootIds.empty()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 506 | ss << ", .relativeRootIds="; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 507 | for (auto rootId : relativeRootIds) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 508 | ss << rootId << ","; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | if (hasRelZLoop()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 513 | ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ","; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 514 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 515 | ss << "}"; |
| 516 | return ss.str(); |
| 517 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 518 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 519 | // Helper class to update a passed in TraversalPath when visiting a child. When the object goes out |
| 520 | // of scope the TraversalPath is reset to its original state. |
| 521 | LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath, |
| 522 | uint32_t layerId, |
| 523 | LayerHierarchy::Variant variant) |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 524 | : mTraversalPath(traversalPath), mParentPath(traversalPath) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 525 | // Update the traversal id with the child layer id and variant. Parent id and variant are |
| 526 | // stored to reset the id upon destruction. |
| 527 | traversalPath.id = layerId; |
| 528 | traversalPath.variant = variant; |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 529 | if (LayerHierarchy::isMirror(variant)) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 530 | traversalPath.mirrorRootIds.emplace_back(mParentPath.id); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 531 | } else if (variant == LayerHierarchy::Variant::Relative) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 532 | if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(), |
| 533 | layerId) != traversalPath.relativeRootIds.end()) { |
| 534 | traversalPath.invalidRelativeRootId = layerId; |
| 535 | } |
| 536 | traversalPath.relativeRootIds.emplace_back(layerId); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 537 | } else if (variant == LayerHierarchy::Variant::Detached) { |
| 538 | traversalPath.detached = true; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() { |
| 542 | // Reset the traversal id to its original parent state using the state that was saved in |
| 543 | // the constructor. |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 544 | if (LayerHierarchy::isMirror(mTraversalPath.variant)) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 545 | mTraversalPath.mirrorRootIds.pop_back(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 546 | } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 547 | mTraversalPath.relativeRootIds.pop_back(); |
| 548 | } |
| 549 | if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) { |
| 550 | mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID; |
| 551 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 552 | mTraversalPath.id = mParentPath.id; |
| 553 | mTraversalPath.variant = mParentPath.variant; |
| 554 | mTraversalPath.detached = mParentPath.detached; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | } // namespace android::surfaceflinger::frontend |