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