blob: 422fa1ca7e774b27865c4371c90e535e7081e159 [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
Dominik Laskowski98041832019-08-01 18:35:59 -0700125 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700126 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800127 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700128 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700129
Ady Abraham9c53ee72020-07-22 21:16:18 -0700130 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800131 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700132}
133
134TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700135 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800136 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700137
Dominik Laskowski98041832019-08-01 18:35:59 -0700138 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800139 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400141 EXPECT_CALL(*mEventThread, onHotplugReceived(kDisplayId1, false)).Times(1);
142 mScheduler->onHotplugReceived(mConnectionHandle, kDisplayId1, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700143
Dominik Laskowski98041832019-08-01 18:35:59 -0700144 std::string output("dump");
145 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800146 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700147 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700148
Ady Abraham9c53ee72020-07-22 21:16:18 -0700149 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800150 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800151
152 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700153 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800154 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700155}
Dominik Laskowski98041832019-08-01 18:35:59 -0700156
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200157TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
158 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700159 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700160
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200161 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700162 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Ady Abrahama0a16272021-03-03 15:23:35 -0800163 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700164 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700165
Rachel Lee6a9731d2022-06-06 17:08:14 -0700166 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500167 mScheduler->setDisplayPowerMode(kPowerModeOn);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700168
169 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700170 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700171
ramindani69b58e82022-09-26 16:48:36 -0700172 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800173 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700174}
175
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200176TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700177 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700178 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700179 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200180
Dominik Laskowski596a2562022-10-28 11:26:12 -0400181 // Replace `mSelector` with a new `RefreshRateSelector` that has different display modes.
182 mScheduler->registerDisplay(kDisplayId1,
183 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
184 kDisplay1Mode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200185
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700186 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200187 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700188 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200189}
190
Dominik Laskowski068173d2021-08-11 17:22:59 -0700191TEST_F(SchedulerTest, dispatchCachedReportedMode) {
192 mScheduler->clearCachedReportedMode();
193
Ady Abraham690f4612021-07-01 23:24:03 -0700194 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700195 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700196}
197
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100198TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700199 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
200 .setId(DisplayModeId(111))
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400201 .setPhysicalDisplayId(kDisplayId1)
Ady Abraham690f4612021-07-01 23:24:03 -0700202 .setVsyncPeriod(111111)
203 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700204
205 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100206 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700207 ConnectionHandle invalidHandle = {.id = 123};
Ady Abrahamace3d052022-11-17 16:25:05 -0800208 EXPECT_NO_FATAL_FAILURE(
209 mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle,
210 {90_Hz, ftl::as_non_null(mode)}));
Ady Abraham690f4612021-07-01 23:24:03 -0700211 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700212}
213
Ady Abraham899dcdb2021-06-15 16:56:21 -0700214TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700215 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
216 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
217 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800218
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700219 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800220
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700221 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800222}
223
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200224MATCHER(Is120Hz, "") {
Ady Abrahamace3d052022-11-17 16:25:05 -0800225 return isApproxEqual(arg.front().mode.fps, 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200226}
227
228TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400229 mScheduler->registerDisplay(kDisplayId1,
230 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
231 kDisplay1Mode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200232
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800233 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
234 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200235
236 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
237
Rachel Lee6a9731d2022-06-06 17:08:14 -0700238 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500239 mScheduler->setDisplayPowerMode(kPowerModeOn);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200240
241 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700242 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200243
ramindani69b58e82022-09-26 16:48:36 -0700244 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(Is120Hz())).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200245 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800246
247 // No-op if layer requirements have not changed.
ramindani69b58e82022-09-26 16:48:36 -0700248 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800249 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200250}
251
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400252TEST_F(SchedulerTest, chooseDisplayModesSingleDisplay) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400253 mScheduler->registerDisplay(kDisplayId1,
254 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
255 kDisplay1Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700256
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400257 std::vector<RefreshRateSelector::LayerRequirement> layers =
258 std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}});
ramindani69b58e82022-09-26 16:48:36 -0700259 mScheduler->setContentRequirements(layers);
260 GlobalSignals globalSignals = {.idle = true};
261 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
262
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400263 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
264
265 auto modeChoices = mScheduler->chooseDisplayModes();
266 ASSERT_EQ(1u, modeChoices.size());
267
268 auto choice = modeChoices.get(kDisplayId1);
269 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800270 EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700271
272 globalSignals = {.idle = false};
273 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400274
275 modeChoices = mScheduler->chooseDisplayModes();
276 ASSERT_EQ(1u, modeChoices.size());
277
278 choice = modeChoices.get(kDisplayId1);
279 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800280 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700281
282 globalSignals = {.touch = true};
283 mScheduler->replaceTouchTimer(10);
284 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700285
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400286 modeChoices = mScheduler->chooseDisplayModes();
287 ASSERT_EQ(1u, modeChoices.size());
288
289 choice = modeChoices.get(kDisplayId1);
290 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800291 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700292}
293
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400294TEST_F(SchedulerTest, chooseDisplayModesMultipleDisplays) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400295 mScheduler->registerDisplay(kDisplayId1,
296 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
297 kDisplay1Mode60->getId()));
298 mScheduler->registerDisplay(kDisplayId2,
299 std::make_shared<RefreshRateSelector>(kDisplay2Modes,
300 kDisplay2Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700301
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400302 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
303 TestableScheduler::DisplayModeChoiceMap expectedChoices;
ramindani69b58e82022-09-26 16:48:36 -0700304
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400305 {
306 const GlobalSignals globalSignals = {.idle = true};
307 expectedChoices =
308 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800309 DisplayModeChoice>(kDisplayId1,
310 FrameRateMode{60_Hz, kDisplay1Mode60},
311 globalSignals)(kDisplayId2,
312 FrameRateMode{60_Hz,
313 kDisplay2Mode60},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400314 globalSignals);
315
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400316 std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f},
317 {.weight = 1.f}};
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400318 mScheduler->setContentRequirements(layers);
319 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
320
321 const auto actualChoices = mScheduler->chooseDisplayModes();
322 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700323 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400324 {
325 const GlobalSignals globalSignals = {.idle = false};
326 expectedChoices =
327 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800328 DisplayModeChoice>(kDisplayId1,
329 FrameRateMode{120_Hz, kDisplay1Mode120},
330 globalSignals)(kDisplayId2,
331 FrameRateMode{120_Hz,
332 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400333 globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700334
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400335 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700336
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400337 const auto actualChoices = mScheduler->chooseDisplayModes();
338 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700339 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400340 {
341 const GlobalSignals globalSignals = {.touch = true};
342 mScheduler->replaceTouchTimer(10);
343 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700344
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400345 expectedChoices =
346 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800347 DisplayModeChoice>(kDisplayId1,
348 FrameRateMode{120_Hz, kDisplay1Mode120},
349 globalSignals)(kDisplayId2,
350 FrameRateMode{120_Hz,
351 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400352 globalSignals);
353
354 const auto actualChoices = mScheduler->chooseDisplayModes();
355 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700356 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400357 {
358 // This display does not support 120 Hz, so we should choose 60 Hz despite the touch signal.
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400359 mScheduler
360 ->registerDisplay(kDisplayId3,
361 std::make_shared<RefreshRateSelector>(kDisplay3Modes,
362 kDisplay3Mode60->getId()));
Dominik Laskowski327d6092022-10-11 18:05:08 -0400363
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400364 const GlobalSignals globalSignals = {.touch = true};
365 mScheduler->replaceTouchTimer(10);
366 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani7c487282022-10-10 16:17:51 -0700367
Ady Abrahamace3d052022-11-17 16:25:05 -0800368 expectedChoices = ftl::init::map<
369 const PhysicalDisplayId&,
370 DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60},
371 globalSignals)(kDisplayId2,
372 FrameRateMode{60_Hz, kDisplay2Mode60},
373 globalSignals)(kDisplayId3,
374 FrameRateMode{60_Hz,
375 kDisplay3Mode60},
376 globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400377
378 const auto actualChoices = mScheduler->chooseDisplayModes();
379 EXPECT_EQ(expectedChoices, actualChoices);
ramindani7c487282022-10-10 16:17:51 -0700380 }
ramindani69b58e82022-09-26 16:48:36 -0700381}
382
Dominik Laskowski068173d2021-08-11 17:22:59 -0700383} // namespace android::scheduler