blob: 8891c06c7595f8709165ea232342e692f06e827f [file] [log] [blame]
Lloyd Pique24b0a482018-03-09 18:52:26 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Lloyd Pique24b0a482018-03-09 18:52:26 -080021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080026#include <log/log.h>
Rachel Lee0655a912023-04-20 19:54:18 -070027#include <scheduler/VsyncConfig.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080028#include <utils/Errors.h>
29
30#include "AsyncCallRecorder.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010031#include "DisplayHardware/DisplayMode.h"
Rachel Lee3f028662021-11-04 19:32:24 +000032#include "FrameTimeline.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070033#include "Scheduler/EventThread.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080034#include "mock/MockVSyncDispatch.h"
35#include "mock/MockVSyncTracker.h"
36#include "mock/MockVsyncController.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080037
38using namespace std::chrono_literals;
39using namespace std::placeholders;
40
41using testing::_;
42using testing::Invoke;
Ady Abraham011f8ba2022-11-22 15:09:07 -080043using testing::Return;
Lloyd Pique24b0a482018-03-09 18:52:26 -080044
45namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070047using namespace ftl::flag_operators;
48
Lloyd Pique24b0a482018-03-09 18:52:26 -080049namespace {
50
Dominik Laskowskif1833852021-03-23 15:06:50 -070051constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
52constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
53constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
54 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
55
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050056constexpr std::chrono::duration VSYNC_PERIOD(16ms);
57
Lloyd Pique24b0a482018-03-09 18:52:26 -080058} // namespace
59
Ady Abrahamf2851612023-09-25 17:19:00 -070060class EventThreadTest : public testing::Test, public IEventThreadCallback {
Lloyd Pique24b0a482018-03-09 18:52:26 -080061protected:
Ady Abraham011f8ba2022-11-22 15:09:07 -080062 static constexpr std::chrono::nanoseconds kWorkDuration = 0ms;
63 static constexpr std::chrono::nanoseconds kReadyDuration = 3ms;
64
Dominik Laskowskif654d572018-12-20 11:03:06 -080065 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080066 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070067 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070068 EventRegistrationFlags eventRegistration)
Ady Abrahamf2851612023-09-25 17:19:00 -070069 : EventThreadConnection(eventThread, callingUid, eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080070 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
71 };
72
73 using ConnectionEventRecorder =
74 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
75
76 EventThreadTest();
77 ~EventThreadTest() override;
78
Ady Abrahamf2851612023-09-25 17:19:00 -070079 void SetUp() override { mVsyncPeriod = VSYNC_PERIOD; }
80
81 // IEventThreadCallback overrides
82 bool throttleVsync(TimePoint, uid_t) override;
83 Period getVsyncPeriod(uid_t) override;
84 void resync() override;
85
86 void setupEventThread();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070087 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
88 EventRegistrationFlags eventRegistration = {},
89 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080090
Ady Abraham011f8ba2022-11-22 15:09:07 -080091 void expectVSyncCallbackScheduleReceived(bool expectState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070092 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
93 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080094 void expectVsyncEventReceivedByConnection(const char* name,
95 ConnectionEventRecorder& connectionEventRecorder,
96 nsecs_t expectedTimestamp, unsigned expectedCount);
97 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Ady Abraham011f8ba2022-11-22 15:09:07 -080098 void expectVsyncEventFrameTimelinesCorrect(
Rachel Lee0655a912023-04-20 19:54:18 -070099 nsecs_t expectedTimestamp, gui::VsyncEventData::FrameTimeline preferredVsyncData);
Ady Abrahamf2851612023-09-25 17:19:00 -0700100 void expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800101 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700102 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800103 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700104 int32_t expectedConfigId,
105 nsecs_t expectedVsyncPeriod);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500106 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700107 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
108 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800109
Ady Abraham011f8ba2022-11-22 15:09:07 -0800110 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime,
111 nsecs_t deadlineTimestamp) {
112 mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp);
113 }
114
115 AsyncCallRecorderWithCannedReturn<
116 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
117 scheduler::VSyncDispatch::ScheduleTiming)>
118 mVSyncCallbackScheduleRecorder{0};
119 AsyncCallRecorderWithCannedReturn<
120 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
121 scheduler::VSyncDispatch::ScheduleTiming)>
122 mVSyncCallbackUpdateRecorder{0};
123 AsyncCallRecorderWithCannedReturn<
124 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
125 std::string)>
126 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
127 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
128 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800129 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500130 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800131 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700132 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800133
Leon Scroggins III67388622023-02-06 20:36:20 -0500134 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700135 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800136 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700137 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000138 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700139
Ady Abrahamf2851612023-09-25 17:19:00 -0700140 std::chrono::nanoseconds mVsyncPeriod;
141
Ady Abraham0bb6a472020-10-12 10:22:13 -0700142 static constexpr uid_t mConnectionUid = 443;
143 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144};
145
146EventThreadTest::EventThreadTest() {
147 const ::testing::TestInfo* const test_info =
148 ::testing::UnitTest::GetInstance()->current_test_info();
149 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
150
Leon Scroggins III67388622023-02-06 20:36:20 -0500151 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
152 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
153 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
154 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
155 nullptr));
156 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800157 EXPECT_CALL(mockDispatch, registerCallback(_, _))
158 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
159 EXPECT_CALL(mockDispatch, schedule(_, _))
160 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
161 EXPECT_CALL(mockDispatch, update(_, _))
162 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
163 EXPECT_CALL(mockDispatch, unregisterCallback(_))
164 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800165}
166
167EventThreadTest::~EventThreadTest() {
168 const ::testing::TestInfo* const test_info =
169 ::testing::UnitTest::GetInstance()->current_test_info();
170 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800171
Ady Abraham011f8ba2022-11-22 15:09:07 -0800172 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800173 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800174 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800175}
176
Ady Abrahamf2851612023-09-25 17:19:00 -0700177bool EventThreadTest::throttleVsync(android::TimePoint expectedVsyncTimestamp, uid_t uid) {
178 mThrottleVsyncCallRecorder.recordCall(expectedVsyncTimestamp.ns(), uid);
179 return (uid == mThrottledConnectionUid);
180}
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500181
Ady Abrahamf2851612023-09-25 17:19:00 -0700182Period EventThreadTest::getVsyncPeriod(uid_t) {
183 return mVsyncPeriod;
184}
185
186void EventThreadTest::resync() {
187 mResyncCallRecorder.recordCall();
188}
189
190void EventThreadTest::setupEventThread() {
Rachel Lee3f028662021-11-04 19:32:24 +0000191 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
Leon Scroggins III67388622023-02-06 20:36:20 -0500192 mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule,
Ady Abrahamf2851612023-09-25 17:19:00 -0700193 mTokenManager.get(), *this, kWorkDuration,
194 kReadyDuration);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800195
196 // EventThread should register itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800197 EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value());
Rachel Lee0655a912023-04-20 19:54:18 -0700198
199 mConnection =
200 createConnection(mConnectionEventCallRecorder,
201 gui::ISurfaceComposer::EventRegistration::modeChanged |
202 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
203 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
204 gui::ISurfaceComposer::EventRegistration::modeChanged,
205 mThrottledConnectionUid);
206
207 // A display must be connected for VSYNC events to be delivered.
208 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
209 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800210}
211
212sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700213 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
214 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800215 sp<MockEventThreadConnection> connection =
Ady Abrahamf2851612023-09-25 17:19:00 -0700216 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid, eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800217 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
218 return connection;
219}
220
Ady Abraham011f8ba2022-11-22 15:09:07 -0800221void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
222 if (expectState) {
223 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
224 } else {
225 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
226 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800227}
228
Ady Abraham9c53ee72020-07-22 21:16:18 -0700229void EventThreadTest::expectVSyncSetDurationCallReceived(
230 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800231 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800232 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800233 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
234 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800235}
236
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500237void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700238 auto args = mThrottleVsyncCallRecorder.waitForCall();
239 ASSERT_TRUE(args.has_value());
240 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
241 EXPECT_EQ(uid, std::get<1>(args.value()));
242}
243
Lloyd Pique24b0a482018-03-09 18:52:26 -0800244void EventThreadTest::expectVsyncEventReceivedByConnection(
245 const char* name, ConnectionEventRecorder& connectionEventRecorder,
246 nsecs_t expectedTimestamp, unsigned expectedCount) {
247 auto args = connectionEventRecorder.waitForCall();
248 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
249 << expectedTimestamp;
250 const auto& event = std::get<0>(args.value());
251 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
252 << name << " did not get the correct event for timestamp " << expectedTimestamp;
253 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
254 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
255 EXPECT_EQ(expectedCount, event.vsync.count)
256 << name << " did not get the expected count for timestamp " << expectedTimestamp;
257}
258
259void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
260 unsigned expectedCount) {
261 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
262 mConnectionEventCallRecorder, expectedTimestamp,
263 expectedCount);
264}
265
Rachel Leeb9c5a772022-02-04 21:17:37 -0800266void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800267 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000268 auto args = mConnectionEventCallRecorder.waitForCall();
269 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
270 << expectedTimestamp;
271 const auto& event = std::get<0>(args.value());
Rachel Lee0655a912023-04-20 19:54:18 -0700272 for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800273 auto prediction = mTokenManager->getPredictionsForToken(
274 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000275 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800276 EXPECT_EQ(prediction.value().endTime,
277 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000278 << "Deadline timestamp does not match cached value";
279 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800280 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
281 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000282
Rachel Lee3f028662021-11-04 19:32:24 +0000283 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800284 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
285 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000286 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800287 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
288 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
289 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000290 }
Rachel Lee0d943202022-02-01 23:29:41 -0800291
292 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800293 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800294 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800295 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
296 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
297 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800298 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800299 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
300 preferredVsyncData.expectedPresentationTime)
301 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000302 }
303 }
304}
305
Ady Abrahamf2851612023-09-25 17:19:00 -0700306void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData) {
Rachel Lee0655a912023-04-20 19:54:18 -0700307 float nonPreferredTimelinesAmount =
Ady Abrahamf2851612023-09-25 17:19:00 -0700308 scheduler::VsyncConfig::kEarlyLatchMaxThreshold / mVsyncPeriod;
Rachel Lee0655a912023-04-20 19:54:18 -0700309 EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1)
310 << "Amount of non-preferred frame timelines too many;"
311 << " expected presentation time will be over threshold";
Rachel Lee40aef422023-04-25 14:35:47 -0700312 EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity)
Rachel Lee0655a912023-04-20 19:54:18 -0700313 << "Amount of non-preferred frame timelines should be less than max capacity";
314 EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0)
315 << "Frame timelines length should be greater than 0";
316 EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength)
317 << "Preferred frame timeline index should be less than frame timelines length";
318}
319
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800320void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
321 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 auto args = mConnectionEventCallRecorder.waitForCall();
323 ASSERT_TRUE(args.has_value());
324 const auto& event = std::get<0>(args.value());
325 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800326 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800327 EXPECT_EQ(expectedConnected, event.hotplug.connected);
328}
329
Ady Abraham447052e2019-02-13 16:07:27 -0800330void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700331 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
332 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800333 auto args = mConnectionEventCallRecorder.waitForCall();
334 ASSERT_TRUE(args.has_value());
335 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100336 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800337 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100338 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
339 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800340}
341
Ady Abraham62f216c2020-10-13 19:07:23 -0700342void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
343 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
344 for (const auto [uid, frameRateHz] : expectedOverrides) {
345 auto args = mConnectionEventCallRecorder.waitForCall();
346 ASSERT_TRUE(args.has_value());
347 const auto& event = std::get<0>(args.value());
348 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
349 EXPECT_EQ(expectedDisplayId, event.header.displayId);
350 EXPECT_EQ(uid, event.frameRateOverride.uid);
351 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
352 }
353
354 auto args = mConnectionEventCallRecorder.waitForCall();
355 ASSERT_TRUE(args.has_value());
356 const auto& event = std::get<0>(args.value());
357 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
358 EXPECT_EQ(expectedDisplayId, event.header.displayId);
359}
360
Lloyd Pique24b0a482018-03-09 18:52:26 -0800361namespace {
362
Rachel Leeef2e21f2022-02-01 14:51:34 -0800363using namespace testing;
364
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365/* ------------------------------------------------------------------------
366 * Test cases
367 */
368
369TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700370 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700371
Ady Abraham011f8ba2022-11-22 15:09:07 -0800372 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
373 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
374 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
375 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800376 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
378}
379
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800380TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700381 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700382
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800383 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
384 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800385
386 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700387 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800388
389 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800390 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800391}
392
Lloyd Pique24b0a482018-03-09 18:52:26 -0800393TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700394 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700395
Lloyd Pique24b0a482018-03-09 18:52:26 -0800396 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700397 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800398
Ady Abraham8532d012019-05-08 14:50:56 -0700399 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800400 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
401
Ady Abraham011f8ba2022-11-22 15:09:07 -0800402 // EventThread should enable schedule a vsync callback
403 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800404
405 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700406 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800407 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500408 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409 expectVsyncEventReceivedByConnection(123, 1u);
410
Ady Abraham011f8ba2022-11-22 15:09:07 -0800411 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
412 expectVSyncCallbackScheduleReceived(true);
413
Lloyd Pique24b0a482018-03-09 18:52:26 -0800414 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700415 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800417 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700418 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
420
421 // EventThread should also detect that at this point that it does not need
422 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800423 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800424}
425
Rachel Lee3f028662021-11-04 19:32:24 +0000426TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700427 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700428
Rachel Lee3f028662021-11-04 19:32:24 +0000429 // Signal that we want the next vsync event to be posted to the connection
430 mThread->requestNextVsync(mConnection);
431
Ady Abraham011f8ba2022-11-22 15:09:07 -0800432 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000433
434 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700435 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800436 onVSyncEvent(123, 456, 789);
437 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000438}
439
Rachel Lee0655a912023-04-20 19:54:18 -0700440TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700441 setupEventThread();
Rachel Lee40aef422023-04-25 14:35:47 -0700442 // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines,
443 // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal
444 // truncation (e.g. 60Hz has 16.6... ms vsync period).
Ady Abrahamf2851612023-09-25 17:19:00 -0700445 mVsyncPeriod = (scheduler::VsyncConfig::kEarlyLatchMaxThreshold /
446 (VsyncEventData::kFrameTimelinesCapacity - 2));
Rachel Lee0655a912023-04-20 19:54:18 -0700447
448 // Signal that we want the next vsync event to be posted to the connection
449 mThread->requestNextVsync(mConnection);
450
451 expectVSyncCallbackScheduleReceived(true);
452
453 // Use the received callback to signal a vsync event.
454 // The throttler should receive the event, as well as the connection.
455 nsecs_t expectedTimestamp = 123;
456 onVSyncEvent(expectedTimestamp, 456, 789);
457
458 auto args = mConnectionEventCallRecorder.waitForCall();
459 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
460 << expectedTimestamp;
461 const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData;
Ady Abrahamf2851612023-09-25 17:19:00 -0700462 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Lee0655a912023-04-20 19:54:18 -0700463}
464
Rachel Leeef2e21f2022-02-01 14:51:34 -0800465TEST_F(EventThreadTest, getLatestVsyncEventData) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700466 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700467
Rachel Leeef2e21f2022-02-01 14:51:34 -0800468 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800469 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800470 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
471
472 mock::VSyncTracker& mockTracker =
473 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
474 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_))
475 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800476
477 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700478
479 // Check EventThread immediately requested a resync.
480 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
481
Ady Abrahamf2851612023-09-25 17:19:00 -0700482 expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800483 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
484 << "Deadline timestamp should be greater than frame time";
Rachel Lee0655a912023-04-20 19:54:18 -0700485 for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) {
Rachel Leeef2e21f2022-02-01 14:51:34 -0800486 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800487 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800488 EXPECT_TRUE(prediction.has_value());
489 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
490 << "Deadline timestamp does not match cached value";
491 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800492 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800493 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800494 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800495 vsyncEventData.frameTimelines[i].deadlineTimestamp)
496 << "Expected vsync timestamp should be greater than deadline";
497
498 if (i > 0) {
499 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
500 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
501 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800502 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
503 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800504 << "Expected vsync timestamp out of order for frame timeline " << i;
505 }
506
507 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800508 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800509 << "Vsync ID incorrect for frame timeline " << i;
510 if (i == vsyncEventData.preferredFrameTimelineIndex) {
511 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
512 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800513 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
514 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800515 << "Preferred expected vsync timestamp incorrect" << i;
516 }
517 }
518}
519
Lloyd Pique24b0a482018-03-09 18:52:26 -0800520TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700521 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700522
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523 // Create a first connection, register it, and request a vsync rate of zero.
524 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700525 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800526 mThread->setVsyncRate(0, firstConnection);
527
528 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800529 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800530
531 // However if there is another connection which wants events at a nonzero rate.....
532 ConnectionEventRecorder secondConnectionEventRecorder{0};
533 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700534 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800535 mThread->setVsyncRate(1, secondConnection);
536
Dominik Laskowski029cc122019-01-23 19:52:06 -0800537 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800538 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800539
540 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700541 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800542 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800543 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800544 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
545 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
546 1u);
547}
548
549TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700550 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700551
Lloyd Pique24b0a482018-03-09 18:52:26 -0800552 mThread->setVsyncRate(1, mConnection);
553
Dominik Laskowski029cc122019-01-23 19:52:06 -0800554 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800555 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800556
557 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700558 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800559 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500560 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561 expectVsyncEventReceivedByConnection(123, 1u);
562
563 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800564 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500565 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800566 expectVsyncEventReceivedByConnection(456, 2u);
567
568 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800569 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500570 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800571 expectVsyncEventReceivedByConnection(789, 3u);
572}
573
574TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700575 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700576
Lloyd Pique24b0a482018-03-09 18:52:26 -0800577 mThread->setVsyncRate(2, mConnection);
578
Dominik Laskowski029cc122019-01-23 19:52:06 -0800579 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800580 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800581
Huihong Luoab8ffef2022-08-18 13:02:26 -0700582 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800583 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800584 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700585 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800586
Huihong Luoab8ffef2022-08-18 13:02:26 -0700587 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800588 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800589 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700590 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800591
Huihong Luoab8ffef2022-08-18 13:02:26 -0700592 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800593 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800594 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700595 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800596
Huihong Luoab8ffef2022-08-18 13:02:26 -0700597 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800598 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800599 expectVsyncEventReceivedByConnection(101112, 4u);
600}
601
602TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700603 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700604
Lloyd Pique24b0a482018-03-09 18:52:26 -0800605 mThread->setVsyncRate(1, mConnection);
606
Dominik Laskowski029cc122019-01-23 19:52:06 -0800607 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800608 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800609
610 // Destroy the only (strong) reference to the connection.
611 mConnection = nullptr;
612
Huihong Luoab8ffef2022-08-18 13:02:26 -0700613 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800614 onVSyncEvent(123, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800615 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
616
617 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800618 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800619}
620
621TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700622 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700623
Lloyd Pique24b0a482018-03-09 18:52:26 -0800624 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700625 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800626 mThread->setVsyncRate(1, errorConnection);
627
Dominik Laskowski029cc122019-01-23 19:52:06 -0800628 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800629 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800630
Huihong Luoab8ffef2022-08-18 13:02:26 -0700631 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800632 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800633 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
634
Ady Abraham011f8ba2022-11-22 15:09:07 -0800635 // Another schedule is expected, since the connection is removed only after
636 // the next vsync is requested.
637 expectVSyncCallbackScheduleReceived(true);
638
Huihong Luoab8ffef2022-08-18 13:02:26 -0700639 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800640 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800641 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
642
643 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800644 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800645}
646
647TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700648 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700649
Lloyd Pique24b0a482018-03-09 18:52:26 -0800650 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700651 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800652 mThread->setVsyncRate(1, errorConnection);
653
Dominik Laskowski029cc122019-01-23 19:52:06 -0800654 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800655 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800656
Huihong Luoab8ffef2022-08-18 13:02:26 -0700657 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800658 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800659 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800660 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800661
Huihong Luoab8ffef2022-08-18 13:02:26 -0700662 // A subsequent event will be seen by the connection, which still then returns a non-fatal
663 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800664 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800665 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800666 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800667
668 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800669 onVSyncEvent(456, 123, 0);
670 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800671}
672
673TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700674 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700675
Ady Abraham9c53ee72020-07-22 21:16:18 -0700676 mThread->setDuration(321ns, 456ns);
677 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800678}
679
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800680TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700681 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700682
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800683 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
684 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800685}
686
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800687TEST_F(EventThreadTest, postHotplugInternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700688 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700689
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800690 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
691 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800692}
693
694TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700695 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700696
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800697 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
698 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800699}
700
701TEST_F(EventThreadTest, postHotplugExternalConnect) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700702 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700703
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800704 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
705 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800706}
707
Ady Abraham447052e2019-02-13 16:07:27 -0800708TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700709 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700710
Ady Abraham690f4612021-07-01 23:24:03 -0700711 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
712 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
713 .setId(DisplayModeId(7))
714 .setVsyncPeriod(16666666)
715 .build();
ramindania04b8a52023-08-07 18:49:47 -0700716 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700717
Ady Abraham67434eb2022-12-01 17:48:12 -0800718 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
719 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800720}
721
722TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700723 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700724
Ady Abraham690f4612021-07-01 23:24:03 -0700725 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
726 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
727 .setId(DisplayModeId(5))
728 .setVsyncPeriod(16666666)
729 .build();
ramindania04b8a52023-08-07 18:49:47 -0700730 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700731
Ady Abraham67434eb2022-12-01 17:48:12 -0800732 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
733 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800734}
735
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700736TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700737 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700738
Ady Abraham690f4612021-07-01 23:24:03 -0700739 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
740 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
741 .setId(DisplayModeId(7))
742 .setVsyncPeriod(16666666)
743 .build();
ramindania04b8a52023-08-07 18:49:47 -0700744 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham67434eb2022-12-01 17:48:12 -0800745 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
746 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700747}
748
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700749TEST_F(EventThreadTest, suppressConfigChanged) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700750 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700751
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700752 ConnectionEventRecorder suppressConnectionEventRecorder{0};
753 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700754 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700755
Ady Abraham690f4612021-07-01 23:24:03 -0700756 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
757 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
758 .setId(DisplayModeId(9))
759 .setVsyncPeriod(16666666)
760 .build();
ramindania04b8a52023-08-07 18:49:47 -0700761 const Fps fps = mode->getPeakFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700762
Ady Abraham67434eb2022-12-01 17:48:12 -0800763 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
764 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700765
766 auto args = suppressConnectionEventRecorder.waitForCall();
767 ASSERT_FALSE(args.has_value());
768}
769
Ady Abraham62f216c2020-10-13 19:07:23 -0700770TEST_F(EventThreadTest, postUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700771 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700772
Ady Abraham62f216c2020-10-13 19:07:23 -0700773 const std::vector<FrameRateOverride> overrides = {
774 {.uid = 1, .frameRateHz = 20},
775 {.uid = 3, .frameRateHz = 40},
776 {.uid = 5, .frameRateHz = 60},
777 };
778
779 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
780 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
781}
782
783TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700784 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700785
Ady Abraham62f216c2020-10-13 19:07:23 -0700786 const std::vector<FrameRateOverride> overrides = {
787 {.uid = 1, .frameRateHz = 20},
788 {.uid = 3, .frameRateHz = 40},
789 {.uid = 5, .frameRateHz = 60},
790 };
791
792 ConnectionEventRecorder suppressConnectionEventRecorder{0};
793 sp<MockEventThreadConnection> suppressConnection =
794 createConnection(suppressConnectionEventRecorder);
795
796 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
797 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
798
799 auto args = suppressConnectionEventRecorder.waitForCall();
800 ASSERT_FALSE(args.has_value());
801}
802
Ady Abraham0bb6a472020-10-12 10:22:13 -0700803TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
Ady Abrahamf2851612023-09-25 17:19:00 -0700804 setupEventThread();
Rachel Lee0655a912023-04-20 19:54:18 -0700805
Ady Abraham0bb6a472020-10-12 10:22:13 -0700806 // Signal that we want the next vsync event to be posted to the throttled connection
807 mThread->requestNextVsync(mThrottledConnection);
808
809 // EventThread should immediately request a resync.
810 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
811
812 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800813 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700814
815 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700816 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800817 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500818 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700819 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800820 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700821
822 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700823 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700824 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800825 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500826 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700827 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800828 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700829
830 // EventThread should not change the vsync state as it didn't send the event
831 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800832 onVSyncEvent(456, 123, 0);
833 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700834}
835
Lloyd Pique24b0a482018-03-09 18:52:26 -0800836} // namespace
837} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100838
839// TODO(b/129481165): remove the #pragma below and fix conversion issues
840#pragma clang diagnostic pop // ignored "-Wextra"