blob: 268a6c416d37f50c0d8e0221d31a5caa3d87f37c [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>
Melody Hsue524dd92024-08-27 22:27:29 +000026#include <gui/DisplayEventReceiver.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080027#include <log/log.h>
Rachel Lee0655a912023-04-20 19:54:18 -070028#include <scheduler/VsyncConfig.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080029#include <utils/Errors.h>
30
31#include "AsyncCallRecorder.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010032#include "DisplayHardware/DisplayMode.h"
Rachel Lee3f028662021-11-04 19:32:24 +000033#include "FrameTimeline.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070034#include "Scheduler/EventThread.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080035#include "mock/MockVSyncDispatch.h"
36#include "mock/MockVSyncTracker.h"
37#include "mock/MockVsyncController.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080038
39using namespace std::chrono_literals;
40using namespace std::placeholders;
41
42using testing::_;
43using testing::Invoke;
Ady Abraham011f8ba2022-11-22 15:09:07 -080044using testing::Return;
Lloyd Pique24b0a482018-03-09 18:52:26 -080045
46namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080047
Dominik Laskowski2f01d772022-03-23 16:01:29 -070048using namespace ftl::flag_operators;
49
Lloyd Pique24b0a482018-03-09 18:52:26 -080050namespace {
51
Dominik Laskowskif1833852021-03-23 15:06:50 -070052constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
53constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
54constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
55 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
56
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050057constexpr std::chrono::duration VSYNC_PERIOD(16ms);
58
Huihong Luocac92162023-12-21 13:52:42 -080059constexpr int HDCP_V1 = 2;
60constexpr int HDCP_V2 = 3;
61
Lloyd Pique24b0a482018-03-09 18:52:26 -080062} // namespace
63
Ady Abrahamf2851612023-09-25 17:19:00 -070064class EventThreadTest : public testing::Test, public IEventThreadCallback {
Lloyd Pique24b0a482018-03-09 18:52:26 -080065protected:
Ady Abraham011f8ba2022-11-22 15:09:07 -080066 static constexpr std::chrono::nanoseconds kWorkDuration = 0ms;
67 static constexpr std::chrono::nanoseconds kReadyDuration = 3ms;
68
Dominik Laskowskif654d572018-12-20 11:03:06 -080069 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080070 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070071 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070072 EventRegistrationFlags eventRegistration)
Ady Abrahamf2851612023-09-25 17:19:00 -070073 : EventThreadConnection(eventThread, callingUid, eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080074 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
75 };
76
77 using ConnectionEventRecorder =
78 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
79
80 EventThreadTest();
81 ~EventThreadTest() override;
82
Ady Abrahamf2851612023-09-25 17:19:00 -070083 void SetUp() override { mVsyncPeriod = VSYNC_PERIOD; }
84
85 // IEventThreadCallback overrides
86 bool throttleVsync(TimePoint, uid_t) override;
87 Period getVsyncPeriod(uid_t) override;
88 void resync() override;
ramindaniae645822024-01-11 10:57:29 -080089 void onExpectedPresentTimePosted(TimePoint) override;
Ady Abrahamf2851612023-09-25 17:19:00 -070090
91 void setupEventThread();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070092 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
93 EventRegistrationFlags eventRegistration = {},
94 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080095
Ady Abraham011f8ba2022-11-22 15:09:07 -080096 void expectVSyncCallbackScheduleReceived(bool expectState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070097 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
98 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080099 void expectVsyncEventReceivedByConnection(const char* name,
100 ConnectionEventRecorder& connectionEventRecorder,
101 nsecs_t expectedTimestamp, unsigned expectedCount);
102 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800103 void expectVsyncEventFrameTimelinesCorrect(
Rachel Lee0655a912023-04-20 19:54:18 -0700104 nsecs_t expectedTimestamp, gui::VsyncEventData::FrameTimeline preferredVsyncData);
Ady Abrahamf2851612023-09-25 17:19:00 -0700105 void expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800106 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700107 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800108 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700109 int32_t expectedConfigId,
110 nsecs_t expectedVsyncPeriod);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500111 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
ramindaniae645822024-01-11 10:57:29 -0800112 void expectOnExpectedPresentTimePosted(nsecs_t expectedPresentTime);
Ady Abraham62f216c2020-10-13 19:07:23 -0700113 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
114 std::vector<FrameRateOverride>);
Melody Hsue524dd92024-08-27 22:27:29 +0000115 void expectQueuedBufferCountReceivedByConnection(
116 ConnectionEventRecorder& connectionEventRecorder, uint32_t expectedBufferCount);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800117
Ady Abraham011f8ba2022-11-22 15:09:07 -0800118 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime,
119 nsecs_t deadlineTimestamp) {
120 mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp);
121 }
122
ramindani32a88b12024-01-31 18:45:30 -0800123 static constexpr scheduler::ScheduleResult kScheduleResult{TimePoint::fromNs(0),
124 TimePoint::fromNs(0)};
Ady Abraham011f8ba2022-11-22 15:09:07 -0800125 AsyncCallRecorderWithCannedReturn<
126 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
127 scheduler::VSyncDispatch::ScheduleTiming)>
ramindani32a88b12024-01-31 18:45:30 -0800128 mVSyncCallbackScheduleRecorder{kScheduleResult};
Ady Abraham011f8ba2022-11-22 15:09:07 -0800129 AsyncCallRecorderWithCannedReturn<
130 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
131 scheduler::VSyncDispatch::ScheduleTiming)>
ramindani32a88b12024-01-31 18:45:30 -0800132 mVSyncCallbackUpdateRecorder{kScheduleResult};
Ady Abraham011f8ba2022-11-22 15:09:07 -0800133 AsyncCallRecorderWithCannedReturn<
134 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
135 std::string)>
136 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
137 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
138 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800139 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500140 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
ramindaniae645822024-01-11 10:57:29 -0800141 AsyncCallRecorder<void (*)(nsecs_t)> mOnExpectedPresentTimePostedRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800142 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700143 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144
Leon Scroggins III67388622023-02-06 20:36:20 -0500145 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700146 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800147 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700148 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000149 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Melody Hsue524dd92024-08-27 22:27:29 +0000150 std::vector<ConnectionEventRecorder*> mBufferStuffedConnectionRecorders;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700151
Ady Abrahamf2851612023-09-25 17:19:00 -0700152 std::chrono::nanoseconds mVsyncPeriod;
153
Ady Abraham0bb6a472020-10-12 10:22:13 -0700154 static constexpr uid_t mConnectionUid = 443;
155 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800156};
157
158EventThreadTest::EventThreadTest() {
159 const ::testing::TestInfo* const test_info =
160 ::testing::UnitTest::GetInstance()->current_test_info();
161 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
162
Leon Scroggins III67388622023-02-06 20:36:20 -0500163 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
164 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
165 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
166 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
167 nullptr));
168 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800169 EXPECT_CALL(mockDispatch, registerCallback(_, _))
170 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
171 EXPECT_CALL(mockDispatch, schedule(_, _))
172 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
173 EXPECT_CALL(mockDispatch, update(_, _))
174 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
175 EXPECT_CALL(mockDispatch, unregisterCallback(_))
176 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800177}
178
179EventThreadTest::~EventThreadTest() {
180 const ::testing::TestInfo* const test_info =
181 ::testing::UnitTest::GetInstance()->current_test_info();
182 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800183
Ady Abraham011f8ba2022-11-22 15:09:07 -0800184 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800185 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800186 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800187}
188
Ady Abrahamf2851612023-09-25 17:19:00 -0700189bool EventThreadTest::throttleVsync(android::TimePoint expectedVsyncTimestamp, uid_t uid) {
190 mThrottleVsyncCallRecorder.recordCall(expectedVsyncTimestamp.ns(), uid);
191 return (uid == mThrottledConnectionUid);
192}
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500193
Ady Abrahamf2851612023-09-25 17:19:00 -0700194Period EventThreadTest::getVsyncPeriod(uid_t) {
195 return mVsyncPeriod;
196}
197
198void EventThreadTest::resync() {
199 mResyncCallRecorder.recordCall();
200}
201
ramindaniae645822024-01-11 10:57:29 -0800202void EventThreadTest::onExpectedPresentTimePosted(TimePoint expectedPresentTime) {
203 mOnExpectedPresentTimePostedRecorder.recordCall(expectedPresentTime.ns());
204}
205
Ady Abrahamf2851612023-09-25 17:19:00 -0700206void EventThreadTest::setupEventThread() {
Rachel Lee3f028662021-11-04 19:32:24 +0000207 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
Leon Scroggins III67388622023-02-06 20:36:20 -0500208 mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule,
Ady Abrahamf2851612023-09-25 17:19:00 -0700209 mTokenManager.get(), *this, kWorkDuration,
210 kReadyDuration);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800211
212 // EventThread should register itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800213 EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value());
Rachel Lee0655a912023-04-20 19:54:18 -0700214
215 mConnection =
216 createConnection(mConnectionEventCallRecorder,
217 gui::ISurfaceComposer::EventRegistration::modeChanged |
218 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
219 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
220 gui::ISurfaceComposer::EventRegistration::modeChanged,
221 mThrottledConnectionUid);
222
223 // A display must be connected for VSYNC events to be delivered.
224 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
225 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800226}
227
228sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700229 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
230 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800231 sp<MockEventThreadConnection> connection =
Ady Abrahamf2851612023-09-25 17:19:00 -0700232 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid, eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800233 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
234 return connection;
235}
236
Ady Abraham011f8ba2022-11-22 15:09:07 -0800237void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
238 if (expectState) {
239 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
240 } else {
241 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
242 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800243}
244
Ady Abraham9c53ee72020-07-22 21:16:18 -0700245void EventThreadTest::expectVSyncSetDurationCallReceived(
246 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800247 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800248 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800249 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
250 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800251}
252
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500253void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700254 auto args = mThrottleVsyncCallRecorder.waitForCall();
255 ASSERT_TRUE(args.has_value());
256 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
257 EXPECT_EQ(uid, std::get<1>(args.value()));
258}
259
ramindaniae645822024-01-11 10:57:29 -0800260void EventThreadTest::expectOnExpectedPresentTimePosted(nsecs_t expectedPresentTime) {
261 auto args = mOnExpectedPresentTimePostedRecorder.waitForCall();
262 ASSERT_TRUE(args.has_value());
263 EXPECT_EQ(expectedPresentTime, std::get<0>(args.value()));
264}
265
Lloyd Pique24b0a482018-03-09 18:52:26 -0800266void EventThreadTest::expectVsyncEventReceivedByConnection(
267 const char* name, ConnectionEventRecorder& connectionEventRecorder,
268 nsecs_t expectedTimestamp, unsigned expectedCount) {
269 auto args = connectionEventRecorder.waitForCall();
270 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
271 << expectedTimestamp;
272 const auto& event = std::get<0>(args.value());
273 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
274 << name << " did not get the correct event for timestamp " << expectedTimestamp;
275 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
276 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
277 EXPECT_EQ(expectedCount, event.vsync.count)
278 << name << " did not get the expected count for timestamp " << expectedTimestamp;
279}
280
281void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
282 unsigned expectedCount) {
283 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
284 mConnectionEventCallRecorder, expectedTimestamp,
285 expectedCount);
286}
287
Rachel Leeb9c5a772022-02-04 21:17:37 -0800288void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800289 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000290 auto args = mConnectionEventCallRecorder.waitForCall();
291 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
292 << expectedTimestamp;
293 const auto& event = std::get<0>(args.value());
Rachel Lee0655a912023-04-20 19:54:18 -0700294 for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800295 auto prediction = mTokenManager->getPredictionsForToken(
296 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000297 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800298 EXPECT_EQ(prediction.value().endTime,
299 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000300 << "Deadline timestamp does not match cached value";
301 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800302 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
303 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000304
Rachel Lee3f028662021-11-04 19:32:24 +0000305 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800306 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
307 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000308 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800309 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
310 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
311 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000312 }
Rachel Lee0d943202022-02-01 23:29:41 -0800313
314 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800315 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800316 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800317 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
318 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
319 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800320 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800321 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
322 preferredVsyncData.expectedPresentationTime)
323 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000324 }
325 }
326}
327
Ady Abrahamf2851612023-09-25 17:19:00 -0700328void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData) {
Rachel Lee0655a912023-04-20 19:54:18 -0700329 float nonPreferredTimelinesAmount =
Ady Abrahamf2851612023-09-25 17:19:00 -0700330 scheduler::VsyncConfig::kEarlyLatchMaxThreshold / mVsyncPeriod;
Rachel Lee0655a912023-04-20 19:54:18 -0700331 EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1)
332 << "Amount of non-preferred frame timelines too many;"
333 << " expected presentation time will be over threshold";
Rachel Lee40aef422023-04-25 14:35:47 -0700334 EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity)
Rachel Lee0655a912023-04-20 19:54:18 -0700335 << "Amount of non-preferred frame timelines should be less than max capacity";
336 EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0)
337 << "Frame timelines length should be greater than 0";
338 EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength)
339 << "Preferred frame timeline index should be less than frame timelines length";
340}
341
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800342void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
343 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800344 auto args = mConnectionEventCallRecorder.waitForCall();
345 ASSERT_TRUE(args.has_value());
346 const auto& event = std::get<0>(args.value());
347 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800348 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800349 EXPECT_EQ(expectedConnected, event.hotplug.connected);
350}
351
Ady Abraham447052e2019-02-13 16:07:27 -0800352void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700353 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
354 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800355 auto args = mConnectionEventCallRecorder.waitForCall();
356 ASSERT_TRUE(args.has_value());
357 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100358 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800359 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100360 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
361 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800362}
363
Ady Abraham62f216c2020-10-13 19:07:23 -0700364void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
365 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
366 for (const auto [uid, frameRateHz] : expectedOverrides) {
367 auto args = mConnectionEventCallRecorder.waitForCall();
368 ASSERT_TRUE(args.has_value());
369 const auto& event = std::get<0>(args.value());
370 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
371 EXPECT_EQ(expectedDisplayId, event.header.displayId);
372 EXPECT_EQ(uid, event.frameRateOverride.uid);
373 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
374 }
375
376 auto args = mConnectionEventCallRecorder.waitForCall();
377 ASSERT_TRUE(args.has_value());
378 const auto& event = std::get<0>(args.value());
379 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
380 EXPECT_EQ(expectedDisplayId, event.header.displayId);
381}
382
Melody Hsue524dd92024-08-27 22:27:29 +0000383void EventThreadTest::expectQueuedBufferCountReceivedByConnection(
384 ConnectionEventRecorder& connectionEventRecorder, uint32_t expectedBufferCount) {
385 auto args = connectionEventRecorder.waitForCall();
386 ASSERT_TRUE(args.has_value());
387 const auto& event = std::get<0>(args.value());
388 EXPECT_EQ(expectedBufferCount, event.vsync.vsyncData.numberQueuedBuffers);
389}
390
Lloyd Pique24b0a482018-03-09 18:52:26 -0800391namespace {
392
Rachel Leeef2e21f2022-02-01 14:51:34 -0800393using namespace testing;
394
Lloyd Pique24b0a482018-03-09 18:52:26 -0800395/* ------------------------------------------------------------------------
396 * Test cases
397 */
398
399TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700400 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700401
Ady Abraham011f8ba2022-11-22 15:09:07 -0800402 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
403 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
404 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
405 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
408}
409
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800410TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700411 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700412
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800413 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
414 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800415
416 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700417 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800418
419 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800420 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800421}
422
Lloyd Pique24b0a482018-03-09 18:52:26 -0800423TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700424 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700425
Lloyd Pique24b0a482018-03-09 18:52:26 -0800426 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700427 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800428
Ady Abraham8532d012019-05-08 14:50:56 -0700429 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800430 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
431
Ady Abraham011f8ba2022-11-22 15:09:07 -0800432 // EventThread should enable schedule a vsync callback
433 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800434
435 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700436 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800437 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500438 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800439 expectVsyncEventReceivedByConnection(123, 1u);
ramindaniae645822024-01-11 10:57:29 -0800440 expectOnExpectedPresentTimePosted(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800441
Ady Abraham011f8ba2022-11-22 15:09:07 -0800442 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
443 expectVSyncCallbackScheduleReceived(true);
444
Lloyd Pique24b0a482018-03-09 18:52:26 -0800445 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700446 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800447 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800448 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700449 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800450 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
451
452 // EventThread should also detect that at this point that it does not need
453 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800454 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800455}
456
Rachel Lee3f028662021-11-04 19:32:24 +0000457TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700458 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700459
Rachel Lee3f028662021-11-04 19:32:24 +0000460 // Signal that we want the next vsync event to be posted to the connection
461 mThread->requestNextVsync(mConnection);
462
Ady Abraham011f8ba2022-11-22 15:09:07 -0800463 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000464
465 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700466 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800467 onVSyncEvent(123, 456, 789);
468 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000469}
470
Rachel Lee0655a912023-04-20 19:54:18 -0700471TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700472 setupEventThread();
Rachel Lee40aef422023-04-25 14:35:47 -0700473 // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines,
474 // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal
475 // truncation (e.g. 60Hz has 16.6... ms vsync period).
Ady Abrahamf2851612023-09-25 17:19:00 -0700476 mVsyncPeriod = (scheduler::VsyncConfig::kEarlyLatchMaxThreshold /
477 (VsyncEventData::kFrameTimelinesCapacity - 2));
Rachel Lee0655a912023-04-20 19:54:18 -0700478
479 // Signal that we want the next vsync event to be posted to the connection
480 mThread->requestNextVsync(mConnection);
481
482 expectVSyncCallbackScheduleReceived(true);
483
484 // Use the received callback to signal a vsync event.
485 // The throttler should receive the event, as well as the connection.
486 nsecs_t expectedTimestamp = 123;
487 onVSyncEvent(expectedTimestamp, 456, 789);
488
489 auto args = mConnectionEventCallRecorder.waitForCall();
490 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
491 << expectedTimestamp;
492 const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData;
Ady Abrahamf2851612023-09-25 17:19:00 -0700493 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Lee0655a912023-04-20 19:54:18 -0700494}
495
Rachel Leeef2e21f2022-02-01 14:51:34 -0800496TEST_F(EventThreadTest, getLatestVsyncEventData) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700497 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700498
Rachel Leeef2e21f2022-02-01 14:51:34 -0800499 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800500 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800501 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
502
503 mock::VSyncTracker& mockTracker =
504 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
Ady Abraham4335afd2023-12-18 19:10:47 -0800505 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_, _))
Ady Abraham011f8ba2022-11-22 15:09:07 -0800506 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800507
Ady Abraham40ec5842024-04-04 13:22:38 -0700508 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection, now);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700509
510 // Check EventThread immediately requested a resync.
511 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
512
Ady Abrahamf2851612023-09-25 17:19:00 -0700513 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800514 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
515 << "Deadline timestamp should be greater than frame time";
Rachel Lee0655a912023-04-20 19:54:18 -0700516 for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800517 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800518 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800519 EXPECT_TRUE(prediction.has_value());
520 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
521 << "Deadline timestamp does not match cached value";
522 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800523 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800524 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800525 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800526 vsyncEventData.frameTimelines[i].deadlineTimestamp)
527 << "Expected vsync timestamp should be greater than deadline";
528
529 if (i > 0) {
530 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
531 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
532 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800533 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
534 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800535 << "Expected vsync timestamp out of order for frame timeline " << i;
536 }
537
538 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800539 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800540 << "Vsync ID incorrect for frame timeline " << i;
541 if (i == vsyncEventData.preferredFrameTimelineIndex) {
542 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
543 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800544 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
545 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800546 << "Preferred expected vsync timestamp incorrect" << i;
547 }
548 }
549}
550
Lloyd Pique24b0a482018-03-09 18:52:26 -0800551TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700552 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700553
Lloyd Pique24b0a482018-03-09 18:52:26 -0800554 // Create a first connection, register it, and request a vsync rate of zero.
555 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700556 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800557 mThread->setVsyncRate(0, firstConnection);
558
559 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800560 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561
562 // However if there is another connection which wants events at a nonzero rate.....
563 ConnectionEventRecorder secondConnectionEventRecorder{0};
564 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700565 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800566 mThread->setVsyncRate(1, secondConnection);
567
Dominik Laskowski029cc122019-01-23 19:52:06 -0800568 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800569 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800570
571 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700572 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800573 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800574 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800575 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
576 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
577 1u);
578}
579
580TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700581 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700582
Lloyd Pique24b0a482018-03-09 18:52:26 -0800583 mThread->setVsyncRate(1, mConnection);
584
Dominik Laskowski029cc122019-01-23 19:52:06 -0800585 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800586 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800587
588 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700589 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800590 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500591 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800592 expectVsyncEventReceivedByConnection(123, 1u);
ramindaniae645822024-01-11 10:57:29 -0800593 expectOnExpectedPresentTimePosted(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800594
595 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800596 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500597 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800598 expectVsyncEventReceivedByConnection(456, 2u);
ramindaniae645822024-01-11 10:57:29 -0800599 expectOnExpectedPresentTimePosted(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800600
601 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800602 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500603 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800604 expectVsyncEventReceivedByConnection(789, 3u);
ramindaniae645822024-01-11 10:57:29 -0800605 expectOnExpectedPresentTimePosted(777);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800606}
607
608TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700609 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700610
Lloyd Pique24b0a482018-03-09 18:52:26 -0800611 mThread->setVsyncRate(2, mConnection);
612
Dominik Laskowski029cc122019-01-23 19:52:06 -0800613 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800614 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800615
Huihong Luoab8ffef2022-08-18 13:02:26 -0700616 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800617 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700619 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800620
Huihong Luoab8ffef2022-08-18 13:02:26 -0700621 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800622 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800623 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700624 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800625
Huihong Luoab8ffef2022-08-18 13:02:26 -0700626 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800627 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800628 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700629 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800630
Huihong Luoab8ffef2022-08-18 13:02:26 -0700631 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800632 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800633 expectVsyncEventReceivedByConnection(101112, 4u);
634}
635
636TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700637 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700638
Lloyd Pique24b0a482018-03-09 18:52:26 -0800639 mThread->setVsyncRate(1, mConnection);
640
Dominik Laskowski029cc122019-01-23 19:52:06 -0800641 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800642 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800643
644 // Destroy the only (strong) reference to the connection.
645 mConnection = nullptr;
646
Huihong Luoab8ffef2022-08-18 13:02:26 -0700647 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800648 onVSyncEvent(123, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800649 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
650
651 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800652 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800653}
654
655TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700656 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700657
Lloyd Pique24b0a482018-03-09 18:52:26 -0800658 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700659 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800660 mThread->setVsyncRate(1, errorConnection);
661
Dominik Laskowski029cc122019-01-23 19:52:06 -0800662 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800663 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800664
Huihong Luoab8ffef2022-08-18 13:02:26 -0700665 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800666 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800667 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
668
Ady Abraham011f8ba2022-11-22 15:09:07 -0800669 // Another schedule is expected, since the connection is removed only after
670 // the next vsync is requested.
671 expectVSyncCallbackScheduleReceived(true);
672
Huihong Luoab8ffef2022-08-18 13:02:26 -0700673 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800674 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800675 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
676
677 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800678 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800679}
680
681TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700682 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700683
Lloyd Pique24b0a482018-03-09 18:52:26 -0800684 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700685 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800686 mThread->setVsyncRate(1, errorConnection);
687
Dominik Laskowski029cc122019-01-23 19:52:06 -0800688 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800689 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800690
Huihong Luoab8ffef2022-08-18 13:02:26 -0700691 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800692 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800693 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800694 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800695
Huihong Luoab8ffef2022-08-18 13:02:26 -0700696 // A subsequent event will be seen by the connection, which still then returns a non-fatal
697 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800698 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800699 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800700 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800701
702 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800703 onVSyncEvent(456, 123, 0);
704 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800705}
706
707TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700708 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700709
Ady Abraham9c53ee72020-07-22 21:16:18 -0700710 mThread->setDuration(321ns, 456ns);
711 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800712}
713
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800714TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
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(INTERNAL_DISPLAY_ID, false);
718 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800719}
720
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800721TEST_F(EventThreadTest, postHotplugInternalConnect) {
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(INTERNAL_DISPLAY_ID, true);
725 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800726}
727
728TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700729 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700730
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800731 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
732 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800733}
734
735TEST_F(EventThreadTest, postHotplugExternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700736 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700737
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800738 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
739 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800740}
741
Ady Abraham447052e2019-02-13 16:07:27 -0800742TEST_F(EventThreadTest, postConfigChangedPrimary) {
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(INTERNAL_DISPLAY_ID)
747 .setId(DisplayModeId(7))
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(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800754}
755
756TEST_F(EventThreadTest, postConfigChangedExternal) {
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(EXTERNAL_DISPLAY_ID)
761 .setId(DisplayModeId(5))
762 .setVsyncPeriod(16666666)
763 .build();
ramindania04b8a52023-08-07 18:49:47 -0700764 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700765
Ady Abraham67434eb2022-12-01 17:48:12 -0800766 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
767 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800768}
769
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700770TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700771 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700772
Ady Abraham690f4612021-07-01 23:24:03 -0700773 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
774 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
775 .setId(DisplayModeId(7))
776 .setVsyncPeriod(16666666)
777 .build();
ramindania04b8a52023-08-07 18:49:47 -0700778 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham67434eb2022-12-01 17:48:12 -0800779 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
780 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700781}
782
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700783TEST_F(EventThreadTest, suppressConfigChanged) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700784 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700785
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700786 ConnectionEventRecorder suppressConnectionEventRecorder{0};
787 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700788 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700789
Ady Abraham690f4612021-07-01 23:24:03 -0700790 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
791 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
792 .setId(DisplayModeId(9))
793 .setVsyncPeriod(16666666)
794 .build();
ramindania04b8a52023-08-07 18:49:47 -0700795 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700796
Ady Abraham67434eb2022-12-01 17:48:12 -0800797 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
798 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700799
800 auto args = suppressConnectionEventRecorder.waitForCall();
801 ASSERT_FALSE(args.has_value());
802}
803
Ady Abraham62f216c2020-10-13 19:07:23 -0700804TEST_F(EventThreadTest, postUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700805 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700806
Ady Abraham62f216c2020-10-13 19:07:23 -0700807 const std::vector<FrameRateOverride> overrides = {
808 {.uid = 1, .frameRateHz = 20},
809 {.uid = 3, .frameRateHz = 40},
810 {.uid = 5, .frameRateHz = 60},
811 };
812
813 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
814 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
815}
816
817TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700818 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700819
Ady Abraham62f216c2020-10-13 19:07:23 -0700820 const std::vector<FrameRateOverride> overrides = {
821 {.uid = 1, .frameRateHz = 20},
822 {.uid = 3, .frameRateHz = 40},
823 {.uid = 5, .frameRateHz = 60},
824 };
825
826 ConnectionEventRecorder suppressConnectionEventRecorder{0};
827 sp<MockEventThreadConnection> suppressConnection =
828 createConnection(suppressConnectionEventRecorder);
829
830 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
831 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
832
833 auto args = suppressConnectionEventRecorder.waitForCall();
834 ASSERT_FALSE(args.has_value());
835}
836
Ady Abraham0bb6a472020-10-12 10:22:13 -0700837TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700838 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700839
Ady Abraham0bb6a472020-10-12 10:22:13 -0700840 // Signal that we want the next vsync event to be posted to the throttled connection
841 mThread->requestNextVsync(mThrottledConnection);
842
843 // EventThread should immediately request a resync.
844 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
845
846 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800847 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700848
849 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700850 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800851 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500852 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700853 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800854 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700855
856 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700857 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700858 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800859 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500860 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700861 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800862 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700863
864 // EventThread should not change the vsync state as it didn't send the event
865 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800866 onVSyncEvent(456, 123, 0);
867 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700868}
869
Huihong Luocac92162023-12-21 13:52:42 -0800870TEST_F(EventThreadTest, postHcpLevelsChanged) {
871 setupEventThread();
872
873 mThread->onHdcpLevelsChanged(EXTERNAL_DISPLAY_ID, HDCP_V1, HDCP_V2);
874 auto args = mConnectionEventCallRecorder.waitForCall();
875 ASSERT_TRUE(args.has_value());
876 const auto& event = std::get<0>(args.value());
877 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE, event.header.type);
878 EXPECT_EQ(EXTERNAL_DISPLAY_ID, event.header.displayId);
879 EXPECT_EQ(HDCP_V1, event.hdcpLevelsChange.connectedLevel);
880 EXPECT_EQ(HDCP_V2, event.hdcpLevelsChange.maxLevel);
881}
882
Melody Hsue524dd92024-08-27 22:27:29 +0000883TEST_F(EventThreadTest, connectionReceivesBufferStuffing) {
884 setupEventThread();
885
886 // Create a connection that will experience buffer stuffing.
887 ConnectionEventRecorder stuffedConnectionEventRecorder{0};
888 sp<MockEventThreadConnection> stuffedConnection =
889 createConnection(stuffedConnectionEventRecorder,
890 gui::ISurfaceComposer::EventRegistration::modeChanged |
891 gui::ISurfaceComposer::EventRegistration::frameRateOverride,
892 111);
893
894 // Add a connection and buffer count to the list of stuffed Uids that will receive
895 // data in the next vsync event.
896 BufferStuffingMap bufferStuffedUids;
897 bufferStuffedUids.try_emplace(stuffedConnection->mOwnerUid, 3);
898 mThread->addBufferStuffedUids(bufferStuffedUids);
899 mBufferStuffedConnectionRecorders.emplace_back(&stuffedConnectionEventRecorder);
900
901 // Signal that we want the next vsync event to be posted to two connections.
902 mThread->requestNextVsync(mConnection);
903 mThread->requestNextVsync(stuffedConnection);
904 onVSyncEvent(123, 456, 789);
905
906 // Vsync event data contains number of queued buffers.
907 expectQueuedBufferCountReceivedByConnection(mConnectionEventCallRecorder, 0);
908 expectQueuedBufferCountReceivedByConnection(stuffedConnectionEventRecorder, 3);
909}
910
911TEST_F(EventThreadTest, connectionsWithSameUidReceiveBufferStuffing) {
912 setupEventThread();
913
914 // Create a connection with the same Uid as another connection.
915 ConnectionEventRecorder secondConnectionEventRecorder{0};
916 sp<MockEventThreadConnection> secondConnection =
917 createConnection(secondConnectionEventRecorder,
918 gui::ISurfaceComposer::EventRegistration::modeChanged |
919 gui::ISurfaceComposer::EventRegistration::frameRateOverride,
920 mConnectionUid);
921
922 // Add connection Uid and buffer count to the list of stuffed Uids that will receive
923 // data in the next vsync event.
924 BufferStuffingMap bufferStuffedUids;
925 bufferStuffedUids.try_emplace(mConnectionUid, 3);
926 mThread->addBufferStuffedUids(bufferStuffedUids);
927 mBufferStuffedConnectionRecorders.emplace_back(&mConnectionEventCallRecorder);
928 mBufferStuffedConnectionRecorders.emplace_back(&secondConnectionEventRecorder);
929
930 // Signal that we want the next vsync event to be posted to two connections.
931 mThread->requestNextVsync(mConnection);
932 mThread->requestNextVsync(secondConnection);
933 onVSyncEvent(123, 456, 789);
934
935 // Vsync event data contains number of queued buffers.
936 expectQueuedBufferCountReceivedByConnection(mConnectionEventCallRecorder, 3);
937 expectQueuedBufferCountReceivedByConnection(secondConnectionEventRecorder, 3);
938}
939
Lloyd Pique24b0a482018-03-09 18:52:26 -0800940} // namespace
941} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100942
943// TODO(b/129481165): remove the #pragma below and fix conversion issues
944#pragma clang diagnostic pop // ignored "-Wextra"