blob: 67a0d7ec3253e2477fd5823585121aeb5a8abb96 [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 }
278 if (event.vsync.frameTimelines[i].deadlineTimestamp == preferredDeadline) {
279 EXPECT_EQ(i, event.vsync.preferredFrameTimelineIndex)
280 << "Preferred frame timeline index should be " << i;
281 // For the platform-preferred frame timeline, the vsync ID is 0 because the first frame
282 // timeline is made before the rest.
283 EXPECT_EQ(0, event.vsync.frameTimelines[i].vsyncId)
284 << "Vsync ID incorrect for frame timeline " << i;
285 } else {
286 // Vsync ID 0 is used for the preferred frame timeline.
287 EXPECT_EQ(i + 1, event.vsync.frameTimelines[i].vsyncId)
288 << "Vsync ID incorrect for frame timeline " << i;
289 }
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
336/* ------------------------------------------------------------------------
337 * Test cases
338 */
339
340TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
341 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
342 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700343 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800344 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
345 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
346 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
347}
348
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800349TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800350 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
351 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800352
353 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700354 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800355
356 // EventThread should not enable vsync callbacks.
357 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800358}
359
Lloyd Pique24b0a482018-03-09 18:52:26 -0800360TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
361 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700362 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800363
Ady Abraham8532d012019-05-08 14:50:56 -0700364 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
366
Dominik Laskowski029cc122019-01-23 19:52:06 -0800367 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800369
370 // Use the received callback to signal a first vsync event.
371 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700372 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800373 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700374 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800375 expectVsyncEventReceivedByConnection(123, 1u);
376
377 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700378 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800379 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700380 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700382 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
384
385 // EventThread should also detect that at this point that it does not need
386 // any more vsync events, and should disable their generation.
387 expectVSyncSetEnabledCallReceived(false);
388}
389
Rachel Lee3f028662021-11-04 19:32:24 +0000390TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) {
391 // Signal that we want the next vsync event to be posted to the connection
392 mThread->requestNextVsync(mConnection);
393
394 expectVSyncSetEnabledCallReceived(true);
395
396 // Use the received callback to signal a vsync event.
397 // The interceptor should receive the event, as well as the connection.
398 mCallback->onVSyncEvent(123, 456, 789);
399 expectInterceptCallReceived(123);
400 expectVsyncEventFrameTimelinesCorrect(123, 789);
401}
402
Lloyd Pique24b0a482018-03-09 18:52:26 -0800403TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
404 // Create a first connection, register it, and request a vsync rate of zero.
405 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700406 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407 mThread->setVsyncRate(0, firstConnection);
408
409 // By itself, this should not enable vsync events
410 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
411 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
412
413 // However if there is another connection which wants events at a nonzero rate.....
414 ConnectionEventRecorder secondConnectionEventRecorder{0};
415 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700416 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800417 mThread->setVsyncRate(1, secondConnection);
418
Dominik Laskowski029cc122019-01-23 19:52:06 -0800419 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800420 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800421
422 // Send a vsync event. EventThread should then make a call to the
423 // interceptor, and the second connection. The first connection should not
424 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700425 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800426 expectInterceptCallReceived(123);
427 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
428 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
429 1u);
430}
431
432TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
433 mThread->setVsyncRate(1, mConnection);
434
Dominik Laskowski029cc122019-01-23 19:52:06 -0800435 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800437
438 // Send a vsync event. EventThread should then make a call to the
439 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700440 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800441 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700442 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443 expectVsyncEventReceivedByConnection(123, 1u);
444
445 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700446 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800447 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700448 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800449 expectVsyncEventReceivedByConnection(456, 2u);
450
451 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700452 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800453 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700454 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800455 expectVsyncEventReceivedByConnection(789, 3u);
456}
457
458TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
459 mThread->setVsyncRate(2, mConnection);
460
Dominik Laskowski029cc122019-01-23 19:52:06 -0800461 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800462 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800463
464 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700465 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800466 expectInterceptCallReceived(123);
467 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700468 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800469
470 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700471 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800472 expectInterceptCallReceived(456);
473 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700474 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800475
476 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700477 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800478 expectInterceptCallReceived(789);
479 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700480 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800481
482 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700483 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800484 expectInterceptCallReceived(101112);
485 expectVsyncEventReceivedByConnection(101112, 4u);
486}
487
488TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
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 // Destroy the only (strong) reference to the connection.
495 mConnection = nullptr;
496
497 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700498 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800499 expectInterceptCallReceived(123);
500 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
501
502 // EventThread should disable vsync callbacks
503 expectVSyncSetEnabledCallReceived(false);
504}
505
506TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
507 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700508 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800509 mThread->setVsyncRate(1, errorConnection);
510
Dominik Laskowski029cc122019-01-23 19:52:06 -0800511 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800512 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800513
514 // The first event will be seen by the interceptor, and by the connection,
515 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700516 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800517 expectInterceptCallReceived(123);
518 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
519
520 // A subsequent event will be seen by the interceptor and not by the
521 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700522 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523 expectInterceptCallReceived(456);
524 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
525
526 // EventThread should disable vsync callbacks with the second event
527 expectVSyncSetEnabledCallReceived(false);
528}
529
Alec Mouri717bcb62020-02-10 17:07:19 -0800530TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700531 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800532 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700533 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800534 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700535 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800536 ConnectionEventRecorder secondConnectionEventRecorder{0};
537 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700538 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800539 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700540 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800541
542 // EventThread should enable vsync callbacks.
543 expectVSyncSetEnabledCallReceived(true);
544
545 // The first event will be seen by the interceptor, and by the connection,
546 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700547 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800548 expectInterceptCallReceived(123);
549 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
550 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
551 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700552 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800553}
554
Lloyd Pique24b0a482018-03-09 18:52:26 -0800555TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
556 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700557 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800558 mThread->setVsyncRate(1, errorConnection);
559
Dominik Laskowski029cc122019-01-23 19:52:06 -0800560 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800561 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800562
563 // The first event will be seen by the interceptor, and by the connection,
564 // which then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700565 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800566 expectInterceptCallReceived(123);
567 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
568
569 // A subsequent event will be seen by the interceptor, and by the connection,
570 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700571 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800572 expectInterceptCallReceived(456);
573 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
574
575 // EventThread will not disable vsync callbacks as the errors are non-fatal.
576 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
577}
578
579TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700580 mThread->setDuration(321ns, 456ns);
581 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800582}
583
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800584TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
585 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
586 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800587}
588
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800589TEST_F(EventThreadTest, postHotplugInternalConnect) {
590 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
591 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800592}
593
594TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800595 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
596 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800597}
598
599TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800600 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
601 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800602}
603
Ady Abraham447052e2019-02-13 16:07:27 -0800604TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham690f4612021-07-01 23:24:03 -0700605 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
606 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
607 .setId(DisplayModeId(7))
608 .setVsyncPeriod(16666666)
609 .build();
610
611 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700612 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800613}
614
615TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham690f4612021-07-01 23:24:03 -0700616 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
617 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
618 .setId(DisplayModeId(5))
619 .setVsyncPeriod(16666666)
620 .build();
621
622 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700623 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800624}
625
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700626TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham690f4612021-07-01 23:24:03 -0700627 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
628 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
629 .setId(DisplayModeId(7))
630 .setVsyncPeriod(16666666)
631 .build();
632 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700633 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700634}
635
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700636TEST_F(EventThreadTest, suppressConfigChanged) {
637 ConnectionEventRecorder suppressConnectionEventRecorder{0};
638 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700639 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700640
Ady Abraham690f4612021-07-01 23:24:03 -0700641 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
642 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
643 .setId(DisplayModeId(9))
644 .setVsyncPeriod(16666666)
645 .build();
646
647 mThread->onModeChanged(mode);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700648 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700649
650 auto args = suppressConnectionEventRecorder.waitForCall();
651 ASSERT_FALSE(args.has_value());
652}
653
Ady Abraham62f216c2020-10-13 19:07:23 -0700654TEST_F(EventThreadTest, postUidFrameRateMapping) {
655 const std::vector<FrameRateOverride> overrides = {
656 {.uid = 1, .frameRateHz = 20},
657 {.uid = 3, .frameRateHz = 40},
658 {.uid = 5, .frameRateHz = 60},
659 };
660
661 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
662 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
663}
664
665TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
666 const std::vector<FrameRateOverride> overrides = {
667 {.uid = 1, .frameRateHz = 20},
668 {.uid = 3, .frameRateHz = 40},
669 {.uid = 5, .frameRateHz = 60},
670 };
671
672 ConnectionEventRecorder suppressConnectionEventRecorder{0};
673 sp<MockEventThreadConnection> suppressConnection =
674 createConnection(suppressConnectionEventRecorder);
675
676 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
677 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
678
679 auto args = suppressConnectionEventRecorder.waitForCall();
680 ASSERT_FALSE(args.has_value());
681}
682
Ady Abraham0bb6a472020-10-12 10:22:13 -0700683TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
684 // Signal that we want the next vsync event to be posted to the throttled connection
685 mThread->requestNextVsync(mThrottledConnection);
686
687 // EventThread should immediately request a resync.
688 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
689
690 // EventThread should enable vsync callbacks.
691 expectVSyncSetEnabledCallReceived(true);
692
693 // Use the received callback to signal a first vsync event.
694 // The interceptor should receive the event, but not the connection.
695 mCallback->onVSyncEvent(123, 456, 789);
696 expectInterceptCallReceived(123);
697 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
698 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
699
700 // Use the received callback to signal a second vsync event.
701 // The interceptor should receive the event, but the connection should
702 // not as it was only interested in the first.
703 mCallback->onVSyncEvent(456, 123, 0);
704 expectInterceptCallReceived(456);
705 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
706 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
707
708 // EventThread should not change the vsync state as it didn't send the event
709 // yet
710 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
711}
712
Lloyd Pique24b0a482018-03-09 18:52:26 -0800713} // namespace
714} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100715
716// TODO(b/129481165): remove the #pragma below and fix conversion issues
717#pragma clang diagnostic pop // ignored "-Wextra"