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 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
| 20 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 21 | #include <mutex> |
| 22 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 23 | #include "Scheduler/EventThread.h" |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 24 | #include "Scheduler/RefreshRateConfigs.h" |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 25 | #include "TestableScheduler.h" |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 26 | #include "TestableSurfaceFlinger.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 27 | #include "mock/MockEventThread.h" |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 28 | #include "mock/MockLayer.h" |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 29 | #include "mock/MockSchedulerCallback.h" |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 30 | |
| 31 | using testing::_; |
| 32 | using testing::Return; |
| 33 | |
| 34 | namespace android { |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 35 | namespace { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 36 | |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 37 | constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID(999); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 38 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 39 | class SchedulerTest : public testing::Test { |
| 40 | protected: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 41 | class MockEventThreadConnection : public android::EventThreadConnection { |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 42 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 43 | explicit MockEventThreadConnection(EventThread* eventThread) |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 44 | : EventThreadConnection(eventThread, /*callingUid=*/0, ResyncCallback()) {} |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 45 | ~MockEventThreadConnection() = default; |
| 46 | |
| 47 | MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel)); |
| 48 | MOCK_METHOD1(setVsyncRate, status_t(uint32_t count)); |
| 49 | MOCK_METHOD0(requestNextVsync, void()); |
| 50 | }; |
| 51 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 52 | SchedulerTest(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 53 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 54 | const scheduler::RefreshRateConfigs |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 55 | mConfigs{{DisplayMode::Builder(0).setVsyncPeriod(16'666'667).setGroup(0).build()}, |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 56 | DisplayModeId(0)}; |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 57 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 58 | mock::SchedulerCallback mSchedulerCallback; |
| 59 | |
| 60 | // The scheduler should initially disable VSYNC. |
| 61 | struct ExpectDisableVsync { |
| 62 | ExpectDisableVsync(mock::SchedulerCallback& callback) { |
| 63 | EXPECT_CALL(callback, setVsyncEnabled(false)).Times(1); |
| 64 | } |
| 65 | } mExpectDisableVsync{mSchedulerCallback}; |
| 66 | |
Marin Shalamanov | 27fa3de | 2020-11-20 16:22:48 +0100 | [diff] [blame] | 67 | TestableScheduler mScheduler{mConfigs, mSchedulerCallback}; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 68 | |
| 69 | Scheduler::ConnectionHandle mConnectionHandle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 70 | mock::EventThread* mEventThread; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 71 | sp<MockEventThreadConnection> mEventThreadConnection; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | SchedulerTest::SchedulerTest() { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 75 | auto eventThread = std::make_unique<mock::EventThread>(); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 76 | mEventThread = eventThread.get(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 77 | EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0)); |
| 78 | |
| 79 | mEventThreadConnection = new MockEventThreadConnection(mEventThread); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 80 | |
| 81 | // createConnection call to scheduler makes a createEventConnection call to EventThread. Make |
| 82 | // sure that call gets executed and returns an EventThread::Connection object. |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 83 | EXPECT_CALL(*mEventThread, createEventConnection(_, _)) |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 84 | .WillRepeatedly(Return(mEventThreadConnection)); |
| 85 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 86 | mConnectionHandle = mScheduler.createConnection(std::move(eventThread)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 87 | EXPECT_TRUE(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 90 | } // namespace |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 91 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 92 | TEST_F(SchedulerTest, invalidConnectionHandle) { |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 93 | Scheduler::ConnectionHandle handle; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 94 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 95 | const sp<IDisplayEventConnection> connection = mScheduler.createDisplayEventConnection(handle); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 96 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 97 | EXPECT_FALSE(connection); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 98 | EXPECT_FALSE(mScheduler.getEventConnection(handle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 99 | |
| 100 | // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads. |
| 101 | EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 102 | mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 103 | |
| 104 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 105 | mScheduler.onScreenAcquired(handle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 106 | |
| 107 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 108 | mScheduler.onScreenReleased(handle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 109 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 110 | std::string output; |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 111 | EXPECT_CALL(*mEventThread, dump(_)).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 112 | mScheduler.dump(handle, output); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 113 | EXPECT_TRUE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 114 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 115 | EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0); |
| 116 | mScheduler.setDuration(handle, 10ns, 20ns); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_F(SchedulerTest, validConnectionHandle) { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 120 | const sp<IDisplayEventConnection> connection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 121 | mScheduler.createDisplayEventConnection(mConnectionHandle); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 122 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 123 | ASSERT_EQ(mEventThreadConnection, connection); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 124 | EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 125 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 126 | EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 127 | mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 128 | |
| 129 | EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 130 | mScheduler.onScreenAcquired(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 131 | |
| 132 | EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 133 | mScheduler.onScreenReleased(mConnectionHandle); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 134 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 135 | std::string output("dump"); |
| 136 | EXPECT_CALL(*mEventThread, dump(output)).Times(1); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 137 | mScheduler.dump(mConnectionHandle, output); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 138 | EXPECT_FALSE(output.empty()); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 139 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 140 | EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1); |
| 141 | mScheduler.setDuration(mConnectionHandle, 10ns, 20ns); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 142 | |
| 143 | static constexpr size_t kEventConnections = 5; |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 144 | EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections)); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 145 | EXPECT_EQ(kEventConnections, mScheduler.getEventThreadConnectionCount(mConnectionHandle)); |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 146 | } |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 147 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 148 | TEST_F(SchedulerTest, noLayerHistory) { |
| 149 | // Layer history should not be created if there is a single config. |
| 150 | ASSERT_FALSE(mScheduler.hasLayerHistory()); |
| 151 | |
| 152 | TestableSurfaceFlinger flinger; |
| 153 | mock::MockLayer layer(flinger.flinger()); |
| 154 | |
| 155 | // Content detection should be no-op. |
| 156 | mScheduler.registerLayer(&layer); |
| 157 | mScheduler.recordLayerHistory(&layer, 0, LayerHistory::LayerUpdateType::Buffer); |
| 158 | |
| 159 | constexpr bool kPowerStateNormal = true; |
| 160 | mScheduler.setDisplayPowerState(kPowerStateNormal); |
| 161 | |
| 162 | constexpr uint32_t kDisplayArea = 999'999; |
| 163 | mScheduler.onPrimaryDisplayAreaChanged(kDisplayArea); |
| 164 | |
Dominik Laskowski | 8b01cc0 | 2020-07-14 19:02:41 -0700 | [diff] [blame] | 165 | EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 166 | mScheduler.chooseRefreshRateForContent(); |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 169 | TEST_F(SchedulerTest, testDispatchCachedReportedMode) { |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 170 | // If the optional fields are cleared, the function should return before |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 171 | // onModeChange is called. |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 172 | mScheduler.clearOptionalFieldsInFeatures(); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 173 | EXPECT_NO_FATAL_FAILURE(mScheduler.dispatchCachedReportedMode()); |
| 174 | EXPECT_CALL(*mEventThread, onModeChanged(_, _, _)).Times(0); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 177 | TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) { |
| 178 | DisplayModeId modeId = DisplayModeId(111); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 179 | nsecs_t vsyncPeriod = 111111; |
| 180 | |
| 181 | // If the handle is incorrect, the function should return before |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 182 | // onModeChange is called. |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 183 | Scheduler::ConnectionHandle invalidHandle = {.id = 123}; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame^] | 184 | EXPECT_NO_FATAL_FAILURE(mScheduler.onNonPrimaryDisplayModeChanged(invalidHandle, |
| 185 | PHYSICAL_DISPLAY_ID, modeId, |
| 186 | vsyncPeriod)); |
| 187 | EXPECT_CALL(*mEventThread, onModeChanged(_, _, _)).Times(0); |
Ana Krulec | 6ddd261 | 2020-09-24 13:06:33 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 190 | } // namespace android |