blob: 62acc6b549965722939f0b5ebc8efcb4ebed1e89 [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
Dominik Laskowskif1833852021-03-23 15:06:50 -070044constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u);
45constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u);
46constexpr PhysicalDisplayId DISPLAY_ID_64BIT =
47 PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu);
48
Jorim Jaggic0086af2021-02-12 18:18:11 +010049constexpr std::chrono::duration VSYNC_PERIOD(16ms);
Dominik Laskowskif1833852021-03-23 15:06:50 -070050
Lloyd Pique24b0a482018-03-09 18:52:26 -080051class MockVSyncSource : public VSyncSource {
52public:
Dominik Laskowski6505f792019-09-18 11:10:05 -070053 const char* getName() const override { return "test"; }
54
Lloyd Pique24b0a482018-03-09 18:52:26 -080055 MOCK_METHOD1(setVSyncEnabled, void(bool));
56 MOCK_METHOD1(setCallback, void(VSyncSource::Callback*));
Ady Abraham9c53ee72020-07-22 21:16:18 -070057 MOCK_METHOD2(setDuration,
58 void(std::chrono::nanoseconds workDuration,
59 std::chrono::nanoseconds readyDuration));
Ady Abrahamb838aed2019-02-12 15:30:16 -080060 MOCK_METHOD1(pauseVsyncCallback, void(bool));
Ady Abraham5e7371c2020-03-24 14:47:24 -070061 MOCK_CONST_METHOD1(dump, void(std::string&));
Lloyd Pique24b0a482018-03-09 18:52:26 -080062};
63
64} // namespace
65
66class EventThreadTest : public testing::Test {
67protected:
Dominik Laskowskif654d572018-12-20 11:03:06 -080068 class MockEventThreadConnection : public EventThreadConnection {
Lloyd Pique24b0a482018-03-09 18:52:26 -080069 public:
Ady Abraham0bb6a472020-10-12 10:22:13 -070070 MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid,
71 ResyncCallback&& resyncCallback,
Ady Abraham62f216c2020-10-13 19:07:23 -070072 ISurfaceComposer::EventRegistrationFlags eventRegistration)
Ady Abraham0bb6a472020-10-12 10:22:13 -070073 : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback),
Ady Abraham62f216c2020-10-13 19:07:23 -070074 eventRegistration) {}
Lloyd Pique24b0a482018-03-09 18:52:26 -080075 MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event));
76 };
77
78 using ConnectionEventRecorder =
79 AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>;
80
81 EventThreadTest();
82 ~EventThreadTest() override;
83
Dominik Laskowski6505f792019-09-18 11:10:05 -070084 void createThread(std::unique_ptr<VSyncSource>);
Ady Abraham62f216c2020-10-13 19:07:23 -070085 sp<MockEventThreadConnection> createConnection(
86 ConnectionEventRecorder& recorder,
87 ISurfaceComposer::EventRegistrationFlags eventRegistration = {},
88 uid_t ownerUid = mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -080089
90 void expectVSyncSetEnabledCallReceived(bool expectedState);
Ady Abraham9c53ee72020-07-22 21:16:18 -070091 void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration,
92 std::chrono::nanoseconds expectedReadyDuration);
Lloyd Pique24b0a482018-03-09 18:52:26 -080093 VSyncSource::Callback* expectVSyncSetCallbackCallReceived();
94 void expectInterceptCallReceived(nsecs_t expectedTimestamp);
95 void expectVsyncEventReceivedByConnection(const char* name,
96 ConnectionEventRecorder& connectionEventRecorder,
97 nsecs_t expectedTimestamp, unsigned expectedCount);
98 void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080099 void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700100 bool expectedConnected);
Ady Abraham447052e2019-02-13 16:07:27 -0800101 void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700102 int32_t expectedConfigId,
103 nsecs_t expectedVsyncPeriod);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700104 void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t);
Ady Abraham62f216c2020-10-13 19:07:23 -0700105 void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
106 std::vector<FrameRateOverride>);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800107
108 AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder;
109 AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder;
Ady Abraham9c53ee72020-07-22 21:16:18 -0700110 AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)>
111 mVSyncSetDurationCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800112 AsyncCallRecorder<void (*)()> mResyncCallRecorder;
113 AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700114 AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800115 ConnectionEventRecorder mConnectionEventCallRecorder{0};
Ady Abraham0bb6a472020-10-12 10:22:13 -0700116 ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0};
Lloyd Pique24b0a482018-03-09 18:52:26 -0800117
Dominik Laskowski6505f792019-09-18 11:10:05 -0700118 MockVSyncSource* mVSyncSource;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800119 VSyncSource::Callback* mCallback = nullptr;
Dominik Laskowski6505f792019-09-18 11:10:05 -0700120 std::unique_ptr<impl::EventThread> mThread;
Lloyd Pique24b0a482018-03-09 18:52:26 -0800121 sp<MockEventThreadConnection> mConnection;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700122 sp<MockEventThreadConnection> mThrottledConnection;
123
124 static constexpr uid_t mConnectionUid = 443;
125 static constexpr uid_t mThrottledConnectionUid = 177;
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("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
132
Dominik Laskowski6505f792019-09-18 11:10:05 -0700133 auto vsyncSource = std::make_unique<MockVSyncSource>();
134 mVSyncSource = vsyncSource.get();
135
136 EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800137 .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable()));
138
Dominik Laskowski6505f792019-09-18 11:10:05 -0700139 EXPECT_CALL(*mVSyncSource, setCallback(_))
Lloyd Pique24b0a482018-03-09 18:52:26 -0800140 .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable()));
141
Ady Abraham9c53ee72020-07-22 21:16:18 -0700142 EXPECT_CALL(*mVSyncSource, setDuration(_, _))
143 .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800144
Dominik Laskowski6505f792019-09-18 11:10:05 -0700145 createThread(std::move(vsyncSource));
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700146 mConnection = createConnection(mConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100147 ISurfaceComposer::EventRegistration::modeChanged |
Ady Abraham62f216c2020-10-13 19:07:23 -0700148 ISurfaceComposer::EventRegistration::frameRateOverride);
149 mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder,
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100150 ISurfaceComposer::EventRegistration::modeChanged,
Ady Abraham62f216c2020-10-13 19:07:23 -0700151 mThrottledConnectionUid);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800152
153 // A display must be connected for VSYNC events to be delivered.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800154 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
155 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800156}
157
158EventThreadTest::~EventThreadTest() {
159 const ::testing::TestInfo* const test_info =
160 ::testing::UnitTest::GetInstance()->current_test_info();
161 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
Dominik Laskowski029cc122019-01-23 19:52:06 -0800162
163 // EventThread should unregister itself as VSyncSource callback.
Lloyd Pique0655b8e2019-01-31 18:20:27 -0800164 EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800165}
166
Dominik Laskowski6505f792019-09-18 11:10:05 -0700167void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700168 const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) {
169 mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid);
170 return (uid == mThrottledConnectionUid);
171 };
Jorim Jaggic0086af2021-02-12 18:18:11 +0100172 const auto getVsyncPeriod = [](uid_t uid) {
173 return VSYNC_PERIOD.count();
174 };
Ady Abraham0bb6a472020-10-12 10:22:13 -0700175
Dominik Laskowski6505f792019-09-18 11:10:05 -0700176 mThread = std::make_unique<impl::EventThread>(std::move(source),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700177 /*tokenManager=*/nullptr,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700178 mInterceptVSyncCallRecorder.getInvocable(),
Jorim Jaggic0086af2021-02-12 18:18:11 +0100179 throttleVsync, getVsyncPeriod);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800180
181 // EventThread should register itself as VSyncSource callback.
182 mCallback = expectVSyncSetCallbackCallReceived();
183 ASSERT_TRUE(mCallback);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800184}
185
186sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection(
Ady Abraham62f216c2020-10-13 19:07:23 -0700187 ConnectionEventRecorder& recorder,
188 ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800189 sp<MockEventThreadConnection> connection =
Ady Abraham0bb6a472020-10-12 10:22:13 -0700190 new MockEventThreadConnection(mThread.get(), ownerUid,
Ady Abraham62f216c2020-10-13 19:07:23 -0700191 mResyncCallRecorder.getInvocable(), eventRegistration);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800192 EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable()));
193 return connection;
194}
195
196void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) {
197 auto args = mVSyncSetEnabledCallRecorder.waitForCall();
198 ASSERT_TRUE(args.has_value());
199 EXPECT_EQ(expectedState, std::get<0>(args.value()));
200}
201
Ady Abraham9c53ee72020-07-22 21:16:18 -0700202void EventThreadTest::expectVSyncSetDurationCallReceived(
203 std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) {
204 auto args = mVSyncSetDurationCallRecorder.waitForCall();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800205 ASSERT_TRUE(args.has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700206 EXPECT_EQ(expectedDuration, std::get<0>(args.value()));
207 EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value()));
Lloyd Pique24b0a482018-03-09 18:52:26 -0800208}
209
210VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() {
211 auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall();
212 return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr;
213}
214
215void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) {
216 auto args = mInterceptVSyncCallRecorder.waitForCall();
217 ASSERT_TRUE(args.has_value());
218 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
219}
220
Ady Abraham0bb6a472020-10-12 10:22:13 -0700221void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) {
222 auto args = mThrottleVsyncCallRecorder.waitForCall();
223 ASSERT_TRUE(args.has_value());
224 EXPECT_EQ(expectedTimestamp, std::get<0>(args.value()));
225 EXPECT_EQ(uid, std::get<1>(args.value()));
226}
227
Lloyd Pique24b0a482018-03-09 18:52:26 -0800228void EventThreadTest::expectVsyncEventReceivedByConnection(
229 const char* name, ConnectionEventRecorder& connectionEventRecorder,
230 nsecs_t expectedTimestamp, unsigned expectedCount) {
231 auto args = connectionEventRecorder.waitForCall();
232 ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp "
233 << expectedTimestamp;
234 const auto& event = std::get<0>(args.value());
235 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type)
236 << name << " did not get the correct event for timestamp " << expectedTimestamp;
237 EXPECT_EQ(expectedTimestamp, event.header.timestamp)
238 << name << " did not get the expected timestamp for timestamp " << expectedTimestamp;
239 EXPECT_EQ(expectedCount, event.vsync.count)
240 << name << " did not get the expected count for timestamp " << expectedTimestamp;
241}
242
243void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp,
244 unsigned expectedCount) {
245 expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder",
246 mConnectionEventCallRecorder, expectedTimestamp,
247 expectedCount);
248}
249
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800250void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId,
251 bool expectedConnected) {
Lloyd Pique24b0a482018-03-09 18:52:26 -0800252 auto args = mConnectionEventCallRecorder.waitForCall();
253 ASSERT_TRUE(args.has_value());
254 const auto& event = std::get<0>(args.value());
255 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type);
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800256 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800257 EXPECT_EQ(expectedConnected, event.hotplug.connected);
258}
259
Ady Abraham447052e2019-02-13 16:07:27 -0800260void EventThreadTest::expectConfigChangedEventReceivedByConnection(
Alec Mouri60aee1c2019-10-28 16:18:59 -0700261 PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId,
262 nsecs_t expectedVsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800263 auto args = mConnectionEventCallRecorder.waitForCall();
264 ASSERT_TRUE(args.has_value());
265 const auto& event = std::get<0>(args.value());
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100266 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type);
Ady Abraham447052e2019-02-13 16:07:27 -0800267 EXPECT_EQ(expectedDisplayId, event.header.displayId);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100268 EXPECT_EQ(expectedConfigId, event.modeChange.modeId);
269 EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod);
Ady Abraham447052e2019-02-13 16:07:27 -0800270}
271
Ady Abraham62f216c2020-10-13 19:07:23 -0700272void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection(
273 PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) {
274 for (const auto [uid, frameRateHz] : expectedOverrides) {
275 auto args = mConnectionEventCallRecorder.waitForCall();
276 ASSERT_TRUE(args.has_value());
277 const auto& event = std::get<0>(args.value());
278 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type);
279 EXPECT_EQ(expectedDisplayId, event.header.displayId);
280 EXPECT_EQ(uid, event.frameRateOverride.uid);
281 EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz);
282 }
283
284 auto args = mConnectionEventCallRecorder.waitForCall();
285 ASSERT_TRUE(args.has_value());
286 const auto& event = std::get<0>(args.value());
287 EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type);
288 EXPECT_EQ(expectedDisplayId, event.header.displayId);
289}
290
Lloyd Pique24b0a482018-03-09 18:52:26 -0800291namespace {
292
293/* ------------------------------------------------------------------------
294 * Test cases
295 */
296
297TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) {
298 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
299 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
Ady Abraham9c53ee72020-07-22 21:16:18 -0700300 EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800301 EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value());
302 EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value());
303 EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value());
304}
305
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800306TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800307 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
308 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800309
310 // Signal that we want the next vsync event to be posted to the connection.
Ady Abraham8532d012019-05-08 14:50:56 -0700311 mThread->requestNextVsync(mConnection);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800312
313 // EventThread should not enable vsync callbacks.
314 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800315}
316
Lloyd Pique24b0a482018-03-09 18:52:26 -0800317TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) {
318 // Signal that we want the next vsync event to be posted to the connection
Ady Abraham8532d012019-05-08 14:50:56 -0700319 mThread->requestNextVsync(mConnection);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800320
Ady Abraham8532d012019-05-08 14:50:56 -0700321 // EventThread should immediately request a resync.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800322 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
323
Dominik Laskowski029cc122019-01-23 19:52:06 -0800324 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800325 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800326
327 // Use the received callback to signal a first vsync event.
328 // The interceptor should receive the event, as well as the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700329 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800330 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700331 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800332 expectVsyncEventReceivedByConnection(123, 1u);
333
334 // Use the received callback to signal a second vsync event.
Ady Abraham0bb6a472020-10-12 10:22:13 -0700335 // The interceptor should receive the event, but the connection should
Lloyd Pique24b0a482018-03-09 18:52:26 -0800336 // not as it was only interested in the first.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700337 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800338 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700339 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800340 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
341
342 // EventThread should also detect that at this point that it does not need
343 // any more vsync events, and should disable their generation.
344 expectVSyncSetEnabledCallReceived(false);
345}
346
347TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) {
348 // Create a first connection, register it, and request a vsync rate of zero.
349 ConnectionEventRecorder firstConnectionEventRecorder{0};
Ady Abraham62f216c2020-10-13 19:07:23 -0700350 sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800351 mThread->setVsyncRate(0, firstConnection);
352
353 // By itself, this should not enable vsync events
354 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
355 EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value());
356
357 // However if there is another connection which wants events at a nonzero rate.....
358 ConnectionEventRecorder secondConnectionEventRecorder{0};
359 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700360 createConnection(secondConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800361 mThread->setVsyncRate(1, secondConnection);
362
Dominik Laskowski029cc122019-01-23 19:52:06 -0800363 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800364 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800365
366 // Send a vsync event. EventThread should then make a call to the
367 // interceptor, and the second connection. The first connection should not
368 // get the event.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700369 mCallback->onVSyncEvent(123, 456, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800370 expectInterceptCallReceived(123);
371 EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value());
372 expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123,
373 1u);
374}
375
376TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) {
377 mThread->setVsyncRate(1, mConnection);
378
Dominik Laskowski029cc122019-01-23 19:52:06 -0800379 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800380 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800381
382 // Send a vsync event. EventThread should then make a call to the
383 // interceptor, and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700384 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800385 expectInterceptCallReceived(123);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700386 expectThrottleVsyncReceived(456, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800387 expectVsyncEventReceivedByConnection(123, 1u);
388
389 // A second event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700390 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800391 expectInterceptCallReceived(456);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700392 expectThrottleVsyncReceived(123, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800393 expectVsyncEventReceivedByConnection(456, 2u);
394
395 // A third event should go to the same places.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700396 mCallback->onVSyncEvent(789, 777, 111);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800397 expectInterceptCallReceived(789);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700398 expectThrottleVsyncReceived(777, mConnectionUid);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800399 expectVsyncEventReceivedByConnection(789, 3u);
400}
401
402TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) {
403 mThread->setVsyncRate(2, mConnection);
404
Dominik Laskowski029cc122019-01-23 19:52:06 -0800405 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800406 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800407
408 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700409 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800410 expectInterceptCallReceived(123);
411 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700412 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800413
414 // The second event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700415 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800416 expectInterceptCallReceived(456);
417 expectVsyncEventReceivedByConnection(456, 2u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700418 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800419
420 // The third event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700421 mCallback->onVSyncEvent(789, 777, 744);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800422 expectInterceptCallReceived(789);
423 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
Ady Abraham0bb6a472020-10-12 10:22:13 -0700424 EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value());
Lloyd Pique24b0a482018-03-09 18:52:26 -0800425
426 // The fourth event will be seen by the interceptor and the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700427 mCallback->onVSyncEvent(101112, 7847, 86);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800428 expectInterceptCallReceived(101112);
429 expectVsyncEventReceivedByConnection(101112, 4u);
430}
431
432TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) {
433 mThread->setVsyncRate(1, mConnection);
434
Dominik Laskowski029cc122019-01-23 19:52:06 -0800435 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800436 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800437
438 // Destroy the only (strong) reference to the connection.
439 mConnection = nullptr;
440
441 // The first event will be seen by the interceptor, and not the connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700442 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800443 expectInterceptCallReceived(123);
444 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
445
446 // EventThread should disable vsync callbacks
447 expectVSyncSetEnabledCallReceived(false);
448}
449
450TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) {
451 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700452 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800453 mThread->setVsyncRate(1, errorConnection);
454
Dominik Laskowski029cc122019-01-23 19:52:06 -0800455 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800456 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800457
458 // The first event will be seen by the interceptor, and by the connection,
459 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700460 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800461 expectInterceptCallReceived(123);
462 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
463
464 // A subsequent event will be seen by the interceptor and not by the
465 // connection.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700466 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800467 expectInterceptCallReceived(456);
468 EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value());
469
470 // EventThread should disable vsync callbacks with the second event
471 expectVSyncSetEnabledCallReceived(false);
472}
473
Alec Mouri717bcb62020-02-10 17:07:19 -0800474TEST_F(EventThreadTest, tracksEventConnections) {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700475 EXPECT_EQ(2, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800476 ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY};
Ady Abraham62f216c2020-10-13 19:07:23 -0700477 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800478 mThread->setVsyncRate(1, errorConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700479 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800480 ConnectionEventRecorder secondConnectionEventRecorder{0};
481 sp<MockEventThreadConnection> secondConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700482 createConnection(secondConnectionEventRecorder);
Alec Mouri717bcb62020-02-10 17:07:19 -0800483 mThread->setVsyncRate(1, secondConnection);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700484 EXPECT_EQ(4, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800485
486 // EventThread should enable vsync callbacks.
487 expectVSyncSetEnabledCallReceived(true);
488
489 // The first event will be seen by the interceptor, and by the connection,
490 // which then returns an error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700491 mCallback->onVSyncEvent(123, 456, 789);
Alec Mouri717bcb62020-02-10 17:07:19 -0800492 expectInterceptCallReceived(123);
493 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
494 expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123,
495 1u);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700496 EXPECT_EQ(3, mThread->getEventThreadConnectionCount());
Alec Mouri717bcb62020-02-10 17:07:19 -0800497}
498
Lloyd Pique24b0a482018-03-09 18:52:26 -0800499TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) {
500 ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK};
Ady Abraham62f216c2020-10-13 19:07:23 -0700501 sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800502 mThread->setVsyncRate(1, errorConnection);
503
Dominik Laskowski029cc122019-01-23 19:52:06 -0800504 // EventThread should enable vsync callbacks.
Lloyd Pique24b0a482018-03-09 18:52:26 -0800505 expectVSyncSetEnabledCallReceived(true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800506
507 // The first event will be seen by the interceptor, and by the connection,
508 // which then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700509 mCallback->onVSyncEvent(123, 456, 789);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800510 expectInterceptCallReceived(123);
511 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u);
512
513 // A subsequent event will be seen by the interceptor, and by the connection,
514 // which still then returns an non-fatal error.
Ady Abraham9c53ee72020-07-22 21:16:18 -0700515 mCallback->onVSyncEvent(456, 123, 0);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800516 expectInterceptCallReceived(456);
517 expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u);
518
519 // EventThread will not disable vsync callbacks as the errors are non-fatal.
520 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
521}
522
523TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) {
Ady Abraham9c53ee72020-07-22 21:16:18 -0700524 mThread->setDuration(321ns, 456ns);
525 expectVSyncSetDurationCallReceived(321ns, 456ns);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800526}
527
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800528TEST_F(EventThreadTest, postHotplugInternalDisconnect) {
529 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false);
530 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800531}
532
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800533TEST_F(EventThreadTest, postHotplugInternalConnect) {
534 mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true);
535 expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800536}
537
538TEST_F(EventThreadTest, postHotplugExternalDisconnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800539 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false);
540 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800541}
542
543TEST_F(EventThreadTest, postHotplugExternalConnect) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800544 mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true);
545 expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true);
Lloyd Pique24b0a482018-03-09 18:52:26 -0800546}
547
Ady Abraham447052e2019-02-13 16:07:27 -0800548TEST_F(EventThreadTest, postConfigChangedPrimary) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100549 mThread->onModeChanged(INTERNAL_DISPLAY_ID, DisplayModeId(7), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700550 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800551}
552
553TEST_F(EventThreadTest, postConfigChangedExternal) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100554 mThread->onModeChanged(EXTERNAL_DISPLAY_ID, DisplayModeId(5), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700555 expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666);
Ady Abraham447052e2019-02-13 16:07:27 -0800556}
557
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700558TEST_F(EventThreadTest, postConfigChangedPrimary64bit) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100559 mThread->onModeChanged(DISPLAY_ID_64BIT, DisplayModeId(7), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700560 expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666);
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700561}
562
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700563TEST_F(EventThreadTest, suppressConfigChanged) {
564 ConnectionEventRecorder suppressConnectionEventRecorder{0};
565 sp<MockEventThreadConnection> suppressConnection =
Ady Abraham62f216c2020-10-13 19:07:23 -0700566 createConnection(suppressConnectionEventRecorder);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700567
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100568 mThread->onModeChanged(INTERNAL_DISPLAY_ID, DisplayModeId(9), 16666666);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700569 expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666);
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700570
571 auto args = suppressConnectionEventRecorder.waitForCall();
572 ASSERT_FALSE(args.has_value());
573}
574
Ady Abraham62f216c2020-10-13 19:07:23 -0700575TEST_F(EventThreadTest, postUidFrameRateMapping) {
576 const std::vector<FrameRateOverride> overrides = {
577 {.uid = 1, .frameRateHz = 20},
578 {.uid = 3, .frameRateHz = 40},
579 {.uid = 5, .frameRateHz = 60},
580 };
581
582 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
583 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
584}
585
586TEST_F(EventThreadTest, suppressUidFrameRateMapping) {
587 const std::vector<FrameRateOverride> overrides = {
588 {.uid = 1, .frameRateHz = 20},
589 {.uid = 3, .frameRateHz = 40},
590 {.uid = 5, .frameRateHz = 60},
591 };
592
593 ConnectionEventRecorder suppressConnectionEventRecorder{0};
594 sp<MockEventThreadConnection> suppressConnection =
595 createConnection(suppressConnectionEventRecorder);
596
597 mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides);
598 expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides);
599
600 auto args = suppressConnectionEventRecorder.waitForCall();
601 ASSERT_FALSE(args.has_value());
602}
603
Ady Abraham0bb6a472020-10-12 10:22:13 -0700604TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) {
605 // Signal that we want the next vsync event to be posted to the throttled connection
606 mThread->requestNextVsync(mThrottledConnection);
607
608 // EventThread should immediately request a resync.
609 EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value());
610
611 // EventThread should enable vsync callbacks.
612 expectVSyncSetEnabledCallReceived(true);
613
614 // Use the received callback to signal a first vsync event.
615 // The interceptor should receive the event, but not the connection.
616 mCallback->onVSyncEvent(123, 456, 789);
617 expectInterceptCallReceived(123);
618 expectThrottleVsyncReceived(456, mThrottledConnectionUid);
619 mThrottledConnectionEventCallRecorder.waitForUnexpectedCall();
620
621 // Use the received callback to signal a second vsync event.
622 // The interceptor should receive the event, but the connection should
623 // not as it was only interested in the first.
624 mCallback->onVSyncEvent(456, 123, 0);
625 expectInterceptCallReceived(456);
626 expectThrottleVsyncReceived(123, mThrottledConnectionUid);
627 EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value());
628
629 // EventThread should not change the vsync state as it didn't send the event
630 // yet
631 EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value());
632}
633
Lloyd Pique24b0a482018-03-09 18:52:26 -0800634} // namespace
635} // namespace android
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100636
637// TODO(b/129481165): remove the #pragma below and fix conversion issues
638#pragma clang diagnostic pop // ignored "-Wextra"