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