Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 22 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 23 | #include <pthread.h> |
| 24 | #include <sched.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 26 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 27 | #include <chrono> |
| 28 | #include <cstdint> |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 29 | #include <optional> |
| 30 | #include <type_traits> |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 31 | #include <utility> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 32 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| 34 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 35 | #include <binder/IPCThreadState.h> |
| 36 | |
Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 37 | #include <cutils/compiler.h> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 38 | #include <cutils/sched_policy.h> |
Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 39 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 40 | #include <gui/DisplayEventReceiver.h> |
| 41 | |
| 42 | #include <utils/Errors.h> |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 43 | #include <utils/Trace.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 44 | |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 45 | #include <scheduler/VsyncConfig.h> |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 46 | #include "DisplayHardware/DisplayMode.h" |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 47 | #include "FrameTimeline.h" |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 48 | #include "VSyncDispatch.h" |
| 49 | #include "VSyncTracker.h" |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 50 | |
| 51 | #include "EventThread.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 52 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 53 | #undef LOG_TAG |
| 54 | #define LOG_TAG "EventThread" |
| 55 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 56 | using namespace std::chrono_literals; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 57 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 58 | namespace android { |
| 59 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 60 | using base::StringAppendF; |
| 61 | using base::StringPrintf; |
| 62 | |
| 63 | namespace { |
| 64 | |
| 65 | auto vsyncPeriod(VSyncRequest request) { |
| 66 | return static_cast<std::underlying_type_t<VSyncRequest>>(request); |
| 67 | } |
| 68 | |
| 69 | std::string toString(VSyncRequest request) { |
| 70 | switch (request) { |
| 71 | case VSyncRequest::None: |
| 72 | return "VSyncRequest::None"; |
| 73 | case VSyncRequest::Single: |
| 74 | return "VSyncRequest::Single"; |
Tim Murray | b5daa91 | 2020-09-09 21:29:13 +0000 | [diff] [blame] | 75 | case VSyncRequest::SingleSuppressCallback: |
| 76 | return "VSyncRequest::SingleSuppressCallback"; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 77 | default: |
| 78 | return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | std::string toString(const EventThreadConnection& connection) { |
| 83 | return StringPrintf("Connection{%p, %s}", &connection, |
| 84 | toString(connection.vsyncRequest).c_str()); |
| 85 | } |
| 86 | |
| 87 | std::string toString(const DisplayEventReceiver::Event& event) { |
| 88 | switch (event.header.type) { |
| 89 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 90 | return StringPrintf("Hotplug{displayId=%s, %s}", |
| 91 | to_string(event.header.displayId).c_str(), |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 92 | event.hotplug.connected ? "connected" : "disconnected"); |
| 93 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 94 | return StringPrintf("VSync{displayId=%s, count=%u, expectedPresentationTime=%" PRId64 |
| 95 | "}", |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 96 | to_string(event.header.displayId).c_str(), event.vsync.count, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 97 | event.vsync.vsyncData.preferredExpectedPresentationTime()); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 98 | case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE: |
| 99 | return StringPrintf("ModeChanged{displayId=%s, modeId=%u}", |
| 100 | to_string(event.header.displayId).c_str(), event.modeChange.modeId); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 101 | default: |
| 102 | return "Event{}"; |
| 103 | } |
| 104 | } |
| 105 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 106 | DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp, |
| 107 | bool connected) { |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 108 | DisplayEventReceiver::Event event; |
| 109 | event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp}; |
| 110 | event.hotplug.connected = connected; |
| 111 | return event; |
| 112 | } |
| 113 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 114 | DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 115 | uint32_t count, nsecs_t expectedPresentationTime, |
Rachel Lee | 0d94320 | 2022-02-01 23:29:41 -0800 | [diff] [blame] | 116 | nsecs_t deadlineTimestamp) { |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 117 | DisplayEventReceiver::Event event; |
| 118 | event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp}; |
| 119 | event.vsync.count = count; |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 120 | event.vsync.vsyncData.preferredFrameTimelineIndex = 0; |
| 121 | // Temporarily store the current vsync information in frameTimelines[0], marked as |
| 122 | // platform-preferred. When the event is dispatched later, the frame interval at that time is |
| 123 | // used with this information to generate multiple frame timeline choices. |
| 124 | event.vsync.vsyncData.frameTimelines[0] = {.vsyncId = FrameTimelineInfo::INVALID_VSYNC_ID, |
| 125 | .deadlineTimestamp = deadlineTimestamp, |
| 126 | .expectedPresentationTime = |
| 127 | expectedPresentationTime}; |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 128 | return event; |
| 129 | } |
| 130 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 131 | DisplayEventReceiver::Event makeModeChanged(const scheduler::FrameRateMode& mode) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 132 | DisplayEventReceiver::Event event; |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 133 | event.header = {DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE, |
| 134 | mode.modePtr->getPhysicalDisplayId(), systemTime()}; |
| 135 | event.modeChange.modeId = mode.modePtr->getId().value(); |
| 136 | event.modeChange.vsyncPeriod = mode.fps.getPeriodNsecs(); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 137 | return event; |
| 138 | } |
| 139 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 140 | DisplayEventReceiver::Event makeFrameRateOverrideEvent(PhysicalDisplayId displayId, |
| 141 | FrameRateOverride frameRateOverride) { |
| 142 | return DisplayEventReceiver::Event{ |
| 143 | .header = |
| 144 | DisplayEventReceiver::Event::Header{ |
| 145 | .type = DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE, |
| 146 | .displayId = displayId, |
| 147 | .timestamp = systemTime(), |
| 148 | }, |
| 149 | .frameRateOverride = frameRateOverride, |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | DisplayEventReceiver::Event makeFrameRateOverrideFlushEvent(PhysicalDisplayId displayId) { |
| 154 | return DisplayEventReceiver::Event{ |
| 155 | .header = DisplayEventReceiver::Event::Header{ |
| 156 | .type = DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH, |
| 157 | .displayId = displayId, |
| 158 | .timestamp = systemTime(), |
| 159 | }}; |
| 160 | } |
| 161 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 162 | } // namespace |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 163 | |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 164 | EventThreadConnection::EventThreadConnection(EventThread* eventThread, uid_t callingUid, |
| 165 | ResyncCallback resyncCallback, |
| 166 | EventRegistrationFlags eventRegistration) |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 167 | : resyncCallback(std::move(resyncCallback)), |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 168 | mOwnerUid(callingUid), |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 169 | mEventRegistration(eventRegistration), |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 170 | mEventThread(eventThread), |
| 171 | mChannel(gui::BitTube::DefaultSize) {} |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 172 | |
| 173 | EventThreadConnection::~EventThreadConnection() { |
| 174 | // do nothing here -- clean-up will happen automatically |
| 175 | // when the main thread wakes up |
| 176 | } |
| 177 | |
| 178 | void EventThreadConnection::onFirstRef() { |
| 179 | // NOTE: mEventThread doesn't hold a strong reference on us |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 180 | mEventThread->registerDisplayEventConnection(sp<EventThreadConnection>::fromExisting(this)); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 183 | binder::Status EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) { |
Ady Abraham | 899e8cd | 2022-06-06 15:16:06 -0700 | [diff] [blame] | 184 | std::scoped_lock lock(mLock); |
| 185 | if (mChannel.initCheck() != NO_ERROR) { |
| 186 | return binder::Status::fromStatusT(NAME_NOT_FOUND); |
| 187 | } |
| 188 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 189 | outChannel->setReceiveFd(mChannel.moveReceiveFd()); |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 190 | outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd()))); |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 191 | return binder::Status::ok(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 194 | binder::Status EventThreadConnection::setVsyncRate(int rate) { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 195 | mEventThread->setVsyncRate(static_cast<uint32_t>(rate), |
| 196 | sp<EventThreadConnection>::fromExisting(this)); |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 197 | return binder::Status::ok(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 200 | binder::Status EventThreadConnection::requestNextVsync() { |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 201 | ATRACE_CALL(); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 202 | mEventThread->requestNextVsync(sp<EventThreadConnection>::fromExisting(this)); |
Huihong Luo | 6fac523 | 2021-11-22 16:05:23 -0800 | [diff] [blame] | 203 | return binder::Status::ok(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 206 | binder::Status EventThreadConnection::getLatestVsyncEventData( |
| 207 | ParcelableVsyncEventData* outVsyncEventData) { |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 208 | ATRACE_CALL(); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 209 | outVsyncEventData->vsync = |
| 210 | mEventThread->getLatestVsyncEventData(sp<EventThreadConnection>::fromExisting(this)); |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 211 | return binder::Status::ok(); |
| 212 | } |
| 213 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 214 | status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 215 | constexpr auto toStatus = [](ssize_t size) { |
| 216 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 217 | }; |
| 218 | |
| 219 | if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE || |
| 220 | event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH) { |
| 221 | mPendingEvents.emplace_back(event); |
| 222 | if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE) { |
| 223 | return status_t(NO_ERROR); |
| 224 | } |
| 225 | |
| 226 | auto size = DisplayEventReceiver::sendEvents(&mChannel, mPendingEvents.data(), |
| 227 | mPendingEvents.size()); |
| 228 | mPendingEvents.clear(); |
| 229 | return toStatus(size); |
| 230 | } |
| 231 | |
| 232 | auto size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1); |
| 233 | return toStatus(size); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // --------------------------------------------------------------------------- |
| 237 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 238 | EventThread::~EventThread() = default; |
| 239 | |
| 240 | namespace impl { |
| 241 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 242 | EventThread::EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule> vsyncSchedule, |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 243 | android::frametimeline::TokenManager* tokenManager, |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 244 | ThrottleVsyncCallback throttleVsyncCallback, |
| 245 | GetVsyncPeriodFunction getVsyncPeriodFunction, |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 246 | std::chrono::nanoseconds workDuration, |
| 247 | std::chrono::nanoseconds readyDuration) |
| 248 | : mThreadName(name), |
| 249 | mVsyncTracer(base::StringPrintf("VSYNC-%s", name), 0), |
| 250 | mWorkDuration(base::StringPrintf("VsyncWorkDuration-%s", name), workDuration), |
| 251 | mReadyDuration(readyDuration), |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 252 | mVsyncSchedule(std::move(vsyncSchedule)), |
| 253 | mVsyncRegistration(mVsyncSchedule->getDispatch(), createDispatchCallback(), name), |
Adithya Srinivasan | 5f683cf | 2020-09-15 14:21:04 -0700 | [diff] [blame] | 254 | mTokenManager(tokenManager), |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 255 | mThrottleVsyncCallback(std::move(throttleVsyncCallback)), |
| 256 | mGetVsyncPeriodFunction(std::move(getVsyncPeriodFunction)) { |
| 257 | LOG_ALWAYS_FATAL_IF(getVsyncPeriodFunction == nullptr, |
| 258 | "getVsyncPeriodFunction must not be null"); |
| 259 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 260 | mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS { |
| 261 | std::unique_lock<std::mutex> lock(mMutex); |
| 262 | threadMain(lock); |
| 263 | }); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 264 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 265 | pthread_setname_np(mThread.native_handle(), mThreadName); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 266 | |
| 267 | pid_t tid = pthread_gettid_np(mThread.native_handle()); |
| 268 | |
| 269 | // Use SCHED_FIFO to minimize jitter |
| 270 | constexpr int EVENT_THREAD_PRIORITY = 2; |
| 271 | struct sched_param param = {0}; |
| 272 | param.sched_priority = EVENT_THREAD_PRIORITY; |
| 273 | if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, ¶m) != 0) { |
| 274 | ALOGE("Couldn't set SCHED_FIFO for EventThread"); |
| 275 | } |
| 276 | |
| 277 | set_sched_policy(tid, SP_FOREGROUND); |
| 278 | } |
| 279 | |
| 280 | EventThread::~EventThread() { |
| 281 | { |
| 282 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 283 | mState = State::Quit; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 284 | mCondition.notify_all(); |
| 285 | } |
| 286 | mThread.join(); |
Ruchi Kandoi | ef472ec | 2014-04-02 12:50:06 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 289 | void EventThread::setDuration(std::chrono::nanoseconds workDuration, |
| 290 | std::chrono::nanoseconds readyDuration) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 291 | std::lock_guard<std::mutex> lock(mMutex); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 292 | mWorkDuration = workDuration; |
| 293 | mReadyDuration = readyDuration; |
| 294 | |
| 295 | mVsyncRegistration.update({.workDuration = mWorkDuration.get().count(), |
| 296 | .readyDuration = mReadyDuration.count(), |
| 297 | .earliestVsync = mLastVsyncCallbackTime.ns()}); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 300 | sp<EventThreadConnection> EventThread::createEventConnection( |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 301 | ResyncCallback resyncCallback, EventRegistrationFlags eventRegistration) const { |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 302 | return sp<EventThreadConnection>::make(const_cast<EventThread*>(this), |
| 303 | IPCThreadState::self()->getCallingUid(), |
| 304 | std::move(resyncCallback), eventRegistration); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 307 | status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 308 | std::lock_guard<std::mutex> lock(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 309 | |
| 310 | // this should never happen |
| 311 | auto it = std::find(mDisplayEventConnections.cbegin(), |
| 312 | mDisplayEventConnections.cend(), connection); |
| 313 | if (it != mDisplayEventConnections.cend()) { |
| 314 | ALOGW("DisplayEventConnection %p already exists", connection.get()); |
| 315 | mCondition.notify_all(); |
| 316 | return ALREADY_EXISTS; |
| 317 | } |
| 318 | |
| 319 | mDisplayEventConnections.push_back(connection); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 320 | mCondition.notify_all(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 321 | return NO_ERROR; |
| 322 | } |
| 323 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 324 | void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) { |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 325 | auto it = std::find(mDisplayEventConnections.cbegin(), |
| 326 | mDisplayEventConnections.cend(), connection); |
| 327 | if (it != mDisplayEventConnections.cend()) { |
| 328 | mDisplayEventConnections.erase(it); |
| 329 | } |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 332 | void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) { |
| 333 | if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) { |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | std::lock_guard<std::mutex> lock(mMutex); |
| 338 | |
| 339 | const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate); |
| 340 | if (connection->vsyncRequest != request) { |
| 341 | connection->vsyncRequest = request; |
| 342 | mCondition.notify_all(); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 346 | void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 347 | if (connection->resyncCallback) { |
| 348 | connection->resyncCallback(); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 349 | } |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 350 | |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 351 | std::lock_guard<std::mutex> lock(mMutex); |
| 352 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 353 | if (connection->vsyncRequest == VSyncRequest::None) { |
| 354 | connection->vsyncRequest = VSyncRequest::Single; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 355 | mCondition.notify_all(); |
Tim Murray | b5daa91 | 2020-09-09 21:29:13 +0000 | [diff] [blame] | 356 | } else if (connection->vsyncRequest == VSyncRequest::SingleSuppressCallback) { |
| 357 | connection->vsyncRequest = VSyncRequest::Single; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 358 | } |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 361 | VsyncEventData EventThread::getLatestVsyncEventData( |
| 362 | const sp<EventThreadConnection>& connection) const { |
Rachel Lee | b5223cf | 2022-03-14 14:39:27 -0700 | [diff] [blame] | 363 | // Resync so that the vsync is accurate with hardware. getLatestVsyncEventData is an alternate |
| 364 | // way to get vsync data (instead of posting callbacks to Choreographer). |
| 365 | if (connection->resyncCallback) { |
| 366 | connection->resyncCallback(); |
| 367 | } |
| 368 | |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 369 | VsyncEventData vsyncEventData; |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 370 | nsecs_t frameInterval = mGetVsyncPeriodFunction(connection->mOwnerUid); |
| 371 | vsyncEventData.frameInterval = frameInterval; |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 372 | const auto [presentTime, deadline] = [&]() -> std::pair<nsecs_t, nsecs_t> { |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 373 | std::lock_guard<std::mutex> lock(mMutex); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 374 | const auto vsyncTime = mVsyncSchedule->getTracker().nextAnticipatedVSyncTimeFrom( |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 375 | systemTime() + mWorkDuration.get().count() + mReadyDuration.count()); |
| 376 | return {vsyncTime, vsyncTime - mReadyDuration.count()}; |
| 377 | }(); |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 378 | generateFrameTimeline(vsyncEventData, frameInterval, systemTime(SYSTEM_TIME_MONOTONIC), |
| 379 | presentTime, deadline); |
Rachel Lee | ef2e21f | 2022-02-01 14:51:34 -0800 | [diff] [blame] | 380 | return vsyncEventData; |
| 381 | } |
| 382 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 383 | void EventThread::enableSyntheticVsync(bool enable) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 384 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 385 | if (!mVSyncState || mVSyncState->synthetic == enable) { |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 386 | return; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 387 | } |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 388 | |
Dominik Laskowski | e99b98c | 2023-02-02 12:37:23 -0500 | [diff] [blame] | 389 | mVSyncState->synthetic = enable; |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 390 | mCondition.notify_all(); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 393 | void EventThread::onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 394 | std::lock_guard<std::mutex> lock(mMutex); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 395 | mLastVsyncCallbackTime = TimePoint::fromNs(vsyncTime); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 396 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 397 | LOG_FATAL_IF(!mVSyncState); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 398 | mVsyncTracer = (mVsyncTracer + 1) % 2; |
| 399 | mPendingEvents.push_back(makeVSync(mVSyncState->displayId, wakeupTime, ++mVSyncState->count, |
| 400 | vsyncTime, readyTime)); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 401 | mCondition.notify_all(); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 404 | void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 405 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 406 | |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 407 | mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected)); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 408 | mCondition.notify_all(); |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Ady Abraham | 67434eb | 2022-12-01 17:48:12 -0800 | [diff] [blame] | 411 | void EventThread::onModeChanged(const scheduler::FrameRateMode& mode) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 412 | std::lock_guard<std::mutex> lock(mMutex); |
| 413 | |
Ady Abraham | 690f461 | 2021-07-01 23:24:03 -0700 | [diff] [blame] | 414 | mPendingEvents.push_back(makeModeChanged(mode)); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 415 | mCondition.notify_all(); |
| 416 | } |
| 417 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 418 | void EventThread::onFrameRateOverridesChanged(PhysicalDisplayId displayId, |
| 419 | std::vector<FrameRateOverride> overrides) { |
| 420 | std::lock_guard<std::mutex> lock(mMutex); |
| 421 | |
| 422 | for (auto frameRateOverride : overrides) { |
| 423 | mPendingEvents.push_back(makeFrameRateOverrideEvent(displayId, frameRateOverride)); |
| 424 | } |
| 425 | mPendingEvents.push_back(makeFrameRateOverrideFlushEvent(displayId)); |
| 426 | |
| 427 | mCondition.notify_all(); |
| 428 | } |
| 429 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 430 | void EventThread::threadMain(std::unique_lock<std::mutex>& lock) { |
| 431 | DisplayEventConsumers consumers; |
| 432 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 433 | while (mState != State::Quit) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 434 | std::optional<DisplayEventReceiver::Event> event; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 435 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 436 | // Determine next event to dispatch. |
| 437 | if (!mPendingEvents.empty()) { |
| 438 | event = mPendingEvents.front(); |
| 439 | mPendingEvents.pop_front(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 440 | |
Huihong Luo | ab8ffef | 2022-08-18 13:02:26 -0700 | [diff] [blame] | 441 | if (event->header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) { |
| 442 | if (event->hotplug.connected && !mVSyncState) { |
| 443 | mVSyncState.emplace(event->header.displayId); |
| 444 | } else if (!event->hotplug.connected && mVSyncState && |
| 445 | mVSyncState->displayId == event->header.displayId) { |
| 446 | mVSyncState.reset(); |
| 447 | } |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 448 | } |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 451 | bool vsyncRequested = false; |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 452 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 453 | // Find connections that should consume this event. |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 454 | auto it = mDisplayEventConnections.begin(); |
| 455 | while (it != mDisplayEventConnections.end()) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 456 | if (const auto connection = it->promote()) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 457 | if (event && shouldConsumeEvent(*event, connection)) { |
| 458 | consumers.push_back(connection); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 459 | } |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 460 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 461 | vsyncRequested |= connection->vsyncRequest != VSyncRequest::None; |
| 462 | |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 463 | ++it; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 464 | } else { |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 465 | it = mDisplayEventConnections.erase(it); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 469 | if (!consumers.empty()) { |
| 470 | dispatchEvent(*event, consumers); |
| 471 | consumers.clear(); |
| 472 | } |
| 473 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 474 | if (mVSyncState && vsyncRequested) { |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 475 | mState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 476 | } else { |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 477 | ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected"); |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 478 | mState = State::Idle; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 481 | if (mState == State::VSync) { |
| 482 | const auto scheduleResult = |
| 483 | mVsyncRegistration.schedule({.workDuration = mWorkDuration.get().count(), |
| 484 | .readyDuration = mReadyDuration.count(), |
| 485 | .earliestVsync = mLastVsyncCallbackTime.ns()}); |
| 486 | LOG_ALWAYS_FATAL_IF(!scheduleResult, "Error scheduling callback"); |
| 487 | } else { |
| 488 | mVsyncRegistration.cancel(); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 491 | if (!mPendingEvents.empty()) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 492 | continue; |
| 493 | } |
| 494 | |
| 495 | // Wait for event or client registration/request. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 496 | if (mState == State::Idle) { |
| 497 | mCondition.wait(lock); |
| 498 | } else { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 499 | // Generate a fake VSYNC after a long timeout in case the driver stalls. When the |
| 500 | // display is off, keep feeding clients at 60 Hz. |
Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 501 | const std::chrono::nanoseconds timeout = |
| 502 | mState == State::SyntheticVSync ? 16ms : 1000ms; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 503 | if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) { |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 504 | if (mState == State::VSync) { |
| 505 | ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 506 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 507 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 508 | LOG_FATAL_IF(!mVSyncState); |
Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 509 | const auto now = systemTime(SYSTEM_TIME_MONOTONIC); |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 510 | const auto deadlineTimestamp = now + timeout.count(); |
| 511 | const auto expectedVSyncTime = deadlineTimestamp + timeout.count(); |
Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 512 | mPendingEvents.push_back(makeVSync(mVSyncState->displayId, now, |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 513 | ++mVSyncState->count, expectedVSyncTime, |
Rachel Lee | 0d94320 | 2022-02-01 23:29:41 -0800 | [diff] [blame] | 514 | deadlineTimestamp)); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 517 | } |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 518 | // cancel any pending vsync event before exiting |
| 519 | mVsyncRegistration.cancel(); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 520 | } |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 521 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 522 | bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event, |
| 523 | const sp<EventThreadConnection>& connection) const { |
Leon Scroggins III | ed66a35 | 2023-03-23 17:55:43 -0400 | [diff] [blame] | 524 | const auto throttleVsync = [&]() REQUIRES(mMutex) { |
Leon Scroggins III | 31d4141 | 2022-11-18 16:42:53 -0500 | [diff] [blame] | 525 | const auto& vsyncData = event.vsync.vsyncData; |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 526 | if (connection->frameRate.isValid()) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 527 | return !mVsyncSchedule->getTracker() |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 528 | .isVSyncInPhase(vsyncData.preferredExpectedPresentationTime(), |
| 529 | connection->frameRate); |
| 530 | } |
| 531 | |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 532 | return mThrottleVsyncCallback && |
| 533 | mThrottleVsyncCallback(event.vsync.vsyncData.preferredExpectedPresentationTime(), |
| 534 | connection->mOwnerUid); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 535 | }; |
| 536 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 537 | switch (event.header.type) { |
| 538 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
| 539 | return true; |
| 540 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 541 | case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE: { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 542 | return connection->mEventRegistration.test( |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 543 | gui::ISurfaceComposer::EventRegistration::modeChanged); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 544 | } |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 545 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 546 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 547 | switch (connection->vsyncRequest) { |
| 548 | case VSyncRequest::None: |
| 549 | return false; |
Tim Murray | b5daa91 | 2020-09-09 21:29:13 +0000 | [diff] [blame] | 550 | case VSyncRequest::SingleSuppressCallback: |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 551 | connection->vsyncRequest = VSyncRequest::None; |
Tim Murray | b5daa91 | 2020-09-09 21:29:13 +0000 | [diff] [blame] | 552 | return false; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 553 | case VSyncRequest::Single: { |
| 554 | if (throttleVsync()) { |
| 555 | return false; |
| 556 | } |
Tim Murray | b5daa91 | 2020-09-09 21:29:13 +0000 | [diff] [blame] | 557 | connection->vsyncRequest = VSyncRequest::SingleSuppressCallback; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 558 | return true; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 559 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 560 | case VSyncRequest::Periodic: |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 561 | if (throttleVsync()) { |
| 562 | return false; |
| 563 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 564 | return true; |
| 565 | default: |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 566 | // We don't throttle vsync if the app set a vsync request rate |
| 567 | // since there is no easy way to do that and this is a very |
| 568 | // rare case |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 569 | return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0; |
| 570 | } |
| 571 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 572 | case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE: |
| 573 | [[fallthrough]]; |
| 574 | case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH: |
| 575 | return connection->mEventRegistration.test( |
Huihong Luo | 1b0c49f | 2022-03-15 19:18:21 -0700 | [diff] [blame] | 576 | gui::ISurfaceComposer::EventRegistration::frameRateOverride); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 577 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 578 | default: |
| 579 | return false; |
| 580 | } |
| 581 | } |
| 582 | |
Rachel Lee | 8d0c610 | 2021-11-03 22:00:25 +0000 | [diff] [blame] | 583 | int64_t EventThread::generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 584 | nsecs_t expectedPresentationTime) const { |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 585 | if (mTokenManager != nullptr) { |
| 586 | return mTokenManager->generateTokenForPredictions( |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 587 | {timestamp, deadlineTimestamp, expectedPresentationTime}); |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 588 | } |
| 589 | return FrameTimelineInfo::INVALID_VSYNC_ID; |
| 590 | } |
| 591 | |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 592 | void EventThread::generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval, |
| 593 | nsecs_t timestamp, |
| 594 | nsecs_t preferredExpectedPresentationTime, |
| 595 | nsecs_t preferredDeadlineTimestamp) const { |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 596 | uint32_t currentIndex = 0; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 597 | // Add 1 to ensure the preferredFrameTimelineIndex entry (when multiplier == 0) is included. |
Rachel Lee | 40aef42 | 2023-04-25 14:35:47 -0700 | [diff] [blame] | 598 | for (int64_t multiplier = -VsyncEventData::kFrameTimelinesCapacity + 1; |
| 599 | currentIndex < VsyncEventData::kFrameTimelinesCapacity; multiplier++) { |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 600 | nsecs_t deadlineTimestamp = preferredDeadlineTimestamp + multiplier * frameInterval; |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 601 | // Valid possible frame timelines must have future values, so find a later frame timeline. |
| 602 | if (deadlineTimestamp <= timestamp) { |
| 603 | continue; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 604 | } |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 605 | |
| 606 | nsecs_t expectedPresentationTime = |
| 607 | preferredExpectedPresentationTime + multiplier * frameInterval; |
| 608 | if (expectedPresentationTime >= preferredExpectedPresentationTime + |
| 609 | scheduler::VsyncConfig::kEarlyLatchMaxThreshold.count()) { |
Rachel Lee | afb6b50 | 2023-05-12 15:53:05 -0700 | [diff] [blame] | 610 | if (currentIndex == 0) { |
| 611 | ALOGW("%s: Expected present time is too far in the future but no timelines are " |
| 612 | "valid. preferred EPT=%" PRId64 ", Calculated EPT=%" PRId64 |
| 613 | ", multiplier=%" PRId64 ", frameInterval=%" PRId64 ", threshold=%" PRId64, |
| 614 | __func__, preferredExpectedPresentationTime, expectedPresentationTime, |
| 615 | multiplier, frameInterval, |
| 616 | static_cast<int64_t>( |
| 617 | scheduler::VsyncConfig::kEarlyLatchMaxThreshold.count())); |
| 618 | } |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 619 | break; |
| 620 | } |
| 621 | |
| 622 | if (multiplier == 0) { |
| 623 | outVsyncEventData.preferredFrameTimelineIndex = currentIndex; |
| 624 | } |
| 625 | |
| 626 | outVsyncEventData.frameTimelines[currentIndex] = |
| 627 | {.vsyncId = generateToken(timestamp, deadlineTimestamp, expectedPresentationTime), |
| 628 | .deadlineTimestamp = deadlineTimestamp, |
| 629 | .expectedPresentationTime = expectedPresentationTime}; |
| 630 | currentIndex++; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 631 | } |
Rachel Lee | afb6b50 | 2023-05-12 15:53:05 -0700 | [diff] [blame] | 632 | |
| 633 | if (currentIndex == 0) { |
| 634 | ALOGW("%s: No timelines are valid. preferred EPT=%" PRId64 ", frameInterval=%" PRId64 |
| 635 | ", threshold=%" PRId64, |
| 636 | __func__, preferredExpectedPresentationTime, frameInterval, |
| 637 | static_cast<int64_t>(scheduler::VsyncConfig::kEarlyLatchMaxThreshold.count())); |
| 638 | outVsyncEventData.frameTimelines[currentIndex] = |
| 639 | {.vsyncId = generateToken(timestamp, preferredDeadlineTimestamp, |
| 640 | preferredExpectedPresentationTime), |
| 641 | .deadlineTimestamp = preferredDeadlineTimestamp, |
| 642 | .expectedPresentationTime = preferredExpectedPresentationTime}; |
| 643 | currentIndex++; |
| 644 | } |
| 645 | |
Rachel Lee | 0655a91 | 2023-04-20 19:54:18 -0700 | [diff] [blame] | 646 | outVsyncEventData.frameTimelinesLength = currentIndex; |
Rachel Lee | 3f02866 | 2021-11-04 19:32:24 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 649 | void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event, |
| 650 | const DisplayEventConsumers& consumers) { |
| 651 | for (const auto& consumer : consumers) { |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 652 | DisplayEventReceiver::Event copy = event; |
| 653 | if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) { |
Leon Scroggins III | db16a2b | 2023-02-06 17:50:05 -0500 | [diff] [blame] | 654 | const int64_t frameInterval = mGetVsyncPeriodFunction(consumer->mOwnerUid); |
| 655 | copy.vsync.vsyncData.frameInterval = frameInterval; |
| 656 | generateFrameTimeline(copy.vsync.vsyncData, frameInterval, copy.header.timestamp, |
Rachel Lee | b9c5a77 | 2022-02-04 21:17:37 -0800 | [diff] [blame] | 657 | event.vsync.vsyncData.preferredExpectedPresentationTime(), |
| 658 | event.vsync.vsyncData.preferredDeadlineTimestamp()); |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 659 | } |
| 660 | switch (consumer->postEvent(copy)) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 661 | case NO_ERROR: |
| 662 | break; |
| 663 | |
| 664 | case -EAGAIN: |
| 665 | // TODO: Try again if pipe is full. |
| 666 | ALOGW("Failed dispatching %s for %s", toString(event).c_str(), |
| 667 | toString(*consumer).c_str()); |
| 668 | break; |
| 669 | |
| 670 | default: |
| 671 | // Treat EPIPE and other errors as fatal. |
| 672 | removeDisplayEventConnectionLocked(consumer); |
| 673 | } |
| 674 | } |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 677 | void EventThread::dump(std::string& result) const { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 678 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 679 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 680 | StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState)); |
| 681 | if (mVSyncState) { |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 682 | StringAppendF(&result, "{displayId=%s, count=%u%s}\n", |
| 683 | to_string(mVSyncState->displayId).c_str(), mVSyncState->count, |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 684 | mVSyncState->synthetic ? ", synthetic" : ""); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 685 | } else { |
| 686 | StringAppendF(&result, "none\n"); |
| 687 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 688 | |
Ady Abraham | 011f8ba | 2022-11-22 15:09:07 -0800 | [diff] [blame] | 689 | const auto relativeLastCallTime = |
| 690 | ticks<std::milli, float>(mLastVsyncCallbackTime - TimePoint::now()); |
| 691 | StringAppendF(&result, "mWorkDuration=%.2f mReadyDuration=%.2f last vsync time ", |
| 692 | mWorkDuration.get().count() / 1e6f, mReadyDuration.count() / 1e6f); |
| 693 | StringAppendF(&result, "%.2fms relative to now\n", relativeLastCallTime); |
| 694 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 695 | StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size()); |
| 696 | for (const auto& event : mPendingEvents) { |
| 697 | StringAppendF(&result, " %s\n", toString(event).c_str()); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 698 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 699 | |
| 700 | StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size()); |
| 701 | for (const auto& ptr : mDisplayEventConnections) { |
| 702 | if (const auto connection = ptr.promote()) { |
| 703 | StringAppendF(&result, " %s\n", toString(*connection).c_str()); |
| 704 | } |
| 705 | } |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 706 | result += '\n'; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 709 | const char* EventThread::toCString(State state) { |
| 710 | switch (state) { |
| 711 | case State::Idle: |
| 712 | return "Idle"; |
| 713 | case State::Quit: |
| 714 | return "Quit"; |
| 715 | case State::SyntheticVSync: |
| 716 | return "SyntheticVSync"; |
| 717 | case State::VSync: |
| 718 | return "VSync"; |
| 719 | } |
| 720 | } |
| 721 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 722 | void EventThread::onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule> schedule) { |
Leon Scroggins III | b225360 | 2023-04-19 17:04:04 -0400 | [diff] [blame] | 723 | // Hold onto the old registration until after releasing the mutex to avoid deadlock. |
| 724 | scheduler::VSyncCallbackRegistration oldRegistration = |
| 725 | onNewVsyncScheduleInternal(std::move(schedule)); |
| 726 | } |
| 727 | |
| 728 | scheduler::VSyncCallbackRegistration EventThread::onNewVsyncScheduleInternal( |
| 729 | std::shared_ptr<scheduler::VsyncSchedule> schedule) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 730 | std::lock_guard<std::mutex> lock(mMutex); |
| 731 | const bool reschedule = mVsyncRegistration.cancel() == scheduler::CancelResult::Cancelled; |
| 732 | mVsyncSchedule = std::move(schedule); |
Leon Scroggins III | b225360 | 2023-04-19 17:04:04 -0400 | [diff] [blame] | 733 | auto oldRegistration = |
| 734 | std::exchange(mVsyncRegistration, |
| 735 | scheduler::VSyncCallbackRegistration(mVsyncSchedule->getDispatch(), |
| 736 | createDispatchCallback(), |
| 737 | mThreadName)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 738 | if (reschedule) { |
| 739 | mVsyncRegistration.schedule({.workDuration = mWorkDuration.get().count(), |
| 740 | .readyDuration = mReadyDuration.count(), |
| 741 | .earliestVsync = mLastVsyncCallbackTime.ns()}); |
| 742 | } |
Leon Scroggins III | b225360 | 2023-04-19 17:04:04 -0400 | [diff] [blame] | 743 | return oldRegistration; |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | scheduler::VSyncDispatch::Callback EventThread::createDispatchCallback() { |
| 747 | return [this](nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) { |
| 748 | onVsync(vsyncTime, wakeupTime, readyTime); |
| 749 | }; |
| 750 | } |
| 751 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 752 | } // namespace impl |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 753 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 754 | |
| 755 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 756 | #pragma clang diagnostic pop // ignored "-Wconversion" |