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