blob: 625d2e68d8923e6d46698fe06e01bb5e01682c49 [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
ramindani32a88b12024-01-31 18:45:30 -0800120 static constexpr scheduler::ScheduleResult kScheduleResult{TimePoint::fromNs(0),
121 TimePoint::fromNs(0)};
Ady Abraham011f8ba2022-11-22 15:09:07 -0800122 AsyncCallRecorderWithCannedReturn<
123 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
124 scheduler::VSyncDispatch::ScheduleTiming)>
ramindani32a88b12024-01-31 18:45:30 -0800125 mVSyncCallbackScheduleRecorder{kScheduleResult};
Ady Abraham011f8ba2022-11-22 15:09:07 -0800126 AsyncCallRecorderWithCannedReturn<
127 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
128 scheduler::VSyncDispatch::ScheduleTiming)>
ramindani32a88b12024-01-31 18:45:30 -0800129 mVSyncCallbackUpdateRecorder{kScheduleResult};
Ady Abraham011f8ba2022-11-22 15:09:07 -0800130 AsyncCallRecorderWithCannedReturn<
131 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
132 std::string)>
133 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
134 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
135 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800136 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500137 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
ramindaniae645822024-01-11 10:57:29 -0800138 AsyncCallRecorder<void (*)(nsecs_t)> mOnExpectedPresentTimePostedRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800139 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700140 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800141
Leon Scroggins III67388622023-02-06 20:36:20 -0500142 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700143 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700145 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000146 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700147
Ady Abrahamf2851612023-09-25 17:19:00 -0700148 std::chrono::nanoseconds mVsyncPeriod;
149
Ady Abraham0bb6a472020-10-12 10:22:13 -0700150 static constexpr uid_t mConnectionUid = 443;
151 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800152};
153
154EventThreadTest::EventThreadTest() {
155 const ::testing::TestInfo* const test_info =
156 ::testing::UnitTest::GetInstance()->current_test_info();
157 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
158
Leon Scroggins III67388622023-02-06 20:36:20 -0500159 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
160 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
161 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
162 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
163 nullptr));
164 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800165 EXPECT_CALL(mockDispatch, registerCallback(_, _))
166 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
167 EXPECT_CALL(mockDispatch, schedule(_, _))
168 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
169 EXPECT_CALL(mockDispatch, update(_, _))
170 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
171 EXPECT_CALL(mockDispatch, unregisterCallback(_))
172 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800173}
174
175EventThreadTest::~EventThreadTest() {
176 const ::testing::TestInfo* const test_info =
177 ::testing::UnitTest::GetInstance()->current_test_info();
178 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800179
Ady Abraham011f8ba2022-11-22 15:09:07 -0800180 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800181 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800182 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800183}
184
Ady Abrahamf2851612023-09-25 17:19:00 -0700185bool EventThreadTest::throttleVsync(android::TimePoint expectedVsyncTimestamp, uid_t uid) {
186 mThrottleVsyncCallRecorder.recordCall(expectedVsyncTimestamp.ns(), uid);
187 return (uid == mThrottledConnectionUid);
188}
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500189
Ady Abrahamf2851612023-09-25 17:19:00 -0700190Period EventThreadTest::getVsyncPeriod(uid_t) {
191 return mVsyncPeriod;
192}
193
194void EventThreadTest::resync() {
195 mResyncCallRecorder.recordCall();
196}
197
ramindaniae645822024-01-11 10:57:29 -0800198void EventThreadTest::onExpectedPresentTimePosted(TimePoint expectedPresentTime) {
199 mOnExpectedPresentTimePostedRecorder.recordCall(expectedPresentTime.ns());
200}
201
Ady Abrahamf2851612023-09-25 17:19:00 -0700202void EventThreadTest::setupEventThread() {
Rachel Lee3f028662021-11-04 19:32:24 +0000203 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
Leon Scroggins III67388622023-02-06 20:36:20 -0500204 mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule,
Ady Abrahamf2851612023-09-25 17:19:00 -0700205 mTokenManager.get(), *this, kWorkDuration,
206 kReadyDuration);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800207
208 // EventThread should register itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800209 EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value());
Rachel Lee0655a912023-04-20 19:54:18 -0700210
211 mConnection =
212 createConnection(mConnectionEventCallRecorder,
213 gui::ISurfaceComposer::EventRegistration::modeChanged |
214 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
215 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
216 gui::ISurfaceComposer::EventRegistration::modeChanged,
217 mThrottledConnectionUid);
218
219 // A display must be connected for VSYNC events to be delivered.
220 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
221 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800222}
223
224sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700225 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
226 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800227 sp<MockEventThreadConnection> connection =
Ady Abrahamf2851612023-09-25 17:19:00 -0700228 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid, eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800229 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
230 return connection;
231}
232
Ady Abraham011f8ba2022-11-22 15:09:07 -0800233void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
234 if (expectState) {
235 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
236 } else {
237 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
238 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800239}
240
Ady Abraham9c53ee72020-07-22 21:16:18 -0700241void EventThreadTest::expectVSyncSetDurationCallReceived(
242 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800243 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800244 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800245 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
246 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800247}
248
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500249void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700250 auto args = mThrottleVsyncCallRecorder.waitForCall();
251 ASSERT_TRUE(args.has_value());
252 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
253 EXPECT_EQ(uid, std::get<1>(args.value()));
254}
255
ramindaniae645822024-01-11 10:57:29 -0800256void EventThreadTest::expectOnExpectedPresentTimePosted(nsecs_t expectedPresentTime) {
257 auto args = mOnExpectedPresentTimePostedRecorder.waitForCall();
258 ASSERT_TRUE(args.has_value());
259 EXPECT_EQ(expectedPresentTime, std::get<0>(args.value()));
260}
261
Lloyd Pique24b0a482018-03-09 18:52:26 -0800262void EventThreadTest::expectVsyncEventReceivedByConnection(
263 const char* name, ConnectionEventRecorder& connectionEventRecorder,
264 nsecs_t expectedTimestamp, unsigned expectedCount) {
265 auto args = connectionEventRecorder.waitForCall();
266 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
267 << expectedTimestamp;
268 const auto& event = std::get<0>(args.value());
269 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
270 << name << " did not get the correct event for timestamp " << expectedTimestamp;
271 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
272 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
273 EXPECT_EQ(expectedCount, event.vsync.count)
274 << name << " did not get the expected count for timestamp " << expectedTimestamp;
275}
276
277void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
278 unsigned expectedCount) {
279 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
280 mConnectionEventCallRecorder, expectedTimestamp,
281 expectedCount);
282}
283
Rachel Leeb9c5a772022-02-04 21:17:37 -0800284void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800285 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000286 auto args = mConnectionEventCallRecorder.waitForCall();
287 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
288 << expectedTimestamp;
289 const auto& event = std::get<0>(args.value());
Rachel Lee0655a912023-04-20 19:54:18 -0700290 for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800291 auto prediction = mTokenManager->getPredictionsForToken(
292 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000293 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800294 EXPECT_EQ(prediction.value().endTime,
295 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000296 << "Deadline timestamp does not match cached value";
297 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800298 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
299 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000300
Rachel Lee3f028662021-11-04 19:32:24 +0000301 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800302 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
303 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000304 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800305 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
306 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
307 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000308 }
Rachel Lee0d943202022-02-01 23:29:41 -0800309
310 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800311 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800312 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800313 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
314 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
315 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800316 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800317 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
318 preferredVsyncData.expectedPresentationTime)
319 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000320 }
321 }
322}
323
Ady Abrahamf2851612023-09-25 17:19:00 -0700324void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData) {
Rachel Lee0655a912023-04-20 19:54:18 -0700325 float nonPreferredTimelinesAmount =
Ady Abrahamf2851612023-09-25 17:19:00 -0700326 scheduler::VsyncConfig::kEarlyLatchMaxThreshold / mVsyncPeriod;
Rachel Lee0655a912023-04-20 19:54:18 -0700327 EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1)
328 << "Amount of non-preferred frame timelines too many;"
329 << " expected presentation time will be over threshold";
Rachel Lee40aef422023-04-25 14:35:47 -0700330 EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity)
Rachel Lee0655a912023-04-20 19:54:18 -0700331 << "Amount of non-preferred frame timelines should be less than max capacity";
332 EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0)
333 << "Frame timelines length should be greater than 0";
334 EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength)
335 << "Preferred frame timeline index should be less than frame timelines length";
336}
337
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800338void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
339 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800340 auto args = mConnectionEventCallRecorder.waitForCall();
341 ASSERT_TRUE(args.has_value());
342 const auto& event = std::get<0>(args.value());
343 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800344 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800345 EXPECT_EQ(expectedConnected, event.hotplug.connected);
346}
347
Ady Abraham447052e2019-02-13 16:07:27 -0800348void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700349 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
350 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800351 auto args = mConnectionEventCallRecorder.waitForCall();
352 ASSERT_TRUE(args.has_value());
353 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100354 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800355 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100356 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
357 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800358}
359
Ady Abraham62f216c2020-10-13 19:07:23 -0700360void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
361 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
362 for (const auto [uid, frameRateHz] : expectedOverrides) {
363 auto args = mConnectionEventCallRecorder.waitForCall();
364 ASSERT_TRUE(args.has_value());
365 const auto& event = std::get<0>(args.value());
366 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
367 EXPECT_EQ(expectedDisplayId, event.header.displayId);
368 EXPECT_EQ(uid, event.frameRateOverride.uid);
369 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
370 }
371
372 auto args = mConnectionEventCallRecorder.waitForCall();
373 ASSERT_TRUE(args.has_value());
374 const auto& event = std::get<0>(args.value());
375 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
376 EXPECT_EQ(expectedDisplayId, event.header.displayId);
377}
378
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379namespace {
380
Rachel Leeef2e21f2022-02-01 14:51:34 -0800381using namespace testing;
382
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383/* ------------------------------------------------------------------------
384 * Test cases
385 */
386
387TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700388 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700389
Ady Abraham011f8ba2022-11-22 15:09:07 -0800390 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
391 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
392 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
393 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800394 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800395 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
396}
397
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800398TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700399 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700400
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800401 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
402 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800403
404 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700405 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800406
407 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800408 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800409}
410
Lloyd Pique24b0a482018-03-09 18:52:26 -0800411TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700412 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700413
Lloyd Pique24b0a482018-03-09 18:52:26 -0800414 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700415 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416
Ady Abraham8532d012019-05-08 14:50:56 -0700417 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800418 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
419
Ady Abraham011f8ba2022-11-22 15:09:07 -0800420 // EventThread should enable schedule a vsync callback
421 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800422
423 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700424 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800425 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500426 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800427 expectVsyncEventReceivedByConnection(123, 1u);
ramindaniae645822024-01-11 10:57:29 -0800428 expectOnExpectedPresentTimePosted(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800429
Ady Abraham011f8ba2022-11-22 15:09:07 -0800430 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
431 expectVSyncCallbackScheduleReceived(true);
432
Lloyd Pique24b0a482018-03-09 18:52:26 -0800433 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700434 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800435 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800436 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700437 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800438 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
439
440 // EventThread should also detect that at this point that it does not need
441 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800442 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443}
444
Rachel Lee3f028662021-11-04 19:32:24 +0000445TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700446 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700447
Rachel Lee3f028662021-11-04 19:32:24 +0000448 // Signal that we want the next vsync event to be posted to the connection
449 mThread->requestNextVsync(mConnection);
450
Ady Abraham011f8ba2022-11-22 15:09:07 -0800451 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000452
453 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700454 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800455 onVSyncEvent(123, 456, 789);
456 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000457}
458
Rachel Lee0655a912023-04-20 19:54:18 -0700459TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700460 setupEventThread();
Rachel Lee40aef422023-04-25 14:35:47 -0700461 // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines,
462 // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal
463 // truncation (e.g. 60Hz has 16.6... ms vsync period).
Ady Abrahamf2851612023-09-25 17:19:00 -0700464 mVsyncPeriod = (scheduler::VsyncConfig::kEarlyLatchMaxThreshold /
465 (VsyncEventData::kFrameTimelinesCapacity - 2));
Rachel Lee0655a912023-04-20 19:54:18 -0700466
467 // Signal that we want the next vsync event to be posted to the connection
468 mThread->requestNextVsync(mConnection);
469
470 expectVSyncCallbackScheduleReceived(true);
471
472 // Use the received callback to signal a vsync event.
473 // The throttler should receive the event, as well as the connection.
474 nsecs_t expectedTimestamp = 123;
475 onVSyncEvent(expectedTimestamp, 456, 789);
476
477 auto args = mConnectionEventCallRecorder.waitForCall();
478 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
479 << expectedTimestamp;
480 const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData;
Ady Abrahamf2851612023-09-25 17:19:00 -0700481 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Lee0655a912023-04-20 19:54:18 -0700482}
483
Rachel Leeef2e21f2022-02-01 14:51:34 -0800484TEST_F(EventThreadTest, getLatestVsyncEventData) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700485 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700486
Rachel Leeef2e21f2022-02-01 14:51:34 -0800487 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800488 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800489 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
490
491 mock::VSyncTracker& mockTracker =
492 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
Ady Abraham4335afd2023-12-18 19:10:47 -0800493 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_, _))
Ady Abraham011f8ba2022-11-22 15:09:07 -0800494 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800495
Ady Abraham40ec5842024-04-04 13:22:38 -0700496 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection, now);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700497
498 // Check EventThread immediately requested a resync.
499 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
500
Ady Abrahamf2851612023-09-25 17:19:00 -0700501 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800502 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
503 << "Deadline timestamp should be greater than frame time";
Rachel Lee0655a912023-04-20 19:54:18 -0700504 for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800505 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800506 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800507 EXPECT_TRUE(prediction.has_value());
508 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
509 << "Deadline timestamp does not match cached value";
510 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800511 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800512 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800513 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800514 vsyncEventData.frameTimelines[i].deadlineTimestamp)
515 << "Expected vsync timestamp should be greater than deadline";
516
517 if (i > 0) {
518 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
519 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
520 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800521 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
522 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800523 << "Expected vsync timestamp out of order for frame timeline " << i;
524 }
525
526 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800527 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800528 << "Vsync ID incorrect for frame timeline " << i;
529 if (i == vsyncEventData.preferredFrameTimelineIndex) {
530 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
531 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800532 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
533 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800534 << "Preferred expected vsync timestamp incorrect" << i;
535 }
536 }
537}
538
Lloyd Pique24b0a482018-03-09 18:52:26 -0800539TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700540 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700541
Lloyd Pique24b0a482018-03-09 18:52:26 -0800542 // Create a first connection, register it, and request a vsync rate of zero.
543 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700544 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800545 mThread->setVsyncRate(0, firstConnection);
546
547 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800548 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800549
550 // However if there is another connection which wants events at a nonzero rate.....
551 ConnectionEventRecorder secondConnectionEventRecorder{0};
552 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700553 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800554 mThread->setVsyncRate(1, secondConnection);
555
Dominik Laskowski029cc122019-01-23 19:52:06 -0800556 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800557 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800558
559 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700560 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800562 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800563 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
564 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
565 1u);
566}
567
568TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700569 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700570
Lloyd Pique24b0a482018-03-09 18:52:26 -0800571 mThread->setVsyncRate(1, mConnection);
572
Dominik Laskowski029cc122019-01-23 19:52:06 -0800573 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800574 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800575
576 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700577 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800578 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500579 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800580 expectVsyncEventReceivedByConnection(123, 1u);
ramindaniae645822024-01-11 10:57:29 -0800581 expectOnExpectedPresentTimePosted(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800582
583 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800584 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500585 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800586 expectVsyncEventReceivedByConnection(456, 2u);
ramindaniae645822024-01-11 10:57:29 -0800587 expectOnExpectedPresentTimePosted(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800588
589 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800590 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500591 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800592 expectVsyncEventReceivedByConnection(789, 3u);
ramindaniae645822024-01-11 10:57:29 -0800593 expectOnExpectedPresentTimePosted(777);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800594}
595
596TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700597 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700598
Lloyd Pique24b0a482018-03-09 18:52:26 -0800599 mThread->setVsyncRate(2, mConnection);
600
Dominik Laskowski029cc122019-01-23 19:52:06 -0800601 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800602 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800603
Huihong Luoab8ffef2022-08-18 13:02:26 -0700604 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800605 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800606 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700607 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800608
Huihong Luoab8ffef2022-08-18 13:02:26 -0700609 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800610 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800611 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700612 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800613
Huihong Luoab8ffef2022-08-18 13:02:26 -0700614 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800615 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800616 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700617 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618
Huihong Luoab8ffef2022-08-18 13:02:26 -0700619 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800620 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800621 expectVsyncEventReceivedByConnection(101112, 4u);
622}
623
624TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700625 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700626
Lloyd Pique24b0a482018-03-09 18:52:26 -0800627 mThread->setVsyncRate(1, mConnection);
628
Dominik Laskowski029cc122019-01-23 19:52:06 -0800629 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800630 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800631
632 // Destroy the only (strong) reference to the connection.
633 mConnection = nullptr;
634
Huihong Luoab8ffef2022-08-18 13:02:26 -0700635 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800636 onVSyncEvent(123, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800637 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
638
639 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800640 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800641}
642
643TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700644 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700645
Lloyd Pique24b0a482018-03-09 18:52:26 -0800646 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700647 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800648 mThread->setVsyncRate(1, errorConnection);
649
Dominik Laskowski029cc122019-01-23 19:52:06 -0800650 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800651 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800652
Huihong Luoab8ffef2022-08-18 13:02:26 -0700653 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800654 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800655 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
656
Ady Abraham011f8ba2022-11-22 15:09:07 -0800657 // Another schedule is expected, since the connection is removed only after
658 // the next vsync is requested.
659 expectVSyncCallbackScheduleReceived(true);
660
Huihong Luoab8ffef2022-08-18 13:02:26 -0700661 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800662 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800663 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
664
665 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800666 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800667}
668
669TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700670 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700671
Lloyd Pique24b0a482018-03-09 18:52:26 -0800672 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700673 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800674 mThread->setVsyncRate(1, errorConnection);
675
Dominik Laskowski029cc122019-01-23 19:52:06 -0800676 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800677 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800678
Huihong Luoab8ffef2022-08-18 13:02:26 -0700679 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800680 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800681 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800682 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800683
Huihong Luoab8ffef2022-08-18 13:02:26 -0700684 // A subsequent event will be seen by the connection, which still then returns a non-fatal
685 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800686 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800687 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800688 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800689
690 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800691 onVSyncEvent(456, 123, 0);
692 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800693}
694
695TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700696 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700697
Ady Abraham9c53ee72020-07-22 21:16:18 -0700698 mThread->setDuration(321ns, 456ns);
699 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800700}
701
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800702TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700703 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700704
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800705 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
706 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800707}
708
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800709TEST_F(EventThreadTest, postHotplugInternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700710 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700711
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800712 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
713 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800714}
715
716TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700717 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700718
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800719 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
720 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800721}
722
723TEST_F(EventThreadTest, postHotplugExternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700724 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700725
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800726 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
727 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800728}
729
Ady Abraham447052e2019-02-13 16:07:27 -0800730TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700731 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700732
Ady Abraham690f4612021-07-01 23:24:03 -0700733 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
734 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
735 .setId(DisplayModeId(7))
736 .setVsyncPeriod(16666666)
737 .build();
ramindania04b8a52023-08-07 18:49:47 -0700738 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700739
Ady Abraham67434eb2022-12-01 17:48:12 -0800740 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
741 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800742}
743
744TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700745 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700746
Ady Abraham690f4612021-07-01 23:24:03 -0700747 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
748 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
749 .setId(DisplayModeId(5))
750 .setVsyncPeriod(16666666)
751 .build();
ramindania04b8a52023-08-07 18:49:47 -0700752 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700753
Ady Abraham67434eb2022-12-01 17:48:12 -0800754 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
755 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800756}
757
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700758TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700759 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700760
Ady Abraham690f4612021-07-01 23:24:03 -0700761 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
762 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
763 .setId(DisplayModeId(7))
764 .setVsyncPeriod(16666666)
765 .build();
ramindania04b8a52023-08-07 18:49:47 -0700766 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham67434eb2022-12-01 17:48:12 -0800767 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
768 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700769}
770
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700771TEST_F(EventThreadTest, suppressConfigChanged) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700772 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700773
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700774 ConnectionEventRecorder suppressConnectionEventRecorder{0};
775 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700776 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700777
Ady Abraham690f4612021-07-01 23:24:03 -0700778 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
779 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
780 .setId(DisplayModeId(9))
781 .setVsyncPeriod(16666666)
782 .build();
ramindania04b8a52023-08-07 18:49:47 -0700783 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700784
Ady Abraham67434eb2022-12-01 17:48:12 -0800785 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
786 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700787
788 auto args = suppressConnectionEventRecorder.waitForCall();
789 ASSERT_FALSE(args.has_value());
790}
791
Ady Abraham62f216c2020-10-13 19:07:23 -0700792TEST_F(EventThreadTest, postUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700793 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700794
Ady Abraham62f216c2020-10-13 19:07:23 -0700795 const std::vector<FrameRateOverride> overrides = {
796 {.uid = 1, .frameRateHz = 20},
797 {.uid = 3, .frameRateHz = 40},
798 {.uid = 5, .frameRateHz = 60},
799 };
800
801 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
802 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
803}
804
805TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700806 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700807
Ady Abraham62f216c2020-10-13 19:07:23 -0700808 const std::vector<FrameRateOverride> overrides = {
809 {.uid = 1, .frameRateHz = 20},
810 {.uid = 3, .frameRateHz = 40},
811 {.uid = 5, .frameRateHz = 60},
812 };
813
814 ConnectionEventRecorder suppressConnectionEventRecorder{0};
815 sp<MockEventThreadConnection> suppressConnection =
816 createConnection(suppressConnectionEventRecorder);
817
818 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
819 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
820
821 auto args = suppressConnectionEventRecorder.waitForCall();
822 ASSERT_FALSE(args.has_value());
823}
824
Ady Abraham0bb6a472020-10-12 10:22:13 -0700825TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700826 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700827
Ady Abraham0bb6a472020-10-12 10:22:13 -0700828 // Signal that we want the next vsync event to be posted to the throttled connection
829 mThread->requestNextVsync(mThrottledConnection);
830
831 // EventThread should immediately request a resync.
832 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
833
834 // EventThread should enable vsync callbacks.
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 first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700838 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800839 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500840 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700841 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800842 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700843
844 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700845 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700846 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800847 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500848 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700849 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800850 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700851
852 // EventThread should not change the vsync state as it didn't send the event
853 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800854 onVSyncEvent(456, 123, 0);
855 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700856}
857
Huihong Luocac92162023-12-21 13:52:42 -0800858TEST_F(EventThreadTest, postHcpLevelsChanged) {
859 setupEventThread();
860
861 mThread->onHdcpLevelsChanged(EXTERNAL_DISPLAY_ID, HDCP_V1, HDCP_V2);
862 auto args = mConnectionEventCallRecorder.waitForCall();
863 ASSERT_TRUE(args.has_value());
864 const auto& event = std::get<0>(args.value());
865 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE, event.header.type);
866 EXPECT_EQ(EXTERNAL_DISPLAY_ID, event.header.displayId);
867 EXPECT_EQ(HDCP_V1, event.hdcpLevelsChange.connectedLevel);
868 EXPECT_EQ(HDCP_V2, event.hdcpLevelsChange.maxLevel);
869}
870
Lloyd Pique24b0a482018-03-09 18:52:26 -0800871} // namespace
872} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100873
874// TODO(b/129481165): remove the #pragma below and fix conversion issues
875#pragma clang diagnostic pop // ignored "-Wextra"