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 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 20 | #include "FrontEnd/LayerLifecycleManager.h" |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 21 | #include "LayerHierarchyTest.h" |
| 22 | #include "TransactionState.h" |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace android::surfaceflinger; |
| 25 | |
| 26 | namespace android::surfaceflinger::frontend { |
| 27 | |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 28 | // To run test: |
| 29 | /** |
| 30 | mp :libsurfaceflinger_unittest && adb sync; adb shell \ |
| 31 | /data/nativetest/libsurfaceflinger_unittest/libsurfaceflinger_unittest \ |
| 32 | --gtest_filter="LayerLifecycleManagerTest.*" --gtest_repeat=100 \ |
| 33 | --gtest_shuffle \ |
| 34 | --gtest_brief=1 |
| 35 | */ |
| 36 | class ExpectLayerLifecycleListener : public LayerLifecycleManager::ILifecycleListener { |
| 37 | public: |
| 38 | void onLayerAdded(const RequestedLayerState& layer) override { |
| 39 | mActualLayersAdded.emplace(layer.id); |
| 40 | }; |
| 41 | void onLayerDestroyed(const RequestedLayerState& layer) override { |
| 42 | mActualLayersDestroyed.emplace(layer.id); |
| 43 | }; |
| 44 | |
| 45 | void expectLayersAdded(const std::unordered_set<uint32_t>& expectedLayersAdded) { |
| 46 | EXPECT_EQ(expectedLayersAdded, mActualLayersAdded); |
| 47 | mActualLayersAdded.clear(); |
| 48 | } |
| 49 | void expectLayersDestroyed(const std::unordered_set<uint32_t>& expectedLayersDestroyed) { |
| 50 | EXPECT_EQ(expectedLayersDestroyed, mActualLayersDestroyed); |
| 51 | mActualLayersDestroyed.clear(); |
| 52 | } |
| 53 | |
| 54 | std::unordered_set<uint32_t> mActualLayersAdded; |
| 55 | std::unordered_set<uint32_t> mActualLayersDestroyed; |
| 56 | }; |
| 57 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 58 | class LayerLifecycleManagerTest : public LayerHierarchyTestBase { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 59 | protected: |
| 60 | std::unique_ptr<RequestedLayerState> rootLayer(uint32_t id) { |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 61 | return std::make_unique<RequestedLayerState>(createArgs(/*id=*/id, /*canBeRoot=*/true, |
| 62 | /*parent=*/UNASSIGNED_LAYER_ID, |
| 63 | /*mirror=*/UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::unique_ptr<RequestedLayerState> childLayer(uint32_t id, uint32_t parentId) { |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 67 | return std::make_unique<RequestedLayerState>(createArgs(/*id=*/id, /*canBeRoot=*/false, |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 68 | parentId, |
| 69 | /*mirror=*/UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | RequestedLayerState* getRequestedLayerState(LayerLifecycleManager& lifecycleManager, |
| 73 | uint32_t layerId) { |
| 74 | return lifecycleManager.getLayerFromId(layerId); |
| 75 | } |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | TEST_F(LayerLifecycleManagerTest, addLayers) { |
| 79 | LayerLifecycleManager lifecycleManager; |
| 80 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 81 | lifecycleManager.addLifecycleListener(listener); |
| 82 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 83 | layers.emplace_back(rootLayer(1)); |
| 84 | layers.emplace_back(rootLayer(2)); |
| 85 | layers.emplace_back(rootLayer(3)); |
| 86 | lifecycleManager.addLayers(std::move(layers)); |
| 87 | lifecycleManager.onHandlesDestroyed({1, 2, 3}); |
| 88 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 89 | lifecycleManager.commitChanges(); |
| 90 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 91 | listener->expectLayersAdded({1, 2, 3}); |
| 92 | listener->expectLayersDestroyed({1, 2, 3}); |
| 93 | } |
| 94 | |
| 95 | TEST_F(LayerLifecycleManagerTest, updateLayerStates) { |
| 96 | LayerLifecycleManager lifecycleManager; |
| 97 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 98 | layers.emplace_back(rootLayer(1)); |
| 99 | lifecycleManager.addLayers(std::move(layers)); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 100 | lifecycleManager.applyTransactions(setZTransaction(1, 2)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 101 | |
| 102 | auto& managedLayers = lifecycleManager.getLayers(); |
| 103 | ASSERT_EQ(managedLayers.size(), 1u); |
| 104 | |
| 105 | EXPECT_EQ(managedLayers.front()->z, 2); |
| 106 | EXPECT_TRUE(managedLayers.front()->changes.test(RequestedLayerState::Changes::Z)); |
| 107 | |
| 108 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 109 | lifecycleManager.commitChanges(); |
| 110 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 111 | ASSERT_EQ(managedLayers.size(), 1u); |
| 112 | EXPECT_FALSE(managedLayers.front()->changes.test(RequestedLayerState::Changes::Z)); |
| 113 | |
| 114 | // apply transactions that do not affect the hierarchy |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 115 | std::vector<TransactionState> transactions; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 116 | transactions.emplace_back(); |
| 117 | transactions.back().states.push_back({}); |
| 118 | transactions.back().states.front().state.backgroundBlurRadius = 22; |
| 119 | transactions.back().states.front().state.what = layer_state_t::eBackgroundBlurRadiusChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 120 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 121 | lifecycleManager.applyTransactions(transactions); |
| 122 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 123 | lifecycleManager.commitChanges(); |
| 124 | EXPECT_FALSE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 125 | EXPECT_EQ(managedLayers.front()->backgroundBlurRadius, 22u); |
| 126 | } |
| 127 | |
| 128 | TEST_F(LayerLifecycleManagerTest, layerWithoutHandleIsDestroyed) { |
| 129 | LayerLifecycleManager lifecycleManager; |
| 130 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 131 | lifecycleManager.addLifecycleListener(listener); |
| 132 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 133 | layers.emplace_back(rootLayer(1)); |
| 134 | layers.emplace_back(rootLayer(2)); |
| 135 | lifecycleManager.addLayers(std::move(layers)); |
| 136 | lifecycleManager.onHandlesDestroyed({1}); |
| 137 | lifecycleManager.commitChanges(); |
| 138 | |
| 139 | SCOPED_TRACE("layerWithoutHandleIsDestroyed"); |
| 140 | listener->expectLayersAdded({1, 2}); |
| 141 | listener->expectLayersDestroyed({1}); |
| 142 | } |
| 143 | |
| 144 | TEST_F(LayerLifecycleManagerTest, rootLayerWithoutHandleIsDestroyed) { |
| 145 | LayerLifecycleManager lifecycleManager; |
| 146 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 147 | lifecycleManager.addLifecycleListener(listener); |
| 148 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 149 | layers.emplace_back(rootLayer(1)); |
| 150 | layers.emplace_back(rootLayer(2)); |
| 151 | lifecycleManager.addLayers(std::move(layers)); |
| 152 | lifecycleManager.onHandlesDestroyed({1}); |
| 153 | lifecycleManager.commitChanges(); |
| 154 | listener->expectLayersAdded({1, 2}); |
| 155 | listener->expectLayersDestroyed({1}); |
| 156 | } |
| 157 | |
| 158 | TEST_F(LayerLifecycleManagerTest, offscreenLayerIsDestroyed) { |
| 159 | LayerLifecycleManager lifecycleManager; |
| 160 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 161 | lifecycleManager.addLifecycleListener(listener); |
| 162 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 163 | layers.emplace_back(rootLayer(1)); |
| 164 | layers.emplace_back(rootLayer(2)); |
| 165 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 166 | lifecycleManager.addLayers(std::move(layers)); |
| 167 | lifecycleManager.commitChanges(); |
| 168 | listener->expectLayersAdded({1, 2, 3}); |
| 169 | listener->expectLayersDestroyed({}); |
| 170 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 171 | lifecycleManager.applyTransactions(reparentLayerTransaction(3, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 172 | lifecycleManager.commitChanges(); |
| 173 | listener->expectLayersAdded({}); |
| 174 | listener->expectLayersDestroyed({}); |
| 175 | |
| 176 | lifecycleManager.onHandlesDestroyed({3}); |
| 177 | lifecycleManager.commitChanges(); |
| 178 | listener->expectLayersAdded({}); |
| 179 | listener->expectLayersDestroyed({3}); |
| 180 | } |
| 181 | |
| 182 | TEST_F(LayerLifecycleManagerTest, offscreenChildLayerWithHandleIsNotDestroyed) { |
| 183 | LayerLifecycleManager lifecycleManager; |
| 184 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 185 | lifecycleManager.addLifecycleListener(listener); |
| 186 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 187 | layers.emplace_back(rootLayer(1)); |
| 188 | layers.emplace_back(rootLayer(2)); |
| 189 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 190 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 191 | lifecycleManager.addLayers(std::move(layers)); |
| 192 | lifecycleManager.commitChanges(); |
| 193 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 194 | listener->expectLayersDestroyed({}); |
| 195 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 196 | lifecycleManager.applyTransactions(reparentLayerTransaction(3, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 197 | lifecycleManager.onHandlesDestroyed({3}); |
| 198 | lifecycleManager.commitChanges(); |
| 199 | listener->expectLayersAdded({}); |
| 200 | listener->expectLayersDestroyed({3}); |
| 201 | } |
| 202 | |
| 203 | TEST_F(LayerLifecycleManagerTest, offscreenChildLayerWithoutHandleIsDestroyed) { |
| 204 | LayerLifecycleManager lifecycleManager; |
| 205 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 206 | lifecycleManager.addLifecycleListener(listener); |
| 207 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 208 | layers.emplace_back(rootLayer(1)); |
| 209 | layers.emplace_back(rootLayer(2)); |
| 210 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 211 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 212 | lifecycleManager.addLayers(std::move(layers)); |
| 213 | lifecycleManager.commitChanges(); |
| 214 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 215 | listener->expectLayersDestroyed({}); |
| 216 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 217 | lifecycleManager.applyTransactions(reparentLayerTransaction(3, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 218 | lifecycleManager.onHandlesDestroyed({3, 4}); |
| 219 | lifecycleManager.commitChanges(); |
| 220 | listener->expectLayersAdded({}); |
| 221 | listener->expectLayersDestroyed({3, 4}); |
| 222 | } |
| 223 | |
| 224 | TEST_F(LayerLifecycleManagerTest, reparentingDoesNotAffectRelativeZ) { |
| 225 | LayerLifecycleManager lifecycleManager; |
| 226 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 227 | lifecycleManager.addLifecycleListener(listener); |
| 228 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 229 | layers.emplace_back(rootLayer(1)); |
| 230 | layers.emplace_back(rootLayer(2)); |
| 231 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 232 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 233 | |
| 234 | lifecycleManager.addLayers(std::move(layers)); |
| 235 | lifecycleManager.commitChanges(); |
| 236 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 237 | listener->expectLayersDestroyed({}); |
| 238 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 239 | lifecycleManager.applyTransactions(relativeLayerTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 240 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 241 | lifecycleManager.applyTransactions(reparentLayerTransaction(4, 2)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 242 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
| 243 | |
| 244 | lifecycleManager.commitChanges(); |
| 245 | listener->expectLayersAdded({}); |
| 246 | listener->expectLayersDestroyed({}); |
| 247 | } |
| 248 | |
| 249 | TEST_F(LayerLifecycleManagerTest, reparentingToNullRemovesRelativeZ) { |
| 250 | LayerLifecycleManager lifecycleManager; |
| 251 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 252 | lifecycleManager.addLifecycleListener(listener); |
| 253 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 254 | layers.emplace_back(rootLayer(1)); |
| 255 | layers.emplace_back(rootLayer(2)); |
| 256 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 257 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 258 | |
| 259 | lifecycleManager.addLayers(std::move(layers)); |
| 260 | lifecycleManager.commitChanges(); |
| 261 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 262 | listener->expectLayersDestroyed({}); |
| 263 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 264 | lifecycleManager.applyTransactions(relativeLayerTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 265 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 266 | lifecycleManager.applyTransactions(reparentLayerTransaction(4, UNASSIGNED_LAYER_ID)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 267 | EXPECT_FALSE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
| 268 | |
| 269 | lifecycleManager.commitChanges(); |
| 270 | listener->expectLayersAdded({}); |
| 271 | listener->expectLayersDestroyed({}); |
| 272 | } |
| 273 | |
| 274 | TEST_F(LayerLifecycleManagerTest, setZRemovesRelativeZ) { |
| 275 | LayerLifecycleManager lifecycleManager; |
| 276 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 277 | lifecycleManager.addLifecycleListener(listener); |
| 278 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 279 | layers.emplace_back(rootLayer(1)); |
| 280 | layers.emplace_back(rootLayer(2)); |
| 281 | layers.emplace_back(childLayer(3, /*parent*/ 2)); |
| 282 | layers.emplace_back(childLayer(4, /*parent*/ 3)); |
| 283 | |
| 284 | lifecycleManager.addLayers(std::move(layers)); |
| 285 | lifecycleManager.commitChanges(); |
| 286 | listener->expectLayersAdded({1, 2, 3, 4}); |
| 287 | listener->expectLayersDestroyed({}); |
| 288 | |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 289 | lifecycleManager.applyTransactions(relativeLayerTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 290 | EXPECT_TRUE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 291 | lifecycleManager.applyTransactions(setZTransaction(4, 1)); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 292 | EXPECT_FALSE(getRequestedLayerState(lifecycleManager, 4)->isRelativeOf); |
| 293 | |
| 294 | lifecycleManager.commitChanges(); |
| 295 | listener->expectLayersAdded({}); |
| 296 | listener->expectLayersDestroyed({}); |
| 297 | } |
| 298 | |
| 299 | TEST_F(LayerLifecycleManagerTest, canAddBackgroundLayer) { |
| 300 | LayerLifecycleManager lifecycleManager; |
| 301 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 302 | lifecycleManager.addLifecycleListener(listener); |
| 303 | |
| 304 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 305 | layers.emplace_back(rootLayer(1)); |
| 306 | lifecycleManager.addLayers(std::move(layers)); |
| 307 | |
| 308 | std::vector<TransactionState> transactions; |
| 309 | transactions.emplace_back(); |
| 310 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 311 | transactions.back().states.front().state.bgColor.a = 0.5; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 312 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 313 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 314 | lifecycleManager.applyTransactions(transactions); |
| 315 | |
| 316 | auto& managedLayers = lifecycleManager.getLayers(); |
| 317 | ASSERT_EQ(managedLayers.size(), 2u); |
| 318 | |
| 319 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 320 | lifecycleManager.commitChanges(); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 321 | auto bgLayerId = LayerCreationArgs::getInternalLayerId(1); |
| 322 | listener->expectLayersAdded({1, bgLayerId}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 323 | listener->expectLayersDestroyed({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 324 | EXPECT_EQ(getRequestedLayerState(lifecycleManager, bgLayerId)->color.a, 0.5_hf); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | TEST_F(LayerLifecycleManagerTest, canDestroyBackgroundLayer) { |
| 328 | LayerLifecycleManager lifecycleManager; |
| 329 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 330 | lifecycleManager.addLifecycleListener(listener); |
| 331 | |
| 332 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 333 | layers.emplace_back(rootLayer(1)); |
| 334 | lifecycleManager.addLayers(std::move(layers)); |
| 335 | |
| 336 | std::vector<TransactionState> transactions; |
| 337 | transactions.emplace_back(); |
| 338 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 339 | transactions.back().states.front().state.bgColor.a = 0.5; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 340 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 341 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 342 | transactions.emplace_back(); |
| 343 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 344 | transactions.back().states.front().state.bgColor.a = 0; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 345 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 346 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 347 | |
| 348 | lifecycleManager.applyTransactions(transactions); |
| 349 | |
| 350 | ASSERT_EQ(lifecycleManager.getLayers().size(), 1u); |
| 351 | ASSERT_EQ(lifecycleManager.getDestroyedLayers().size(), 1u); |
| 352 | |
| 353 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 354 | lifecycleManager.commitChanges(); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 355 | auto bgLayerId = LayerCreationArgs::getInternalLayerId(1); |
| 356 | listener->expectLayersAdded({1, bgLayerId}); |
| 357 | listener->expectLayersDestroyed({bgLayerId}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | TEST_F(LayerLifecycleManagerTest, onParentDestroyDestroysBackgroundLayer) { |
| 361 | LayerLifecycleManager lifecycleManager; |
| 362 | auto listener = std::make_shared<ExpectLayerLifecycleListener>(); |
| 363 | lifecycleManager.addLifecycleListener(listener); |
| 364 | |
| 365 | std::vector<std::unique_ptr<RequestedLayerState>> layers; |
| 366 | layers.emplace_back(rootLayer(1)); |
| 367 | lifecycleManager.addLayers(std::move(layers)); |
| 368 | |
| 369 | std::vector<TransactionState> transactions; |
| 370 | transactions.emplace_back(); |
| 371 | transactions.back().states.push_back({}); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 372 | transactions.back().states.front().state.bgColor.a = 0.5; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 373 | transactions.back().states.front().state.what = layer_state_t::eBackgroundColorChanged; |
Vishnu Nair | 1391de2 | 2023-03-05 19:56:14 -0800 | [diff] [blame^] | 374 | transactions.back().states.front().layerId = 1; |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 375 | transactions.emplace_back(); |
| 376 | lifecycleManager.applyTransactions(transactions); |
| 377 | lifecycleManager.onHandlesDestroyed({1}); |
| 378 | |
| 379 | ASSERT_EQ(lifecycleManager.getLayers().size(), 0u); |
| 380 | ASSERT_EQ(lifecycleManager.getDestroyedLayers().size(), 2u); |
| 381 | |
| 382 | EXPECT_TRUE(lifecycleManager.getGlobalChanges().test(RequestedLayerState::Changes::Hierarchy)); |
| 383 | lifecycleManager.commitChanges(); |
Vishnu Nair | d47bcee | 2023-02-24 18:08:51 +0000 | [diff] [blame] | 384 | auto bgLayerId = LayerCreationArgs::getInternalLayerId(1); |
| 385 | listener->expectLayersAdded({1, bgLayerId}); |
| 386 | listener->expectLayersDestroyed({1, bgLayerId}); |
Vishnu Nair | dc4d31b | 2022-11-17 03:20:58 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | } // namespace android::surfaceflinger::frontend |