blob: 16fa35e9c31741a3b6823a23cd888e223633220a [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
Dominik Laskowski008bec02023-03-14 12:04:58 -0400158TEST_F(SchedulerTest, registerDisplay) {
159 // Hardware VSYNC should not change if the display is already registered.
160 EXPECT_CALL(mSchedulerCallback, setVsyncEnabled(kDisplayId1, false)).Times(0);
161 mScheduler->registerDisplay(kDisplayId1,
162 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
163 kDisplay1Mode60->getId()));
164
165 // Hardware VSYNC should be disabled for newly registered displays.
166 EXPECT_CALL(mSchedulerCallback, setVsyncEnabled(kDisplayId2, false)).Times(1);
167 EXPECT_CALL(mSchedulerCallback, setVsyncEnabled(kDisplayId3, false)).Times(1);
168
169 mScheduler->registerDisplay(kDisplayId2,
170 std::make_shared<RefreshRateSelector>(kDisplay2Modes,
171 kDisplay2Mode60->getId()));
172 mScheduler->registerDisplay(kDisplayId3,
173 std::make_shared<RefreshRateSelector>(kDisplay3Modes,
174 kDisplay3Mode60->getId()));
175}
176
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200177TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
178 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700179 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700180
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200181 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700182 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000183 mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0,
184 LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700185 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700186
Rachel Lee6a9731d2022-06-06 17:08:14 -0700187 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins III67388622023-02-06 20:36:20 -0500188 FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700189
190 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700191 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700192
ramindani69b58e82022-09-26 16:48:36 -0700193 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800194 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700195}
196
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200197TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700198 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700199 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700200 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200201
Dominik Laskowski596a2562022-10-28 11:26:12 -0400202 // Replace `mSelector` with a new `RefreshRateSelector` that has different display modes.
203 mScheduler->registerDisplay(kDisplayId1,
204 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
205 kDisplay1Mode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200206
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700207 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000208 mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0,
209 LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700210 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200211}
212
Dominik Laskowski068173d2021-08-11 17:22:59 -0700213TEST_F(SchedulerTest, dispatchCachedReportedMode) {
214 mScheduler->clearCachedReportedMode();
215
Ady Abraham690f4612021-07-01 23:24:03 -0700216 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700217 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700218}
219
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100220TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700221 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
222 .setId(DisplayModeId(111))
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400223 .setPhysicalDisplayId(kDisplayId1)
Ady Abraham690f4612021-07-01 23:24:03 -0700224 .setVsyncPeriod(111111)
225 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700226
227 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100228 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700229 ConnectionHandle invalidHandle = {.id = 123};
Ady Abrahamace3d052022-11-17 16:25:05 -0800230 EXPECT_NO_FATAL_FAILURE(
231 mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle,
232 {90_Hz, ftl::as_non_null(mode)}));
Ady Abraham690f4612021-07-01 23:24:03 -0700233 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700234}
235
Ady Abraham899dcdb2021-06-15 16:56:21 -0700236TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700237 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
238 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
239 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800240
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700241 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800242
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700243 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800244}
245
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200246MATCHER(Is120Hz, "") {
Ady Abrahamace3d052022-11-17 16:25:05 -0800247 return isApproxEqual(arg.front().mode.fps, 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200248}
249
250TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400251 mScheduler->registerDisplay(kDisplayId1,
252 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
253 kDisplay1Mode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200254
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800255 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
256 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200257
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000258 mScheduler->recordLayerHistory(layer->getSequence(), layer->getLayerProps(), 0,
259 LayerHistory::LayerUpdateType::Buffer);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200260
Rachel Lee6a9731d2022-06-06 17:08:14 -0700261 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins III67388622023-02-06 20:36:20 -0500262 FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200263
264 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700265 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200266
ramindani69b58e82022-09-26 16:48:36 -0700267 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(Is120Hz())).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200268 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800269
270 // No-op if layer requirements have not changed.
ramindani69b58e82022-09-26 16:48:36 -0700271 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800272 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200273}
274
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400275TEST_F(SchedulerTest, chooseDisplayModesSingleDisplay) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400276 mScheduler->registerDisplay(kDisplayId1,
277 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
278 kDisplay1Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700279
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400280 std::vector<RefreshRateSelector::LayerRequirement> layers =
281 std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}});
ramindani69b58e82022-09-26 16:48:36 -0700282 mScheduler->setContentRequirements(layers);
283 GlobalSignals globalSignals = {.idle = true};
284 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
285
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400286 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
287
288 auto modeChoices = mScheduler->chooseDisplayModes();
289 ASSERT_EQ(1u, modeChoices.size());
290
291 auto choice = modeChoices.get(kDisplayId1);
292 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800293 EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700294
295 globalSignals = {.idle = false};
296 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400297
298 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 globalSignals = {.touch = true};
306 mScheduler->replaceTouchTimer(10);
307 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700308
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400309 modeChoices = mScheduler->chooseDisplayModes();
310 ASSERT_EQ(1u, modeChoices.size());
311
312 choice = modeChoices.get(kDisplayId1);
313 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800314 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700315}
316
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400317TEST_F(SchedulerTest, chooseDisplayModesMultipleDisplays) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400318 mScheduler->registerDisplay(kDisplayId1,
319 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
320 kDisplay1Mode60->getId()));
321 mScheduler->registerDisplay(kDisplayId2,
322 std::make_shared<RefreshRateSelector>(kDisplay2Modes,
323 kDisplay2Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700324
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400325 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
326 TestableScheduler::DisplayModeChoiceMap expectedChoices;
ramindani69b58e82022-09-26 16:48:36 -0700327
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400328 {
329 const GlobalSignals globalSignals = {.idle = true};
330 expectedChoices =
331 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800332 DisplayModeChoice>(kDisplayId1,
333 FrameRateMode{60_Hz, kDisplay1Mode60},
334 globalSignals)(kDisplayId2,
335 FrameRateMode{60_Hz,
336 kDisplay2Mode60},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400337 globalSignals);
338
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400339 std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f},
340 {.weight = 1.f}};
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400341 mScheduler->setContentRequirements(layers);
342 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
343
344 const auto actualChoices = mScheduler->chooseDisplayModes();
345 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700346 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400347 {
348 const GlobalSignals globalSignals = {.idle = false};
349 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);
ramindani69b58e82022-09-26 16:48:36 -0700357
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400358 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700359
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400360 const auto actualChoices = mScheduler->chooseDisplayModes();
361 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700362 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400363 {
364 const GlobalSignals globalSignals = {.touch = true};
365 mScheduler->replaceTouchTimer(10);
366 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700367
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400368 expectedChoices =
369 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800370 DisplayModeChoice>(kDisplayId1,
371 FrameRateMode{120_Hz, kDisplay1Mode120},
372 globalSignals)(kDisplayId2,
373 FrameRateMode{120_Hz,
374 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400375 globalSignals);
376
377 const auto actualChoices = mScheduler->chooseDisplayModes();
378 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700379 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400380 {
ramindani22f2ead2023-04-21 10:27:11 -0700381 // The kDisplayId3 does not support 120Hz, The pacesetter display rate is chosen to be 120
382 // Hz. In this case only the display kDisplayId3 choose 60Hz as it does not support 120Hz.
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400383 mScheduler
384 ->registerDisplay(kDisplayId3,
385 std::make_shared<RefreshRateSelector>(kDisplay3Modes,
386 kDisplay3Mode60->getId()));
Dominik Laskowski327d6092022-10-11 18:05:08 -0400387
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400388 const GlobalSignals globalSignals = {.touch = true};
389 mScheduler->replaceTouchTimer(10);
390 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani7c487282022-10-10 16:17:51 -0700391
Ady Abrahamace3d052022-11-17 16:25:05 -0800392 expectedChoices = ftl::init::map<
393 const PhysicalDisplayId&,
ramindani22f2ead2023-04-21 10:27:11 -0700394 DisplayModeChoice>(kDisplayId1, FrameRateMode{120_Hz, kDisplay1Mode120},
395 globalSignals)(kDisplayId2,
396 FrameRateMode{120_Hz, kDisplay2Mode120},
397 globalSignals)(kDisplayId3,
398 FrameRateMode{60_Hz,
399 kDisplay3Mode60},
400 globalSignals);
401
402 const auto actualChoices = mScheduler->chooseDisplayModes();
403 EXPECT_EQ(expectedChoices, actualChoices);
404 }
405 {
406 // We should choose 60Hz despite the touch signal as pacesetter only supports 60Hz
407 mScheduler->setPacesetterDisplay(kDisplayId3);
408 const GlobalSignals globalSignals = {.touch = true};
409 mScheduler->replaceTouchTimer(10);
410 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
411
412 expectedChoices = ftl::init::map<
413 const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800414 DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60},
415 globalSignals)(kDisplayId2,
416 FrameRateMode{60_Hz, kDisplay2Mode60},
417 globalSignals)(kDisplayId3,
418 FrameRateMode{60_Hz,
419 kDisplay3Mode60},
420 globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400421
422 const auto actualChoices = mScheduler->chooseDisplayModes();
423 EXPECT_EQ(expectedChoices, actualChoices);
ramindani7c487282022-10-10 16:17:51 -0700424 }
ramindani69b58e82022-09-26 16:48:36 -0700425}
426
Dominik Laskowski068173d2021-08-11 17:22:59 -0700427} // namespace android::scheduler