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