blob: 5fed9b45a692d7e3bb03f0ec805824b4e0c04e71 [file] [log] [blame]
Lloyd Pique24b0a482018-03-09 18:52:26 -08001/*
2 * Copyright (C) 2018 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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Lloyd Pique24b0a482018-03-09 18:52:26 -080021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080026#include <log/log.h>
Rachel Lee0655a912023-04-20 19:54:18 -070027#include <scheduler/VsyncConfig.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080028#include <utils/Errors.h>
29
30#include "AsyncCallRecorder.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010031#include "DisplayHardware/DisplayMode.h"
Rachel Lee3f028662021-11-04 19:32:24 +000032#include "FrameTimeline.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070033#include "Scheduler/EventThread.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080034#include "mock/MockVSyncDispatch.h"
35#include "mock/MockVSyncTracker.h"
36#include "mock/MockVsyncController.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080037
38using namespace std::chrono_literals;
39using namespace std::placeholders;
40
41using testing::_;
42using testing::Invoke;
Ady Abraham011f8ba2022-11-22 15:09:07 -080043using testing::Return;
Lloyd Pique24b0a482018-03-09 18:52:26 -080044
45namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
48
Lloyd Pique24b0a482018-03-09 18:52:26 -080049namespace {
50
Dominik Laskowskif1833852021-03-23 15:06:50 -070051constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
52constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
53constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
54 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
55
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050056constexpr std::chrono::duration VSYNC_PERIOD(16ms);
57
Lloyd Pique24b0a482018-03-09 18:52:26 -080058} // namespace
59
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050060class EventThreadTest : public testing::Test {
Lloyd Pique24b0a482018-03-09 18:52:26 -080061protected:
Ady Abraham011f8ba2022-11-22 15:09:07 -080062 static constexpr std::chrono::nanoseconds kWorkDuration = 0ms;
63 static constexpr std::chrono::nanoseconds kReadyDuration = 3ms;
64
Dominik Laskowskif654d572018-12-20 11:03:06 -080065 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080066 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070067 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
68 ResyncCallback&& resyncCallback,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070069 EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070070 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070071 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080072 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
73 };
74
75 using ConnectionEventRecorder =
76 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
77
78 EventThreadTest();
79 ~EventThreadTest() override;
80
Rachel Lee0655a912023-04-20 19:54:18 -070081 void setupEventThread(std::chrono::nanoseconds vsyncPeriod);
Huihong Luo1b0c49f2022-03-15 19:18:21 -070082 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
83 EventRegistrationFlags eventRegistration = {},
84 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080085
Ady Abraham011f8ba2022-11-22 15:09:07 -080086 void expectVSyncCallbackScheduleReceived(bool expectState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070087 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
88 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080089 void expectVsyncEventReceivedByConnection(const char* name,
90 ConnectionEventRecorder& connectionEventRecorder,
91 nsecs_t expectedTimestamp, unsigned expectedCount);
92 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Ady Abraham011f8ba2022-11-22 15:09:07 -080093 void expectVsyncEventFrameTimelinesCorrect(
Rachel Lee0655a912023-04-20 19:54:18 -070094 nsecs_t expectedTimestamp, gui::VsyncEventData::FrameTimeline preferredVsyncData);
95 void expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData,
96 std::chrono::nanoseconds vsyncPeriod);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080097 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070098 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080099 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700100 int32_t expectedConfigId,
101 nsecs_t expectedVsyncPeriod);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500102 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700103 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
104 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800105
Ady Abraham011f8ba2022-11-22 15:09:07 -0800106 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime,
107 nsecs_t deadlineTimestamp) {
108 mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp);
109 }
110
111 AsyncCallRecorderWithCannedReturn<
112 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
113 scheduler::VSyncDispatch::ScheduleTiming)>
114 mVSyncCallbackScheduleRecorder{0};
115 AsyncCallRecorderWithCannedReturn<
116 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
117 scheduler::VSyncDispatch::ScheduleTiming)>
118 mVSyncCallbackUpdateRecorder{0};
119 AsyncCallRecorderWithCannedReturn<
120 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
121 std::string)>
122 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
123 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
124 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500126 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800127 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700128 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800129
Leon Scroggins III67388622023-02-06 20:36:20 -0500130 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700131 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800132 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700133 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000134 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700135
136 static constexpr uid_t mConnectionUid = 443;
137 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800138};
139
140EventThreadTest::EventThreadTest() {
141 const ::testing::TestInfo* const test_info =
142 ::testing::UnitTest::GetInstance()->current_test_info();
143 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
144
Leon Scroggins III67388622023-02-06 20:36:20 -0500145 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
146 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
147 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
148 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
149 nullptr));
150 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800151 EXPECT_CALL(mockDispatch, registerCallback(_, _))
152 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
153 EXPECT_CALL(mockDispatch, schedule(_, _))
154 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
155 EXPECT_CALL(mockDispatch, update(_, _))
156 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
157 EXPECT_CALL(mockDispatch, unregisterCallback(_))
158 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800159}
160
161EventThreadTest::~EventThreadTest() {
162 const ::testing::TestInfo* const test_info =
163 ::testing::UnitTest::GetInstance()->current_test_info();
164 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800165
Ady Abraham011f8ba2022-11-22 15:09:07 -0800166 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800167 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800168 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800169}
170
Rachel Lee0655a912023-04-20 19:54:18 -0700171void EventThreadTest::setupEventThread(std::chrono::nanoseconds vsyncPeriod) {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500172 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
173 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
174 return (uid == mThrottledConnectionUid);
175 };
Rachel Lee0655a912023-04-20 19:54:18 -0700176 const auto getVsyncPeriod = [vsyncPeriod](uid_t uid) { return vsyncPeriod.count(); };
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500177
Rachel Lee3f028662021-11-04 19:32:24 +0000178 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
Leon Scroggins III67388622023-02-06 20:36:20 -0500179 mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule,
180 mTokenManager.get(), throttleVsync,
181 getVsyncPeriod, kWorkDuration, kReadyDuration);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800182
183 // EventThread should register itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800184 EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value());
Rachel Lee0655a912023-04-20 19:54:18 -0700185
186 mConnection =
187 createConnection(mConnectionEventCallRecorder,
188 gui::ISurfaceComposer::EventRegistration::modeChanged |
189 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
190 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
191 gui::ISurfaceComposer::EventRegistration::modeChanged,
192 mThrottledConnectionUid);
193
194 // A display must be connected for VSYNC events to be delivered.
195 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
196 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800197}
198
199sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700200 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
201 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800202 sp<MockEventThreadConnection> connection =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700203 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid,
204 mResyncCallRecorder.getInvocable(),
205 eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800206 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
207 return connection;
208}
209
Ady Abraham011f8ba2022-11-22 15:09:07 -0800210void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
211 if (expectState) {
212 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
213 } else {
214 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
215 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800216}
217
Ady Abraham9c53ee72020-07-22 21:16:18 -0700218void EventThreadTest::expectVSyncSetDurationCallReceived(
219 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800220 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800221 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800222 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
223 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800224}
225
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500226void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700227 auto args = mThrottleVsyncCallRecorder.waitForCall();
228 ASSERT_TRUE(args.has_value());
229 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
230 EXPECT_EQ(uid, std::get<1>(args.value()));
231}
232
Lloyd Pique24b0a482018-03-09 18:52:26 -0800233void EventThreadTest::expectVsyncEventReceivedByConnection(
234 const char* name, ConnectionEventRecorder& connectionEventRecorder,
235 nsecs_t expectedTimestamp, unsigned expectedCount) {
236 auto args = connectionEventRecorder.waitForCall();
237 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
238 << expectedTimestamp;
239 const auto& event = std::get<0>(args.value());
240 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
241 << name << " did not get the correct event for timestamp " << expectedTimestamp;
242 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
243 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
244 EXPECT_EQ(expectedCount, event.vsync.count)
245 << name << " did not get the expected count for timestamp " << expectedTimestamp;
246}
247
248void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
249 unsigned expectedCount) {
250 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
251 mConnectionEventCallRecorder, expectedTimestamp,
252 expectedCount);
253}
254
Rachel Leeb9c5a772022-02-04 21:17:37 -0800255void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800256 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000257 auto args = mConnectionEventCallRecorder.waitForCall();
258 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
259 << expectedTimestamp;
260 const auto& event = std::get<0>(args.value());
Rachel Lee0655a912023-04-20 19:54:18 -0700261 for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800262 auto prediction = mTokenManager->getPredictionsForToken(
263 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000264 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800265 EXPECT_EQ(prediction.value().endTime,
266 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000267 << "Deadline timestamp does not match cached value";
268 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800269 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
270 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000271
Rachel Lee3f028662021-11-04 19:32:24 +0000272 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800273 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
274 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000275 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800276 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
277 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
278 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000279 }
Rachel Lee0d943202022-02-01 23:29:41 -0800280
281 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800282 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800283 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800284 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
285 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
286 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800287 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800288 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
289 preferredVsyncData.expectedPresentationTime)
290 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000291 }
292 }
293}
294
Rachel Lee0655a912023-04-20 19:54:18 -0700295void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(
296 VsyncEventData vsyncEventData, std::chrono::nanoseconds vsyncPeriod) {
297 float nonPreferredTimelinesAmount =
298 scheduler::VsyncConfig::kEarlyLatchMaxThreshold / vsyncPeriod;
299 EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1)
300 << "Amount of non-preferred frame timelines too many;"
301 << " expected presentation time will be over threshold";
Rachel Lee40aef422023-04-25 14:35:47 -0700302 EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity)
Rachel Lee0655a912023-04-20 19:54:18 -0700303 << "Amount of non-preferred frame timelines should be less than max capacity";
304 EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0)
305 << "Frame timelines length should be greater than 0";
306 EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength)
307 << "Preferred frame timeline index should be less than frame timelines length";
308}
309
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800310void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
311 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800312 auto args = mConnectionEventCallRecorder.waitForCall();
313 ASSERT_TRUE(args.has_value());
314 const auto& event = std::get<0>(args.value());
315 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800316 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317 EXPECT_EQ(expectedConnected, event.hotplug.connected);
318}
319
Ady Abraham447052e2019-02-13 16:07:27 -0800320void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700321 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
322 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800323 auto args = mConnectionEventCallRecorder.waitForCall();
324 ASSERT_TRUE(args.has_value());
325 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100326 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800327 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100328 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
329 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800330}
331
Ady Abraham62f216c2020-10-13 19:07:23 -0700332void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
333 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
334 for (const auto [uid, frameRateHz] : expectedOverrides) {
335 auto args = mConnectionEventCallRecorder.waitForCall();
336 ASSERT_TRUE(args.has_value());
337 const auto& event = std::get<0>(args.value());
338 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
339 EXPECT_EQ(expectedDisplayId, event.header.displayId);
340 EXPECT_EQ(uid, event.frameRateOverride.uid);
341 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
342 }
343
344 auto args = mConnectionEventCallRecorder.waitForCall();
345 ASSERT_TRUE(args.has_value());
346 const auto& event = std::get<0>(args.value());
347 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
348 EXPECT_EQ(expectedDisplayId, event.header.displayId);
349}
350
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351namespace {
352
Rachel Leeef2e21f2022-02-01 14:51:34 -0800353using namespace testing;
354
Lloyd Pique24b0a482018-03-09 18:52:26 -0800355/* ------------------------------------------------------------------------
356 * Test cases
357 */
358
359TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Rachel Lee0655a912023-04-20 19:54:18 -0700360 setupEventThread(VSYNC_PERIOD);
361
Ady Abraham011f8ba2022-11-22 15:09:07 -0800362 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
363 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
364 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
365 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800366 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800367 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
368}
369
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800370TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Rachel Lee0655a912023-04-20 19:54:18 -0700371 setupEventThread(VSYNC_PERIOD);
372
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800373 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
374 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800375
376 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700377 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800378
379 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800380 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800381}
382
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
Rachel Lee0655a912023-04-20 19:54:18 -0700384 setupEventThread(VSYNC_PERIOD);
385
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700387 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800388
Ady Abraham8532d012019-05-08 14:50:56 -0700389 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800390 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
391
Ady Abraham011f8ba2022-11-22 15:09:07 -0800392 // EventThread should enable schedule a vsync callback
393 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800394
395 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700396 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800397 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500398 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800399 expectVsyncEventReceivedByConnection(123, 1u);
400
Ady Abraham011f8ba2022-11-22 15:09:07 -0800401 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
402 expectVSyncCallbackScheduleReceived(true);
403
Lloyd Pique24b0a482018-03-09 18:52:26 -0800404 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700405 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800407 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700408 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
410
411 // EventThread should also detect that at this point that it does not need
412 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800413 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800414}
415
Rachel Lee3f028662021-11-04 19:32:24 +0000416TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
Rachel Lee0655a912023-04-20 19:54:18 -0700417 setupEventThread(VSYNC_PERIOD);
418
Rachel Lee3f028662021-11-04 19:32:24 +0000419 // Signal that we want the next vsync event to be posted to the connection
420 mThread->requestNextVsync(mConnection);
421
Ady Abraham011f8ba2022-11-22 15:09:07 -0800422 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000423
424 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700425 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800426 onVSyncEvent(123, 456, 789);
427 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000428}
429
Rachel Lee0655a912023-04-20 19:54:18 -0700430TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) {
Rachel Lee40aef422023-04-25 14:35:47 -0700431 // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines,
432 // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal
433 // truncation (e.g. 60Hz has 16.6... ms vsync period).
Rachel Lee0655a912023-04-20 19:54:18 -0700434 std::chrono::nanoseconds vsyncPeriod(scheduler::VsyncConfig::kEarlyLatchMaxThreshold /
Rachel Lee40aef422023-04-25 14:35:47 -0700435 (VsyncEventData::kFrameTimelinesCapacity - 2));
Rachel Lee0655a912023-04-20 19:54:18 -0700436 setupEventThread(vsyncPeriod);
437
438 // Signal that we want the next vsync event to be posted to the connection
439 mThread->requestNextVsync(mConnection);
440
441 expectVSyncCallbackScheduleReceived(true);
442
443 // Use the received callback to signal a vsync event.
444 // The throttler should receive the event, as well as the connection.
445 nsecs_t expectedTimestamp = 123;
446 onVSyncEvent(expectedTimestamp, 456, 789);
447
448 auto args = mConnectionEventCallRecorder.waitForCall();
449 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
450 << expectedTimestamp;
451 const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData;
452 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData, vsyncPeriod);
453}
454
Rachel Leeef2e21f2022-02-01 14:51:34 -0800455TEST_F(EventThreadTest, getLatestVsyncEventData) {
Rachel Lee0655a912023-04-20 19:54:18 -0700456 setupEventThread(VSYNC_PERIOD);
457
Rachel Leeef2e21f2022-02-01 14:51:34 -0800458 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800459 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800460 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
461
462 mock::VSyncTracker& mockTracker =
463 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
464 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_))
465 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800466
467 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700468
469 // Check EventThread immediately requested a resync.
470 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
471
Rachel Lee0655a912023-04-20 19:54:18 -0700472 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData, VSYNC_PERIOD);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800473 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
474 << "Deadline timestamp should be greater than frame time";
Rachel Lee0655a912023-04-20 19:54:18 -0700475 for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800476 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800477 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800478 EXPECT_TRUE(prediction.has_value());
479 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
480 << "Deadline timestamp does not match cached value";
481 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800482 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800483 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800484 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800485 vsyncEventData.frameTimelines[i].deadlineTimestamp)
486 << "Expected vsync timestamp should be greater than deadline";
487
488 if (i > 0) {
489 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
490 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
491 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800492 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
493 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800494 << "Expected vsync timestamp out of order for frame timeline " << i;
495 }
496
497 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800498 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800499 << "Vsync ID incorrect for frame timeline " << i;
500 if (i == vsyncEventData.preferredFrameTimelineIndex) {
501 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
502 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800503 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
504 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800505 << "Preferred expected vsync timestamp incorrect" << i;
506 }
507 }
508}
509
Lloyd Pique24b0a482018-03-09 18:52:26 -0800510TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
Rachel Lee0655a912023-04-20 19:54:18 -0700511 setupEventThread(VSYNC_PERIOD);
512
Lloyd Pique24b0a482018-03-09 18:52:26 -0800513 // Create a first connection, register it, and request a vsync rate of zero.
514 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700515 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800516 mThread->setVsyncRate(0, firstConnection);
517
518 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800519 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800520
521 // However if there is another connection which wants events at a nonzero rate.....
522 ConnectionEventRecorder secondConnectionEventRecorder{0};
523 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700524 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800525 mThread->setVsyncRate(1, secondConnection);
526
Dominik Laskowski029cc122019-01-23 19:52:06 -0800527 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800528 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800529
530 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700531 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800532 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800533 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800534 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
535 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
536 1u);
537}
538
539TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
Rachel Lee0655a912023-04-20 19:54:18 -0700540 setupEventThread(VSYNC_PERIOD);
541
Lloyd Pique24b0a482018-03-09 18:52:26 -0800542 mThread->setVsyncRate(1, mConnection);
543
Dominik Laskowski029cc122019-01-23 19:52:06 -0800544 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800545 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800546
547 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700548 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800549 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500550 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800551 expectVsyncEventReceivedByConnection(123, 1u);
552
553 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800554 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500555 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800556 expectVsyncEventReceivedByConnection(456, 2u);
557
558 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800559 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500560 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561 expectVsyncEventReceivedByConnection(789, 3u);
562}
563
564TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
Rachel Lee0655a912023-04-20 19:54:18 -0700565 setupEventThread(VSYNC_PERIOD);
566
Lloyd Pique24b0a482018-03-09 18:52:26 -0800567 mThread->setVsyncRate(2, mConnection);
568
Dominik Laskowski029cc122019-01-23 19:52:06 -0800569 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800570 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800571
Huihong Luoab8ffef2022-08-18 13:02:26 -0700572 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800573 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800574 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700575 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800576
Huihong Luoab8ffef2022-08-18 13:02:26 -0700577 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800578 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800579 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700580 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800581
Huihong Luoab8ffef2022-08-18 13:02:26 -0700582 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800583 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800584 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700585 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800586
Huihong Luoab8ffef2022-08-18 13:02:26 -0700587 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800588 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800589 expectVsyncEventReceivedByConnection(101112, 4u);
590}
591
592TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
Rachel Lee0655a912023-04-20 19:54:18 -0700593 setupEventThread(VSYNC_PERIOD);
594
Lloyd Pique24b0a482018-03-09 18:52:26 -0800595 mThread->setVsyncRate(1, mConnection);
596
Dominik Laskowski029cc122019-01-23 19:52:06 -0800597 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800598 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800599
600 // Destroy the only (strong) reference to the connection.
601 mConnection = nullptr;
602
Huihong Luoab8ffef2022-08-18 13:02:26 -0700603 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800604 onVSyncEvent(123, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800605 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
606
607 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800608 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800609}
610
611TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
Rachel Lee0655a912023-04-20 19:54:18 -0700612 setupEventThread(VSYNC_PERIOD);
613
Lloyd Pique24b0a482018-03-09 18:52:26 -0800614 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700615 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800616 mThread->setVsyncRate(1, errorConnection);
617
Dominik Laskowski029cc122019-01-23 19:52:06 -0800618 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800619 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800620
Huihong Luoab8ffef2022-08-18 13:02:26 -0700621 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800622 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800623 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
624
Ady Abraham011f8ba2022-11-22 15:09:07 -0800625 // Another schedule is expected, since the connection is removed only after
626 // the next vsync is requested.
627 expectVSyncCallbackScheduleReceived(true);
628
Huihong Luoab8ffef2022-08-18 13:02:26 -0700629 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800630 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800631 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
632
633 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800634 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800635}
636
Alec Mouri717bcb62020-02-10 17:07:19 -0800637TEST_F(EventThreadTest, tracksEventConnections) {
Rachel Lee0655a912023-04-20 19:54:18 -0700638 setupEventThread(VSYNC_PERIOD);
639
Ady Abraham0bb6a472020-10-12 10:22:13 -0700640 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800641 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700642 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800643 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700644 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800645 ConnectionEventRecorder secondConnectionEventRecorder{0};
646 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700647 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800648 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700649 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800650
651 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800652 expectVSyncCallbackScheduleReceived(true);
Alec Mouri717bcb62020-02-10 17:07:19 -0800653
Huihong Luoab8ffef2022-08-18 13:02:26 -0700654 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800655 onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800656 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
657 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
658 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700659 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800660}
661
Lloyd Pique24b0a482018-03-09 18:52:26 -0800662TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
Rachel Lee0655a912023-04-20 19:54:18 -0700663 setupEventThread(VSYNC_PERIOD);
664
Lloyd Pique24b0a482018-03-09 18:52:26 -0800665 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700666 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800667 mThread->setVsyncRate(1, errorConnection);
668
Dominik Laskowski029cc122019-01-23 19:52:06 -0800669 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800670 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800671
Huihong Luoab8ffef2022-08-18 13:02:26 -0700672 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800673 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800674 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800675 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800676
Huihong Luoab8ffef2022-08-18 13:02:26 -0700677 // A subsequent event will be seen by the connection, which still then returns a non-fatal
678 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800679 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800680 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800681 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800682
683 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800684 onVSyncEvent(456, 123, 0);
685 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800686}
687
688TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Rachel Lee0655a912023-04-20 19:54:18 -0700689 setupEventThread(VSYNC_PERIOD);
690
Ady Abraham9c53ee72020-07-22 21:16:18 -0700691 mThread->setDuration(321ns, 456ns);
692 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800693}
694
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800695TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
Rachel Lee0655a912023-04-20 19:54:18 -0700696 setupEventThread(VSYNC_PERIOD);
697
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800698 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
699 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800700}
701
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800702TEST_F(EventThreadTest, postHotplugInternalConnect) {
Rachel Lee0655a912023-04-20 19:54:18 -0700703 setupEventThread(VSYNC_PERIOD);
704
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800705 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
706 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800707}
708
709TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Rachel Lee0655a912023-04-20 19:54:18 -0700710 setupEventThread(VSYNC_PERIOD);
711
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800712 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
713 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800714}
715
716TEST_F(EventThreadTest, postHotplugExternalConnect) {
Rachel Lee0655a912023-04-20 19:54:18 -0700717 setupEventThread(VSYNC_PERIOD);
718
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800719 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
720 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800721}
722
Ady Abraham447052e2019-02-13 16:07:27 -0800723TEST_F(EventThreadTest, postConfigChangedPrimary) {
Rachel Lee0655a912023-04-20 19:54:18 -0700724 setupEventThread(VSYNC_PERIOD);
725
Ady Abraham690f4612021-07-01 23:24:03 -0700726 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
727 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
728 .setId(DisplayModeId(7))
729 .setVsyncPeriod(16666666)
730 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800731 const Fps fps = mode->getFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700732
Ady Abraham67434eb2022-12-01 17:48:12 -0800733 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
734 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800735}
736
737TEST_F(EventThreadTest, postConfigChangedExternal) {
Rachel Lee0655a912023-04-20 19:54:18 -0700738 setupEventThread(VSYNC_PERIOD);
739
Ady Abraham690f4612021-07-01 23:24:03 -0700740 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
741 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
742 .setId(DisplayModeId(5))
743 .setVsyncPeriod(16666666)
744 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800745 const Fps fps = mode->getFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700746
Ady Abraham67434eb2022-12-01 17:48:12 -0800747 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
748 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800749}
750
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700751TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Rachel Lee0655a912023-04-20 19:54:18 -0700752 setupEventThread(VSYNC_PERIOD);
753
Ady Abraham690f4612021-07-01 23:24:03 -0700754 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
755 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
756 .setId(DisplayModeId(7))
757 .setVsyncPeriod(16666666)
758 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800759 const Fps fps = mode->getFps() / 2;
760 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
761 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700762}
763
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700764TEST_F(EventThreadTest, suppressConfigChanged) {
Rachel Lee0655a912023-04-20 19:54:18 -0700765 setupEventThread(VSYNC_PERIOD);
766
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700767 ConnectionEventRecorder suppressConnectionEventRecorder{0};
768 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700769 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700770
Ady Abraham690f4612021-07-01 23:24:03 -0700771 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
772 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
773 .setId(DisplayModeId(9))
774 .setVsyncPeriod(16666666)
775 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800776 const Fps fps = mode->getFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700777
Ady Abraham67434eb2022-12-01 17:48:12 -0800778 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
779 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700780
781 auto args = suppressConnectionEventRecorder.waitForCall();
782 ASSERT_FALSE(args.has_value());
783}
784
Ady Abraham62f216c2020-10-13 19:07:23 -0700785TEST_F(EventThreadTest, postUidFrameRateMapping) {
Rachel Lee0655a912023-04-20 19:54:18 -0700786 setupEventThread(VSYNC_PERIOD);
787
Ady Abraham62f216c2020-10-13 19:07:23 -0700788 const std::vector<FrameRateOverride> overrides = {
789 {.uid = 1, .frameRateHz = 20},
790 {.uid = 3, .frameRateHz = 40},
791 {.uid = 5, .frameRateHz = 60},
792 };
793
794 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
795 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
796}
797
798TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
Rachel Lee0655a912023-04-20 19:54:18 -0700799 setupEventThread(VSYNC_PERIOD);
800
Ady Abraham62f216c2020-10-13 19:07:23 -0700801 const std::vector<FrameRateOverride> overrides = {
802 {.uid = 1, .frameRateHz = 20},
803 {.uid = 3, .frameRateHz = 40},
804 {.uid = 5, .frameRateHz = 60},
805 };
806
807 ConnectionEventRecorder suppressConnectionEventRecorder{0};
808 sp<MockEventThreadConnection> suppressConnection =
809 createConnection(suppressConnectionEventRecorder);
810
811 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
812 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
813
814 auto args = suppressConnectionEventRecorder.waitForCall();
815 ASSERT_FALSE(args.has_value());
816}
817
Ady Abraham0bb6a472020-10-12 10:22:13 -0700818TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
Rachel Lee0655a912023-04-20 19:54:18 -0700819 setupEventThread(VSYNC_PERIOD);
820
Ady Abraham0bb6a472020-10-12 10:22:13 -0700821 // Signal that we want the next vsync event to be posted to the throttled connection
822 mThread->requestNextVsync(mThrottledConnection);
823
824 // EventThread should immediately request a resync.
825 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
826
827 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800828 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700829
830 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700831 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800832 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500833 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700834 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800835 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700836
837 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700838 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700839 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800840 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500841 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700842 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800843 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700844
845 // EventThread should not change the vsync state as it didn't send the event
846 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800847 onVSyncEvent(456, 123, 0);
848 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700849}
850
Lloyd Pique24b0a482018-03-09 18:52:26 -0800851} // namespace
852} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100853
854// TODO(b/129481165): remove the #pragma below and fix conversion issues
855#pragma clang diagnostic pop // ignored "-Wextra"