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