Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wextra" |
| 20 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "LibSurfaceFlingerUnittests" |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <gtest/gtest.h> |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 26 | #include <log/log.h> |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 27 | #include <scheduler/VsyncConfig.h> |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 28 | #include <utils/Errors.h> |
| 29 | |
| 30 | #include "AsyncCallRecorder.h" |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 31 | #include "DisplayHardware/DisplayMode.h" |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 32 | #include "FrameTimeline.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 33 | #include "Scheduler/EventThread.h" |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 34 | #include "mock/MockVSyncDispatch.h" |
| 35 | #include "mock/MockVSyncTracker.h" |
| 36 | #include "mock/MockVsyncController.h" |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 37 | |
| 38 | using namespace std::chrono_literals; |
| 39 | using namespace std::placeholders; |
| 40 | |
| 41 | using testing::_; |
| 42 | using testing::Invoke; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 43 | using testing::Return; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 44 | |
| 45 | namespace android { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 46 | |
Dominik Laskowski | 2f01d77 | 2022-03-23 16:01:29 -0700 | [diff] [blame] | 47 | using namespace ftl::flag_operators; |
| 48 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 49 | namespace { |
| 50 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 51 | constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u); |
| 52 | constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u); |
| 53 | constexpr PhysicalDisplayId DISPLAY_ID_64BIT = |
| 54 | PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu); |
| 55 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 56 | constexpr std::chrono::duration VSYNC_PERIOD(16ms); |
| 57 | |
Huihong Luo | cac9216 | 2023-12-21 13:52:42 -0800 | [diff] [blame] | 58 | constexpr int HDCP_V1 = 2; |
| 59 | constexpr int HDCP_V2 = 3; |
| 60 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 61 | } // namespace |
| 62 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 63 | class EventThreadTest : public testing::Test, public IEventThreadCallback { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 64 | protected: |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 65 | static constexpr std::chrono::nanoseconds kWorkDuration = 0ms; |
| 66 | static constexpr std::chrono::nanoseconds kReadyDuration = 3ms; |
| 67 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 68 | class MockEventThreadConnection : public EventThreadConnection { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 69 | public: |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 70 | MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid, |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 71 | EventRegistrationFlags eventRegistration) |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 72 | : EventThreadConnection(eventThread, callingUid, eventRegistration) {} |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 73 | MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event)); |
| 74 | }; |
| 75 | |
| 76 | using ConnectionEventRecorder = |
| 77 | AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>; |
| 78 | |
| 79 | EventThreadTest(); |
| 80 | ~EventThreadTest() override; |
| 81 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 82 | void SetUp() override { mVsyncPeriod = VSYNC_PERIOD; } |
| 83 | |
| 84 | // IEventThreadCallback overrides |
| 85 | bool throttleVsync(TimePoint, uid_t) override; |
| 86 | Period getVsyncPeriod(uid_t) override; |
| 87 | void resync() override; |
| 88 | |
| 89 | void setupEventThread(); |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 90 | sp<MockEventThreadConnection> createConnection(ConnectionEventRecorder& recorder, |
| 91 | EventRegistrationFlags eventRegistration = {}, |
| 92 | uid_t ownerUid = mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 93 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 94 | void expectVSyncCallbackScheduleReceived(bool expectState); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 95 | void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration, |
| 96 | std::chrono::nanoseconds expectedReadyDuration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 97 | void expectVsyncEventReceivedByConnection(const char* name, |
| 98 | ConnectionEventRecorder& connectionEventRecorder, |
| 99 | nsecs_t expectedTimestamp, unsigned expectedCount); |
| 100 | void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 101 | void expectVsyncEventFrameTimelinesCorrect( |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 102 | nsecs_t expectedTimestamp, gui::VsyncEventData::FrameTimeline preferredVsyncData); |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 103 | void expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 104 | void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 105 | bool expectedConnected); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 106 | void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 107 | int32_t expectedConfigId, |
| 108 | nsecs_t expectedVsyncPeriod); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 109 | void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 110 | void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 111 | std::vector<FrameRateOverride>); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 112 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 113 | void onVSyncEvent(nsecs_t timestamp, nsecs_t expectedPresentationTime, |
| 114 | nsecs_t deadlineTimestamp) { |
| 115 | mThread->onVsync(expectedPresentationTime, timestamp, deadlineTimestamp); |
| 116 | } |
| 117 | |
| 118 | AsyncCallRecorderWithCannedReturn< |
| 119 | scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken, |
| 120 | scheduler::VSyncDispatch::ScheduleTiming)> |
| 121 | mVSyncCallbackScheduleRecorder{0}; |
| 122 | AsyncCallRecorderWithCannedReturn< |
| 123 | scheduler::ScheduleResult (*)(scheduler::VSyncDispatch::CallbackToken, |
| 124 | scheduler::VSyncDispatch::ScheduleTiming)> |
| 125 | mVSyncCallbackUpdateRecorder{0}; |
| 126 | AsyncCallRecorderWithCannedReturn< |
| 127 | scheduler::VSyncDispatch::CallbackToken (*)(scheduler::VSyncDispatch::Callback, |
| 128 | std::string)> |
| 129 | mVSyncCallbackRegisterRecorder{scheduler::VSyncDispatch::CallbackToken(0)}; |
| 130 | AsyncCallRecorder<void (*)(scheduler::VSyncDispatch::CallbackToken)> |
| 131 | mVSyncCallbackUnregisterRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 132 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 133 | AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 134 | ConnectionEventRecorder mConnectionEventCallRecorder{0}; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 135 | ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0}; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 136 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 137 | std::shared_ptr<scheduler::VsyncSchedule> mVsyncSchedule; |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 138 | std::unique_ptr<impl::EventThread> mThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 139 | sp<MockEventThreadConnection> mConnection; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 140 | sp<MockEventThreadConnection> mThrottledConnection; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 141 | std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 142 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 143 | std::chrono::nanoseconds mVsyncPeriod; |
| 144 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 145 | static constexpr uid_t mConnectionUid = 443; |
| 146 | static constexpr uid_t mThrottledConnectionUid = 177; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | EventThreadTest::EventThreadTest() { |
| 150 | const ::testing::TestInfo* const test_info = |
| 151 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 152 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 153 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 154 | auto mockDispatchPtr = std::make_shared<mock::VSyncDispatch>(); |
| 155 | mVsyncSchedule = std::shared_ptr<scheduler::VsyncSchedule>( |
| 156 | new scheduler::VsyncSchedule(INTERNAL_DISPLAY_ID, |
| 157 | std::make_shared<mock::VSyncTracker>(), mockDispatchPtr, |
| 158 | nullptr)); |
| 159 | mock::VSyncDispatch& mockDispatch = *mockDispatchPtr; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 160 | EXPECT_CALL(mockDispatch, registerCallback(_, _)) |
| 161 | .WillRepeatedly(Invoke(mVSyncCallbackRegisterRecorder.getInvocable())); |
| 162 | EXPECT_CALL(mockDispatch, schedule(_, _)) |
| 163 | .WillRepeatedly(Invoke(mVSyncCallbackScheduleRecorder.getInvocable())); |
| 164 | EXPECT_CALL(mockDispatch, update(_, _)) |
| 165 | .WillRepeatedly(Invoke(mVSyncCallbackUpdateRecorder.getInvocable())); |
| 166 | EXPECT_CALL(mockDispatch, unregisterCallback(_)) |
| 167 | .WillRepeatedly(Invoke(mVSyncCallbackUnregisterRecorder.getInvocable())); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | EventThreadTest::~EventThreadTest() { |
| 171 | const ::testing::TestInfo* const test_info = |
| 172 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 173 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 174 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 175 | mThread.reset(); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 176 | // EventThread should unregister itself as VSyncSource callback. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 177 | EXPECT_TRUE(mVSyncCallbackUnregisterRecorder.waitForCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 180 | bool EventThreadTest::throttleVsync(android::TimePoint expectedVsyncTimestamp, uid_t uid) { |
| 181 | mThrottleVsyncCallRecorder.recordCall(expectedVsyncTimestamp.ns(), uid); |
| 182 | return (uid == mThrottledConnectionUid); |
| 183 | } |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 184 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 185 | Period EventThreadTest::getVsyncPeriod(uid_t) { |
| 186 | return mVsyncPeriod; |
| 187 | } |
| 188 | |
| 189 | void EventThreadTest::resync() { |
| 190 | mResyncCallRecorder.recordCall(); |
| 191 | } |
| 192 | |
| 193 | void EventThreadTest::setupEventThread() { |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 194 | mTokenManager = std::make_unique<frametimeline::impl::TokenManager>(); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 195 | mThread = std::make_unique<impl::EventThread>("EventThreadTest", mVsyncSchedule, |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 196 | mTokenManager.get(), *this, kWorkDuration, |
| 197 | kReadyDuration); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 198 | |
| 199 | // EventThread should register itself as VSyncSource callback. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 200 | EXPECT_TRUE(mVSyncCallbackRegisterRecorder.waitForCall().has_value()); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 201 | |
| 202 | mConnection = |
| 203 | createConnection(mConnectionEventCallRecorder, |
| 204 | gui::ISurfaceComposer::EventRegistration::modeChanged | |
| 205 | gui::ISurfaceComposer::EventRegistration::frameRateOverride); |
| 206 | mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder, |
| 207 | gui::ISurfaceComposer::EventRegistration::modeChanged, |
| 208 | mThrottledConnectionUid); |
| 209 | |
| 210 | // A display must be connected for VSYNC events to be delivered. |
| 211 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 212 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 216 | ConnectionEventRecorder& recorder, EventRegistrationFlags eventRegistration, |
| 217 | uid_t ownerUid) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 218 | sp<MockEventThreadConnection> connection = |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 219 | sp<MockEventThreadConnection>::make(mThread.get(), ownerUid, eventRegistration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 220 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 221 | return connection; |
| 222 | } |
| 223 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 224 | void EventThreadTest::expectVSyncCallbackScheduleReceived(bool expectState) { |
| 225 | if (expectState) { |
| 226 | ASSERT_TRUE(mVSyncCallbackScheduleRecorder.waitForCall().has_value()); |
| 227 | } else { |
| 228 | ASSERT_FALSE(mVSyncCallbackScheduleRecorder.waitForUnexpectedCall().has_value()); |
| 229 | } |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 232 | void EventThreadTest::expectVSyncSetDurationCallReceived( |
| 233 | std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 234 | auto args = mVSyncCallbackUpdateRecorder.waitForCall(); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 235 | ASSERT_TRUE(args.has_value()); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 236 | EXPECT_EQ(expectedDuration.count(), std::get<1>(args.value()).workDuration); |
| 237 | EXPECT_EQ(expectedReadyDuration.count(), std::get<1>(args.value()).readyDuration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 240 | void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 241 | auto args = mThrottleVsyncCallRecorder.waitForCall(); |
| 242 | ASSERT_TRUE(args.has_value()); |
| 243 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 244 | EXPECT_EQ(uid, std::get<1>(args.value())); |
| 245 | } |
| 246 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 247 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 248 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 249 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 250 | auto args = connectionEventRecorder.waitForCall(); |
| 251 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 252 | << expectedTimestamp; |
| 253 | const auto& event = std::get<0>(args.value()); |
| 254 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 255 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 256 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 257 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 258 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 259 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 260 | } |
| 261 | |
| 262 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 263 | unsigned expectedCount) { |
| 264 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 265 | mConnectionEventCallRecorder, expectedTimestamp, |
| 266 | expectedCount); |
| 267 | } |
| 268 | |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 269 | void EventThreadTest::expectVsyncEventFrameTimelinesCorrect( |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 270 | nsecs_t expectedTimestamp, VsyncEventData::FrameTimeline preferredVsyncData) { |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 271 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 272 | ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp " |
| 273 | << expectedTimestamp; |
| 274 | const auto& event = std::get<0>(args.value()); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 275 | for (int i = 0; i < event.vsync.vsyncData.frameTimelinesLength; i++) { |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 276 | auto prediction = mTokenManager->getPredictionsForToken( |
| 277 | event.vsync.vsyncData.frameTimelines[i].vsyncId); |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame] | 278 | EXPECT_TRUE(prediction.has_value()); |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 279 | EXPECT_EQ(prediction.value().endTime, |
| 280 | event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp) |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame] | 281 | << "Deadline timestamp does not match cached value"; |
| 282 | EXPECT_EQ(prediction.value().presentTime, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 283 | event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime) |
| 284 | << "Expected vsync.vsyncData timestamp does not match cached value"; |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame] | 285 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 286 | if (i > 0) { |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 287 | EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp, |
| 288 | event.vsync.vsyncData.frameTimelines[i - 1].deadlineTimestamp) |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 289 | << "Deadline timestamp out of order for frame timeline " << i; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 290 | EXPECT_GT(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime, |
| 291 | event.vsync.vsyncData.frameTimelines[i - 1].expectedPresentationTime) |
| 292 | << "Expected vsync.vsyncData timestamp out of order for frame timeline " << i; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 293 | } |
Rachel Lee | 0d94320 | 2022-02-01 23:29:41 -0800 | [diff] [blame] | 294 | |
| 295 | // Vsync ID order lines up with registration into test token manager. |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 296 | EXPECT_EQ(i, event.vsync.vsyncData.frameTimelines[i].vsyncId) |
Rachel Lee | 0d94320 | 2022-02-01 23:29:41 -0800 | [diff] [blame] | 297 | << "Vsync ID incorrect for frame timeline " << i; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 298 | if (i == event.vsync.vsyncData.preferredFrameTimelineIndex) { |
| 299 | EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].deadlineTimestamp, |
| 300 | preferredVsyncData.deadlineTimestamp) |
Rachel Lee | 0d94320 | 2022-02-01 23:29:41 -0800 | [diff] [blame] | 301 | << "Preferred deadline timestamp incorrect" << i; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 302 | EXPECT_EQ(event.vsync.vsyncData.frameTimelines[i].expectedPresentationTime, |
| 303 | preferredVsyncData.expectedPresentationTime) |
| 304 | << "Preferred expected vsync.vsyncData timestamp incorrect" << i; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 309 | void EventThreadTest::expectVsyncEventDataFrameTimelinesValidLength(VsyncEventData vsyncEventData) { |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 310 | float nonPreferredTimelinesAmount = |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 311 | scheduler::VsyncConfig::kEarlyLatchMaxThreshold / mVsyncPeriod; |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 312 | EXPECT_LE(vsyncEventData.frameTimelinesLength, nonPreferredTimelinesAmount + 1) |
| 313 | << "Amount of non-preferred frame timelines too many;" |
| 314 | << " expected presentation time will be over threshold"; |
Rachel Lee | 40aef42 | 2023-04-25 14:35:47 -0700 | [diff] [blame] | 315 | EXPECT_LT(nonPreferredTimelinesAmount, VsyncEventData::kFrameTimelinesCapacity) |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 316 | << "Amount of non-preferred frame timelines should be less than max capacity"; |
| 317 | EXPECT_GT(static_cast<int64_t>(vsyncEventData.frameTimelinesLength), 0) |
| 318 | << "Frame timelines length should be greater than 0"; |
| 319 | EXPECT_LT(vsyncEventData.preferredFrameTimelineIndex, vsyncEventData.frameTimelinesLength) |
| 320 | << "Preferred frame timeline index should be less than frame timelines length"; |
| 321 | } |
| 322 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 323 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 324 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 325 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 326 | ASSERT_TRUE(args.has_value()); |
| 327 | const auto& event = std::get<0>(args.value()); |
| 328 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 329 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 330 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 331 | } |
| 332 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 333 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 334 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId, |
| 335 | nsecs_t expectedVsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 336 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 337 | ASSERT_TRUE(args.has_value()); |
| 338 | const auto& event = std::get<0>(args.value()); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 339 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 340 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 341 | EXPECT_EQ(expectedConfigId, event.modeChange.modeId); |
| 342 | EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 345 | void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection( |
| 346 | PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) { |
| 347 | for (const auto [uid, frameRateHz] : expectedOverrides) { |
| 348 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 349 | ASSERT_TRUE(args.has_value()); |
| 350 | const auto& event = std::get<0>(args.value()); |
| 351 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type); |
| 352 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 353 | EXPECT_EQ(uid, event.frameRateOverride.uid); |
| 354 | EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz); |
| 355 | } |
| 356 | |
| 357 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 358 | ASSERT_TRUE(args.has_value()); |
| 359 | const auto& event = std::get<0>(args.value()); |
| 360 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type); |
| 361 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 362 | } |
| 363 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 364 | namespace { |
| 365 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 366 | using namespace testing; |
| 367 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 368 | /* ------------------------------------------------------------------------ |
| 369 | * Test cases |
| 370 | */ |
| 371 | |
| 372 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 373 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 374 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 375 | EXPECT_FALSE(mVSyncCallbackRegisterRecorder.waitForCall(0us).has_value()); |
| 376 | EXPECT_FALSE(mVSyncCallbackScheduleRecorder.waitForCall(0us).has_value()); |
| 377 | EXPECT_FALSE(mVSyncCallbackUpdateRecorder.waitForCall(0us).has_value()); |
| 378 | EXPECT_FALSE(mVSyncCallbackUnregisterRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 379 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 380 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 381 | } |
| 382 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 383 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 384 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 385 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 386 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 387 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 388 | |
| 389 | // Signal that we want the next vsync event to be posted to the connection. |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 390 | mThread->requestNextVsync(mConnection); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 391 | |
| 392 | // EventThread should not enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 393 | expectVSyncCallbackScheduleReceived(false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 396 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 397 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 398 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 399 | // Signal that we want the next vsync event to be posted to the connection |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 400 | mThread->requestNextVsync(mConnection); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 401 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 402 | // EventThread should immediately request a resync. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 403 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 404 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 405 | // EventThread should enable schedule a vsync callback |
| 406 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 407 | |
| 408 | // Use the received callback to signal a first vsync event. |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 409 | // The throttler should receive the event, as well as the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 410 | onVSyncEvent(123, 456, 789); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 411 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 412 | expectVsyncEventReceivedByConnection(123, 1u); |
| 413 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 414 | // EventThread is requesting one more callback due to VsyncRequest::SingleSuppressCallback |
| 415 | expectVSyncCallbackScheduleReceived(true); |
| 416 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 417 | // Use the received callback to signal a second vsync event. |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 418 | // The throttler should receive the event, but the connection should |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 419 | // not as it was only interested in the first. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 420 | onVSyncEvent(456, 123, 0); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 421 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 422 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 423 | |
| 424 | // EventThread should also detect that at this point that it does not need |
| 425 | // any more vsync events, and should disable their generation. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 426 | expectVSyncCallbackScheduleReceived(false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 429 | TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 430 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 431 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 432 | // Signal that we want the next vsync event to be posted to the connection |
| 433 | mThread->requestNextVsync(mConnection); |
| 434 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 435 | expectVSyncCallbackScheduleReceived(true); |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 436 | |
| 437 | // Use the received callback to signal a vsync event. |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 438 | // The throttler should receive the event, as well as the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 439 | onVSyncEvent(123, 456, 789); |
| 440 | expectVsyncEventFrameTimelinesCorrect(123, {-1, 789, 456}); |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 443 | TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesValidLength) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 444 | setupEventThread(); |
Rachel Lee | 40aef42 | 2023-04-25 14:35:47 -0700 | [diff] [blame] | 445 | // The VsyncEventData should not have kFrameTimelinesCapacity amount of valid frame timelines, |
| 446 | // due to longer vsync period and kEarlyLatchMaxThreshold. Use length-2 to avoid decimal |
| 447 | // truncation (e.g. 60Hz has 16.6... ms vsync period). |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 448 | mVsyncPeriod = (scheduler::VsyncConfig::kEarlyLatchMaxThreshold / |
| 449 | (VsyncEventData::kFrameTimelinesCapacity - 2)); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 450 | |
| 451 | // Signal that we want the next vsync event to be posted to the connection |
| 452 | mThread->requestNextVsync(mConnection); |
| 453 | |
| 454 | expectVSyncCallbackScheduleReceived(true); |
| 455 | |
| 456 | // Use the received callback to signal a vsync event. |
| 457 | // The throttler should receive the event, as well as the connection. |
| 458 | nsecs_t expectedTimestamp = 123; |
| 459 | onVSyncEvent(expectedTimestamp, 456, 789); |
| 460 | |
| 461 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 462 | ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp " |
| 463 | << expectedTimestamp; |
| 464 | const VsyncEventData vsyncEventData = std::get<0>(args.value()).vsync.vsyncData; |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 465 | expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 468 | TEST_F(EventThreadTest, getLatestVsyncEventData) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 469 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 470 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 471 | const nsecs_t now = systemTime(); |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 472 | const nsecs_t preferredExpectedPresentationTime = now + 20000000; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 473 | const nsecs_t preferredDeadline = preferredExpectedPresentationTime - kReadyDuration.count(); |
| 474 | |
| 475 | mock::VSyncTracker& mockTracker = |
| 476 | *static_cast<mock::VSyncTracker*>(&mVsyncSchedule->getTracker()); |
Ady Abraham | 4335afd | 2023-12-18 19:10:47 -0800 | [diff] [blame] | 477 | EXPECT_CALL(mockTracker, nextAnticipatedVSyncTimeFrom(_, _)) |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 478 | .WillOnce(Return(preferredExpectedPresentationTime)); |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 479 | |
| 480 | VsyncEventData vsyncEventData = mThread->getLatestVsyncEventData(mConnection); |
Rachel Lee | b5223cf | 2022-03-14 14:39:27 -0700 | [diff] [blame] | 481 | |
| 482 | // Check EventThread immediately requested a resync. |
| 483 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 484 | |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 485 | expectVsyncEventDataFrameTimelinesValidLength(vsyncEventData); |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 486 | EXPECT_GT(vsyncEventData.frameTimelines[0].deadlineTimestamp, now) |
| 487 | << "Deadline timestamp should be greater than frame time"; |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 488 | for (size_t i = 0; i < vsyncEventData.frameTimelinesLength; i++) { |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 489 | auto prediction = |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 490 | mTokenManager->getPredictionsForToken(vsyncEventData.frameTimelines[i].vsyncId); |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 491 | EXPECT_TRUE(prediction.has_value()); |
| 492 | EXPECT_EQ(prediction.value().endTime, vsyncEventData.frameTimelines[i].deadlineTimestamp) |
| 493 | << "Deadline timestamp does not match cached value"; |
| 494 | EXPECT_EQ(prediction.value().presentTime, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 495 | vsyncEventData.frameTimelines[i].expectedPresentationTime) |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 496 | << "Expected vsync timestamp does not match cached value"; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 497 | EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime, |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 498 | vsyncEventData.frameTimelines[i].deadlineTimestamp) |
| 499 | << "Expected vsync timestamp should be greater than deadline"; |
| 500 | |
| 501 | if (i > 0) { |
| 502 | EXPECT_GT(vsyncEventData.frameTimelines[i].deadlineTimestamp, |
| 503 | vsyncEventData.frameTimelines[i - 1].deadlineTimestamp) |
| 504 | << "Deadline timestamp out of order for frame timeline " << i; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 505 | EXPECT_GT(vsyncEventData.frameTimelines[i].expectedPresentationTime, |
| 506 | vsyncEventData.frameTimelines[i - 1].expectedPresentationTime) |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 507 | << "Expected vsync timestamp out of order for frame timeline " << i; |
| 508 | } |
| 509 | |
| 510 | // Vsync ID order lines up with registration into test token manager. |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 511 | EXPECT_EQ(i, vsyncEventData.frameTimelines[i].vsyncId) |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 512 | << "Vsync ID incorrect for frame timeline " << i; |
| 513 | if (i == vsyncEventData.preferredFrameTimelineIndex) { |
| 514 | EXPECT_EQ(vsyncEventData.frameTimelines[i].deadlineTimestamp, preferredDeadline) |
| 515 | << "Preferred deadline timestamp incorrect" << i; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 516 | EXPECT_EQ(vsyncEventData.frameTimelines[i].expectedPresentationTime, |
| 517 | preferredExpectedPresentationTime) |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 518 | << "Preferred expected vsync timestamp incorrect" << i; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 523 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 524 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 525 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 526 | // Create a first connection, register it, and request a vsync rate of zero. |
| 527 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 528 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 529 | mThread->setVsyncRate(0, firstConnection); |
| 530 | |
| 531 | // By itself, this should not enable vsync events |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 532 | expectVSyncCallbackScheduleReceived(false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 533 | |
| 534 | // However if there is another connection which wants events at a nonzero rate..... |
| 535 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 536 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 537 | createConnection(secondConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 538 | mThread->setVsyncRate(1, secondConnection); |
| 539 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 540 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 541 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 542 | |
| 543 | // Send a vsync event. EventThread should then make a call to the |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 544 | // the second connection. The first connection should not |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 545 | // get the event. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 546 | onVSyncEvent(123, 0456, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 547 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 548 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 549 | 1u); |
| 550 | } |
| 551 | |
| 552 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 553 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 554 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 555 | mThread->setVsyncRate(1, mConnection); |
| 556 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 557 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 558 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 559 | |
| 560 | // Send a vsync event. EventThread should then make a call to the |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 561 | // throttler, and the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 562 | onVSyncEvent(123, 456, 789); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 563 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 564 | expectVsyncEventReceivedByConnection(123, 1u); |
| 565 | |
| 566 | // A second event should go to the same places. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 567 | onVSyncEvent(456, 123, 0); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 568 | expectThrottleVsyncReceived(123, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 569 | expectVsyncEventReceivedByConnection(456, 2u); |
| 570 | |
| 571 | // A third event should go to the same places. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 572 | onVSyncEvent(789, 777, 111); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 573 | expectThrottleVsyncReceived(777, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 574 | expectVsyncEventReceivedByConnection(789, 3u); |
| 575 | } |
| 576 | |
| 577 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 578 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 579 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 580 | mThread->setVsyncRate(2, mConnection); |
| 581 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 582 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 583 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 584 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 585 | // The first event will not be seen by the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 586 | onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 587 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 588 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 589 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 590 | // The second event will be seen by the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 591 | onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 592 | expectVsyncEventReceivedByConnection(456, 2u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 593 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 594 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 595 | // The third event will not be seen by the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 596 | onVSyncEvent(789, 777, 744); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 597 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 598 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 599 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 600 | // The fourth event will be seen by the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 601 | onVSyncEvent(101112, 7847, 86); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 602 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 603 | } |
| 604 | |
| 605 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 606 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 607 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 608 | mThread->setVsyncRate(1, mConnection); |
| 609 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 610 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 611 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 612 | |
| 613 | // Destroy the only (strong) reference to the connection. |
| 614 | mConnection = nullptr; |
| 615 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 616 | // The first event will not be seen by the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 617 | onVSyncEvent(123, 56, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 618 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 619 | |
| 620 | // EventThread should disable vsync callbacks |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 621 | expectVSyncCallbackScheduleReceived(false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 625 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 626 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 627 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 628 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 629 | mThread->setVsyncRate(1, errorConnection); |
| 630 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 631 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 632 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 633 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 634 | // The first event will be seen by the connection, which then returns an error. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 635 | onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 636 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 637 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 638 | // Another schedule is expected, since the connection is removed only after |
| 639 | // the next vsync is requested. |
| 640 | expectVSyncCallbackScheduleReceived(true); |
| 641 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 642 | // A subsequent event will not be seen by the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 643 | onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 644 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 645 | |
| 646 | // EventThread should disable vsync callbacks with the second event |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 647 | expectVSyncCallbackScheduleReceived(false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 651 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 652 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 653 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 654 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 655 | mThread->setVsyncRate(1, errorConnection); |
| 656 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 657 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 658 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 659 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 660 | // The first event will be seen by the connection, which then returns a non-fatal error. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 661 | onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 662 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 663 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 664 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 665 | // A subsequent event will be seen by the connection, which still then returns a non-fatal |
| 666 | // error. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 667 | onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 668 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 669 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 670 | |
| 671 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 672 | onVSyncEvent(456, 123, 0); |
| 673 | expectVSyncCallbackScheduleReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 677 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 678 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 679 | mThread->setDuration(321ns, 456ns); |
| 680 | expectVSyncSetDurationCallReceived(321ns, 456ns); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 683 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 684 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 685 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 686 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 687 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 690 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 691 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 692 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 693 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 694 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 698 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 699 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 700 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 701 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 705 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 706 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 707 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 708 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 711 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 712 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 713 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 714 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 715 | .setPhysicalDisplayId(INTERNAL_DISPLAY_ID) |
| 716 | .setId(DisplayModeId(7)) |
| 717 | .setVsyncPeriod(16666666) |
| 718 | .build(); |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 719 | const Fps fps = mode->getPeakFps() / 2; |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 720 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 721 | mThread->onModeChanged({fps, ftl::as_non_null(mode)}); |
| 722 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, fps.getPeriodNsecs()); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 726 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 727 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 728 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 729 | .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID) |
| 730 | .setId(DisplayModeId(5)) |
| 731 | .setVsyncPeriod(16666666) |
| 732 | .build(); |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 733 | const Fps fps = mode->getPeakFps() / 2; |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 734 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 735 | mThread->onModeChanged({fps, ftl::as_non_null(mode)}); |
| 736 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, fps.getPeriodNsecs()); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 739 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 740 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 741 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 742 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 743 | .setPhysicalDisplayId(DISPLAY_ID_64BIT) |
| 744 | .setId(DisplayModeId(7)) |
| 745 | .setVsyncPeriod(16666666) |
| 746 | .build(); |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 747 | const Fps fps = mode->getPeakFps() / 2; |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 748 | mThread->onModeChanged({fps, ftl::as_non_null(mode)}); |
| 749 | expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, fps.getPeriodNsecs()); |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 752 | TEST_F(EventThreadTest, suppressConfigChanged) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 753 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 754 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 755 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 756 | sp<MockEventThreadConnection> suppressConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 757 | createConnection(suppressConnectionEventRecorder); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 758 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 759 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 760 | .setPhysicalDisplayId(INTERNAL_DISPLAY_ID) |
| 761 | .setId(DisplayModeId(9)) |
| 762 | .setVsyncPeriod(16666666) |
| 763 | .build(); |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 764 | const Fps fps = mode->getPeakFps() / 2; |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 765 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 766 | mThread->onModeChanged({fps, ftl::as_non_null(mode)}); |
| 767 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, fps.getPeriodNsecs()); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 768 | |
| 769 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 770 | ASSERT_FALSE(args.has_value()); |
| 771 | } |
| 772 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 773 | TEST_F(EventThreadTest, postUidFrameRateMapping) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 774 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 775 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 776 | const std::vector<FrameRateOverride> overrides = { |
| 777 | {.uid = 1, .frameRateHz = 20}, |
| 778 | {.uid = 3, .frameRateHz = 40}, |
| 779 | {.uid = 5, .frameRateHz = 60}, |
| 780 | }; |
| 781 | |
| 782 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 783 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 784 | } |
| 785 | |
| 786 | TEST_F(EventThreadTest, suppressUidFrameRateMapping) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 787 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 788 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 789 | const std::vector<FrameRateOverride> overrides = { |
| 790 | {.uid = 1, .frameRateHz = 20}, |
| 791 | {.uid = 3, .frameRateHz = 40}, |
| 792 | {.uid = 5, .frameRateHz = 60}, |
| 793 | }; |
| 794 | |
| 795 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 796 | sp<MockEventThreadConnection> suppressConnection = |
| 797 | createConnection(suppressConnectionEventRecorder); |
| 798 | |
| 799 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 800 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 801 | |
| 802 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 803 | ASSERT_FALSE(args.has_value()); |
| 804 | } |
| 805 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 806 | TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) { |
Ady Abraham | f285161 | 2023-09-25 17:19:00 -0700 | [diff] [blame] | 807 | setupEventThread(); |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 808 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 809 | // Signal that we want the next vsync event to be posted to the throttled connection |
| 810 | mThread->requestNextVsync(mThrottledConnection); |
| 811 | |
| 812 | // EventThread should immediately request a resync. |
| 813 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 814 | |
| 815 | // EventThread should enable vsync callbacks. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 816 | expectVSyncCallbackScheduleReceived(true); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 817 | |
| 818 | // Use the received callback to signal a first vsync event. |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 819 | // The throttler should receive the event, but not the connection. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 820 | onVSyncEvent(123, 456, 789); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 821 | expectThrottleVsyncReceived(456, mThrottledConnectionUid); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 822 | mThrottledConnectionEventCallRecorder.waitForUnexpectedCall(); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 823 | expectVSyncCallbackScheduleReceived(true); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 824 | |
| 825 | // Use the received callback to signal a second vsync event. |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 826 | // The throttler should receive the event, but the connection should |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 827 | // not as it was only interested in the first. |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 828 | onVSyncEvent(456, 123, 0); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 829 | expectThrottleVsyncReceived(123, mThrottledConnectionUid); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 830 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 831 | expectVSyncCallbackScheduleReceived(true); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 832 | |
| 833 | // EventThread should not change the vsync state as it didn't send the event |
| 834 | // yet |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 835 | onVSyncEvent(456, 123, 0); |
| 836 | expectVSyncCallbackScheduleReceived(true); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Huihong Luo | cac9216 | 2023-12-21 13:52:42 -0800 | [diff] [blame] | 839 | TEST_F(EventThreadTest, postHcpLevelsChanged) { |
| 840 | setupEventThread(); |
| 841 | |
| 842 | mThread->onHdcpLevelsChanged(EXTERNAL_DISPLAY_ID, HDCP_V1, HDCP_V2); |
| 843 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 844 | ASSERT_TRUE(args.has_value()); |
| 845 | const auto& event = std::get<0>(args.value()); |
| 846 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE, event.header.type); |
| 847 | EXPECT_EQ(EXTERNAL_DISPLAY_ID, event.header.displayId); |
| 848 | EXPECT_EQ(HDCP_V1, event.hdcpLevelsChange.connectedLevel); |
| 849 | EXPECT_EQ(HDCP_V2, event.hdcpLevelsChange.maxLevel); |
| 850 | } |
| 851 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 852 | } // namespace |
| 853 | } // namespace android |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 854 | |
| 855 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 856 | #pragma clang diagnostic pop // ignored "-Wextra" |