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