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