| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2020 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 | #undef LOG_TAG | 
|  | 18 | #define LOG_TAG "LibSurfaceFlingerUnittests" | 
|  | 19 |  | 
|  | 20 | #include <gmock/gmock.h> | 
|  | 21 | #include <gtest/gtest.h> | 
|  | 22 | #include <gui/LayerMetadata.h> | 
|  | 23 |  | 
|  | 24 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 25 | #pragma clang diagnostic push | 
|  | 26 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 27 | #include "BufferQueueLayer.h" | 
|  | 28 | #include "BufferStateLayer.h" | 
|  | 29 | #include "EffectLayer.h" | 
|  | 30 | #include "Layer.h" | 
|  | 31 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 32 | #pragma clang diagnostic pop // ignored "-Wconversion" | 
|  | 33 | #include "TestableSurfaceFlinger.h" | 
|  | 34 | #include "mock/DisplayHardware/MockComposer.h" | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 35 | #include "mock/MockEventThread.h" | 
|  | 36 | #include "mock/MockMessageQueue.h" | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 37 | #include "mock/MockVsyncController.h" | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 38 |  | 
|  | 39 | namespace android { | 
|  | 40 |  | 
|  | 41 | using testing::_; | 
|  | 42 | using testing::DoAll; | 
|  | 43 | using testing::Mock; | 
|  | 44 | using testing::Return; | 
|  | 45 | using testing::SetArgPointee; | 
|  | 46 |  | 
|  | 47 | using android::Hwc2::IComposer; | 
|  | 48 | using android::Hwc2::IComposerClient; | 
|  | 49 |  | 
|  | 50 | using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector; | 
|  | 51 |  | 
|  | 52 | using FrameRate = Layer::FrameRate; | 
|  | 53 | using FrameRateCompatibility = Layer::FrameRateCompatibility; | 
|  | 54 |  | 
|  | 55 | class LayerFactory { | 
|  | 56 | public: | 
|  | 57 | virtual ~LayerFactory() = default; | 
|  | 58 |  | 
|  | 59 | virtual std::string name() = 0; | 
|  | 60 | virtual sp<Layer> createLayer(TestableSurfaceFlinger& flinger) = 0; | 
|  | 61 |  | 
|  | 62 | protected: | 
|  | 63 | static constexpr uint32_t WIDTH = 100; | 
|  | 64 | static constexpr uint32_t HEIGHT = 100; | 
|  | 65 | static constexpr uint32_t LAYER_FLAGS = 0; | 
|  | 66 | }; | 
|  | 67 |  | 
|  | 68 | class BufferQueueLayerFactory : public LayerFactory { | 
|  | 69 | public: | 
|  | 70 | std::string name() override { return "BufferQueueLayer"; } | 
|  | 71 | sp<Layer> createLayer(TestableSurfaceFlinger& flinger) override { | 
|  | 72 | sp<Client> client; | 
|  | 73 | LayerCreationArgs args(flinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT, | 
|  | 74 | LAYER_FLAGS, LayerMetadata()); | 
|  | 75 | return new BufferQueueLayer(args); | 
|  | 76 | } | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | class BufferStateLayerFactory : public LayerFactory { | 
|  | 80 | public: | 
|  | 81 | std::string name() override { return "BufferStateLayer"; } | 
|  | 82 | sp<Layer> createLayer(TestableSurfaceFlinger& flinger) override { | 
|  | 83 | sp<Client> client; | 
| Ady Abraham | 44e9f3b | 2021-02-16 15:22:58 -0800 | [diff] [blame] | 84 | LayerCreationArgs args(flinger.flinger(), client, "buffer-state-layer", WIDTH, HEIGHT, | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 85 | LAYER_FLAGS, LayerMetadata()); | 
|  | 86 | return new BufferStateLayer(args); | 
|  | 87 | } | 
|  | 88 | }; | 
|  | 89 |  | 
|  | 90 | class EffectLayerFactory : public LayerFactory { | 
|  | 91 | public: | 
|  | 92 | std::string name() override { return "EffectLayer"; } | 
|  | 93 | sp<Layer> createLayer(TestableSurfaceFlinger& flinger) override { | 
|  | 94 | sp<Client> client; | 
|  | 95 | LayerCreationArgs args(flinger.flinger(), client, "color-layer", WIDTH, HEIGHT, LAYER_FLAGS, | 
|  | 96 | LayerMetadata()); | 
|  | 97 | return new EffectLayer(args); | 
|  | 98 | } | 
|  | 99 | }; | 
|  | 100 |  | 
|  | 101 | std::string PrintToStringParamName( | 
|  | 102 | const ::testing::TestParamInfo<std::shared_ptr<LayerFactory>>& info) { | 
|  | 103 | return info.param->name(); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | /** | 
|  | 107 | * This class tests the behaviour of Layer::SetFrameRate and Layer::GetFrameRate | 
|  | 108 | */ | 
|  | 109 | class SetFrameRateTest : public ::testing::TestWithParam<std::shared_ptr<LayerFactory>> { | 
|  | 110 | protected: | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 111 | const FrameRate FRAME_RATE_VOTE1 = FrameRate(Fps(67.f), FrameRateCompatibility::Default); | 
|  | 112 | const FrameRate FRAME_RATE_VOTE2 = | 
|  | 113 | FrameRate(Fps(14.f), FrameRateCompatibility::ExactOrMultiple); | 
|  | 114 | const FrameRate FRAME_RATE_VOTE3 = FrameRate(Fps(99.f), FrameRateCompatibility::NoVote); | 
|  | 115 | const FrameRate FRAME_RATE_TREE = FrameRate(Fps(0.f), FrameRateCompatibility::NoVote); | 
|  | 116 | const FrameRate FRAME_RATE_NO_VOTE = FrameRate(Fps(0.f), FrameRateCompatibility::Default); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 117 |  | 
|  | 118 | SetFrameRateTest(); | 
|  | 119 |  | 
|  | 120 | void setupScheduler(); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | void addChild(sp<Layer> layer, sp<Layer> child); | 
|  | 123 | void removeChild(sp<Layer> layer, sp<Layer> child); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 124 | void commitTransaction(); | 
|  | 125 |  | 
|  | 126 | TestableSurfaceFlinger mFlinger; | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 127 | mock::MessageQueue* mMessageQueue = new mock::MessageQueue(); | 
|  | 128 |  | 
|  | 129 | std::vector<sp<Layer>> mLayers; | 
|  | 130 | }; | 
|  | 131 |  | 
|  | 132 | SetFrameRateTest::SetFrameRateTest() { | 
|  | 133 | const ::testing::TestInfo* const test_info = | 
|  | 134 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 135 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 136 |  | 
|  | 137 | mFlinger.mutableUseFrameRateApi() = true; | 
|  | 138 |  | 
|  | 139 | setupScheduler(); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 140 |  | 
| Dominik Laskowski | 1394860 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 141 | mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 142 | mFlinger.mutableEventQueue().reset(mMessageQueue); | 
|  | 143 | } | 
| Dominik Laskowski | 1394860 | 2021-03-08 20:48:28 -0800 | [diff] [blame] | 144 |  | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 145 | void SetFrameRateTest::addChild(sp<Layer> layer, sp<Layer> child) { | 
|  | 146 | layer.get()->addChild(child.get()); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | void SetFrameRateTest::removeChild(sp<Layer> layer, sp<Layer> child) { | 
|  | 150 | layer.get()->removeChild(child.get()); | 
|  | 151 | } | 
|  | 152 |  | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 153 | void SetFrameRateTest::commitTransaction() { | 
|  | 154 | for (auto layer : mLayers) { | 
| Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 155 | auto c = layer->getDrawingState(); | 
| Robert Carr | 0758e5d | 2021-03-11 22:15:04 -0800 | [diff] [blame] | 156 | layer->commitTransaction(c); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 157 | } | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | void SetFrameRateTest::setupScheduler() { | 
|  | 161 | auto eventThread = std::make_unique<mock::EventThread>(); | 
|  | 162 | auto sfEventThread = std::make_unique<mock::EventThread>(); | 
|  | 163 |  | 
|  | 164 | EXPECT_CALL(*eventThread, registerDisplayEventConnection(_)); | 
|  | 165 | EXPECT_CALL(*eventThread, createEventConnection(_, _)) | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 166 | .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0, | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 167 | ResyncCallback()))); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_)); | 
|  | 170 | EXPECT_CALL(*sfEventThread, createEventConnection(_, _)) | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 171 | .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0, | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 172 | ResyncCallback()))); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 173 |  | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 174 | auto vsyncController = std::make_unique<mock::VsyncController>(); | 
|  | 175 | auto vsyncTracker = std::make_unique<mock::VSyncTracker>(); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 176 |  | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 177 | EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0)); | 
|  | 178 | EXPECT_CALL(*vsyncTracker, currentPeriod()) | 
| Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 179 | .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)); | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 180 | EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0)); | 
|  | 181 | mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker), | 
| Ady Abraham | 44e9f3b | 2021-02-16 15:22:58 -0800 | [diff] [blame] | 182 | std::move(eventThread), std::move(sfEventThread), /*callback*/ nullptr, | 
|  | 183 | /*hasMultipleModes*/ true); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 186 | namespace { | 
|  | 187 | /* ------------------------------------------------------------------------ | 
|  | 188 | * Test cases | 
|  | 189 | */ | 
|  | 190 | TEST_P(SetFrameRateTest, SetAndGet) { | 
|  | 191 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 192 |  | 
|  | 193 | const auto& layerFactory = GetParam(); | 
|  | 194 |  | 
|  | 195 | auto layer = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 196 | layer->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 197 | commitTransaction(); | 
|  | 198 | EXPECT_EQ(FRAME_RATE_VOTE1, layer->getFrameRateForLayerTree()); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | TEST_P(SetFrameRateTest, SetAndGetParent) { | 
|  | 202 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 203 |  | 
|  | 204 | const auto& layerFactory = GetParam(); | 
|  | 205 |  | 
|  | 206 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 207 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 208 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 209 |  | 
|  | 210 | addChild(parent, child1); | 
|  | 211 | addChild(child1, child2); | 
|  | 212 |  | 
|  | 213 | child2->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 214 | commitTransaction(); | 
|  | 215 | EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree()); | 
|  | 216 | EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree()); | 
|  | 217 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
|  | 218 |  | 
|  | 219 | child2->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 220 | commitTransaction(); | 
|  | 221 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 222 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 223 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | TEST_P(SetFrameRateTest, SetAndGetParentAllVote) { | 
|  | 227 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 228 |  | 
|  | 229 | const auto& layerFactory = GetParam(); | 
|  | 230 |  | 
|  | 231 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 232 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 233 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 234 |  | 
|  | 235 | addChild(parent, child1); | 
|  | 236 | addChild(child1, child2); | 
|  | 237 |  | 
|  | 238 | child2->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 239 | child1->setFrameRate(FRAME_RATE_VOTE2); | 
|  | 240 | parent->setFrameRate(FRAME_RATE_VOTE3); | 
|  | 241 | commitTransaction(); | 
|  | 242 | EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree()); | 
|  | 243 | EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree()); | 
|  | 244 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
|  | 245 |  | 
|  | 246 | child2->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 247 | commitTransaction(); | 
|  | 248 | EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree()); | 
|  | 249 | EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 250 | EXPECT_EQ(FRAME_RATE_VOTE2, child2->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 251 |  | 
|  | 252 | child1->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 253 | commitTransaction(); | 
|  | 254 | EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 255 | EXPECT_EQ(FRAME_RATE_VOTE3, child1->getFrameRateForLayerTree()); | 
|  | 256 | EXPECT_EQ(FRAME_RATE_VOTE3, child2->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 257 |  | 
|  | 258 | parent->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 259 | commitTransaction(); | 
|  | 260 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 261 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 262 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | TEST_P(SetFrameRateTest, SetAndGetChild) { | 
|  | 266 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 267 |  | 
|  | 268 | const auto& layerFactory = GetParam(); | 
|  | 269 |  | 
|  | 270 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 271 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 272 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 273 |  | 
|  | 274 | addChild(parent, child1); | 
|  | 275 | addChild(child1, child2); | 
|  | 276 |  | 
|  | 277 | parent->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 278 | commitTransaction(); | 
|  | 279 | EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 280 | EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree()); | 
|  | 281 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 282 |  | 
|  | 283 | parent->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 284 | commitTransaction(); | 
|  | 285 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 286 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 287 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | TEST_P(SetFrameRateTest, SetAndGetChildAllVote) { | 
|  | 291 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 292 |  | 
|  | 293 | const auto& layerFactory = GetParam(); | 
|  | 294 |  | 
|  | 295 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 296 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 297 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 298 |  | 
|  | 299 | addChild(parent, child1); | 
|  | 300 | addChild(child1, child2); | 
|  | 301 |  | 
|  | 302 | child2->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 303 | child1->setFrameRate(FRAME_RATE_VOTE2); | 
|  | 304 | parent->setFrameRate(FRAME_RATE_VOTE3); | 
|  | 305 | commitTransaction(); | 
|  | 306 | EXPECT_EQ(FRAME_RATE_VOTE3, parent->getFrameRateForLayerTree()); | 
|  | 307 | EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree()); | 
|  | 308 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
|  | 309 |  | 
|  | 310 | parent->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 311 | commitTransaction(); | 
|  | 312 | EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree()); | 
|  | 313 | EXPECT_EQ(FRAME_RATE_VOTE2, child1->getFrameRateForLayerTree()); | 
|  | 314 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
|  | 315 |  | 
|  | 316 | child1->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 317 | commitTransaction(); | 
|  | 318 | EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree()); | 
|  | 319 | EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree()); | 
|  | 320 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
|  | 321 |  | 
|  | 322 | child2->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 323 | commitTransaction(); | 
|  | 324 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 325 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 326 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | TEST_P(SetFrameRateTest, SetAndGetChildAddAfterVote) { | 
|  | 330 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 331 |  | 
|  | 332 | const auto& layerFactory = GetParam(); | 
|  | 333 |  | 
|  | 334 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 335 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 336 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 337 |  | 
|  | 338 | addChild(parent, child1); | 
|  | 339 |  | 
|  | 340 | parent->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 341 | commitTransaction(); | 
|  | 342 | EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 343 | EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 344 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 345 |  | 
|  | 346 | addChild(child1, child2); | 
|  | 347 | commitTransaction(); | 
|  | 348 | EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 349 | EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree()); | 
|  | 350 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 351 |  | 
|  | 352 | parent->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 353 | commitTransaction(); | 
|  | 354 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 355 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 356 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | TEST_P(SetFrameRateTest, SetAndGetChildRemoveAfterVote) { | 
|  | 360 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 361 |  | 
|  | 362 | const auto& layerFactory = GetParam(); | 
|  | 363 |  | 
|  | 364 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 365 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 366 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 367 |  | 
|  | 368 | addChild(parent, child1); | 
|  | 369 | addChild(child1, child2); | 
|  | 370 |  | 
|  | 371 | parent->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 372 | commitTransaction(); | 
|  | 373 | EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 374 | EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree()); | 
|  | 375 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 376 |  | 
|  | 377 | removeChild(child1, child2); | 
|  | 378 | commitTransaction(); | 
|  | 379 | EXPECT_EQ(FRAME_RATE_VOTE1, parent->getFrameRateForLayerTree()); | 
| Ady Abraham | f467f89 | 2020-07-31 16:01:53 -0700 | [diff] [blame] | 380 | EXPECT_EQ(FRAME_RATE_VOTE1, child1->getFrameRateForLayerTree()); | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 381 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 382 |  | 
|  | 383 | parent->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 384 | commitTransaction(); | 
|  | 385 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 386 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 387 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | TEST_P(SetFrameRateTest, SetAndGetParentNotInTree) { | 
|  | 391 | EXPECT_CALL(*mMessageQueue, invalidate()).Times(1); | 
|  | 392 |  | 
|  | 393 | const auto& layerFactory = GetParam(); | 
|  | 394 |  | 
|  | 395 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 396 | auto child1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 397 | auto child2 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 398 | auto child2_1 = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 399 |  | 
|  | 400 | addChild(parent, child1); | 
|  | 401 | addChild(child1, child2); | 
|  | 402 | addChild(child1, child2_1); | 
|  | 403 |  | 
|  | 404 | child2->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 405 | commitTransaction(); | 
|  | 406 | EXPECT_EQ(FRAME_RATE_TREE, parent->getFrameRateForLayerTree()); | 
|  | 407 | EXPECT_EQ(FRAME_RATE_TREE, child1->getFrameRateForLayerTree()); | 
|  | 408 | EXPECT_EQ(FRAME_RATE_VOTE1, child2->getFrameRateForLayerTree()); | 
|  | 409 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2_1->getFrameRateForLayerTree()); | 
|  | 410 |  | 
|  | 411 | child2->setFrameRate(FRAME_RATE_NO_VOTE); | 
|  | 412 | commitTransaction(); | 
|  | 413 | EXPECT_EQ(FRAME_RATE_NO_VOTE, parent->getFrameRateForLayerTree()); | 
|  | 414 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child1->getFrameRateForLayerTree()); | 
|  | 415 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2->getFrameRateForLayerTree()); | 
|  | 416 | EXPECT_EQ(FRAME_RATE_NO_VOTE, child2_1->getFrameRateForLayerTree()); | 
|  | 417 | } | 
|  | 418 |  | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 419 | INSTANTIATE_TEST_SUITE_P(PerLayerType, SetFrameRateTest, | 
|  | 420 | testing::Values(std::make_shared<BufferQueueLayerFactory>(), | 
|  | 421 | std::make_shared<BufferStateLayerFactory>(), | 
|  | 422 | std::make_shared<EffectLayerFactory>()), | 
|  | 423 | PrintToStringParamName); | 
|  | 424 |  | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 425 | TEST_F(SetFrameRateTest, ValidateFrameRate) { | 
| Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 426 | EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, | 
|  | 427 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 428 | EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, | 
|  | 429 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 430 | EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, | 
|  | 431 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ALWAYS, "")); | 
|  | 432 | EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_FIXED_SOURCE, | 
|  | 433 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 434 | EXPECT_TRUE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT, | 
|  | 435 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "", | 
|  | 436 | /*privileged=*/true)); | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 437 |  | 
| Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 438 | EXPECT_FALSE(ValidateFrameRate(-1, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, | 
|  | 439 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 440 | EXPECT_FALSE(ValidateFrameRate(1.0f / 0.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, | 
|  | 441 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 442 | EXPECT_FALSE(ValidateFrameRate(0.0f / 0.0f, ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_DEFAULT, | 
|  | 443 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 444 |  | 
| Marin Shalamanov | c598677 | 2021-03-16 16:09:49 +0100 | [diff] [blame] | 445 | EXPECT_FALSE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT, | 
|  | 446 | ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 447 |  | 
|  | 448 | // Invalid compatibility | 
|  | 449 | EXPECT_FALSE( | 
|  | 450 | ValidateFrameRate(60.0f, -1, ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 451 | EXPECT_FALSE(ValidateFrameRate(60.0f, 2, ANATIVEWINDOW_CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, "")); | 
|  | 452 |  | 
|  | 453 | // Invalid change frame rate strategy | 
|  | 454 | EXPECT_FALSE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT, -1, "")); | 
|  | 455 | EXPECT_FALSE(ValidateFrameRate(60.0f, ANATIVEWINDOW_FRAME_RATE_EXACT, 2, "")); | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
| Ady Abraham | 44e9f3b | 2021-02-16 15:22:58 -0800 | [diff] [blame] | 458 | TEST_P(SetFrameRateTest, SetOnParentActivatesTree) { | 
|  | 459 | const auto& layerFactory = GetParam(); | 
|  | 460 |  | 
|  | 461 | auto parent = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 462 | if (!parent->isVisible()) { | 
|  | 463 | // This is a hack as all the test layers except EffectLayer are not visible, | 
|  | 464 | // but since the logic is unified in Layer, it should be fine. | 
|  | 465 | return; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | auto child = mLayers.emplace_back(layerFactory->createLayer(mFlinger)); | 
|  | 469 | addChild(parent, child); | 
|  | 470 |  | 
|  | 471 | parent->setFrameRate(FRAME_RATE_VOTE1); | 
|  | 472 | commitTransaction(); | 
|  | 473 |  | 
| Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 474 | mFlinger.mutableScheduler() | 
|  | 475 | .mutableLayerHistory() | 
|  | 476 | ->record(parent.get(), 0, 0, LayerHistory::LayerUpdateType::Buffer); | 
|  | 477 | mFlinger.mutableScheduler() | 
|  | 478 | .mutableLayerHistory() | 
|  | 479 | ->record(child.get(), 0, 0, LayerHistory::LayerUpdateType::Buffer); | 
|  | 480 |  | 
| Ady Abraham | 44e9f3b | 2021-02-16 15:22:58 -0800 | [diff] [blame] | 481 | const auto layerHistorySummary = | 
|  | 482 | mFlinger.mutableScheduler().mutableLayerHistory()->summarize(0); | 
|  | 483 | ASSERT_EQ(2u, layerHistorySummary.size()); | 
|  | 484 | EXPECT_TRUE(FRAME_RATE_VOTE1.rate.equalsWithMargin(layerHistorySummary[0].desiredRefreshRate)); | 
|  | 485 | EXPECT_TRUE(FRAME_RATE_VOTE1.rate.equalsWithMargin(layerHistorySummary[1].desiredRefreshRate)); | 
|  | 486 | } | 
|  | 487 |  | 
| Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 488 | } // namespace | 
|  | 489 | } // namespace android |