blob: 57cb5cf441885d412b24a80f7872d83bb7d7e1ad [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));
Ady Abraham5e7371c2020-03-24 14:47:24 -070062 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080063};
64
65} // namespace
66
67class EventThreadTest : public testing::Test {
68protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080069 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080070 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070071 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
72 ResyncCallback&& resyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070073 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070074 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070075 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080076 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
77 };
78
79 using ConnectionEventRecorder =
80 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
81
82 EventThreadTest();
83 ~EventThreadTest() override;
84
Dominik Laskowski6505f792019-09-18 11:10:05 -070085 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham62f216c2020-10-13 19:07:23 -070086 sp<MockEventThreadConnection> createConnection(
87 ConnectionEventRecorder& recorder,
88 ISurfaceComposer::EventRegistrationFlags eventRegistration = {},
89 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080090
91 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070092 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
93 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080094 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
95 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
96 void expectVsyncEventReceivedByConnection(const char* name,
97 ConnectionEventRecorder& connectionEventRecorder,
98 nsecs_t expectedTimestamp, unsigned expectedCount);
99 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Rachel Lee3f028662021-11-04 19:32:24 +0000100 void expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp,
101 nsecs_t preferredDeadline);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800102 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700103 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800104 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700105 int32_t expectedConfigId,
106 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700107 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700108 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
109 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800110
111 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
112 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700113 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
114 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800115 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
116 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700117 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800118 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700119 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800120
Dominik Laskowski6505f792019-09-18 11:10:05 -0700121 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800122 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700123 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800124 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700125 sp<MockEventThreadConnection> mThrottledConnection;
Rachel Lee3f028662021-11-04 19:32:24 +0000126 std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700127
128 static constexpr uid_t mConnectionUid = 443;
129 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800130};
131
132EventThreadTest::EventThreadTest() {
133 const ::testing::TestInfo* const test_info =
134 ::testing::UnitTest::GetInstance()->current_test_info();
135 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
136
Dominik Laskowski6505f792019-09-18 11:10:05 -0700137 auto vsyncSource = std::make_unique<MockVSyncSource>();
138 mVSyncSource = vsyncSource.get();
139
140 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800141 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
142
Dominik Laskowski6505f792019-09-18 11:10:05 -0700143 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
145
Ady Abraham9c53ee72020-07-22 21:16:18 -0700146 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
147 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800148
Dominik Laskowski6505f792019-09-18 11:10:05 -0700149 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700150 mConnection = createConnection(mConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100151 ISurfaceComposer::EventRegistration::modeChanged |
Ady Abraham62f216c2020-10-13 19:07:23 -0700152 ISurfaceComposer::EventRegistration::frameRateOverride);
153 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100154 ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700155 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800156
157 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800158 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
159 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800160}
161
162EventThreadTest::~EventThreadTest() {
163 const ::testing::TestInfo* const test_info =
164 ::testing::UnitTest::GetInstance()->current_test_info();
165 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800166
167 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800168 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800169}
170
Dominik Laskowski6505f792019-09-18 11:10:05 -0700171void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700172 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
173 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
174 return (uid == mThrottledConnectionUid);
175 };
Jorim Jaggic0086af2021-02-12 18:18:11 +0100176 const auto getVsyncPeriod = [](uid_t uid) {
177 return VSYNC_PERIOD.count();
178 };
Ady Abraham0bb6a472020-10-12 10:22:13 -0700179
Rachel Lee3f028662021-11-04 19:32:24 +0000180 mTokenManager = std::make_unique<frametimeline::impl::TokenManager>();
181 mThread = std::make_unique<impl::EventThread>(std::move(source), mTokenManager.get(),
Ady Abraham0bb6a472020-10-12 10:22:13 -0700182 mInterceptVSyncCallRecorder.getInvocable(),
Jorim Jaggic0086af2021-02-12 18:18:11 +0100183 throttleVsync, getVsyncPeriod);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800184
185 // EventThread should register itself as VSyncSource callback.
186 mCallback = expectVSyncSetCallbackCallReceived();
187 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800188}
189
190sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700191 ConnectionEventRecorder& recorder,
192 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800193 sp<MockEventThreadConnection> connection =
Ady Abraham0bb6a472020-10-12 10:22:13 -0700194 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700195 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800196 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
197 return connection;
198}
199
200void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
201 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
202 ASSERT_TRUE(args.has_value());
203 EXPECT_EQ(expectedState, std::get<0>(args.value()));
204}
205
Ady Abraham9c53ee72020-07-22 21:16:18 -0700206void EventThreadTest::expectVSyncSetDurationCallReceived(
207 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
208 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800209 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700210 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
211 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800212}
213
214VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
215 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
216 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
217}
218
219void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
220 auto args = mInterceptVSyncCallRecorder.waitForCall();
221 ASSERT_TRUE(args.has_value());
222 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
223}
224
Ady Abraham0bb6a472020-10-12 10:22:13 -0700225void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
226 auto args = mThrottleVsyncCallRecorder.waitForCall();
227 ASSERT_TRUE(args.has_value());
228 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
229 EXPECT_EQ(uid, std::get<1>(args.value()));
230}
231
Lloyd Pique24b0a482018-03-09 18:52:26 -0800232void EventThreadTest::expectVsyncEventReceivedByConnection(
233 const char* name, ConnectionEventRecorder& connectionEventRecorder,
234 nsecs_t expectedTimestamp, unsigned expectedCount) {
235 auto args = connectionEventRecorder.waitForCall();
236 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
237 << expectedTimestamp;
238 const auto& event = std::get<0>(args.value());
239 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
240 << name << " did not get the correct event for timestamp " << expectedTimestamp;
241 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
242 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
243 EXPECT_EQ(expectedCount, event.vsync.count)
244 << name << " did not get the expected count for timestamp " << expectedTimestamp;
245}
246
247void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
248 unsigned expectedCount) {
249 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
250 mConnectionEventCallRecorder, expectedTimestamp,
251 expectedCount);
252}
253
Rachel Lee3f028662021-11-04 19:32:24 +0000254void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp,
255 nsecs_t preferredDeadline) {
256 auto args = mConnectionEventCallRecorder.waitForCall();
257 ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp "
258 << expectedTimestamp;
259 const auto& event = std::get<0>(args.value());
260 for (int i = 0; i < DisplayEventReceiver::kFrameTimelinesLength; i++) {
Rachel Lee8d0c6102021-11-03 22:00:25 +0000261 auto prediction =
262 mTokenManager->getPredictionsForToken(event.vsync.frameTimelines[i].vsyncId);
263 EXPECT_TRUE(prediction.has_value());
264 EXPECT_EQ(prediction.value().endTime, event.vsync.frameTimelines[i].deadlineTimestamp)
265 << "Deadline timestamp does not match cached value";
266 EXPECT_EQ(prediction.value().presentTime,
267 event.vsync.frameTimelines[i].expectedVSyncTimestamp)
268 << "Expected vsync timestamp does not match cached value";
269
Rachel Lee3f028662021-11-04 19:32:24 +0000270 if (i > 0) {
271 EXPECT_GT(event.vsync.frameTimelines[i].deadlineTimestamp,
272 event.vsync.frameTimelines[i - 1].deadlineTimestamp)
273 << "Deadline timestamp out of order for frame timeline " << i;
274 EXPECT_GT(event.vsync.frameTimelines[i].expectedVSyncTimestamp,
275 event.vsync.frameTimelines[i - 1].expectedVSyncTimestamp)
276 << "Expected vsync timestamp out of order for frame timeline " << i;
277 }
Rachel Lee0d943202022-02-01 23:29:41 -0800278
279 // Vsync ID order lines up with registration into test token manager.
280 EXPECT_EQ(i, event.vsync.frameTimelines[i].vsyncId)
281 << "Vsync ID incorrect for frame timeline " << i;
282 if (i == event.vsync.preferredFrameTimelineIndex) {
283 EXPECT_EQ(event.vsync.frameTimelines[i].deadlineTimestamp, preferredDeadline)
284 << "Preferred deadline timestamp incorrect" << i;
285 EXPECT_EQ(event.vsync.frameTimelines[i].expectedVSyncTimestamp,
286 event.vsync.expectedVSyncTimestamp)
287 << "Preferred expected vsync timestamp incorrect" << i;
Rachel Lee3f028662021-11-04 19:32:24 +0000288 }
289 }
290}
291
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800292void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
293 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800294 auto args = mConnectionEventCallRecorder.waitForCall();
295 ASSERT_TRUE(args.has_value());
296 const auto& event = std::get<0>(args.value());
297 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800298 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800299 EXPECT_EQ(expectedConnected, event.hotplug.connected);
300}
301
Ady Abraham447052e2019-02-13 16:07:27 -0800302void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700303 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
304 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800305 auto args = mConnectionEventCallRecorder.waitForCall();
306 ASSERT_TRUE(args.has_value());
307 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100308 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800309 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100310 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
311 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800312}
313
Ady Abraham62f216c2020-10-13 19:07:23 -0700314void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
315 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
316 for (const auto [uid, frameRateHz] : expectedOverrides) {
317 auto args = mConnectionEventCallRecorder.waitForCall();
318 ASSERT_TRUE(args.has_value());
319 const auto& event = std::get<0>(args.value());
320 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
321 EXPECT_EQ(expectedDisplayId, event.header.displayId);
322 EXPECT_EQ(uid, event.frameRateOverride.uid);
323 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
324 }
325
326 auto args = mConnectionEventCallRecorder.waitForCall();
327 ASSERT_TRUE(args.has_value());
328 const auto& event = std::get<0>(args.value());
329 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
330 EXPECT_EQ(expectedDisplayId, event.header.displayId);
331}
332
Lloyd Pique24b0a482018-03-09 18:52:26 -0800333namespace {
334
335/* ------------------------------------------------------------------------
336 * Test cases
337 */
338
339TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
340 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
341 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700342 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
344 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
345 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
346}
347
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800348TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800349 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
350 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800351
352 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700353 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800354
355 // EventThread should not enable vsync callbacks.
356 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800357}
358
Lloyd Pique24b0a482018-03-09 18:52:26 -0800359TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
360 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700361 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800362
Ady Abraham8532d012019-05-08 14:50:56 -0700363 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
365
Dominik Laskowski029cc122019-01-23 19:52:06 -0800366 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800367 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368
369 // Use the received callback to signal a first vsync event.
370 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700371 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800372 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700373 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800374 expectVsyncEventReceivedByConnection(123, 1u);
375
376 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700377 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800378 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700379 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800380 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700381 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800382 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
383
384 // EventThread should also detect that at this point that it does not need
385 // any more vsync events, and should disable their generation.
386 expectVSyncSetEnabledCallReceived(false);
387}
388
Rachel Lee3f028662021-11-04 19:32:24 +0000389TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
390 // Signal that we want the next vsync event to be posted to the connection
391 mThread->requestNextVsync(mConnection);
392
393 expectVSyncSetEnabledCallReceived(true);
394
395 // Use the received callback to signal a vsync event.
396 // The interceptor should receive the event, as well as the connection.
397 mCallback->onVSyncEvent(123, 456, 789);
398 expectInterceptCallReceived(123);
399 expectVsyncEventFrameTimelinesCorrect(123, 789);
400}
401
Lloyd Pique24b0a482018-03-09 18:52:26 -0800402TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
403 // Create a first connection, register it, and request a vsync rate of zero.
404 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700405 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406 mThread->setVsyncRate(0, firstConnection);
407
408 // By itself, this should not enable vsync events
409 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
410 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
411
412 // However if there is another connection which wants events at a nonzero rate.....
413 ConnectionEventRecorder secondConnectionEventRecorder{0};
414 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700415 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 mThread->setVsyncRate(1, secondConnection);
417
Dominik Laskowski029cc122019-01-23 19:52:06 -0800418 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800420
421 // Send a vsync event. EventThread should then make a call to the
422 // interceptor, and the second connection. The first connection should not
423 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700424 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800425 expectInterceptCallReceived(123);
426 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
427 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
428 1u);
429}
430
431TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
432 mThread->setVsyncRate(1, mConnection);
433
Dominik Laskowski029cc122019-01-23 19:52:06 -0800434 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800435 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436
437 // Send a vsync event. EventThread should then make a call to the
438 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700439 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800440 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700441 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800442 expectVsyncEventReceivedByConnection(123, 1u);
443
444 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700445 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800446 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700447 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800448 expectVsyncEventReceivedByConnection(456, 2u);
449
450 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700451 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800452 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700453 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800454 expectVsyncEventReceivedByConnection(789, 3u);
455}
456
457TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
458 mThread->setVsyncRate(2, mConnection);
459
Dominik Laskowski029cc122019-01-23 19:52:06 -0800460 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800461 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800462
463 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700464 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800465 expectInterceptCallReceived(123);
466 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700467 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800468
469 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700470 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800471 expectInterceptCallReceived(456);
472 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700473 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800474
475 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700476 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800477 expectInterceptCallReceived(789);
478 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700479 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800480
481 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700482 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800483 expectInterceptCallReceived(101112);
484 expectVsyncEventReceivedByConnection(101112, 4u);
485}
486
487TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
488 mThread->setVsyncRate(1, mConnection);
489
Dominik Laskowski029cc122019-01-23 19:52:06 -0800490 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800491 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800492
493 // Destroy the only (strong) reference to the connection.
494 mConnection = nullptr;
495
496 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700497 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800498 expectInterceptCallReceived(123);
499 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
500
501 // EventThread should disable vsync callbacks
502 expectVSyncSetEnabledCallReceived(false);
503}
504
505TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
506 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700507 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800508 mThread->setVsyncRate(1, errorConnection);
509
Dominik Laskowski029cc122019-01-23 19:52:06 -0800510 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800511 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800512
513 // The first event will be seen by the interceptor, and by the connection,
514 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700515 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800516 expectInterceptCallReceived(123);
517 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
518
519 // A subsequent event will be seen by the interceptor and not by the
520 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700521 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800522 expectInterceptCallReceived(456);
523 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
524
525 // EventThread should disable vsync callbacks with the second event
526 expectVSyncSetEnabledCallReceived(false);
527}
528
Alec Mouri717bcb62020-02-10 17:07:19 -0800529TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700530 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800531 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700532 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800533 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700534 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800535 ConnectionEventRecorder secondConnectionEventRecorder{0};
536 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700537 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800538 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700539 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800540
541 // EventThread should enable vsync callbacks.
542 expectVSyncSetEnabledCallReceived(true);
543
544 // The first event will be seen by the interceptor, and by the connection,
545 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700546 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800547 expectInterceptCallReceived(123);
548 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
549 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
550 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700551 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800552}
553
Lloyd Pique24b0a482018-03-09 18:52:26 -0800554TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
555 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
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 non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700564 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 by the connection,
569 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700570 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800571 expectInterceptCallReceived(456);
572 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
573
574 // EventThread will not disable vsync callbacks as the errors are non-fatal.
575 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
576}
577
578TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700579 mThread->setDuration(321ns, 456ns);
580 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800581}
582
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800583TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
584 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
585 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800586}
587
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800588TEST_F(EventThreadTest, postHotplugInternalConnect) {
589 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
590 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800591}
592
593TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800594 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
595 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800596}
597
598TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800599 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
600 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800601}
602
Ady Abraham447052e2019-02-13 16:07:27 -0800603TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700604 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
605 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
606 .setId(DisplayModeId(7))
607 .setVsyncPeriod(16666666)
608 .build();
609
610 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700611 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800612}
613
614TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700615 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
616 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
617 .setId(DisplayModeId(5))
618 .setVsyncPeriod(16666666)
619 .build();
620
621 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700622 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800623}
624
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700625TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700626 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
627 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
628 .setId(DisplayModeId(7))
629 .setVsyncPeriod(16666666)
630 .build();
631 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700632 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700633}
634
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700635TEST_F(EventThreadTest, suppressConfigChanged) {
636 ConnectionEventRecorder suppressConnectionEventRecorder{0};
637 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700638 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700639
Ady Abraham690f4612021-07-01 23:24:03 -0700640 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
641 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
642 .setId(DisplayModeId(9))
643 .setVsyncPeriod(16666666)
644 .build();
645
646 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700647 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700648
649 auto args = suppressConnectionEventRecorder.waitForCall();
650 ASSERT_FALSE(args.has_value());
651}
652
Ady Abraham62f216c2020-10-13 19:07:23 -0700653TEST_F(EventThreadTest, postUidFrameRateMapping) {
654 const std::vector<FrameRateOverride> overrides = {
655 {.uid = 1, .frameRateHz = 20},
656 {.uid = 3, .frameRateHz = 40},
657 {.uid = 5, .frameRateHz = 60},
658 };
659
660 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
661 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
662}
663
664TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
665 const std::vector<FrameRateOverride> overrides = {
666 {.uid = 1, .frameRateHz = 20},
667 {.uid = 3, .frameRateHz = 40},
668 {.uid = 5, .frameRateHz = 60},
669 };
670
671 ConnectionEventRecorder suppressConnectionEventRecorder{0};
672 sp<MockEventThreadConnection> suppressConnection =
673 createConnection(suppressConnectionEventRecorder);
674
675 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
676 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
677
678 auto args = suppressConnectionEventRecorder.waitForCall();
679 ASSERT_FALSE(args.has_value());
680}
681
Ady Abraham0bb6a472020-10-12 10:22:13 -0700682TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
683 // Signal that we want the next vsync event to be posted to the throttled connection
684 mThread->requestNextVsync(mThrottledConnection);
685
686 // EventThread should immediately request a resync.
687 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
688
689 // EventThread should enable vsync callbacks.
690 expectVSyncSetEnabledCallReceived(true);
691
692 // Use the received callback to signal a first vsync event.
693 // The interceptor should receive the event, but not the connection.
694 mCallback->onVSyncEvent(123, 456, 789);
695 expectInterceptCallReceived(123);
696 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
697 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
698
699 // Use the received callback to signal a second vsync event.
700 // The interceptor should receive the event, but the connection should
701 // not as it was only interested in the first.
702 mCallback->onVSyncEvent(456, 123, 0);
703 expectInterceptCallReceived(456);
704 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
705 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
706
707 // EventThread should not change the vsync state as it didn't send the event
708 // yet
709 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
710}
711
Lloyd Pique24b0a482018-03-09 18:52:26 -0800712} // namespace
713} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100714
715// TODO(b/129481165): remove the #pragma below and fix conversion issues
716#pragma clang diagnostic pop // ignored "-Wextra"