blob: 7ee04b046609230ac4f0d7ee3cb4a62277de7317 [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 Abraham0bb6a472020-10-12 10:22:13 -0700196 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700197 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800198 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
199 return connection;
200}
201
202void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
203 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
204 ASSERT_TRUE(args.has_value());
205 EXPECT_EQ(expectedState, std::get<0>(args.value()));
206}
207
Ady Abraham9c53ee72020-07-22 21:16:18 -0700208void EventThreadTest::expectVSyncSetDurationCallReceived(
209 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
210 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800211 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700212 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
213 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800214}
215
216VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
217 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
218 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
219}
220
221void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
222 auto args = mInterceptVSyncCallRecorder.waitForCall();
223 ASSERT_TRUE(args.has_value());
224 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
225}
226
Ady Abraham0bb6a472020-10-12 10:22:13 -0700227void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
228 auto args = mThrottleVsyncCallRecorder.waitForCall();
229 ASSERT_TRUE(args.has_value());
230 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
231 EXPECT_EQ(uid, std::get<1>(args.value()));
232}
233
Lloyd Pique24b0a482018-03-09 18:52:26 -0800234void EventThreadTest::expectVsyncEventReceivedByConnection(
235 const char* name, ConnectionEventRecorder& connectionEventRecorder,
236 nsecs_t expectedTimestamp, unsigned expectedCount) {
237 auto args = connectionEventRecorder.waitForCall();
238 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
239 << expectedTimestamp;
240 const auto& event = std::get<0>(args.value());
241 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
242 << name << " did not get the correct event for timestamp " << expectedTimestamp;
243 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
244 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
245 EXPECT_EQ(expectedCount, event.vsync.count)
246 << name << " did not get the expected count for timestamp " << expectedTimestamp;
247}
248
249void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
250 unsigned expectedCount) {
251 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
252 mConnectionEventCallRecorder, expectedTimestamp,
253 expectedCount);
254}
255
Rachel Leeb9c5a772022-02-04 21:17:37 -0800256void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(
257 nsecs_t expectedTimestamp, VSyncSource::VSyncData preferredVsyncData) {
Rachel Lee3f028662021-11-04 19:32:24 +0000258 auto args = mConnectionEventCallRecorder.waitForCall();
259 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
260 << expectedTimestamp;
261 const auto& event = std::get<0>(args.value());
Rachel Leeef2e21f2022-02-01 14:51:34 -0800262 for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800263 auto prediction = mTokenManager->getPredictionsForToken(
264 event.vsync.vsyncData.frameTimelines[i].vsyncId);
Rachel Lee8d0c6102021-11-03 22:00:25 +0000265 EXPECT_TRUE(prediction.has_value());
Rachel Leeb9c5a772022-02-04 21:17:37 -0800266 EXPECT_EQ(prediction.value().endTime,
267 event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp)
Rachel Lee8d0c6102021-11-03 22:00:25 +0000268 << "Deadline timestamp does not match cached value";
269 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800270 event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime)
271 << "Expected vsync.vsyncData timestamp does not match cached value";
Rachel Lee8d0c6102021-11-03 22:00:25 +0000272
Rachel Lee3f028662021-11-04 19:32:24 +0000273 if (i > 0) {
Rachel Leeb9c5a772022-02-04 21:17:37 -0800274 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
275 event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp)
Rachel Lee3f028662021-11-04 19:32:24 +0000276 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800277 EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
278 event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime)
279 << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000280 }
Rachel Lee0d943202022-02-01 23:29:41 -0800281
282 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800283 EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId)
Rachel Lee0d943202022-02-01 23:29:41 -0800284 << "Vsync ID incorrect for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800285 if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) {
286 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp,
287 preferredVsyncData.deadlineTimestamp)
Rachel Lee0d943202022-02-01 23:29:41 -0800288 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800289 EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime,
290 preferredVsyncData.expectedPresentationTime)
291 << "Preferred expected vsync.vsyncData timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000292 }
293 }
294}
295
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800296void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
297 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800298 auto args = mConnectionEventCallRecorder.waitForCall();
299 ASSERT_TRUE(args.has_value());
300 const auto& event = std::get<0>(args.value());
301 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800302 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800303 EXPECT_EQ(expectedConnected, event.hotplug.connected);
304}
305
Ady Abraham447052e2019-02-13 16:07:27 -0800306void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700307 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
308 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800309 auto args = mConnectionEventCallRecorder.waitForCall();
310 ASSERT_TRUE(args.has_value());
311 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100312 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800313 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100314 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
315 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800316}
317
Ady Abraham62f216c2020-10-13 19:07:23 -0700318void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
319 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
320 for (const auto [uid, frameRateHz] : expectedOverrides) {
321 auto args = mConnectionEventCallRecorder.waitForCall();
322 ASSERT_TRUE(args.has_value());
323 const auto& event = std::get<0>(args.value());
324 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
325 EXPECT_EQ(expectedDisplayId, event.header.displayId);
326 EXPECT_EQ(uid, event.frameRateOverride.uid);
327 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
328 }
329
330 auto args = mConnectionEventCallRecorder.waitForCall();
331 ASSERT_TRUE(args.has_value());
332 const auto& event = std::get<0>(args.value());
333 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
334 EXPECT_EQ(expectedDisplayId, event.header.displayId);
335}
336
Lloyd Pique24b0a482018-03-09 18:52:26 -0800337namespace {
338
Rachel Leeef2e21f2022-02-01 14:51:34 -0800339using namespace testing;
340
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341/* ------------------------------------------------------------------------
342 * Test cases
343 */
344
345TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
346 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
347 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700348 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800349 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
350 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
351 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
352}
353
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800354TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800355 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
356 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800357
358 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700359 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800360
361 // EventThread should not enable vsync callbacks.
362 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800363}
364
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
366 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700367 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368
Ady Abraham8532d012019-05-08 14:50:56 -0700369 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
371
Dominik Laskowski029cc122019-01-23 19:52:06 -0800372 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800373 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374
375 // Use the received callback to signal a first vsync event.
376 // The interceptor should receive the event, as well as the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800377 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800378 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700379 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800380 expectVsyncEventReceivedByConnection(123, 1u);
381
382 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700383 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800384 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800385 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700387 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800388 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
389
390 // EventThread should also detect that at this point that it does not need
391 // any more vsync events, and should disable their generation.
392 expectVSyncSetEnabledCallReceived(false);
393}
394
Rachel Lee3f028662021-11-04 19:32:24 +0000395TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
396 // Signal that we want the next vsync event to be posted to the connection
397 mThread->requestNextVsync(mConnection);
398
399 expectVSyncSetEnabledCallReceived(true);
400
401 // Use the received callback to signal a vsync event.
402 // The interceptor should receive the event, as well as the connection.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800403 VSyncSource::VSyncData vsyncData = {456, 789};
404 mCallback->onVSyncEvent(123, vsyncData);
Rachel Lee3f028662021-11-04 19:32:24 +0000405 expectInterceptCallReceived(123);
Rachel Leeb9c5a772022-02-04 21:17:37 -0800406 expectVsyncEventFrameTimelinesCorrect(123, vsyncData);
Rachel Lee3f028662021-11-04 19:32:24 +0000407}
408
Rachel Leeef2e21f2022-02-01 14:51:34 -0800409TEST_F(EventThreadTest, getLatestVsyncEventData) {
410 const nsecs_t now = systemTime();
411 const nsecs_t preferredDeadline = now + 10000000;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800412 const nsecs_t preferredExpectedPresentationTime = now + 20000000;
413 const VSyncSource::VSyncData preferredData = {preferredExpectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800414 preferredDeadline};
415 EXPECT_CALL(*mVSyncSource, getLatestVSyncData()).WillOnce(Return(preferredData));
416
417 VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection);
Rachel Leeb5223cf2022-03-14 14:39:27 -0700418
419 // Check EventThread immediately requested a resync.
420 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
421
Rachel Leeef2e21f2022-02-01 14:51:34 -0800422 EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now)
423 << "Deadline timestamp should be greater than frame time";
424 for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
425 auto prediction =
Rachel Leeb9c5a772022-02-04 21:17:37 -0800426 mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId);
Rachel Leeef2e21f2022-02-01 14:51:34 -0800427 EXPECT_TRUE(prediction.has_value());
428 EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp)
429 << "Deadline timestamp does not match cached value";
430 EXPECT_EQ(prediction.value().presentTime,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800431 vsyncEventData.frameTimelines[i].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800432 << "Expected vsync timestamp does not match cached value";
Rachel Leeb9c5a772022-02-04 21:17:37 -0800433 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
Rachel Leeef2e21f2022-02-01 14:51:34 -0800434 vsyncEventData.frameTimelines[i].deadlineTimestamp)
435 << "Expected vsync timestamp should be greater than deadline";
436
437 if (i > 0) {
438 EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp,
439 vsyncEventData.frameTimelines[i - 1].deadlineTimestamp)
440 << "Deadline timestamp out of order for frame timeline " << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800441 EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime,
442 vsyncEventData.frameTimelines[i - 1].expectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800443 << "Expected vsync timestamp out of order for frame timeline " << i;
444 }
445
446 // Vsync ID order lines up with registration into test token manager.
Rachel Leeb9c5a772022-02-04 21:17:37 -0800447 EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800448 << "Vsync ID incorrect for frame timeline " << i;
449 if (i == vsyncEventData.preferredFrameTimelineIndex) {
450 EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline)
451 << "Preferred deadline timestamp incorrect" << i;
Rachel Leeb9c5a772022-02-04 21:17:37 -0800452 EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime,
453 preferredExpectedPresentationTime)
Rachel Leeef2e21f2022-02-01 14:51:34 -0800454 << "Preferred expected vsync timestamp incorrect" << i;
455 }
456 }
457}
458
Lloyd Pique24b0a482018-03-09 18:52:26 -0800459TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
460 // Create a first connection, register it, and request a vsync rate of zero.
461 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700462 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800463 mThread->setVsyncRate(0, firstConnection);
464
465 // By itself, this should not enable vsync events
466 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
467 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
468
469 // However if there is another connection which wants events at a nonzero rate.....
470 ConnectionEventRecorder secondConnectionEventRecorder{0};
471 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700472 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800473 mThread->setVsyncRate(1, secondConnection);
474
Dominik Laskowski029cc122019-01-23 19:52:06 -0800475 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800476 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800477
478 // Send a vsync event. EventThread should then make a call to the
479 // interceptor, and the second connection. The first connection should not
480 // get the event.
Rachel Leef16da3c2022-01-20 13:57:18 -0800481 mCallback->onVSyncEvent(123, {456, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800482 expectInterceptCallReceived(123);
483 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
484 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
485 1u);
486}
487
488TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
489 mThread->setVsyncRate(1, mConnection);
490
Dominik Laskowski029cc122019-01-23 19:52:06 -0800491 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800492 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800493
494 // Send a vsync event. EventThread should then make a call to the
495 // interceptor, and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800496 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800497 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700498 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800499 expectVsyncEventReceivedByConnection(123, 1u);
500
501 // A second event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800502 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800503 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700504 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800505 expectVsyncEventReceivedByConnection(456, 2u);
506
507 // A third event should go to the same places.
Rachel Leef16da3c2022-01-20 13:57:18 -0800508 mCallback->onVSyncEvent(789, {777, 111});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800509 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700510 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800511 expectVsyncEventReceivedByConnection(789, 3u);
512}
513
514TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
515 mThread->setVsyncRate(2, mConnection);
516
Dominik Laskowski029cc122019-01-23 19:52:06 -0800517 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800518 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800519
520 // The first event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800521 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800522 expectInterceptCallReceived(123);
523 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700524 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800525
526 // The second event will be seen by the interceptor and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800527 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800528 expectInterceptCallReceived(456);
529 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700530 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800531
532 // The third event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800533 mCallback->onVSyncEvent(789, {777, 744});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800534 expectInterceptCallReceived(789);
535 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700536 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800537
538 // The fourth event will be seen by the interceptor and the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800539 mCallback->onVSyncEvent(101112, {7847, 86});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800540 expectInterceptCallReceived(101112);
541 expectVsyncEventReceivedByConnection(101112, 4u);
542}
543
544TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
545 mThread->setVsyncRate(1, mConnection);
546
Dominik Laskowski029cc122019-01-23 19:52:06 -0800547 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800548 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800549
550 // Destroy the only (strong) reference to the connection.
551 mConnection = nullptr;
552
553 // The first event will be seen by the interceptor, and not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800554 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800555 expectInterceptCallReceived(123);
556 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
557
558 // EventThread should disable vsync callbacks
559 expectVSyncSetEnabledCallReceived(false);
560}
561
562TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
563 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700564 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800565 mThread->setVsyncRate(1, errorConnection);
566
Dominik Laskowski029cc122019-01-23 19:52:06 -0800567 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800568 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800569
570 // The first event will be seen by the interceptor, and by the connection,
571 // which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800572 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800573 expectInterceptCallReceived(123);
574 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
575
576 // A subsequent event will be seen by the interceptor and not by the
577 // connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800578 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800579 expectInterceptCallReceived(456);
580 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
581
582 // EventThread should disable vsync callbacks with the second event
583 expectVSyncSetEnabledCallReceived(false);
584}
585
Alec Mouri717bcb62020-02-10 17:07:19 -0800586TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700587 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800588 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700589 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800590 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700591 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800592 ConnectionEventRecorder secondConnectionEventRecorder{0};
593 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700594 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800595 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700596 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800597
598 // EventThread should enable vsync callbacks.
599 expectVSyncSetEnabledCallReceived(true);
600
601 // The first event will be seen by the interceptor, and by the connection,
602 // which then returns an error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800603 mCallback->onVSyncEvent(123, {456, 789});
Alec Mouri717bcb62020-02-10 17:07:19 -0800604 expectInterceptCallReceived(123);
605 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
606 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
607 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700608 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800609}
610
Lloyd Pique24b0a482018-03-09 18:52:26 -0800611TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
612 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700613 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800614 mThread->setVsyncRate(1, errorConnection);
615
Dominik Laskowski029cc122019-01-23 19:52:06 -0800616 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800617 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800618
619 // The first event will be seen by the interceptor, and by the connection,
620 // which then returns an non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800621 mCallback->onVSyncEvent(123, {456, 789});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800622 expectInterceptCallReceived(123);
623 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
624
625 // A subsequent event will be seen by the interceptor, and by the connection,
626 // which still then returns an non-fatal error.
Rachel Leef16da3c2022-01-20 13:57:18 -0800627 mCallback->onVSyncEvent(456, {123, 0});
Lloyd Pique24b0a482018-03-09 18:52:26 -0800628 expectInterceptCallReceived(456);
629 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
630
631 // EventThread will not disable vsync callbacks as the errors are non-fatal.
632 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
633}
634
635TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700636 mThread->setDuration(321ns, 456ns);
637 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800638}
639
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800640TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
641 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
642 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800643}
644
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800645TEST_F(EventThreadTest, postHotplugInternalConnect) {
646 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
647 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800648}
649
650TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800651 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
652 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800653}
654
655TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800656 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
657 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800658}
659
Ady Abraham447052e2019-02-13 16:07:27 -0800660TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700661 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
662 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
663 .setId(DisplayModeId(7))
664 .setVsyncPeriod(16666666)
665 .build();
666
667 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700668 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800669}
670
671TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700672 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
673 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
674 .setId(DisplayModeId(5))
675 .setVsyncPeriod(16666666)
676 .build();
677
678 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700679 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800680}
681
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700682TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700683 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
684 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
685 .setId(DisplayModeId(7))
686 .setVsyncPeriod(16666666)
687 .build();
688 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700689 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700690}
691
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700692TEST_F(EventThreadTest, suppressConfigChanged) {
693 ConnectionEventRecorder suppressConnectionEventRecorder{0};
694 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700695 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700696
Ady Abraham690f4612021-07-01 23:24:03 -0700697 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
698 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
699 .setId(DisplayModeId(9))
700 .setVsyncPeriod(16666666)
701 .build();
702
703 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700704 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700705
706 auto args = suppressConnectionEventRecorder.waitForCall();
707 ASSERT_FALSE(args.has_value());
708}
709
Ady Abraham62f216c2020-10-13 19:07:23 -0700710TEST_F(EventThreadTest, postUidFrameRateMapping) {
711 const std::vector<FrameRateOverride> overrides = {
712 {.uid = 1, .frameRateHz = 20},
713 {.uid = 3, .frameRateHz = 40},
714 {.uid = 5, .frameRateHz = 60},
715 };
716
717 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
718 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
719}
720
721TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
722 const std::vector<FrameRateOverride> overrides = {
723 {.uid = 1, .frameRateHz = 20},
724 {.uid = 3, .frameRateHz = 40},
725 {.uid = 5, .frameRateHz = 60},
726 };
727
728 ConnectionEventRecorder suppressConnectionEventRecorder{0};
729 sp<MockEventThreadConnection> suppressConnection =
730 createConnection(suppressConnectionEventRecorder);
731
732 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
733 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
734
735 auto args = suppressConnectionEventRecorder.waitForCall();
736 ASSERT_FALSE(args.has_value());
737}
738
Ady Abraham0bb6a472020-10-12 10:22:13 -0700739TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
740 // Signal that we want the next vsync event to be posted to the throttled connection
741 mThread->requestNextVsync(mThrottledConnection);
742
743 // EventThread should immediately request a resync.
744 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
745
746 // EventThread should enable vsync callbacks.
747 expectVSyncSetEnabledCallReceived(true);
748
749 // Use the received callback to signal a first vsync event.
750 // The interceptor should receive the event, but not the connection.
Rachel Leef16da3c2022-01-20 13:57:18 -0800751 mCallback->onVSyncEvent(123, {456, 789});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700752 expectInterceptCallReceived(123);
753 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
754 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
755
756 // Use the received callback to signal a second vsync event.
757 // The interceptor should receive the event, but the connection should
758 // not as it was only interested in the first.
Rachel Leef16da3c2022-01-20 13:57:18 -0800759 mCallback->onVSyncEvent(456, {123, 0});
Ady Abraham0bb6a472020-10-12 10:22:13 -0700760 expectInterceptCallReceived(456);
761 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
762 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
763
764 // EventThread should not change the vsync state as it didn't send the event
765 // yet
766 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
767}
768
Lloyd Pique24b0a482018-03-09 18:52:26 -0800769} // namespace
770} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100771
772// TODO(b/129481165): remove the #pragma below and fix conversion issues
773#pragma clang diagnostic pop // ignored "-Wextra"