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