1 /*
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
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <log/log.h>
20
21 #include <mutex>
22
23 #include "Scheduler/EventThread.h"
24 #include "Scheduler/RefreshRateConfigs.h"
25 #include "TestableScheduler.h"
26 #include "TestableSurfaceFlinger.h"
27 #include "mock/MockEventThread.h"
28 #include "mock/MockLayer.h"
29 #include "mock/MockSchedulerCallback.h"
30
31 using testing::_;
32 using testing::Return;
33
34 namespace android {
35 namespace {
36
37 constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID(999);
38
39 class SchedulerTest : public testing::Test {
40 protected:
41 class MockEventThreadConnection : public android::EventThreadConnection {
42 public:
MockEventThreadConnection(EventThread * eventThread)43 explicit MockEventThreadConnection(EventThread* eventThread)
44 : EventThreadConnection(eventThread, /*callingUid=*/0, ResyncCallback()) {}
45 ~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
52 SchedulerTest();
53
54 const DisplayModePtr mode60 = DisplayMode::Builder(0)
55 .setId(DisplayModeId(0))
56 .setPhysicalDisplayId(PhysicalDisplayId(0))
57 .setVsyncPeriod(Fps(60.f).getPeriodNsecs())
58 .setGroup(0)
59 .build();
60 const DisplayModePtr mode120 = DisplayMode::Builder(1)
61 .setId(DisplayModeId(1))
62 .setPhysicalDisplayId(PhysicalDisplayId(0))
63 .setVsyncPeriod(Fps(120.f).getPeriodNsecs())
64 .setGroup(0)
65 .build();
66
67 std::shared_ptr<scheduler::RefreshRateConfigs> mConfigs =
68 std::make_shared<scheduler::RefreshRateConfigs>(DisplayModes{mode60}, mode60->getId());
69
70 mock::SchedulerCallback mSchedulerCallback;
71
72 // The scheduler should initially disable VSYNC.
73 struct ExpectDisableVsync {
ExpectDisableVsyncandroid::__anon8feee2170110::SchedulerTest::ExpectDisableVsync74 ExpectDisableVsync(mock::SchedulerCallback& callback) {
75 EXPECT_CALL(callback, setVsyncEnabled(false)).Times(1);
76 }
77 } mExpectDisableVsync{mSchedulerCallback};
78
79 TestableScheduler* mScheduler = new TestableScheduler{mConfigs, mSchedulerCallback};
80
81 Scheduler::ConnectionHandle mConnectionHandle;
82 mock::EventThread* mEventThread;
83 sp<MockEventThreadConnection> mEventThreadConnection;
84
85 TestableSurfaceFlinger mFlinger;
86 };
87
SchedulerTest()88 SchedulerTest::SchedulerTest() {
89 auto eventThread = std::make_unique<mock::EventThread>();
90 mEventThread = eventThread.get();
91 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
92
93 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
94
95 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
96 // sure that call gets executed and returns an EventThread::Connection object.
97 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
98 .WillRepeatedly(Return(mEventThreadConnection));
99
100 mConnectionHandle = mScheduler->createConnection(std::move(eventThread));
101 EXPECT_TRUE(mConnectionHandle);
102
103 mFlinger.resetScheduler(mScheduler);
104 }
105
106 } // namespace
107
TEST_F(SchedulerTest,invalidConnectionHandle)108 TEST_F(SchedulerTest, invalidConnectionHandle) {
109 Scheduler::ConnectionHandle handle;
110
111 const sp<IDisplayEventConnection> connection = mScheduler->createDisplayEventConnection(handle);
112
113 EXPECT_FALSE(connection);
114 EXPECT_FALSE(mScheduler->getEventConnection(handle));
115
116 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
117 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
118 mScheduler->onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
119
120 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
121 mScheduler->onScreenAcquired(handle);
122
123 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
124 mScheduler->onScreenReleased(handle);
125
126 std::string output;
127 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
128 mScheduler->dump(handle, output);
129 EXPECT_TRUE(output.empty());
130
131 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
132 mScheduler->setDuration(handle, 10ns, 20ns);
133 }
134
TEST_F(SchedulerTest,validConnectionHandle)135 TEST_F(SchedulerTest, validConnectionHandle) {
136 const sp<IDisplayEventConnection> connection =
137 mScheduler->createDisplayEventConnection(mConnectionHandle);
138
139 ASSERT_EQ(mEventThreadConnection, connection);
140 EXPECT_TRUE(mScheduler->getEventConnection(mConnectionHandle));
141
142 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
143 mScheduler->onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
144
145 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
146 mScheduler->onScreenAcquired(mConnectionHandle);
147
148 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
149 mScheduler->onScreenReleased(mConnectionHandle);
150
151 std::string output("dump");
152 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
153 mScheduler->dump(mConnectionHandle, output);
154 EXPECT_FALSE(output.empty());
155
156 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
157 mScheduler->setDuration(mConnectionHandle, 10ns, 20ns);
158
159 static constexpr size_t kEventConnections = 5;
160 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
161 EXPECT_EQ(kEventConnections, mScheduler->getEventThreadConnectionCount(mConnectionHandle));
162 }
163
TEST_F(SchedulerTest,chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported)164 TEST_F(SchedulerTest, chooseRefreshRateForContentIsNoopWhenModeSwitchingIsNotSupported) {
165 // The layer is registered at creation time and deregistered at destruction time.
166 sp<mock::MockLayer> layer = sp<mock::MockLayer>::make(mFlinger.flinger());
167
168 // recordLayerHistory should be a noop
169 ASSERT_EQ(static_cast<size_t>(0), mScheduler->getNumActiveLayers());
170 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
171 ASSERT_EQ(static_cast<size_t>(0), mScheduler->getNumActiveLayers());
172
173 constexpr bool kPowerStateNormal = true;
174 mScheduler->setDisplayPowerState(kPowerStateNormal);
175
176 constexpr uint32_t kDisplayArea = 999'999;
177 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
178
179 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0);
180 mScheduler->chooseRefreshRateForContent();
181 }
182
TEST_F(SchedulerTest,updateDisplayModes)183 TEST_F(SchedulerTest, updateDisplayModes) {
184 ASSERT_EQ(static_cast<size_t>(0), mScheduler->layerHistorySize());
185 sp<mock::MockLayer> layer = sp<mock::MockLayer>::make(mFlinger.flinger());
186 ASSERT_EQ(static_cast<size_t>(1), mScheduler->layerHistorySize());
187
188 mScheduler->setRefreshRateConfigs(
189 std::make_shared<scheduler::RefreshRateConfigs>(DisplayModes{mode60, mode120},
190 mode60->getId()));
191
192 ASSERT_EQ(static_cast<size_t>(0), mScheduler->getNumActiveLayers());
193 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
194 ASSERT_EQ(static_cast<size_t>(1), mScheduler->getNumActiveLayers());
195 }
196
TEST_F(SchedulerTest,testDispatchCachedReportedMode)197 TEST_F(SchedulerTest, testDispatchCachedReportedMode) {
198 // If the optional fields are cleared, the function should return before
199 // onModeChange is called.
200 mScheduler->clearOptionalFieldsInFeatures();
201 EXPECT_NO_FATAL_FAILURE(mScheduler->dispatchCachedReportedMode());
202 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
203 }
204
TEST_F(SchedulerTest,onNonPrimaryDisplayModeChanged_invalidParameters)205 TEST_F(SchedulerTest, onNonPrimaryDisplayModeChanged_invalidParameters) {
206 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
207 .setId(DisplayModeId(111))
208 .setPhysicalDisplayId(PHYSICAL_DISPLAY_ID)
209 .setVsyncPeriod(111111)
210 .build();
211
212 // If the handle is incorrect, the function should return before
213 // onModeChange is called.
214 Scheduler::ConnectionHandle invalidHandle = {.id = 123};
215 EXPECT_NO_FATAL_FAILURE(mScheduler->onNonPrimaryDisplayModeChanged(invalidHandle, mode));
216 EXPECT_CALL(*mEventThread, onModeChanged(_)).Times(0);
217 }
218
TEST_F(SchedulerTest,calculateMaxAcquiredBufferCount)219 TEST_F(SchedulerTest, calculateMaxAcquiredBufferCount) {
220 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(Fps(60), 30ms));
221 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(Fps(90), 30ms));
222 EXPECT_EQ(3, mFlinger.calculateMaxAcquiredBufferCount(Fps(120), 30ms));
223
224 EXPECT_EQ(2, mFlinger.calculateMaxAcquiredBufferCount(Fps(60), 40ms));
225
226 EXPECT_EQ(1, mFlinger.calculateMaxAcquiredBufferCount(Fps(60), 10ms));
227 }
228
229 MATCHER(Is120Hz, "") {
230 return arg.getFps().equalsWithMargin(Fps(120.f));
231 }
232
TEST_F(SchedulerTest,chooseRefreshRateForContentSelectsMaxRefreshRate)233 TEST_F(SchedulerTest, chooseRefreshRateForContentSelectsMaxRefreshRate) {
234 mScheduler->setRefreshRateConfigs(
235 std::make_shared<scheduler::RefreshRateConfigs>(DisplayModes{mode60, mode120},
236 mode60->getId()));
237
238 sp<mock::MockLayer> layer = sp<mock::MockLayer>::make(mFlinger.flinger());
239
240 mScheduler->recordLayerHistory(layer.get(), 0, LayerHistory::LayerUpdateType::Buffer);
241
242 constexpr bool kPowerStateNormal = true;
243 mScheduler->setDisplayPowerState(kPowerStateNormal);
244
245 constexpr uint32_t kDisplayArea = 999'999;
246 mScheduler->onActiveDisplayAreaChanged(kDisplayArea);
247
248 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(Is120Hz(), _)).Times(1);
249 mScheduler->chooseRefreshRateForContent();
250 }
251
252 } // namespace android
253