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