| 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 | layer->pushPendingState(); | 
|  | 122 | auto c = layer->getCurrentState(); | 
|  | 123 | if (layer->applyPendingStates(&c)) { | 
|  | 124 | layer->commitTransaction(c); | 
|  | 125 | } | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
|  | 128 | void RefreshRateSelectionTest::setupScheduler() { | 
|  | 129 | auto eventThread = std::make_unique<mock::EventThread>(); | 
|  | 130 | auto sfEventThread = std::make_unique<mock::EventThread>(); | 
|  | 131 |  | 
|  | 132 | EXPECT_CALL(*eventThread, registerDisplayEventConnection(_)); | 
|  | 133 | EXPECT_CALL(*eventThread, createEventConnection(_, _)) | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 134 | .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0, | 
|  | 135 | ResyncCallback(), | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 136 | ISurfaceComposer::eConfigChangedSuppress))); | 
|  | 137 |  | 
|  | 138 | EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_)); | 
|  | 139 | EXPECT_CALL(*sfEventThread, createEventConnection(_, _)) | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 140 | .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0, | 
|  | 141 | ResyncCallback(), | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 142 | ISurfaceComposer::eConfigChangedSuppress))); | 
|  | 143 |  | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 144 | auto vsyncController = std::make_unique<mock::VsyncController>(); | 
|  | 145 | auto vsyncTracker = std::make_unique<mock::VSyncTracker>(); | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 146 |  | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 147 | EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0)); | 
|  | 148 | EXPECT_CALL(*vsyncTracker, currentPeriod()) | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 149 | .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_REFRESH_RATE)); | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 150 | EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0)); | 
|  | 151 | mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker), | 
|  | 152 | std::move(eventThread), std::move(sfEventThread)); | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 155 | void RefreshRateSelectionTest::setupComposer(uint32_t virtualDisplayCount) { | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 156 | mComposer = new Hwc2::mock::Composer(); | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 157 | EXPECT_CALL(*mComposer, getMaxVirtualDisplayCount()).WillOnce(Return(virtualDisplayCount)); | 
|  | 158 | mFlinger.setupComposer(std::unique_ptr<Hwc2::Composer>(mComposer)); | 
|  | 159 |  | 
|  | 160 | Mock::VerifyAndClear(mComposer); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | namespace { | 
|  | 164 | /* ------------------------------------------------------------------------ | 
|  | 165 | * Test cases | 
|  | 166 | */ | 
|  | 167 | TEST_F(RefreshRateSelectionTest, testPriorityOnBufferQueueLayers) { | 
|  | 168 | mParent = createBufferQueueLayer(); | 
|  | 169 | mChild = createBufferQueueLayer(); | 
|  | 170 | setParent(mChild.get(), mParent.get()); | 
|  | 171 | mGrandChild = createBufferQueueLayer(); | 
|  | 172 | setParent(mGrandChild.get(), mChild.get()); | 
|  | 173 |  | 
|  | 174 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 175 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); | 
|  | 176 | ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 177 |  | 
|  | 178 | // Child has its own priority. | 
|  | 179 | mGrandChild->setFrameRateSelectionPriority(1); | 
|  | 180 | commitTransaction(mGrandChild.get()); | 
|  | 181 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 182 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); | 
|  | 183 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 184 |  | 
|  | 185 | // Child inherits from his parent. | 
|  | 186 | mChild->setFrameRateSelectionPriority(1); | 
|  | 187 | commitTransaction(mChild.get()); | 
|  | 188 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 189 | commitTransaction(mGrandChild.get()); | 
|  | 190 |  | 
|  | 191 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 192 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); | 
|  | 193 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 194 |  | 
|  | 195 | // Grandchild inherits from his grand parent. | 
|  | 196 | mParent->setFrameRateSelectionPriority(1); | 
|  | 197 | commitTransaction(mParent.get()); | 
|  | 198 | mChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 199 | commitTransaction(mChild.get()); | 
|  | 200 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 201 | commitTransaction(mGrandChild.get()); | 
|  | 202 | ASSERT_EQ(1, mParent->getFrameRateSelectionPriority()); | 
|  | 203 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); | 
|  | 204 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) { | 
|  | 208 | mParent = createBufferStateLayer(); | 
|  | 209 | mChild = createBufferStateLayer(); | 
|  | 210 | setParent(mChild.get(), mParent.get()); | 
|  | 211 | mGrandChild = createBufferStateLayer(); | 
|  | 212 | setParent(mGrandChild.get(), mChild.get()); | 
|  | 213 |  | 
|  | 214 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 215 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); | 
|  | 216 | ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 217 |  | 
|  | 218 | // Child has its own priority. | 
|  | 219 | mGrandChild->setFrameRateSelectionPriority(1); | 
|  | 220 | commitTransaction(mGrandChild.get()); | 
|  | 221 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 222 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); | 
|  | 223 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 224 |  | 
|  | 225 | // Child inherits from his parent. | 
|  | 226 | mChild->setFrameRateSelectionPriority(1); | 
|  | 227 | commitTransaction(mChild.get()); | 
|  | 228 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 229 | commitTransaction(mGrandChild.get()); | 
|  | 230 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 231 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); | 
|  | 232 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 233 |  | 
|  | 234 | // Grandchild inherits from his grand parent. | 
|  | 235 | mParent->setFrameRateSelectionPriority(1); | 
|  | 236 | commitTransaction(mParent.get()); | 
|  | 237 | mChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 238 | commitTransaction(mChild.get()); | 
|  | 239 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 240 | commitTransaction(mGrandChild.get()); | 
|  | 241 | ASSERT_EQ(1, mParent->getFrameRateSelectionPriority()); | 
|  | 242 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); | 
|  | 243 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 244 | } | 
|  | 245 |  | 
| Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 246 | TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) { | 
|  | 247 | mParent = createEffectLayer(); | 
|  | 248 | mChild = createEffectLayer(); | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 249 | setParent(mChild.get(), mParent.get()); | 
| Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 250 | mGrandChild = createEffectLayer(); | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 251 | setParent(mGrandChild.get(), mChild.get()); | 
|  | 252 |  | 
|  | 253 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 254 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); | 
|  | 255 | ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 256 |  | 
|  | 257 | // Child has its own priority. | 
|  | 258 | mGrandChild->setFrameRateSelectionPriority(1); | 
|  | 259 | commitTransaction(mGrandChild.get()); | 
|  | 260 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 261 | ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority()); | 
|  | 262 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 263 |  | 
|  | 264 | // Child inherits from his parent. | 
|  | 265 | mChild->setFrameRateSelectionPriority(1); | 
|  | 266 | commitTransaction(mChild.get()); | 
|  | 267 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 268 | commitTransaction(mGrandChild.get()); | 
|  | 269 | ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority()); | 
|  | 270 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); | 
|  | 271 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 272 |  | 
|  | 273 | // Grandchild inherits from his grand parent. | 
|  | 274 | mParent->setFrameRateSelectionPriority(1); | 
|  | 275 | commitTransaction(mParent.get()); | 
|  | 276 | mChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 277 | commitTransaction(mChild.get()); | 
|  | 278 | mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET); | 
|  | 279 | commitTransaction(mGrandChild.get()); | 
|  | 280 | ASSERT_EQ(1, mParent->getFrameRateSelectionPriority()); | 
|  | 281 | ASSERT_EQ(1, mChild->getFrameRateSelectionPriority()); | 
|  | 282 | ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority()); | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | } // namespace | 
| Ady Abraham | 2b55c3b | 2020-01-16 16:38:51 -0800 | [diff] [blame] | 286 | } // namespace android |