blob: f1cdca3ee1e46bb5e3275dd08dd088eab1318748 [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>
Lloyd Pique24b0a482018-03-09 18:52:26 -080027#include <utils/Errors.h>
28
29#include "AsyncCallRecorder.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010030#include "DisplayHardware/DisplayMode.h"
Rachel Lee3f028662021-11-04 19:32:24 +000031#include "FrameTimeline.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070032#include "Scheduler/EventThread.h"
Ady Abraham011f8ba2022-11-22 15:09:07 -080033#include "mock/MockVSyncDispatch.h"
34#include "mock/MockVSyncTracker.h"
35#include "mock/MockVsyncController.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080036
37using namespace std::chrono_literals;
38using namespace std::placeholders;
39
40using testing::_;
41using testing::Invoke;
Ady Abraham011f8ba2022-11-22 15:09:07 -080042using testing::Return;
Lloyd Pique24b0a482018-03-09 18:52:26 -080043
44namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080045
Dominik Laskowski2f01d772022-03-23 16:01:29 -070046using namespace ftl::flag_operators;
47
Lloyd Pique24b0a482018-03-09 18:52:26 -080048namespace {
49
Dominik Laskowskif1833852021-03-23 15:06:50 -070050constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
51constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
52constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
53 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
54
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050055constexpr std::chrono::duration VSYNC_PERIOD(16ms);
56
Lloyd Pique24b0a482018-03-09 18:52:26 -080057} // namespace
58
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -050059class EventThreadTest : public testing::Test {
Lloyd Pique24b0a482018-03-09 18:52:26 -080060protected:
Ady Abraham011f8ba2022-11-22 15:09:07 -080061 static constexpr std::chrono::nanoseconds kWorkDuration = 0ms;
62 static constexpr std::chrono::nanoseconds kReadyDuration = 3ms;
63
Dominik Laskowskif654d572018-12-20 11:03:06 -080064 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080065 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070066 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
67 ResyncCallback&& resyncCallback,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070068 EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070069 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070070 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080071 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
72 };
73
74 using ConnectionEventRecorder =
75 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
76
77 EventThreadTest();
78 ~EventThreadTest() override;
79
Ady Abraham011f8ba2022-11-22 15:09:07 -080080 void createThread();
Huihong Luo1b0c49f2022-03-15 19:18:21 -070081 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
82 EventRegistrationFlags eventRegistration = {},
83 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080084
Ady Abraham011f8ba2022-11-22 15:09:07 -080085 void expectVSyncCallbackScheduleReceived(bool expectState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070086 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
87 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080088 void expectVsyncEventReceivedByConnection(const char* name,
89 ConnectionEventRecorder& connectionEventRecorder,
90 nsecs_t expectedTimestamp, unsigned expectedCount);
91 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Ady Abraham011f8ba2022-11-22 15:09:07 -080092 void expectVsyncEventFrameTimelinesCorrect(
93 nsecs_t expectedTimestamp,
94 /*VSyncSource::VSyncData*/ gui::VsyncEventData::FrameTimeline preferredVsyncData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080095 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070096 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080097 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070098 int32_t expectedConfigId,
99 nsecs_t expectedVsyncPeriod);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500100 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700101 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
102 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800103
Ady Abraham011f8ba2022-11-22 15:09:07 -0800104 void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime,
105 nsecs_t deadlineTimestamp) {
106 mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp);
107 }
108
109 AsyncCallRecorderWithCannedReturn<
110 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
111 scheduler::VSyncDispatch::ScheduleTiming)>
112 mVSyncCallbackScheduleRecorder{0};
113 AsyncCallRecorderWithCannedReturn<
114 scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken,
115 scheduler::VSyncDispatch::ScheduleTiming)>
116 mVSyncCallbackUpdateRecorder{0};
117 AsyncCallRecorderWithCannedReturn<
118 scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback,
119 std::string)>
120 mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)};
121 AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)>
122 mVSyncCallbackUnregisterRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500124 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700126 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800127
Leon Scroggins III67388622023-02-06 20:36:20 -0500128 std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700129 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800130 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700131 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000132 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700133
134 static constexpr uid_t mConnectionUid = 443;
135 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800136};
137
138EventThreadTest::EventThreadTest() {
139 const ::testing::TestInfo* const test_info =
140 ::testing::UnitTest::GetInstance()->current_test_info();
141 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
142
Leon Scroggins III67388622023-02-06 20:36:20 -0500143 auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>();
144 mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>(
145 new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID,
146 std::make_shared<mock::VSyncTracker>(), mockDispatchPtr,
147 nullptr));
148 mock::VSyncDispatch& mockDispatch = *mockDispatchPtr;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800149 EXPECT_CALL(mockDispatch, registerCallback(_, _))
150 .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable()));
151 EXPECT_CALL(mockDispatch, schedule(_, _))
152 .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable()));
153 EXPECT_CALL(mockDispatch, update(_, _))
154 .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable()));
155 EXPECT_CALL(mockDispatch, unregisterCallback(_))
156 .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800157
Ady Abraham011f8ba2022-11-22 15:09:07 -0800158 createThread();
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700159 mConnection =
160 createConnection(mConnectionEventCallRecorder,
161 gui::ISurfaceComposer::EventRegistration::modeChanged |
162 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
Ady Abraham62f216c2020-10-13 19:07:23 -0700163 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700164 gui::ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700165 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800166
167 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800168 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
169 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800170}
171
172EventThreadTest::~EventThreadTest() {
173 const ::testing::TestInfo* const test_info =
174 ::testing::UnitTest::GetInstance()->current_test_info();
175 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800176
Ady Abraham011f8ba2022-11-22 15:09:07 -0800177 mThread.reset();
Dominik Laskowski029cc122019-01-23 19:52:06 -0800178 // EventThread should unregister itself as VSyncSource callback.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800179 EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800180}
181
Ady Abraham011f8ba2022-11-22 15:09:07 -0800182void EventThreadTest::createThread() {
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500183 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
184 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
185 return (uid == mThrottledConnectionUid);
186 };
187 const auto getVsyncPeriod = [](uid_t uid) {
188 return VSYNC_PERIOD.count();
189 };
190
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,
193 mTokenManager.get(), throttleVsync,
194 getVsyncPeriod, kWorkDuration, 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());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800198}
199
200sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700201 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
202 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800203 sp<MockEventThreadConnection> connection =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700204 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid,
205 mResyncCallRecorder.getInvocable(),
206 eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800207 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
208 return connection;
209}
210
Ady Abraham011f8ba2022-11-22 15:09:07 -0800211void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) {
212 if (expectState) {
213 ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value());
214 } else {
215 ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value());
216 }
Lloyd Pique24b0a482018-03-09 18:52:26 -0800217}
218
Ady Abraham9c53ee72020-07-22 21:16:18 -0700219void EventThreadTest::expectVSyncSetDurationCallReceived(
220 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800221 auto args = mVSyncCallbackUpdateRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800222 ASSERT_TRUE(args.has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800223 EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration);
224 EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800225}
226
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500227void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700228 auto args = mThrottleVsyncCallRecorder.waitForCall();
229 ASSERT_TRUE(args.has_value());
230 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
231 EXPECT_EQ(uid, std::get<1>(args.value()));
232}
233
Lloyd Pique24b0a482018-03-09 18:52:26 -0800234void EventThreadTest::expectVsyncEventReceivedByConnection(
235 const char* name, ConnectionEventRecorder& connectionEventRecorder,
236 nsecs_t expectedTimestamp, unsigned expectedCount) {
237 auto args = connectionEventRecorder.waitForCall();
238 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
239 << expectedTimestamp;
240 const auto& event = std::get<0>(args.value());
241 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
242 << name << " did not get the correct event for timestamp " << expectedTimestamp;
243 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
244 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
245 EXPECT_EQ(expectedCount, event.vsync.count)
246 << name << " did not get the expected count for timestamp " << expectedTimestamp;
247}
248
249void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
250 unsigned expectedCount) {
251 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
252 mConnectionEventCallRecorder, expectedTimestamp,
253 expectedCount);
254}
255
Rachel Leeb9c5a772022-02-04 21:17:37 -0800256void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
Ady Abraham011f8ba2022-11-22 15:09:07 -0800257 nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000258 auto args = mConnectionEventCallRecorder.waitForCall();
259 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
260 << expectedTimestamp;
261 const auto& event = std::get<0>(args.value());
Rachel Leeef2e21f2022-02-01 14:51:34 -0800262 for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800263 auto prediction = mTokenManager->getPredictionsForToken(
264 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000265 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800266 EXPECT_EQ(prediction.value().endTime,
267 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000268 << "Deadline timestamp does not match cached value";
269 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800270 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
271 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000272
Rachel Lee3f028662021-11-04 19:32:24 +0000273 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800274 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
275 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000276 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800277 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
278 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
279 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000280 }
Rachel Lee0d943202022-02-01 23:29:41 -0800281
282 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800283 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800284 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800285 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
286 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
287 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800288 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800289 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
290 preferredVsyncData.expectedPresentationTime)
291 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000292 }
293 }
294}
295
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800296void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
297 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800298 auto args = mConnectionEventCallRecorder.waitForCall();
299 ASSERT_TRUE(args.has_value());
300 const auto& event = std::get<0>(args.value());
301 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800302 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800303 EXPECT_EQ(expectedConnected, event.hotplug.connected);
304}
305
Ady Abraham447052e2019-02-13 16:07:27 -0800306void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700307 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
308 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800309 auto args = mConnectionEventCallRecorder.waitForCall();
310 ASSERT_TRUE(args.has_value());
311 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100312 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800313 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100314 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
315 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800316}
317
Ady Abraham62f216c2020-10-13 19:07:23 -0700318void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
319 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
320 for (const auto [uid, frameRateHz] : expectedOverrides) {
321 auto args = mConnectionEventCallRecorder.waitForCall();
322 ASSERT_TRUE(args.has_value());
323 const auto& event = std::get<0>(args.value());
324 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
325 EXPECT_EQ(expectedDisplayId, event.header.displayId);
326 EXPECT_EQ(uid, event.frameRateOverride.uid);
327 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
328 }
329
330 auto args = mConnectionEventCallRecorder.waitForCall();
331 ASSERT_TRUE(args.has_value());
332 const auto& event = std::get<0>(args.value());
333 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
334 EXPECT_EQ(expectedDisplayId, event.header.displayId);
335}
336
Lloyd Pique24b0a482018-03-09 18:52:26 -0800337namespace {
338
Rachel Leeef2e21f2022-02-01 14:51:34 -0800339using namespace testing;
340
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341/* ------------------------------------------------------------------------
342 * Test cases
343 */
344
345TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
Ady Abraham011f8ba2022-11-22 15:09:07 -0800346 EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value());
347 EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value());
348 EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value());
349 EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800350 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
352}
353
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800354TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800355 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
356 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800357
358 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700359 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800360
361 // EventThread should not enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800362 expectVSyncCallbackScheduleReceived(false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800363}
364
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
366 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700367 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368
Ady Abraham8532d012019-05-08 14:50:56 -0700369 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
371
Ady Abraham011f8ba2022-11-22 15:09:07 -0800372 // EventThread should enable schedule a vsync callback
373 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374
375 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700376 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800377 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500378 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 expectVsyncEventReceivedByConnection(123, 1u);
380
Ady Abraham011f8ba2022-11-22 15:09:07 -0800381 // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback
382 expectVSyncCallbackScheduleReceived(true);
383
Lloyd Pique24b0a482018-03-09 18:52:26 -0800384 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700385 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800387 onVSyncEvent(456, 123, 0);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700388 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800389 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
390
391 // EventThread should also detect that at this point that it does not need
392 // any more vsync events, and should disable their generation.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800393 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800394}
395
Rachel Lee3f028662021-11-04 19:32:24 +0000396TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
397 // Signal that we want the next vsync event to be posted to the connection
398 mThread->requestNextVsync(mConnection);
399
Ady Abraham011f8ba2022-11-22 15:09:07 -0800400 expectVSyncCallbackScheduleReceived(true);
Rachel Lee3f028662021-11-04 19:32:24 +0000401
402 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700403 // The throttler should receive the event, as well as the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800404 onVSyncEvent(123, 456, 789);
405 expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456});
Rachel Lee3f028662021-11-04 19:32:24 +0000406}
407
Rachel Leeef2e21f2022-02-01 14:51:34 -0800408TEST_F(EventThreadTest, getLatestVsyncEventData) {
409 const nsecs_t now = systemTime();
Rachel Leeb9c5a772022-02-04 21:17:37 -0800410 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800411 const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count();
412
413 mock::VSyncTracker& mockTracker =
414 *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker());
415 EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_))
416 .WillOnce(Return(preferredExpectedPresentationTime));
Rachel Leeef2e21f2022-02-01 14:51:34 -0800417
418 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700419
420 // Check EventThread immediately requested a resync.
421 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
422
Rachel Leeef2e21f2022-02-01 14:51:34 -0800423 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
424 << "Deadline timestamp should be greater than frame time";
425 for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
426 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800427 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800428 EXPECT_TRUE(prediction.has_value());
429 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
430 << "Deadline timestamp does not match cached value";
431 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800432 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800433 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800434 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800435 vsyncEventData.frameTimelines[i].deadlineTimestamp)
436 << "Expected vsync timestamp should be greater than deadline";
437
438 if (i > 0) {
439 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
440 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
441 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800442 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
443 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800444 << "Expected vsync timestamp out of order for frame timeline " << i;
445 }
446
447 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800448 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800449 << "Vsync ID incorrect for frame timeline " << i;
450 if (i == vsyncEventData.preferredFrameTimelineIndex) {
451 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
452 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800453 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
454 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800455 << "Preferred expected vsync timestamp incorrect" << i;
456 }
457 }
458}
459
Lloyd Pique24b0a482018-03-09 18:52:26 -0800460TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
461 // Create a first connection, register it, and request a vsync rate of zero.
462 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700463 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800464 mThread->setVsyncRate(0, firstConnection);
465
466 // By itself, this should not enable vsync events
Ady Abraham011f8ba2022-11-22 15:09:07 -0800467 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800468
469 // However if there is another connection which wants events at a nonzero rate.....
470 ConnectionEventRecorder secondConnectionEventRecorder{0};
471 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700472 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800473 mThread->setVsyncRate(1, secondConnection);
474
Dominik Laskowski029cc122019-01-23 19:52:06 -0800475 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800476 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800477
478 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700479 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800480 // get the event.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800481 onVSyncEvent(123, 0456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800482 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
483 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
484 1u);
485}
486
487TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
488 mThread->setVsyncRate(1, mConnection);
489
Dominik Laskowski029cc122019-01-23 19:52:06 -0800490 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800491 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800492
493 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700494 // throttler, and the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800495 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500496 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800497 expectVsyncEventReceivedByConnection(123, 1u);
498
499 // A second event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800500 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500501 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800502 expectVsyncEventReceivedByConnection(456, 2u);
503
504 // A third event should go to the same places.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800505 onVSyncEvent(789, 777, 111);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500506 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800507 expectVsyncEventReceivedByConnection(789, 3u);
508}
509
510TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
511 mThread->setVsyncRate(2, mConnection);
512
Dominik Laskowski029cc122019-01-23 19:52:06 -0800513 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800514 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800515
Huihong Luoab8ffef2022-08-18 13:02:26 -0700516 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800517 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800518 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700519 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800520
Huihong Luoab8ffef2022-08-18 13:02:26 -0700521 // The second event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800522 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700524 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800525
Huihong Luoab8ffef2022-08-18 13:02:26 -0700526 // The third event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800527 onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800528 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700529 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800530
Huihong Luoab8ffef2022-08-18 13:02:26 -0700531 // The fourth event will be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800532 onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800533 expectVsyncEventReceivedByConnection(101112, 4u);
534}
535
536TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
537 mThread->setVsyncRate(1, mConnection);
538
Dominik Laskowski029cc122019-01-23 19:52:06 -0800539 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800540 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800541
542 // Destroy the only (strong) reference to the connection.
543 mConnection = nullptr;
544
Huihong Luoab8ffef2022-08-18 13:02:26 -0700545 // The first event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800546 onVSyncEvent(123, 56, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800547 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
548
549 // EventThread should disable vsync callbacks
Ady Abraham011f8ba2022-11-22 15:09:07 -0800550 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800551}
552
553TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
554 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700555 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800556 mThread->setVsyncRate(1, errorConnection);
557
Dominik Laskowski029cc122019-01-23 19:52:06 -0800558 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800559 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800560
Huihong Luoab8ffef2022-08-18 13:02:26 -0700561 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800562 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800563 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
564
Ady Abraham011f8ba2022-11-22 15:09:07 -0800565 // Another schedule is expected, since the connection is removed only after
566 // the next vsync is requested.
567 expectVSyncCallbackScheduleReceived(true);
568
Huihong Luoab8ffef2022-08-18 13:02:26 -0700569 // A subsequent event will not be seen by the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800570 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800571 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
572
573 // EventThread should disable vsync callbacks with the second event
Ady Abraham011f8ba2022-11-22 15:09:07 -0800574 expectVSyncCallbackScheduleReceived(false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800575}
576
Alec Mouri717bcb62020-02-10 17:07:19 -0800577TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700578 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800579 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700580 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800581 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700582 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800583 ConnectionEventRecorder secondConnectionEventRecorder{0};
584 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700585 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800586 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700587 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800588
589 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800590 expectVSyncCallbackScheduleReceived(true);
Alec Mouri717bcb62020-02-10 17:07:19 -0800591
Huihong Luoab8ffef2022-08-18 13:02:26 -0700592 // The first event will be seen by the connection, which then returns an error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800593 onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800594 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
595 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
596 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700597 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800598}
599
Lloyd Pique24b0a482018-03-09 18:52:26 -0800600TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
601 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700602 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800603 mThread->setVsyncRate(1, errorConnection);
604
Dominik Laskowski029cc122019-01-23 19:52:06 -0800605 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800606 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800607
Huihong Luoab8ffef2022-08-18 13:02:26 -0700608 // The first event will be seen by the connection, which then returns a non-fatal error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800609 onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800610 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800611 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800612
Huihong Luoab8ffef2022-08-18 13:02:26 -0700613 // A subsequent event will be seen by the connection, which still then returns a non-fatal
614 // error.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800615 onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800616 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800617 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618
619 // EventThread will not disable vsync callbacks as the errors are non-fatal.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800620 onVSyncEvent(456, 123, 0);
621 expectVSyncCallbackScheduleReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800622}
623
624TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700625 mThread->setDuration(321ns, 456ns);
626 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800627}
628
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800629TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
630 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
631 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800632}
633
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800634TEST_F(EventThreadTest, postHotplugInternalConnect) {
635 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
636 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800637}
638
639TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800640 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
641 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800642}
643
644TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800645 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
646 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800647}
648
Ady Abraham447052e2019-02-13 16:07:27 -0800649TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700650 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
651 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
652 .setId(DisplayModeId(7))
653 .setVsyncPeriod(16666666)
654 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800655 const Fps fps = mode->getFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700656
Ady Abraham67434eb2022-12-01 17:48:12 -0800657 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
658 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800659}
660
661TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700662 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
663 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
664 .setId(DisplayModeId(5))
665 .setVsyncPeriod(16666666)
666 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800667 const Fps fps = mode->getFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700668
Ady Abraham67434eb2022-12-01 17:48:12 -0800669 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
670 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs());
Ady Abraham447052e2019-02-13 16:07:27 -0800671}
672
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700673TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700674 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
675 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
676 .setId(DisplayModeId(7))
677 .setVsyncPeriod(16666666)
678 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800679 const Fps fps = mode->getFps() / 2;
680 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
681 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs());
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700682}
683
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700684TEST_F(EventThreadTest, suppressConfigChanged) {
685 ConnectionEventRecorder suppressConnectionEventRecorder{0};
686 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700687 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700688
Ady Abraham690f4612021-07-01 23:24:03 -0700689 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
690 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
691 .setId(DisplayModeId(9))
692 .setVsyncPeriod(16666666)
693 .build();
Ady Abraham67434eb2022-12-01 17:48:12 -0800694 const Fps fps = mode->getFps() / 2;
Ady Abraham690f4612021-07-01 23:24:03 -0700695
Ady Abraham67434eb2022-12-01 17:48:12 -0800696 mThread->onModeChanged({fps, ftl::as_non_null(mode)});
697 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs());
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700698
699 auto args = suppressConnectionEventRecorder.waitForCall();
700 ASSERT_FALSE(args.has_value());
701}
702
Ady Abraham62f216c2020-10-13 19:07:23 -0700703TEST_F(EventThreadTest, postUidFrameRateMapping) {
704 const std::vector<FrameRateOverride> overrides = {
705 {.uid = 1, .frameRateHz = 20},
706 {.uid = 3, .frameRateHz = 40},
707 {.uid = 5, .frameRateHz = 60},
708 };
709
710 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
711 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
712}
713
714TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
715 const std::vector<FrameRateOverride> overrides = {
716 {.uid = 1, .frameRateHz = 20},
717 {.uid = 3, .frameRateHz = 40},
718 {.uid = 5, .frameRateHz = 60},
719 };
720
721 ConnectionEventRecorder suppressConnectionEventRecorder{0};
722 sp<MockEventThreadConnection> suppressConnection =
723 createConnection(suppressConnectionEventRecorder);
724
725 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
726 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
727
728 auto args = suppressConnectionEventRecorder.waitForCall();
729 ASSERT_FALSE(args.has_value());
730}
731
Ady Abraham0bb6a472020-10-12 10:22:13 -0700732TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
733 // Signal that we want the next vsync event to be posted to the throttled connection
734 mThread->requestNextVsync(mThrottledConnection);
735
736 // EventThread should immediately request a resync.
737 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
738
739 // EventThread should enable vsync callbacks.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800740 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700741
742 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700743 // The throttler should receive the event, but not the connection.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800744 onVSyncEvent(123, 456, 789);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500745 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700746 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
Ady Abraham011f8ba2022-11-22 15:09:07 -0800747 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700748
749 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700750 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700751 // not as it was only interested in the first.
Ady Abraham011f8ba2022-11-22 15:09:07 -0800752 onVSyncEvent(456, 123, 0);
Leon Scroggins IIIdb16a2b2023-02-06 17:50:05 -0500753 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700754 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham011f8ba2022-11-22 15:09:07 -0800755 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700756
757 // EventThread should not change the vsync state as it didn't send the event
758 // yet
Ady Abraham011f8ba2022-11-22 15:09:07 -0800759 onVSyncEvent(456, 123, 0);
760 expectVSyncCallbackScheduleReceived(true);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700761}
762
Lloyd Pique24b0a482018-03-09 18:52:26 -0800763} // namespace
764} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100765
766// TODO(b/129481165): remove the #pragma below and fix conversion issues
767#pragma clang diagnostic pop // ignored "-Wextra"