blob: b4a1481e9cf217b0dd08503ecf8401208d195cdc [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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Lloyd Pique24b0a482018-03-09 18:52:26 -080021#undef LOG_TAG
22#define LOG_TAG "LibSurfaceFlingerUnittests"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080026#include <log/log.h>
Lloyd Pique24b0a482018-03-09 18:52:26 -080027#include <utils/Errors.h>
28
29#include "AsyncCallRecorder.h"
Marin Shalamanov23c44202020-12-22 19:09:20 +010030#include "DisplayHardware/DisplayMode.h"
Ana Krulecfefcb582018-08-07 14:22:37 -070031#include "Scheduler/EventThread.h"
Lloyd Pique24b0a482018-03-09 18:52:26 -080032
33using namespace std::chrono_literals;
34using namespace std::placeholders;
35
Ady Abraham62f216c2020-10-13 19:07:23 -070036using namespace android::flag_operators;
Lloyd Pique24b0a482018-03-09 18:52:26 -080037using testing::_;
38using testing::Invoke;
39
40namespace android {
Ady Abraham2139f732019-11-13 18:56:40 -080041
Lloyd Pique24b0a482018-03-09 18:52:26 -080042namespace {
43
Marin Shalamanova524a092020-07-27 21:39:55 +020044constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID(111);
45constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID(222);
46constexpr PhysicalDisplayId DISPLAY_ID_64BIT(0xabcd12349876fedcULL);
Jorim Jaggic0086af2021-02-12 18:18:11 +010047constexpr std::chrono::duration VSYNC_PERIOD(16ms);
Lloyd Pique24b0a482018-03-09 18:52:26 -080048class MockVSyncSource : public VSyncSource {
49public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070050 const char* getName() const override { return "test"; }
51
Lloyd Pique24b0a482018-03-09 18:52:26 -080052 MOCK_METHOD1(setVSyncEnabled, void(bool));
53 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070054 MOCK_METHOD2(setDuration,
55 void(std::chrono::nanoseconds workDuration,
56 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080057 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Ady Abraham5e7371c2020-03-24 14:47:24 -070058 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080059};
60
61} // namespace
62
63class EventThreadTest : public testing::Test {
64protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080065 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080066 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070067 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
68 ResyncCallback&& resyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070069 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070070 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070071 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080072 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
73 };
74
75 using ConnectionEventRecorder =
76 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
77
78 EventThreadTest();
79 ~EventThreadTest() override;
80
Dominik Laskowski6505f792019-09-18 11:10:05 -070081 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham62f216c2020-10-13 19:07:23 -070082 sp<MockEventThreadConnection> createConnection(
83 ConnectionEventRecorder& recorder,
84 ISurfaceComposer::EventRegistrationFlags eventRegistration = {},
85 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080086
87 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070088 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
89 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080090 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
91 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
92 void expectVsyncEventReceivedByConnection(const char* name,
93 ConnectionEventRecorder& connectionEventRecorder,
94 nsecs_t expectedTimestamp, unsigned expectedCount);
95 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080096 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -070097 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -080098 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -070099 int32_t expectedConfigId,
100 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700101 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700102 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
103 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800104
105 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
106 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700107 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
108 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800109 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
110 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700111 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800112 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700113 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800114
Dominik Laskowski6505f792019-09-18 11:10:05 -0700115 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800116 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700117 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800118 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700119 sp<MockEventThreadConnection> mThrottledConnection;
120
121 static constexpr uid_t mConnectionUid = 443;
122 static constexpr uid_t mThrottledConnectionUid = 177;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800123};
124
125EventThreadTest::EventThreadTest() {
126 const ::testing::TestInfo* const test_info =
127 ::testing::UnitTest::GetInstance()->current_test_info();
128 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
129
Dominik Laskowski6505f792019-09-18 11:10:05 -0700130 auto vsyncSource = std::make_unique<MockVSyncSource>();
131 mVSyncSource = vsyncSource.get();
132
133 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800134 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
135
Dominik Laskowski6505f792019-09-18 11:10:05 -0700136 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
138
Ady Abraham9c53ee72020-07-22 21:16:18 -0700139 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
140 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800141
Dominik Laskowski6505f792019-09-18 11:10:05 -0700142 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700143 mConnection = createConnection(mConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100144 ISurfaceComposer::EventRegistration::modeChanged |
Ady Abraham62f216c2020-10-13 19:07:23 -0700145 ISurfaceComposer::EventRegistration::frameRateOverride);
146 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100147 ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700148 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800149
150 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800151 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
152 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800153}
154
155EventThreadTest::~EventThreadTest() {
156 const ::testing::TestInfo* const test_info =
157 ::testing::UnitTest::GetInstance()->current_test_info();
158 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800159
160 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800161 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800162}
163
Dominik Laskowski6505f792019-09-18 11:10:05 -0700164void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700165 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
166 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
167 return (uid == mThrottledConnectionUid);
168 };
Jorim Jaggic0086af2021-02-12 18:18:11 +0100169 const auto getVsyncPeriod = [](uid_t uid) {
170 return VSYNC_PERIOD.count();
171 };
Ady Abraham0bb6a472020-10-12 10:22:13 -0700172
Dominik Laskowski6505f792019-09-18 11:10:05 -0700173 mThread = std::make_unique<impl::EventThread>(std::move(source),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700174 /*tokenManager=*/nullptr,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700175 mInterceptVSyncCallRecorder.getInvocable(),
Jorim Jaggic0086af2021-02-12 18:18:11 +0100176 throttleVsync, getVsyncPeriod);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800177
178 // EventThread should register itself as VSyncSource callback.
179 mCallback = expectVSyncSetCallbackCallReceived();
180 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800181}
182
183sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700184 ConnectionEventRecorder& recorder,
185 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800186 sp<MockEventThreadConnection> connection =
Ady Abraham0bb6a472020-10-12 10:22:13 -0700187 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700188 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800189 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
190 return connection;
191}
192
193void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
194 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
195 ASSERT_TRUE(args.has_value());
196 EXPECT_EQ(expectedState, std::get<0>(args.value()));
197}
198
Ady Abraham9c53ee72020-07-22 21:16:18 -0700199void EventThreadTest::expectVSyncSetDurationCallReceived(
200 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
201 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800202 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700203 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
204 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800205}
206
207VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
208 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
209 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
210}
211
212void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
213 auto args = mInterceptVSyncCallRecorder.waitForCall();
214 ASSERT_TRUE(args.has_value());
215 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
216}
217
Ady Abraham0bb6a472020-10-12 10:22:13 -0700218void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
219 auto args = mThrottleVsyncCallRecorder.waitForCall();
220 ASSERT_TRUE(args.has_value());
221 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
222 EXPECT_EQ(uid, std::get<1>(args.value()));
223}
224
Lloyd Pique24b0a482018-03-09 18:52:26 -0800225void EventThreadTest::expectVsyncEventReceivedByConnection(
226 const char* name, ConnectionEventRecorder& connectionEventRecorder,
227 nsecs_t expectedTimestamp, unsigned expectedCount) {
228 auto args = connectionEventRecorder.waitForCall();
229 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
230 << expectedTimestamp;
231 const auto& event = std::get<0>(args.value());
232 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
233 << name << " did not get the correct event for timestamp " << expectedTimestamp;
234 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
235 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
236 EXPECT_EQ(expectedCount, event.vsync.count)
237 << name << " did not get the expected count for timestamp " << expectedTimestamp;
238}
239
240void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
241 unsigned expectedCount) {
242 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
243 mConnectionEventCallRecorder, expectedTimestamp,
244 expectedCount);
245}
246
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800247void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
248 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800249 auto args = mConnectionEventCallRecorder.waitForCall();
250 ASSERT_TRUE(args.has_value());
251 const auto& event = std::get<0>(args.value());
252 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800253 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800254 EXPECT_EQ(expectedConnected, event.hotplug.connected);
255}
256
Ady Abraham447052e2019-02-13 16:07:27 -0800257void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700258 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
259 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800260 auto args = mConnectionEventCallRecorder.waitForCall();
261 ASSERT_TRUE(args.has_value());
262 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100263 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800264 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100265 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
266 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800267}
268
Ady Abraham62f216c2020-10-13 19:07:23 -0700269void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
270 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
271 for (const auto [uid, frameRateHz] : expectedOverrides) {
272 auto args = mConnectionEventCallRecorder.waitForCall();
273 ASSERT_TRUE(args.has_value());
274 const auto& event = std::get<0>(args.value());
275 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
276 EXPECT_EQ(expectedDisplayId, event.header.displayId);
277 EXPECT_EQ(uid, event.frameRateOverride.uid);
278 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
279 }
280
281 auto args = mConnectionEventCallRecorder.waitForCall();
282 ASSERT_TRUE(args.has_value());
283 const auto& event = std::get<0>(args.value());
284 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
285 EXPECT_EQ(expectedDisplayId, event.header.displayId);
286}
287
Lloyd Pique24b0a482018-03-09 18:52:26 -0800288namespace {
289
290/* ------------------------------------------------------------------------
291 * Test cases
292 */
293
294TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
295 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
296 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700297 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800298 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
299 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
300 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
301}
302
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800303TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800304 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
305 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800306
307 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700308 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800309
310 // EventThread should not enable vsync callbacks.
311 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800312}
313
Lloyd Pique24b0a482018-03-09 18:52:26 -0800314TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
315 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700316 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317
Ady Abraham8532d012019-05-08 14:50:56 -0700318 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800319 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
320
Dominik Laskowski029cc122019-01-23 19:52:06 -0800321 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800323
324 // Use the received callback to signal a first vsync event.
325 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700326 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800327 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700328 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800329 expectVsyncEventReceivedByConnection(123, 1u);
330
331 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700332 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800333 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700334 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800335 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700336 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800337 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
338
339 // EventThread should also detect that at this point that it does not need
340 // any more vsync events, and should disable their generation.
341 expectVSyncSetEnabledCallReceived(false);
342}
343
344TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
345 // Create a first connection, register it, and request a vsync rate of zero.
346 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700347 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800348 mThread->setVsyncRate(0, firstConnection);
349
350 // By itself, this should not enable vsync events
351 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
352 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
353
354 // However if there is another connection which wants events at a nonzero rate.....
355 ConnectionEventRecorder secondConnectionEventRecorder{0};
356 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700357 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800358 mThread->setVsyncRate(1, secondConnection);
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 // Send a vsync event. EventThread should then make a call to the
364 // interceptor, and the second connection. The first connection should not
365 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700366 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800367 expectInterceptCallReceived(123);
368 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
369 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
370 1u);
371}
372
373TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
374 mThread->setVsyncRate(1, mConnection);
375
Dominik Laskowski029cc122019-01-23 19:52:06 -0800376 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800377 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800378
379 // Send a vsync event. EventThread should then make a call to the
380 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700381 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800382 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700383 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800384 expectVsyncEventReceivedByConnection(123, 1u);
385
386 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700387 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800388 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700389 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800390 expectVsyncEventReceivedByConnection(456, 2u);
391
392 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700393 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800394 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700395 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800396 expectVsyncEventReceivedByConnection(789, 3u);
397}
398
399TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
400 mThread->setVsyncRate(2, mConnection);
401
Dominik Laskowski029cc122019-01-23 19:52:06 -0800402 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800403 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800404
405 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700406 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407 expectInterceptCallReceived(123);
408 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700409 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800410
411 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700412 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800413 expectInterceptCallReceived(456);
414 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700415 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416
417 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700418 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419 expectInterceptCallReceived(789);
420 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700421 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800422
423 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700424 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800425 expectInterceptCallReceived(101112);
426 expectVsyncEventReceivedByConnection(101112, 4u);
427}
428
429TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
430 mThread->setVsyncRate(1, mConnection);
431
Dominik Laskowski029cc122019-01-23 19:52:06 -0800432 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800433 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800434
435 // Destroy the only (strong) reference to the connection.
436 mConnection = nullptr;
437
438 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700439 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800440 expectInterceptCallReceived(123);
441 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
442
443 // EventThread should disable vsync callbacks
444 expectVSyncSetEnabledCallReceived(false);
445}
446
447TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
448 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700449 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800450 mThread->setVsyncRate(1, errorConnection);
451
Dominik Laskowski029cc122019-01-23 19:52:06 -0800452 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800453 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800454
455 // The first event will be seen by the interceptor, and by the connection,
456 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700457 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800458 expectInterceptCallReceived(123);
459 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
460
461 // A subsequent event will be seen by the interceptor and not by the
462 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700463 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800464 expectInterceptCallReceived(456);
465 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
466
467 // EventThread should disable vsync callbacks with the second event
468 expectVSyncSetEnabledCallReceived(false);
469}
470
Alec Mouri717bcb62020-02-10 17:07:19 -0800471TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700472 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800473 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700474 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800475 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700476 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800477 ConnectionEventRecorder secondConnectionEventRecorder{0};
478 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700479 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800480 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700481 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800482
483 // EventThread should enable vsync callbacks.
484 expectVSyncSetEnabledCallReceived(true);
485
486 // The first event will be seen by the interceptor, and by the connection,
487 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700488 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800489 expectInterceptCallReceived(123);
490 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
491 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
492 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700493 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800494}
495
Lloyd Pique24b0a482018-03-09 18:52:26 -0800496TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
497 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700498 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800499 mThread->setVsyncRate(1, errorConnection);
500
Dominik Laskowski029cc122019-01-23 19:52:06 -0800501 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800502 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800503
504 // The first event will be seen by the interceptor, and by the connection,
505 // which then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700506 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800507 expectInterceptCallReceived(123);
508 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
509
510 // A subsequent event will be seen by the interceptor, and by the connection,
511 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700512 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800513 expectInterceptCallReceived(456);
514 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
515
516 // EventThread will not disable vsync callbacks as the errors are non-fatal.
517 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
518}
519
520TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700521 mThread->setDuration(321ns, 456ns);
522 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800523}
524
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800525TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
526 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
527 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800528}
529
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800530TEST_F(EventThreadTest, postHotplugInternalConnect) {
531 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
532 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800533}
534
535TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800536 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
537 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800538}
539
540TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800541 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
542 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800543}
544
Ady Abraham447052e2019-02-13 16:07:27 -0800545TEST_F(EventThreadTest, postConfigChangedPrimary) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100546 mThread->onModeChanged(INTERNAL_DISPLAY_ID, DisplayModeId(7), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700547 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800548}
549
550TEST_F(EventThreadTest, postConfigChangedExternal) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100551 mThread->onModeChanged(EXTERNAL_DISPLAY_ID, DisplayModeId(5), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700552 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800553}
554
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700555TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100556 mThread->onModeChanged(DISPLAY_ID_64BIT, DisplayModeId(7), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700557 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700558}
559
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700560TEST_F(EventThreadTest, suppressConfigChanged) {
561 ConnectionEventRecorder suppressConnectionEventRecorder{0};
562 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700563 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700564
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100565 mThread->onModeChanged(INTERNAL_DISPLAY_ID, DisplayModeId(9), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700566 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700567
568 auto args = suppressConnectionEventRecorder.waitForCall();
569 ASSERT_FALSE(args.has_value());
570}
571
Ady Abraham62f216c2020-10-13 19:07:23 -0700572TEST_F(EventThreadTest, postUidFrameRateMapping) {
573 const std::vector<FrameRateOverride> overrides = {
574 {.uid = 1, .frameRateHz = 20},
575 {.uid = 3, .frameRateHz = 40},
576 {.uid = 5, .frameRateHz = 60},
577 };
578
579 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
580 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
581}
582
583TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
584 const std::vector<FrameRateOverride> overrides = {
585 {.uid = 1, .frameRateHz = 20},
586 {.uid = 3, .frameRateHz = 40},
587 {.uid = 5, .frameRateHz = 60},
588 };
589
590 ConnectionEventRecorder suppressConnectionEventRecorder{0};
591 sp<MockEventThreadConnection> suppressConnection =
592 createConnection(suppressConnectionEventRecorder);
593
594 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
595 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
596
597 auto args = suppressConnectionEventRecorder.waitForCall();
598 ASSERT_FALSE(args.has_value());
599}
600
Ady Abraham0bb6a472020-10-12 10:22:13 -0700601TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
602 // Signal that we want the next vsync event to be posted to the throttled connection
603 mThread->requestNextVsync(mThrottledConnection);
604
605 // EventThread should immediately request a resync.
606 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
607
608 // EventThread should enable vsync callbacks.
609 expectVSyncSetEnabledCallReceived(true);
610
611 // Use the received callback to signal a first vsync event.
612 // The interceptor should receive the event, but not the connection.
613 mCallback->onVSyncEvent(123, 456, 789);
614 expectInterceptCallReceived(123);
615 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
616 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
617
618 // Use the received callback to signal a second vsync event.
619 // The interceptor should receive the event, but the connection should
620 // not as it was only interested in the first.
621 mCallback->onVSyncEvent(456, 123, 0);
622 expectInterceptCallReceived(456);
623 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
624 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
625
626 // EventThread should not change the vsync state as it didn't send the event
627 // yet
628 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
629}
630
Lloyd Pique24b0a482018-03-09 18:52:26 -0800631} // namespace
632} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100633
634// TODO(b/129481165): remove the #pragma below and fix conversion issues
635#pragma clang diagnostic pop // ignored "-Wextra"