blob: 45db0c56a9a78c3d49f3b6313f8c6b52ae136578 [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;
88
89 void setupEventThread();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070090 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
91 EventRegistrationFlags eventRegistration = {},
92 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080093
Ady Abraham011f8ba2022-11-22 15:09:07 -080094 void expectVSyncCallbackScheduleReceived(bool expectState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070095 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
96 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080097 void expectVsyncEventReceivedByConnection(const char* name,
98 ConnectionEventRecorder& connectionEventRecorder,
99 nsecs_t expectedTimestamp, unsigned expectedCount);
100 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800101 void expectVsyncEventFrameTimelinesCorrect(
Rachel Lee0655a912023-04-20 19:54:18 -0700102 nsecs_t expectedTimestamp, gui::VsyncEventData::FrameTimeline preferredVsyncData);
Ady Abrahamf2851612023-09-25 17:19:00 -0700103 void expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800104 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700105 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800106 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700107 int32_t expectedConfigId,
108 nsecs_t expectedVsyncPeriod);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500109 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700110 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
111 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800112
Ady Abraham011f8ba2022-11-22 15:09:07 -0800113 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime,
114 nsecs_t deadlineTimestamp) {
115 mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp);
116 }
117
118 AsyncCallRecorderWithCannedReturn<
119 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
120 scheduler::VSyncDispatch::ScheduleTiming)>
121 mVSyncCallbackScheduleRecorder{0};
122 AsyncCallRecorderWithCannedReturn<
123 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
124 scheduler::VSyncDispatch::ScheduleTiming)>
125 mVSyncCallbackUpdateRecorder{0};
126 AsyncCallRecorderWithCannedReturn<
127 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
128 std::string)>
129 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
130 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
131 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800132 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500133 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800134 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700135 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800136
Leon Scroggins III67388622023-02-06 20:36:20 -0500137 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700138 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800139 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700140 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000141 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700142
Ady Abrahamf2851612023-09-25 17:19:00 -0700143 std::chrono::nanoseconds mVsyncPeriod;
144
Ady Abraham0bb6a472020-10-12 10:22:13 -0700145 static constexpr uid_t mConnectionUid = 443;
146 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800147};
148
149EventThreadTest::EventThreadTest() {
150 const ::testing::TestInfo* const test_info =
151 ::testing::UnitTest::GetInstance()->current_test_info();
152 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
153
Leon Scroggins III67388622023-02-06 20:36:20 -0500154 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
155 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
156 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
157 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
158 nullptr));
159 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800160 EXPECT_CALL(mockDispatch, registerCallback(_, _))
161 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
162 EXPECT_CALL(mockDispatch, schedule(_, _))
163 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
164 EXPECT_CALL(mockDispatch, update(_, _))
165 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
166 EXPECT_CALL(mockDispatch, unregisterCallback(_))
167 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800168}
169
170EventThreadTest::~EventThreadTest() {
171 const ::testing::TestInfo* const test_info =
172 ::testing::UnitTest::GetInstance()->current_test_info();
173 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800174
Ady Abraham011f8ba2022-11-22 15:09:07 -0800175 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800176 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800177 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800178}
179
Ady Abrahamf2851612023-09-25 17:19:00 -0700180bool EventThreadTest::throttleVsync(android::TimePoint expectedVsyncTimestamp, uid_t uid) {
181 mThrottleVsyncCallRecorder.recordCall(expectedVsyncTimestamp.ns(), uid);
182 return (uid == mThrottledConnectionUid);
183}
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500184
Ady Abrahamf2851612023-09-25 17:19:00 -0700185Period EventThreadTest::getVsyncPeriod(uid_t) {
186 return mVsyncPeriod;
187}
188
189void EventThreadTest::resync() {
190 mResyncCallRecorder.recordCall();
191}
192
193void EventThreadTest::setupEventThread() {
Rachel Lee3f028662021-11-04 19:32:24 +0000194 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
Leon Scroggins III67388622023-02-06 20:36:20 -0500195 mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule,
Ady Abrahamf2851612023-09-25 17:19:00 -0700196 mTokenManager.get(), *this, kWorkDuration,
197 kReadyDuration);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800198
199 // EventThread should register itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800200 EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value());
Rachel Lee0655a912023-04-20 19:54:18 -0700201
202 mConnection =
203 createConnection(mConnectionEventCallRecorder,
204 gui::ISurfaceComposer::EventRegistration::modeChanged |
205 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
206 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
207 gui::ISurfaceComposer::EventRegistration::modeChanged,
208 mThrottledConnectionUid);
209
210 // A display must be connected for VSYNC events to be delivered.
211 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
212 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800213}
214
215sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700216 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
217 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800218 sp<MockEventThreadConnection> connection =
Ady Abrahamf2851612023-09-25 17:19:00 -0700219 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid, eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800220 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
221 return connection;
222}
223
Ady Abraham011f8ba2022-11-22 15:09:07 -0800224void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
225 if (expectState) {
226 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
227 } else {
228 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
229 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800230}
231
Ady Abraham9c53ee72020-07-22 21:16:18 -0700232void EventThreadTest::expectVSyncSetDurationCallReceived(
233 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800234 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800235 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800236 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
237 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800238}
239
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500240void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700241 auto args = mThrottleVsyncCallRecorder.waitForCall();
242 ASSERT_TRUE(args.has_value());
243 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
244 EXPECT_EQ(uid, std::get<1>(args.value()));
245}
246
Lloyd Pique24b0a482018-03-09 18:52:26 -0800247void EventThreadTest::expectVsyncEventReceivedByConnection(
248 const char* name, ConnectionEventRecorder& connectionEventRecorder,
249 nsecs_t expectedTimestamp, unsigned expectedCount) {
250 auto args = connectionEventRecorder.waitForCall();
251 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
252 << expectedTimestamp;
253 const auto& event = std::get<0>(args.value());
254 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
255 << name << " did not get the correct event for timestamp " << expectedTimestamp;
256 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
257 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
258 EXPECT_EQ(expectedCount, event.vsync.count)
259 << name << " did not get the expected count for timestamp " << expectedTimestamp;
260}
261
262void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
263 unsigned expectedCount) {
264 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
265 mConnectionEventCallRecorder, expectedTimestamp,
266 expectedCount);
267}
268
Rachel Leeb9c5a772022-02-04 21:17:37 -0800269void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800270 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000271 auto args = mConnectionEventCallRecorder.waitForCall();
272 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
273 << expectedTimestamp;
274 const auto& event = std::get<0>(args.value());
Rachel Lee0655a912023-04-20 19:54:18 -0700275 for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800276 auto prediction = mTokenManager->getPredictionsForToken(
277 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000278 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800279 EXPECT_EQ(prediction.value().endTime,
280 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000281 << "Deadline timestamp does not match cached value";
282 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800283 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
284 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000285
Rachel Lee3f028662021-11-04 19:32:24 +0000286 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800287 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
288 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000289 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800290 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
291 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
292 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000293 }
Rachel Lee0d943202022-02-01 23:29:41 -0800294
295 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800296 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800297 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800298 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
299 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
300 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800301 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800302 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
303 preferredVsyncData.expectedPresentationTime)
304 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000305 }
306 }
307}
308
Ady Abrahamf2851612023-09-25 17:19:00 -0700309void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData) {
Rachel Lee0655a912023-04-20 19:54:18 -0700310 float nonPreferredTimelinesAmount =
Ady Abrahamf2851612023-09-25 17:19:00 -0700311 scheduler::VsyncConfig::kEarlyLatchMaxThreshold / mVsyncPeriod;
Rachel Lee0655a912023-04-20 19:54:18 -0700312 EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1)
313 << "Amount of non-preferred frame timelines too many;"
314 << " expected presentation time will be over threshold";
Rachel Lee40aef422023-04-25 14:35:47 -0700315 EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity)
Rachel Lee0655a912023-04-20 19:54:18 -0700316 << "Amount of non-preferred frame timelines should be less than max capacity";
317 EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0)
318 << "Frame timelines length should be greater than 0";
319 EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength)
320 << "Preferred frame timeline index should be less than frame timelines length";
321}
322
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800323void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
324 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800325 auto args = mConnectionEventCallRecorder.waitForCall();
326 ASSERT_TRUE(args.has_value());
327 const auto& event = std::get<0>(args.value());
328 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800329 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330 EXPECT_EQ(expectedConnected, event.hotplug.connected);
331}
332
Ady Abraham447052e2019-02-13 16:07:27 -0800333void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700334 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
335 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800336 auto args = mConnectionEventCallRecorder.waitForCall();
337 ASSERT_TRUE(args.has_value());
338 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100339 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800340 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100341 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
342 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800343}
344
Ady Abraham62f216c2020-10-13 19:07:23 -0700345void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
346 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
347 for (const auto [uid, frameRateHz] : expectedOverrides) {
348 auto args = mConnectionEventCallRecorder.waitForCall();
349 ASSERT_TRUE(args.has_value());
350 const auto& event = std::get<0>(args.value());
351 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
352 EXPECT_EQ(expectedDisplayId, event.header.displayId);
353 EXPECT_EQ(uid, event.frameRateOverride.uid);
354 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
355 }
356
357 auto args = mConnectionEventCallRecorder.waitForCall();
358 ASSERT_TRUE(args.has_value());
359 const auto& event = std::get<0>(args.value());
360 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
361 EXPECT_EQ(expectedDisplayId, event.header.displayId);
362}
363
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364namespace {
365
Rachel Leeef2e21f2022-02-01 14:51:34 -0800366using namespace testing;
367
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368/* ------------------------------------------------------------------------
369 * Test cases
370 */
371
372TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700373 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700374
Ady Abraham011f8ba2022-11-22 15:09:07 -0800375 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
376 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
377 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
378 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800380 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
381}
382
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800383TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700384 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700385
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800386 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
387 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800388
389 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700390 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800391
392 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800393 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800394}
395
Lloyd Pique24b0a482018-03-09 18:52:26 -0800396TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700397 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700398
Lloyd Pique24b0a482018-03-09 18:52:26 -0800399 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700400 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800401
Ady Abraham8532d012019-05-08 14:50:56 -0700402 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800403 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
404
Ady Abraham011f8ba2022-11-22 15:09:07 -0800405 // EventThread should enable schedule a vsync callback
406 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407
408 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700409 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800410 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500411 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800412 expectVsyncEventReceivedByConnection(123, 1u);
413
Ady Abraham011f8ba2022-11-22 15:09:07 -0800414 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
415 expectVSyncCallbackScheduleReceived(true);
416
Lloyd Pique24b0a482018-03-09 18:52:26 -0800417 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700418 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800420 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700421 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800422 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
423
424 // EventThread should also detect that at this point that it does not need
425 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800426 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800427}
428
Rachel Lee3f028662021-11-04 19:32:24 +0000429TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700430 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700431
Rachel Lee3f028662021-11-04 19:32:24 +0000432 // Signal that we want the next vsync event to be posted to the connection
433 mThread->requestNextVsync(mConnection);
434
Ady Abraham011f8ba2022-11-22 15:09:07 -0800435 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000436
437 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700438 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800439 onVSyncEvent(123, 456, 789);
440 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000441}
442
Rachel Lee0655a912023-04-20 19:54:18 -0700443TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700444 setupEventThread();
Rachel Lee40aef422023-04-25 14:35:47 -0700445 // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines,
446 // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal
447 // truncation (e.g. 60Hz has 16.6... ms vsync period).
Ady Abrahamf2851612023-09-25 17:19:00 -0700448 mVsyncPeriod = (scheduler::VsyncConfig::kEarlyLatchMaxThreshold /
449 (VsyncEventData::kFrameTimelinesCapacity - 2));
Rachel Lee0655a912023-04-20 19:54:18 -0700450
451 // Signal that we want the next vsync event to be posted to the connection
452 mThread->requestNextVsync(mConnection);
453
454 expectVSyncCallbackScheduleReceived(true);
455
456 // Use the received callback to signal a vsync event.
457 // The throttler should receive the event, as well as the connection.
458 nsecs_t expectedTimestamp = 123;
459 onVSyncEvent(expectedTimestamp, 456, 789);
460
461 auto args = mConnectionEventCallRecorder.waitForCall();
462 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
463 << expectedTimestamp;
464 const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData;
Ady Abrahamf2851612023-09-25 17:19:00 -0700465 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Lee0655a912023-04-20 19:54:18 -0700466}
467
Rachel Leeef2e21f2022-02-01 14:51:34 -0800468TEST_F(EventThreadTest, getLatestVsyncEventData) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700469 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700470
Rachel Leeef2e21f2022-02-01 14:51:34 -0800471 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800472 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800473 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
474
475 mock::VSyncTracker& mockTracker =
476 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
Ady Abraham4335afd2023-12-18 19:10:47 -0800477 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_, _))
Ady Abraham011f8ba2022-11-22 15:09:07 -0800478 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800479
480 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700481
482 // Check EventThread immediately requested a resync.
483 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
484
Ady Abrahamf2851612023-09-25 17:19:00 -0700485 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800486 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
487 << "Deadline timestamp should be greater than frame time";
Rachel Lee0655a912023-04-20 19:54:18 -0700488 for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800489 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800490 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800491 EXPECT_TRUE(prediction.has_value());
492 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
493 << "Deadline timestamp does not match cached value";
494 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800495 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800496 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800497 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800498 vsyncEventData.frameTimelines[i].deadlineTimestamp)
499 << "Expected vsync timestamp should be greater than deadline";
500
501 if (i > 0) {
502 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
503 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
504 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800505 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
506 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800507 << "Expected vsync timestamp out of order for frame timeline " << i;
508 }
509
510 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800511 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800512 << "Vsync ID incorrect for frame timeline " << i;
513 if (i == vsyncEventData.preferredFrameTimelineIndex) {
514 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
515 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800516 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
517 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800518 << "Preferred expected vsync timestamp incorrect" << i;
519 }
520 }
521}
522
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700524 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700525
Lloyd Pique24b0a482018-03-09 18:52:26 -0800526 // Create a first connection, register it, and request a vsync rate of zero.
527 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700528 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800529 mThread->setVsyncRate(0, firstConnection);
530
531 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800532 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800533
534 // However if there is another connection which wants events at a nonzero rate.....
535 ConnectionEventRecorder secondConnectionEventRecorder{0};
536 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700537 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800538 mThread->setVsyncRate(1, secondConnection);
539
Dominik Laskowski029cc122019-01-23 19:52:06 -0800540 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800541 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800542
543 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700544 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800545 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800546 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800547 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
548 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
549 1u);
550}
551
552TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700553 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700554
Lloyd Pique24b0a482018-03-09 18:52:26 -0800555 mThread->setVsyncRate(1, mConnection);
556
Dominik Laskowski029cc122019-01-23 19:52:06 -0800557 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800558 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800559
560 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700561 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800562 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500563 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800564 expectVsyncEventReceivedByConnection(123, 1u);
565
566 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800567 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500568 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800569 expectVsyncEventReceivedByConnection(456, 2u);
570
571 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800572 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500573 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800574 expectVsyncEventReceivedByConnection(789, 3u);
575}
576
577TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700578 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700579
Lloyd Pique24b0a482018-03-09 18:52:26 -0800580 mThread->setVsyncRate(2, mConnection);
581
Dominik Laskowski029cc122019-01-23 19:52:06 -0800582 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800583 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800584
Huihong Luoab8ffef2022-08-18 13:02:26 -0700585 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800586 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800587 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700588 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800589
Huihong Luoab8ffef2022-08-18 13:02:26 -0700590 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800591 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800592 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700593 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800594
Huihong Luoab8ffef2022-08-18 13:02:26 -0700595 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800596 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800597 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700598 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800599
Huihong Luoab8ffef2022-08-18 13:02:26 -0700600 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800601 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800602 expectVsyncEventReceivedByConnection(101112, 4u);
603}
604
605TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700606 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700607
Lloyd Pique24b0a482018-03-09 18:52:26 -0800608 mThread->setVsyncRate(1, mConnection);
609
Dominik Laskowski029cc122019-01-23 19:52:06 -0800610 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800611 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800612
613 // Destroy the only (strong) reference to the connection.
614 mConnection = nullptr;
615
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, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
619
620 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800621 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800622}
623
624TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
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 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700628 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800629 mThread->setVsyncRate(1, errorConnection);
630
Dominik Laskowski029cc122019-01-23 19:52:06 -0800631 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800632 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800633
Huihong Luoab8ffef2022-08-18 13:02:26 -0700634 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800635 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800636 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
637
Ady Abraham011f8ba2022-11-22 15:09:07 -0800638 // Another schedule is expected, since the connection is removed only after
639 // the next vsync is requested.
640 expectVSyncCallbackScheduleReceived(true);
641
Huihong Luoab8ffef2022-08-18 13:02:26 -0700642 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800643 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800644 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
645
646 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800647 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800648}
649
650TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700651 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700652
Lloyd Pique24b0a482018-03-09 18:52:26 -0800653 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700654 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800655 mThread->setVsyncRate(1, errorConnection);
656
Dominik Laskowski029cc122019-01-23 19:52:06 -0800657 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800658 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800659
Huihong Luoab8ffef2022-08-18 13:02:26 -0700660 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800661 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800662 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
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 // A subsequent event will be seen by the connection, which still then returns a non-fatal
666 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800667 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800668 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800669 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800670
671 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800672 onVSyncEvent(456, 123, 0);
673 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800674}
675
676TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700677 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700678
Ady Abraham9c53ee72020-07-22 21:16:18 -0700679 mThread->setDuration(321ns, 456ns);
680 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800681}
682
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800683TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700684 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700685
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800686 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
687 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800688}
689
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800690TEST_F(EventThreadTest, postHotplugInternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700691 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700692
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800693 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
694 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800695}
696
697TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700698 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700699
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800700 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
701 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800702}
703
704TEST_F(EventThreadTest, postHotplugExternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700705 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700706
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800707 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
708 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800709}
710
Ady Abraham447052e2019-02-13 16:07:27 -0800711TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700712 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700713
Ady Abraham690f4612021-07-01 23:24:03 -0700714 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
715 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
716 .setId(DisplayModeId(7))
717 .setVsyncPeriod(16666666)
718 .build();
ramindania04b8a52023-08-07 18:49:47 -0700719 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700720
Ady Abraham67434eb2022-12-01 17:48:12 -0800721 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
722 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800723}
724
725TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700726 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700727
Ady Abraham690f4612021-07-01 23:24:03 -0700728 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
729 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
730 .setId(DisplayModeId(5))
731 .setVsyncPeriod(16666666)
732 .build();
ramindania04b8a52023-08-07 18:49:47 -0700733 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700734
Ady Abraham67434eb2022-12-01 17:48:12 -0800735 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
736 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800737}
738
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700739TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700740 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700741
Ady Abraham690f4612021-07-01 23:24:03 -0700742 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
743 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
744 .setId(DisplayModeId(7))
745 .setVsyncPeriod(16666666)
746 .build();
ramindania04b8a52023-08-07 18:49:47 -0700747 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham67434eb2022-12-01 17:48:12 -0800748 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
749 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700750}
751
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700752TEST_F(EventThreadTest, suppressConfigChanged) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700753 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700754
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700755 ConnectionEventRecorder suppressConnectionEventRecorder{0};
756 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700757 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700758
Ady Abraham690f4612021-07-01 23:24:03 -0700759 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
760 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
761 .setId(DisplayModeId(9))
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(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700768
769 auto args = suppressConnectionEventRecorder.waitForCall();
770 ASSERT_FALSE(args.has_value());
771}
772
Ady Abraham62f216c2020-10-13 19:07:23 -0700773TEST_F(EventThreadTest, postUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700774 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700775
Ady Abraham62f216c2020-10-13 19:07:23 -0700776 const std::vector<FrameRateOverride> overrides = {
777 {.uid = 1, .frameRateHz = 20},
778 {.uid = 3, .frameRateHz = 40},
779 {.uid = 5, .frameRateHz = 60},
780 };
781
782 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
783 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
784}
785
786TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700787 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700788
Ady Abraham62f216c2020-10-13 19:07:23 -0700789 const std::vector<FrameRateOverride> overrides = {
790 {.uid = 1, .frameRateHz = 20},
791 {.uid = 3, .frameRateHz = 40},
792 {.uid = 5, .frameRateHz = 60},
793 };
794
795 ConnectionEventRecorder suppressConnectionEventRecorder{0};
796 sp<MockEventThreadConnection> suppressConnection =
797 createConnection(suppressConnectionEventRecorder);
798
799 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
800 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
801
802 auto args = suppressConnectionEventRecorder.waitForCall();
803 ASSERT_FALSE(args.has_value());
804}
805
Ady Abraham0bb6a472020-10-12 10:22:13 -0700806TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700807 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700808
Ady Abraham0bb6a472020-10-12 10:22:13 -0700809 // Signal that we want the next vsync event to be posted to the throttled connection
810 mThread->requestNextVsync(mThrottledConnection);
811
812 // EventThread should immediately request a resync.
813 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
814
815 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800816 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700817
818 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700819 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800820 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500821 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700822 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800823 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700824
825 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700826 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700827 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800828 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500829 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700830 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800831 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700832
833 // EventThread should not change the vsync state as it didn't send the event
834 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800835 onVSyncEvent(456, 123, 0);
836 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700837}
838
Huihong Luocac92162023-12-21 13:52:42 -0800839TEST_F(EventThreadTest, postHcpLevelsChanged) {
840 setupEventThread();
841
842 mThread->onHdcpLevelsChanged(EXTERNAL_DISPLAY_ID, HDCP_V1, HDCP_V2);
843 auto args = mConnectionEventCallRecorder.waitForCall();
844 ASSERT_TRUE(args.has_value());
845 const auto& event = std::get<0>(args.value());
846 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE, event.header.type);
847 EXPECT_EQ(EXTERNAL_DISPLAY_ID, event.header.displayId);
848 EXPECT_EQ(HDCP_V1, event.hdcpLevelsChange.connectedLevel);
849 EXPECT_EQ(HDCP_V2, event.hdcpLevelsChange.maxLevel);
850}
851
Lloyd Pique24b0a482018-03-09 18:52:26 -0800852} // namespace
853} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100854
855// TODO(b/129481165): remove the #pragma below and fix conversion issues
856#pragma clang diagnostic pop // ignored "-Wextra"