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