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