Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [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 | #include "BufferQueueLayer.h" |
| 25 | #include "BufferStateLayer.h" |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 26 | #include "EffectLayer.h" |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 27 | #include "Layer.h" |
| 28 | #include "TestableSurfaceFlinger.h" |
| 29 | #include "mock/DisplayHardware/MockComposer.h" |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 30 | #include "mock/MockEventThread.h" |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 31 | #include "mock/MockVsyncController.h" |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | using testing::_; |
| 36 | using testing::DoAll; |
| 37 | using testing::Mock; |
| 38 | using testing::Return; |
| 39 | using testing::SetArgPointee; |
| 40 | |
| 41 | using android::Hwc2::IComposer; |
| 42 | using android::Hwc2::IComposerClient; |
| 43 | |
| 44 | using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector; |
| 45 | |
| 46 | /** |
| 47 | * This class covers all the test that are related to refresh rate selection. |
| 48 | */ |
| 49 | class RefreshRateSelectionTest : public testing::Test { |
| 50 | public: |
| 51 | RefreshRateSelectionTest(); |
| 52 | ~RefreshRateSelectionTest() override; |
| 53 | |
| 54 | protected: |
| 55 | static constexpr int DEFAULT_DISPLAY_WIDTH = 1920; |
| 56 | static constexpr int DEFAULT_DISPLAY_HEIGHT = 1024; |
| 57 | static constexpr uint32_t WIDTH = 100; |
| 58 | static constexpr uint32_t HEIGHT = 100; |
| 59 | static constexpr uint32_t LAYER_FLAGS = 0; |
| 60 | static constexpr int32_t PRIORITY_UNSET = -1; |
| 61 | |
| 62 | void setupScheduler(); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 63 | void setupComposer(uint32_t virtualDisplayCount); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 64 | sp<BufferQueueLayer> createBufferQueueLayer(); |
| 65 | sp<BufferStateLayer> createBufferStateLayer(); |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 66 | sp<EffectLayer> createEffectLayer(); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 67 | |
| 68 | void setParent(Layer* child, Layer* parent); |
| 69 | void commitTransaction(Layer* layer); |
| 70 | |
| 71 | TestableSurfaceFlinger mFlinger; |
| 72 | Hwc2::mock::Composer* mComposer = nullptr; |
| 73 | |
| 74 | sp<Client> mClient; |
| 75 | sp<Layer> mParent; |
| 76 | sp<Layer> mChild; |
| 77 | sp<Layer> mGrandChild; |
| 78 | }; |
| 79 | |
| 80 | RefreshRateSelectionTest::RefreshRateSelectionTest() { |
| 81 | const ::testing::TestInfo* const test_info = |
| 82 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 83 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 84 | |
| 85 | setupScheduler(); |
| 86 | setupComposer(0); |
| 87 | } |
| 88 | |
| 89 | RefreshRateSelectionTest::~RefreshRateSelectionTest() { |
| 90 | const ::testing::TestInfo* const test_info = |
| 91 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 92 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 93 | } |
| 94 | |
| 95 | sp<BufferQueueLayer> RefreshRateSelectionTest::createBufferQueueLayer() { |
| 96 | sp<Client> client; |
| 97 | LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT, |
| 98 | LAYER_FLAGS, LayerMetadata()); |
| 99 | return new BufferQueueLayer(args); |
| 100 | } |
| 101 | |
| 102 | sp<BufferStateLayer> RefreshRateSelectionTest::createBufferStateLayer() { |
| 103 | sp<Client> client; |
| 104 | LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT, |
| 105 | LAYER_FLAGS, LayerMetadata()); |
| 106 | return new BufferStateLayer(args); |
| 107 | } |
| 108 | |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 109 | sp<EffectLayer> RefreshRateSelectionTest::createEffectLayer() { |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 110 | sp<Client> client; |
| 111 | LayerCreationArgs args(mFlinger.flinger(), client, "color-layer", WIDTH, HEIGHT, LAYER_FLAGS, |
| 112 | LayerMetadata()); |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 113 | return new EffectLayer(args); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void RefreshRateSelectionTest::setParent(Layer* child, Layer* parent) { |
| 117 | child->setParent(parent); |
| 118 | } |
| 119 | |
| 120 | void RefreshRateSelectionTest::commitTransaction(Layer* layer) { |
Ady Abraham | 22c7b5c | 2020-09-22 19:33:40 -0700 | [diff] [blame] | 121 | auto c = layer->getCurrentState(); |
Robert Carr | 0758e5d | 2021-03-11 22:15:04 -0800 | [diff] [blame^] | 122 | layer->commitTransaction(c); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void RefreshRateSelectionTest::setupScheduler() { |
| 126 | auto eventThread = std::make_unique<mock::EventThread>(); |
| 127 | auto sfEventThread = std::make_unique<mock::EventThread>(); |
| 128 | |
| 129 | EXPECT_CALL(*eventThread, registerDisplayEventConnection(_)); |
| 130 | EXPECT_CALL(*eventThread, createEventConnection(_, _)) |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 131 | .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 132 | ResyncCallback()))); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 133 | |
| 134 | EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_)); |
| 135 | EXPECT_CALL(*sfEventThread, createEventConnection(_, _)) |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 136 | .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 137 | ResyncCallback()))); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 138 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 139 | auto vsyncController = std::make_unique<mock::VsyncController>(); |
| 140 | auto vsyncTracker = std::make_unique<mock::VSyncTracker>(); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 141 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 142 | EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0)); |
| 143 | EXPECT_CALL(*vsyncTracker, currentPeriod()) |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 144 | .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD)); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 145 | EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0)); |
| 146 | mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker), |
| 147 | std::move(eventThread), std::move(sfEventThread)); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 150 | void RefreshRateSelectionTest::setupComposer(uint32_t virtualDisplayCount) { |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 151 | mComposer = new Hwc2::mock::Composer(); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 152 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); |
| 153 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); |
| 154 | |
| 155 | Mock::VerifyAndClear(mComposer); |
| 156 | } |
| 157 | |
| 158 | namespace { |
| 159 | /* ------------------------------------------------------------------------ |
| 160 | * Test cases |
| 161 | */ |
| 162 | TEST_F(RefreshRateSelectionTest, testPriorityOnBufferQueueLayers) { |
| 163 | mParent = createBufferQueueLayer(); |
| 164 | mChild = createBufferQueueLayer(); |
| 165 | setParent(mChild.get(), mParent.get()); |
| 166 | mGrandChild = createBufferQueueLayer(); |
| 167 | setParent(mGrandChild.get(), mChild.get()); |
| 168 | |
| 169 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 170 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); |
| 171 | ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority()); |
| 172 | |
| 173 | // Child has its own priority. |
| 174 | mGrandChild->setFrameRateSelectionPriority(1); |
| 175 | commitTransaction(mGrandChild.get()); |
| 176 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 177 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); |
| 178 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 179 | |
| 180 | // Child inherits from his parent. |
| 181 | mChild->setFrameRateSelectionPriority(1); |
| 182 | commitTransaction(mChild.get()); |
| 183 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 184 | commitTransaction(mGrandChild.get()); |
| 185 | |
| 186 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 187 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); |
| 188 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 189 | |
| 190 | // Grandchild inherits from his grand parent. |
| 191 | mParent->setFrameRateSelectionPriority(1); |
| 192 | commitTransaction(mParent.get()); |
| 193 | mChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 194 | commitTransaction(mChild.get()); |
| 195 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 196 | commitTransaction(mGrandChild.get()); |
| 197 | ASSERT_EQ(1, mParent->getFrameRateSelectionPriority()); |
| 198 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); |
| 199 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 200 | } |
| 201 | |
| 202 | TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) { |
| 203 | mParent = createBufferStateLayer(); |
| 204 | mChild = createBufferStateLayer(); |
| 205 | setParent(mChild.get(), mParent.get()); |
| 206 | mGrandChild = createBufferStateLayer(); |
| 207 | setParent(mGrandChild.get(), mChild.get()); |
| 208 | |
| 209 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 210 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); |
| 211 | ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority()); |
| 212 | |
| 213 | // Child has its own priority. |
| 214 | mGrandChild->setFrameRateSelectionPriority(1); |
| 215 | commitTransaction(mGrandChild.get()); |
| 216 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 217 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); |
| 218 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 219 | |
| 220 | // Child inherits from his parent. |
| 221 | mChild->setFrameRateSelectionPriority(1); |
| 222 | commitTransaction(mChild.get()); |
| 223 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 224 | commitTransaction(mGrandChild.get()); |
| 225 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 226 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); |
| 227 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 228 | |
| 229 | // Grandchild inherits from his grand parent. |
| 230 | mParent->setFrameRateSelectionPriority(1); |
| 231 | commitTransaction(mParent.get()); |
| 232 | mChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 233 | commitTransaction(mChild.get()); |
| 234 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 235 | commitTransaction(mGrandChild.get()); |
| 236 | ASSERT_EQ(1, mParent->getFrameRateSelectionPriority()); |
| 237 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); |
| 238 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 239 | } |
| 240 | |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 241 | TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) { |
| 242 | mParent = createEffectLayer(); |
| 243 | mChild = createEffectLayer(); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 244 | setParent(mChild.get(), mParent.get()); |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 245 | mGrandChild = createEffectLayer(); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 246 | setParent(mGrandChild.get(), mChild.get()); |
| 247 | |
| 248 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 249 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); |
| 250 | ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority()); |
| 251 | |
| 252 | // Child has its own priority. |
| 253 | mGrandChild->setFrameRateSelectionPriority(1); |
| 254 | commitTransaction(mGrandChild.get()); |
| 255 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 256 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); |
| 257 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 258 | |
| 259 | // Child inherits from his parent. |
| 260 | mChild->setFrameRateSelectionPriority(1); |
| 261 | commitTransaction(mChild.get()); |
| 262 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 263 | commitTransaction(mGrandChild.get()); |
| 264 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); |
| 265 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); |
| 266 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 267 | |
| 268 | // Grandchild inherits from his grand parent. |
| 269 | mParent->setFrameRateSelectionPriority(1); |
| 270 | commitTransaction(mParent.get()); |
| 271 | mChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 272 | commitTransaction(mChild.get()); |
| 273 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); |
| 274 | commitTransaction(mGrandChild.get()); |
| 275 | ASSERT_EQ(1, mParent->getFrameRateSelectionPriority()); |
| 276 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); |
| 277 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); |
| 278 | } |
| 279 | |
| 280 | } // namespace |
Ady Abraham | 2b55c3b | 2020-01-16 16:38:51 -0800 | [diff] [blame] | 281 | } // namespace android |