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 | |
qinyige1 | 0e011d0 | 2024-05-23 15:59:08 +0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 23 | #include "LayerHierarchy.h" |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 24 | #include "LayerLog.h" |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 25 | #include "SwapErase.h" |
| 26 | |
| 27 | namespace android::surfaceflinger::frontend { |
| 28 | |
| 29 | namespace { |
| 30 | auto layerZCompare = [](const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& lhs, |
| 31 | const std::pair<LayerHierarchy*, LayerHierarchy::Variant>& rhs) { |
| 32 | auto lhsLayer = lhs.first->getLayer(); |
| 33 | auto rhsLayer = rhs.first->getLayer(); |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 34 | if (lhsLayer->layerStack.id != rhsLayer->layerStack.id) { |
Vishnu Nair | 444f395 | 2023-04-11 13:01:02 -0700 | [diff] [blame] | 35 | return lhsLayer->layerStack.id < rhsLayer->layerStack.id; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 36 | } |
| 37 | if (lhsLayer->z != rhsLayer->z) { |
| 38 | return lhsLayer->z < rhsLayer->z; |
| 39 | } |
| 40 | return lhsLayer->id < rhsLayer->id; |
| 41 | }; |
| 42 | |
| 43 | void insertSorted(std::vector<std::pair<LayerHierarchy*, LayerHierarchy::Variant>>& vec, |
| 44 | std::pair<LayerHierarchy*, LayerHierarchy::Variant> value) { |
| 45 | auto it = std::upper_bound(vec.begin(), vec.end(), value, layerZCompare); |
| 46 | vec.insert(it, std::move(value)); |
| 47 | } |
| 48 | } // namespace |
| 49 | |
| 50 | LayerHierarchy::LayerHierarchy(RequestedLayerState* layer) : mLayer(layer) {} |
| 51 | |
| 52 | LayerHierarchy::LayerHierarchy(const LayerHierarchy& hierarchy, bool childrenOnly) { |
| 53 | mLayer = (childrenOnly) ? nullptr : hierarchy.mLayer; |
| 54 | mChildren = hierarchy.mChildren; |
| 55 | } |
| 56 | |
Melody Hsu | bb2ec6a | 2024-05-27 22:18:05 +0000 | [diff] [blame] | 57 | void LayerHierarchy::traverse(const Visitor& visitor, LayerHierarchy::TraversalPath& traversalPath, |
| 58 | uint32_t depth) const { |
| 59 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(depth > 50, |
| 60 | "Cycle detected in LayerHierarchy::traverse. See " |
| 61 | "traverse_stack_overflow_transactions.winscope"); |
| 62 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 63 | if (mLayer) { |
| 64 | bool breakTraversal = !visitor(*this, traversalPath); |
| 65 | if (breakTraversal) { |
| 66 | return; |
| 67 | } |
| 68 | } |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 69 | |
| 70 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(traversalPath.hasRelZLoop(), "Found relative z loop layerId:%d", |
| 71 | traversalPath.invalidRelativeRootId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 72 | for (auto& [child, childVariant] : mChildren) { |
| 73 | ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id, |
| 74 | childVariant); |
Melody Hsu | bb2ec6a | 2024-05-27 22:18:05 +0000 | [diff] [blame] | 75 | child->traverse(visitor, traversalPath, depth + 1); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | void LayerHierarchy::traverseInZOrder(const Visitor& visitor, |
| 80 | LayerHierarchy::TraversalPath& traversalPath) const { |
| 81 | bool traverseThisLayer = (mLayer != nullptr); |
| 82 | for (auto it = mChildren.begin(); it < mChildren.end(); it++) { |
| 83 | auto& [child, childVariant] = *it; |
| 84 | if (traverseThisLayer && child->getLayer()->z >= 0) { |
Vishnu Nair | cfb2d25 | 2023-01-19 04:44:02 +0000 | [diff] [blame] | 85 | traverseThisLayer = false; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 86 | bool breakTraversal = !visitor(*this, traversalPath); |
| 87 | if (breakTraversal) { |
| 88 | return; |
| 89 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 90 | } |
| 91 | if (childVariant == LayerHierarchy::Variant::Detached) { |
| 92 | continue; |
| 93 | } |
| 94 | ScopedAddToTraversalPath addChildToTraversalPath(traversalPath, child->mLayer->id, |
| 95 | childVariant); |
| 96 | child->traverseInZOrder(visitor, traversalPath); |
| 97 | } |
| 98 | |
| 99 | if (traverseThisLayer) { |
| 100 | visitor(*this, traversalPath); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void LayerHierarchy::addChild(LayerHierarchy* child, LayerHierarchy::Variant variant) { |
| 105 | insertSorted(mChildren, {child, variant}); |
| 106 | } |
| 107 | |
| 108 | void LayerHierarchy::removeChild(LayerHierarchy* child) { |
| 109 | auto it = std::find_if(mChildren.begin(), mChildren.end(), |
| 110 | [child](const std::pair<LayerHierarchy*, Variant>& x) { |
| 111 | return x.first == child; |
| 112 | }); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 113 | 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] | 114 | mChildren.erase(it); |
| 115 | } |
| 116 | |
| 117 | void LayerHierarchy::sortChildrenByZOrder() { |
| 118 | std::sort(mChildren.begin(), mChildren.end(), layerZCompare); |
| 119 | } |
| 120 | |
| 121 | void LayerHierarchy::updateChild(LayerHierarchy* hierarchy, LayerHierarchy::Variant variant) { |
| 122 | auto it = std::find_if(mChildren.begin(), mChildren.end(), |
| 123 | [hierarchy](std::pair<LayerHierarchy*, Variant>& child) { |
| 124 | return child.first == hierarchy; |
| 125 | }); |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 126 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(it == mChildren.end(), "Could not find child!"); |
| 127 | it->second = variant; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | const RequestedLayerState* LayerHierarchy::getLayer() const { |
| 131 | return mLayer; |
| 132 | } |
| 133 | |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 134 | const LayerHierarchy* LayerHierarchy::getRelativeParent() const { |
| 135 | return mRelativeParent; |
| 136 | } |
| 137 | |
| 138 | const LayerHierarchy* LayerHierarchy::getParent() const { |
| 139 | return mParent; |
| 140 | } |
| 141 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 142 | std::string LayerHierarchy::getDebugStringShort() const { |
| 143 | std::string debug = "LayerHierarchy{"; |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 144 | debug += ((mLayer) ? mLayer->getDebugString() : "root") + " "; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 145 | if (mChildren.empty()) { |
| 146 | debug += "no children"; |
| 147 | } else { |
| 148 | debug += std::to_string(mChildren.size()) + " children"; |
| 149 | } |
| 150 | return debug + "}"; |
| 151 | } |
| 152 | |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 153 | void LayerHierarchy::dump(std::ostream& out, const std::string& prefix, |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 154 | LayerHierarchy::Variant variant, bool isLastChild, |
| 155 | bool includeMirroredHierarchy) const { |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 156 | if (!mLayer) { |
| 157 | out << " ROOT"; |
| 158 | } else { |
| 159 | out << prefix + (isLastChild ? "└─ " : "├─ "); |
| 160 | if (variant == LayerHierarchy::Variant::Relative) { |
| 161 | out << "(Relative) "; |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 162 | } else if (LayerHierarchy::isMirror(variant)) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 163 | if (!includeMirroredHierarchy) { |
| 164 | out << "(Mirroring) " << *mLayer << "\n" + prefix + " └─ ..."; |
| 165 | return; |
| 166 | } |
| 167 | out << "(Mirroring) "; |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 168 | } |
| 169 | out << *mLayer; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 170 | } |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 171 | |
| 172 | for (size_t i = 0; i < mChildren.size(); i++) { |
| 173 | auto& [child, childVariant] = mChildren[i]; |
| 174 | if (childVariant == LayerHierarchy::Variant::Detached) continue; |
| 175 | const bool lastChild = i == (mChildren.size() - 1); |
| 176 | std::string childPrefix = prefix; |
| 177 | if (mLayer) { |
| 178 | childPrefix += (isLastChild ? " " : "│ "); |
| 179 | } |
| 180 | out << "\n"; |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 181 | child->dump(out, childPrefix, childVariant, lastChild, includeMirroredHierarchy); |
Vishnu Nair | 3cc15a4 | 2023-06-30 06:20:22 +0000 | [diff] [blame] | 182 | } |
| 183 | return; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool LayerHierarchy::hasRelZLoop(uint32_t& outInvalidRelativeRoot) const { |
| 187 | outInvalidRelativeRoot = UNASSIGNED_LAYER_ID; |
| 188 | traverse([&outInvalidRelativeRoot](const LayerHierarchy&, |
| 189 | const LayerHierarchy::TraversalPath& traversalPath) -> bool { |
| 190 | if (traversalPath.hasRelZLoop()) { |
| 191 | outInvalidRelativeRoot = traversalPath.invalidRelativeRootId; |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | }); |
| 196 | return outInvalidRelativeRoot != UNASSIGNED_LAYER_ID; |
| 197 | } |
| 198 | |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 199 | void LayerHierarchyBuilder::init(const std::vector<std::unique_ptr<RequestedLayerState>>& layers) { |
| 200 | mLayerIdToHierarchy.clear(); |
| 201 | mHierarchies.clear(); |
| 202 | mRoot = nullptr; |
| 203 | mOffscreenRoot = nullptr; |
| 204 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 205 | mHierarchies.reserve(layers.size()); |
| 206 | mLayerIdToHierarchy.reserve(layers.size()); |
| 207 | for (auto& layer : layers) { |
| 208 | mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get())); |
| 209 | mLayerIdToHierarchy[layer->id] = mHierarchies.back().get(); |
| 210 | } |
| 211 | for (const auto& layer : layers) { |
| 212 | onLayerAdded(layer.get()); |
| 213 | } |
| 214 | detachHierarchyFromRelativeParent(&mOffscreenRoot); |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 215 | mInitialized = true; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void LayerHierarchyBuilder::attachToParent(LayerHierarchy* hierarchy) { |
| 219 | auto layer = hierarchy->mLayer; |
| 220 | LayerHierarchy::Variant type = layer->hasValidRelativeParent() |
| 221 | ? LayerHierarchy::Variant::Detached |
| 222 | : LayerHierarchy::Variant::Attached; |
| 223 | |
| 224 | LayerHierarchy* parent; |
| 225 | |
| 226 | if (layer->parentId != UNASSIGNED_LAYER_ID) { |
| 227 | parent = getHierarchyFromId(layer->parentId); |
| 228 | } else if (layer->canBeRoot) { |
| 229 | parent = &mRoot; |
| 230 | } else { |
| 231 | parent = &mOffscreenRoot; |
| 232 | } |
| 233 | parent->addChild(hierarchy, type); |
| 234 | hierarchy->mParent = parent; |
| 235 | } |
| 236 | |
| 237 | void LayerHierarchyBuilder::detachFromParent(LayerHierarchy* hierarchy) { |
| 238 | hierarchy->mParent->removeChild(hierarchy); |
| 239 | hierarchy->mParent = nullptr; |
| 240 | } |
| 241 | |
| 242 | void LayerHierarchyBuilder::attachToRelativeParent(LayerHierarchy* hierarchy) { |
| 243 | auto layer = hierarchy->mLayer; |
| 244 | if (!layer->hasValidRelativeParent() || hierarchy->mRelativeParent) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | if (layer->relativeParentId != UNASSIGNED_LAYER_ID) { |
| 249 | hierarchy->mRelativeParent = getHierarchyFromId(layer->relativeParentId); |
| 250 | } else { |
| 251 | hierarchy->mRelativeParent = &mOffscreenRoot; |
| 252 | } |
| 253 | hierarchy->mRelativeParent->addChild(hierarchy, LayerHierarchy::Variant::Relative); |
| 254 | hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Detached); |
| 255 | } |
| 256 | |
| 257 | void LayerHierarchyBuilder::detachFromRelativeParent(LayerHierarchy* hierarchy) { |
| 258 | if (hierarchy->mRelativeParent) { |
| 259 | hierarchy->mRelativeParent->removeChild(hierarchy); |
| 260 | } |
| 261 | hierarchy->mRelativeParent = nullptr; |
| 262 | hierarchy->mParent->updateChild(hierarchy, LayerHierarchy::Variant::Attached); |
| 263 | } |
| 264 | |
Vishnu Nair | f12a678 | 2024-06-03 23:08:15 +0000 | [diff] [blame] | 265 | std::vector<LayerHierarchy*> LayerHierarchyBuilder::getDescendants(LayerHierarchy* root) { |
| 266 | std::vector<LayerHierarchy*> hierarchies; |
| 267 | hierarchies.push_back(root); |
| 268 | std::vector<LayerHierarchy*> descendants; |
| 269 | for (size_t i = 0; i < hierarchies.size(); i++) { |
| 270 | LayerHierarchy* hierarchy = hierarchies[i]; |
| 271 | if (hierarchy->mLayer) { |
| 272 | descendants.push_back(hierarchy); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 273 | } |
Vishnu Nair | f12a678 | 2024-06-03 23:08:15 +0000 | [diff] [blame] | 274 | for (auto& [child, childVariant] : hierarchy->mChildren) { |
| 275 | if (childVariant == LayerHierarchy::Variant::Detached || |
| 276 | childVariant == LayerHierarchy::Variant::Attached) { |
| 277 | hierarchies.push_back(child); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | return descendants; |
| 282 | } |
| 283 | |
| 284 | void LayerHierarchyBuilder::attachHierarchyToRelativeParent(LayerHierarchy* root) { |
| 285 | std::vector<LayerHierarchy*> hierarchiesToAttach = getDescendants(root); |
| 286 | for (LayerHierarchy* hierarchy : hierarchiesToAttach) { |
| 287 | attachToRelativeParent(hierarchy); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | void LayerHierarchyBuilder::detachHierarchyFromRelativeParent(LayerHierarchy* root) { |
Vishnu Nair | f12a678 | 2024-06-03 23:08:15 +0000 | [diff] [blame] | 292 | std::vector<LayerHierarchy*> hierarchiesToDetach = getDescendants(root); |
| 293 | for (LayerHierarchy* hierarchy : hierarchiesToDetach) { |
| 294 | detachFromRelativeParent(hierarchy); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | void LayerHierarchyBuilder::onLayerAdded(RequestedLayerState* layer) { |
| 299 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 300 | attachToParent(hierarchy); |
| 301 | attachToRelativeParent(hierarchy); |
| 302 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 303 | for (uint32_t mirrorId : layer->mirrorIds) { |
| 304 | LayerHierarchy* mirror = getHierarchyFromId(mirrorId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 305 | hierarchy->addChild(mirror, LayerHierarchy::Variant::Mirror); |
| 306 | } |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 307 | if (FlagManager::getInstance().detached_mirror()) { |
| 308 | if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) { |
| 309 | LayerHierarchy* mirror = getHierarchyFromId(layer->layerIdToMirror); |
| 310 | hierarchy->addChild(mirror, LayerHierarchy::Variant::Detached_Mirror); |
| 311 | } |
| 312 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void LayerHierarchyBuilder::onLayerDestroyed(RequestedLayerState* layer) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 316 | LLOGV(layer->id, ""); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 317 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id, /*crashOnFailure=*/false); |
| 318 | if (!hierarchy) { |
| 319 | // Layer was never part of the hierarchy if it was created and destroyed in the same |
| 320 | // transaction. |
| 321 | return; |
| 322 | } |
| 323 | // detach from parent |
| 324 | detachFromRelativeParent(hierarchy); |
| 325 | detachFromParent(hierarchy); |
| 326 | |
| 327 | // detach children |
| 328 | for (auto& [child, variant] : hierarchy->mChildren) { |
| 329 | if (variant == LayerHierarchy::Variant::Attached || |
| 330 | variant == LayerHierarchy::Variant::Detached) { |
| 331 | mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached); |
| 332 | child->mParent = &mOffscreenRoot; |
| 333 | } else if (variant == LayerHierarchy::Variant::Relative) { |
| 334 | mOffscreenRoot.addChild(child, LayerHierarchy::Variant::Attached); |
| 335 | child->mRelativeParent = &mOffscreenRoot; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | swapErase(mHierarchies, [hierarchy](std::unique_ptr<LayerHierarchy>& layerHierarchy) { |
| 340 | return layerHierarchy.get() == hierarchy; |
| 341 | }); |
| 342 | mLayerIdToHierarchy.erase(layer->id); |
| 343 | } |
| 344 | |
| 345 | void LayerHierarchyBuilder::updateMirrorLayer(RequestedLayerState* layer) { |
| 346 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 347 | auto it = hierarchy->mChildren.begin(); |
| 348 | while (it != hierarchy->mChildren.end()) { |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 349 | if (LayerHierarchy::isMirror(it->second)) { |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 350 | it = hierarchy->mChildren.erase(it); |
| 351 | } else { |
| 352 | it++; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 353 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 356 | for (uint32_t mirrorId : layer->mirrorIds) { |
| 357 | hierarchy->addChild(getHierarchyFromId(mirrorId), LayerHierarchy::Variant::Mirror); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 358 | } |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 359 | if (FlagManager::getInstance().detached_mirror()) { |
| 360 | if (layer->layerIdToMirror != UNASSIGNED_LAYER_ID) { |
| 361 | hierarchy->addChild(getHierarchyFromId(layer->layerIdToMirror), |
| 362 | LayerHierarchy::Variant::Detached_Mirror); |
| 363 | } |
| 364 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 367 | void LayerHierarchyBuilder::doUpdate( |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 368 | const std::vector<std::unique_ptr<RequestedLayerState>>& layers, |
| 369 | const std::vector<std::unique_ptr<RequestedLayerState>>& destroyedLayers) { |
| 370 | // rebuild map |
| 371 | for (auto& layer : layers) { |
| 372 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 373 | mHierarchies.emplace_back(std::make_unique<LayerHierarchy>(layer.get())); |
| 374 | mLayerIdToHierarchy[layer->id] = mHierarchies.back().get(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | for (auto& layer : layers) { |
| 379 | if (layer->changes.get() == 0) { |
| 380 | continue; |
| 381 | } |
| 382 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 383 | onLayerAdded(layer.get()); |
| 384 | continue; |
| 385 | } |
| 386 | LayerHierarchy* hierarchy = getHierarchyFromId(layer->id); |
| 387 | if (layer->changes.test(RequestedLayerState::Changes::Parent)) { |
| 388 | detachFromParent(hierarchy); |
| 389 | attachToParent(hierarchy); |
| 390 | } |
| 391 | if (layer->changes.test(RequestedLayerState::Changes::RelativeParent)) { |
| 392 | detachFromRelativeParent(hierarchy); |
| 393 | attachToRelativeParent(hierarchy); |
| 394 | } |
| 395 | if (layer->changes.test(RequestedLayerState::Changes::Z)) { |
| 396 | hierarchy->mParent->sortChildrenByZOrder(); |
| 397 | if (hierarchy->mRelativeParent) { |
| 398 | hierarchy->mRelativeParent->sortChildrenByZOrder(); |
| 399 | } |
| 400 | } |
| 401 | if (layer->changes.test(RequestedLayerState::Changes::Mirror)) { |
| 402 | updateMirrorLayer(layer.get()); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | for (auto& layer : destroyedLayers) { |
| 407 | onLayerDestroyed(layer.get()); |
| 408 | } |
| 409 | // When moving from onscreen to offscreen and vice versa, we need to attach and detach |
| 410 | // from our relative parents. This walks down both trees to do so. We can optimize this |
| 411 | // further by tracking onscreen, offscreen state in LayerHierarchy. |
| 412 | detachHierarchyFromRelativeParent(&mOffscreenRoot); |
| 413 | attachHierarchyToRelativeParent(&mRoot); |
| 414 | } |
| 415 | |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 416 | void LayerHierarchyBuilder::update(LayerLifecycleManager& layerLifecycleManager) { |
| 417 | if (!mInitialized) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 418 | SFTRACE_NAME("LayerHierarchyBuilder:init"); |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 419 | init(layerLifecycleManager.getLayers()); |
| 420 | } else if (layerLifecycleManager.getGlobalChanges().test( |
| 421 | RequestedLayerState::Changes::Hierarchy)) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 422 | SFTRACE_NAME("LayerHierarchyBuilder:update"); |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 423 | doUpdate(layerLifecycleManager.getLayers(), layerLifecycleManager.getDestroyedLayers()); |
| 424 | } else { |
| 425 | return; // nothing to do |
| 426 | } |
| 427 | |
| 428 | uint32_t invalidRelativeRoot; |
| 429 | bool hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot); |
| 430 | while (hasRelZLoop) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 431 | SFTRACE_NAME("FixRelZLoop"); |
Vishnu Nair | a029228 | 2023-12-16 14:32:00 -0800 | [diff] [blame] | 432 | TransactionTraceWriter::getInstance().invoke("relz_loop_detected", |
| 433 | /*overwrite=*/false); |
| 434 | layerLifecycleManager.fixRelativeZLoop(invalidRelativeRoot); |
| 435 | // reinitialize the hierarchy with the updated layer data |
| 436 | init(layerLifecycleManager.getLayers()); |
| 437 | // check if we have any remaining loops |
| 438 | hasRelZLoop = mRoot.hasRelZLoop(invalidRelativeRoot); |
| 439 | } |
| 440 | } |
| 441 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 442 | const LayerHierarchy& LayerHierarchyBuilder::getHierarchy() const { |
| 443 | return mRoot; |
| 444 | } |
| 445 | |
| 446 | const LayerHierarchy& LayerHierarchyBuilder::getOffscreenHierarchy() const { |
| 447 | return mOffscreenRoot; |
| 448 | } |
| 449 | |
| 450 | std::string LayerHierarchyBuilder::getDebugString(uint32_t layerId, uint32_t depth) const { |
| 451 | if (depth > 10) return "too deep, loop?"; |
| 452 | if (layerId == UNASSIGNED_LAYER_ID) return ""; |
| 453 | auto it = mLayerIdToHierarchy.find(layerId); |
| 454 | if (it == mLayerIdToHierarchy.end()) return "not found"; |
| 455 | |
| 456 | LayerHierarchy* hierarchy = it->second; |
| 457 | if (!hierarchy->mLayer) return "none"; |
| 458 | |
| 459 | std::string debug = |
| 460 | "[" + std::to_string(hierarchy->mLayer->id) + "] " + hierarchy->mLayer->name; |
| 461 | if (hierarchy->mRelativeParent) { |
| 462 | debug += " Relative:" + hierarchy->mRelativeParent->getDebugStringShort(); |
| 463 | } |
| 464 | if (hierarchy->mParent) { |
| 465 | debug += " Parent:" + hierarchy->mParent->getDebugStringShort(); |
| 466 | } |
| 467 | return debug; |
| 468 | } |
| 469 | |
| 470 | LayerHierarchy LayerHierarchyBuilder::getPartialHierarchy(uint32_t layerId, |
| 471 | bool childrenOnly) const { |
| 472 | auto it = mLayerIdToHierarchy.find(layerId); |
| 473 | if (it == mLayerIdToHierarchy.end()) return {nullptr}; |
| 474 | |
| 475 | LayerHierarchy hierarchy(*it->second, childrenOnly); |
| 476 | return hierarchy; |
| 477 | } |
| 478 | |
| 479 | LayerHierarchy* LayerHierarchyBuilder::getHierarchyFromId(uint32_t layerId, bool crashOnFailure) { |
| 480 | auto it = mLayerIdToHierarchy.find(layerId); |
| 481 | if (it == mLayerIdToHierarchy.end()) { |
Vishnu Nair | 606d9d0 | 2023-08-19 14:20:18 -0700 | [diff] [blame] | 482 | LLOG_ALWAYS_FATAL_WITH_TRACE_IF(crashOnFailure, "Could not find hierarchy for layer id %d", |
| 483 | layerId); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 484 | return nullptr; |
| 485 | }; |
| 486 | |
| 487 | return it->second; |
| 488 | } |
| 489 | |
qinyige1 | 0e011d0 | 2024-05-23 15:59:08 +0800 | [diff] [blame] | 490 | void LayerHierarchyBuilder::logSampledChildren(const LayerHierarchy& hierarchy) const { |
| 491 | LOG(ERROR) << "Dumping random sampling of child layers."; |
| 492 | int sampleSize = static_cast<int>(hierarchy.mChildren.size() / 100 + 1); |
| 493 | for (const auto& [child, variant] : hierarchy.mChildren) { |
| 494 | if (rand() % sampleSize == 0) { |
| 495 | LOG(ERROR) << "Child Layer: " << *(child->mLayer); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void LayerHierarchyBuilder::dumpLayerSample(const LayerHierarchy& root) const { |
| 501 | LOG(ERROR) << "Dumping layer keeping > 20 children alive:"; |
| 502 | // If mLayer is nullptr, it will be skipped while traversing. |
| 503 | if (!root.mLayer && root.mChildren.size() > 20) { |
| 504 | LOG(ERROR) << "ROOT has " << root.mChildren.size() << " children"; |
| 505 | logSampledChildren(root); |
| 506 | } |
| 507 | root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool { |
| 508 | if (hierarchy.mChildren.size() <= 20) { |
| 509 | return true; |
| 510 | } |
| 511 | // mLayer is ensured to be non-null. See LayerHierarchy::traverse. |
| 512 | const auto* layer = hierarchy.mLayer; |
| 513 | const auto childrenCount = hierarchy.mChildren.size(); |
| 514 | LOG(ERROR) << "Layer " << *layer << " has " << childrenCount << " children"; |
| 515 | |
| 516 | const auto* parent = hierarchy.mParent; |
| 517 | while (parent != nullptr) { |
| 518 | if (!parent->mLayer) break; |
| 519 | LOG(ERROR) << "Parent Layer: " << *(parent->mLayer); |
| 520 | parent = parent->mParent; |
| 521 | } |
| 522 | |
| 523 | logSampledChildren(hierarchy); |
| 524 | // Stop traversing. |
| 525 | return false; |
| 526 | }); |
| 527 | LOG(ERROR) << "Dumping random sampled layers."; |
| 528 | size_t numLayers = 0; |
| 529 | root.traverse([&](const LayerHierarchy& hierarchy, const auto&) -> bool { |
| 530 | if (hierarchy.mLayer) numLayers++; |
| 531 | if ((rand() % 20 == 13) && hierarchy.mLayer) { |
| 532 | LOG(ERROR) << "Layer: " << *(hierarchy.mLayer); |
| 533 | } |
| 534 | return true; |
| 535 | }); |
| 536 | LOG(ERROR) << "Total layer count: " << numLayers; |
| 537 | } |
| 538 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 539 | const LayerHierarchy::TraversalPath LayerHierarchy::TraversalPath::ROOT = |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 540 | {.id = UNASSIGNED_LAYER_ID, .variant = LayerHierarchy::Attached}; |
| 541 | |
| 542 | std::string LayerHierarchy::TraversalPath::toString() const { |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 543 | if (id == UNASSIGNED_LAYER_ID) { |
| 544 | return "TraversalPath{ROOT}"; |
| 545 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 546 | std::stringstream ss; |
| 547 | ss << "TraversalPath{.id = " << id; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 548 | |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 549 | if (!mirrorRootIds.empty()) { |
| 550 | ss << ", .mirrorRootIds="; |
| 551 | for (auto rootId : mirrorRootIds) { |
| 552 | ss << rootId << ","; |
| 553 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | if (!relativeRootIds.empty()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 557 | ss << ", .relativeRootIds="; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 558 | for (auto rootId : relativeRootIds) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 559 | ss << rootId << ","; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | if (hasRelZLoop()) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 564 | ss << "hasRelZLoop=true invalidRelativeRootId=" << invalidRelativeRootId << ","; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 565 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 566 | ss << "}"; |
| 567 | return ss.str(); |
| 568 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 569 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 570 | // Helper class to update a passed in TraversalPath when visiting a child. When the object goes out |
| 571 | // of scope the TraversalPath is reset to its original state. |
| 572 | LayerHierarchy::ScopedAddToTraversalPath::ScopedAddToTraversalPath(TraversalPath& traversalPath, |
| 573 | uint32_t layerId, |
| 574 | LayerHierarchy::Variant variant) |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 575 | : mTraversalPath(traversalPath), mParentPath(traversalPath) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 576 | // Update the traversal id with the child layer id and variant. Parent id and variant are |
| 577 | // stored to reset the id upon destruction. |
| 578 | traversalPath.id = layerId; |
| 579 | traversalPath.variant = variant; |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 580 | if (LayerHierarchy::isMirror(variant)) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 581 | traversalPath.mirrorRootIds.emplace_back(mParentPath.id); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 582 | } else if (variant == LayerHierarchy::Variant::Relative) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 583 | if (std::find(traversalPath.relativeRootIds.begin(), traversalPath.relativeRootIds.end(), |
| 584 | layerId) != traversalPath.relativeRootIds.end()) { |
| 585 | traversalPath.invalidRelativeRootId = layerId; |
| 586 | } |
| 587 | traversalPath.relativeRootIds.emplace_back(layerId); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 588 | } else if (variant == LayerHierarchy::Variant::Detached) { |
| 589 | traversalPath.detached = true; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | LayerHierarchy::ScopedAddToTraversalPath::~ScopedAddToTraversalPath() { |
| 593 | // Reset the traversal id to its original parent state using the state that was saved in |
| 594 | // the constructor. |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 595 | if (LayerHierarchy::isMirror(mTraversalPath.variant)) { |
Vishnu Nair | 6f87831 | 2023-09-08 11:05:01 -0700 | [diff] [blame] | 596 | mTraversalPath.mirrorRootIds.pop_back(); |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 597 | } else if (mTraversalPath.variant == LayerHierarchy::Variant::Relative) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 598 | mTraversalPath.relativeRootIds.pop_back(); |
| 599 | } |
| 600 | if (mTraversalPath.invalidRelativeRootId == mTraversalPath.id) { |
| 601 | mTraversalPath.invalidRelativeRootId = UNASSIGNED_LAYER_ID; |
| 602 | } |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 603 | mTraversalPath.id = mParentPath.id; |
| 604 | mTraversalPath.variant = mParentPath.variant; |
| 605 | mTraversalPath.detached = mParentPath.detached; |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | } // namespace android::surfaceflinger::frontend |