blob: 895c6b3d6820e69f7c4798355e421a53bd3114a9 [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"
Ady Abrahamabc27602020-04-08 17:20:29 -070027#include "mock/DisplayHardware/MockDisplay.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
32using testing::_;
33using testing::Return;
34
35namespace android {
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070036namespace {
Ana Krulec0c8cd522018-08-31 12:27:28 -070037
Marin Shalamanova524a092020-07-27 21:39:55 +020038constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID(999);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080039
Ana Krulec0c8cd522018-08-31 12:27:28 -070040class SchedulerTest : public testing::Test {
41protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080042 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070043 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080044 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abraham0bb6a472020-10-12 10:22:13 -070045 : EventThreadConnection(eventThread, /*callingUid=*/0, ResyncCallback(),
Ady Abraham0f4a1b12019-06-04 16:04:04 -070046 ISurfaceComposer::eConfigChangedSuppress) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070047 ~MockEventThreadConnection() = default;
48
49 MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel));
50 MOCK_METHOD1(setVsyncRate, status_t(uint32_t count));
51 MOCK_METHOD0(requestNextVsync, void());
52 };
53
Ana Krulec0c8cd522018-08-31 12:27:28 -070054 SchedulerTest();
Ana Krulec0c8cd522018-08-31 12:27:28 -070055
Dominik Laskowski983f2b52020-06-25 16:54:06 -070056 Hwc2::mock::Display mDisplay;
57 const scheduler::RefreshRateConfigs mConfigs{{HWC2::Display::Config::Builder(mDisplay, 0)
58 .setVsyncPeriod(16'666'667)
59 .setConfigGroup(0)
60 .build()},
61 HwcConfigIndexType(0)};
62
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070063 mock::SchedulerCallback mSchedulerCallback;
64
65 // The scheduler should initially disable VSYNC.
66 struct ExpectDisableVsync {
67 ExpectDisableVsync(mock::SchedulerCallback& callback) {
68 EXPECT_CALL(callback, setVsyncEnabled(false)).Times(1);
69 }
70 } mExpectDisableVsync{mSchedulerCallback};
71
Marin Shalamanov27fa3de2020-11-20 16:22:48 +010072 TestableScheduler mScheduler{mConfigs, mSchedulerCallback};
Dominik Laskowski98041832019-08-01 18:35:59 -070073
74 Scheduler::ConnectionHandle mConnectionHandle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070075 mock::EventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070076 sp<MockEventThreadConnection> mEventThreadConnection;
Ana Krulec0c8cd522018-08-31 12:27:28 -070077};
78
79SchedulerTest::SchedulerTest() {
Dominik Laskowski98041832019-08-01 18:35:59 -070080 auto eventThread = std::make_unique<mock::EventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070081 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080082 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
83
84 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070085
86 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
87 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -070088 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -070089 .WillRepeatedly(Return(mEventThreadConnection));
90
Dominik Laskowski983f2b52020-06-25 16:54:06 -070091 mConnectionHandle = mScheduler.createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -070092 EXPECT_TRUE(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -070093}
94
Dominik Laskowski8b01cc02020-07-14 19:02:41 -070095} // namespace
Ana Krulec0c8cd522018-08-31 12:27:28 -070096
Ana Krulec0c8cd522018-08-31 12:27:28 -070097TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski98041832019-08-01 18:35:59 -070098 Scheduler::ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070099
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700100 const sp<IDisplayEventConnection> connection =
101 mScheduler.createDisplayEventConnection(handle,
102 ISurfaceComposer::eConfigChangedSuppress);
103
Dominik Laskowski98041832019-08-01 18:35:59 -0700104 EXPECT_FALSE(connection);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700105 EXPECT_FALSE(mScheduler.getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106
107 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
108 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700109 mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700110
111 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700112 mScheduler.onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700113
114 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700115 mScheduler.onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116
Dominik Laskowski98041832019-08-01 18:35:59 -0700117 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700118 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700119 mScheduler.dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700120 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700121
Ady Abraham9c53ee72020-07-22 21:16:18 -0700122 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(0);
123 mScheduler.setDuration(handle, 10ns, 20ns);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700124}
125
126TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700127 const sp<IDisplayEventConnection> connection =
128 mScheduler.createDisplayEventConnection(mConnectionHandle,
129 ISurfaceComposer::eConfigChangedSuppress);
130
Dominik Laskowski98041832019-08-01 18:35:59 -0700131 ASSERT_EQ(mEventThreadConnection, connection);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700132 EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700133
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800134 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700135 mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700136
137 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700138 mScheduler.onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700139
140 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700141 mScheduler.onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700142
Dominik Laskowski98041832019-08-01 18:35:59 -0700143 std::string output("dump");
144 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700145 mScheduler.dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700146 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700147
Ady Abraham9c53ee72020-07-22 21:16:18 -0700148 EXPECT_CALL(*mEventThread, setDuration(10ns, 20ns)).Times(1);
149 mScheduler.setDuration(mConnectionHandle, 10ns, 20ns);
Alec Mouri717bcb62020-02-10 17:07:19 -0800150
151 static constexpr size_t kEventConnections = 5;
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700152 EXPECT_CALL(*mEventThread, getEventThreadConnectionCount()).WillOnce(Return(kEventConnections));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700153 EXPECT_EQ(kEventConnections, mScheduler.getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700154}
Dominik Laskowski98041832019-08-01 18:35:59 -0700155
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700156TEST_F(SchedulerTest, noLayerHistory) {
157 // Layer history should not be created if there is a single config.
158 ASSERT_FALSE(mScheduler.hasLayerHistory());
159
160 TestableSurfaceFlinger flinger;
161 mock::MockLayer layer(flinger.flinger());
162
163 // Content detection should be no-op.
164 mScheduler.registerLayer(&layer);
165 mScheduler.recordLayerHistory(&layer, 0, LayerHistory::LayerUpdateType::Buffer);
166
167 constexpr bool kPowerStateNormal = true;
168 mScheduler.setDisplayPowerState(kPowerStateNormal);
169
170 constexpr uint32_t kDisplayArea = 999'999;
171 mScheduler.onPrimaryDisplayAreaChanged(kDisplayArea);
172
Dominik Laskowski8b01cc02020-07-14 19:02:41 -0700173 EXPECT_CALL(mSchedulerCallback, changeRefreshRate(_, _)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700174 mScheduler.chooseRefreshRateForContent();
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700175}
176
Ana Krulec6ddd2612020-09-24 13:06:33 -0700177TEST_F(SchedulerTest, testDispatchCachedReportedConfig) {
178 // If the optional fields are cleared, the function should return before
179 // onConfigChange is called.
180 mScheduler.clearOptionalFieldsInFeatures();
181 EXPECT_NO_FATAL_FAILURE(mScheduler.dispatchCachedReportedConfig());
182 EXPECT_CALL(*mEventThread, onConfigChanged(_, _, _)).Times(0);
183}
184
185TEST_F(SchedulerTest, onNonPrimaryDisplayConfigChanged_invalidParameters) {
186 HwcConfigIndexType configId = HwcConfigIndexType(111);
187 nsecs_t vsyncPeriod = 111111;
188
189 // If the handle is incorrect, the function should return before
190 // onConfigChange is called.
191 Scheduler::ConnectionHandle invalidHandle = {.id = 123};
192 EXPECT_NO_FATAL_FAILURE(mScheduler.onNonPrimaryDisplayConfigChanged(invalidHandle,
193 PHYSICAL_DISPLAY_ID,
194 configId, vsyncPeriod));
195 EXPECT_CALL(*mEventThread, onConfigChanged(_, _, _)).Times(0);
196}
197
Ana Krulec3084c052018-11-21 20:27:17 +0100198} // namespace android