blob: d5ec654c59b1ccd5edf3ee5044bc77af4b3d59d7 [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
Huihong Luocac92162023-12-21 13:52:42 -080058constexpr int HDCP_V1 = 2;
59constexpr int HDCP_V2 = 3;
60
Lloyd Pique24b0a482018-03-09 18:52:26 -080061} // namespace
62
Ady Abrahamf2851612023-09-25 17:19:00 -070063class EventThreadTest : public testing::Test, public IEventThreadCallback {
Lloyd Pique24b0a482018-03-09 18:52:26 -080064protected:
Ady Abraham011f8ba2022-11-22 15:09:07 -080065 static constexpr std::chrono::nanoseconds kWorkDuration = 0ms;
66 static constexpr std::chrono::nanoseconds kReadyDuration = 3ms;
67
Dominik Laskowskif654d572018-12-20 11:03:06 -080068 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080069 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070070 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070071 EventRegistrationFlags eventRegistration)
Ady Abrahamf2851612023-09-25 17:19:00 -070072 : EventThreadConnection(eventThread, callingUid, eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080073 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
74 };
75
76 using ConnectionEventRecorder =
77 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
78
79 EventThreadTest();
80 ~EventThreadTest() override;
81
Ady Abrahamf2851612023-09-25 17:19:00 -070082 void SetUp() override { mVsyncPeriod = VSYNC_PERIOD; }
83
84 // IEventThreadCallback overrides
85 bool throttleVsync(TimePoint, uid_t) override;
86 Period getVsyncPeriod(uid_t) override;
87 void resync() override;
ramindaniae645822024-01-11 10:57:29 -080088 void onExpectedPresentTimePosted(TimePoint) override;
Ady Abrahamf2851612023-09-25 17:19:00 -070089
90 void setupEventThread();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070091 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
92 EventRegistrationFlags eventRegistration = {},
93 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080094
Ady Abraham011f8ba2022-11-22 15:09:07 -080095 void expectVSyncCallbackScheduleReceived(bool expectState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070096 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
97 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080098 void expectVsyncEventReceivedByConnection(const char* name,
99 ConnectionEventRecorder& connectionEventRecorder,
100 nsecs_t expectedTimestamp, unsigned expectedCount);
101 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800102 void expectVsyncEventFrameTimelinesCorrect(
Rachel Lee0655a912023-04-20 19:54:18 -0700103 nsecs_t expectedTimestamp, gui::VsyncEventData::FrameTimeline preferredVsyncData);
Ady Abrahamf2851612023-09-25 17:19:00 -0700104 void expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800105 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700106 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800107 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700108 int32_t expectedConfigId,
109 nsecs_t expectedVsyncPeriod);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500110 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
ramindaniae645822024-01-11 10:57:29 -0800111 void expectOnExpectedPresentTimePosted(nsecs_t expectedPresentTime);
Ady Abraham62f216c2020-10-13 19:07:23 -0700112 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
113 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800114
Ady Abraham011f8ba2022-11-22 15:09:07 -0800115 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime,
116 nsecs_t deadlineTimestamp) {
117 mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp);
118 }
119
120 AsyncCallRecorderWithCannedReturn<
121 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
122 scheduler::VSyncDispatch::ScheduleTiming)>
123 mVSyncCallbackScheduleRecorder{0};
124 AsyncCallRecorderWithCannedReturn<
125 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
126 scheduler::VSyncDispatch::ScheduleTiming)>
127 mVSyncCallbackUpdateRecorder{0};
128 AsyncCallRecorderWithCannedReturn<
129 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
130 std::string)>
131 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
132 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
133 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800134 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500135 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
ramindaniae645822024-01-11 10:57:29 -0800136 AsyncCallRecorder<void (*)(nsecs_t)> mOnExpectedPresentTimePostedRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700138 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800139
Leon Scroggins III67388622023-02-06 20:36:20 -0500140 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700141 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800142 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700143 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000144 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700145
Ady Abrahamf2851612023-09-25 17:19:00 -0700146 std::chrono::nanoseconds mVsyncPeriod;
147
Ady Abraham0bb6a472020-10-12 10:22:13 -0700148 static constexpr uid_t mConnectionUid = 443;
149 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800150};
151
152EventThreadTest::EventThreadTest() {
153 const ::testing::TestInfo* const test_info =
154 ::testing::UnitTest::GetInstance()->current_test_info();
155 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
156
Leon Scroggins III67388622023-02-06 20:36:20 -0500157 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
158 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
159 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
160 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
161 nullptr));
162 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800163 EXPECT_CALL(mockDispatch, registerCallback(_, _))
164 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
165 EXPECT_CALL(mockDispatch, schedule(_, _))
166 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
167 EXPECT_CALL(mockDispatch, update(_, _))
168 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
169 EXPECT_CALL(mockDispatch, unregisterCallback(_))
170 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800171}
172
173EventThreadTest::~EventThreadTest() {
174 const ::testing::TestInfo* const test_info =
175 ::testing::UnitTest::GetInstance()->current_test_info();
176 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800177
Ady Abraham011f8ba2022-11-22 15:09:07 -0800178 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800179 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800180 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800181}
182
Ady Abrahamf2851612023-09-25 17:19:00 -0700183bool EventThreadTest::throttleVsync(android::TimePoint expectedVsyncTimestamp, uid_t uid) {
184 mThrottleVsyncCallRecorder.recordCall(expectedVsyncTimestamp.ns(), uid);
185 return (uid == mThrottledConnectionUid);
186}
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500187
Ady Abrahamf2851612023-09-25 17:19:00 -0700188Period EventThreadTest::getVsyncPeriod(uid_t) {
189 return mVsyncPeriod;
190}
191
192void EventThreadTest::resync() {
193 mResyncCallRecorder.recordCall();
194}
195
ramindaniae645822024-01-11 10:57:29 -0800196void EventThreadTest::onExpectedPresentTimePosted(TimePoint expectedPresentTime) {
197 mOnExpectedPresentTimePostedRecorder.recordCall(expectedPresentTime.ns());
198}
199
Ady Abrahamf2851612023-09-25 17:19:00 -0700200void EventThreadTest::setupEventThread() {
Rachel Lee3f028662021-11-04 19:32:24 +0000201 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
Leon Scroggins III67388622023-02-06 20:36:20 -0500202 mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule,
Ady Abrahamf2851612023-09-25 17:19:00 -0700203 mTokenManager.get(), *this, kWorkDuration,
204 kReadyDuration);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800205
206 // EventThread should register itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800207 EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value());
Rachel Lee0655a912023-04-20 19:54:18 -0700208
209 mConnection =
210 createConnection(mConnectionEventCallRecorder,
211 gui::ISurfaceComposer::EventRegistration::modeChanged |
212 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
213 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
214 gui::ISurfaceComposer::EventRegistration::modeChanged,
215 mThrottledConnectionUid);
216
217 // A display must be connected for VSYNC events to be delivered.
218 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
219 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800220}
221
222sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700223 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
224 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800225 sp<MockEventThreadConnection> connection =
Ady Abrahamf2851612023-09-25 17:19:00 -0700226 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid, eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800227 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
228 return connection;
229}
230
Ady Abraham011f8ba2022-11-22 15:09:07 -0800231void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
232 if (expectState) {
233 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
234 } else {
235 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
236 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800237}
238
Ady Abraham9c53ee72020-07-22 21:16:18 -0700239void EventThreadTest::expectVSyncSetDurationCallReceived(
240 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800241 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800242 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800243 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
244 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800245}
246
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500247void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700248 auto args = mThrottleVsyncCallRecorder.waitForCall();
249 ASSERT_TRUE(args.has_value());
250 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
251 EXPECT_EQ(uid, std::get<1>(args.value()));
252}
253
ramindaniae645822024-01-11 10:57:29 -0800254void EventThreadTest::expectOnExpectedPresentTimePosted(nsecs_t expectedPresentTime) {
255 auto args = mOnExpectedPresentTimePostedRecorder.waitForCall();
256 ASSERT_TRUE(args.has_value());
257 EXPECT_EQ(expectedPresentTime, std::get<0>(args.value()));
258}
259
Lloyd Pique24b0a482018-03-09 18:52:26 -0800260void EventThreadTest::expectVsyncEventReceivedByConnection(
261 const char* name, ConnectionEventRecorder& connectionEventRecorder,
262 nsecs_t expectedTimestamp, unsigned expectedCount) {
263 auto args = connectionEventRecorder.waitForCall();
264 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
265 << expectedTimestamp;
266 const auto& event = std::get<0>(args.value());
267 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
268 << name << " did not get the correct event for timestamp " << expectedTimestamp;
269 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
270 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
271 EXPECT_EQ(expectedCount, event.vsync.count)
272 << name << " did not get the expected count for timestamp " << expectedTimestamp;
273}
274
275void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
276 unsigned expectedCount) {
277 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
278 mConnectionEventCallRecorder, expectedTimestamp,
279 expectedCount);
280}
281
Rachel Leeb9c5a772022-02-04 21:17:37 -0800282void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800283 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000284 auto args = mConnectionEventCallRecorder.waitForCall();
285 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
286 << expectedTimestamp;
287 const auto& event = std::get<0>(args.value());
Rachel Lee0655a912023-04-20 19:54:18 -0700288 for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800289 auto prediction = mTokenManager->getPredictionsForToken(
290 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000291 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800292 EXPECT_EQ(prediction.value().endTime,
293 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000294 << "Deadline timestamp does not match cached value";
295 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800296 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
297 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000298
Rachel Lee3f028662021-11-04 19:32:24 +0000299 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800300 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
301 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000302 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800303 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
304 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
305 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000306 }
Rachel Lee0d943202022-02-01 23:29:41 -0800307
308 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800309 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800310 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800311 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
312 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
313 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800314 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800315 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
316 preferredVsyncData.expectedPresentationTime)
317 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000318 }
319 }
320}
321
Ady Abrahamf2851612023-09-25 17:19:00 -0700322void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData) {
Rachel Lee0655a912023-04-20 19:54:18 -0700323 float nonPreferredTimelinesAmount =
Ady Abrahamf2851612023-09-25 17:19:00 -0700324 scheduler::VsyncConfig::kEarlyLatchMaxThreshold / mVsyncPeriod;
Rachel Lee0655a912023-04-20 19:54:18 -0700325 EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1)
326 << "Amount of non-preferred frame timelines too many;"
327 << " expected presentation time will be over threshold";
Rachel Lee40aef422023-04-25 14:35:47 -0700328 EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity)
Rachel Lee0655a912023-04-20 19:54:18 -0700329 << "Amount of non-preferred frame timelines should be less than max capacity";
330 EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0)
331 << "Frame timelines length should be greater than 0";
332 EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength)
333 << "Preferred frame timeline index should be less than frame timelines length";
334}
335
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800336void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
337 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338 auto args = mConnectionEventCallRecorder.waitForCall();
339 ASSERT_TRUE(args.has_value());
340 const auto& event = std::get<0>(args.value());
341 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800342 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 EXPECT_EQ(expectedConnected, event.hotplug.connected);
344}
345
Ady Abraham447052e2019-02-13 16:07:27 -0800346void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700347 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
348 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800349 auto args = mConnectionEventCallRecorder.waitForCall();
350 ASSERT_TRUE(args.has_value());
351 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100352 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800353 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100354 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
355 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800356}
357
Ady Abraham62f216c2020-10-13 19:07:23 -0700358void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
359 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
360 for (const auto [uid, frameRateHz] : expectedOverrides) {
361 auto args = mConnectionEventCallRecorder.waitForCall();
362 ASSERT_TRUE(args.has_value());
363 const auto& event = std::get<0>(args.value());
364 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
365 EXPECT_EQ(expectedDisplayId, event.header.displayId);
366 EXPECT_EQ(uid, event.frameRateOverride.uid);
367 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
368 }
369
370 auto args = mConnectionEventCallRecorder.waitForCall();
371 ASSERT_TRUE(args.has_value());
372 const auto& event = std::get<0>(args.value());
373 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
374 EXPECT_EQ(expectedDisplayId, event.header.displayId);
375}
376
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377namespace {
378
Rachel Leeef2e21f2022-02-01 14:51:34 -0800379using namespace testing;
380
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381/* ------------------------------------------------------------------------
382 * Test cases
383 */
384
385TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700386 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700387
Ady Abraham011f8ba2022-11-22 15:09:07 -0800388 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
389 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
390 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
391 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800392 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800393 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
394}
395
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800396TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700397 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700398
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800399 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
400 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800401
402 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700403 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800404
405 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800406 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800407}
408
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700410 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700411
Lloyd Pique24b0a482018-03-09 18:52:26 -0800412 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700413 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800414
Ady Abraham8532d012019-05-08 14:50:56 -0700415 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
417
Ady Abraham011f8ba2022-11-22 15:09:07 -0800418 // EventThread should enable schedule a vsync callback
419 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800420
421 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700422 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800423 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500424 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800425 expectVsyncEventReceivedByConnection(123, 1u);
ramindaniae645822024-01-11 10:57:29 -0800426 expectOnExpectedPresentTimePosted(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800427
Ady Abraham011f8ba2022-11-22 15:09:07 -0800428 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
429 expectVSyncCallbackScheduleReceived(true);
430
Lloyd Pique24b0a482018-03-09 18:52:26 -0800431 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700432 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800433 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800434 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700435 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
437
438 // EventThread should also detect that at this point that it does not need
439 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800440 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800441}
442
Rachel Lee3f028662021-11-04 19:32:24 +0000443TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700444 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700445
Rachel Lee3f028662021-11-04 19:32:24 +0000446 // Signal that we want the next vsync event to be posted to the connection
447 mThread->requestNextVsync(mConnection);
448
Ady Abraham011f8ba2022-11-22 15:09:07 -0800449 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000450
451 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700452 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800453 onVSyncEvent(123, 456, 789);
454 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000455}
456
Rachel Lee0655a912023-04-20 19:54:18 -0700457TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700458 setupEventThread();
Rachel Lee40aef422023-04-25 14:35:47 -0700459 // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines,
460 // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal
461 // truncation (e.g. 60Hz has 16.6... ms vsync period).
Ady Abrahamf2851612023-09-25 17:19:00 -0700462 mVsyncPeriod = (scheduler::VsyncConfig::kEarlyLatchMaxThreshold /
463 (VsyncEventData::kFrameTimelinesCapacity - 2));
Rachel Lee0655a912023-04-20 19:54:18 -0700464
465 // Signal that we want the next vsync event to be posted to the connection
466 mThread->requestNextVsync(mConnection);
467
468 expectVSyncCallbackScheduleReceived(true);
469
470 // Use the received callback to signal a vsync event.
471 // The throttler should receive the event, as well as the connection.
472 nsecs_t expectedTimestamp = 123;
473 onVSyncEvent(expectedTimestamp, 456, 789);
474
475 auto args = mConnectionEventCallRecorder.waitForCall();
476 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
477 << expectedTimestamp;
478 const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData;
Ady Abrahamf2851612023-09-25 17:19:00 -0700479 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Lee0655a912023-04-20 19:54:18 -0700480}
481
Rachel Leeef2e21f2022-02-01 14:51:34 -0800482TEST_F(EventThreadTest, getLatestVsyncEventData) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700483 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700484
Rachel Leeef2e21f2022-02-01 14:51:34 -0800485 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800486 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800487 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
488
489 mock::VSyncTracker& mockTracker =
490 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
Ady Abraham4335afd2023-12-18 19:10:47 -0800491 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_, _))
Ady Abraham011f8ba2022-11-22 15:09:07 -0800492 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800493
494 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700495
496 // Check EventThread immediately requested a resync.
497 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
498
Ady Abrahamf2851612023-09-25 17:19:00 -0700499 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800500 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
501 << "Deadline timestamp should be greater than frame time";
Rachel Lee0655a912023-04-20 19:54:18 -0700502 for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800503 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800504 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800505 EXPECT_TRUE(prediction.has_value());
506 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
507 << "Deadline timestamp does not match cached value";
508 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800509 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800510 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800511 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800512 vsyncEventData.frameTimelines[i].deadlineTimestamp)
513 << "Expected vsync timestamp should be greater than deadline";
514
515 if (i > 0) {
516 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
517 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
518 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800519 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
520 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800521 << "Expected vsync timestamp out of order for frame timeline " << i;
522 }
523
524 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800525 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800526 << "Vsync ID incorrect for frame timeline " << i;
527 if (i == vsyncEventData.preferredFrameTimelineIndex) {
528 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
529 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800530 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
531 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800532 << "Preferred expected vsync timestamp incorrect" << i;
533 }
534 }
535}
536
Lloyd Pique24b0a482018-03-09 18:52:26 -0800537TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700538 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700539
Lloyd Pique24b0a482018-03-09 18:52:26 -0800540 // Create a first connection, register it, and request a vsync rate of zero.
541 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700542 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800543 mThread->setVsyncRate(0, firstConnection);
544
545 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800546 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800547
548 // However if there is another connection which wants events at a nonzero rate.....
549 ConnectionEventRecorder secondConnectionEventRecorder{0};
550 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700551 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800552 mThread->setVsyncRate(1, secondConnection);
553
Dominik Laskowski029cc122019-01-23 19:52:06 -0800554 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800555 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800556
557 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700558 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800559 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800560 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
562 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
563 1u);
564}
565
566TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700567 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700568
Lloyd Pique24b0a482018-03-09 18:52:26 -0800569 mThread->setVsyncRate(1, mConnection);
570
Dominik Laskowski029cc122019-01-23 19:52:06 -0800571 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800572 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800573
574 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700575 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800576 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500577 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800578 expectVsyncEventReceivedByConnection(123, 1u);
ramindaniae645822024-01-11 10:57:29 -0800579 expectOnExpectedPresentTimePosted(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800580
581 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800582 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500583 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800584 expectVsyncEventReceivedByConnection(456, 2u);
ramindaniae645822024-01-11 10:57:29 -0800585 expectOnExpectedPresentTimePosted(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800586
587 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800588 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500589 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800590 expectVsyncEventReceivedByConnection(789, 3u);
ramindaniae645822024-01-11 10:57:29 -0800591 expectOnExpectedPresentTimePosted(777);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800592}
593
594TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700595 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700596
Lloyd Pique24b0a482018-03-09 18:52:26 -0800597 mThread->setVsyncRate(2, mConnection);
598
Dominik Laskowski029cc122019-01-23 19:52:06 -0800599 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800600 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800601
Huihong Luoab8ffef2022-08-18 13:02:26 -0700602 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800603 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800604 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700605 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800606
Huihong Luoab8ffef2022-08-18 13:02:26 -0700607 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800608 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800609 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700610 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800611
Huihong Luoab8ffef2022-08-18 13:02:26 -0700612 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800613 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800614 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700615 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800616
Huihong Luoab8ffef2022-08-18 13:02:26 -0700617 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800618 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800619 expectVsyncEventReceivedByConnection(101112, 4u);
620}
621
622TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700623 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700624
Lloyd Pique24b0a482018-03-09 18:52:26 -0800625 mThread->setVsyncRate(1, mConnection);
626
Dominik Laskowski029cc122019-01-23 19:52:06 -0800627 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800628 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800629
630 // Destroy the only (strong) reference to the connection.
631 mConnection = nullptr;
632
Huihong Luoab8ffef2022-08-18 13:02:26 -0700633 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800634 onVSyncEvent(123, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800635 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
636
637 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800638 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800639}
640
641TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700642 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700643
Lloyd Pique24b0a482018-03-09 18:52:26 -0800644 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700645 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800646 mThread->setVsyncRate(1, errorConnection);
647
Dominik Laskowski029cc122019-01-23 19:52:06 -0800648 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800649 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800650
Huihong Luoab8ffef2022-08-18 13:02:26 -0700651 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800652 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800653 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
654
Ady Abraham011f8ba2022-11-22 15:09:07 -0800655 // Another schedule is expected, since the connection is removed only after
656 // the next vsync is requested.
657 expectVSyncCallbackScheduleReceived(true);
658
Huihong Luoab8ffef2022-08-18 13:02:26 -0700659 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800660 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800661 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
662
663 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800664 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800665}
666
667TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700668 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700669
Lloyd Pique24b0a482018-03-09 18:52:26 -0800670 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700671 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800672 mThread->setVsyncRate(1, errorConnection);
673
Dominik Laskowski029cc122019-01-23 19:52:06 -0800674 // EventThread should enable vsync callbacks.
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 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800678 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800679 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800680 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800681
Huihong Luoab8ffef2022-08-18 13:02:26 -0700682 // A subsequent event will be seen by the connection, which still then returns a non-fatal
683 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800684 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800685 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800686 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800687
688 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800689 onVSyncEvent(456, 123, 0);
690 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800691}
692
693TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700694 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700695
Ady Abraham9c53ee72020-07-22 21:16:18 -0700696 mThread->setDuration(321ns, 456ns);
697 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800698}
699
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800700TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700701 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700702
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800703 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
704 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800705}
706
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800707TEST_F(EventThreadTest, postHotplugInternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700708 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700709
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800710 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
711 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800712}
713
714TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700715 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700716
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800717 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
718 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800719}
720
721TEST_F(EventThreadTest, postHotplugExternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700722 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700723
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800724 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
725 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800726}
727
Ady Abraham447052e2019-02-13 16:07:27 -0800728TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700729 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700730
Ady Abraham690f4612021-07-01 23:24:03 -0700731 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
732 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
733 .setId(DisplayModeId(7))
734 .setVsyncPeriod(16666666)
735 .build();
ramindania04b8a52023-08-07 18:49:47 -0700736 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700737
Ady Abraham67434eb2022-12-01 17:48:12 -0800738 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
739 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800740}
741
742TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700743 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700744
Ady Abraham690f4612021-07-01 23:24:03 -0700745 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
746 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
747 .setId(DisplayModeId(5))
748 .setVsyncPeriod(16666666)
749 .build();
ramindania04b8a52023-08-07 18:49:47 -0700750 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700751
Ady Abraham67434eb2022-12-01 17:48:12 -0800752 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
753 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800754}
755
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700756TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700757 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700758
Ady Abraham690f4612021-07-01 23:24:03 -0700759 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
760 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
761 .setId(DisplayModeId(7))
762 .setVsyncPeriod(16666666)
763 .build();
ramindania04b8a52023-08-07 18:49:47 -0700764 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham67434eb2022-12-01 17:48:12 -0800765 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
766 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700767}
768
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700769TEST_F(EventThreadTest, suppressConfigChanged) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700770 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700771
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700772 ConnectionEventRecorder suppressConnectionEventRecorder{0};
773 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700774 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700775
Ady Abraham690f4612021-07-01 23:24:03 -0700776 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
777 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
778 .setId(DisplayModeId(9))
779 .setVsyncPeriod(16666666)
780 .build();
ramindania04b8a52023-08-07 18:49:47 -0700781 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700782
Ady Abraham67434eb2022-12-01 17:48:12 -0800783 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
784 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700785
786 auto args = suppressConnectionEventRecorder.waitForCall();
787 ASSERT_FALSE(args.has_value());
788}
789
Ady Abraham62f216c2020-10-13 19:07:23 -0700790TEST_F(EventThreadTest, postUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700791 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700792
Ady Abraham62f216c2020-10-13 19:07:23 -0700793 const std::vector<FrameRateOverride> overrides = {
794 {.uid = 1, .frameRateHz = 20},
795 {.uid = 3, .frameRateHz = 40},
796 {.uid = 5, .frameRateHz = 60},
797 };
798
799 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
800 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
801}
802
803TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700804 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700805
Ady Abraham62f216c2020-10-13 19:07:23 -0700806 const std::vector<FrameRateOverride> overrides = {
807 {.uid = 1, .frameRateHz = 20},
808 {.uid = 3, .frameRateHz = 40},
809 {.uid = 5, .frameRateHz = 60},
810 };
811
812 ConnectionEventRecorder suppressConnectionEventRecorder{0};
813 sp<MockEventThreadConnection> suppressConnection =
814 createConnection(suppressConnectionEventRecorder);
815
816 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
817 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
818
819 auto args = suppressConnectionEventRecorder.waitForCall();
820 ASSERT_FALSE(args.has_value());
821}
822
Ady Abraham0bb6a472020-10-12 10:22:13 -0700823TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700824 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700825
Ady Abraham0bb6a472020-10-12 10:22:13 -0700826 // Signal that we want the next vsync event to be posted to the throttled connection
827 mThread->requestNextVsync(mThrottledConnection);
828
829 // EventThread should immediately request a resync.
830 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
831
832 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800833 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700834
835 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700836 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800837 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500838 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700839 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800840 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700841
842 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700843 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700844 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800845 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500846 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700847 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800848 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700849
850 // EventThread should not change the vsync state as it didn't send the event
851 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800852 onVSyncEvent(456, 123, 0);
853 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700854}
855
Huihong Luocac92162023-12-21 13:52:42 -0800856TEST_F(EventThreadTest, postHcpLevelsChanged) {
857 setupEventThread();
858
859 mThread->onHdcpLevelsChanged(EXTERNAL_DISPLAY_ID, HDCP_V1, HDCP_V2);
860 auto args = mConnectionEventCallRecorder.waitForCall();
861 ASSERT_TRUE(args.has_value());
862 const auto& event = std::get<0>(args.value());
863 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE, event.header.type);
864 EXPECT_EQ(EXTERNAL_DISPLAY_ID, event.header.displayId);
865 EXPECT_EQ(HDCP_V1, event.hdcpLevelsChange.connectedLevel);
866 EXPECT_EQ(HDCP_V2, event.hdcpLevelsChange.maxLevel);
867}
868
Lloyd Pique24b0a482018-03-09 18:52:26 -0800869} // namespace
870} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100871
872// TODO(b/129481165): remove the #pragma below and fix conversion issues
873#pragma clang diagnostic pop // ignored "-Wextra"