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