blob: 65b3e35d3258313e137719bace331d95acfa40fb [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
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,
Alec Mouri60aee1c2019-10-28 16:18:59 -070086 int32_t expectedConfigId,
87 nsecs_t expectedVsyncPeriod);
Lloyd Pique24b0a482018-03-09 18:52:26 -080088
89 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
90 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
91 AsyncCallRecorder<void (*)(nsecs_t)> mVSyncSetPhaseOffsetCallRecorder;
92 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
93 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
94 ConnectionEventRecorder mConnectionEventCallRecorder{0};
95
Dominik Laskowski6505f792019-09-18 11:10:05 -070096 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -080097 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -070098 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -080099 sp<MockEventThreadConnection> mConnection;
100};
101
102EventThreadTest::EventThreadTest() {
103 const ::testing::TestInfo* const test_info =
104 ::testing::UnitTest::GetInstance()->current_test_info();
105 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
106
Dominik Laskowski6505f792019-09-18 11:10:05 -0700107 auto vsyncSource = std::make_unique<MockVSyncSource>();
108 mVSyncSource = vsyncSource.get();
109
110 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800111 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
112
Dominik Laskowski6505f792019-09-18 11:10:05 -0700113 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800114 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
115
Dominik Laskowski6505f792019-09-18 11:10:05 -0700116 EXPECT_CALL(*mVSyncSource, setPhaseOffset(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800117 .WillRepeatedly(Invoke(mVSyncSetPhaseOffsetCallRecorder.getInvocable()));
118
Dominik Laskowski6505f792019-09-18 11:10:05 -0700119 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700120 mConnection = createConnection(mConnectionEventCallRecorder,
121 ISurfaceComposer::eConfigChangedDispatch);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800122
123 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800124 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
125 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800126}
127
128EventThreadTest::~EventThreadTest() {
129 const ::testing::TestInfo* const test_info =
130 ::testing::UnitTest::GetInstance()->current_test_info();
131 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800132
133 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800134 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800135}
136
Dominik Laskowski6505f792019-09-18 11:10:05 -0700137void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
138 mThread = std::make_unique<impl::EventThread>(std::move(source),
139 mInterceptVSyncCallRecorder.getInvocable());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800140
141 // EventThread should register itself as VSyncSource callback.
142 mCallback = expectVSyncSetCallbackCallReceived();
143 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144}
145
146sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700147 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800148 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700149 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
150 configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800151 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
152 return connection;
153}
154
155void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
156 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
157 ASSERT_TRUE(args.has_value());
158 EXPECT_EQ(expectedState, std::get<0>(args.value()));
159}
160
161void EventThreadTest::expectVSyncSetPhaseOffsetCallReceived(nsecs_t expectedPhaseOffset) {
162 auto args = mVSyncSetPhaseOffsetCallRecorder.waitForCall();
163 ASSERT_TRUE(args.has_value());
164 EXPECT_EQ(expectedPhaseOffset, std::get<0>(args.value()));
165}
166
167VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
168 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
169 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
170}
171
172void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
173 auto args = mInterceptVSyncCallRecorder.waitForCall();
174 ASSERT_TRUE(args.has_value());
175 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
176}
177
178void EventThreadTest::expectVsyncEventReceivedByConnection(
179 const char* name, ConnectionEventRecorder& connectionEventRecorder,
180 nsecs_t expectedTimestamp, unsigned expectedCount) {
181 auto args = connectionEventRecorder.waitForCall();
182 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
183 << expectedTimestamp;
184 const auto& event = std::get<0>(args.value());
185 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
186 << name << " did not get the correct event for timestamp " << expectedTimestamp;
187 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
188 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
189 EXPECT_EQ(expectedCount, event.vsync.count)
190 << name << " did not get the expected count for timestamp " << expectedTimestamp;
191}
192
193void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
194 unsigned expectedCount) {
195 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
196 mConnectionEventCallRecorder, expectedTimestamp,
197 expectedCount);
198}
199
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800200void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
201 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800202 auto args = mConnectionEventCallRecorder.waitForCall();
203 ASSERT_TRUE(args.has_value());
204 const auto& event = std::get<0>(args.value());
205 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800206 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800207 EXPECT_EQ(expectedConnected, event.hotplug.connected);
208}
209
Ady Abraham447052e2019-02-13 16:07:27 -0800210void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700211 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
212 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800213 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);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700219 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800220}
221
Lloyd Pique24b0a482018-03-09 18:52:26 -0800222namespace {
223
224/* ------------------------------------------------------------------------
225 * Test cases
226 */
227
228TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
229 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
230 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
231 EXPECT_FALSE(mVSyncSetPhaseOffsetCallRecorder.waitForCall(0us).has_value());
232 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
233 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
234 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
235}
236
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800237TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800238 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
239 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800240
241 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700242 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800243
244 // EventThread should not enable vsync callbacks.
245 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800246}
247
Lloyd Pique24b0a482018-03-09 18:52:26 -0800248TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
249 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700250 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800251
Ady Abraham8532d012019-05-08 14:50:56 -0700252 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800253 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
254
Dominik Laskowski029cc122019-01-23 19:52:06 -0800255 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800256 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800257
258 // Use the received callback to signal a first vsync event.
259 // The interceptor should receive the event, as well as the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800260 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800261 expectInterceptCallReceived(123);
262 expectVsyncEventReceivedByConnection(123, 1u);
263
264 // Use the received callback to signal a second vsync event.
265 // The interceptor should receive the event, but the the connection should
266 // not as it was only interested in the first.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800267 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800268 expectInterceptCallReceived(456);
269 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
270
271 // EventThread should also detect that at this point that it does not need
272 // any more vsync events, and should disable their generation.
273 expectVSyncSetEnabledCallReceived(false);
274}
275
276TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
277 // Create a first connection, register it, and request a vsync rate of zero.
278 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700279 sp<MockEventThreadConnection> firstConnection =
280 createConnection(firstConnectionEventRecorder,
281 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800282 mThread->setVsyncRate(0, firstConnection);
283
284 // By itself, this should not enable vsync events
285 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
286 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
287
288 // However if there is another connection which wants events at a nonzero rate.....
289 ConnectionEventRecorder secondConnectionEventRecorder{0};
290 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700291 createConnection(secondConnectionEventRecorder,
292 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800293 mThread->setVsyncRate(1, secondConnection);
294
Dominik Laskowski029cc122019-01-23 19:52:06 -0800295 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800296 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800297
298 // Send a vsync event. EventThread should then make a call to the
299 // interceptor, and the second connection. The first connection should not
300 // get the event.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800301 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800302 expectInterceptCallReceived(123);
303 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
304 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
305 1u);
306}
307
308TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
309 mThread->setVsyncRate(1, mConnection);
310
Dominik Laskowski029cc122019-01-23 19:52:06 -0800311 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800312 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800313
314 // Send a vsync event. EventThread should then make a call to the
315 // interceptor, and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800316 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317 expectInterceptCallReceived(123);
318 expectVsyncEventReceivedByConnection(123, 1u);
319
320 // A second event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800321 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 expectInterceptCallReceived(456);
323 expectVsyncEventReceivedByConnection(456, 2u);
324
325 // A third event should go to the same places.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800326 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800327 expectInterceptCallReceived(789);
328 expectVsyncEventReceivedByConnection(789, 3u);
329}
330
331TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
332 mThread->setVsyncRate(2, mConnection);
333
Dominik Laskowski029cc122019-01-23 19:52:06 -0800334 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800335 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800336
337 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800338 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800339 expectInterceptCallReceived(123);
340 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
341
342 // The second event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800343 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800344 expectInterceptCallReceived(456);
345 expectVsyncEventReceivedByConnection(456, 2u);
346
347 // The third event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800348 mCallback->onVSyncEvent(789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800349 expectInterceptCallReceived(789);
350 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
351
352 // The fourth event will be seen by the interceptor and the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800353 mCallback->onVSyncEvent(101112);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800354 expectInterceptCallReceived(101112);
355 expectVsyncEventReceivedByConnection(101112, 4u);
356}
357
358TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
359 mThread->setVsyncRate(1, mConnection);
360
Dominik Laskowski029cc122019-01-23 19:52:06 -0800361 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800362 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800363
364 // Destroy the only (strong) reference to the connection.
365 mConnection = nullptr;
366
367 // The first event will be seen by the interceptor, and not the connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800368 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800369 expectInterceptCallReceived(123);
370 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
371
372 // EventThread should disable vsync callbacks
373 expectVSyncSetEnabledCallReceived(false);
374}
375
376TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
377 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700378 sp<MockEventThreadConnection> errorConnection =
379 createConnection(errorConnectionEventRecorder,
380 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381 mThread->setVsyncRate(1, errorConnection);
382
Dominik Laskowski029cc122019-01-23 19:52:06 -0800383 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800384 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385
386 // The first event will be seen by the interceptor, and by the connection,
387 // which then returns an error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800388 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800389 expectInterceptCallReceived(123);
390 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
391
392 // A subsequent event will be seen by the interceptor and not by the
393 // connection.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800394 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800395 expectInterceptCallReceived(456);
396 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
397
398 // EventThread should disable vsync callbacks with the second event
399 expectVSyncSetEnabledCallReceived(false);
400}
401
Alec Mouri717bcb62020-02-10 17:07:19 -0800402TEST_F(EventThreadTest, tracksEventConnections) {
403 EXPECT_EQ(1, mThread->getEventThreadConnectionCount());
404 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
405 sp<MockEventThreadConnection> errorConnection =
406 createConnection(errorConnectionEventRecorder,
407 ISurfaceComposer::eConfigChangedSuppress);
408 mThread->setVsyncRate(1, errorConnection);
409 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
410 ConnectionEventRecorder secondConnectionEventRecorder{0};
411 sp<MockEventThreadConnection> secondConnection =
412 createConnection(secondConnectionEventRecorder,
413 ISurfaceComposer::eConfigChangedSuppress);
414 mThread->setVsyncRate(1, secondConnection);
415 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
416
417 // EventThread should enable vsync callbacks.
418 expectVSyncSetEnabledCallReceived(true);
419
420 // The first event will be seen by the interceptor, and by the connection,
421 // which then returns an error.
422 mCallback->onVSyncEvent(123);
423 expectInterceptCallReceived(123);
424 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
425 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
426 1u);
427 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
428}
429
Lloyd Pique24b0a482018-03-09 18:52:26 -0800430TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
431 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700432 sp<MockEventThreadConnection> errorConnection =
433 createConnection(errorConnectionEventRecorder,
434 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800435 mThread->setVsyncRate(1, errorConnection);
436
Dominik Laskowski029cc122019-01-23 19:52:06 -0800437 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800438 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800439
440 // The first event will be seen by the interceptor, and by the connection,
441 // which then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800442 mCallback->onVSyncEvent(123);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443 expectInterceptCallReceived(123);
444 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
445
446 // A subsequent event will be seen by the interceptor, and by the connection,
447 // which still then returns an non-fatal error.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800448 mCallback->onVSyncEvent(456);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800449 expectInterceptCallReceived(456);
450 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
451
452 // EventThread will not disable vsync callbacks as the errors are non-fatal.
453 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
454}
455
456TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
457 mThread->setPhaseOffset(321);
458 expectVSyncSetPhaseOffsetCallReceived(321);
459}
460
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800461TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
462 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
463 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800464}
465
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800466TEST_F(EventThreadTest, postHotplugInternalConnect) {
467 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
468 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800469}
470
471TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800472 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
473 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800474}
475
476TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800477 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
478 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800479}
480
Ady Abraham447052e2019-02-13 16:07:27 -0800481TEST_F(EventThreadTest, postConfigChangedPrimary) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700482 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7), 16666666);
483 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800484}
485
486TEST_F(EventThreadTest, postConfigChangedExternal) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700487 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5), 16666666);
488 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800489}
490
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700491TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700492 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7), 16666666);
493 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700494}
495
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700496TEST_F(EventThreadTest, suppressConfigChanged) {
497 ConnectionEventRecorder suppressConnectionEventRecorder{0};
498 sp<MockEventThreadConnection> suppressConnection =
499 createConnection(suppressConnectionEventRecorder,
500 ISurfaceComposer::eConfigChangedSuppress);
501
Alec Mouri60aee1c2019-10-28 16:18:59 -0700502 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9), 16666666);
503 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700504
505 auto args = suppressConnectionEventRecorder.waitForCall();
506 ASSERT_FALSE(args.has_value());
507}
508
Lloyd Pique24b0a482018-03-09 18:52:26 -0800509} // namespace
510} // namespace android