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