blob: 80bca021ab56ae73a37cb51dae91cd270881430f [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"
Ady Abraham2139f732019-11-13 18:56:40 -080029#include "Scheduler/HwcStrongTypes.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080030
31using namespace std::chrono_literals;
32using namespace std::placeholders;
33
34using testing::_;
35using testing::Invoke;
36
37namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080038
Lloyd Pique24b0a482018-03-09 18:52:26 -080039namespace {
40
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080041constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = 111;
42constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = 222;
Ady Abrahamaf0ec272019-03-28 11:38:31 -070043constexpr PhysicalDisplayId DISPLAY_ID_64BIT = 0xabcd12349876fedcULL;
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080044
Lloyd Pique24b0a482018-03-09 18:52:26 -080045class MockVSyncSource : public VSyncSource {
46public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070047 const char* getName() const override { return "test"; }
48
Lloyd Pique24b0a482018-03-09 18:52:26 -080049 MOCK_METHOD1(setVSyncEnabled, void(bool));
50 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
51 MOCK_METHOD1(setPhaseOffset, void(nsecs_t));
Ady Abrahamb838aed2019-02-12 15:30:16 -080052 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Lloyd Pique24b0a482018-03-09 18:52:26 -080053};
54
55} // namespace
56
57class EventThreadTest : public testing::Test {
58protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080059 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080060 public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070061 MockEventThreadConnection(impl::EventThread* eventThread, ResyncCallback&& resyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070062 ISurfaceComposer::ConfigChanged configChanged)
63 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080064 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
65 };
66
67 using ConnectionEventRecorder =
68 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
69
70 EventThreadTest();
71 ~EventThreadTest() override;
72
Dominik Laskowski6505f792019-09-18 11:10:05 -070073 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070074 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
75 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080076
77 void expectVSyncSetEnabledCallReceived(bool expectedState);
78 void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset);
79 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
80 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
81 void expectVsyncEventReceivedByConnection(const char* name,
82 ConnectionEventRecorder& connectionEventRecorder,
83 nsecs_t expectedTimestamp, unsigned expectedCount);
84 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080085 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070086 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080087 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
88 int32_t expectedConfigId);
Lloyd Pique24b0a482018-03-09 18:52:26 -080089
90 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
91 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
92 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
93 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
94 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
95 ConnectionEventRecorder mConnectionEventCallRecorder{0};
96
Dominik Laskowski6505f792019-09-18 11:10:05 -070097 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080098 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -070099 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800100 sp<MockEventThreadConnection> mConnection;
101};
102
103EventThreadTest::EventThreadTest() {
104 const ::testing::TestInfo* const test_info =
105 ::testing::UnitTest::GetInstance()->current_test_info();
106 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
107
Dominik Laskowski6505f792019-09-18 11:10:05 -0700108 auto vsyncSource = std::make_unique<MockVSyncSource>();
109 mVSyncSource = vsyncSource.get();
110
111 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800112 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
113
Dominik Laskowski6505f792019-09-18 11:10:05 -0700114 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800115 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
116
Dominik Laskowski6505f792019-09-18 11:10:05 -0700117 EXPECT_CALL(*mVSyncSource, setPhaseOffset(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800118 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
119
Dominik Laskowski6505f792019-09-18 11:10:05 -0700120 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700121 mConnection = createConnection(mConnectionEventCallRecorder,
122 ISurfaceComposer::eConfigChangedDispatch);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800123
124 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800125 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
126 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800127}
128
129EventThreadTest::~EventThreadTest() {
130 const ::testing::TestInfo* const test_info =
131 ::testing::UnitTest::GetInstance()->current_test_info();
132 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800133
134 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800135 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800136}
137
Dominik Laskowski6505f792019-09-18 11:10:05 -0700138void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
139 mThread = std::make_unique<impl::EventThread>(std::move(source),
140 mInterceptVSyncCallRecorder.getInvocable());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800141
142 // EventThread should register itself as VSyncSource callback.
143 mCallback = expectVSyncSetCallbackCallReceived();
144 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800145}
146
147sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700148 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800149 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700150 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
151 configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800152 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
153 return connection;
154}
155
156void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
157 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
158 ASSERT_TRUE(args.has_value());
159 EXPECT_EQ(expectedState, std::get<0>(args.value()));
160}
161
162void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
163 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
164 ASSERT_TRUE(args.has_value());
165 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
166}
167
168VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
169 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
170 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
171}
172
173void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
174 auto args = mInterceptVSyncCallRecorder.waitForCall();
175 ASSERT_TRUE(args.has_value());
176 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
177}
178
179void EventThreadTest::expectVsyncEventReceivedByConnection(
180 const char* name, ConnectionEventRecorder& connectionEventRecorder,
181 nsecs_t expectedTimestamp, unsigned expectedCount) {
182 auto args = connectionEventRecorder.waitForCall();
183 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
184 << expectedTimestamp;
185 const auto& event = std::get<0>(args.value());
186 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
187 << name << " did not get the correct event for timestamp " << expectedTimestamp;
188 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
189 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
190 EXPECT_EQ(expectedCount, event.vsync.count)
191 << name << " did not get the expected count for timestamp " << expectedTimestamp;
192}
193
194void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
195 unsigned expectedCount) {
196 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
197 mConnectionEventCallRecorder, expectedTimestamp,
198 expectedCount);
199}
200
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800201void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
202 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800203 auto args = mConnectionEventCallRecorder.waitForCall();
204 ASSERT_TRUE(args.has_value());
205 const auto& event = std::get<0>(args.value());
206 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800207 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800208 EXPECT_EQ(expectedConnected, event.hotplug.connected);
209}
210
Ady Abraham447052e2019-02-13 16:07:27 -0800211void EventThreadTest::expectConfigChangedEventReceivedByConnection(
212 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId) {
213 auto args = mConnectionEventCallRecorder.waitForCall();
214 ASSERT_TRUE(args.has_value());
215 const auto& event = std::get<0>(args.value());
216 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
217 EXPECT_EQ(expectedDisplayId, event.header.displayId);
218 EXPECT_EQ(expectedConfigId, event.config.configId);
219}
220
Lloyd Pique24b0a482018-03-09 18:52:26 -0800221namespace {
222
223/* ------------------------------------------------------------------------
224 * Test cases
225 */
226
227TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
228 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
229 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
230 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
231 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
232 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
233 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
234}
235
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800236TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800237 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
238 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800239
240 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700241 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800242
243 // EventThread should not enable vsync callbacks.
244 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800245}
246
Lloyd Pique24b0a482018-03-09 18:52:26 -0800247TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
248 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700249 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800250
Ady Abraham8532d012019-05-08 14:50:56 -0700251 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800252 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
253
Dominik Laskowski029cc122019-01-23 19:52:06 -0800254 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800255 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800256
257 // Use the received callback to signal a first vsync event.
258 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800259 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800260 expectInterceptCallReceived(123);
261 expectVsyncEventReceivedByConnection(123, 1u);
262
263 // Use the received callback to signal a second vsync event.
264 // The interceptor should receive the event, but the the connection should
265 // not as it was only interested in the first.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800266 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800267 expectInterceptCallReceived(456);
268 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
269
270 // EventThread should also detect that at this point that it does not need
271 // any more vsync events, and should disable their generation.
272 expectVSyncSetEnabledCallReceived(false);
273}
274
275TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
276 // Create a first connection, register it, and request a vsync rate of zero.
277 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700278 sp<MockEventThreadConnection> firstConnection =
279 createConnection(firstConnectionEventRecorder,
280 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800281 mThread->setVsyncRate(0, firstConnection);
282
283 // By itself, this should not enable vsync events
284 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
285 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
286
287 // However if there is another connection which wants events at a nonzero rate.....
288 ConnectionEventRecorder secondConnectionEventRecorder{0};
289 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700290 createConnection(secondConnectionEventRecorder,
291 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800292 mThread->setVsyncRate(1, secondConnection);
293
Dominik Laskowski029cc122019-01-23 19:52:06 -0800294 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800295 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800296
297 // Send a vsync event. EventThread should then make a call to the
298 // interceptor, and the second connection. The first connection should not
299 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800300 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800301 expectInterceptCallReceived(123);
302 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
303 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
304 1u);
305}
306
307TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
308 mThread->setVsyncRate(1, mConnection);
309
Dominik Laskowski029cc122019-01-23 19:52:06 -0800310 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800311 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800312
313 // Send a vsync event. EventThread should then make a call to the
314 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800315 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800316 expectInterceptCallReceived(123);
317 expectVsyncEventReceivedByConnection(123, 1u);
318
319 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800320 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800321 expectInterceptCallReceived(456);
322 expectVsyncEventReceivedByConnection(456, 2u);
323
324 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800325 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800326 expectInterceptCallReceived(789);
327 expectVsyncEventReceivedByConnection(789, 3u);
328}
329
330TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
331 mThread->setVsyncRate(2, mConnection);
332
Dominik Laskowski029cc122019-01-23 19:52:06 -0800333 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800334 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800335
336 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800337 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338 expectInterceptCallReceived(123);
339 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
340
341 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800342 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343 expectInterceptCallReceived(456);
344 expectVsyncEventReceivedByConnection(456, 2u);
345
346 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800347 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800348 expectInterceptCallReceived(789);
349 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
350
351 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800352 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800353 expectInterceptCallReceived(101112);
354 expectVsyncEventReceivedByConnection(101112, 4u);
355}
356
357TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
358 mThread->setVsyncRate(1, mConnection);
359
Dominik Laskowski029cc122019-01-23 19:52:06 -0800360 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800361 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800362
363 // Destroy the only (strong) reference to the connection.
364 mConnection = nullptr;
365
366 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800367 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800368 expectInterceptCallReceived(123);
369 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
370
371 // EventThread should disable vsync callbacks
372 expectVSyncSetEnabledCallReceived(false);
373}
374
375TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
376 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700377 sp<MockEventThreadConnection> errorConnection =
378 createConnection(errorConnectionEventRecorder,
379 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800380 mThread->setVsyncRate(1, errorConnection);
381
Dominik Laskowski029cc122019-01-23 19:52:06 -0800382 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800383 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800384
385 // The first event will be seen by the interceptor, and by the connection,
386 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800387 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800388 expectInterceptCallReceived(123);
389 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
390
391 // A subsequent event will be seen by the interceptor and not by the
392 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800393 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800394 expectInterceptCallReceived(456);
395 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
396
397 // EventThread should disable vsync callbacks with the second event
398 expectVSyncSetEnabledCallReceived(false);
399}
400
401TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
402 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700403 sp<MockEventThreadConnection> errorConnection =
404 createConnection(errorConnectionEventRecorder,
405 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406 mThread->setVsyncRate(1, errorConnection);
407
Dominik Laskowski029cc122019-01-23 19:52:06 -0800408 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800409 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800410
411 // The first event will be seen by the interceptor, and by the connection,
412 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800413 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800414 expectInterceptCallReceived(123);
415 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
416
417 // A subsequent event will be seen by the interceptor, and by the connection,
418 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800419 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800420 expectInterceptCallReceived(456);
421 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
422
423 // EventThread will not disable vsync callbacks as the errors are non-fatal.
424 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
425}
426
427TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
428 mThread->setPhaseOffset(321);
429 expectVSyncSetPhaseOffsetCallReceived(321);
430}
431
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800432TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
433 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
434 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800435}
436
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800437TEST_F(EventThreadTest, postHotplugInternalConnect) {
438 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
439 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800440}
441
442TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800443 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
444 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800445}
446
447TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800448 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
449 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800450}
451
Ady Abraham447052e2019-02-13 16:07:27 -0800452TEST_F(EventThreadTest, postConfigChangedPrimary) {
Ady Abraham2139f732019-11-13 18:56:40 -0800453 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7));
Ady Abraham447052e2019-02-13 16:07:27 -0800454 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7);
455}
456
457TEST_F(EventThreadTest, postConfigChangedExternal) {
Ady Abraham2139f732019-11-13 18:56:40 -0800458 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5));
Ady Abraham447052e2019-02-13 16:07:27 -0800459 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5);
460}
461
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700462TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Ady Abraham2139f732019-11-13 18:56:40 -0800463 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7));
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700464 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7);
465}
466
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700467TEST_F(EventThreadTest, suppressConfigChanged) {
468 ConnectionEventRecorder suppressConnectionEventRecorder{0};
469 sp<MockEventThreadConnection> suppressConnection =
470 createConnection(suppressConnectionEventRecorder,
471 ISurfaceComposer::eConfigChangedSuppress);
472
Ady Abraham2139f732019-11-13 18:56:40 -0800473 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700474 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9);
475
476 auto args = suppressConnectionEventRecorder.waitForCall();
477 ASSERT_FALSE(args.has_value());
478}
479
Lloyd Pique24b0a482018-03-09 18:52:26 -0800480} // namespace
481} // namespace android