blob: a5beaba2866d81de8f36aa4b3554f515fc256a3e [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();
Lloyd Pique24b0a482018-03-09 18:52:26 -080096 void expectVsyncEventReceivedByConnection(const char* name,
97 ConnectionEventRecorder& connectionEventRecorder,
98 nsecs_t expectedTimestamp, unsigned expectedCount);
99 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Rachel Lee3f028662021-11-04 19:32:24 +0000100 void expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800101 VSyncSource::VSyncData preferredVsyncData);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800102 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700103 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800104 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700105 int32_t expectedConfigId,
106 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700107 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700108 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
109 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800110
111 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
112 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
114 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800115 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700116 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800117 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700118 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119
Dominik Laskowski6505f792019-09-18 11:10:05 -0700120 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800121 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700122 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700124 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000125 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700126
127 static constexpr uid_t mConnectionUid = 443;
128 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800129};
130
131EventThreadTest::EventThreadTest() {
132 const ::testing::TestInfo* const test_info =
133 ::testing::UnitTest::GetInstance()->current_test_info();
134 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
135
Dominik Laskowski6505f792019-09-18 11:10:05 -0700136 auto vsyncSource = std::make_unique<MockVSyncSource>();
137 mVSyncSource = vsyncSource.get();
138
139 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800140 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
141
Dominik Laskowski6505f792019-09-18 11:10:05 -0700142 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800143 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
144
Ady Abraham9c53ee72020-07-22 21:16:18 -0700145 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
146 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800147
Dominik Laskowski6505f792019-09-18 11:10:05 -0700148 createThread(std::move(vsyncSource));
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700149 mConnection =
150 createConnection(mConnectionEventCallRecorder,
151 gui::ISurfaceComposer::EventRegistration::modeChanged |
152 gui::ISurfaceComposer::EventRegistration::frameRateOverride);
Ady Abraham62f216c2020-10-13 19:07:23 -0700153 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700154 gui::ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700155 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800156
157 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800158 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
159 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800160}
161
162EventThreadTest::~EventThreadTest() {
163 const ::testing::TestInfo* const test_info =
164 ::testing::UnitTest::GetInstance()->current_test_info();
165 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800166
167 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800168 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800169}
170
Dominik Laskowski6505f792019-09-18 11:10:05 -0700171void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700172 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
173 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
174 return (uid == mThrottledConnectionUid);
175 };
Jorim Jaggic0086af2021-02-12 18:18:11 +0100176 const auto getVsyncPeriod = [](uid_t uid) {
177 return VSYNC_PERIOD.count();
178 };
Ady Abraham0bb6a472020-10-12 10:22:13 -0700179
Rachel Lee3f028662021-11-04 19:32:24 +0000180 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
181 mThread = std::make_unique<impl::EventThread>(std::move(source), mTokenManager.get(),
Jorim Jaggic0086af2021-02-12 18:18:11 +0100182 throttleVsync, getVsyncPeriod);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800183
184 // EventThread should register itself as VSyncSource callback.
185 mCallback = expectVSyncSetCallbackCallReceived();
186 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800187}
188
189sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Huihong Luo1b0c49f2022-03-15 19:18:21 -0700190 ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration,
191 uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800192 sp<MockEventThreadConnection> connection =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700193 sp<MockEventThreadConnection>::make(mThread.get(), ownerUid,
194 mResyncCallRecorder.getInvocable(),
195 eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800196 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
197 return connection;
198}
199
200void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
201 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
202 ASSERT_TRUE(args.has_value());
203 EXPECT_EQ(expectedState, std::get<0>(args.value()));
204}
205
Ady Abraham9c53ee72020-07-22 21:16:18 -0700206void EventThreadTest::expectVSyncSetDurationCallReceived(
207 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
208 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800209 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700210 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
211 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800212}
213
214VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
215 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
216 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
217}
218
Ady Abraham0bb6a472020-10-12 10:22:13 -0700219void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
220 auto args = mThrottleVsyncCallRecorder.waitForCall();
221 ASSERT_TRUE(args.has_value());
222 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
223 EXPECT_EQ(uid, std::get<1>(args.value()));
224}
225
Lloyd Pique24b0a482018-03-09 18:52:26 -0800226void EventThreadTest::expectVsyncEventReceivedByConnection(
227 const char* name, ConnectionEventRecorder& connectionEventRecorder,
228 nsecs_t expectedTimestamp, unsigned expectedCount) {
229 auto args = connectionEventRecorder.waitForCall();
230 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
231 << expectedTimestamp;
232 const auto& event = std::get<0>(args.value());
233 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
234 << name << " did not get the correct event for timestamp " << expectedTimestamp;
235 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
236 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
237 EXPECT_EQ(expectedCount, event.vsync.count)
238 << name << " did not get the expected count for timestamp " << expectedTimestamp;
239}
240
241void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
242 unsigned expectedCount) {
243 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
244 mConnectionEventCallRecorder, expectedTimestamp,
245 expectedCount);
246}
247
Rachel Leeb9c5a772022-02-04 21:17:37 -0800248void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
249 nsecs_t expectedTimestamp, VSyncSource::VSyncData preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000250 auto args = mConnectionEventCallRecorder.waitForCall();
251 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
252 << expectedTimestamp;
253 const auto& event = std::get<0>(args.value());
Rachel Leeef2e21f2022-02-01 14:51:34 -0800254 for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800255 auto prediction = mTokenManager->getPredictionsForToken(
256 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000257 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800258 EXPECT_EQ(prediction.value().endTime,
259 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000260 << "Deadline timestamp does not match cached value";
261 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800262 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
263 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000264
Rachel Lee3f028662021-11-04 19:32:24 +0000265 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800266 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
267 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000268 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800269 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
270 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
271 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000272 }
Rachel Lee0d943202022-02-01 23:29:41 -0800273
274 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800275 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800276 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800277 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
278 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
279 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800280 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800281 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
282 preferredVsyncData.expectedPresentationTime)
283 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000284 }
285 }
286}
287
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800288void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
289 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800290 auto args = mConnectionEventCallRecorder.waitForCall();
291 ASSERT_TRUE(args.has_value());
292 const auto& event = std::get<0>(args.value());
293 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800294 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800295 EXPECT_EQ(expectedConnected, event.hotplug.connected);
296}
297
Ady Abraham447052e2019-02-13 16:07:27 -0800298void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700299 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
300 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800301 auto args = mConnectionEventCallRecorder.waitForCall();
302 ASSERT_TRUE(args.has_value());
303 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100304 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800305 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100306 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
307 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800308}
309
Ady Abraham62f216c2020-10-13 19:07:23 -0700310void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
311 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
312 for (const auto [uid, frameRateHz] : expectedOverrides) {
313 auto args = mConnectionEventCallRecorder.waitForCall();
314 ASSERT_TRUE(args.has_value());
315 const auto& event = std::get<0>(args.value());
316 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
317 EXPECT_EQ(expectedDisplayId, event.header.displayId);
318 EXPECT_EQ(uid, event.frameRateOverride.uid);
319 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
320 }
321
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_FLUSH, event.header.type);
326 EXPECT_EQ(expectedDisplayId, event.header.displayId);
327}
328
Lloyd Pique24b0a482018-03-09 18:52:26 -0800329namespace {
330
Rachel Leeef2e21f2022-02-01 14:51:34 -0800331using namespace testing;
332
Lloyd Pique24b0a482018-03-09 18:52:26 -0800333/* ------------------------------------------------------------------------
334 * Test cases
335 */
336
337TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
338 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
339 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700340 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800342 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
343}
344
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800345TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800346 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
347 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800348
349 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700350 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800351
352 // EventThread should not enable vsync callbacks.
353 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800354}
355
Lloyd Pique24b0a482018-03-09 18:52:26 -0800356TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
357 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700358 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800359
Ady Abraham8532d012019-05-08 14:50:56 -0700360 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800361 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
362
Dominik Laskowski029cc122019-01-23 19:52:06 -0800363 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365
366 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700367 // The throttler should receive the event, as well as the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800368 mCallback->onVSyncEvent(123, {456, 789});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700369 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 expectVsyncEventReceivedByConnection(123, 1u);
371
372 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700373 // The throttler should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800375 mCallback->onVSyncEvent(456, {123, 0});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700376 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
378
379 // EventThread should also detect that at this point that it does not need
380 // any more vsync events, and should disable their generation.
381 expectVSyncSetEnabledCallReceived(false);
382}
383
Rachel Lee3f028662021-11-04 19:32:24 +0000384TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
385 // Signal that we want the next vsync event to be posted to the connection
386 mThread->requestNextVsync(mConnection);
387
388 expectVSyncSetEnabledCallReceived(true);
389
390 // Use the received callback to signal a vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700391 // The throttler should receive the event, as well as the connection.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800392 VSyncSource::VSyncData vsyncData = {456, 789};
393 mCallback->onVSyncEvent(123, vsyncData);
Rachel Leeb9c5a772022-02-04 21:17:37 -0800394 expectVsyncEventFrameTimelinesCorrect(123, vsyncData);
Rachel Lee3f028662021-11-04 19:32:24 +0000395}
396
Rachel Leeef2e21f2022-02-01 14:51:34 -0800397TEST_F(EventThreadTest, getLatestVsyncEventData) {
398 const nsecs_t now = systemTime();
399 const nsecs_t preferredDeadline = now + 10000000;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800400 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
401 const VSyncSource::VSyncData preferredData = {preferredExpectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800402 preferredDeadline};
403 EXPECT_CALL(*mVSyncSource, getLatestVSyncData()).WillOnce(Return(preferredData));
404
405 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700406
407 // Check EventThread immediately requested a resync.
408 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
409
Rachel Leeef2e21f2022-02-01 14:51:34 -0800410 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
411 << "Deadline timestamp should be greater than frame time";
412 for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
413 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800414 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800415 EXPECT_TRUE(prediction.has_value());
416 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
417 << "Deadline timestamp does not match cached value";
418 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800419 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800420 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800421 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800422 vsyncEventData.frameTimelines[i].deadlineTimestamp)
423 << "Expected vsync timestamp should be greater than deadline";
424
425 if (i > 0) {
426 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
427 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
428 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800429 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
430 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800431 << "Expected vsync timestamp out of order for frame timeline " << i;
432 }
433
434 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800435 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800436 << "Vsync ID incorrect for frame timeline " << i;
437 if (i == vsyncEventData.preferredFrameTimelineIndex) {
438 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
439 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800440 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
441 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800442 << "Preferred expected vsync timestamp incorrect" << i;
443 }
444 }
445}
446
Lloyd Pique24b0a482018-03-09 18:52:26 -0800447TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
448 // Create a first connection, register it, and request a vsync rate of zero.
449 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700450 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800451 mThread->setVsyncRate(0, firstConnection);
452
453 // By itself, this should not enable vsync events
454 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
455 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
456
457 // However if there is another connection which wants events at a nonzero rate.....
458 ConnectionEventRecorder secondConnectionEventRecorder{0};
459 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700460 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800461 mThread->setVsyncRate(1, secondConnection);
462
Dominik Laskowski029cc122019-01-23 19:52:06 -0800463 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800464 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800465
466 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700467 // the second connection. The first connection should not
Lloyd Pique24b0a482018-03-09 18:52:26 -0800468 // get the event.
Rachel Leef16da3c2022-01-20 13:57:18 -0800469 mCallback->onVSyncEvent(123, {456, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800470 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
471 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
472 1u);
473}
474
475TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
476 mThread->setVsyncRate(1, mConnection);
477
Dominik Laskowski029cc122019-01-23 19:52:06 -0800478 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800479 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800480
481 // Send a vsync event. EventThread should then make a call to the
Huihong Luoab8ffef2022-08-18 13:02:26 -0700482 // throttler, and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800483 mCallback->onVSyncEvent(123, {456, 789});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700484 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800485 expectVsyncEventReceivedByConnection(123, 1u);
486
487 // A second event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800488 mCallback->onVSyncEvent(456, {123, 0});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700489 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800490 expectVsyncEventReceivedByConnection(456, 2u);
491
492 // A third event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800493 mCallback->onVSyncEvent(789, {777, 111});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700494 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800495 expectVsyncEventReceivedByConnection(789, 3u);
496}
497
498TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
499 mThread->setVsyncRate(2, mConnection);
500
Dominik Laskowski029cc122019-01-23 19:52:06 -0800501 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800502 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800503
Huihong Luoab8ffef2022-08-18 13:02:26 -0700504 // The first event will not be seen by the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800505 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800506 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700507 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800508
Huihong Luoab8ffef2022-08-18 13:02:26 -0700509 // The second event will be seen by the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800510 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800511 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700512 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800513
Huihong Luoab8ffef2022-08-18 13:02:26 -0700514 // The third event will not be seen by the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800515 mCallback->onVSyncEvent(789, {777, 744});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800516 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700517 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800518
Huihong Luoab8ffef2022-08-18 13:02:26 -0700519 // The fourth event will be seen by the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800520 mCallback->onVSyncEvent(101112, {7847, 86});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800521 expectVsyncEventReceivedByConnection(101112, 4u);
522}
523
524TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
525 mThread->setVsyncRate(1, mConnection);
526
Dominik Laskowski029cc122019-01-23 19:52:06 -0800527 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800528 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800529
530 // Destroy the only (strong) reference to the connection.
531 mConnection = nullptr;
532
Huihong Luoab8ffef2022-08-18 13:02:26 -0700533 // The first event will not be seen by the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800534 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800535 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
536
537 // EventThread should disable vsync callbacks
538 expectVSyncSetEnabledCallReceived(false);
539}
540
541TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
542 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700543 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800544 mThread->setVsyncRate(1, errorConnection);
545
Dominik Laskowski029cc122019-01-23 19:52:06 -0800546 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800547 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800548
Huihong Luoab8ffef2022-08-18 13:02:26 -0700549 // The first event will be seen by the connection, which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800550 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800551 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
552
Huihong Luoab8ffef2022-08-18 13:02:26 -0700553 // A subsequent event will not be seen by the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800554 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800555 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
556
557 // EventThread should disable vsync callbacks with the second event
558 expectVSyncSetEnabledCallReceived(false);
559}
560
Alec Mouri717bcb62020-02-10 17:07:19 -0800561TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700562 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800563 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700564 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800565 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700566 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800567 ConnectionEventRecorder secondConnectionEventRecorder{0};
568 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700569 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800570 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700571 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800572
573 // EventThread should enable vsync callbacks.
574 expectVSyncSetEnabledCallReceived(true);
575
Huihong Luoab8ffef2022-08-18 13:02:26 -0700576 // The first event will be seen by the connection, which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800577 mCallback->onVSyncEvent(123, {456, 789});
Alec Mouri717bcb62020-02-10 17:07:19 -0800578 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
579 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
580 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700581 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800582}
583
Lloyd Pique24b0a482018-03-09 18:52:26 -0800584TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
585 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700586 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800587 mThread->setVsyncRate(1, errorConnection);
588
Dominik Laskowski029cc122019-01-23 19:52:06 -0800589 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800590 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800591
Huihong Luoab8ffef2022-08-18 13:02:26 -0700592 // The first event will be seen by the connection, which then returns a non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800593 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800594 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
595
Huihong Luoab8ffef2022-08-18 13:02:26 -0700596 // A subsequent event will be seen by the connection, which still then returns a non-fatal
597 // error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800598 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800599 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
600
601 // EventThread will not disable vsync callbacks as the errors are non-fatal.
602 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
603}
604
605TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700606 mThread->setDuration(321ns, 456ns);
607 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800608}
609
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800610TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
611 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
612 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800613}
614
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800615TEST_F(EventThreadTest, postHotplugInternalConnect) {
616 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
617 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618}
619
620TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800621 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
622 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800623}
624
625TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800626 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
627 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800628}
629
Ady Abraham447052e2019-02-13 16:07:27 -0800630TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700631 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
632 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
633 .setId(DisplayModeId(7))
634 .setVsyncPeriod(16666666)
635 .build();
636
637 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700638 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800639}
640
641TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700642 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
643 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
644 .setId(DisplayModeId(5))
645 .setVsyncPeriod(16666666)
646 .build();
647
648 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700649 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800650}
651
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700652TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700653 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
654 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
655 .setId(DisplayModeId(7))
656 .setVsyncPeriod(16666666)
657 .build();
658 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700659 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700660}
661
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700662TEST_F(EventThreadTest, suppressConfigChanged) {
663 ConnectionEventRecorder suppressConnectionEventRecorder{0};
664 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700665 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700666
Ady Abraham690f4612021-07-01 23:24:03 -0700667 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
668 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
669 .setId(DisplayModeId(9))
670 .setVsyncPeriod(16666666)
671 .build();
672
673 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700674 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700675
676 auto args = suppressConnectionEventRecorder.waitForCall();
677 ASSERT_FALSE(args.has_value());
678}
679
Ady Abraham62f216c2020-10-13 19:07:23 -0700680TEST_F(EventThreadTest, postUidFrameRateMapping) {
681 const std::vector<FrameRateOverride> overrides = {
682 {.uid = 1, .frameRateHz = 20},
683 {.uid = 3, .frameRateHz = 40},
684 {.uid = 5, .frameRateHz = 60},
685 };
686
687 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
688 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
689}
690
691TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
692 const std::vector<FrameRateOverride> overrides = {
693 {.uid = 1, .frameRateHz = 20},
694 {.uid = 3, .frameRateHz = 40},
695 {.uid = 5, .frameRateHz = 60},
696 };
697
698 ConnectionEventRecorder suppressConnectionEventRecorder{0};
699 sp<MockEventThreadConnection> suppressConnection =
700 createConnection(suppressConnectionEventRecorder);
701
702 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
703 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
704
705 auto args = suppressConnectionEventRecorder.waitForCall();
706 ASSERT_FALSE(args.has_value());
707}
708
Ady Abraham0bb6a472020-10-12 10:22:13 -0700709TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
710 // Signal that we want the next vsync event to be posted to the throttled connection
711 mThread->requestNextVsync(mThrottledConnection);
712
713 // EventThread should immediately request a resync.
714 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
715
716 // EventThread should enable vsync callbacks.
717 expectVSyncSetEnabledCallReceived(true);
718
719 // Use the received callback to signal a first vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700720 // The throttler should receive the event, but not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800721 mCallback->onVSyncEvent(123, {456, 789});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700722 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
723 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
724
725 // Use the received callback to signal a second vsync event.
Huihong Luoab8ffef2022-08-18 13:02:26 -0700726 // The throttler should receive the event, but the connection should
Ady Abraham0bb6a472020-10-12 10:22:13 -0700727 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800728 mCallback->onVSyncEvent(456, {123, 0});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700729 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
730 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
731
732 // EventThread should not change the vsync state as it didn't send the event
733 // yet
734 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
735}
736
Lloyd Pique24b0a482018-03-09 18:52:26 -0800737} // namespace
738} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100739
740// TODO(b/129481165): remove the #pragma below and fix conversion issues
741#pragma clang diagnostic pop // ignored "-Wextra"