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