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