blob: aab27957c3deb63f6a9c80a5c188687395e9a501 [file] [log] [blame]
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001/*
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 Krulec0c8cd522018-08-31 12:27:28 -070017#include <gmock/gmock.h>
18#include <gtest/gtest.h>
Ana Krulec0c8cd522018-08-31 12:27:28 -070019#include <log/log.h>
20
Ana Krulece588e312018-09-18 12:32:24 -070021#include <mutex>
22
Ana Krulec0c8cd522018-08-31 12:27:28 -070023#include "Scheduler/EventThread.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070024#include "Scheduler/RefreshRateConfigs.h"
Dominik Laskowski98041832019-08-01 18:35:59 -070025#include "TestableScheduler.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070026#include "TestableSurfaceFlinger.h"
Dominik Laskowskib0054a22022-03-03 09:03:06 -080027#include "mock/DisplayHardware/MockDisplayMode.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070028#include "mock/MockEventThread.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070029#include "mock/MockLayer.h"
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070030#include "mock/MockSchedulerCallback.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070031
Dominik Laskowski068173d2021-08-11 17:22:59 -070032namespace android::scheduler {
33
Dominik Laskowskib0054a22022-03-03 09:03:06 -080034using android::mock::createDisplayMode;
35
Ana Krulec0c8cd522018-08-31 12:27:28 -070036using testing::_;
37using testing::Return;
38
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070039namespace {
Ana Krulec0c8cd522018-08-31 12:27:28 -070040
Dominik Laskowski068173d2021-08-11 17:22:59 -070041using MockEventThread = android::mock::EventThread;
42using MockLayer = android::mock::MockLayer;
43
Dominik Laskowskif1833852021-03-23 15:06:50 -070044constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = PhysicalDisplayId::fromPort(255u);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080045
Ana Krulec0c8cd522018-08-31 12:27:28 -070046class SchedulerTest : public testing::Test {
47protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080048 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070049 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080050 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abraham62f216c2020-10-13 19:07:23 -070051 : EventThreadConnection(eventThread, /*callingUid=*/0, ResyncCallback()) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070052 ~MockEventThreadConnection() = default;
53
Huihong Luo6fac5232021-11-22 16:05:23 -080054 MOCK_METHOD1(stealReceiveChannel, binder::Status(gui::BitTube* outChannel));
55 MOCK_METHOD1(setVsyncRate, binder::Status(int count));
56 MOCK_METHOD0(requestNextVsync, binder::Status());
Ana Krulec0c8cd522018-08-31 12:27:28 -070057 };
58
Ana Krulec0c8cd522018-08-31 12:27:28 -070059 SchedulerTest();
Ana Krulec0c8cd522018-08-31 12:27:28 -070060
Dominik Laskowskib0054a22022-03-03 09:03:06 -080061 static inline const DisplayModePtr kMode60 = createDisplayMode(DisplayModeId(0), 60_Hz);
62 static inline const DisplayModePtr kMode120 = createDisplayMode(DisplayModeId(1), 120_Hz);
Marin Shalamanov2cde1002021-06-08 19:50:10 +020063
Dominik Laskowski068173d2021-08-11 17:22:59 -070064 std::shared_ptr<RefreshRateConfigs> mConfigs =
Dominik Laskowskib0054a22022-03-03 09:03:06 -080065 std::make_shared<RefreshRateConfigs>(makeModes(kMode60), kMode60->getId());
Dominik Laskowski983f2b52020-06-25 16:54:06 -070066
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070067 mock::SchedulerCallback mSchedulerCallback;
Ady Abrahama0a16272021-03-03 15:23:35 -080068 TestableScheduler* mScheduler = new TestableScheduler{mConfigs, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070069
Dominik Laskowski068173d2021-08-11 17:22:59 -070070 ConnectionHandle mConnectionHandle;
71 MockEventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070072 sp<MockEventThreadConnection> mEventThreadConnection;
Ady Abraham564f9de2021-02-03 18:34:33 -080073
74 TestableSurfaceFlinger mFlinger;
Ana Krulec0c8cd522018-08-31 12:27:28 -070075};
76
77SchedulerTest::SchedulerTest() {
Dominik Laskowski068173d2021-08-11 17:22:59 -070078 auto eventThread = std::make_unique<MockEventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070079 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080080 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
81
82 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070083
84 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
85 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -070086 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -070087 .WillRepeatedly(Return(mEventThreadConnection));
88
Ady Abrahama0a16272021-03-03 15:23:35 -080089 mConnectionHandle = mScheduler->createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -070090 EXPECT_TRUE(mConnectionHandle);
Ady Abrahama0a16272021-03-03 15:23:35 -080091
92 mFlinger.resetScheduler(mScheduler);
Ana Krulec0c8cd522018-08-31 12:27:28 -070093}
94
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070095} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -070096
Ana Krulec0c8cd522018-08-31 12:27:28 -070097TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070098 ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070099
Ady Abrahama0a16272021-03-03 15:23:35 -0800100 const sp<IDisplayEventConnection> connection = mScheduler->createDisplayEventConnection(handle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700101
Dominik Laskowski98041832019-08-01 18:35:59 -0700102 EXPECT_FALSE(connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800103 EXPECT_FALSE(mScheduler->getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104
105 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
106 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800107 mScheduler->onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108
109 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800110 mScheduler->onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700111
112 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800113 mScheduler->onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700114
Dominik Laskowski98041832019-08-01 18:35:59 -0700115 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800117 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700118 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700119
Ady Abraham9c53ee72020-07-22 21:16:18 -0700120 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800121 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700122}
123
124TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700125 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800126 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700127
Dominik Laskowski98041832019-08-01 18:35:59 -0700128 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800129 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800131 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800132 mScheduler->onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133
134 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800135 mScheduler->onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700136
137 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800138 mScheduler->onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700139
Dominik Laskowski98041832019-08-01 18:35:59 -0700140 std::string output("dump");
141 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800142 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700143 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700144
Ady Abraham9c53ee72020-07-22 21:16:18 -0700145 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800146 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800147
148 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700149 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800150 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151}
Dominik Laskowski98041832019-08-01 18:35:59 -0700152
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200153TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
154 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700155 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700156
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200157 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700158 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Ady Abrahama0a16272021-03-03 15:23:35 -0800159 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700160 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700161
162 constexpr bool kPowerStateNormal = true;
Ady Abrahama0a16272021-03-03 15:23:35 -0800163 mScheduler->setDisplayPowerState(kPowerStateNormal);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700164
165 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700166 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700167
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800168 EXPECT_CALL(mSchedulerCallback, requestDisplayMode(_, _)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800169 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700170}
171
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200172TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700173 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700174 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700175 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200176
Ady Abraham3efa3942021-06-24 19:01:25 -0700177 mScheduler->setRefreshRateConfigs(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800178 std::make_shared<RefreshRateConfigs>(makeModes(kMode60, kMode120), kMode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200179
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700180 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200181 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700182 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200183}
184
Dominik Laskowski068173d2021-08-11 17:22:59 -0700185TEST_F(SchedulerTest, dispatchCachedReportedMode) {
186 mScheduler->clearCachedReportedMode();
187
Ady Abraham690f4612021-07-01 23:24:03 -0700188 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700189 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700190}
191
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100192TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700193 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
194 .setId(DisplayModeId(111))
195 .setPhysicalDisplayId(PHYSICAL_DISPLAY_ID)
196 .setVsyncPeriod(111111)
197 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700198
199 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100200 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700201 ConnectionHandle invalidHandle = {.id = 123};
Ady Abraham690f4612021-07-01 23:24:03 -0700202 EXPECT_NO_FATAL_FAILURE(mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle, mode));
203 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700204}
205
Ady Abraham899dcdb2021-06-15 16:56:21 -0700206TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700207 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
208 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
209 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800210
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700211 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800212
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700213 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800214}
215
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200216MATCHER(Is120Hz, "") {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800217 return isApproxEqual(arg->getFps(), 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200218}
219
220TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Ady Abraham3efa3942021-06-24 19:01:25 -0700221 mScheduler->setRefreshRateConfigs(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800222 std::make_shared<RefreshRateConfigs>(makeModes(kMode60, kMode120), kMode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200223
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800224 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
225 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200226
227 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
228
229 constexpr bool kPowerStateNormal = true;
230 mScheduler->setDisplayPowerState(kPowerStateNormal);
231
232 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700233 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200234
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800235 EXPECT_CALL(mSchedulerCallback, requestDisplayMode(Is120Hz(), _)).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200236 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800237
238 // No-op if layer requirements have not changed.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800239 EXPECT_CALL(mSchedulerCallback, requestDisplayMode(_, _)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800240 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200241}
242
Dominik Laskowski068173d2021-08-11 17:22:59 -0700243} // namespace android::scheduler