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