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