blob: 978afc510ae291489ae0c2536465075ce9fd9e19 [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"
Lloyd Pique24b0a482018-03-09 18:52:26 -080033
34using namespace std::chrono_literals;
35using namespace std::placeholders;
36
37using testing::_;
38using testing::Invoke;
39
40namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080041
Dominik Laskowski2f01d772022-03-23 16:01:29 -070042using namespace ftl::flag_operators;
43
Lloyd Pique24b0a482018-03-09 18:52:26 -080044namespace {
45
Dominik Laskowskif1833852021-03-23 15:06:50 -070046constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
47constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
48constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
49 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
50
Jorim Jaggic0086af2021-02-12 18:18:11 +010051constexpr std::chrono::duration VSYNC_PERIOD(16ms);
Dominik Laskowskif1833852021-03-23 15:06:50 -070052
Lloyd Pique24b0a482018-03-09 18:52:26 -080053class MockVSyncSource : public VSyncSource {
54public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070055 const char* getName() const override { return "test"; }
56
Lloyd Pique24b0a482018-03-09 18:52:26 -080057 MOCK_METHOD1(setVSyncEnabled, void(bool));
58 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070059 MOCK_METHOD2(setDuration,
60 void(std::chrono::nanoseconds workDuration,
61 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080062 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Rachel Leeef2e21f2022-02-01 14:51:34 -080063 MOCK_METHOD(VSyncSource::VSyncData, getLatestVSyncData, (), (const, override));
Ady Abraham5e7371c2020-03-24 14:47:24 -070064 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080065};
66
67} // namespace
68
69class EventThreadTest : public testing::Test {
70protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080071 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080072 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070073 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
74 ResyncCallback&& resyncCallback,
Huihong Luo1b0c49f2022-03-15 19:18:21 -070075 EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070076 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070077 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080078 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
79 };
80
81 using ConnectionEventRecorder =
82 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
83
84 EventThreadTest();
85 ~EventThreadTest() override;
86
Dominik Laskowski6505f792019-09-18 11:10:05 -070087 void createThread(std::unique_ptr<VSyncSource>);
Huihong Luo1b0c49f2022-03-15 19:18:21 -070088 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
89 EventRegistrationFlags eventRegistration = {},
90 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080091
92 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070093 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
94 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080095 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
96 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
97 void expectVsyncEventReceivedByConnection(const char* name,
98 ConnectionEventRecorder& connectionEventRecorder,
99 nsecs_t expectedTimestamp, unsigned expectedCount);
100 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Rachel Lee3f028662021-11-04 19:32:24 +0000101 void expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800102 VSyncSource::VSyncData preferredVsyncData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800103 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700104 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800105 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700106 int32_t expectedConfigId,
107 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700108 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700109 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
110 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800111
112 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
113 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700114 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
115 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800116 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
117 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700118 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700120 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800121
Dominik Laskowski6505f792019-09-18 11:10:05 -0700122 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800123 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700124 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700126 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000127 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700128
129 static constexpr uid_t mConnectionUid = 443;
130 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800131};
132
133EventThreadTest::EventThreadTest() {
134 const ::testing::TestInfo* const test_info =
135 ::testing::UnitTest::GetInstance()->current_test_info();
136 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
137
Dominik Laskowski6505f792019-09-18 11:10:05 -0700138 auto vsyncSource = std::make_unique<MockVSyncSource>();
139 mVSyncSource = vsyncSource.get();
140
141 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800142 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
143
Dominik Laskowski6505f792019-09-18 11:10:05 -0700144 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800145 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
146
Ady Abraham9c53ee72020-07-22 21:16:18 -0700147 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
148 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800149
Dominik Laskowski6505f792019-09-18 11:10:05 -0700150 createThread(std::move(vsyncSource));
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700151 mConnection =
152 createConnection(mConnectionEventCallRecorder,
153 gui::ISurfaceComposer::EventRegistration::modeChanged |
154 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
Ady Abraham62f216c2020-10-13 19:07:23 -0700155 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700156 gui::ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700157 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800158
159 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800160 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
161 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800162}
163
164EventThreadTest::~EventThreadTest() {
165 const ::testing::TestInfo* const test_info =
166 ::testing::UnitTest::GetInstance()->current_test_info();
167 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800168
169 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800170 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800171}
172
Dominik Laskowski6505f792019-09-18 11:10:05 -0700173void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700174 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
175 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
176 return (uid == mThrottledConnectionUid);
177 };
Jorim Jaggic0086af2021-02-12 18:18:11 +0100178 const auto getVsyncPeriod = [](uid_t uid) {
179 return VSYNC_PERIOD.count();
180 };
Ady Abraham0bb6a472020-10-12 10:22:13 -0700181
Rachel Lee3f028662021-11-04 19:32:24 +0000182 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
183 mThread = std::make_unique<impl::EventThread>(std::move(source), mTokenManager.get(),
Ady Abraham0bb6a472020-10-12 10:22:13 -0700184 mInterceptVSyncCallRecorder.getInvocable(),
Jorim Jaggic0086af2021-02-12 18:18:11 +0100185 throttleVsync, getVsyncPeriod);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800186
187 // EventThread should register itself as VSyncSource callback.
188 mCallback = expectVSyncSetCallbackCallReceived();
189 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800190}
191
192sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700193 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
194 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800195 sp<MockEventThreadConnection> connection =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700196 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid,
197 mResyncCallRecorder.getInvocable(),
198 eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800199 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
200 return connection;
201}
202
203void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
204 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
205 ASSERT_TRUE(args.has_value());
206 EXPECT_EQ(expectedState, std::get<0>(args.value()));
207}
208
Ady Abraham9c53ee72020-07-22 21:16:18 -0700209void EventThreadTest::expectVSyncSetDurationCallReceived(
210 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
211 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800212 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700213 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
214 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800215}
216
217VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
218 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
219 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
220}
221
222void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
223 auto args = mInterceptVSyncCallRecorder.waitForCall();
224 ASSERT_TRUE(args.has_value());
225 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
226}
227
Ady Abraham0bb6a472020-10-12 10:22:13 -0700228void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
229 auto args = mThrottleVsyncCallRecorder.waitForCall();
230 ASSERT_TRUE(args.has_value());
231 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
232 EXPECT_EQ(uid, std::get<1>(args.value()));
233}
234
Lloyd Pique24b0a482018-03-09 18:52:26 -0800235void EventThreadTest::expectVsyncEventReceivedByConnection(
236 const char* name, ConnectionEventRecorder& connectionEventRecorder,
237 nsecs_t expectedTimestamp, unsigned expectedCount) {
238 auto args = connectionEventRecorder.waitForCall();
239 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
240 << expectedTimestamp;
241 const auto& event = std::get<0>(args.value());
242 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
243 << name << " did not get the correct event for timestamp " << expectedTimestamp;
244 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
245 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
246 EXPECT_EQ(expectedCount, event.vsync.count)
247 << name << " did not get the expected count for timestamp " << expectedTimestamp;
248}
249
250void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
251 unsigned expectedCount) {
252 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
253 mConnectionEventCallRecorder, expectedTimestamp,
254 expectedCount);
255}
256
Rachel Leeb9c5a772022-02-04 21:17:37 -0800257void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
258 nsecs_t expectedTimestamp, VSyncSource::VSyncData preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000259 auto args = mConnectionEventCallRecorder.waitForCall();
260 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
261 << expectedTimestamp;
262 const auto& event = std::get<0>(args.value());
Rachel Leeef2e21f2022-02-01 14:51:34 -0800263 for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800264 auto prediction = mTokenManager->getPredictionsForToken(
265 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000266 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800267 EXPECT_EQ(prediction.value().endTime,
268 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000269 << "Deadline timestamp does not match cached value";
270 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800271 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
272 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000273
Rachel Lee3f028662021-11-04 19:32:24 +0000274 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800275 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
276 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000277 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800278 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
279 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
280 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000281 }
Rachel Lee0d943202022-02-01 23:29:41 -0800282
283 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800284 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800285 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800286 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
287 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
288 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800289 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800290 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
291 preferredVsyncData.expectedPresentationTime)
292 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000293 }
294 }
295}
296
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800297void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
298 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800299 auto args = mConnectionEventCallRecorder.waitForCall();
300 ASSERT_TRUE(args.has_value());
301 const auto& event = std::get<0>(args.value());
302 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800303 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800304 EXPECT_EQ(expectedConnected, event.hotplug.connected);
305}
306
Ady Abraham447052e2019-02-13 16:07:27 -0800307void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700308 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
309 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800310 auto args = mConnectionEventCallRecorder.waitForCall();
311 ASSERT_TRUE(args.has_value());
312 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100313 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800314 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100315 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
316 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800317}
318
Ady Abraham62f216c2020-10-13 19:07:23 -0700319void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
320 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
321 for (const auto [uid, frameRateHz] : expectedOverrides) {
322 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_FRAME_RATE_OVERRIDE, event.header.type);
326 EXPECT_EQ(expectedDisplayId, event.header.displayId);
327 EXPECT_EQ(uid, event.frameRateOverride.uid);
328 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
329 }
330
331 auto args = mConnectionEventCallRecorder.waitForCall();
332 ASSERT_TRUE(args.has_value());
333 const auto& event = std::get<0>(args.value());
334 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
335 EXPECT_EQ(expectedDisplayId, event.header.displayId);
336}
337
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338namespace {
339
Rachel Leeef2e21f2022-02-01 14:51:34 -0800340using namespace testing;
341
Lloyd Pique24b0a482018-03-09 18:52:26 -0800342/* ------------------------------------------------------------------------
343 * Test cases
344 */
345
346TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
347 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
348 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700349 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800350 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
351 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
352 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
353}
354
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800355TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800356 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
357 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800358
359 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700360 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800361
362 // EventThread should not enable vsync callbacks.
363 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800364}
365
Lloyd Pique24b0a482018-03-09 18:52:26 -0800366TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
367 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700368 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800369
Ady Abraham8532d012019-05-08 14:50:56 -0700370 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800371 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
372
Dominik Laskowski029cc122019-01-23 19:52:06 -0800373 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800375
376 // Use the received callback to signal a first vsync event.
377 // The interceptor should receive the event, as well as the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800378 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700380 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 expectVsyncEventReceivedByConnection(123, 1u);
382
383 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700384 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800386 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387 expectInterceptCallReceived(456);
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.
393 expectVSyncSetEnabledCallReceived(false);
394}
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
400 expectVSyncSetEnabledCallReceived(true);
401
402 // Use the received callback to signal a vsync event.
403 // The interceptor should receive the event, as well as the connection.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800404 VSyncSource::VSyncData vsyncData = {456, 789};
405 mCallback->onVSyncEvent(123, vsyncData);
Rachel Lee3f028662021-11-04 19:32:24 +0000406 expectInterceptCallReceived(123);
Rachel Leeb9c5a772022-02-04 21:17:37 -0800407 expectVsyncEventFrameTimelinesCorrect(123, vsyncData);
Rachel Lee3f028662021-11-04 19:32:24 +0000408}
409
Rachel Leeef2e21f2022-02-01 14:51:34 -0800410TEST_F(EventThreadTest, getLatestVsyncEventData) {
411 const nsecs_t now = systemTime();
412 const nsecs_t preferredDeadline = now + 10000000;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800413 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
414 const VSyncSource::VSyncData preferredData = {preferredExpectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800415 preferredDeadline};
416 EXPECT_CALL(*mVSyncSource, getLatestVSyncData()).WillOnce(Return(preferredData));
417
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
467 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
468 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
469
470 // However if there is another connection which wants events at a nonzero rate.....
471 ConnectionEventRecorder secondConnectionEventRecorder{0};
472 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700473 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800474 mThread->setVsyncRate(1, secondConnection);
475
Dominik Laskowski029cc122019-01-23 19:52:06 -0800476 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800477 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800478
479 // Send a vsync event. EventThread should then make a call to the
480 // interceptor, and the second connection. The first connection should not
481 // get the event.
Rachel Leef16da3c2022-01-20 13:57:18 -0800482 mCallback->onVSyncEvent(123, {456, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800483 expectInterceptCallReceived(123);
484 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
485 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
486 1u);
487}
488
489TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
490 mThread->setVsyncRate(1, mConnection);
491
Dominik Laskowski029cc122019-01-23 19:52:06 -0800492 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800493 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800494
495 // Send a vsync event. EventThread should then make a call to the
496 // interceptor, and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800497 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800498 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700499 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800500 expectVsyncEventReceivedByConnection(123, 1u);
501
502 // A second event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800503 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800504 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700505 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800506 expectVsyncEventReceivedByConnection(456, 2u);
507
508 // A third event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800509 mCallback->onVSyncEvent(789, {777, 111});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800510 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700511 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800512 expectVsyncEventReceivedByConnection(789, 3u);
513}
514
515TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
516 mThread->setVsyncRate(2, mConnection);
517
Dominik Laskowski029cc122019-01-23 19:52:06 -0800518 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800519 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800520
521 // The first event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800522 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523 expectInterceptCallReceived(123);
524 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700525 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800526
527 // The second event will be seen by the interceptor and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800528 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800529 expectInterceptCallReceived(456);
530 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700531 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800532
533 // The third event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800534 mCallback->onVSyncEvent(789, {777, 744});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800535 expectInterceptCallReceived(789);
536 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700537 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800538
539 // The fourth event will be seen by the interceptor and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800540 mCallback->onVSyncEvent(101112, {7847, 86});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800541 expectInterceptCallReceived(101112);
542 expectVsyncEventReceivedByConnection(101112, 4u);
543}
544
545TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
546 mThread->setVsyncRate(1, mConnection);
547
Dominik Laskowski029cc122019-01-23 19:52:06 -0800548 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800549 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800550
551 // Destroy the only (strong) reference to the connection.
552 mConnection = nullptr;
553
554 // The first event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800555 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800556 expectInterceptCallReceived(123);
557 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
558
559 // EventThread should disable vsync callbacks
560 expectVSyncSetEnabledCallReceived(false);
561}
562
563TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
564 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700565 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800566 mThread->setVsyncRate(1, errorConnection);
567
Dominik Laskowski029cc122019-01-23 19:52:06 -0800568 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800569 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800570
571 // The first event will be seen by the interceptor, and by the connection,
572 // which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800573 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800574 expectInterceptCallReceived(123);
575 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
576
577 // A subsequent event will be seen by the interceptor and not by the
578 // connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800579 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800580 expectInterceptCallReceived(456);
581 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
582
583 // EventThread should disable vsync callbacks with the second event
584 expectVSyncSetEnabledCallReceived(false);
585}
586
Alec Mouri717bcb62020-02-10 17:07:19 -0800587TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700588 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800589 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700590 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800591 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700592 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800593 ConnectionEventRecorder secondConnectionEventRecorder{0};
594 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700595 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800596 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700597 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800598
599 // EventThread should enable vsync callbacks.
600 expectVSyncSetEnabledCallReceived(true);
601
602 // The first event will be seen by the interceptor, and by the connection,
603 // which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800604 mCallback->onVSyncEvent(123, {456, 789});
Alec Mouri717bcb62020-02-10 17:07:19 -0800605 expectInterceptCallReceived(123);
606 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
607 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
608 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700609 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800610}
611
Lloyd Pique24b0a482018-03-09 18:52:26 -0800612TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
613 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700614 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800615 mThread->setVsyncRate(1, errorConnection);
616
Dominik Laskowski029cc122019-01-23 19:52:06 -0800617 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800619
620 // The first event will be seen by the interceptor, and by the connection,
621 // which then returns an non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800622 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800623 expectInterceptCallReceived(123);
624 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
625
626 // A subsequent event will be seen by the interceptor, and by the connection,
627 // which still then returns an non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800628 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800629 expectInterceptCallReceived(456);
630 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
631
632 // EventThread will not disable vsync callbacks as the errors are non-fatal.
633 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
634}
635
636TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700637 mThread->setDuration(321ns, 456ns);
638 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800639}
640
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800641TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
642 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
643 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800644}
645
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800646TEST_F(EventThreadTest, postHotplugInternalConnect) {
647 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
648 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800649}
650
651TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800652 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
653 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800654}
655
656TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800657 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
658 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800659}
660
Ady Abraham447052e2019-02-13 16:07:27 -0800661TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700662 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
663 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
664 .setId(DisplayModeId(7))
665 .setVsyncPeriod(16666666)
666 .build();
667
668 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700669 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800670}
671
672TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700673 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
674 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
675 .setId(DisplayModeId(5))
676 .setVsyncPeriod(16666666)
677 .build();
678
679 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700680 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800681}
682
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700683TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700684 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
685 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
686 .setId(DisplayModeId(7))
687 .setVsyncPeriod(16666666)
688 .build();
689 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700690 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700691}
692
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700693TEST_F(EventThreadTest, suppressConfigChanged) {
694 ConnectionEventRecorder suppressConnectionEventRecorder{0};
695 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700696 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700697
Ady Abraham690f4612021-07-01 23:24:03 -0700698 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
699 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
700 .setId(DisplayModeId(9))
701 .setVsyncPeriod(16666666)
702 .build();
703
704 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700705 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700706
707 auto args = suppressConnectionEventRecorder.waitForCall();
708 ASSERT_FALSE(args.has_value());
709}
710
Ady Abraham62f216c2020-10-13 19:07:23 -0700711TEST_F(EventThreadTest, postUidFrameRateMapping) {
712 const std::vector<FrameRateOverride> overrides = {
713 {.uid = 1, .frameRateHz = 20},
714 {.uid = 3, .frameRateHz = 40},
715 {.uid = 5, .frameRateHz = 60},
716 };
717
718 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
719 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
720}
721
722TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
723 const std::vector<FrameRateOverride> overrides = {
724 {.uid = 1, .frameRateHz = 20},
725 {.uid = 3, .frameRateHz = 40},
726 {.uid = 5, .frameRateHz = 60},
727 };
728
729 ConnectionEventRecorder suppressConnectionEventRecorder{0};
730 sp<MockEventThreadConnection> suppressConnection =
731 createConnection(suppressConnectionEventRecorder);
732
733 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
734 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
735
736 auto args = suppressConnectionEventRecorder.waitForCall();
737 ASSERT_FALSE(args.has_value());
738}
739
Ady Abraham0bb6a472020-10-12 10:22:13 -0700740TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
741 // Signal that we want the next vsync event to be posted to the throttled connection
742 mThread->requestNextVsync(mThrottledConnection);
743
744 // EventThread should immediately request a resync.
745 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
746
747 // EventThread should enable vsync callbacks.
748 expectVSyncSetEnabledCallReceived(true);
749
750 // Use the received callback to signal a first vsync event.
751 // The interceptor should receive the event, but not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800752 mCallback->onVSyncEvent(123, {456, 789});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700753 expectInterceptCallReceived(123);
754 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
755 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
756
757 // Use the received callback to signal a second vsync event.
758 // The interceptor should receive the event, but the connection should
759 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800760 mCallback->onVSyncEvent(456, {123, 0});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700761 expectInterceptCallReceived(456);
762 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
763 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
764
765 // EventThread should not change the vsync state as it didn't send the event
766 // yet
767 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
768}
769
Lloyd Pique24b0a482018-03-09 18:52:26 -0800770} // namespace
771} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100772
773// TODO(b/129481165): remove the #pragma below and fix conversion issues
774#pragma clang diagnostic pop // ignored "-Wextra"