blob: dc76b4c90f391a67b8a105c7b86c3e675144a51b [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
Leon Scroggins III67388622023-02-06 20:36:20 -050017#include <ftl/fake_guard.h>
Ana Krulec0c8cd522018-08-31 12:27:28 -070018#include <gmock/gmock.h>
19#include <gtest/gtest.h>
Ana Krulec0c8cd522018-08-31 12:27:28 -070020#include <log/log.h>
21
Ana Krulece588e312018-09-18 12:32:24 -070022#include <mutex>
23
Ana Krulec0c8cd522018-08-31 12:27:28 -070024#include "Scheduler/EventThread.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040025#include "Scheduler/RefreshRateSelector.h"
Dominik Laskowski98041832019-08-01 18:35:59 -070026#include "TestableScheduler.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070027#include "TestableSurfaceFlinger.h"
Dominik Laskowskib0054a22022-03-03 09:03:06 -080028#include "mock/DisplayHardware/MockDisplayMode.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070029#include "mock/MockEventThread.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070030#include "mock/MockLayer.h"
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070031#include "mock/MockSchedulerCallback.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070032
Dominik Laskowski068173d2021-08-11 17:22:59 -070033namespace android::scheduler {
34
Dominik Laskowskib0054a22022-03-03 09:03:06 -080035using android::mock::createDisplayMode;
36
Ana Krulec0c8cd522018-08-31 12:27:28 -070037using testing::_;
38using testing::Return;
39
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070040namespace {
Ana Krulec0c8cd522018-08-31 12:27:28 -070041
Dominik Laskowski068173d2021-08-11 17:22:59 -070042using MockEventThread = android::mock::EventThread;
43using MockLayer = android::mock::MockLayer;
44
Ana Krulec0c8cd522018-08-31 12:27:28 -070045class SchedulerTest : public testing::Test {
46protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080047 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070048 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080049 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abrahamd11bade2022-08-01 16:18:03 -070050 : EventThreadConnection(eventThread, /*callingUid*/ static_cast<uid_t>(0),
51 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 Laskowski530d6bd2022-10-10 16:55:54 -040061 static constexpr PhysicalDisplayId kDisplayId1 = PhysicalDisplayId::fromPort(255u);
Ady Abrahamace3d052022-11-17 16:25:05 -080062 static inline const ftl::NonNull<DisplayModePtr> kDisplay1Mode60 =
63 ftl::as_non_null(createDisplayMode(kDisplayId1, DisplayModeId(0), 60_Hz));
64 static inline const ftl::NonNull<DisplayModePtr> kDisplay1Mode120 =
65 ftl::as_non_null(createDisplayMode(kDisplayId1, DisplayModeId(1), 120_Hz));
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040066 static inline const DisplayModes kDisplay1Modes = makeModes(kDisplay1Mode60, kDisplay1Mode120);
67
68 static constexpr PhysicalDisplayId kDisplayId2 = PhysicalDisplayId::fromPort(254u);
Ady Abrahamace3d052022-11-17 16:25:05 -080069 static inline const ftl::NonNull<DisplayModePtr> kDisplay2Mode60 =
70 ftl::as_non_null(createDisplayMode(kDisplayId2, DisplayModeId(0), 60_Hz));
71 static inline const ftl::NonNull<DisplayModePtr> kDisplay2Mode120 =
72 ftl::as_non_null(createDisplayMode(kDisplayId2, DisplayModeId(1), 120_Hz));
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040073 static inline const DisplayModes kDisplay2Modes = makeModes(kDisplay2Mode60, kDisplay2Mode120);
74
75 static constexpr PhysicalDisplayId kDisplayId3 = PhysicalDisplayId::fromPort(253u);
Ady Abrahamace3d052022-11-17 16:25:05 -080076 static inline const ftl::NonNull<DisplayModePtr> kDisplay3Mode60 =
77 ftl::as_non_null(createDisplayMode(kDisplayId3, DisplayModeId(0), 60_Hz));
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040078 static inline const DisplayModes kDisplay3Modes = makeModes(kDisplay3Mode60);
Marin Shalamanov2cde1002021-06-08 19:50:10 +020079
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040080 std::shared_ptr<RefreshRateSelector> mSelector =
81 std::make_shared<RefreshRateSelector>(makeModes(kDisplay1Mode60),
82 kDisplay1Mode60->getId());
Dominik Laskowski983f2b52020-06-25 16:54:06 -070083
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070084 mock::SchedulerCallback mSchedulerCallback;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040085 TestableScheduler* mScheduler = new TestableScheduler{mSelector, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070086
Dominik Laskowski068173d2021-08-11 17:22:59 -070087 ConnectionHandle mConnectionHandle;
88 MockEventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070089 sp<MockEventThreadConnection> mEventThreadConnection;
Ady Abraham564f9de2021-02-03 18:34:33 -080090
91 TestableSurfaceFlinger mFlinger;
Ana Krulec0c8cd522018-08-31 12:27:28 -070092};
93
94SchedulerTest::SchedulerTest() {
Dominik Laskowski068173d2021-08-11 17:22:59 -070095 auto eventThread = std::make_unique<MockEventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070096 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080097 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
98
Ady Abrahamd11bade2022-08-01 16:18:03 -070099 mEventThreadConnection = sp<MockEventThreadConnection>::make(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700100
101 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
102 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700103 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104 .WillRepeatedly(Return(mEventThreadConnection));
105
Ady Abrahama0a16272021-03-03 15:23:35 -0800106 mConnectionHandle = mScheduler->createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -0700107 EXPECT_TRUE(mConnectionHandle);
Ady Abrahama0a16272021-03-03 15:23:35 -0800108
109 mFlinger.resetScheduler(mScheduler);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700110}
111
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700112} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113
Ana Krulec0c8cd522018-08-31 12:27:28 -0700114TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700115 ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116
Ady Abrahama0a16272021-03-03 15:23:35 -0800117 const sp<IDisplayEventConnection> connection = mScheduler->createDisplayEventConnection(handle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700118
Dominik Laskowski98041832019-08-01 18:35:59 -0700119 EXPECT_FALSE(connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800120 EXPECT_FALSE(mScheduler->getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700121
122 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
123 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400124 mScheduler->onHotplugReceived(handle, kDisplayId1, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700125
Dominik Laskowski98041832019-08-01 18:35:59 -0700126 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700127 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800128 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700129 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130
Ady Abraham9c53ee72020-07-22 21:16:18 -0700131 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800132 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133}
134
135TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700136 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800137 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700138
Dominik Laskowski98041832019-08-01 18:35:59 -0700139 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800140 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700141
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400142 EXPECT_CALL(*mEventThread, onHotplugReceived(kDisplayId1, false)).Times(1);
143 mScheduler->onHotplugReceived(mConnectionHandle, kDisplayId1, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700144
Dominik Laskowski98041832019-08-01 18:35:59 -0700145 std::string output("dump");
146 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800147 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700148 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700149
Ady Abraham9c53ee72020-07-22 21:16:18 -0700150 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800151 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800152
153 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700154 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800155 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156}
Dominik Laskowski98041832019-08-01 18:35:59 -0700157
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200158TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
159 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700160 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700161
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200162 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700163 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000164 mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0,
165 LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700166 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700167
Rachel Lee6a9731d2022-06-06 17:08:14 -0700168 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins III67388622023-02-06 20:36:20 -0500169 FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700170
171 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700172 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700173
ramindani69b58e82022-09-26 16:48:36 -0700174 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800175 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700176}
177
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200178TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700179 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700180 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700181 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200182
Dominik Laskowski596a2562022-10-28 11:26:12 -0400183 // Replace `mSelector` with a new `RefreshRateSelector` that has different display modes.
184 mScheduler->registerDisplay(kDisplayId1,
185 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
186 kDisplay1Mode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200187
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700188 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000189 mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0,
190 LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700191 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200192}
193
Dominik Laskowski068173d2021-08-11 17:22:59 -0700194TEST_F(SchedulerTest, dispatchCachedReportedMode) {
195 mScheduler->clearCachedReportedMode();
196
Ady Abraham690f4612021-07-01 23:24:03 -0700197 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700198 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700199}
200
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100201TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700202 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
203 .setId(DisplayModeId(111))
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400204 .setPhysicalDisplayId(kDisplayId1)
Ady Abraham690f4612021-07-01 23:24:03 -0700205 .setVsyncPeriod(111111)
206 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700207
208 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100209 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700210 ConnectionHandle invalidHandle = {.id = 123};
Ady Abrahamace3d052022-11-17 16:25:05 -0800211 EXPECT_NO_FATAL_FAILURE(
212 mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle,
213 {90_Hz, ftl::as_non_null(mode)}));
Ady Abraham690f4612021-07-01 23:24:03 -0700214 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700215}
216
Ady Abraham899dcdb2021-06-15 16:56:21 -0700217TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700218 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
219 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
220 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800221
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700222 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800223
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700224 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800225}
226
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200227MATCHER(Is120Hz, "") {
Ady Abrahamace3d052022-11-17 16:25:05 -0800228 return isApproxEqual(arg.front().mode.fps, 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200229}
230
231TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400232 mScheduler->registerDisplay(kDisplayId1,
233 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
234 kDisplay1Mode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200235
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800236 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
237 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200238
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000239 mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0,
240 LayerHistory::LayerUpdateType::Buffer);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200241
Rachel Lee6a9731d2022-06-06 17:08:14 -0700242 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins III67388622023-02-06 20:36:20 -0500243 FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200244
245 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700246 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200247
ramindani69b58e82022-09-26 16:48:36 -0700248 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(Is120Hz())).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200249 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800250
251 // No-op if layer requirements have not changed.
ramindani69b58e82022-09-26 16:48:36 -0700252 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800253 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200254}
255
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400256TEST_F(SchedulerTest, chooseDisplayModesSingleDisplay) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400257 mScheduler->registerDisplay(kDisplayId1,
258 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
259 kDisplay1Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700260
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400261 std::vector<RefreshRateSelector::LayerRequirement> layers =
262 std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}});
ramindani69b58e82022-09-26 16:48:36 -0700263 mScheduler->setContentRequirements(layers);
264 GlobalSignals globalSignals = {.idle = true};
265 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
266
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400267 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
268
269 auto modeChoices = mScheduler->chooseDisplayModes();
270 ASSERT_EQ(1u, modeChoices.size());
271
272 auto choice = modeChoices.get(kDisplayId1);
273 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800274 EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700275
276 globalSignals = {.idle = false};
277 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400278
279 modeChoices = mScheduler->chooseDisplayModes();
280 ASSERT_EQ(1u, modeChoices.size());
281
282 choice = modeChoices.get(kDisplayId1);
283 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800284 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700285
286 globalSignals = {.touch = true};
287 mScheduler->replaceTouchTimer(10);
288 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700289
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400290 modeChoices = mScheduler->chooseDisplayModes();
291 ASSERT_EQ(1u, modeChoices.size());
292
293 choice = modeChoices.get(kDisplayId1);
294 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800295 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700296}
297
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400298TEST_F(SchedulerTest, chooseDisplayModesMultipleDisplays) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400299 mScheduler->registerDisplay(kDisplayId1,
300 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
301 kDisplay1Mode60->getId()));
302 mScheduler->registerDisplay(kDisplayId2,
303 std::make_shared<RefreshRateSelector>(kDisplay2Modes,
304 kDisplay2Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700305
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400306 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
307 TestableScheduler::DisplayModeChoiceMap expectedChoices;
ramindani69b58e82022-09-26 16:48:36 -0700308
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400309 {
310 const GlobalSignals globalSignals = {.idle = true};
311 expectedChoices =
312 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800313 DisplayModeChoice>(kDisplayId1,
314 FrameRateMode{60_Hz, kDisplay1Mode60},
315 globalSignals)(kDisplayId2,
316 FrameRateMode{60_Hz,
317 kDisplay2Mode60},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400318 globalSignals);
319
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400320 std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f},
321 {.weight = 1.f}};
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400322 mScheduler->setContentRequirements(layers);
323 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
324
325 const auto actualChoices = mScheduler->chooseDisplayModes();
326 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700327 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400328 {
329 const GlobalSignals globalSignals = {.idle = false};
330 expectedChoices =
331 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800332 DisplayModeChoice>(kDisplayId1,
333 FrameRateMode{120_Hz, kDisplay1Mode120},
334 globalSignals)(kDisplayId2,
335 FrameRateMode{120_Hz,
336 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400337 globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700338
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400339 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700340
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400341 const auto actualChoices = mScheduler->chooseDisplayModes();
342 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700343 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400344 {
345 const GlobalSignals globalSignals = {.touch = true};
346 mScheduler->replaceTouchTimer(10);
347 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700348
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400349 expectedChoices =
350 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800351 DisplayModeChoice>(kDisplayId1,
352 FrameRateMode{120_Hz, kDisplay1Mode120},
353 globalSignals)(kDisplayId2,
354 FrameRateMode{120_Hz,
355 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400356 globalSignals);
357
358 const auto actualChoices = mScheduler->chooseDisplayModes();
359 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700360 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400361 {
362 // This display does not support 120 Hz, so we should choose 60 Hz despite the touch signal.
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400363 mScheduler
364 ->registerDisplay(kDisplayId3,
365 std::make_shared<RefreshRateSelector>(kDisplay3Modes,
366 kDisplay3Mode60->getId()));
Dominik Laskowski327d6092022-10-11 18:05:08 -0400367
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400368 const GlobalSignals globalSignals = {.touch = true};
369 mScheduler->replaceTouchTimer(10);
370 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani7c487282022-10-10 16:17:51 -0700371
Ady Abrahamace3d052022-11-17 16:25:05 -0800372 expectedChoices = ftl::init::map<
373 const PhysicalDisplayId&,
374 DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60},
375 globalSignals)(kDisplayId2,
376 FrameRateMode{60_Hz, kDisplay2Mode60},
377 globalSignals)(kDisplayId3,
378 FrameRateMode{60_Hz,
379 kDisplay3Mode60},
380 globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400381
382 const auto actualChoices = mScheduler->chooseDisplayModes();
383 EXPECT_EQ(expectedChoices, actualChoices);
ramindani7c487282022-10-10 16:17:51 -0700384 }
ramindani69b58e82022-09-26 16:48:36 -0700385}
386
Dominik Laskowski068173d2021-08-11 17:22:59 -0700387} // namespace android::scheduler