Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +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 | |
| 19 | #undef LOG_TAG |
| 20 | #define LOG_TAG "LayerLifecycleManager" |
| 21 | |
| 22 | #include "LayerLifecycleManager.h" |
| 23 | #include "Layer.h" // temporarily needed for LayerHandle |
| 24 | #include "LayerHandle.h" |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 25 | #include "LayerLog.h" |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 26 | #include "SwapErase.h" |
| 27 | |
| 28 | namespace android::surfaceflinger::frontend { |
| 29 | |
| 30 | using namespace ftl::flag_operators; |
| 31 | |
| 32 | void LayerLifecycleManager::addLayers(std::vector<std::unique_ptr<RequestedLayerState>> newLayers) { |
| 33 | if (newLayers.empty()) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | mGlobalChanges |= RequestedLayerState::Changes::Hierarchy; |
| 38 | for (auto& newLayer : newLayers) { |
| 39 | RequestedLayerState& layer = *newLayer.get(); |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 40 | LLOGV(layer.id, "%s layer %s", __func__, layer.getDebugStringShort().c_str()); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 41 | auto [it, inserted] = mIdToLayer.try_emplace(layer.id, References{.owner = layer}); |
| 42 | if (!inserted) { |
| 43 | LOG_ALWAYS_FATAL("Duplicate layer id %d found. Existing layer: %s", layer.id, |
| 44 | it->second.owner.getDebugString().c_str()); |
| 45 | } |
| 46 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 47 | layer.parentId = linkLayer(layer.parentId, layer.id); |
| 48 | layer.relativeParentId = linkLayer(layer.relativeParentId, layer.id); |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 49 | if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) { |
| 50 | // if this layer is mirroring a display, then walk though all the existing root layers |
| 51 | // for the layer stack and add them as children to be mirrored. |
| 52 | mDisplayMirroringLayers.emplace_back(layer.id); |
| 53 | for (auto& rootLayer : mLayers) { |
| 54 | if (rootLayer->isRoot() && rootLayer->layerStack == layer.layerStackToMirror) { |
| 55 | layer.mirrorIds.emplace_back(rootLayer->id); |
| 56 | linkLayer(rootLayer->id, layer.id); |
| 57 | } |
| 58 | } |
| 59 | } else { |
| 60 | // Check if we are mirroring a single layer, and if so add it to the list of children |
| 61 | // to be mirrored. |
| 62 | layer.layerIdToMirror = linkLayer(layer.layerIdToMirror, layer.id); |
| 63 | if (layer.layerIdToMirror != UNASSIGNED_LAYER_ID) { |
| 64 | layer.mirrorIds.emplace_back(layer.layerIdToMirror); |
| 65 | } |
| 66 | } |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 67 | layer.touchCropId = linkLayer(layer.touchCropId, layer.id); |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 68 | if (layer.isRoot()) { |
| 69 | updateDisplayMirrorLayers(layer); |
| 70 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 71 | mLayers.emplace_back(std::move(newLayer)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void LayerLifecycleManager::onHandlesDestroyed(const std::vector<uint32_t>& destroyedHandles) { |
| 76 | std::vector<uint32_t> layersToBeDestroyed; |
| 77 | for (const auto& layerId : destroyedHandles) { |
| 78 | auto it = mIdToLayer.find(layerId); |
| 79 | if (it == mIdToLayer.end()) { |
| 80 | LOG_ALWAYS_FATAL("%s Layerid not found %d", __func__, layerId); |
| 81 | continue; |
| 82 | } |
| 83 | RequestedLayerState& layer = it->second.owner; |
| 84 | layer.handleAlive = false; |
| 85 | if (!layer.canBeDestroyed()) { |
| 86 | continue; |
| 87 | } |
| 88 | layer.changes |= RequestedLayerState::Changes::Destroyed; |
| 89 | layersToBeDestroyed.emplace_back(layerId); |
| 90 | } |
| 91 | |
| 92 | if (layersToBeDestroyed.empty()) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | mGlobalChanges |= RequestedLayerState::Changes::Hierarchy; |
| 97 | for (size_t i = 0; i < layersToBeDestroyed.size(); i++) { |
| 98 | uint32_t layerId = layersToBeDestroyed[i]; |
| 99 | auto it = mIdToLayer.find(layerId); |
| 100 | if (it == mIdToLayer.end()) { |
| 101 | LOG_ALWAYS_FATAL("%s Layer with id %d not found", __func__, layerId); |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | RequestedLayerState& layer = it->second.owner; |
| 106 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 107 | layer.parentId = unlinkLayer(layer.parentId, layer.id); |
| 108 | layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id); |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 109 | if (layer.layerStackToMirror != ui::INVALID_LAYER_STACK) { |
| 110 | layer.mirrorIds = unlinkLayers(layer.mirrorIds, layer.id); |
| 111 | swapErase(mDisplayMirroringLayers, layer.id); |
| 112 | } else { |
| 113 | layer.layerIdToMirror = unlinkLayer(layer.layerIdToMirror, layer.id); |
| 114 | layer.mirrorIds.clear(); |
| 115 | } |
| 116 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 117 | layer.touchCropId = unlinkLayer(layer.touchCropId, layer.id); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 118 | |
| 119 | auto& references = it->second.references; |
| 120 | for (uint32_t linkedLayerId : references) { |
| 121 | RequestedLayerState* linkedLayer = getLayerFromId(linkedLayerId); |
| 122 | if (!linkedLayer) { |
| 123 | LOG_ALWAYS_FATAL("%s Layerid reference %d not found for %d", __func__, |
| 124 | linkedLayerId, layer.id); |
| 125 | continue; |
| 126 | }; |
| 127 | if (linkedLayer->parentId == layer.id) { |
| 128 | linkedLayer->parentId = UNASSIGNED_LAYER_ID; |
| 129 | if (linkedLayer->canBeDestroyed()) { |
| 130 | linkedLayer->changes |= RequestedLayerState::Changes::Destroyed; |
| 131 | layersToBeDestroyed.emplace_back(linkedLayer->id); |
| 132 | } |
| 133 | } |
| 134 | if (linkedLayer->relativeParentId == layer.id) { |
| 135 | linkedLayer->relativeParentId = UNASSIGNED_LAYER_ID; |
| 136 | } |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 137 | if (swapErase(linkedLayer->mirrorIds, layer.id)) { |
| 138 | linkedLayer->changes |= RequestedLayerState::Changes::Mirror; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 139 | } |
| 140 | if (linkedLayer->touchCropId == layer.id) { |
| 141 | linkedLayer->touchCropId = UNASSIGNED_LAYER_ID; |
| 142 | } |
| 143 | } |
| 144 | mIdToLayer.erase(it); |
| 145 | } |
| 146 | |
| 147 | auto it = mLayers.begin(); |
| 148 | while (it != mLayers.end()) { |
| 149 | RequestedLayerState* layer = it->get(); |
| 150 | if (layer->changes.test(RequestedLayerState::Changes::Destroyed)) { |
Vishnu Nair | 80a5a70 | 2023-02-11 01:21:51 +0000 | [diff] [blame] | 151 | LLOGV(layer->id, "destroyed layer %s", layer->getDebugStringShort().c_str()); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 152 | std::iter_swap(it, mLayers.end() - 1); |
| 153 | mDestroyedLayers.emplace_back(std::move(mLayers.back())); |
Vishnu Nair | aa548fd | 2022-11-23 18:50:09 +0000 | [diff] [blame] | 154 | if (it == mLayers.end() - 1) { |
| 155 | it = mLayers.erase(mLayers.end() - 1); |
| 156 | } else { |
| 157 | mLayers.erase(mLayers.end() - 1); |
| 158 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 159 | } else { |
| 160 | it++; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void LayerLifecycleManager::applyTransactions(const std::vector<TransactionState>& transactions) { |
| 166 | for (const auto& transaction : transactions) { |
| 167 | for (const auto& resolvedComposerState : transaction.states) { |
| 168 | const auto& clientState = resolvedComposerState.state; |
| 169 | uint32_t layerId = LayerHandle::getLayerId(clientState.surface); |
| 170 | if (layerId == UNASSIGNED_LAYER_ID) { |
| 171 | ALOGW("%s Handle %p is not valid", __func__, clientState.surface.get()); |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | RequestedLayerState* layer = getLayerFromId(layerId); |
| 176 | if (layer == nullptr) { |
| 177 | LOG_ALWAYS_FATAL("%s Layer with handle %p (layerid=%d) not found", __func__, |
| 178 | clientState.surface.get(), layerId); |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | if (!layer->handleAlive) { |
| 183 | LOG_ALWAYS_FATAL("%s Layer's handle %p (layerid=%d) is not alive. Possible out of " |
| 184 | "order LayerLifecycleManager updates", |
| 185 | __func__, clientState.surface.get(), layerId); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | uint32_t oldParentId = layer->parentId; |
| 190 | uint32_t oldRelativeParentId = layer->relativeParentId; |
| 191 | uint32_t oldTouchCropId = layer->touchCropId; |
| 192 | layer->merge(resolvedComposerState); |
| 193 | |
| 194 | if (layer->what & layer_state_t::eBackgroundColorChanged) { |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 195 | if (layer->bgColorLayerId == UNASSIGNED_LAYER_ID && layer->bgColor.a != 0) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 196 | LayerCreationArgs backgroundLayerArgs{nullptr, |
| 197 | nullptr, |
| 198 | layer->name + "BackgroundColorLayer", |
| 199 | ISurfaceComposerClient::eFXSurfaceEffect, |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 200 | {}, |
| 201 | layer->id, |
| 202 | /*internalLayer=*/true}; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 203 | std::vector<std::unique_ptr<RequestedLayerState>> newLayers; |
| 204 | newLayers.emplace_back( |
| 205 | std::make_unique<RequestedLayerState>(backgroundLayerArgs)); |
| 206 | RequestedLayerState* backgroundLayer = newLayers.back().get(); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 207 | backgroundLayer->bgColorLayer = true; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 208 | backgroundLayer->handleAlive = false; |
| 209 | backgroundLayer->parentId = layer->id; |
| 210 | backgroundLayer->z = std::numeric_limits<int32_t>::min(); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 211 | backgroundLayer->color = layer->bgColor; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 212 | backgroundLayer->dataspace = layer->bgColorDataspace; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 213 | layer->bgColorLayerId = backgroundLayer->id; |
| 214 | addLayers({std::move(newLayers)}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 215 | } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID && layer->bgColor.a == 0) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 216 | RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 217 | layer->bgColorLayerId = UNASSIGNED_LAYER_ID; |
| 218 | bgColorLayer->parentId = unlinkLayer(bgColorLayer->parentId, bgColorLayer->id); |
| 219 | onHandlesDestroyed({bgColorLayer->id}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 220 | } else if (layer->bgColorLayerId != UNASSIGNED_LAYER_ID) { |
| 221 | RequestedLayerState* bgColorLayer = getLayerFromId(layer->bgColorLayerId); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 222 | bgColorLayer->color = layer->bgColor; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 223 | bgColorLayer->dataspace = layer->bgColorDataspace; |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 224 | bgColorLayer->what |= layer_state_t::eColorChanged | |
| 225 | layer_state_t::eDataspaceChanged | layer_state_t::eAlphaChanged; |
| 226 | bgColorLayer->changes |= RequestedLayerState::Changes::Content; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 227 | mGlobalChanges |= RequestedLayerState::Changes::Content; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (oldParentId != layer->parentId) { |
| 232 | unlinkLayer(oldParentId, layer->id); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 233 | layer->parentId = linkLayer(layer->parentId, layer->id); |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 234 | if (oldParentId == UNASSIGNED_LAYER_ID) { |
| 235 | updateDisplayMirrorLayers(*layer); |
| 236 | } |
| 237 | } |
| 238 | if (layer->what & layer_state_t::eLayerStackChanged && layer->isRoot()) { |
| 239 | updateDisplayMirrorLayers(*layer); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 240 | } |
| 241 | if (oldRelativeParentId != layer->relativeParentId) { |
| 242 | unlinkLayer(oldRelativeParentId, layer->id); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 243 | layer->relativeParentId = linkLayer(layer->relativeParentId, layer->id); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 244 | } |
| 245 | if (oldTouchCropId != layer->touchCropId) { |
| 246 | unlinkLayer(oldTouchCropId, layer->id); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 247 | layer->touchCropId = linkLayer(layer->touchCropId, layer->id); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Vishnu Nair | 8fc721b | 2022-12-22 20:06:32 +0000 | [diff] [blame] | 250 | mGlobalChanges |= layer->changes; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void LayerLifecycleManager::commitChanges() { |
| 256 | for (auto& layer : mLayers) { |
| 257 | if (layer->changes.test(RequestedLayerState::Changes::Created)) { |
| 258 | for (auto listener : mListeners) { |
| 259 | listener->onLayerAdded(*layer); |
| 260 | } |
| 261 | } |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 262 | layer->clearChanges(); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | for (auto& destroyedLayer : mDestroyedLayers) { |
| 266 | if (destroyedLayer->changes.test(RequestedLayerState::Changes::Created)) { |
| 267 | for (auto listener : mListeners) { |
| 268 | listener->onLayerAdded(*destroyedLayer); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | for (auto listener : mListeners) { |
| 273 | listener->onLayerDestroyed(*destroyedLayer); |
| 274 | } |
| 275 | } |
| 276 | mDestroyedLayers.clear(); |
| 277 | mGlobalChanges.clear(); |
| 278 | } |
| 279 | |
| 280 | void LayerLifecycleManager::addLifecycleListener(std::shared_ptr<ILifecycleListener> listener) { |
| 281 | mListeners.emplace_back(std::move(listener)); |
| 282 | } |
| 283 | |
| 284 | void LayerLifecycleManager::removeLifecycleListener(std::shared_ptr<ILifecycleListener> listener) { |
| 285 | swapErase(mListeners, listener); |
| 286 | } |
| 287 | |
| 288 | const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getLayers() const { |
| 289 | return mLayers; |
| 290 | } |
| 291 | |
| 292 | const std::vector<std::unique_ptr<RequestedLayerState>>& LayerLifecycleManager::getDestroyedLayers() |
| 293 | const { |
| 294 | return mDestroyedLayers; |
| 295 | } |
| 296 | |
| 297 | const ftl::Flags<RequestedLayerState::Changes> LayerLifecycleManager::getGlobalChanges() const { |
| 298 | return mGlobalChanges; |
| 299 | } |
| 300 | |
| 301 | RequestedLayerState* LayerLifecycleManager::getLayerFromId(uint32_t id) { |
| 302 | if (id == UNASSIGNED_LAYER_ID) { |
| 303 | return nullptr; |
| 304 | } |
| 305 | auto it = mIdToLayer.find(id); |
| 306 | if (it == mIdToLayer.end()) { |
| 307 | return nullptr; |
| 308 | } |
| 309 | return &it->second.owner; |
| 310 | } |
| 311 | |
| 312 | std::vector<uint32_t>* LayerLifecycleManager::getLinkedLayersFromId(uint32_t id) { |
| 313 | if (id == UNASSIGNED_LAYER_ID) { |
| 314 | return nullptr; |
| 315 | } |
| 316 | auto it = mIdToLayer.find(id); |
| 317 | if (it == mIdToLayer.end()) { |
| 318 | return nullptr; |
| 319 | } |
| 320 | return &it->second.references; |
| 321 | } |
| 322 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 323 | uint32_t LayerLifecycleManager::linkLayer(uint32_t layerId, uint32_t layerToLink) { |
| 324 | if (layerId == UNASSIGNED_LAYER_ID) { |
| 325 | return UNASSIGNED_LAYER_ID; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 326 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 327 | |
| 328 | std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId); |
| 329 | if (!linkedLayers) { |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 330 | ALOGV("Could not find layer id %d to link %d. Parent is probably destroyed", layerId, |
| 331 | layerToLink); |
| 332 | return UNASSIGNED_LAYER_ID; |
| 333 | } |
| 334 | linkedLayers->emplace_back(layerToLink); |
| 335 | return layerId; |
| 336 | } |
| 337 | |
| 338 | uint32_t LayerLifecycleManager::unlinkLayer(uint32_t layerId, uint32_t linkedLayer) { |
| 339 | std::vector<uint32_t>* linkedLayers = getLinkedLayersFromId(layerId); |
| 340 | if (!linkedLayers) { |
| 341 | return UNASSIGNED_LAYER_ID; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 342 | } |
| 343 | swapErase(*linkedLayers, linkedLayer); |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 344 | return UNASSIGNED_LAYER_ID; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 347 | std::vector<uint32_t> LayerLifecycleManager::unlinkLayers(const std::vector<uint32_t>& layerIds, |
| 348 | uint32_t linkedLayer) { |
| 349 | for (uint32_t layerId : layerIds) { |
| 350 | unlinkLayer(layerId, linkedLayer); |
| 351 | } |
| 352 | return {}; |
| 353 | } |
| 354 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 355 | std::string LayerLifecycleManager::References::getDebugString() const { |
| 356 | std::string debugInfo = owner.name + "[" + std::to_string(owner.id) + "] refs:"; |
| 357 | std::for_each(references.begin(), references.end(), |
| 358 | [&debugInfo = debugInfo](const uint32_t& reference) mutable { |
| 359 | debugInfo += std::to_string(reference) + ","; |
| 360 | }); |
| 361 | return debugInfo; |
| 362 | } |
| 363 | |
Vishnu Nair | 04f8969 | 2022-11-16 23:21:05 +0000 | [diff] [blame] | 364 | void LayerLifecycleManager::fixRelativeZLoop(uint32_t relativeRootId) { |
| 365 | auto it = mIdToLayer.find(relativeRootId); |
| 366 | if (it == mIdToLayer.end()) { |
| 367 | return; |
| 368 | } |
| 369 | RequestedLayerState& layer = it->second.owner; |
| 370 | layer.relativeParentId = unlinkLayer(layer.relativeParentId, layer.id); |
| 371 | layer.changes |= |
| 372 | RequestedLayerState::Changes::Hierarchy | RequestedLayerState::Changes::RelativeParent; |
| 373 | mGlobalChanges |= RequestedLayerState::Changes::Hierarchy; |
| 374 | } |
| 375 | |
Vishnu Nair | a9c4376 | 2023-01-27 19:10:25 +0000 | [diff] [blame] | 376 | // Some layers mirror the entire display stack. Since we don't have a single root layer per display |
| 377 | // we have to track all these layers and update what they mirror when the list of root layers |
| 378 | // on a display changes. This function walks through the list of display mirroring layers |
| 379 | // and updates its list of layers that its mirroring. This function should be called when a new |
| 380 | // root layer is added, removed or moved to another display. |
| 381 | void LayerLifecycleManager::updateDisplayMirrorLayers(RequestedLayerState& rootLayer) { |
| 382 | for (uint32_t mirrorLayerId : mDisplayMirroringLayers) { |
| 383 | RequestedLayerState* mirrorLayer = getLayerFromId(mirrorLayerId); |
| 384 | bool canBeMirrored = |
| 385 | rootLayer.isRoot() && rootLayer.layerStack == mirrorLayer->layerStackToMirror; |
| 386 | bool currentlyMirrored = |
| 387 | std::find(mirrorLayer->mirrorIds.begin(), mirrorLayer->mirrorIds.end(), |
| 388 | rootLayer.id) != mirrorLayer->mirrorIds.end(); |
| 389 | |
| 390 | if (canBeMirrored && !currentlyMirrored) { |
| 391 | mirrorLayer->mirrorIds.emplace_back(rootLayer.id); |
| 392 | linkLayer(rootLayer.id, mirrorLayer->id); |
| 393 | mirrorLayer->changes |= RequestedLayerState::Changes::Mirror; |
| 394 | } else if (!canBeMirrored && currentlyMirrored) { |
| 395 | swapErase(mirrorLayer->mirrorIds, rootLayer.id); |
| 396 | unlinkLayer(rootLayer.id, mirrorLayer->id); |
| 397 | mirrorLayer->changes |= RequestedLayerState::Changes::Mirror; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 402 | } // namespace android::surfaceflinger::frontend |