blob: cc0a40f74b307642aad050b29c457a8ae21c32fa [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
Ady Abraham62f216c2020-10-13 19:07:23 -070037using namespace android::flag_operators;
Lloyd Pique24b0a482018-03-09 18:52:26 -080038using testing::_;
39using testing::Invoke;
40
41namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080042
Lloyd Pique24b0a482018-03-09 18:52:26 -080043namespace {
44
Dominik Laskowskif1833852021-03-23 15:06:50 -070045constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
46constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
47constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
48 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
49
Jorim Jaggic0086af2021-02-12 18:18:11 +010050constexpr std::chrono::duration VSYNC_PERIOD(16ms);
Dominik Laskowskif1833852021-03-23 15:06:50 -070051
Lloyd Pique24b0a482018-03-09 18:52:26 -080052class MockVSyncSource : public VSyncSource {
53public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070054 const char* getName() const override { return "test"; }
55
Lloyd Pique24b0a482018-03-09 18:52:26 -080056 MOCK_METHOD1(setVSyncEnabled, void(bool));
57 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070058 MOCK_METHOD2(setDuration,
59 void(std::chrono::nanoseconds workDuration,
60 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080061 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Rachel Leeef2e21f2022-02-01 14:51:34 -080062 MOCK_METHOD(VSyncSource::VSyncData, getLatestVSyncData, (), (const, override));
Ady Abraham5e7371c2020-03-24 14:47:24 -070063 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080064};
65
66} // namespace
67
68class EventThreadTest : public testing::Test {
69protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080070 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080071 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070072 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
73 ResyncCallback&& resyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070074 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070075 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070076 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080077 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
78 };
79
80 using ConnectionEventRecorder =
81 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
82
83 EventThreadTest();
84 ~EventThreadTest() override;
85
Dominik Laskowski6505f792019-09-18 11:10:05 -070086 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham62f216c2020-10-13 19:07:23 -070087 sp<MockEventThreadConnection> createConnection(
88 ConnectionEventRecorder& recorder,
89 ISurfaceComposer::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,
102 nsecs_t preferredDeadline);
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));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700151 mConnection = createConnection(mConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100152 ISurfaceComposer::EventRegistration::modeChanged |
Ady Abraham62f216c2020-10-13 19:07:23 -0700153 ISurfaceComposer::EventRegistration::frameRateOverride);
154 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100155 ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700156 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800157
158 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800159 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
160 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800161}
162
163EventThreadTest::~EventThreadTest() {
164 const ::testing::TestInfo* const test_info =
165 ::testing::UnitTest::GetInstance()->current_test_info();
166 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800167
168 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800169 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800170}
171
Dominik Laskowski6505f792019-09-18 11:10:05 -0700172void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700173 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
174 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
175 return (uid == mThrottledConnectionUid);
176 };
Jorim Jaggic0086af2021-02-12 18:18:11 +0100177 const auto getVsyncPeriod = [](uid_t uid) {
178 return VSYNC_PERIOD.count();
179 };
Ady Abraham0bb6a472020-10-12 10:22:13 -0700180
Rachel Lee3f028662021-11-04 19:32:24 +0000181 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
182 mThread = std::make_unique<impl::EventThread>(std::move(source), mTokenManager.get(),
Ady Abraham0bb6a472020-10-12 10:22:13 -0700183 mInterceptVSyncCallRecorder.getInvocable(),
Jorim Jaggic0086af2021-02-12 18:18:11 +0100184 throttleVsync, getVsyncPeriod);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800185
186 // EventThread should register itself as VSyncSource callback.
187 mCallback = expectVSyncSetCallbackCallReceived();
188 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800189}
190
191sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700192 ConnectionEventRecorder& recorder,
193 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800194 sp<MockEventThreadConnection> connection =
Ady Abraham0bb6a472020-10-12 10:22:13 -0700195 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700196 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800197 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
198 return connection;
199}
200
201void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
202 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
203 ASSERT_TRUE(args.has_value());
204 EXPECT_EQ(expectedState, std::get<0>(args.value()));
205}
206
Ady Abraham9c53ee72020-07-22 21:16:18 -0700207void EventThreadTest::expectVSyncSetDurationCallReceived(
208 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
209 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800210 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700211 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
212 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800213}
214
215VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
216 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
217 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
218}
219
220void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
221 auto args = mInterceptVSyncCallRecorder.waitForCall();
222 ASSERT_TRUE(args.has_value());
223 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
224}
225
Ady Abraham0bb6a472020-10-12 10:22:13 -0700226void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
227 auto args = mThrottleVsyncCallRecorder.waitForCall();
228 ASSERT_TRUE(args.has_value());
229 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
230 EXPECT_EQ(uid, std::get<1>(args.value()));
231}
232
Lloyd Pique24b0a482018-03-09 18:52:26 -0800233void EventThreadTest::expectVsyncEventReceivedByConnection(
234 const char* name, ConnectionEventRecorder& connectionEventRecorder,
235 nsecs_t expectedTimestamp, unsigned expectedCount) {
236 auto args = connectionEventRecorder.waitForCall();
237 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
238 << expectedTimestamp;
239 const auto& event = std::get<0>(args.value());
240 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
241 << name << " did not get the correct event for timestamp " << expectedTimestamp;
242 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
243 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
244 EXPECT_EQ(expectedCount, event.vsync.count)
245 << name << " did not get the expected count for timestamp " << expectedTimestamp;
246}
247
248void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
249 unsigned expectedCount) {
250 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
251 mConnectionEventCallRecorder, expectedTimestamp,
252 expectedCount);
253}
254
Rachel Lee3f028662021-11-04 19:32:24 +0000255void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp,
256 nsecs_t preferredDeadline) {
257 auto args = mConnectionEventCallRecorder.waitForCall();
258 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
259 << expectedTimestamp;
260 const auto& event = std::get<0>(args.value());
Rachel Leeef2e21f2022-02-01 14:51:34 -0800261 for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
Rachel Lee8d0c6102021-11-03 22:00:25 +0000262 auto prediction =
263 mTokenManager->getPredictionsForToken(event.vsync.frameTimelines[i].vsyncId);
264 EXPECT_TRUE(prediction.has_value());
265 EXPECT_EQ(prediction.value().endTime, event.vsync.frameTimelines[i].deadlineTimestamp)
266 << "Deadline timestamp does not match cached value";
267 EXPECT_EQ(prediction.value().presentTime,
268 event.vsync.frameTimelines[i].expectedVSyncTimestamp)
269 << "Expected vsync timestamp does not match cached value";
270
Rachel Lee3f028662021-11-04 19:32:24 +0000271 if (i > 0) {
272 EXPECT_GT(event.vsync.frameTimelines[i].deadlineTimestamp,
273 event.vsync.frameTimelines[i - 1].deadlineTimestamp)
274 << "Deadline timestamp out of order for frame timeline " << i;
275 EXPECT_GT(event.vsync.frameTimelines[i].expectedVSyncTimestamp,
276 event.vsync.frameTimelines[i - 1].expectedVSyncTimestamp)
277 << "Expected vsync timestamp out of order for frame timeline " << i;
278 }
Rachel Lee0d943202022-02-01 23:29:41 -0800279
280 // Vsync ID order lines up with registration into test token manager.
281 EXPECT_EQ(i, event.vsync.frameTimelines[i].vsyncId)
282 << "Vsync ID incorrect for frame timeline " << i;
283 if (i == event.vsync.preferredFrameTimelineIndex) {
284 EXPECT_EQ(event.vsync.frameTimelines[i].deadlineTimestamp, preferredDeadline)
285 << "Preferred deadline timestamp incorrect" << i;
286 EXPECT_EQ(event.vsync.frameTimelines[i].expectedVSyncTimestamp,
287 event.vsync.expectedVSyncTimestamp)
288 << "Preferred expected vsync timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000289 }
290 }
291}
292
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800293void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
294 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800295 auto args = mConnectionEventCallRecorder.waitForCall();
296 ASSERT_TRUE(args.has_value());
297 const auto& event = std::get<0>(args.value());
298 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800299 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800300 EXPECT_EQ(expectedConnected, event.hotplug.connected);
301}
302
Ady Abraham447052e2019-02-13 16:07:27 -0800303void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700304 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
305 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800306 auto args = mConnectionEventCallRecorder.waitForCall();
307 ASSERT_TRUE(args.has_value());
308 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100309 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800310 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100311 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
312 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800313}
314
Ady Abraham62f216c2020-10-13 19:07:23 -0700315void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
316 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
317 for (const auto [uid, frameRateHz] : expectedOverrides) {
318 auto args = mConnectionEventCallRecorder.waitForCall();
319 ASSERT_TRUE(args.has_value());
320 const auto& event = std::get<0>(args.value());
321 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
322 EXPECT_EQ(expectedDisplayId, event.header.displayId);
323 EXPECT_EQ(uid, event.frameRateOverride.uid);
324 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
325 }
326
327 auto args = mConnectionEventCallRecorder.waitForCall();
328 ASSERT_TRUE(args.has_value());
329 const auto& event = std::get<0>(args.value());
330 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
331 EXPECT_EQ(expectedDisplayId, event.header.displayId);
332}
333
Lloyd Pique24b0a482018-03-09 18:52:26 -0800334namespace {
335
Rachel Leeef2e21f2022-02-01 14:51:34 -0800336using namespace testing;
337
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338/* ------------------------------------------------------------------------
339 * Test cases
340 */
341
342TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
343 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
344 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700345 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800346 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
347 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
348 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
349}
350
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800351TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800352 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
353 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800354
355 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700356 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800357
358 // EventThread should not enable vsync callbacks.
359 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800360}
361
Lloyd Pique24b0a482018-03-09 18:52:26 -0800362TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
363 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700364 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365
Ady Abraham8532d012019-05-08 14:50:56 -0700366 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800367 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
368
Dominik Laskowski029cc122019-01-23 19:52:06 -0800369 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800371
372 // Use the received callback to signal a first vsync event.
373 // The interceptor should receive the event, as well as the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800374 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800375 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700376 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377 expectVsyncEventReceivedByConnection(123, 1u);
378
379 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700380 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800382 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700384 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
386
387 // EventThread should also detect that at this point that it does not need
388 // any more vsync events, and should disable their generation.
389 expectVSyncSetEnabledCallReceived(false);
390}
391
Rachel Lee3f028662021-11-04 19:32:24 +0000392TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
393 // Signal that we want the next vsync event to be posted to the connection
394 mThread->requestNextVsync(mConnection);
395
396 expectVSyncSetEnabledCallReceived(true);
397
398 // Use the received callback to signal a vsync event.
399 // The interceptor should receive the event, as well as the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800400 mCallback->onVSyncEvent(123, {456, 789});
Rachel Lee3f028662021-11-04 19:32:24 +0000401 expectInterceptCallReceived(123);
402 expectVsyncEventFrameTimelinesCorrect(123, 789);
403}
404
Rachel Leeef2e21f2022-02-01 14:51:34 -0800405TEST_F(EventThreadTest, getLatestVsyncEventData) {
406 const nsecs_t now = systemTime();
407 const nsecs_t preferredDeadline = now + 10000000;
408 const nsecs_t preferredExpectedVSyncTimestamp = now + 20000000;
409 const VSyncSource::VSyncData preferredData = {preferredExpectedVSyncTimestamp,
410 preferredDeadline};
411 EXPECT_CALL(*mVSyncSource, getLatestVSyncData()).WillOnce(Return(preferredData));
412
413 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
414 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
415 << "Deadline timestamp should be greater than frame time";
416 for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
417 auto prediction =
418 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].id);
419 EXPECT_TRUE(prediction.has_value());
420 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
421 << "Deadline timestamp does not match cached value";
422 EXPECT_EQ(prediction.value().presentTime,
423 vsyncEventData.frameTimelines[i].expectedPresentTime)
424 << "Expected vsync timestamp does not match cached value";
425 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentTime,
426 vsyncEventData.frameTimelines[i].deadlineTimestamp)
427 << "Expected vsync timestamp should be greater than deadline";
428
429 if (i > 0) {
430 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
431 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
432 << "Deadline timestamp out of order for frame timeline " << i;
433 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentTime,
434 vsyncEventData.frameTimelines[i - 1].expectedPresentTime)
435 << "Expected vsync timestamp out of order for frame timeline " << i;
436 }
437
438 // Vsync ID order lines up with registration into test token manager.
439 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].id)
440 << "Vsync ID incorrect for frame timeline " << i;
441 if (i == vsyncEventData.preferredFrameTimelineIndex) {
442 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
443 << "Preferred deadline timestamp incorrect" << i;
444 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentTime,
445 preferredExpectedVSyncTimestamp)
446 << "Preferred expected vsync timestamp incorrect" << i;
447 }
448 }
449}
450
Lloyd Pique24b0a482018-03-09 18:52:26 -0800451TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
452 // Create a first connection, register it, and request a vsync rate of zero.
453 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700454 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800455 mThread->setVsyncRate(0, firstConnection);
456
457 // By itself, this should not enable vsync events
458 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
459 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
460
461 // However if there is another connection which wants events at a nonzero rate.....
462 ConnectionEventRecorder secondConnectionEventRecorder{0};
463 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700464 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800465 mThread->setVsyncRate(1, secondConnection);
466
Dominik Laskowski029cc122019-01-23 19:52:06 -0800467 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800468 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800469
470 // Send a vsync event. EventThread should then make a call to the
471 // interceptor, and the second connection. The first connection should not
472 // get the event.
Rachel Leef16da3c2022-01-20 13:57:18 -0800473 mCallback->onVSyncEvent(123, {456, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800474 expectInterceptCallReceived(123);
475 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
476 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
477 1u);
478}
479
480TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
481 mThread->setVsyncRate(1, mConnection);
482
Dominik Laskowski029cc122019-01-23 19:52:06 -0800483 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800484 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800485
486 // Send a vsync event. EventThread should then make a call to the
487 // interceptor, and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800488 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800489 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700490 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800491 expectVsyncEventReceivedByConnection(123, 1u);
492
493 // A second event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800494 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800495 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700496 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800497 expectVsyncEventReceivedByConnection(456, 2u);
498
499 // A third event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800500 mCallback->onVSyncEvent(789, {777, 111});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800501 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700502 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800503 expectVsyncEventReceivedByConnection(789, 3u);
504}
505
506TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
507 mThread->setVsyncRate(2, mConnection);
508
Dominik Laskowski029cc122019-01-23 19:52:06 -0800509 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800510 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800511
512 // The first event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800513 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800514 expectInterceptCallReceived(123);
515 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700516 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800517
518 // The second event will be seen by the interceptor and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800519 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800520 expectInterceptCallReceived(456);
521 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700522 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523
524 // The third event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800525 mCallback->onVSyncEvent(789, {777, 744});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800526 expectInterceptCallReceived(789);
527 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700528 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800529
530 // The fourth event will be seen by the interceptor and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800531 mCallback->onVSyncEvent(101112, {7847, 86});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800532 expectInterceptCallReceived(101112);
533 expectVsyncEventReceivedByConnection(101112, 4u);
534}
535
536TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
537 mThread->setVsyncRate(1, mConnection);
538
Dominik Laskowski029cc122019-01-23 19:52:06 -0800539 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800540 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800541
542 // Destroy the only (strong) reference to the connection.
543 mConnection = nullptr;
544
545 // The first event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800546 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800547 expectInterceptCallReceived(123);
548 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
549
550 // EventThread should disable vsync callbacks
551 expectVSyncSetEnabledCallReceived(false);
552}
553
554TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
555 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700556 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800557 mThread->setVsyncRate(1, errorConnection);
558
Dominik Laskowski029cc122019-01-23 19:52:06 -0800559 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800560 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561
562 // The first event will be seen by the interceptor, and by the connection,
563 // which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800564 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800565 expectInterceptCallReceived(123);
566 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
567
568 // A subsequent event will be seen by the interceptor and not by the
569 // connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800570 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800571 expectInterceptCallReceived(456);
572 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
573
574 // EventThread should disable vsync callbacks with the second event
575 expectVSyncSetEnabledCallReceived(false);
576}
577
Alec Mouri717bcb62020-02-10 17:07:19 -0800578TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700579 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800580 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700581 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800582 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700583 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800584 ConnectionEventRecorder secondConnectionEventRecorder{0};
585 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700586 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800587 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700588 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800589
590 // EventThread should enable vsync callbacks.
591 expectVSyncSetEnabledCallReceived(true);
592
593 // The first event will be seen by the interceptor, and by the connection,
594 // which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800595 mCallback->onVSyncEvent(123, {456, 789});
Alec Mouri717bcb62020-02-10 17:07:19 -0800596 expectInterceptCallReceived(123);
597 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
598 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
599 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700600 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800601}
602
Lloyd Pique24b0a482018-03-09 18:52:26 -0800603TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
604 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700605 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800606 mThread->setVsyncRate(1, errorConnection);
607
Dominik Laskowski029cc122019-01-23 19:52:06 -0800608 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800609 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800610
611 // The first event will be seen by the interceptor, and by the connection,
612 // which then returns an non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800613 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800614 expectInterceptCallReceived(123);
615 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
616
617 // A subsequent event will be seen by the interceptor, and by the connection,
618 // which still then returns an non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800619 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800620 expectInterceptCallReceived(456);
621 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
622
623 // EventThread will not disable vsync callbacks as the errors are non-fatal.
624 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
625}
626
627TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700628 mThread->setDuration(321ns, 456ns);
629 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800630}
631
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800632TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
633 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
634 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800635}
636
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800637TEST_F(EventThreadTest, postHotplugInternalConnect) {
638 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
639 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800640}
641
642TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800643 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
644 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800645}
646
647TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800648 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
649 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800650}
651
Ady Abraham447052e2019-02-13 16:07:27 -0800652TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700653 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
654 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
655 .setId(DisplayModeId(7))
656 .setVsyncPeriod(16666666)
657 .build();
658
659 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700660 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800661}
662
663TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700664 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
665 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
666 .setId(DisplayModeId(5))
667 .setVsyncPeriod(16666666)
668 .build();
669
670 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700671 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800672}
673
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700674TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700675 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
676 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
677 .setId(DisplayModeId(7))
678 .setVsyncPeriod(16666666)
679 .build();
680 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700681 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700682}
683
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700684TEST_F(EventThreadTest, suppressConfigChanged) {
685 ConnectionEventRecorder suppressConnectionEventRecorder{0};
686 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700687 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700688
Ady Abraham690f4612021-07-01 23:24:03 -0700689 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
690 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
691 .setId(DisplayModeId(9))
692 .setVsyncPeriod(16666666)
693 .build();
694
695 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700696 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700697
698 auto args = suppressConnectionEventRecorder.waitForCall();
699 ASSERT_FALSE(args.has_value());
700}
701
Ady Abraham62f216c2020-10-13 19:07:23 -0700702TEST_F(EventThreadTest, postUidFrameRateMapping) {
703 const std::vector<FrameRateOverride> overrides = {
704 {.uid = 1, .frameRateHz = 20},
705 {.uid = 3, .frameRateHz = 40},
706 {.uid = 5, .frameRateHz = 60},
707 };
708
709 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
710 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
711}
712
713TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
714 const std::vector<FrameRateOverride> overrides = {
715 {.uid = 1, .frameRateHz = 20},
716 {.uid = 3, .frameRateHz = 40},
717 {.uid = 5, .frameRateHz = 60},
718 };
719
720 ConnectionEventRecorder suppressConnectionEventRecorder{0};
721 sp<MockEventThreadConnection> suppressConnection =
722 createConnection(suppressConnectionEventRecorder);
723
724 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
725 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
726
727 auto args = suppressConnectionEventRecorder.waitForCall();
728 ASSERT_FALSE(args.has_value());
729}
730
Ady Abraham0bb6a472020-10-12 10:22:13 -0700731TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
732 // Signal that we want the next vsync event to be posted to the throttled connection
733 mThread->requestNextVsync(mThrottledConnection);
734
735 // EventThread should immediately request a resync.
736 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
737
738 // EventThread should enable vsync callbacks.
739 expectVSyncSetEnabledCallReceived(true);
740
741 // Use the received callback to signal a first vsync event.
742 // The interceptor should receive the event, but not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800743 mCallback->onVSyncEvent(123, {456, 789});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700744 expectInterceptCallReceived(123);
745 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
746 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
747
748 // Use the received callback to signal a second vsync event.
749 // The interceptor should receive the event, but the connection should
750 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800751 mCallback->onVSyncEvent(456, {123, 0});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700752 expectInterceptCallReceived(456);
753 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
754 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
755
756 // EventThread should not change the vsync state as it didn't send the event
757 // yet
758 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
759}
760
Lloyd Pique24b0a482018-03-09 18:52:26 -0800761} // namespace
762} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100763
764// TODO(b/129481165): remove the #pragma below and fix conversion issues
765#pragma clang diagnostic pop // ignored "-Wextra"