blob: 2662f52581ea4c301669d4578da087157e32f29e [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
17#undef LOG_TAG
18#define LOG_TAG "LibSurfaceFlingerUnittests"
19
20#include <gmock/gmock.h>
21#include <gtest/gtest.h>
22
23#include <log/log.h>
24
25#include <utils/Errors.h>
26
27#include "AsyncCallRecorder.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070028#include "Scheduler/EventThread.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080029
30using namespace std::chrono_literals;
31using namespace std::placeholders;
32
33using testing::_;
34using testing::Invoke;
35
36namespace android {
37namespace {
38
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080039constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111;
40constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222;
Ady Abrahamaf0ec272019-03-28 11:38:31 -070041constexpr PhysicalDisplayId DISPLAY_ID_64BIT = 0xabcd12349876fedcULL;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080042
Lloyd Pique24b0a482018-03-09 18:52:26 -080043class MockVSyncSource : public VSyncSource {
44public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070045 const char* getName() const override { return "test"; }
46
Lloyd Pique24b0a482018-03-09 18:52:26 -080047 MOCK_METHOD1(setVSyncEnabled, void(bool));
48 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
49 MOCK_METHOD1(setPhaseOffset, void(nsecs_t));
Ady Abrahamb838aed2019-02-12 15:30:16 -080050 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Lloyd Pique24b0a482018-03-09 18:52:26 -080051};
52
53} // namespace
54
55class EventThreadTest : public testing::Test {
56protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080057 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080058 public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070059 MockEventThreadConnection(impl::EventThread* eventThread, ResyncCallback&& resyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070060 ISurfaceComposer::ConfigChanged configChanged)
61 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080062 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
63 };
64
65 using ConnectionEventRecorder =
66 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
67
68 EventThreadTest();
69 ~EventThreadTest() override;
70
Dominik Laskowski6505f792019-09-18 11:10:05 -070071 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070072 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
73 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080074
75 void expectVSyncSetEnabledCallReceived(bool expectedState);
76 void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset);
77 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
78 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
79 void expectVsyncEventReceivedByConnection(const char* name,
80 ConnectionEventRecorder& connectionEventRecorder,
81 nsecs_t expectedTimestamp, unsigned expectedCount);
82 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080083 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070084 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080085 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
86 int32_t expectedConfigId);
Lloyd Pique24b0a482018-03-09 18:52:26 -080087
88 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
89 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
90 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
91 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
92 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
93 ConnectionEventRecorder mConnectionEventCallRecorder{0};
94
Dominik Laskowski6505f792019-09-18 11:10:05 -070095 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080096 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -070097 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080098 sp<MockEventThreadConnection> mConnection;
99};
100
101EventThreadTest::EventThreadTest() {
102 const ::testing::TestInfo* const test_info =
103 ::testing::UnitTest::GetInstance()->current_test_info();
104 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
105
Dominik Laskowski6505f792019-09-18 11:10:05 -0700106 auto vsyncSource = std::make_unique<MockVSyncSource>();
107 mVSyncSource = vsyncSource.get();
108
109 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800110 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
111
Dominik Laskowski6505f792019-09-18 11:10:05 -0700112 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800113 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
114
Dominik Laskowski6505f792019-09-18 11:10:05 -0700115 EXPECT_CALL(*mVSyncSource, setPhaseOffset(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800116 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
117
Dominik Laskowski6505f792019-09-18 11:10:05 -0700118 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700119 mConnection = createConnection(mConnectionEventCallRecorder,
120 ISurfaceComposer::eConfigChangedDispatch);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800121
122 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800123 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
124 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800125}
126
127EventThreadTest::~EventThreadTest() {
128 const ::testing::TestInfo* const test_info =
129 ::testing::UnitTest::GetInstance()->current_test_info();
130 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800131
132 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800133 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800134}
135
Dominik Laskowski6505f792019-09-18 11:10:05 -0700136void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
137 mThread = std::make_unique<impl::EventThread>(std::move(source),
138 mInterceptVSyncCallRecorder.getInvocable());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800139
140 // EventThread should register itself as VSyncSource callback.
141 mCallback = expectVSyncSetCallbackCallReceived();
142 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800143}
144
145sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700146 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800147 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700148 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
149 configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800150 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
151 return connection;
152}
153
154void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
155 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
156 ASSERT_TRUE(args.has_value());
157 EXPECT_EQ(expectedState, std::get<0>(args.value()));
158}
159
160void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
161 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
162 ASSERT_TRUE(args.has_value());
163 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
164}
165
166VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
167 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
168 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
169}
170
171void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
172 auto args = mInterceptVSyncCallRecorder.waitForCall();
173 ASSERT_TRUE(args.has_value());
174 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
175}
176
177void EventThreadTest::expectVsyncEventReceivedByConnection(
178 const char* name, ConnectionEventRecorder& connectionEventRecorder,
179 nsecs_t expectedTimestamp, unsigned expectedCount) {
180 auto args = connectionEventRecorder.waitForCall();
181 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
182 << expectedTimestamp;
183 const auto& event = std::get<0>(args.value());
184 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
185 << name << " did not get the correct event for timestamp " << expectedTimestamp;
186 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
187 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
188 EXPECT_EQ(expectedCount, event.vsync.count)
189 << name << " did not get the expected count for timestamp " << expectedTimestamp;
190}
191
192void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
193 unsigned expectedCount) {
194 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
195 mConnectionEventCallRecorder, expectedTimestamp,
196 expectedCount);
197}
198
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800199void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
200 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800201 auto args = mConnectionEventCallRecorder.waitForCall();
202 ASSERT_TRUE(args.has_value());
203 const auto& event = std::get<0>(args.value());
204 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800205 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800206 EXPECT_EQ(expectedConnected, event.hotplug.connected);
207}
208
Ady Abraham447052e2019-02-13 16:07:27 -0800209void EventThreadTest::expectConfigChangedEventReceivedByConnection(
210 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId) {
211 auto args = mConnectionEventCallRecorder.waitForCall();
212 ASSERT_TRUE(args.has_value());
213 const auto& event = std::get<0>(args.value());
214 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
215 EXPECT_EQ(expectedDisplayId, event.header.displayId);
216 EXPECT_EQ(expectedConfigId, event.config.configId);
217}
218
Lloyd Pique24b0a482018-03-09 18:52:26 -0800219namespace {
220
221/* ------------------------------------------------------------------------
222 * Test cases
223 */
224
225TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
226 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
227 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
228 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
229 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
230 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
231 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
232}
233
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800234TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800235 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
236 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800237
238 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700239 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800240
241 // EventThread should not enable vsync callbacks.
242 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800243}
244
Lloyd Pique24b0a482018-03-09 18:52:26 -0800245TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
246 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700247 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800248
Ady Abraham8532d012019-05-08 14:50:56 -0700249 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800250 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
251
Dominik Laskowski029cc122019-01-23 19:52:06 -0800252 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800253 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800254
255 // Use the received callback to signal a first vsync event.
256 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800257 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800258 expectInterceptCallReceived(123);
259 expectVsyncEventReceivedByConnection(123, 1u);
260
261 // Use the received callback to signal a second vsync event.
262 // The interceptor should receive the event, but the the connection should
263 // not as it was only interested in the first.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800264 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800265 expectInterceptCallReceived(456);
266 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
267
268 // EventThread should also detect that at this point that it does not need
269 // any more vsync events, and should disable their generation.
270 expectVSyncSetEnabledCallReceived(false);
271}
272
273TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
274 // Create a first connection, register it, and request a vsync rate of zero.
275 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700276 sp<MockEventThreadConnection> firstConnection =
277 createConnection(firstConnectionEventRecorder,
278 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800279 mThread->setVsyncRate(0, firstConnection);
280
281 // By itself, this should not enable vsync events
282 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
283 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
284
285 // However if there is another connection which wants events at a nonzero rate.....
286 ConnectionEventRecorder secondConnectionEventRecorder{0};
287 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700288 createConnection(secondConnectionEventRecorder,
289 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800290 mThread->setVsyncRate(1, secondConnection);
291
Dominik Laskowski029cc122019-01-23 19:52:06 -0800292 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800293 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800294
295 // Send a vsync event. EventThread should then make a call to the
296 // interceptor, and the second connection. The first connection should not
297 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800298 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800299 expectInterceptCallReceived(123);
300 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
301 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
302 1u);
303}
304
305TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
306 mThread->setVsyncRate(1, mConnection);
307
Dominik Laskowski029cc122019-01-23 19:52:06 -0800308 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800309 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800310
311 // Send a vsync event. EventThread should then make a call to the
312 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800313 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800314 expectInterceptCallReceived(123);
315 expectVsyncEventReceivedByConnection(123, 1u);
316
317 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800318 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800319 expectInterceptCallReceived(456);
320 expectVsyncEventReceivedByConnection(456, 2u);
321
322 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800323 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800324 expectInterceptCallReceived(789);
325 expectVsyncEventReceivedByConnection(789, 3u);
326}
327
328TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
329 mThread->setVsyncRate(2, mConnection);
330
Dominik Laskowski029cc122019-01-23 19:52:06 -0800331 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800332 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800333
334 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800335 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800336 expectInterceptCallReceived(123);
337 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
338
339 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800340 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800341 expectInterceptCallReceived(456);
342 expectVsyncEventReceivedByConnection(456, 2u);
343
344 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800345 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800346 expectInterceptCallReceived(789);
347 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
348
349 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800350 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351 expectInterceptCallReceived(101112);
352 expectVsyncEventReceivedByConnection(101112, 4u);
353}
354
355TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
356 mThread->setVsyncRate(1, mConnection);
357
Dominik Laskowski029cc122019-01-23 19:52:06 -0800358 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800359 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800360
361 // Destroy the only (strong) reference to the connection.
362 mConnection = nullptr;
363
364 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800365 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800366 expectInterceptCallReceived(123);
367 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
368
369 // EventThread should disable vsync callbacks
370 expectVSyncSetEnabledCallReceived(false);
371}
372
373TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
374 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700375 sp<MockEventThreadConnection> errorConnection =
376 createConnection(errorConnectionEventRecorder,
377 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800378 mThread->setVsyncRate(1, errorConnection);
379
Dominik Laskowski029cc122019-01-23 19:52:06 -0800380 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800382
383 // The first event will be seen by the interceptor, and by the connection,
384 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800385 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386 expectInterceptCallReceived(123);
387 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
388
389 // A subsequent event will be seen by the interceptor and not by the
390 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800391 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800392 expectInterceptCallReceived(456);
393 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
394
395 // EventThread should disable vsync callbacks with the second event
396 expectVSyncSetEnabledCallReceived(false);
397}
398
399TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
400 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700401 sp<MockEventThreadConnection> errorConnection =
402 createConnection(errorConnectionEventRecorder,
403 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800404 mThread->setVsyncRate(1, errorConnection);
405
Dominik Laskowski029cc122019-01-23 19:52:06 -0800406 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800408
409 // The first event will be seen by the interceptor, and by the connection,
410 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800411 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800412 expectInterceptCallReceived(123);
413 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
414
415 // A subsequent event will be seen by the interceptor, and by the connection,
416 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800417 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800418 expectInterceptCallReceived(456);
419 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
420
421 // EventThread will not disable vsync callbacks as the errors are non-fatal.
422 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
423}
424
425TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
426 mThread->setPhaseOffset(321);
427 expectVSyncSetPhaseOffsetCallReceived(321);
428}
429
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800430TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
431 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
432 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800433}
434
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800435TEST_F(EventThreadTest, postHotplugInternalConnect) {
436 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
437 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800438}
439
440TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800441 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
442 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443}
444
445TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800446 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
447 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800448}
449
Ady Abraham447052e2019-02-13 16:07:27 -0800450TEST_F(EventThreadTest, postConfigChangedPrimary) {
451 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 7);
452 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7);
453}
454
455TEST_F(EventThreadTest, postConfigChangedExternal) {
456 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, 5);
457 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5);
458}
459
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700460TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
461 mThread->onConfigChanged(DISPLAY_ID_64BIT, 7);
462 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7);
463}
464
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700465TEST_F(EventThreadTest, suppressConfigChanged) {
466 ConnectionEventRecorder suppressConnectionEventRecorder{0};
467 sp<MockEventThreadConnection> suppressConnection =
468 createConnection(suppressConnectionEventRecorder,
469 ISurfaceComposer::eConfigChangedSuppress);
470
471 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, 9);
472 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9);
473
474 auto args = suppressConnectionEventRecorder.waitForCall();
475 ASSERT_FALSE(args.has_value());
476}
477
Lloyd Pique24b0a482018-03-09 18:52:26 -0800478} // namespace
479} // namespace android