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> |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 27 | #include <utils/Errors.h> |
| 28 | |
| 29 | #include "AsyncCallRecorder.h" |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 30 | #include "DisplayHardware/DisplayMode.h" |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 31 | #include "FrameTimeline.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 32 | #include "Scheduler/EventThread.h" |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 33 | |
| 34 | using namespace std::chrono_literals; |
| 35 | using namespace std::placeholders; |
| 36 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 37 | using namespace android::flag_operators; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 38 | using testing::_; |
| 39 | using testing::Invoke; |
| 40 | |
| 41 | namespace android { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 42 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 45 | constexpr PhysicalDisplayId INTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(111u); |
| 46 | constexpr PhysicalDisplayId EXTERNAL_DISPLAY_ID = PhysicalDisplayId::fromPort(222u); |
| 47 | constexpr PhysicalDisplayId DISPLAY_ID_64BIT = |
| 48 | PhysicalDisplayId::fromEdid(0xffu, 0xffffu, 0xffff'ffffu); |
| 49 | |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 50 | constexpr std::chrono::duration VSYNC_PERIOD(16ms); |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 51 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 52 | class MockVSyncSource : public VSyncSource { |
| 53 | public: |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 54 | const char* getName() const override { return "test"; } |
| 55 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 56 | MOCK_METHOD1(setVSyncEnabled, void(bool)); |
| 57 | MOCK_METHOD1(setCallback, void(VSyncSource::Callback*)); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 58 | MOCK_METHOD2(setDuration, |
| 59 | void(std::chrono::nanoseconds workDuration, |
| 60 | std::chrono::nanoseconds readyDuration)); |
Ady Abraham | b838aed | 2019-02-12 15:30:16 -0800 | [diff] [blame] | 61 | MOCK_METHOD1(pauseVsyncCallback, void(bool)); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 62 | MOCK_CONST_METHOD1(dump, void(std::string&)); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | class EventThreadTest : public testing::Test { |
| 68 | protected: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 69 | class MockEventThreadConnection : public EventThreadConnection { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 70 | public: |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 71 | MockEventThreadConnection(impl::EventThread* eventThread, uid_t callingUid, |
| 72 | ResyncCallback&& resyncCallback, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 73 | ISurfaceComposer::EventRegistrationFlags eventRegistration) |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 74 | : EventThreadConnection(eventThread, callingUid, std::move(resyncCallback), |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 75 | eventRegistration) {} |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 76 | MOCK_METHOD1(postEvent, status_t(const DisplayEventReceiver::Event& event)); |
| 77 | }; |
| 78 | |
| 79 | using ConnectionEventRecorder = |
| 80 | AsyncCallRecorderWithCannedReturn<status_t (*)(const DisplayEventReceiver::Event&)>; |
| 81 | |
| 82 | EventThreadTest(); |
| 83 | ~EventThreadTest() override; |
| 84 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 85 | void createThread(std::unique_ptr<VSyncSource>); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 86 | sp<MockEventThreadConnection> createConnection( |
| 87 | ConnectionEventRecorder& recorder, |
| 88 | ISurfaceComposer::EventRegistrationFlags eventRegistration = {}, |
| 89 | uid_t ownerUid = mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 90 | |
| 91 | void expectVSyncSetEnabledCallReceived(bool expectedState); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 92 | void expectVSyncSetDurationCallReceived(std::chrono::nanoseconds expectedDuration, |
| 93 | std::chrono::nanoseconds expectedReadyDuration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 94 | VSyncSource::Callback* expectVSyncSetCallbackCallReceived(); |
| 95 | void expectInterceptCallReceived(nsecs_t expectedTimestamp); |
| 96 | void expectVsyncEventReceivedByConnection(const char* name, |
| 97 | ConnectionEventRecorder& connectionEventRecorder, |
| 98 | nsecs_t expectedTimestamp, unsigned expectedCount); |
| 99 | void expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, unsigned expectedCount); |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 100 | void expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp, |
| 101 | nsecs_t preferredDeadline); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 102 | void expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 103 | bool expectedConnected); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 104 | void expectConfigChangedEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 105 | int32_t expectedConfigId, |
| 106 | nsecs_t expectedVsyncPeriod); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 107 | void expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 108 | void expectUidFrameRateMappingEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 109 | std::vector<FrameRateOverride>); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 110 | |
| 111 | AsyncCallRecorder<void (*)(bool)> mVSyncSetEnabledCallRecorder; |
| 112 | AsyncCallRecorder<void (*)(VSyncSource::Callback*)> mVSyncSetCallbackCallRecorder; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 113 | AsyncCallRecorder<void (*)(std::chrono::nanoseconds, std::chrono::nanoseconds)> |
| 114 | mVSyncSetDurationCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 115 | AsyncCallRecorder<void (*)()> mResyncCallRecorder; |
| 116 | AsyncCallRecorder<void (*)(nsecs_t)> mInterceptVSyncCallRecorder; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 117 | AsyncCallRecorder<void (*)(nsecs_t, uid_t)> mThrottleVsyncCallRecorder; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 118 | ConnectionEventRecorder mConnectionEventCallRecorder{0}; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 119 | ConnectionEventRecorder mThrottledConnectionEventCallRecorder{0}; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 120 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 121 | MockVSyncSource* mVSyncSource; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 122 | VSyncSource::Callback* mCallback = nullptr; |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 123 | std::unique_ptr<impl::EventThread> mThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 124 | sp<MockEventThreadConnection> mConnection; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 125 | sp<MockEventThreadConnection> mThrottledConnection; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 126 | std::unique_ptr<frametimeline::impl::TokenManager> mTokenManager; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 127 | |
| 128 | static constexpr uid_t mConnectionUid = 443; |
| 129 | static constexpr uid_t mThrottledConnectionUid = 177; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | EventThreadTest::EventThreadTest() { |
| 133 | const ::testing::TestInfo* const test_info = |
| 134 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 135 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 136 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 137 | auto vsyncSource = std::make_unique<MockVSyncSource>(); |
| 138 | mVSyncSource = vsyncSource.get(); |
| 139 | |
| 140 | EXPECT_CALL(*mVSyncSource, setVSyncEnabled(_)) |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 141 | .WillRepeatedly(Invoke(mVSyncSetEnabledCallRecorder.getInvocable())); |
| 142 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 143 | EXPECT_CALL(*mVSyncSource, setCallback(_)) |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 144 | .WillRepeatedly(Invoke(mVSyncSetCallbackCallRecorder.getInvocable())); |
| 145 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 146 | EXPECT_CALL(*mVSyncSource, setDuration(_, _)) |
| 147 | .WillRepeatedly(Invoke(mVSyncSetDurationCallRecorder.getInvocable())); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 148 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 149 | createThread(std::move(vsyncSource)); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 150 | mConnection = createConnection(mConnectionEventCallRecorder, |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 151 | ISurfaceComposer::EventRegistration::modeChanged | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 152 | ISurfaceComposer::EventRegistration::frameRateOverride); |
| 153 | mThrottledConnection = createConnection(mThrottledConnectionEventCallRecorder, |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 154 | ISurfaceComposer::EventRegistration::modeChanged, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 155 | mThrottledConnectionUid); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 156 | |
| 157 | // A display must be connected for VSYNC events to be delivered. |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 158 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 159 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | EventThreadTest::~EventThreadTest() { |
| 163 | const ::testing::TestInfo* const test_info = |
| 164 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 165 | 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] | 166 | |
| 167 | // EventThread should unregister itself as VSyncSource callback. |
Lloyd Pique | 0655b8e | 2019-01-31 18:20:27 -0800 | [diff] [blame] | 168 | EXPECT_TRUE(!mVSyncSetCallbackCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 171 | void EventThreadTest::createThread(std::unique_ptr<VSyncSource> source) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 172 | const auto throttleVsync = [&](nsecs_t expectedVsyncTimestamp, uid_t uid) { |
| 173 | mThrottleVsyncCallRecorder.getInvocable()(expectedVsyncTimestamp, uid); |
| 174 | return (uid == mThrottledConnectionUid); |
| 175 | }; |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 176 | const auto getVsyncPeriod = [](uid_t uid) { |
| 177 | return VSYNC_PERIOD.count(); |
| 178 | }; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 179 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 180 | mTokenManager = std::make_unique<frametimeline::impl::TokenManager>(); |
| 181 | mThread = std::make_unique<impl::EventThread>(std::move(source), mTokenManager.get(), |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 182 | mInterceptVSyncCallRecorder.getInvocable(), |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 183 | throttleVsync, getVsyncPeriod); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 184 | |
| 185 | // EventThread should register itself as VSyncSource callback. |
| 186 | mCallback = expectVSyncSetCallbackCallReceived(); |
| 187 | ASSERT_TRUE(mCallback); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | sp<EventThreadTest::MockEventThreadConnection> EventThreadTest::createConnection( |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 191 | ConnectionEventRecorder& recorder, |
| 192 | ISurfaceComposer::EventRegistrationFlags eventRegistration, uid_t ownerUid) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 193 | sp<MockEventThreadConnection> connection = |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 194 | new MockEventThreadConnection(mThread.get(), ownerUid, |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 195 | mResyncCallRecorder.getInvocable(), eventRegistration); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 196 | EXPECT_CALL(*connection, postEvent(_)).WillRepeatedly(Invoke(recorder.getInvocable())); |
| 197 | return connection; |
| 198 | } |
| 199 | |
| 200 | void EventThreadTest::expectVSyncSetEnabledCallReceived(bool expectedState) { |
| 201 | auto args = mVSyncSetEnabledCallRecorder.waitForCall(); |
| 202 | ASSERT_TRUE(args.has_value()); |
| 203 | EXPECT_EQ(expectedState, std::get<0>(args.value())); |
| 204 | } |
| 205 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 206 | void EventThreadTest::expectVSyncSetDurationCallReceived( |
| 207 | std::chrono::nanoseconds expectedDuration, std::chrono::nanoseconds expectedReadyDuration) { |
| 208 | auto args = mVSyncSetDurationCallRecorder.waitForCall(); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 209 | ASSERT_TRUE(args.has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 210 | EXPECT_EQ(expectedDuration, std::get<0>(args.value())); |
| 211 | EXPECT_EQ(expectedReadyDuration, std::get<1>(args.value())); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | VSyncSource::Callback* EventThreadTest::expectVSyncSetCallbackCallReceived() { |
| 215 | auto callbackSet = mVSyncSetCallbackCallRecorder.waitForCall(); |
| 216 | return callbackSet.has_value() ? std::get<0>(callbackSet.value()) : nullptr; |
| 217 | } |
| 218 | |
| 219 | void EventThreadTest::expectInterceptCallReceived(nsecs_t expectedTimestamp) { |
| 220 | auto args = mInterceptVSyncCallRecorder.waitForCall(); |
| 221 | ASSERT_TRUE(args.has_value()); |
| 222 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 223 | } |
| 224 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 225 | void EventThreadTest::expectThrottleVsyncReceived(nsecs_t expectedTimestamp, uid_t uid) { |
| 226 | auto args = mThrottleVsyncCallRecorder.waitForCall(); |
| 227 | ASSERT_TRUE(args.has_value()); |
| 228 | EXPECT_EQ(expectedTimestamp, std::get<0>(args.value())); |
| 229 | EXPECT_EQ(uid, std::get<1>(args.value())); |
| 230 | } |
| 231 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 232 | void EventThreadTest::expectVsyncEventReceivedByConnection( |
| 233 | const char* name, ConnectionEventRecorder& connectionEventRecorder, |
| 234 | nsecs_t expectedTimestamp, unsigned expectedCount) { |
| 235 | auto args = connectionEventRecorder.waitForCall(); |
| 236 | ASSERT_TRUE(args.has_value()) << name << " did not receive an event for timestamp " |
| 237 | << expectedTimestamp; |
| 238 | const auto& event = std::get<0>(args.value()); |
| 239 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_VSYNC, event.header.type) |
| 240 | << name << " did not get the correct event for timestamp " << expectedTimestamp; |
| 241 | EXPECT_EQ(expectedTimestamp, event.header.timestamp) |
| 242 | << name << " did not get the expected timestamp for timestamp " << expectedTimestamp; |
| 243 | EXPECT_EQ(expectedCount, event.vsync.count) |
| 244 | << name << " did not get the expected count for timestamp " << expectedTimestamp; |
| 245 | } |
| 246 | |
| 247 | void EventThreadTest::expectVsyncEventReceivedByConnection(nsecs_t expectedTimestamp, |
| 248 | unsigned expectedCount) { |
| 249 | expectVsyncEventReceivedByConnection("mConnectionEventCallRecorder", |
| 250 | mConnectionEventCallRecorder, expectedTimestamp, |
| 251 | expectedCount); |
| 252 | } |
| 253 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 254 | void EventThreadTest::expectVsyncEventFrameTimelinesCorrect(nsecs_t expectedTimestamp, |
| 255 | nsecs_t preferredDeadline) { |
| 256 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 257 | ASSERT_TRUE(args.has_value()) << " did not receive an event for timestamp " |
| 258 | << expectedTimestamp; |
| 259 | const auto& event = std::get<0>(args.value()); |
| 260 | for (int i = 0; i < DisplayEventReceiver::kFrameTimelinesLength; i++) { |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame^] | 261 | auto prediction = |
| 262 | mTokenManager->getPredictionsForToken(event.vsync.frameTimelines[i].vsyncId); |
| 263 | EXPECT_TRUE(prediction.has_value()); |
| 264 | EXPECT_EQ(prediction.value().endTime, event.vsync.frameTimelines[i].deadlineTimestamp) |
| 265 | << "Deadline timestamp does not match cached value"; |
| 266 | EXPECT_EQ(prediction.value().presentTime, |
| 267 | event.vsync.frameTimelines[i].expectedVSyncTimestamp) |
| 268 | << "Expected vsync timestamp does not match cached value"; |
| 269 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 270 | if (i > 0) { |
| 271 | EXPECT_GT(event.vsync.frameTimelines[i].deadlineTimestamp, |
| 272 | event.vsync.frameTimelines[i - 1].deadlineTimestamp) |
| 273 | << "Deadline timestamp out of order for frame timeline " << i; |
| 274 | EXPECT_GT(event.vsync.frameTimelines[i].expectedVSyncTimestamp, |
| 275 | event.vsync.frameTimelines[i - 1].expectedVSyncTimestamp) |
| 276 | << "Expected vsync timestamp out of order for frame timeline " << i; |
| 277 | } |
| 278 | if (event.vsync.frameTimelines[i].deadlineTimestamp == preferredDeadline) { |
| 279 | EXPECT_EQ(i, event.vsync.preferredFrameTimelineIndex) |
| 280 | << "Preferred frame timeline index should be " << i; |
| 281 | // For the platform-preferred frame timeline, the vsync ID is 0 because the first frame |
| 282 | // timeline is made before the rest. |
| 283 | EXPECT_EQ(0, event.vsync.frameTimelines[i].vsyncId) |
| 284 | << "Vsync ID incorrect for frame timeline " << i; |
| 285 | } else { |
| 286 | // Vsync ID 0 is used for the preferred frame timeline. |
| 287 | EXPECT_EQ(i + 1, event.vsync.frameTimelines[i].vsyncId) |
| 288 | << "Vsync ID incorrect for frame timeline " << i; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 293 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 294 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 295 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 296 | ASSERT_TRUE(args.has_value()); |
| 297 | const auto& event = std::get<0>(args.value()); |
| 298 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 299 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 300 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 301 | } |
| 302 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 303 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 304 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId, |
| 305 | nsecs_t expectedVsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 306 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 307 | ASSERT_TRUE(args.has_value()); |
| 308 | const auto& event = std::get<0>(args.value()); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 309 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 310 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 311 | EXPECT_EQ(expectedConfigId, event.modeChange.modeId); |
| 312 | EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 315 | void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection( |
| 316 | PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) { |
| 317 | for (const auto [uid, frameRateHz] : expectedOverrides) { |
| 318 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 319 | ASSERT_TRUE(args.has_value()); |
| 320 | const auto& event = std::get<0>(args.value()); |
| 321 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type); |
| 322 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 323 | EXPECT_EQ(uid, event.frameRateOverride.uid); |
| 324 | EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz); |
| 325 | } |
| 326 | |
| 327 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 328 | ASSERT_TRUE(args.has_value()); |
| 329 | const auto& event = std::get<0>(args.value()); |
| 330 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, event.header.type); |
| 331 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 332 | } |
| 333 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 334 | namespace { |
| 335 | |
| 336 | /* ------------------------------------------------------------------------ |
| 337 | * Test cases |
| 338 | */ |
| 339 | |
| 340 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 341 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 342 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 343 | EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 344 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
| 345 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 346 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 347 | } |
| 348 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 349 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 350 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 351 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 352 | |
| 353 | // 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] | 354 | mThread->requestNextVsync(mConnection); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 355 | |
| 356 | // EventThread should not enable vsync callbacks. |
| 357 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 360 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 361 | // 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] | 362 | mThread->requestNextVsync(mConnection); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 363 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 364 | // EventThread should immediately request a resync. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 365 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 366 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 367 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 368 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 369 | |
| 370 | // Use the received callback to signal a first vsync event. |
| 371 | // The interceptor should receive the event, as well as the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 372 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 373 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 374 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 375 | expectVsyncEventReceivedByConnection(123, 1u); |
| 376 | |
| 377 | // Use the received callback to signal a second vsync event. |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 378 | // The interceptor should receive the event, but the connection should |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 379 | // not as it was only interested in the first. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 380 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 381 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 382 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 383 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 384 | |
| 385 | // EventThread should also detect that at this point that it does not need |
| 386 | // any more vsync events, and should disable their generation. |
| 387 | expectVSyncSetEnabledCallReceived(false); |
| 388 | } |
| 389 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 390 | TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) { |
| 391 | // Signal that we want the next vsync event to be posted to the connection |
| 392 | mThread->requestNextVsync(mConnection); |
| 393 | |
| 394 | expectVSyncSetEnabledCallReceived(true); |
| 395 | |
| 396 | // Use the received callback to signal a vsync event. |
| 397 | // The interceptor should receive the event, as well as the connection. |
| 398 | mCallback->onVSyncEvent(123, 456, 789); |
| 399 | expectInterceptCallReceived(123); |
| 400 | expectVsyncEventFrameTimelinesCorrect(123, 789); |
| 401 | } |
| 402 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 403 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 404 | // Create a first connection, register it, and request a vsync rate of zero. |
| 405 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 406 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 407 | mThread->setVsyncRate(0, firstConnection); |
| 408 | |
| 409 | // By itself, this should not enable vsync events |
| 410 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 411 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 412 | |
| 413 | // However if there is another connection which wants events at a nonzero rate..... |
| 414 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 415 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 416 | createConnection(secondConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 417 | mThread->setVsyncRate(1, secondConnection); |
| 418 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 419 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 420 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 421 | |
| 422 | // Send a vsync event. EventThread should then make a call to the |
| 423 | // interceptor, and the second connection. The first connection should not |
| 424 | // get the event. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 425 | mCallback->onVSyncEvent(123, 456, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 426 | expectInterceptCallReceived(123); |
| 427 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 428 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 429 | 1u); |
| 430 | } |
| 431 | |
| 432 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 433 | mThread->setVsyncRate(1, mConnection); |
| 434 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 435 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 436 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 437 | |
| 438 | // Send a vsync event. EventThread should then make a call to the |
| 439 | // interceptor, and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 440 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 441 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 442 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 443 | expectVsyncEventReceivedByConnection(123, 1u); |
| 444 | |
| 445 | // A second event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 446 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 447 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 448 | expectThrottleVsyncReceived(123, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 449 | expectVsyncEventReceivedByConnection(456, 2u); |
| 450 | |
| 451 | // A third event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 452 | mCallback->onVSyncEvent(789, 777, 111); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 453 | expectInterceptCallReceived(789); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 454 | expectThrottleVsyncReceived(777, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 455 | expectVsyncEventReceivedByConnection(789, 3u); |
| 456 | } |
| 457 | |
| 458 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 459 | mThread->setVsyncRate(2, mConnection); |
| 460 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 461 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 462 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 463 | |
| 464 | // The first event will be seen by the interceptor, and not the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 465 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 466 | expectInterceptCallReceived(123); |
| 467 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 468 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 469 | |
| 470 | // The second event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 471 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 472 | expectInterceptCallReceived(456); |
| 473 | expectVsyncEventReceivedByConnection(456, 2u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 474 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 475 | |
| 476 | // The third event will be seen by the interceptor, and not the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 477 | mCallback->onVSyncEvent(789, 777, 744); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 478 | expectInterceptCallReceived(789); |
| 479 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 480 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 481 | |
| 482 | // The fourth event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 483 | mCallback->onVSyncEvent(101112, 7847, 86); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 484 | expectInterceptCallReceived(101112); |
| 485 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 486 | } |
| 487 | |
| 488 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 489 | mThread->setVsyncRate(1, mConnection); |
| 490 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 491 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 492 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 493 | |
| 494 | // Destroy the only (strong) reference to the connection. |
| 495 | mConnection = nullptr; |
| 496 | |
| 497 | // The first event will be seen by the interceptor, and not the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 498 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 499 | expectInterceptCallReceived(123); |
| 500 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 501 | |
| 502 | // EventThread should disable vsync callbacks |
| 503 | expectVSyncSetEnabledCallReceived(false); |
| 504 | } |
| 505 | |
| 506 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 507 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 508 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 509 | mThread->setVsyncRate(1, errorConnection); |
| 510 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 511 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 512 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 513 | |
| 514 | // The first event will be seen by the interceptor, and by the connection, |
| 515 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 516 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 517 | expectInterceptCallReceived(123); |
| 518 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 519 | |
| 520 | // A subsequent event will be seen by the interceptor and not by the |
| 521 | // connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 522 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 523 | expectInterceptCallReceived(456); |
| 524 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 525 | |
| 526 | // EventThread should disable vsync callbacks with the second event |
| 527 | expectVSyncSetEnabledCallReceived(false); |
| 528 | } |
| 529 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 530 | TEST_F(EventThreadTest, tracksEventConnections) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 531 | EXPECT_EQ(2, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 532 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 533 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 534 | mThread->setVsyncRate(1, errorConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 535 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 536 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 537 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 538 | createConnection(secondConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 539 | mThread->setVsyncRate(1, secondConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 540 | EXPECT_EQ(4, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 541 | |
| 542 | // EventThread should enable vsync callbacks. |
| 543 | expectVSyncSetEnabledCallReceived(true); |
| 544 | |
| 545 | // The first event will be seen by the interceptor, and by the connection, |
| 546 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 547 | mCallback->onVSyncEvent(123, 456, 789); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 548 | expectInterceptCallReceived(123); |
| 549 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 550 | expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123, |
| 551 | 1u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 552 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 555 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 556 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 557 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 558 | mThread->setVsyncRate(1, errorConnection); |
| 559 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 560 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 561 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 562 | |
| 563 | // The first event will be seen by the interceptor, and by the connection, |
| 564 | // which then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 565 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 566 | expectInterceptCallReceived(123); |
| 567 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 568 | |
| 569 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 570 | // which still then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 571 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 572 | expectInterceptCallReceived(456); |
| 573 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 574 | |
| 575 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 576 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 577 | } |
| 578 | |
| 579 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 580 | mThread->setDuration(321ns, 456ns); |
| 581 | expectVSyncSetDurationCallReceived(321ns, 456ns); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 584 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 585 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 586 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 589 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 590 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 591 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 595 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 596 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 600 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 601 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 604 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 605 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 606 | .setPhysicalDisplayId(INTERNAL_DISPLAY_ID) |
| 607 | .setId(DisplayModeId(7)) |
| 608 | .setVsyncPeriod(16666666) |
| 609 | .build(); |
| 610 | |
| 611 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 612 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 616 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 617 | .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID) |
| 618 | .setId(DisplayModeId(5)) |
| 619 | .setVsyncPeriod(16666666) |
| 620 | .build(); |
| 621 | |
| 622 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 623 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 626 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 627 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 628 | .setPhysicalDisplayId(DISPLAY_ID_64BIT) |
| 629 | .setId(DisplayModeId(7)) |
| 630 | .setVsyncPeriod(16666666) |
| 631 | .build(); |
| 632 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 633 | expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666); |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 636 | TEST_F(EventThreadTest, suppressConfigChanged) { |
| 637 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 638 | sp<MockEventThreadConnection> suppressConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 639 | createConnection(suppressConnectionEventRecorder); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 640 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 641 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 642 | .setPhysicalDisplayId(INTERNAL_DISPLAY_ID) |
| 643 | .setId(DisplayModeId(9)) |
| 644 | .setVsyncPeriod(16666666) |
| 645 | .build(); |
| 646 | |
| 647 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 648 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 649 | |
| 650 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 651 | ASSERT_FALSE(args.has_value()); |
| 652 | } |
| 653 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 654 | TEST_F(EventThreadTest, postUidFrameRateMapping) { |
| 655 | const std::vector<FrameRateOverride> overrides = { |
| 656 | {.uid = 1, .frameRateHz = 20}, |
| 657 | {.uid = 3, .frameRateHz = 40}, |
| 658 | {.uid = 5, .frameRateHz = 60}, |
| 659 | }; |
| 660 | |
| 661 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 662 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 663 | } |
| 664 | |
| 665 | TEST_F(EventThreadTest, suppressUidFrameRateMapping) { |
| 666 | const std::vector<FrameRateOverride> overrides = { |
| 667 | {.uid = 1, .frameRateHz = 20}, |
| 668 | {.uid = 3, .frameRateHz = 40}, |
| 669 | {.uid = 5, .frameRateHz = 60}, |
| 670 | }; |
| 671 | |
| 672 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 673 | sp<MockEventThreadConnection> suppressConnection = |
| 674 | createConnection(suppressConnectionEventRecorder); |
| 675 | |
| 676 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 677 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 678 | |
| 679 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 680 | ASSERT_FALSE(args.has_value()); |
| 681 | } |
| 682 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 683 | TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) { |
| 684 | // Signal that we want the next vsync event to be posted to the throttled connection |
| 685 | mThread->requestNextVsync(mThrottledConnection); |
| 686 | |
| 687 | // EventThread should immediately request a resync. |
| 688 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 689 | |
| 690 | // EventThread should enable vsync callbacks. |
| 691 | expectVSyncSetEnabledCallReceived(true); |
| 692 | |
| 693 | // Use the received callback to signal a first vsync event. |
| 694 | // The interceptor should receive the event, but not the connection. |
| 695 | mCallback->onVSyncEvent(123, 456, 789); |
| 696 | expectInterceptCallReceived(123); |
| 697 | expectThrottleVsyncReceived(456, mThrottledConnectionUid); |
| 698 | mThrottledConnectionEventCallRecorder.waitForUnexpectedCall(); |
| 699 | |
| 700 | // Use the received callback to signal a second vsync event. |
| 701 | // The interceptor should receive the event, but the connection should |
| 702 | // not as it was only interested in the first. |
| 703 | mCallback->onVSyncEvent(456, 123, 0); |
| 704 | expectInterceptCallReceived(456); |
| 705 | expectThrottleVsyncReceived(123, mThrottledConnectionUid); |
| 706 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 707 | |
| 708 | // EventThread should not change the vsync state as it didn't send the event |
| 709 | // yet |
| 710 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 711 | } |
| 712 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 713 | } // namespace |
| 714 | } // namespace android |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 715 | |
| 716 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 717 | #pragma clang diagnostic pop // ignored "-Wextra" |