blob: 53e49eb0d4cb29ea9fb146469bbc335a88428d59 [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"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070024#include "Scheduler/RefreshRateConfigs.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
Dominik Laskowskif1833852021-03-23 15:06:50 -070044constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = PhysicalDisplayId::fromPort(255u);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080045
Ana Krulec0c8cd522018-08-31 12:27:28 -070046class SchedulerTest : public testing::Test {
47protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080048 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070049 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080050 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abrahamd11bade2022-08-01 16:18:03 -070051 : EventThreadConnection(eventThread, /*callingUid*/ static_cast<uid_t>(0),
52 ResyncCallback()) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070053 ~MockEventThreadConnection() = default;
54
Huihong Luo6fac5232021-11-22 16:05:23 -080055 MOCK_METHOD1(stealReceiveChannel, binder::Status(gui::BitTube* outChannel));
56 MOCK_METHOD1(setVsyncRate, binder::Status(int count));
57 MOCK_METHOD0(requestNextVsync, binder::Status());
Ana Krulec0c8cd522018-08-31 12:27:28 -070058 };
59
Ana Krulec0c8cd522018-08-31 12:27:28 -070060 SchedulerTest();
Ana Krulec0c8cd522018-08-31 12:27:28 -070061
Dominik Laskowskib0054a22022-03-03 09:03:06 -080062 static inline const DisplayModePtr kMode60 = createDisplayMode(DisplayModeId(0), 60_Hz);
63 static inline const DisplayModePtr kMode120 = createDisplayMode(DisplayModeId(1), 120_Hz);
Marin Shalamanov2cde1002021-06-08 19:50:10 +020064
Dominik Laskowski068173d2021-08-11 17:22:59 -070065 std::shared_ptr<RefreshRateConfigs> mConfigs =
Dominik Laskowskib0054a22022-03-03 09:03:06 -080066 std::make_shared<RefreshRateConfigs>(makeModes(kMode60), kMode60->getId());
Dominik Laskowski983f2b52020-06-25 16:54:06 -070067
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070068 mock::SchedulerCallback mSchedulerCallback;
Ady Abrahama0a16272021-03-03 15:23:35 -080069 TestableScheduler* mScheduler = new TestableScheduler{mConfigs, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070070
Dominik Laskowski068173d2021-08-11 17:22:59 -070071 ConnectionHandle mConnectionHandle;
72 MockEventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070073 sp<MockEventThreadConnection> mEventThreadConnection;
Ady Abraham564f9de2021-02-03 18:34:33 -080074
75 TestableSurfaceFlinger mFlinger;
Ana Krulec0c8cd522018-08-31 12:27:28 -070076};
77
78SchedulerTest::SchedulerTest() {
Dominik Laskowski068173d2021-08-11 17:22:59 -070079 auto eventThread = std::make_unique<MockEventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070080 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080081 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
82
Ady Abrahamd11bade2022-08-01 16:18:03 -070083 mEventThreadConnection = sp<MockEventThreadConnection>::make(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070084
85 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
86 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -070087 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -070088 .WillRepeatedly(Return(mEventThreadConnection));
89
Ady Abrahama0a16272021-03-03 15:23:35 -080090 mConnectionHandle = mScheduler->createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -070091 EXPECT_TRUE(mConnectionHandle);
Ady Abrahama0a16272021-03-03 15:23:35 -080092
93 mFlinger.resetScheduler(mScheduler);
Ana Krulec0c8cd522018-08-31 12:27:28 -070094}
95
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070096} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -070097
Ana Krulec0c8cd522018-08-31 12:27:28 -070098TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski068173d2021-08-11 17:22:59 -070099 ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700100
Ady Abrahama0a16272021-03-03 15:23:35 -0800101 const sp<IDisplayEventConnection> connection = mScheduler->createDisplayEventConnection(handle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700102
Dominik Laskowski98041832019-08-01 18:35:59 -0700103 EXPECT_FALSE(connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800104 EXPECT_FALSE(mScheduler->getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700105
106 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
107 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800108 mScheduler->onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700109
110 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800111 mScheduler->onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700112
113 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800114 mScheduler->onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115
Dominik Laskowski98041832019-08-01 18:35:59 -0700116 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700117 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800118 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700119 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700120
Ady Abraham9c53ee72020-07-22 21:16:18 -0700121 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800122 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700123}
124
125TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700126 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800127 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700128
Dominik Laskowski98041832019-08-01 18:35:59 -0700129 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800130 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700131
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800132 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800133 mScheduler->onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700134
135 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800136 mScheduler->onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700137
138 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800139 mScheduler->onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140
Dominik Laskowski98041832019-08-01 18:35:59 -0700141 std::string output("dump");
142 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800143 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700144 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700145
Ady Abraham9c53ee72020-07-22 21:16:18 -0700146 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800147 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800148
149 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700150 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800151 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152}
Dominik Laskowski98041832019-08-01 18:35:59 -0700153
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200154TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
155 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700156 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700157
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200158 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700159 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Ady Abrahama0a16272021-03-03 15:23:35 -0800160 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700161 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700162
Rachel Lee6a9731d2022-06-06 17:08:14 -0700163 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
164 mScheduler->setDisplayPowerMode(kPowerModeOn);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700165
166 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700167 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700168
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800169 EXPECT_CALL(mSchedulerCallback, requestDisplayMode(_, _)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800170 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700171}
172
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200173TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700174 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700175 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700176 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200177
Ady Abraham3efa3942021-06-24 19:01:25 -0700178 mScheduler->setRefreshRateConfigs(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800179 std::make_shared<RefreshRateConfigs>(makeModes(kMode60, kMode120), kMode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200180
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700181 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200182 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700183 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200184}
185
Dominik Laskowski068173d2021-08-11 17:22:59 -0700186TEST_F(SchedulerTest, dispatchCachedReportedMode) {
187 mScheduler->clearCachedReportedMode();
188
Ady Abraham690f4612021-07-01 23:24:03 -0700189 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700190 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700191}
192
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100193TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700194 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
195 .setId(DisplayModeId(111))
196 .setPhysicalDisplayId(PHYSICAL_DISPLAY_ID)
197 .setVsyncPeriod(111111)
198 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700199
200 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100201 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700202 ConnectionHandle invalidHandle = {.id = 123};
Ady Abraham690f4612021-07-01 23:24:03 -0700203 EXPECT_NO_FATAL_FAILURE(mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle, mode));
204 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700205}
206
Ady Abraham899dcdb2021-06-15 16:56:21 -0700207TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700208 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
209 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
210 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800211
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700212 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800213
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700214 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800215}
216
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200217MATCHER(Is120Hz, "") {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800218 return isApproxEqual(arg->getFps(), 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200219}
220
221TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Ady Abraham3efa3942021-06-24 19:01:25 -0700222 mScheduler->setRefreshRateConfigs(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800223 std::make_shared<RefreshRateConfigs>(makeModes(kMode60, kMode120), kMode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200224
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800225 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
226 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200227
228 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
229
Rachel Lee6a9731d2022-06-06 17:08:14 -0700230 constexpr hal::PowerMode kPowerModeOn = hal::PowerMode::ON;
231 mScheduler->setDisplayPowerMode(kPowerModeOn);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200232
233 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700234 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200235
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800236 EXPECT_CALL(mSchedulerCallback, requestDisplayMode(Is120Hz(), _)).Times(1);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200237 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800238
239 // No-op if layer requirements have not changed.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800240 EXPECT_CALL(mSchedulerCallback, requestDisplayMode(_, _)).Times(0);
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800241 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200242}
243
Dominik Laskowski068173d2021-08-11 17:22:59 -0700244} // namespace android::scheduler