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