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