blob: aab6d01f01e291f6a935dc666e40831596d7c9b2 [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>
Lloyd Pique24b0a482018-03-09 18:52:26 -080022#include <log/log.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080023#include <utils/Errors.h>
24
25#include "AsyncCallRecorder.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070026#include "Scheduler/EventThread.h"
Ady Abraham2139f732019-11-13 18:56:40 -080027#include "Scheduler/HwcStrongTypes.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080028
29using namespace std::chrono_literals;
30using namespace std::placeholders;
31
32using testing::_;
33using testing::Invoke;
34
35namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080036
Lloyd Pique24b0a482018-03-09 18:52:26 -080037namespace {
38
Marin Shalamanova524a092020-07-27 21:39:55 +020039constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111);
40constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222);
41constexpr 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));
Ady Abraham5e7371c2020-03-24 14:47:24 -070051 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080052};
53
54} // namespace
55
56class EventThreadTest : public testing::Test {
57protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080058 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080059 public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070060 MockEventThreadConnection(impl::EventThread* eventThread, ResyncCallback&& resyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070061 ISurfaceComposer::ConfigChanged configChanged)
62 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080063 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
64 };
65
66 using ConnectionEventRecorder =
67 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
68
69 EventThreadTest();
70 ~EventThreadTest() override;
71
Dominik Laskowski6505f792019-09-18 11:10:05 -070072 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070073 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
74 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080075
76 void expectVSyncSetEnabledCallReceived(bool expectedState);
77 void expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset);
78 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
79 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
80 void expectVsyncEventReceivedByConnection(const char* name,
81 ConnectionEventRecorder& connectionEventRecorder,
82 nsecs_t expectedTimestamp, unsigned expectedCount);
83 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080084 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070085 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080086 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070087 int32_t expectedConfigId,
88 nsecs_t expectedVsyncPeriod);
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(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700212 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
213 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800214 auto args = mConnectionEventCallRecorder.waitForCall();
215 ASSERT_TRUE(args.has_value());
216 const auto& event = std::get<0>(args.value());
217 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
218 EXPECT_EQ(expectedDisplayId, event.header.displayId);
219 EXPECT_EQ(expectedConfigId, event.config.configId);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700220 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800221}
222
Lloyd Pique24b0a482018-03-09 18:52:26 -0800223namespace {
224
225/* ------------------------------------------------------------------------
226 * Test cases
227 */
228
229TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
230 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
231 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
232 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
233 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
234 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
235 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
236}
237
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800238TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800239 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
240 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800241
242 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700243 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800244
245 // EventThread should not enable vsync callbacks.
246 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800247}
248
Lloyd Pique24b0a482018-03-09 18:52:26 -0800249TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
250 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700251 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800252
Ady Abraham8532d012019-05-08 14:50:56 -0700253 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800254 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
255
Dominik Laskowski029cc122019-01-23 19:52:06 -0800256 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800257 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800258
259 // Use the received callback to signal a first vsync event.
260 // The interceptor should receive the event, as well as the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700261 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800262 expectInterceptCallReceived(123);
263 expectVsyncEventReceivedByConnection(123, 1u);
264
265 // Use the received callback to signal a second vsync event.
266 // The interceptor should receive the event, but the the connection should
267 // not as it was only interested in the first.
Ady Abraham5facfb12020-04-22 15:18:31 -0700268 mCallback->onVSyncEvent(456, 123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800269 expectInterceptCallReceived(456);
270 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
271
272 // EventThread should also detect that at this point that it does not need
273 // any more vsync events, and should disable their generation.
274 expectVSyncSetEnabledCallReceived(false);
275}
276
277TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
278 // Create a first connection, register it, and request a vsync rate of zero.
279 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700280 sp<MockEventThreadConnection> firstConnection =
281 createConnection(firstConnectionEventRecorder,
282 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800283 mThread->setVsyncRate(0, firstConnection);
284
285 // By itself, this should not enable vsync events
286 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
287 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
288
289 // However if there is another connection which wants events at a nonzero rate.....
290 ConnectionEventRecorder secondConnectionEventRecorder{0};
291 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700292 createConnection(secondConnectionEventRecorder,
293 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800294 mThread->setVsyncRate(1, secondConnection);
295
Dominik Laskowski029cc122019-01-23 19:52:06 -0800296 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800297 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800298
299 // Send a vsync event. EventThread should then make a call to the
300 // interceptor, and the second connection. The first connection should not
301 // get the event.
Ady Abraham5facfb12020-04-22 15:18:31 -0700302 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800303 expectInterceptCallReceived(123);
304 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
305 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
306 1u);
307}
308
309TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
310 mThread->setVsyncRate(1, mConnection);
311
Dominik Laskowski029cc122019-01-23 19:52:06 -0800312 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800313 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800314
315 // Send a vsync event. EventThread should then make a call to the
316 // interceptor, and the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700317 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800318 expectInterceptCallReceived(123);
319 expectVsyncEventReceivedByConnection(123, 1u);
320
321 // A second event should go to the same places.
Ady Abraham5facfb12020-04-22 15:18:31 -0700322 mCallback->onVSyncEvent(456, 123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800323 expectInterceptCallReceived(456);
324 expectVsyncEventReceivedByConnection(456, 2u);
325
326 // A third event should go to the same places.
Ady Abraham5facfb12020-04-22 15:18:31 -0700327 mCallback->onVSyncEvent(789, 777);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800328 expectInterceptCallReceived(789);
329 expectVsyncEventReceivedByConnection(789, 3u);
330}
331
332TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
333 mThread->setVsyncRate(2, mConnection);
334
Dominik Laskowski029cc122019-01-23 19:52:06 -0800335 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800336 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800337
338 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700339 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800340 expectInterceptCallReceived(123);
341 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
342
343 // The second event will be seen by the interceptor and the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700344 mCallback->onVSyncEvent(456, 123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800345 expectInterceptCallReceived(456);
346 expectVsyncEventReceivedByConnection(456, 2u);
347
348 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700349 mCallback->onVSyncEvent(789, 777);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800350 expectInterceptCallReceived(789);
351 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
352
353 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700354 mCallback->onVSyncEvent(101112, 7847);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800355 expectInterceptCallReceived(101112);
356 expectVsyncEventReceivedByConnection(101112, 4u);
357}
358
359TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
360 mThread->setVsyncRate(1, mConnection);
361
Dominik Laskowski029cc122019-01-23 19:52:06 -0800362 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800363 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364
365 // Destroy the only (strong) reference to the connection.
366 mConnection = nullptr;
367
368 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700369 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 expectInterceptCallReceived(123);
371 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
372
373 // EventThread should disable vsync callbacks
374 expectVSyncSetEnabledCallReceived(false);
375}
376
377TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
378 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700379 sp<MockEventThreadConnection> errorConnection =
380 createConnection(errorConnectionEventRecorder,
381 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800382 mThread->setVsyncRate(1, errorConnection);
383
Dominik Laskowski029cc122019-01-23 19:52:06 -0800384 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800386
387 // The first event will be seen by the interceptor, and by the connection,
388 // which then returns an error.
Ady Abraham5facfb12020-04-22 15:18:31 -0700389 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800390 expectInterceptCallReceived(123);
391 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
392
393 // A subsequent event will be seen by the interceptor and not by the
394 // connection.
Ady Abraham5facfb12020-04-22 15:18:31 -0700395 mCallback->onVSyncEvent(456, 123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800396 expectInterceptCallReceived(456);
397 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
398
399 // EventThread should disable vsync callbacks with the second event
400 expectVSyncSetEnabledCallReceived(false);
401}
402
Alec Mouri717bcb62020-02-10 17:07:19 -0800403TEST_F(EventThreadTest, tracksEventConnections) {
404 EXPECT_EQ(1, mThread->getEventThreadConnectionCount());
405 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
406 sp<MockEventThreadConnection> errorConnection =
407 createConnection(errorConnectionEventRecorder,
408 ISurfaceComposer::eConfigChangedSuppress);
409 mThread->setVsyncRate(1, errorConnection);
410 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
411 ConnectionEventRecorder secondConnectionEventRecorder{0};
412 sp<MockEventThreadConnection> secondConnection =
413 createConnection(secondConnectionEventRecorder,
414 ISurfaceComposer::eConfigChangedSuppress);
415 mThread->setVsyncRate(1, secondConnection);
416 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
417
418 // EventThread should enable vsync callbacks.
419 expectVSyncSetEnabledCallReceived(true);
420
421 // The first event will be seen by the interceptor, and by the connection,
422 // which then returns an error.
Ady Abraham5facfb12020-04-22 15:18:31 -0700423 mCallback->onVSyncEvent(123, 456);
Alec Mouri717bcb62020-02-10 17:07:19 -0800424 expectInterceptCallReceived(123);
425 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
426 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
427 1u);
428 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
429}
430
Lloyd Pique24b0a482018-03-09 18:52:26 -0800431TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
432 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700433 sp<MockEventThreadConnection> errorConnection =
434 createConnection(errorConnectionEventRecorder,
435 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436 mThread->setVsyncRate(1, errorConnection);
437
Dominik Laskowski029cc122019-01-23 19:52:06 -0800438 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800439 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800440
441 // The first event will be seen by the interceptor, and by the connection,
442 // which then returns an non-fatal error.
Ady Abraham5facfb12020-04-22 15:18:31 -0700443 mCallback->onVSyncEvent(123, 456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800444 expectInterceptCallReceived(123);
445 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
446
447 // A subsequent event will be seen by the interceptor, and by the connection,
448 // which still then returns an non-fatal error.
Ady Abraham5facfb12020-04-22 15:18:31 -0700449 mCallback->onVSyncEvent(456, 123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800450 expectInterceptCallReceived(456);
451 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
452
453 // EventThread will not disable vsync callbacks as the errors are non-fatal.
454 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
455}
456
457TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
458 mThread->setPhaseOffset(321);
459 expectVSyncSetPhaseOffsetCallReceived(321);
460}
461
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800462TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
463 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
464 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800465}
466
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800467TEST_F(EventThreadTest, postHotplugInternalConnect) {
468 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
469 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800470}
471
472TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800473 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
474 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800475}
476
477TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800478 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
479 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800480}
481
Ady Abraham447052e2019-02-13 16:07:27 -0800482TEST_F(EventThreadTest, postConfigChangedPrimary) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700483 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7), 16666666);
484 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800485}
486
487TEST_F(EventThreadTest, postConfigChangedExternal) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700488 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5), 16666666);
489 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800490}
491
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700492TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700493 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7), 16666666);
494 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700495}
496
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700497TEST_F(EventThreadTest, suppressConfigChanged) {
498 ConnectionEventRecorder suppressConnectionEventRecorder{0};
499 sp<MockEventThreadConnection> suppressConnection =
500 createConnection(suppressConnectionEventRecorder,
501 ISurfaceComposer::eConfigChangedSuppress);
502
Alec Mouri60aee1c2019-10-28 16:18:59 -0700503 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9), 16666666);
504 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700505
506 auto args = suppressConnectionEventRecorder.waitForCall();
507 ASSERT_FALSE(args.has_value());
508}
509
Lloyd Pique24b0a482018-03-09 18:52:26 -0800510} // namespace
511} // namespace android