1 /*
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
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wextra"
20
21 #undef LOG_TAG
22 #define LOG_TAG "LibSurfaceFlingerUnittests"
23
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <log/log.h>
27 #include <utils/Errors.h>
28
29 #include "AsyncCallRecorder.h"
30 #include "DisplayHardware/DisplayMode.h"
31 #include "Scheduler/EventThread.h"
32
33 using namespace std::chrono_literals;
34 using namespace std::placeholders;
35
36 using namespace android::flag_operators;
37 using testing::_;
38 using testing::Invoke;
39
40 namespace android {
41
42 namespace {
43
44 constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111);
45 constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222);
46 constexpr PhysicalDisplayId DISPLAY_ID_64BIT(0xabcd12349876fedcULL);
47 constexpr std::chrono::duration VSYNC_PERIOD(16ms);
48 class MockVSyncSource : public VSyncSource {
49 public:
getName() const50 const char* getName() const override { return "test"; }
51
52 MOCK_METHOD1(setVSyncEnabled, void(bool));
53 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
54 MOCK_METHOD2(setDuration,
55 void(std::chrono::nanoseconds workDuration,
56 std::chrono::nanoseconds readyDuration));
57 MOCK_METHOD1(pauseVsyncCallback, void(bool));
58 MOCK_CONST_METHOD1(dump, void(std::string&));
59 };
60
61 } // namespace
62
63 class EventThreadTest : public testing::Test {
64 protected:
65 class MockEventThreadConnection : public EventThreadConnection {
66 public:
MockEventThreadConnection(impl::EventThread * eventThread,uid_t callingUid,ResyncCallback && resyncCallback,ISurfaceComposer::EventRegistrationFlags eventRegistration)67 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
68 ResyncCallback&& resyncCallback,
69 ISurfaceComposer::EventRegistrationFlags eventRegistration)
70 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
71 eventRegistration) {}
72 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
73 };
74
75 using ConnectionEventRecorder =
76 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
77
78 EventThreadTest();
79 ~EventThreadTest() override;
80
81 void createThread(std::unique_ptr<VSyncSource>);
82 sp<MockEventThreadConnection> createConnection(
83 ConnectionEventRecorder& recorder,
84 ISurfaceComposer::EventRegistrationFlags eventRegistration = {},
85 uid_t ownerUid = mConnectionUid);
86
87 void expectVSyncSetEnabledCallReceived(bool expectedState);
88 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
89 std::chrono::nanoseconds expectedReadyDuration);
90 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
91 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
92 void expectVsyncEventReceivedByConnection(const char* name,
93 ConnectionEventRecorder& connectionEventRecorder,
94 nsecs_t expectedTimestamp, unsigned expectedCount);
95 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
96 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
97 bool expectedConnected);
98 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
99 int32_t expectedConfigId,
100 nsecs_t expectedVsyncPeriod);
101 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
102 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
103 std::vector<FrameRateOverride>);
104
105 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
106 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
107 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
108 mVSyncSetDurationCallRecorder;
109 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
110 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
111 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
112 ConnectionEventRecorder mConnectionEventCallRecorder{0};
113 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
114
115 MockVSyncSource* mVSyncSource;
116 VSyncSource::Callback* mCallback = nullptr;
117 std::unique_ptr<impl::EventThread> mThread;
118 sp<MockEventThreadConnection> mConnection;
119 sp<MockEventThreadConnection> mThrottledConnection;
120
121 static constexpr uid_t mConnectionUid = 443;
122 static constexpr uid_t mThrottledConnectionUid = 177;
123 };
124
EventThreadTest()125 EventThreadTest::EventThreadTest() {
126 const ::testing::TestInfo* const test_info =
127 ::testing::UnitTest::GetInstance()->current_test_info();
128 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
129
130 auto vsyncSource = std::make_unique<MockVSyncSource>();
131 mVSyncSource = vsyncSource.get();
132
133 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
134 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
135
136 EXPECT_CALL(*mVSyncSource, setCallback(_))
137 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
138
139 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
140 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
141
142 createThread(std::move(vsyncSource));
143 mConnection = createConnection(mConnectionEventCallRecorder,
144 ISurfaceComposer::EventRegistration::modeChanged |
145 ISurfaceComposer::EventRegistration::frameRateOverride);
146 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
147 ISurfaceComposer::EventRegistration::modeChanged,
148 mThrottledConnectionUid);
149
150 // A display must be connected for VSYNC events to be delivered.
151 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
152 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
153 }
154
~EventThreadTest()155 EventThreadTest::~EventThreadTest() {
156 const ::testing::TestInfo* const test_info =
157 ::testing::UnitTest::GetInstance()->current_test_info();
158 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
159
160 // EventThread should unregister itself as VSyncSource callback.
161 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
162 }
163
createThread(std::unique_ptr<VSyncSource> source)164 void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
165 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
166 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
167 return (uid == mThrottledConnectionUid);
168 };
169 const auto getVsyncPeriod = [](uid_t uid) {
170 return VSYNC_PERIOD.count();
171 };
172
173 mThread = std::make_unique<impl::EventThread>(std::move(source),
174 /*tokenManager=*/nullptr,
175 mInterceptVSyncCallRecorder.getInvocable(),
176 throttleVsync, getVsyncPeriod);
177
178 // EventThread should register itself as VSyncSource callback.
179 mCallback = expectVSyncSetCallbackCallReceived();
180 ASSERT_TRUE(mCallback);
181 }
182
createConnection(ConnectionEventRecorder & recorder,ISurfaceComposer::EventRegistrationFlags eventRegistration,uid_t ownerUid)183 sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
184 ConnectionEventRecorder& recorder,
185 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
186 sp<MockEventThreadConnection> connection =
187 new MockEventThreadConnection(mThread.get(), ownerUid,
188 mResyncCallRecorder.getInvocable(), eventRegistration);
189 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
190 return connection;
191 }
192
expectVSyncSetEnabledCallReceived(bool expectedState)193 void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
194 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
195 ASSERT_TRUE(args.has_value());
196 EXPECT_EQ(expectedState, std::get<0>(args.value()));
197 }
198
expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,std::chrono::nanoseconds expectedReadyDuration)199 void EventThreadTest::expectVSyncSetDurationCallReceived(
200 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
201 auto args = mVSyncSetDurationCallRecorder.waitForCall();
202 ASSERT_TRUE(args.has_value());
203 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
204 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
205 }
206
expectVSyncSetCallbackCallReceived()207 VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
208 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
209 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
210 }
211
expectInterceptCallReceived(nsecs_t expectedTimestamp)212 void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
213 auto args = mInterceptVSyncCallRecorder.waitForCall();
214 ASSERT_TRUE(args.has_value());
215 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
216 }
217
expectThrottleVsyncReceived(nsecs_t expectedTimestamp,uid_t uid)218 void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
219 auto args = mThrottleVsyncCallRecorder.waitForCall();
220 ASSERT_TRUE(args.has_value());
221 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
222 EXPECT_EQ(uid, std::get<1>(args.value()));
223 }
224
expectVsyncEventReceivedByConnection(const char * name,ConnectionEventRecorder & connectionEventRecorder,nsecs_t expectedTimestamp,unsigned expectedCount)225 void EventThreadTest::expectVsyncEventReceivedByConnection(
226 const char* name, ConnectionEventRecorder& connectionEventRecorder,
227 nsecs_t expectedTimestamp, unsigned expectedCount) {
228 auto args = connectionEventRecorder.waitForCall();
229 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
230 << expectedTimestamp;
231 const auto& event = std::get<0>(args.value());
232 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
233 << name << " did not get the correct event for timestamp " << expectedTimestamp;
234 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
235 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
236 EXPECT_EQ(expectedCount, event.vsync.count)
237 << name << " did not get the expected count for timestamp " << expectedTimestamp;
238 }
239
expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,unsigned expectedCount)240 void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
241 unsigned expectedCount) {
242 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
243 mConnectionEventCallRecorder, expectedTimestamp,
244 expectedCount);
245 }
246
expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,bool expectedConnected)247 void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
248 bool expectedConnected) {
249 auto args = mConnectionEventCallRecorder.waitForCall();
250 ASSERT_TRUE(args.has_value());
251 const auto& event = std::get<0>(args.value());
252 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
253 EXPECT_EQ(expectedDisplayId, event.header.displayId);
254 EXPECT_EQ(expectedConnected, event.hotplug.connected);
255 }
256
expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,int32_t expectedConfigId,nsecs_t expectedVsyncPeriod)257 void EventThreadTest::expectConfigChangedEventReceivedByConnection(
258 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
259 nsecs_t expectedVsyncPeriod) {
260 auto args = mConnectionEventCallRecorder.waitForCall();
261 ASSERT_TRUE(args.has_value());
262 const auto& event = std::get<0>(args.value());
263 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
264 EXPECT_EQ(expectedDisplayId, event.header.displayId);
265 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
266 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
267 }
268
expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,std::vector<FrameRateOverride> expectedOverrides)269 void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
270 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
271 for (const auto [uid, frameRateHz] : expectedOverrides) {
272 auto args = mConnectionEventCallRecorder.waitForCall();
273 ASSERT_TRUE(args.has_value());
274 const auto& event = std::get<0>(args.value());
275 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
276 EXPECT_EQ(expectedDisplayId, event.header.displayId);
277 EXPECT_EQ(uid, event.frameRateOverride.uid);
278 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
279 }
280
281 auto args = mConnectionEventCallRecorder.waitForCall();
282 ASSERT_TRUE(args.has_value());
283 const auto& event = std::get<0>(args.value());
284 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
285 EXPECT_EQ(expectedDisplayId, event.header.displayId);
286 }
287
288 namespace {
289
290 /* ------------------------------------------------------------------------
291 * Test cases
292 */
293
TEST_F(EventThreadTest,canCreateAndDestroyThreadWithNoEventsSent)294 TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
295 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
296 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
297 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
298 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
299 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
300 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
301 }
302
TEST_F(EventThreadTest,vsyncRequestIsIgnoredIfDisplayIsDisconnected)303 TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
304 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
305 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
306
307 // Signal that we want the next vsync event to be posted to the connection.
308 mThread->requestNextVsync(mConnection);
309
310 // EventThread should not enable vsync callbacks.
311 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
312 }
313
TEST_F(EventThreadTest,requestNextVsyncPostsASingleVSyncEventToTheConnection)314 TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
315 // Signal that we want the next vsync event to be posted to the connection
316 mThread->requestNextVsync(mConnection);
317
318 // EventThread should immediately request a resync.
319 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
320
321 // EventThread should enable vsync callbacks.
322 expectVSyncSetEnabledCallReceived(true);
323
324 // Use the received callback to signal a first vsync event.
325 // The interceptor should receive the event, as well as the connection.
326 mCallback->onVSyncEvent(123, 456, 789);
327 expectInterceptCallReceived(123);
328 expectThrottleVsyncReceived(456, mConnectionUid);
329 expectVsyncEventReceivedByConnection(123, 1u);
330
331 // Use the received callback to signal a second vsync event.
332 // The interceptor should receive the event, but the connection should
333 // not as it was only interested in the first.
334 mCallback->onVSyncEvent(456, 123, 0);
335 expectInterceptCallReceived(456);
336 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
337 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
338
339 // EventThread should also detect that at this point that it does not need
340 // any more vsync events, and should disable their generation.
341 expectVSyncSetEnabledCallReceived(false);
342 }
343
TEST_F(EventThreadTest,setVsyncRateZeroPostsNoVSyncEventsToThatConnection)344 TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
345 // Create a first connection, register it, and request a vsync rate of zero.
346 ConnectionEventRecorder firstConnectionEventRecorder{0};
347 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
348 mThread->setVsyncRate(0, firstConnection);
349
350 // By itself, this should not enable vsync events
351 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
352 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
353
354 // However if there is another connection which wants events at a nonzero rate.....
355 ConnectionEventRecorder secondConnectionEventRecorder{0};
356 sp<MockEventThreadConnection> secondConnection =
357 createConnection(secondConnectionEventRecorder);
358 mThread->setVsyncRate(1, secondConnection);
359
360 // EventThread should enable vsync callbacks.
361 expectVSyncSetEnabledCallReceived(true);
362
363 // Send a vsync event. EventThread should then make a call to the
364 // interceptor, and the second connection. The first connection should not
365 // get the event.
366 mCallback->onVSyncEvent(123, 456, 0);
367 expectInterceptCallReceived(123);
368 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
369 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
370 1u);
371 }
372
TEST_F(EventThreadTest,setVsyncRateOnePostsAllEventsToThatConnection)373 TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
374 mThread->setVsyncRate(1, mConnection);
375
376 // EventThread should enable vsync callbacks.
377 expectVSyncSetEnabledCallReceived(true);
378
379 // Send a vsync event. EventThread should then make a call to the
380 // interceptor, and the connection.
381 mCallback->onVSyncEvent(123, 456, 789);
382 expectInterceptCallReceived(123);
383 expectThrottleVsyncReceived(456, mConnectionUid);
384 expectVsyncEventReceivedByConnection(123, 1u);
385
386 // A second event should go to the same places.
387 mCallback->onVSyncEvent(456, 123, 0);
388 expectInterceptCallReceived(456);
389 expectThrottleVsyncReceived(123, mConnectionUid);
390 expectVsyncEventReceivedByConnection(456, 2u);
391
392 // A third event should go to the same places.
393 mCallback->onVSyncEvent(789, 777, 111);
394 expectInterceptCallReceived(789);
395 expectThrottleVsyncReceived(777, mConnectionUid);
396 expectVsyncEventReceivedByConnection(789, 3u);
397 }
398
TEST_F(EventThreadTest,setVsyncRateTwoPostsEveryOtherEventToThatConnection)399 TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
400 mThread->setVsyncRate(2, mConnection);
401
402 // EventThread should enable vsync callbacks.
403 expectVSyncSetEnabledCallReceived(true);
404
405 // The first event will be seen by the interceptor, and not the connection.
406 mCallback->onVSyncEvent(123, 456, 789);
407 expectInterceptCallReceived(123);
408 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
409 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
410
411 // The second event will be seen by the interceptor and the connection.
412 mCallback->onVSyncEvent(456, 123, 0);
413 expectInterceptCallReceived(456);
414 expectVsyncEventReceivedByConnection(456, 2u);
415 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
416
417 // The third event will be seen by the interceptor, and not the connection.
418 mCallback->onVSyncEvent(789, 777, 744);
419 expectInterceptCallReceived(789);
420 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
421 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
422
423 // The fourth event will be seen by the interceptor and the connection.
424 mCallback->onVSyncEvent(101112, 7847, 86);
425 expectInterceptCallReceived(101112);
426 expectVsyncEventReceivedByConnection(101112, 4u);
427 }
428
TEST_F(EventThreadTest,connectionsRemovedIfInstanceDestroyed)429 TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
430 mThread->setVsyncRate(1, mConnection);
431
432 // EventThread should enable vsync callbacks.
433 expectVSyncSetEnabledCallReceived(true);
434
435 // Destroy the only (strong) reference to the connection.
436 mConnection = nullptr;
437
438 // The first event will be seen by the interceptor, and not the connection.
439 mCallback->onVSyncEvent(123, 456, 789);
440 expectInterceptCallReceived(123);
441 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
442
443 // EventThread should disable vsync callbacks
444 expectVSyncSetEnabledCallReceived(false);
445 }
446
TEST_F(EventThreadTest,connectionsRemovedIfEventDeliveryError)447 TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
448 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
449 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
450 mThread->setVsyncRate(1, errorConnection);
451
452 // EventThread should enable vsync callbacks.
453 expectVSyncSetEnabledCallReceived(true);
454
455 // The first event will be seen by the interceptor, and by the connection,
456 // which then returns an error.
457 mCallback->onVSyncEvent(123, 456, 789);
458 expectInterceptCallReceived(123);
459 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
460
461 // A subsequent event will be seen by the interceptor and not by the
462 // connection.
463 mCallback->onVSyncEvent(456, 123, 0);
464 expectInterceptCallReceived(456);
465 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
466
467 // EventThread should disable vsync callbacks with the second event
468 expectVSyncSetEnabledCallReceived(false);
469 }
470
TEST_F(EventThreadTest,tracksEventConnections)471 TEST_F(EventThreadTest, tracksEventConnections) {
472 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
473 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
474 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
475 mThread->setVsyncRate(1, errorConnection);
476 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
477 ConnectionEventRecorder secondConnectionEventRecorder{0};
478 sp<MockEventThreadConnection> secondConnection =
479 createConnection(secondConnectionEventRecorder);
480 mThread->setVsyncRate(1, secondConnection);
481 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
482
483 // EventThread should enable vsync callbacks.
484 expectVSyncSetEnabledCallReceived(true);
485
486 // The first event will be seen by the interceptor, and by the connection,
487 // which then returns an error.
488 mCallback->onVSyncEvent(123, 456, 789);
489 expectInterceptCallReceived(123);
490 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
491 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
492 1u);
493 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
494 }
495
TEST_F(EventThreadTest,eventsDroppedIfNonfatalEventDeliveryError)496 TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
497 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
498 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
499 mThread->setVsyncRate(1, errorConnection);
500
501 // EventThread should enable vsync callbacks.
502 expectVSyncSetEnabledCallReceived(true);
503
504 // The first event will be seen by the interceptor, and by the connection,
505 // which then returns an non-fatal error.
506 mCallback->onVSyncEvent(123, 456, 789);
507 expectInterceptCallReceived(123);
508 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
509
510 // A subsequent event will be seen by the interceptor, and by the connection,
511 // which still then returns an non-fatal error.
512 mCallback->onVSyncEvent(456, 123, 0);
513 expectInterceptCallReceived(456);
514 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
515
516 // EventThread will not disable vsync callbacks as the errors are non-fatal.
517 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
518 }
519
TEST_F(EventThreadTest,setPhaseOffsetForwardsToVSyncSource)520 TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
521 mThread->setDuration(321ns, 456ns);
522 expectVSyncSetDurationCallReceived(321ns, 456ns);
523 }
524
TEST_F(EventThreadTest,postHotplugInternalDisconnect)525 TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
526 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
527 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
528 }
529
TEST_F(EventThreadTest,postHotplugInternalConnect)530 TEST_F(EventThreadTest, postHotplugInternalConnect) {
531 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
532 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
533 }
534
TEST_F(EventThreadTest,postHotplugExternalDisconnect)535 TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
536 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
537 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
538 }
539
TEST_F(EventThreadTest,postHotplugExternalConnect)540 TEST_F(EventThreadTest, postHotplugExternalConnect) {
541 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
542 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
543 }
544
TEST_F(EventThreadTest,postConfigChangedPrimary)545 TEST_F(EventThreadTest, postConfigChangedPrimary) {
546 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
547 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
548 .setId(DisplayModeId(7))
549 .setVsyncPeriod(16666666)
550 .build();
551
552 mThread->onModeChanged(mode);
553 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
554 }
555
TEST_F(EventThreadTest,postConfigChangedExternal)556 TEST_F(EventThreadTest, postConfigChangedExternal) {
557 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
558 .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID)
559 .setId(DisplayModeId(5))
560 .setVsyncPeriod(16666666)
561 .build();
562
563 mThread->onModeChanged(mode);
564 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
565 }
566
TEST_F(EventThreadTest,postConfigChangedPrimary64bit)567 TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
568 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
569 .setPhysicalDisplayId(DISPLAY_ID_64BIT)
570 .setId(DisplayModeId(7))
571 .setVsyncPeriod(16666666)
572 .build();
573 mThread->onModeChanged(mode);
574 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
575 }
576
TEST_F(EventThreadTest,suppressConfigChanged)577 TEST_F(EventThreadTest, suppressConfigChanged) {
578 ConnectionEventRecorder suppressConnectionEventRecorder{0};
579 sp<MockEventThreadConnection> suppressConnection =
580 createConnection(suppressConnectionEventRecorder);
581
582 const auto mode = DisplayMode::Builder(hal::HWConfigId(0))
583 .setPhysicalDisplayId(INTERNAL_DISPLAY_ID)
584 .setId(DisplayModeId(9))
585 .setVsyncPeriod(16666666)
586 .build();
587
588 mThread->onModeChanged(mode);
589 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
590
591 auto args = suppressConnectionEventRecorder.waitForCall();
592 ASSERT_FALSE(args.has_value());
593 }
594
TEST_F(EventThreadTest,postUidFrameRateMapping)595 TEST_F(EventThreadTest, postUidFrameRateMapping) {
596 const std::vector<FrameRateOverride> overrides = {
597 {.uid = 1, .frameRateHz = 20},
598 {.uid = 3, .frameRateHz = 40},
599 {.uid = 5, .frameRateHz = 60},
600 };
601
602 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
603 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
604 }
605
TEST_F(EventThreadTest,suppressUidFrameRateMapping)606 TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
607 const std::vector<FrameRateOverride> overrides = {
608 {.uid = 1, .frameRateHz = 20},
609 {.uid = 3, .frameRateHz = 40},
610 {.uid = 5, .frameRateHz = 60},
611 };
612
613 ConnectionEventRecorder suppressConnectionEventRecorder{0};
614 sp<MockEventThreadConnection> suppressConnection =
615 createConnection(suppressConnectionEventRecorder);
616
617 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
618 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
619
620 auto args = suppressConnectionEventRecorder.waitForCall();
621 ASSERT_FALSE(args.has_value());
622 }
623
TEST_F(EventThreadTest,requestNextVsyncWithThrottleVsyncDoesntPostVSync)624 TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
625 // Signal that we want the next vsync event to be posted to the throttled connection
626 mThread->requestNextVsync(mThrottledConnection);
627
628 // EventThread should immediately request a resync.
629 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
630
631 // EventThread should enable vsync callbacks.
632 expectVSyncSetEnabledCallReceived(true);
633
634 // Use the received callback to signal a first vsync event.
635 // The interceptor should receive the event, but not the connection.
636 mCallback->onVSyncEvent(123, 456, 789);
637 expectInterceptCallReceived(123);
638 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
639 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
640
641 // Use the received callback to signal a second vsync event.
642 // The interceptor should receive the event, but the connection should
643 // not as it was only interested in the first.
644 mCallback->onVSyncEvent(456, 123, 0);
645 expectInterceptCallReceived(456);
646 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
647 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
648
649 // EventThread should not change the vsync state as it didn't send the event
650 // yet
651 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
652 }
653
654 } // namespace
655 } // namespace android
656
657 // TODO(b/129481165): remove the #pragma below and fix conversion issues
658 #pragma clang diagnostic pop // ignored "-Wextra"
659