Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "SchedulerUnittests" |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 26 | #include <log/log.h> |
| 27 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 28 | #include <mutex> |
| 29 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 30 | #include "Scheduler/EventControlThread.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 31 | #include "Scheduler/EventThread.h" |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 32 | #include "Scheduler/RefreshRateConfigs.h" |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 33 | #include "TestableScheduler.h" |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 34 | #include "TestableSurfaceFlinger.h" |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 35 | #include "mock/DisplayHardware/MockDisplay.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 36 | #include "mock/MockEventThread.h" |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 37 | #include "mock/MockLayer.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 38 | |
| 39 | using testing::_; |
| 40 | using testing::Return; |
| 41 | |
| 42 | namespace android { |
| 43 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 44 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999; |
| 45 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 46 | class SchedulerTest : public testing::Test { |
| 47 | protected: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 48 | class MockEventThreadConnection : public android::EventThreadConnection { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 49 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 50 | explicit MockEventThreadConnection(EventThread* eventThread) |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 51 | : EventThreadConnection(eventThread, ResyncCallback(), |
| 52 | ISurfaceComposer::eConfigChangedSuppress) {} |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 53 | ~MockEventThreadConnection() = default; |
| 54 | |
| 55 | MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel)); |
| 56 | MOCK_METHOD1(setVsyncRate, status_t(uint32_t count)); |
| 57 | MOCK_METHOD0(requestNextVsync, void()); |
| 58 | }; |
| 59 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 60 | SchedulerTest(); |
| 61 | ~SchedulerTest() override; |
| 62 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 63 | Hwc2::mock::Display mDisplay; |
| 64 | const scheduler::RefreshRateConfigs mConfigs{{HWC2::Display::Config::Builder(mDisplay, 0) |
| 65 | .setVsyncPeriod(16'666'667) |
| 66 | .setConfigGroup(0) |
| 67 | .build()}, |
| 68 | HwcConfigIndexType(0)}; |
| 69 | |
| 70 | TestableScheduler mScheduler{mConfigs, false}; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 71 | |
| 72 | Scheduler::ConnectionHandle mConnectionHandle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 73 | mock::EventThread* mEventThread; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 74 | sp<MockEventThreadConnection> mEventThreadConnection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | SchedulerTest::SchedulerTest() { |
| 78 | const ::testing::TestInfo* const test_info = |
| 79 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 80 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 81 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 82 | auto eventThread = std::make_unique<mock::EventThread>(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 83 | mEventThread = eventThread.get(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 84 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 85 | |
| 86 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 87 | |
| 88 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 89 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 90 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 91 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 92 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 93 | mConnectionHandle = mScheduler.createConnection(std::move(eventThread)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 94 | EXPECT_TRUE(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | SchedulerTest::~SchedulerTest() { |
| 98 | const ::testing::TestInfo* const test_info = |
| 99 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 100 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 101 | } |
| 102 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 103 | /* ------------------------------------------------------------------------ |
| 104 | * Test cases |
| 105 | */ |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 106 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 107 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 108 | Scheduler::ConnectionHandle handle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 109 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 110 | const sp<IDisplayEventConnection> connection = |
| 111 | mScheduler.createDisplayEventConnection(handle, |
| 112 | ISurfaceComposer::eConfigChangedSuppress); |
| 113 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 114 | EXPECT_FALSE(connection); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 115 | EXPECT_FALSE(mScheduler.getEventConnection(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 116 | |
| 117 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 118 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 119 | mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 120 | |
| 121 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 122 | mScheduler.onScreenAcquired(handle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 123 | |
| 124 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 125 | mScheduler.onScreenReleased(handle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 126 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 127 | std::string output; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 128 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 129 | mScheduler.dump(handle, output); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 130 | EXPECT_TRUE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 131 | |
| 132 | EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 133 | mScheduler.setPhaseOffset(handle, 10); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(SchedulerTest, validConnectionHandle) { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 137 | const sp<IDisplayEventConnection> connection = |
| 138 | mScheduler.createDisplayEventConnection(mConnectionHandle, |
| 139 | ISurfaceComposer::eConfigChangedSuppress); |
| 140 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 141 | ASSERT_EQ(mEventThreadConnection, connection); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 142 | EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 143 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 144 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 145 | mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 146 | |
| 147 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 148 | mScheduler.onScreenAcquired(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 149 | |
| 150 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 151 | mScheduler.onScreenReleased(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 152 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 153 | std::string output("dump"); |
| 154 | EXPECT_CALL(*mEventThread, dump(output)).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 155 | mScheduler.dump(mConnectionHandle, output); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 156 | EXPECT_FALSE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 157 | |
| 158 | EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 159 | mScheduler.setPhaseOffset(mConnectionHandle, 10); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 160 | |
| 161 | static constexpr size_t kEventConnections = 5; |
| 162 | ON_CALL(*mEventThread, getEventThreadConnectionCount()) |
| 163 | .WillByDefault(Return(kEventConnections)); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 164 | EXPECT_EQ(kEventConnections, mScheduler.getEventThreadConnectionCount(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 165 | } |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 166 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame^] | 167 | TEST_F(SchedulerTest, noLayerHistory) { |
| 168 | // Layer history should not be created if there is a single config. |
| 169 | ASSERT_FALSE(mScheduler.hasLayerHistory()); |
| 170 | |
| 171 | TestableSurfaceFlinger flinger; |
| 172 | mock::MockLayer layer(flinger.flinger()); |
| 173 | |
| 174 | // Content detection should be no-op. |
| 175 | mScheduler.registerLayer(&layer); |
| 176 | mScheduler.recordLayerHistory(&layer, 0, LayerHistory::LayerUpdateType::Buffer); |
| 177 | |
| 178 | constexpr bool kPowerStateNormal = true; |
| 179 | mScheduler.setDisplayPowerState(kPowerStateNormal); |
| 180 | |
| 181 | constexpr uint32_t kDisplayArea = 999'999; |
| 182 | mScheduler.onPrimaryDisplayAreaChanged(kDisplayArea); |
| 183 | |
| 184 | mScheduler.chooseRefreshRateForContent(); |
| 185 | EXPECT_EQ(0, mScheduler.refreshRateChangeCount()); |
| 186 | } |
| 187 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 188 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 189 | |
| 190 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 191 | #pragma clang diagnostic pop // ignored "-Wconversion" |