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++) { |
| 261 | if (i > 0) { |
| 262 | EXPECT_GT(event.vsync.frameTimelines[i].deadlineTimestamp, |
| 263 | event.vsync.frameTimelines[i - 1].deadlineTimestamp) |
| 264 | << "Deadline timestamp out of order for frame timeline " << i; |
| 265 | EXPECT_GT(event.vsync.frameTimelines[i].expectedVSyncTimestamp, |
| 266 | event.vsync.frameTimelines[i - 1].expectedVSyncTimestamp) |
| 267 | << "Expected vsync timestamp out of order for frame timeline " << i; |
| 268 | } |
| 269 | if (event.vsync.frameTimelines[i].deadlineTimestamp == preferredDeadline) { |
| 270 | EXPECT_EQ(i, event.vsync.preferredFrameTimelineIndex) |
| 271 | << "Preferred frame timeline index should be " << i; |
| 272 | // For the platform-preferred frame timeline, the vsync ID is 0 because the first frame |
| 273 | // timeline is made before the rest. |
| 274 | EXPECT_EQ(0, event.vsync.frameTimelines[i].vsyncId) |
| 275 | << "Vsync ID incorrect for frame timeline " << i; |
| 276 | } else { |
| 277 | // Vsync ID 0 is used for the preferred frame timeline. |
| 278 | EXPECT_EQ(i + 1, event.vsync.frameTimelines[i].vsyncId) |
| 279 | << "Vsync ID incorrect for frame timeline " << i; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 284 | void EventThreadTest::expectHotplugEventReceivedByConnection(PhysicalDisplayId expectedDisplayId, |
| 285 | bool expectedConnected) { |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 286 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 287 | ASSERT_TRUE(args.has_value()); |
| 288 | const auto& event = std::get<0>(args.value()); |
| 289 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, event.header.type); |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 290 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 291 | EXPECT_EQ(expectedConnected, event.hotplug.connected); |
| 292 | } |
| 293 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 294 | void EventThreadTest::expectConfigChangedEventReceivedByConnection( |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 295 | PhysicalDisplayId expectedDisplayId, int32_t expectedConfigId, |
| 296 | nsecs_t expectedVsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 297 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 298 | ASSERT_TRUE(args.has_value()); |
| 299 | const auto& event = std::get<0>(args.value()); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 300 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, event.header.type); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 301 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 302 | EXPECT_EQ(expectedConfigId, event.modeChange.modeId); |
| 303 | EXPECT_EQ(expectedVsyncPeriod, event.modeChange.vsyncPeriod); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 306 | void EventThreadTest::expectUidFrameRateMappingEventReceivedByConnection( |
| 307 | PhysicalDisplayId expectedDisplayId, std::vector<FrameRateOverride> expectedOverrides) { |
| 308 | for (const auto [uid, frameRateHz] : expectedOverrides) { |
| 309 | auto args = mConnectionEventCallRecorder.waitForCall(); |
| 310 | ASSERT_TRUE(args.has_value()); |
| 311 | const auto& event = std::get<0>(args.value()); |
| 312 | EXPECT_EQ(DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, event.header.type); |
| 313 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 314 | EXPECT_EQ(uid, event.frameRateOverride.uid); |
| 315 | EXPECT_EQ(frameRateHz, event.frameRateOverride.frameRateHz); |
| 316 | } |
| 317 | |
| 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_FLUSH, event.header.type); |
| 322 | EXPECT_EQ(expectedDisplayId, event.header.displayId); |
| 323 | } |
| 324 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 325 | namespace { |
| 326 | |
| 327 | /* ------------------------------------------------------------------------ |
| 328 | * Test cases |
| 329 | */ |
| 330 | |
| 331 | TEST_F(EventThreadTest, canCreateAndDestroyThreadWithNoEventsSent) { |
| 332 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 333 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 334 | EXPECT_FALSE(mVSyncSetDurationCallRecorder.waitForCall(0us).has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 335 | EXPECT_FALSE(mResyncCallRecorder.waitForCall(0us).has_value()); |
| 336 | EXPECT_FALSE(mInterceptVSyncCallRecorder.waitForCall(0us).has_value()); |
| 337 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForCall(0us).has_value()); |
| 338 | } |
| 339 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 340 | TEST_F(EventThreadTest, vsyncRequestIsIgnoredIfDisplayIsDisconnected) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 341 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 342 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 343 | |
| 344 | // 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] | 345 | mThread->requestNextVsync(mConnection); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 346 | |
| 347 | // EventThread should not enable vsync callbacks. |
| 348 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 351 | TEST_F(EventThreadTest, requestNextVsyncPostsASingleVSyncEventToTheConnection) { |
| 352 | // 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] | 353 | mThread->requestNextVsync(mConnection); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 354 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 355 | // EventThread should immediately request a resync. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 356 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 357 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 358 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 359 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 360 | |
| 361 | // Use the received callback to signal a first vsync event. |
| 362 | // The interceptor should receive the event, as well as the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 363 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 364 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 365 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 366 | expectVsyncEventReceivedByConnection(123, 1u); |
| 367 | |
| 368 | // Use the received callback to signal a second vsync event. |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 369 | // The interceptor should receive the event, but the connection should |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 370 | // not as it was only interested in the first. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 371 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 372 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 373 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 374 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 375 | |
| 376 | // EventThread should also detect that at this point that it does not need |
| 377 | // any more vsync events, and should disable their generation. |
| 378 | expectVSyncSetEnabledCallReceived(false); |
| 379 | } |
| 380 | |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame^] | 381 | TEST_F(EventThreadTest, requestNextVsyncEventFrameTimelinesCorrect) { |
| 382 | // Signal that we want the next vsync event to be posted to the connection |
| 383 | mThread->requestNextVsync(mConnection); |
| 384 | |
| 385 | expectVSyncSetEnabledCallReceived(true); |
| 386 | |
| 387 | // Use the received callback to signal a vsync event. |
| 388 | // The interceptor should receive the event, as well as the connection. |
| 389 | mCallback->onVSyncEvent(123, 456, 789); |
| 390 | expectInterceptCallReceived(123); |
| 391 | expectVsyncEventFrameTimelinesCorrect(123, 789); |
| 392 | } |
| 393 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 394 | TEST_F(EventThreadTest, setVsyncRateZeroPostsNoVSyncEventsToThatConnection) { |
| 395 | // Create a first connection, register it, and request a vsync rate of zero. |
| 396 | ConnectionEventRecorder firstConnectionEventRecorder{0}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 397 | sp<MockEventThreadConnection> firstConnection = createConnection(firstConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 398 | mThread->setVsyncRate(0, firstConnection); |
| 399 | |
| 400 | // By itself, this should not enable vsync events |
| 401 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 402 | EXPECT_FALSE(mVSyncSetCallbackCallRecorder.waitForCall(0us).has_value()); |
| 403 | |
| 404 | // However if there is another connection which wants events at a nonzero rate..... |
| 405 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 406 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 407 | createConnection(secondConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 408 | mThread->setVsyncRate(1, secondConnection); |
| 409 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 410 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 411 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 412 | |
| 413 | // Send a vsync event. EventThread should then make a call to the |
| 414 | // interceptor, and the second connection. The first connection should not |
| 415 | // get the event. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 416 | mCallback->onVSyncEvent(123, 456, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 417 | expectInterceptCallReceived(123); |
| 418 | EXPECT_FALSE(firstConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 419 | expectVsyncEventReceivedByConnection("secondConnection", secondConnectionEventRecorder, 123, |
| 420 | 1u); |
| 421 | } |
| 422 | |
| 423 | TEST_F(EventThreadTest, setVsyncRateOnePostsAllEventsToThatConnection) { |
| 424 | mThread->setVsyncRate(1, mConnection); |
| 425 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 426 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 427 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 428 | |
| 429 | // Send a vsync event. EventThread should then make a call to the |
| 430 | // interceptor, and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 431 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 432 | expectInterceptCallReceived(123); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 433 | expectThrottleVsyncReceived(456, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 434 | expectVsyncEventReceivedByConnection(123, 1u); |
| 435 | |
| 436 | // A second event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 437 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 438 | expectInterceptCallReceived(456); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 439 | expectThrottleVsyncReceived(123, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 440 | expectVsyncEventReceivedByConnection(456, 2u); |
| 441 | |
| 442 | // A third event should go to the same places. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 443 | mCallback->onVSyncEvent(789, 777, 111); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 444 | expectInterceptCallReceived(789); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 445 | expectThrottleVsyncReceived(777, mConnectionUid); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 446 | expectVsyncEventReceivedByConnection(789, 3u); |
| 447 | } |
| 448 | |
| 449 | TEST_F(EventThreadTest, setVsyncRateTwoPostsEveryOtherEventToThatConnection) { |
| 450 | mThread->setVsyncRate(2, mConnection); |
| 451 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 452 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 453 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 454 | |
| 455 | // 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] | 456 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 457 | expectInterceptCallReceived(123); |
| 458 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 459 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 460 | |
| 461 | // The second event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 462 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 463 | expectInterceptCallReceived(456); |
| 464 | expectVsyncEventReceivedByConnection(456, 2u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 465 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 466 | |
| 467 | // 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] | 468 | mCallback->onVSyncEvent(789, 777, 744); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 469 | expectInterceptCallReceived(789); |
| 470 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 471 | EXPECT_FALSE(mThrottleVsyncCallRecorder.waitForUnexpectedCall().has_value()); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 472 | |
| 473 | // The fourth event will be seen by the interceptor and the connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 474 | mCallback->onVSyncEvent(101112, 7847, 86); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 475 | expectInterceptCallReceived(101112); |
| 476 | expectVsyncEventReceivedByConnection(101112, 4u); |
| 477 | } |
| 478 | |
| 479 | TEST_F(EventThreadTest, connectionsRemovedIfInstanceDestroyed) { |
| 480 | mThread->setVsyncRate(1, mConnection); |
| 481 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 482 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 483 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 484 | |
| 485 | // Destroy the only (strong) reference to the connection. |
| 486 | mConnection = nullptr; |
| 487 | |
| 488 | // 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] | 489 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 490 | expectInterceptCallReceived(123); |
| 491 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 492 | |
| 493 | // EventThread should disable vsync callbacks |
| 494 | expectVSyncSetEnabledCallReceived(false); |
| 495 | } |
| 496 | |
| 497 | TEST_F(EventThreadTest, connectionsRemovedIfEventDeliveryError) { |
| 498 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 499 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 500 | mThread->setVsyncRate(1, errorConnection); |
| 501 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 502 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 503 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 504 | |
| 505 | // The first event will be seen by the interceptor, and by the connection, |
| 506 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 507 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 508 | expectInterceptCallReceived(123); |
| 509 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 510 | |
| 511 | // A subsequent event will be seen by the interceptor and not by the |
| 512 | // connection. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 513 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 514 | expectInterceptCallReceived(456); |
| 515 | EXPECT_FALSE(errorConnectionEventRecorder.waitForUnexpectedCall().has_value()); |
| 516 | |
| 517 | // EventThread should disable vsync callbacks with the second event |
| 518 | expectVSyncSetEnabledCallReceived(false); |
| 519 | } |
| 520 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 521 | TEST_F(EventThreadTest, tracksEventConnections) { |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 522 | EXPECT_EQ(2, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 523 | ConnectionEventRecorder errorConnectionEventRecorder{NO_MEMORY}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 524 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 525 | mThread->setVsyncRate(1, errorConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 526 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 527 | ConnectionEventRecorder secondConnectionEventRecorder{0}; |
| 528 | sp<MockEventThreadConnection> secondConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 529 | createConnection(secondConnectionEventRecorder); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 530 | mThread->setVsyncRate(1, secondConnection); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 531 | EXPECT_EQ(4, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 532 | |
| 533 | // EventThread should enable vsync callbacks. |
| 534 | expectVSyncSetEnabledCallReceived(true); |
| 535 | |
| 536 | // The first event will be seen by the interceptor, and by the connection, |
| 537 | // which then returns an error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 538 | mCallback->onVSyncEvent(123, 456, 789); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 539 | expectInterceptCallReceived(123); |
| 540 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 541 | expectVsyncEventReceivedByConnection("successConnection", secondConnectionEventRecorder, 123, |
| 542 | 1u); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 543 | EXPECT_EQ(3, mThread->getEventThreadConnectionCount()); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 546 | TEST_F(EventThreadTest, eventsDroppedIfNonfatalEventDeliveryError) { |
| 547 | ConnectionEventRecorder errorConnectionEventRecorder{WOULD_BLOCK}; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 548 | sp<MockEventThreadConnection> errorConnection = createConnection(errorConnectionEventRecorder); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 549 | mThread->setVsyncRate(1, errorConnection); |
| 550 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 551 | // EventThread should enable vsync callbacks. |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 552 | expectVSyncSetEnabledCallReceived(true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 553 | |
| 554 | // The first event will be seen by the interceptor, and by the connection, |
| 555 | // which then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 556 | mCallback->onVSyncEvent(123, 456, 789); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 557 | expectInterceptCallReceived(123); |
| 558 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 123, 1u); |
| 559 | |
| 560 | // A subsequent event will be seen by the interceptor, and by the connection, |
| 561 | // which still then returns an non-fatal error. |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 562 | mCallback->onVSyncEvent(456, 123, 0); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 563 | expectInterceptCallReceived(456); |
| 564 | expectVsyncEventReceivedByConnection("errorConnection", errorConnectionEventRecorder, 456, 2u); |
| 565 | |
| 566 | // EventThread will not disable vsync callbacks as the errors are non-fatal. |
| 567 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 568 | } |
| 569 | |
| 570 | TEST_F(EventThreadTest, setPhaseOffsetForwardsToVSyncSource) { |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 571 | mThread->setDuration(321ns, 456ns); |
| 572 | expectVSyncSetDurationCallReceived(321ns, 456ns); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 575 | TEST_F(EventThreadTest, postHotplugInternalDisconnect) { |
| 576 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, false); |
| 577 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 580 | TEST_F(EventThreadTest, postHotplugInternalConnect) { |
| 581 | mThread->onHotplugReceived(INTERNAL_DISPLAY_ID, true); |
| 582 | expectHotplugEventReceivedByConnection(INTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | TEST_F(EventThreadTest, postHotplugExternalDisconnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 586 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, false); |
| 587 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, false); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | TEST_F(EventThreadTest, postHotplugExternalConnect) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 591 | mThread->onHotplugReceived(EXTERNAL_DISPLAY_ID, true); |
| 592 | expectHotplugEventReceivedByConnection(EXTERNAL_DISPLAY_ID, true); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 595 | TEST_F(EventThreadTest, postConfigChangedPrimary) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 596 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 597 | .setPhysicalDisplayId(INTERNAL_DISPLAY_ID) |
| 598 | .setId(DisplayModeId(7)) |
| 599 | .setVsyncPeriod(16666666) |
| 600 | .build(); |
| 601 | |
| 602 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 603 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 7, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | TEST_F(EventThreadTest, postConfigChangedExternal) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 607 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 608 | .setPhysicalDisplayId(EXTERNAL_DISPLAY_ID) |
| 609 | .setId(DisplayModeId(5)) |
| 610 | .setVsyncPeriod(16666666) |
| 611 | .build(); |
| 612 | |
| 613 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 614 | expectConfigChangedEventReceivedByConnection(EXTERNAL_DISPLAY_ID, 5, 16666666); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 617 | TEST_F(EventThreadTest, postConfigChangedPrimary64bit) { |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 618 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 619 | .setPhysicalDisplayId(DISPLAY_ID_64BIT) |
| 620 | .setId(DisplayModeId(7)) |
| 621 | .setVsyncPeriod(16666666) |
| 622 | .build(); |
| 623 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 624 | expectConfigChangedEventReceivedByConnection(DISPLAY_ID_64BIT, 7, 16666666); |
Ady Abraham | af0ec27 | 2019-03-28 11:38:31 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 627 | TEST_F(EventThreadTest, suppressConfigChanged) { |
| 628 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 629 | sp<MockEventThreadConnection> suppressConnection = |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 630 | createConnection(suppressConnectionEventRecorder); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 631 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 632 | const auto mode = DisplayMode::Builder(hal::HWConfigId(0)) |
| 633 | .setPhysicalDisplayId(INTERNAL_DISPLAY_ID) |
| 634 | .setId(DisplayModeId(9)) |
| 635 | .setVsyncPeriod(16666666) |
| 636 | .build(); |
| 637 | |
| 638 | mThread->onModeChanged(mode); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 639 | expectConfigChangedEventReceivedByConnection(INTERNAL_DISPLAY_ID, 9, 16666666); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 640 | |
| 641 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 642 | ASSERT_FALSE(args.has_value()); |
| 643 | } |
| 644 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 645 | TEST_F(EventThreadTest, postUidFrameRateMapping) { |
| 646 | const std::vector<FrameRateOverride> overrides = { |
| 647 | {.uid = 1, .frameRateHz = 20}, |
| 648 | {.uid = 3, .frameRateHz = 40}, |
| 649 | {.uid = 5, .frameRateHz = 60}, |
| 650 | }; |
| 651 | |
| 652 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 653 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 654 | } |
| 655 | |
| 656 | TEST_F(EventThreadTest, suppressUidFrameRateMapping) { |
| 657 | const std::vector<FrameRateOverride> overrides = { |
| 658 | {.uid = 1, .frameRateHz = 20}, |
| 659 | {.uid = 3, .frameRateHz = 40}, |
| 660 | {.uid = 5, .frameRateHz = 60}, |
| 661 | }; |
| 662 | |
| 663 | ConnectionEventRecorder suppressConnectionEventRecorder{0}; |
| 664 | sp<MockEventThreadConnection> suppressConnection = |
| 665 | createConnection(suppressConnectionEventRecorder); |
| 666 | |
| 667 | mThread->onFrameRateOverridesChanged(INTERNAL_DISPLAY_ID, overrides); |
| 668 | expectUidFrameRateMappingEventReceivedByConnection(INTERNAL_DISPLAY_ID, overrides); |
| 669 | |
| 670 | auto args = suppressConnectionEventRecorder.waitForCall(); |
| 671 | ASSERT_FALSE(args.has_value()); |
| 672 | } |
| 673 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 674 | TEST_F(EventThreadTest, requestNextVsyncWithThrottleVsyncDoesntPostVSync) { |
| 675 | // Signal that we want the next vsync event to be posted to the throttled connection |
| 676 | mThread->requestNextVsync(mThrottledConnection); |
| 677 | |
| 678 | // EventThread should immediately request a resync. |
| 679 | EXPECT_TRUE(mResyncCallRecorder.waitForCall().has_value()); |
| 680 | |
| 681 | // EventThread should enable vsync callbacks. |
| 682 | expectVSyncSetEnabledCallReceived(true); |
| 683 | |
| 684 | // Use the received callback to signal a first vsync event. |
| 685 | // The interceptor should receive the event, but not the connection. |
| 686 | mCallback->onVSyncEvent(123, 456, 789); |
| 687 | expectInterceptCallReceived(123); |
| 688 | expectThrottleVsyncReceived(456, mThrottledConnectionUid); |
| 689 | mThrottledConnectionEventCallRecorder.waitForUnexpectedCall(); |
| 690 | |
| 691 | // Use the received callback to signal a second vsync event. |
| 692 | // The interceptor should receive the event, but the connection should |
| 693 | // not as it was only interested in the first. |
| 694 | mCallback->onVSyncEvent(456, 123, 0); |
| 695 | expectInterceptCallReceived(456); |
| 696 | expectThrottleVsyncReceived(123, mThrottledConnectionUid); |
| 697 | EXPECT_FALSE(mConnectionEventCallRecorder.waitForUnexpectedCall().has_value()); |
| 698 | |
| 699 | // EventThread should not change the vsync state as it didn't send the event |
| 700 | // yet |
| 701 | EXPECT_FALSE(mVSyncSetEnabledCallRecorder.waitForUnexpectedCall().has_value()); |
| 702 | } |
| 703 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 704 | } // namespace |
| 705 | } // namespace android |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 706 | |
| 707 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 708 | #pragma clang diagnostic pop // ignored "-Wextra" |