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