blob: ae94f16f5b77147d8d6b648d0b54a39438ebfd2a [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*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070049 MOCK_METHOD2(setDuration,
50 void(std::chrono::nanoseconds workDuration,
51 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080052 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Ady Abraham5e7371c2020-03-24 14:47:24 -070053 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080054};
55
56} // namespace
57
58class EventThreadTest : public testing::Test {
59protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080060 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080061 public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070062 MockEventThreadConnection(impl::EventThread* eventThread, ResyncCallback&& resyncCallback,
Ady Abraham0f4a1b12019-06-04 16:04:04 -070063 ISurfaceComposer::ConfigChanged configChanged)
64 : EventThreadConnection(eventThread, std::move(resyncCallback), configChanged) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080065 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
66 };
67
68 using ConnectionEventRecorder =
69 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
70
71 EventThreadTest();
72 ~EventThreadTest() override;
73
Dominik Laskowski6505f792019-09-18 11:10:05 -070074 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070075 sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder,
76 ISurfaceComposer::ConfigChanged configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -080077
78 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070079 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
80 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080081 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
82 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
83 void expectVsyncEventReceivedByConnection(const char* name,
84 ConnectionEventRecorder& connectionEventRecorder,
85 nsecs_t expectedTimestamp, unsigned expectedCount);
86 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080087 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070088 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080089 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070090 int32_t expectedConfigId,
91 nsecs_t expectedVsyncPeriod);
Lloyd Pique24b0a482018-03-09 18:52:26 -080092
93 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
94 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -070095 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
96 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -080097 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
98 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
99 ConnectionEventRecorder mConnectionEventCallRecorder{0};
100
Dominik Laskowski6505f792019-09-18 11:10:05 -0700101 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800102 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700103 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800104 sp<MockEventThreadConnection> mConnection;
105};
106
107EventThreadTest::EventThreadTest() {
108 const ::testing::TestInfo* const test_info =
109 ::testing::UnitTest::GetInstance()->current_test_info();
110 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
111
Dominik Laskowski6505f792019-09-18 11:10:05 -0700112 auto vsyncSource = std::make_unique<MockVSyncSource>();
113 mVSyncSource = vsyncSource.get();
114
115 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800116 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
117
Dominik Laskowski6505f792019-09-18 11:10:05 -0700118 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800119 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
120
Ady Abraham9c53ee72020-07-22 21:16:18 -0700121 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
122 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123
Dominik Laskowski6505f792019-09-18 11:10:05 -0700124 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700125 mConnection = createConnection(mConnectionEventCallRecorder,
126 ISurfaceComposer::eConfigChangedDispatch);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800127
128 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800129 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
130 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800131}
132
133EventThreadTest::~EventThreadTest() {
134 const ::testing::TestInfo* const test_info =
135 ::testing::UnitTest::GetInstance()->current_test_info();
136 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800137
138 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800139 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800140}
141
Dominik Laskowski6505f792019-09-18 11:10:05 -0700142void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
143 mThread = std::make_unique<impl::EventThread>(std::move(source),
144 mInterceptVSyncCallRecorder.getInvocable());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800145
146 // EventThread should register itself as VSyncSource callback.
147 mCallback = expectVSyncSetCallbackCallReceived();
148 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800149}
150
151sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700152 ConnectionEventRecorder& recorder, ISurfaceComposer::ConfigChanged configChanged) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800153 sp<MockEventThreadConnection> connection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700154 new MockEventThreadConnection(mThread.get(), mResyncCallRecorder.getInvocable(),
155 configChanged);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800156 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
157 return connection;
158}
159
160void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
161 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
162 ASSERT_TRUE(args.has_value());
163 EXPECT_EQ(expectedState, std::get<0>(args.value()));
164}
165
Ady Abraham9c53ee72020-07-22 21:16:18 -0700166void EventThreadTest::expectVSyncSetDurationCallReceived(
167 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
168 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800169 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700170 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
171 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800172}
173
174VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
175 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
176 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
177}
178
179void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
180 auto args = mInterceptVSyncCallRecorder.waitForCall();
181 ASSERT_TRUE(args.has_value());
182 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
183}
184
185void EventThreadTest::expectVsyncEventReceivedByConnection(
186 const char* name, ConnectionEventRecorder& connectionEventRecorder,
187 nsecs_t expectedTimestamp, unsigned expectedCount) {
188 auto args = connectionEventRecorder.waitForCall();
189 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
190 << expectedTimestamp;
191 const auto& event = std::get<0>(args.value());
192 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
193 << name << " did not get the correct event for timestamp " << expectedTimestamp;
194 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
195 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
196 EXPECT_EQ(expectedCount, event.vsync.count)
197 << name << " did not get the expected count for timestamp " << expectedTimestamp;
198}
199
200void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
201 unsigned expectedCount) {
202 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
203 mConnectionEventCallRecorder, expectedTimestamp,
204 expectedCount);
205}
206
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800207void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
208 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800209 auto args = mConnectionEventCallRecorder.waitForCall();
210 ASSERT_TRUE(args.has_value());
211 const auto& event = std::get<0>(args.value());
212 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800213 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800214 EXPECT_EQ(expectedConnected, event.hotplug.connected);
215}
216
Ady Abraham447052e2019-02-13 16:07:27 -0800217void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700218 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
219 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800220 auto args = mConnectionEventCallRecorder.waitForCall();
221 ASSERT_TRUE(args.has_value());
222 const auto& event = std::get<0>(args.value());
223 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, event.header.type);
224 EXPECT_EQ(expectedDisplayId, event.header.displayId);
225 EXPECT_EQ(expectedConfigId, event.config.configId);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700226 EXPECT_EQ(expectedVsyncPeriod, event.config.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800227}
228
Lloyd Pique24b0a482018-03-09 18:52:26 -0800229namespace {
230
231/* ------------------------------------------------------------------------
232 * Test cases
233 */
234
235TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
236 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
237 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700238 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800239 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
240 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
241 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
242}
243
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800244TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800245 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
246 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800247
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);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800250
251 // EventThread should not enable vsync callbacks.
252 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800253}
254
Lloyd Pique24b0a482018-03-09 18:52:26 -0800255TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
256 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700257 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800258
Ady Abraham8532d012019-05-08 14:50:56 -0700259 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800260 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
261
Dominik Laskowski029cc122019-01-23 19:52:06 -0800262 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800263 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800264
265 // Use the received callback to signal a first vsync event.
266 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700267 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800268 expectInterceptCallReceived(123);
269 expectVsyncEventReceivedByConnection(123, 1u);
270
271 // Use the received callback to signal a second vsync event.
272 // The interceptor should receive the event, but the the connection should
273 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700274 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800275 expectInterceptCallReceived(456);
276 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
277
278 // EventThread should also detect that at this point that it does not need
279 // any more vsync events, and should disable their generation.
280 expectVSyncSetEnabledCallReceived(false);
281}
282
283TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
284 // Create a first connection, register it, and request a vsync rate of zero.
285 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700286 sp<MockEventThreadConnection> firstConnection =
287 createConnection(firstConnectionEventRecorder,
288 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800289 mThread->setVsyncRate(0, firstConnection);
290
291 // By itself, this should not enable vsync events
292 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
293 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
294
295 // However if there is another connection which wants events at a nonzero rate.....
296 ConnectionEventRecorder secondConnectionEventRecorder{0};
297 sp<MockEventThreadConnection> secondConnection =
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700298 createConnection(secondConnectionEventRecorder,
299 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800300 mThread->setVsyncRate(1, secondConnection);
301
Dominik Laskowski029cc122019-01-23 19:52:06 -0800302 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800303 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800304
305 // Send a vsync event. EventThread should then make a call to the
306 // interceptor, and the second connection. The first connection should not
307 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700308 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800309 expectInterceptCallReceived(123);
310 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
311 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
312 1u);
313}
314
315TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
316 mThread->setVsyncRate(1, mConnection);
317
Dominik Laskowski029cc122019-01-23 19:52:06 -0800318 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800319 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800320
321 // Send a vsync event. EventThread should then make a call to the
322 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700323 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800324 expectInterceptCallReceived(123);
325 expectVsyncEventReceivedByConnection(123, 1u);
326
327 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700328 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800329 expectInterceptCallReceived(456);
330 expectVsyncEventReceivedByConnection(456, 2u);
331
332 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700333 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800334 expectInterceptCallReceived(789);
335 expectVsyncEventReceivedByConnection(789, 3u);
336}
337
338TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
339 mThread->setVsyncRate(2, mConnection);
340
Dominik Laskowski029cc122019-01-23 19:52:06 -0800341 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800342 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800343
344 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700345 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800346 expectInterceptCallReceived(123);
347 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
348
349 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700350 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351 expectInterceptCallReceived(456);
352 expectVsyncEventReceivedByConnection(456, 2u);
353
354 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700355 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800356 expectInterceptCallReceived(789);
357 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
358
359 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700360 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800361 expectInterceptCallReceived(101112);
362 expectVsyncEventReceivedByConnection(101112, 4u);
363}
364
365TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
366 mThread->setVsyncRate(1, mConnection);
367
Dominik Laskowski029cc122019-01-23 19:52:06 -0800368 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800369 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370
371 // Destroy the only (strong) reference to the connection.
372 mConnection = nullptr;
373
374 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700375 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800376 expectInterceptCallReceived(123);
377 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
378
379 // EventThread should disable vsync callbacks
380 expectVSyncSetEnabledCallReceived(false);
381}
382
383TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
384 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700385 sp<MockEventThreadConnection> errorConnection =
386 createConnection(errorConnectionEventRecorder,
387 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800388 mThread->setVsyncRate(1, errorConnection);
389
Dominik Laskowski029cc122019-01-23 19:52:06 -0800390 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800391 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800392
393 // The first event will be seen by the interceptor, and by the connection,
394 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700395 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800396 expectInterceptCallReceived(123);
397 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
398
399 // A subsequent event will be seen by the interceptor and not by the
400 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700401 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800402 expectInterceptCallReceived(456);
403 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
404
405 // EventThread should disable vsync callbacks with the second event
406 expectVSyncSetEnabledCallReceived(false);
407}
408
Alec Mouri717bcb62020-02-10 17:07:19 -0800409TEST_F(EventThreadTest, tracksEventConnections) {
410 EXPECT_EQ(1, mThread->getEventThreadConnectionCount());
411 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
412 sp<MockEventThreadConnection> errorConnection =
413 createConnection(errorConnectionEventRecorder,
414 ISurfaceComposer::eConfigChangedSuppress);
415 mThread->setVsyncRate(1, errorConnection);
416 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
417 ConnectionEventRecorder secondConnectionEventRecorder{0};
418 sp<MockEventThreadConnection> secondConnection =
419 createConnection(secondConnectionEventRecorder,
420 ISurfaceComposer::eConfigChangedSuppress);
421 mThread->setVsyncRate(1, secondConnection);
422 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
423
424 // EventThread should enable vsync callbacks.
425 expectVSyncSetEnabledCallReceived(true);
426
427 // The first event will be seen by the interceptor, and by the connection,
428 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700429 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800430 expectInterceptCallReceived(123);
431 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
432 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
433 1u);
434 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
435}
436
Lloyd Pique24b0a482018-03-09 18:52:26 -0800437TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
438 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700439 sp<MockEventThreadConnection> errorConnection =
440 createConnection(errorConnectionEventRecorder,
441 ISurfaceComposer::eConfigChangedSuppress);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800442 mThread->setVsyncRate(1, errorConnection);
443
Dominik Laskowski029cc122019-01-23 19:52:06 -0800444 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800445 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800446
447 // The first event will be seen by the interceptor, and by the connection,
448 // which then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700449 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800450 expectInterceptCallReceived(123);
451 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
452
453 // A subsequent event will be seen by the interceptor, and by the connection,
454 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700455 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800456 expectInterceptCallReceived(456);
457 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
458
459 // EventThread will not disable vsync callbacks as the errors are non-fatal.
460 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
461}
462
463TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700464 mThread->setDuration(321ns, 456ns);
465 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800466}
467
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800468TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
469 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
470 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800471}
472
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800473TEST_F(EventThreadTest, postHotplugInternalConnect) {
474 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
475 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800476}
477
478TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800479 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
480 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800481}
482
483TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800484 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
485 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800486}
487
Ady Abraham447052e2019-02-13 16:07:27 -0800488TEST_F(EventThreadTest, postConfigChangedPrimary) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700489 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(7), 16666666);
490 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800491}
492
493TEST_F(EventThreadTest, postConfigChangedExternal) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700494 mThread->onConfigChanged(EXTERNAL_DISPLAY_ID, HwcConfigIndexType(5), 16666666);
495 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800496}
497
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700498TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700499 mThread->onConfigChanged(DISPLAY_ID_64BIT, HwcConfigIndexType(7), 16666666);
500 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700501}
502
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700503TEST_F(EventThreadTest, suppressConfigChanged) {
504 ConnectionEventRecorder suppressConnectionEventRecorder{0};
505 sp<MockEventThreadConnection> suppressConnection =
506 createConnection(suppressConnectionEventRecorder,
507 ISurfaceComposer::eConfigChangedSuppress);
508
Alec Mouri60aee1c2019-10-28 16:18:59 -0700509 mThread->onConfigChanged(INTERNAL_DISPLAY_ID, HwcConfigIndexType(9), 16666666);
510 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700511
512 auto args = suppressConnectionEventRecorder.waitForCall();
513 ASSERT_FALSE(args.has_value());
514}
515
Lloyd Pique24b0a482018-03-09 18:52:26 -0800516} // namespace
517} // namespace android