blob: f0dd06d75825c7637805dd51418853e325148b05 [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 III31d41412022-11-18 16:42:53 -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
126 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800127 mScheduler->onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700128
129 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800130 mScheduler->onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700131
Dominik Laskowski98041832019-08-01 18:35:59 -0700132 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800134 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700135 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700136
Ady Abraham9c53ee72020-07-22 21:16:18 -0700137 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800138 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700139}
140
141TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700142 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800143 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700144
Dominik Laskowski98041832019-08-01 18:35:59 -0700145 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800146 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700147
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400148 EXPECT_CALL(*mEventThread, onHotplugReceived(kDisplayId1, false)).Times(1);
149 mScheduler->onHotplugReceived(mConnectionHandle, kDisplayId1, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700150
151 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800152 mScheduler->onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700153
154 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800155 mScheduler->onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700156
Dominik Laskowski98041832019-08-01 18:35:59 -0700157 std::string output("dump");
158 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800159 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700160 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700161
Ady Abraham9c53ee72020-07-22 21:16:18 -0700162 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800163 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800164
165 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700166 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800167 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700168}
Dominik Laskowski98041832019-08-01 18:35:59 -0700169
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200170TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
171 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700172 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700173
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200174 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700175 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Ady Abrahama0a16272021-03-03 15:23:35 -0800176 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700177 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700178
Rachel Lee6a9731d2022-06-06 17:08:14 -0700179 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins III31d41412022-11-18 16:42:53 -0500180 FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700181
182 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700183 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700184
ramindani69b58e82022-09-26 16:48:36 -0700185 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800186 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700187}
188
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200189TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700190 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700191 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700192 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200193
Dominik Laskowski596a2562022-10-28 11:26:12 -0400194 // Replace `mSelector` with a new `RefreshRateSelector` that has different display modes.
195 mScheduler->registerDisplay(kDisplayId1,
196 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
197 kDisplay1Mode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200198
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700199 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200200 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700201 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200202}
203
Dominik Laskowski068173d2021-08-11 17:22:59 -0700204TEST_F(SchedulerTest, dispatchCachedReportedMode) {
205 mScheduler->clearCachedReportedMode();
206
Ady Abraham690f4612021-07-01 23:24:03 -0700207 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700208 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700209}
210
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100211TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700212 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
213 .setId(DisplayModeId(111))
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400214 .setPhysicalDisplayId(kDisplayId1)
Ady Abraham690f4612021-07-01 23:24:03 -0700215 .setVsyncPeriod(111111)
216 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700217
218 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100219 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700220 ConnectionHandle invalidHandle = {.id = 123};
Ady Abrahamace3d052022-11-17 16:25:05 -0800221 EXPECT_NO_FATAL_FAILURE(
222 mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle,
223 {90_Hz, ftl::as_non_null(mode)}));
Ady Abraham690f4612021-07-01 23:24:03 -0700224 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700225}
226
Ady Abraham899dcdb2021-06-15 16:56:21 -0700227TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700228 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
229 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
230 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800231
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700232 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800233
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700234 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800235}
236
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200237MATCHER(Is120Hz, "") {
Ady Abrahamace3d052022-11-17 16:25:05 -0800238 return isApproxEqual(arg.front().mode.fps, 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200239}
240
241TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Dominik Laskowski596a2562022-10-28 11:26:12 -0400242 mScheduler->registerDisplay(kDisplayId1,
243 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
244 kDisplay1Mode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200245
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800246 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
247 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200248
249 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
250
Rachel Lee6a9731d2022-06-06 17:08:14 -0700251 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
Leon Scroggins III31d41412022-11-18 16:42:53 -0500252 FTL_FAKE_GUARD(kMainThreadContext, mScheduler->setDisplayPowerMode(kDisplayId1, kPowerModeOn));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200253
254 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700255 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200256
ramindani69b58e82022-09-26 16:48:36 -0700257 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(Is120Hz())).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200258 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800259
260 // No-op if layer requirements have not changed.
ramindani69b58e82022-09-26 16:48:36 -0700261 EXPECT_CALL(mSchedulerCallback, requestDisplayModes(_)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800262 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200263}
264
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400265TEST_F(SchedulerTest, chooseDisplayModesSingleDisplay) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400266 mScheduler->registerDisplay(kDisplayId1,
267 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
268 kDisplay1Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700269
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400270 std::vector<RefreshRateSelector::LayerRequirement> layers =
271 std::vector<RefreshRateSelector::LayerRequirement>({{.weight = 1.f}, {.weight = 1.f}});
ramindani69b58e82022-09-26 16:48:36 -0700272 mScheduler->setContentRequirements(layers);
273 GlobalSignals globalSignals = {.idle = true};
274 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
275
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400276 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
277
278 auto modeChoices = mScheduler->chooseDisplayModes();
279 ASSERT_EQ(1u, modeChoices.size());
280
281 auto choice = modeChoices.get(kDisplayId1);
282 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800283 EXPECT_EQ(choice->get(), DisplayModeChoice({60_Hz, kDisplay1Mode60}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700284
285 globalSignals = {.idle = false};
286 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400287
288 modeChoices = mScheduler->chooseDisplayModes();
289 ASSERT_EQ(1u, modeChoices.size());
290
291 choice = modeChoices.get(kDisplayId1);
292 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800293 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700294
295 globalSignals = {.touch = true};
296 mScheduler->replaceTouchTimer(10);
297 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700298
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400299 modeChoices = mScheduler->chooseDisplayModes();
300 ASSERT_EQ(1u, modeChoices.size());
301
302 choice = modeChoices.get(kDisplayId1);
303 ASSERT_TRUE(choice);
Ady Abrahamace3d052022-11-17 16:25:05 -0800304 EXPECT_EQ(choice->get(), DisplayModeChoice({120_Hz, kDisplay1Mode120}, globalSignals));
ramindani69b58e82022-09-26 16:48:36 -0700305}
306
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400307TEST_F(SchedulerTest, chooseDisplayModesMultipleDisplays) {
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400308 mScheduler->registerDisplay(kDisplayId1,
309 std::make_shared<RefreshRateSelector>(kDisplay1Modes,
310 kDisplay1Mode60->getId()));
311 mScheduler->registerDisplay(kDisplayId2,
312 std::make_shared<RefreshRateSelector>(kDisplay2Modes,
313 kDisplay2Mode60->getId()));
ramindani69b58e82022-09-26 16:48:36 -0700314
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400315 using DisplayModeChoice = TestableScheduler::DisplayModeChoice;
316 TestableScheduler::DisplayModeChoiceMap expectedChoices;
ramindani69b58e82022-09-26 16:48:36 -0700317
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400318 {
319 const GlobalSignals globalSignals = {.idle = true};
320 expectedChoices =
321 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800322 DisplayModeChoice>(kDisplayId1,
323 FrameRateMode{60_Hz, kDisplay1Mode60},
324 globalSignals)(kDisplayId2,
325 FrameRateMode{60_Hz,
326 kDisplay2Mode60},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400327 globalSignals);
328
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400329 std::vector<RefreshRateSelector::LayerRequirement> layers = {{.weight = 1.f},
330 {.weight = 1.f}};
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400331 mScheduler->setContentRequirements(layers);
332 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
333
334 const auto actualChoices = mScheduler->chooseDisplayModes();
335 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700336 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400337 {
338 const GlobalSignals globalSignals = {.idle = false};
339 expectedChoices =
340 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800341 DisplayModeChoice>(kDisplayId1,
342 FrameRateMode{120_Hz, kDisplay1Mode120},
343 globalSignals)(kDisplayId2,
344 FrameRateMode{120_Hz,
345 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400346 globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700347
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400348 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700349
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400350 const auto actualChoices = mScheduler->chooseDisplayModes();
351 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700352 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400353 {
354 const GlobalSignals globalSignals = {.touch = true};
355 mScheduler->replaceTouchTimer(10);
356 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani69b58e82022-09-26 16:48:36 -0700357
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400358 expectedChoices =
359 ftl::init::map<const PhysicalDisplayId&,
Ady Abrahamace3d052022-11-17 16:25:05 -0800360 DisplayModeChoice>(kDisplayId1,
361 FrameRateMode{120_Hz, kDisplay1Mode120},
362 globalSignals)(kDisplayId2,
363 FrameRateMode{120_Hz,
364 kDisplay2Mode120},
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400365 globalSignals);
366
367 const auto actualChoices = mScheduler->chooseDisplayModes();
368 EXPECT_EQ(expectedChoices, actualChoices);
ramindani69b58e82022-09-26 16:48:36 -0700369 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400370 {
371 // This display does not support 120 Hz, so we should choose 60 Hz despite the touch signal.
Dominik Laskowskib5a094b2022-10-27 12:00:12 -0400372 mScheduler
373 ->registerDisplay(kDisplayId3,
374 std::make_shared<RefreshRateSelector>(kDisplay3Modes,
375 kDisplay3Mode60->getId()));
Dominik Laskowski327d6092022-10-11 18:05:08 -0400376
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400377 const GlobalSignals globalSignals = {.touch = true};
378 mScheduler->replaceTouchTimer(10);
379 mScheduler->setTouchStateAndIdleTimerPolicy(globalSignals);
ramindani7c487282022-10-10 16:17:51 -0700380
Ady Abrahamace3d052022-11-17 16:25:05 -0800381 expectedChoices = ftl::init::map<
382 const PhysicalDisplayId&,
383 DisplayModeChoice>(kDisplayId1, FrameRateMode{60_Hz, kDisplay1Mode60},
384 globalSignals)(kDisplayId2,
385 FrameRateMode{60_Hz, kDisplay2Mode60},
386 globalSignals)(kDisplayId3,
387 FrameRateMode{60_Hz,
388 kDisplay3Mode60},
389 globalSignals);
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400390
391 const auto actualChoices = mScheduler->chooseDisplayModes();
392 EXPECT_EQ(expectedChoices, actualChoices);
ramindani7c487282022-10-10 16:17:51 -0700393 }
ramindani69b58e82022-09-26 16:48:36 -0700394}
395
Dominik Laskowski068173d2021-08-11 17:22:59 -0700396} // namespace android::scheduler