blob: a992a91226f9fdd0b8ad8b94b63f9e44b10e7b3e [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"
Ana Krulec0c8cd522018-08-31 12:27:28 -070027#include "mock/MockEventThread.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070028#include "mock/MockLayer.h"
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070029#include "mock/MockSchedulerCallback.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070030
Dominik Laskowski068173d2021-08-11 17:22:59 -070031namespace android::scheduler {
32
Ana Krulec0c8cd522018-08-31 12:27:28 -070033using testing::_;
34using testing::Return;
35
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070036namespace {
Ana Krulec0c8cd522018-08-31 12:27:28 -070037
Dominik Laskowski068173d2021-08-11 17:22:59 -070038using MockEventThread = android::mock::EventThread;
39using MockLayer = android::mock::MockLayer;
40
Dominik Laskowskif1833852021-03-23 15:06:50 -070041constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = PhysicalDisplayId::fromPort(255u);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080042
Ana Krulec0c8cd522018-08-31 12:27:28 -070043class SchedulerTest : public testing::Test {
44protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080045 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070046 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080047 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abraham62f216c2020-10-13 19:07:23 -070048 : EventThreadConnection(eventThread, /*callingUid=*/0, ResyncCallback()) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070049 ~MockEventThreadConnection() = default;
50
Huihong Luo6fac5232021-11-22 16:05:23 -080051 MOCK_METHOD1(stealReceiveChannel, binder::Status(gui::BitTube* outChannel));
52 MOCK_METHOD1(setVsyncRate, binder::Status(int count));
53 MOCK_METHOD0(requestNextVsync, binder::Status());
Ana Krulec0c8cd522018-08-31 12:27:28 -070054 };
55
Ana Krulec0c8cd522018-08-31 12:27:28 -070056 SchedulerTest();
Ana Krulec0c8cd522018-08-31 12:27:28 -070057
Marin Shalamanov2cde1002021-06-08 19:50:10 +020058 const DisplayModePtr mode60 = DisplayMode::Builder(0)
59 .setId(DisplayModeId(0))
Ady Abraham5e7ee862021-06-23 17:43:41 -070060 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070061 .setVsyncPeriod((60_Hz).getPeriodNsecs())
Marin Shalamanov2cde1002021-06-08 19:50:10 +020062 .setGroup(0)
63 .build();
64 const DisplayModePtr mode120 = DisplayMode::Builder(1)
65 .setId(DisplayModeId(1))
Ady Abraham5e7ee862021-06-23 17:43:41 -070066 .setPhysicalDisplayId(PhysicalDisplayId::fromPort(0))
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070067 .setVsyncPeriod((120_Hz).getPeriodNsecs())
Marin Shalamanov2cde1002021-06-08 19:50:10 +020068 .setGroup(0)
69 .build();
70
Dominik Laskowski068173d2021-08-11 17:22:59 -070071 std::shared_ptr<RefreshRateConfigs> mConfigs =
72 std::make_shared<RefreshRateConfigs>(DisplayModes{mode60}, mode60->getId());
Dominik Laskowski983f2b52020-06-25 16:54:06 -070073
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070074 mock::SchedulerCallback mSchedulerCallback;
Ady Abrahama0a16272021-03-03 15:23:35 -080075 TestableScheduler* mScheduler = new TestableScheduler{mConfigs, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070076
Dominik Laskowski068173d2021-08-11 17:22:59 -070077 ConnectionHandle mConnectionHandle;
78 MockEventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070079 sp<MockEventThreadConnection> mEventThreadConnection;
Ady Abraham564f9de2021-02-03 18:34:33 -080080
81 TestableSurfaceFlinger mFlinger;
Ana Krulec0c8cd522018-08-31 12:27:28 -070082};
83
84SchedulerTest::SchedulerTest() {
Dominik Laskowski068173d2021-08-11 17:22:59 -070085 auto eventThread = std::make_unique<MockEventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070086 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080087 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
88
89 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070090
91 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
92 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -070093 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -070094 .WillRepeatedly(Return(mEventThreadConnection));
95
Ady Abrahama0a16272021-03-03 15:23:35 -080096 mConnectionHandle = mScheduler->createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -070097 EXPECT_TRUE(mConnectionHandle);
Ady Abrahama0a16272021-03-03 15:23:35 -080098
99 mFlinger.resetScheduler(mScheduler);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700100}
101
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700102} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -0700103
Ana Krulec0c8cd522018-08-31 12:27:28 -0700104TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski068173d2021-08-11 17:22:59 -0700105 ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106
Ady Abrahama0a16272021-03-03 15:23:35 -0800107 const sp<IDisplayEventConnection> connection = mScheduler->createDisplayEventConnection(handle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700108
Dominik Laskowski98041832019-08-01 18:35:59 -0700109 EXPECT_FALSE(connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800110 EXPECT_FALSE(mScheduler->getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700111
112 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
113 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800114 mScheduler->onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700115
116 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800117 mScheduler->onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700118
119 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800120 mScheduler->onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700121
Dominik Laskowski98041832019-08-01 18:35:59 -0700122 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700123 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800124 mScheduler->dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700125 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700126
Ady Abraham9c53ee72020-07-22 21:16:18 -0700127 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800128 mScheduler->setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700129}
130
131TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700132 const sp<IDisplayEventConnection> connection =
Ady Abrahama0a16272021-03-03 15:23:35 -0800133 mScheduler->createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700134
Dominik Laskowski98041832019-08-01 18:35:59 -0700135 ASSERT_EQ(mEventThreadConnection, connection);
Ady Abrahama0a16272021-03-03 15:23:35 -0800136 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700137
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800138 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800139 mScheduler->onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700140
141 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800142 mScheduler->onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700143
144 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800145 mScheduler->onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700146
Dominik Laskowski98041832019-08-01 18:35:59 -0700147 std::string output("dump");
148 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800149 mScheduler->dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700150 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151
Ady Abraham9c53ee72020-07-22 21:16:18 -0700152 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
Ady Abrahama0a16272021-03-03 15:23:35 -0800153 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800154
155 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700156 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Ady Abrahama0a16272021-03-03 15:23:35 -0800157 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700158}
Dominik Laskowski98041832019-08-01 18:35:59 -0700159
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200160TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
161 // The layer is registered at creation time and deregistered at destruction time.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700162 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700163
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200164 // recordLayerHistory should be a noop
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700165 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Ady Abrahama0a16272021-03-03 15:23:35 -0800166 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700167 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700168
169 constexpr bool kPowerStateNormal = true;
Ady Abrahama0a16272021-03-03 15:23:35 -0800170 mScheduler->setDisplayPowerState(kPowerStateNormal);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700171
172 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700173 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700174
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700175 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0);
Ady Abrahama0a16272021-03-03 15:23:35 -0800176 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700177}
178
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200179TEST_F(SchedulerTest, updateDisplayModes) {
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700180 ASSERT_EQ(0u, mScheduler->layerHistorySize());
Dominik Laskowski068173d2021-08-11 17:22:59 -0700181 sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700182 ASSERT_EQ(1u, mScheduler->layerHistorySize());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200183
Ady Abraham3efa3942021-06-24 19:01:25 -0700184 mScheduler->setRefreshRateConfigs(
Dominik Laskowski068173d2021-08-11 17:22:59 -0700185 std::make_shared<RefreshRateConfigs>(DisplayModes{mode60, mode120}, mode60->getId()));
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200186
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700187 ASSERT_EQ(0u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200188 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
Dominik Laskowski9c93d602021-10-07 19:38:26 -0700189 ASSERT_EQ(1u, mScheduler->getNumActiveLayers());
Marin Shalamanov2cde1002021-06-08 19:50:10 +0200190}
191
Dominik Laskowski068173d2021-08-11 17:22:59 -0700192TEST_F(SchedulerTest, dispatchCachedReportedMode) {
193 mScheduler->clearCachedReportedMode();
194
Ady Abraham690f4612021-07-01 23:24:03 -0700195 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Dominik Laskowski068173d2021-08-11 17:22:59 -0700196 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
Ana Krulec6ddd2612020-09-24 13:06:33 -0700197}
198
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100199TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
Ady Abraham690f4612021-07-01 23:24:03 -0700200 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
201 .setId(DisplayModeId(111))
202 .setPhysicalDisplayId(PHYSICAL_DISPLAY_ID)
203 .setVsyncPeriod(111111)
204 .build();
Ana Krulec6ddd2612020-09-24 13:06:33 -0700205
206 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100207 // onModeChange is called.
Dominik Laskowski068173d2021-08-11 17:22:59 -0700208 ConnectionHandle invalidHandle = {.id = 123};
Ady Abraham690f4612021-07-01 23:24:03 -0700209 EXPECT_NO_FATAL_FAILURE(mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle, mode));
210 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700211}
212
Ady Abraham899dcdb2021-06-15 16:56:21 -0700213TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700214 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 30ms));
215 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(90_Hz, 30ms));
216 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(120_Hz, 30ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800217
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700218 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 40ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800219
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700220 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(60_Hz, 10ms));
Ady Abraham564f9de2021-02-03 18:34:33 -0800221}
222
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200223MATCHER(Is120Hz, "") {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700224 return isApproxEqual(arg.getFps(), 120_Hz);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200225}
226
227TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
Ady Abraham3efa3942021-06-24 19:01:25 -0700228 mScheduler->setRefreshRateConfigs(
Dominik Laskowski068173d2021-08-11 17:22:59 -0700229 std::make_shared<RefreshRateConfigs>(DisplayModes{mode60, mode120}, mode60->getId()));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200230
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800231 const sp<MockLayer> layer = sp<MockLayer>::make(mFlinger.flinger());
232 EXPECT_CALL(*layer, isVisible()).WillOnce(Return(true));
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200233
234 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
235
236 constexpr bool kPowerStateNormal = true;
237 mScheduler->setDisplayPowerState(kPowerStateNormal);
238
239 constexpr uint32_t kDisplayArea = 999'999;
Ady Abrahamed3290f2021-05-17 15:12:14 -0700240 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200241
242 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(Is120Hz(), _)).Times(1);
243 mScheduler->chooseRefreshRateForContent();
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800244
245 // No-op if layer requirements have not changed.
246 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0);
247 mScheduler->chooseRefreshRateForContent();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200248}
249
Dominik Laskowski068173d2021-08-11 17:22:59 -0700250} // namespace android::scheduler