blob: e46a270ea8dfe8450bfd9f6fa290f77fe1025b85 [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
31using testing::_;
32using testing::Return;
33
34namespace android {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070035namespace {
Ana Krulec0c8cd522018-08-31 12:27:28 -070036
Marin Shalamanova524a092020-07-27 21:39:55 +020037constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID(999);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080038
Ana Krulec0c8cd522018-08-31 12:27:28 -070039class SchedulerTest : public testing::Test {
40protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080041 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070042 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080043 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abraham62f216c2020-10-13 19:07:23 -070044 : EventThreadConnection(eventThread, /*callingUid=*/0, ResyncCallback()) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070045 ~MockEventThreadConnection() = default;
46
47 MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel));
48 MOCK_METHOD1(setVsyncRate, status_t(uint32_t count));
49 MOCK_METHOD0(requestNextVsync, void());
50 };
51
Ana Krulec0c8cd522018-08-31 12:27:28 -070052 SchedulerTest();
Ana Krulec0c8cd522018-08-31 12:27:28 -070053
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010054 const scheduler::RefreshRateConfigs
Marin Shalamanova7fe3042021-01-29 21:02:08 +010055 mConfigs{{DisplayMode::Builder(0).setVsyncPeriod(16'666'667).setGroup(0).build()},
Marin Shalamanov23c44202020-12-22 19:09:20 +010056 DisplayModeId(0)};
Dominik Laskowski983f2b52020-06-25 16:54:06 -070057
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070058 mock::SchedulerCallback mSchedulerCallback;
59
60 // The scheduler should initially disable VSYNC.
61 struct ExpectDisableVsync {
62 ExpectDisableVsync(mock::SchedulerCallback& callback) {
63 EXPECT_CALL(callback, setVsyncEnabled(false)).Times(1);
64 }
65 } mExpectDisableVsync{mSchedulerCallback};
66
Marin Shalamanov27fa3de2020-11-20 16:22:48 +010067 TestableScheduler mScheduler{mConfigs, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070068
69 Scheduler::ConnectionHandle mConnectionHandle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070070 mock::EventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070071 sp<MockEventThreadConnection> mEventThreadConnection;
Ady Abraham564f9de2021-02-03 18:34:33 -080072
73 TestableSurfaceFlinger mFlinger;
Ana Krulec0c8cd522018-08-31 12:27:28 -070074};
75
76SchedulerTest::SchedulerTest() {
Dominik Laskowski98041832019-08-01 18:35:59 -070077 auto eventThread = std::make_unique<mock::EventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070078 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080079 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
80
81 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070082
83 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
84 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -070085 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -070086 .WillRepeatedly(Return(mEventThreadConnection));
87
Dominik Laskowski983f2b52020-06-25 16:54:06 -070088 mConnectionHandle = mScheduler.createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -070089 EXPECT_TRUE(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -070090}
91
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070092} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -070093
Ana Krulec0c8cd522018-08-31 12:27:28 -070094TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski98041832019-08-01 18:35:59 -070095 Scheduler::ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070096
Ady Abraham62f216c2020-10-13 19:07:23 -070097 const sp<IDisplayEventConnection> connection = mScheduler.createDisplayEventConnection(handle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -070098
Dominik Laskowski98041832019-08-01 18:35:59 -070099 EXPECT_FALSE(connection);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700100 EXPECT_FALSE(mScheduler.getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700101
102 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
103 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700104 mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700105
106 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700107 mScheduler.onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700108
109 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700110 mScheduler.onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700111
Dominik Laskowski98041832019-08-01 18:35:59 -0700112 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700114 mScheduler.dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700115 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116
Ady Abraham9c53ee72020-07-22 21:16:18 -0700117 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
118 mScheduler.setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700119}
120
121TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700122 const sp<IDisplayEventConnection> connection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700123 mScheduler.createDisplayEventConnection(mConnectionHandle);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700124
Dominik Laskowski98041832019-08-01 18:35:59 -0700125 ASSERT_EQ(mEventThreadConnection, connection);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700126 EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700127
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800128 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700129 mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700130
131 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700132 mScheduler.onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133
134 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700135 mScheduler.onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700136
Dominik Laskowski98041832019-08-01 18:35:59 -0700137 std::string output("dump");
138 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700139 mScheduler.dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700140 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700141
Ady Abraham9c53ee72020-07-22 21:16:18 -0700142 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
143 mScheduler.setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800144
145 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700146 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700147 EXPECT_EQ(kEventConnections, mScheduler.getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700148}
Dominik Laskowski98041832019-08-01 18:35:59 -0700149
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700150TEST_F(SchedulerTest, noLayerHistory) {
151 // Layer history should not be created if there is a single config.
152 ASSERT_FALSE(mScheduler.hasLayerHistory());
153
154 TestableSurfaceFlinger flinger;
155 mock::MockLayer layer(flinger.flinger());
156
157 // Content detection should be no-op.
158 mScheduler.registerLayer(&layer);
159 mScheduler.recordLayerHistory(&layer, 0, LayerHistory::LayerUpdateType::Buffer);
160
161 constexpr bool kPowerStateNormal = true;
162 mScheduler.setDisplayPowerState(kPowerStateNormal);
163
164 constexpr uint32_t kDisplayArea = 999'999;
165 mScheduler.onPrimaryDisplayAreaChanged(kDisplayArea);
166
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700167 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700168 mScheduler.chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700169}
170
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100171TEST_F(SchedulerTest, testDispatchCachedReportedMode) {
Ana Krulec6ddd2612020-09-24 13:06:33 -0700172 // If the optional fields are cleared, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100173 // onModeChange is called.
Ana Krulec6ddd2612020-09-24 13:06:33 -0700174 mScheduler.clearOptionalFieldsInFeatures();
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100175 EXPECT_NO_FATAL_FAILURE(mScheduler.dispatchCachedReportedMode());
176 EXPECT_CALL(*mEventThread, onModeChanged(_, _, _)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700177}
178
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100179TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
180 DisplayModeId modeId = DisplayModeId(111);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700181 nsecs_t vsyncPeriod = 111111;
182
183 // If the handle is incorrect, the function should return before
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100184 // onModeChange is called.
Ana Krulec6ddd2612020-09-24 13:06:33 -0700185 Scheduler::ConnectionHandle invalidHandle = {.id = 123};
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100186 EXPECT_NO_FATAL_FAILURE(mScheduler.onNonPrimaryDisplayModeChanged(invalidHandle,
187 PHYSICAL_DISPLAY_ID, modeId,
188 vsyncPeriod));
189 EXPECT_CALL(*mEventThread, onModeChanged(_, _, _)).Times(0);
Ana Krulec6ddd2612020-09-24 13:06:33 -0700190}
191
Ady Abraham564f9de2021-02-03 18:34:33 -0800192TEST_F(SchedulerTest, calculateExtraBufferCount) {
193 EXPECT_EQ(0, mFlinger.calculateExtraBufferCount(Fps(60), 30ms));
194 EXPECT_EQ(1, mFlinger.calculateExtraBufferCount(Fps(90), 30ms));
195 EXPECT_EQ(2, mFlinger.calculateExtraBufferCount(Fps(120), 30ms));
196
197 EXPECT_EQ(1, mFlinger.calculateExtraBufferCount(Fps(60), 40ms));
198
199 EXPECT_EQ(0, mFlinger.calculateExtraBufferCount(Fps(60), 10ms));
200}
201
Ana Krulec3084c052018-11-21 20:27:17 +0100202} // namespace android