blob: 4b15385fa82b5ac257e3e180c769ba9c4281ac4d [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"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040024#include "Scheduler/RefreshRateSelector.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
Ana Krulec0c8cd522018-08-31 12:27:28 -070044class SchedulerTest : public testing::Test {
45protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080046 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070047 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080048 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abrahamd11bade2022-08-01 16:18:03 -070049 : EventThreadConnection(eventThread, /*callingUid*/ static_cast<uid_t>(0),
50 ResyncCallback()) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070051 ~MockEventThreadConnection() = default;
52
Huihong Luo6fac5232021-11-22 16:05:23 -080053 MOCK_METHOD1(stealReceiveChannel, binder::Status(gui::BitTube* outChannel));
54 MOCK_METHOD1(setVsyncRate, binder::Status(int count));
55 MOCK_METHOD0(requestNextVsync, binder::Status());
Ana Krulec0c8cd522018-08-31 12:27:28 -070056 };
57
Ana Krulec0c8cd522018-08-31 12:27:28 -070058 SchedulerTest();
Ana Krulec0c8cd522018-08-31 12:27:28 -070059
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040060 static constexpr PhysicalDisplayId kDisplayId1 = PhysicalDisplayId::fromPort(255u);
Ady Abrahamace3d052022-11-17 16:25:05 -080061 static inline const ftl::NonNull<DisplayModePtr> kDisplay1Mode60 =
62 ftl::as_non_null(createDisplayMode(kDisplayId1, DisplayModeId(0), 60_Hz));
63 static inline const ftl::NonNull<DisplayModePtr> kDisplay1Mode120 =
64 ftl::as_non_null(createDisplayMode(kDisplayId1, DisplayModeId(1), 120_Hz));
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040065 static inline const DisplayModes kDisplay1Modes = makeModes(kDisplay1Mode60, kDisplay1Mode120);
66
67 static constexpr PhysicalDisplayId kDisplayId2 = PhysicalDisplayId::fromPort(254u);
Ady Abrahamace3d052022-11-17 16:25:05 -080068 static inline const ftl::NonNull<DisplayModePtr> kDisplay2Mode60 =
69 ftl::as_non_null(createDisplayMode(kDisplayId2, DisplayModeId(0), 60_Hz));
70 static inline const ftl::NonNull<DisplayModePtr> kDisplay2Mode120 =
71 ftl::as_non_null(createDisplayMode(kDisplayId2, DisplayModeId(1), 120_Hz));
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040072 static inline const DisplayModes kDisplay2Modes = makeModes(kDisplay2Mode60, kDisplay2Mode120);
73
74 static constexpr PhysicalDisplayId kDisplayId3 = PhysicalDisplayId::fromPort(253u);
Ady Abrahamace3d052022-11-17 16:25:05 -080075 static inline const ftl::NonNull<DisplayModePtr> kDisplay3Mode60 =
76 ftl::as_non_null(createDisplayMode(kDisplayId3, DisplayModeId(0), 60_Hz));
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040077 static inline const DisplayModes kDisplay3Modes = makeModes(kDisplay3Mode60);
Marin Shalamanov2cde1002021-06-08 19:50:10 +020078
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040079 std::shared_ptr<RefreshRateSelector> mSelector =
80 std::make_shared<RefreshRateSelector>(makeModes(kDisplay1Mode60),
81 kDisplay1Mode60->getId());
Dominik Laskowski983f2b52020-06-25 16:54:06 -070082
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070083 mock::SchedulerCallback mSchedulerCallback;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040084 TestableScheduler* mScheduler = new TestableScheduler{mSelector, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070085
Dominik Laskowski068173d2021-08-11 17:22:59 -070086 ConnectionHandle mConnectionHandle;
87 MockEventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070088 sp<MockEventThreadConnection> mEventThreadConnection;
Ady Abraham564f9de2021-02-03 18:34:33 -080089
90 TestableSurfaceFlinger mFlinger;
Ana Krulec0c8cd522018-08-31 12:27:28 -070091};
92
93SchedulerTest::SchedulerTest() {
Dominik Laskowski068173d2021-08-11 17:22:59 -070094 auto eventThread = std::make_unique<MockEventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070095 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080096 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
97
Ady Abrahamd11bade2022-08-01 16:18:03 -070098 mEventThreadConnection = sp<MockEventThreadConnection>::make(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070099
100 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
101 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700102 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -0700103 .WillRepeatedly(Return(mEventThreadConnection));
104
Ady Abrahama0a16272021-03-03 15:23:35 -0800105 mConnectionHandle = mScheduler->createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700106 EXPECT_TRUE(mConnectionHandle);
Ady Abrahama0a16272021-03-03 15:23:35 -0800107
108 mFlinger.resetScheduler(mScheduler);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700109}
110
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700111} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -0700112
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700114 ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115
Ady Abrahama0a16272021-03-03 15:23:35 -0800116 const sp<IDisplayEventConnection> connection = mScheduler->createDisplayEventConnection(handle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700117
Dominik Laskowski98041832019-08-01 18:35:59 -0700118 EXPECT_FALSE(connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800119 EXPECT_FALSE(mScheduler->getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700120
121 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
122 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400123 mScheduler->onHotplugReceived(handle, kDisplayId1, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700124
125 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800126 mScheduler->onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700127
128 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800129 mScheduler->onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130
Dominik Laskowski98041832019-08-01 18:35:59 -0700131 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700132 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800133 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700134 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700135
Ady Abraham9c53ee72020-07-22 21:16:18 -0700136 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800137 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700138}
139
140TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700141 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800142 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700143
Dominik Laskowski98041832019-08-01 18:35:59 -0700144 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800145 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700146
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400147 EXPECT_CALL(*mEventThread, onHotplugReceived(kDisplayId1, false)).Times(1);
148 mScheduler->onHotplugReceived(mConnectionHandle, kDisplayId1, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700149
150 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800151 mScheduler->onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152
153 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800154 mScheduler->onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155
Dominik Laskowski98041832019-08-01 18:35:59 -0700156 std::string output("dump");
157 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800158 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700159 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700160
Ady Abraham9c53ee72020-07-22 21:16:18 -0700161 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800162 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800163
164 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700165 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800166 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700167}
Dominik Laskowski98041832019-08-01 18:35:59 -0700168
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200169TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
170 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700171 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700172
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200173 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700174 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Ady Abrahama0a16272021-03-03 15:23:35 -0800175 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700176 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700177
Rachel Lee6a9731d2022-06-06 17:08:14 -0700178 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500179 mScheduler->setDisplayPowerMode(kPowerModeOn);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700180
181 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700182 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700183
ramindani69b58e82022-09-26 16:48:36 -0700184 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800185 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700186}
187
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200188TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700189 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700190 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700191 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200192
Dominik Laskowski596a2562022-10-28 11:26:12 -0400193 // Replace `mSelector` with a new `RefreshRateSelector` that has different display modes.
194 mScheduler->registerDisplay(kDisplayId1,
195 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
196 kDisplay1Mode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200197
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700198 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200199 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700200 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200201}
202
Dominik Laskowski068173d2021-08-11 17:22:59 -0700203TEST_F(SchedulerTest, dispatchCachedReportedMode) {
204 mScheduler->clearCachedReportedMode();
205
Ady Abraham690f4612021-07-01 23:24:03 -0700206 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700207 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700208}
209
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100210TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700211 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
212 .setId(DisplayModeId(111))
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400213 .setPhysicalDisplayId(kDisplayId1)
Ady Abraham690f4612021-07-01 23:24:03 -0700214 .setVsyncPeriod(111111)
215 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700216
217 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100218 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700219 ConnectionHandle invalidHandle = {.id = 123};
Ady Abrahamace3d052022-11-17 16:25:05 -0800220 EXPECT_NO_FATAL_FAILURE(
221 mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle,
222 {90_Hz, ftl::as_non_null(mode)}));
Ady Abraham690f4612021-07-01 23:24:03 -0700223 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700224}
225
Ady Abraham899dcdb2021-06-15 16:56:21 -0700226TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700227 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
228 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
229 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800230
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700231 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800232
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700233 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800234}
235
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200236MATCHER(Is120Hz, "") {
Ady Abrahamace3d052022-11-17 16:25:05 -0800237 return isApproxEqual(arg.front().mode.fps, 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200238}
239
240TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400241 mScheduler->registerDisplay(kDisplayId1,
242 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
243 kDisplay1Mode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200244
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800245 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
246 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200247
248 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
249
Rachel Lee6a9731d2022-06-06 17:08:14 -0700250 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500251 mScheduler->setDisplayPowerMode(kPowerModeOn);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200252
253 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700254 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200255
ramindani69b58e82022-09-26 16:48:36 -0700256 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(Is120Hz())).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200257 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800258
259 // No-op if layer requirements have not changed.
ramindani69b58e82022-09-26 16:48:36 -0700260 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800261 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200262}
263
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400264TEST_F(SchedulerTest, chooseDisplayModesSingleDisplay) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400265 mScheduler->registerDisplay(kDisplayId1,
266 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
267 kDisplay1Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700268
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400269 std::vector<RefreshRateSelector::LayerRequirement> layers =
270 std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}});
ramindani69b58e82022-09-26 16:48:36 -0700271 mScheduler->setContentRequirements(layers);
272 GlobalSignals globalSignals = {.idle = true};
273 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
274
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400275 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
276
277 auto modeChoices = mScheduler->chooseDisplayModes();
278 ASSERT_EQ(1u, modeChoices.size());
279
280 auto choice = modeChoices.get(kDisplayId1);
281 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800282 EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700283
284 globalSignals = {.idle = false};
285 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400286
287 modeChoices = mScheduler->chooseDisplayModes();
288 ASSERT_EQ(1u, modeChoices.size());
289
290 choice = modeChoices.get(kDisplayId1);
291 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800292 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700293
294 globalSignals = {.touch = true};
295 mScheduler->replaceTouchTimer(10);
296 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700297
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400298 modeChoices = mScheduler->chooseDisplayModes();
299 ASSERT_EQ(1u, modeChoices.size());
300
301 choice = modeChoices.get(kDisplayId1);
302 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800303 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700304}
305
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400306TEST_F(SchedulerTest, chooseDisplayModesMultipleDisplays) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400307 mScheduler->registerDisplay(kDisplayId1,
308 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
309 kDisplay1Mode60->getId()));
310 mScheduler->registerDisplay(kDisplayId2,
311 std::make_shared<RefreshRateSelector>(kDisplay2Modes,
312 kDisplay2Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700313
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400314 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
315 TestableScheduler::DisplayModeChoiceMap expectedChoices;
ramindani69b58e82022-09-26 16:48:36 -0700316
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400317 {
318 const GlobalSignals globalSignals = {.idle = true};
319 expectedChoices =
320 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800321 DisplayModeChoice>(kDisplayId1,
322 FrameRateMode{60_Hz, kDisplay1Mode60},
323 globalSignals)(kDisplayId2,
324 FrameRateMode{60_Hz,
325 kDisplay2Mode60},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400326 globalSignals);
327
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400328 std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f},
329 {.weight = 1.f}};
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400330 mScheduler->setContentRequirements(layers);
331 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
332
333 const auto actualChoices = mScheduler->chooseDisplayModes();
334 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700335 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400336 {
337 const GlobalSignals globalSignals = {.idle = false};
338 expectedChoices =
339 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800340 DisplayModeChoice>(kDisplayId1,
341 FrameRateMode{120_Hz, kDisplay1Mode120},
342 globalSignals)(kDisplayId2,
343 FrameRateMode{120_Hz,
344 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400345 globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700346
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400347 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700348
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400349 const auto actualChoices = mScheduler->chooseDisplayModes();
350 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700351 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400352 {
353 const GlobalSignals globalSignals = {.touch = true};
354 mScheduler->replaceTouchTimer(10);
355 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700356
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400357 expectedChoices =
358 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800359 DisplayModeChoice>(kDisplayId1,
360 FrameRateMode{120_Hz, kDisplay1Mode120},
361 globalSignals)(kDisplayId2,
362 FrameRateMode{120_Hz,
363 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400364 globalSignals);
365
366 const auto actualChoices = mScheduler->chooseDisplayModes();
367 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700368 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400369 {
370 // This display does not support 120 Hz, so we should choose 60 Hz despite the touch signal.
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400371 mScheduler
372 ->registerDisplay(kDisplayId3,
373 std::make_shared<RefreshRateSelector>(kDisplay3Modes,
374 kDisplay3Mode60->getId()));
Dominik Laskowski327d6092022-10-11 18:05:08 -0400375
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400376 const GlobalSignals globalSignals = {.touch = true};
377 mScheduler->replaceTouchTimer(10);
378 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani7c487282022-10-10 16:17:51 -0700379
Ady Abrahamace3d052022-11-17 16:25:05 -0800380 expectedChoices = ftl::init::map<
381 const PhysicalDisplayId&,
382 DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60},
383 globalSignals)(kDisplayId2,
384 FrameRateMode{60_Hz, kDisplay2Mode60},
385 globalSignals)(kDisplayId3,
386 FrameRateMode{60_Hz,
387 kDisplay3Mode60},
388 globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400389
390 const auto actualChoices = mScheduler->chooseDisplayModes();
391 EXPECT_EQ(expectedChoices, actualChoices);
ramindani7c487282022-10-10 16:17:51 -0700392 }
ramindani69b58e82022-09-26 16:48:36 -0700393}
394
Dominik Laskowski068173d2021-08-11 17:22:59 -0700395} // namespace android::scheduler