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 | |
Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 34 | #include <cutils/compiler.h> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 35 | #include <cutils/sched_policy.h> |
Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 36 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 37 | #include <gui/DisplayEventReceiver.h> |
| 38 | |
| 39 | #include <utils/Errors.h> |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 41 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 42 | #include "EventThread.h" |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 43 | #include "HwcStrongTypes.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 44 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 45 | using namespace std::chrono_literals; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 46 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 47 | namespace android { |
| 48 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 49 | using base::StringAppendF; |
| 50 | using base::StringPrintf; |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | auto vsyncPeriod(VSyncRequest request) { |
| 55 | return static_cast<std::underlying_type_t<VSyncRequest>>(request); |
| 56 | } |
| 57 | |
| 58 | std::string toString(VSyncRequest request) { |
| 59 | switch (request) { |
| 60 | case VSyncRequest::None: |
| 61 | return "VSyncRequest::None"; |
| 62 | case VSyncRequest::Single: |
| 63 | return "VSyncRequest::Single"; |
| 64 | default: |
| 65 | return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | std::string toString(const EventThreadConnection& connection) { |
| 70 | return StringPrintf("Connection{%p, %s}", &connection, |
| 71 | toString(connection.vsyncRequest).c_str()); |
| 72 | } |
| 73 | |
| 74 | std::string toString(const DisplayEventReceiver::Event& event) { |
| 75 | switch (event.header.type) { |
| 76 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 77 | return StringPrintf("Hotplug{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", %s}", |
| 78 | event.header.displayId, |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 79 | event.hotplug.connected ? "connected" : "disconnected"); |
| 80 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 81 | return StringPrintf("VSync{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT |
| 82 | ", count=%u}", |
| 83 | event.header.displayId, event.vsync.count); |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 84 | case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: |
| 85 | return StringPrintf("ConfigChanged{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT |
| 86 | ", configId=%u}", |
| 87 | event.header.displayId, event.config.configId); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 88 | default: |
| 89 | return "Event{}"; |
| 90 | } |
| 91 | } |
| 92 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 93 | DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp, |
| 94 | bool connected) { |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 95 | DisplayEventReceiver::Event event; |
| 96 | event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp}; |
| 97 | event.hotplug.connected = connected; |
| 98 | return event; |
| 99 | } |
| 100 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 101 | DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp, |
| 102 | uint32_t count) { |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 103 | DisplayEventReceiver::Event event; |
| 104 | event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp}; |
| 105 | event.vsync.count = count; |
| 106 | return event; |
| 107 | } |
| 108 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 109 | DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId, |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 110 | HwcConfigIndexType configId, nsecs_t vsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 111 | DisplayEventReceiver::Event event; |
| 112 | event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()}; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 113 | event.config.configId = configId.value(); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 114 | event.config.vsyncPeriod = vsyncPeriod; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 115 | return event; |
| 116 | } |
| 117 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 118 | } // namespace |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 119 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 120 | EventThreadConnection::EventThreadConnection(EventThread* eventThread, |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 121 | ResyncCallback resyncCallback, |
| 122 | ISurfaceComposer::ConfigChanged configChanged) |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 123 | : resyncCallback(std::move(resyncCallback)), |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 124 | mConfigChanged(configChanged), |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 125 | mEventThread(eventThread), |
| 126 | mChannel(gui::BitTube::DefaultSize) {} |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 127 | |
| 128 | EventThreadConnection::~EventThreadConnection() { |
| 129 | // do nothing here -- clean-up will happen automatically |
| 130 | // when the main thread wakes up |
| 131 | } |
| 132 | |
| 133 | void EventThreadConnection::onFirstRef() { |
| 134 | // NOTE: mEventThread doesn't hold a strong reference on us |
| 135 | mEventThread->registerDisplayEventConnection(this); |
| 136 | } |
| 137 | |
| 138 | status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) { |
| 139 | outChannel->setReceiveFd(mChannel.moveReceiveFd()); |
| 140 | return NO_ERROR; |
| 141 | } |
| 142 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 143 | status_t EventThreadConnection::setVsyncRate(uint32_t rate) { |
| 144 | mEventThread->setVsyncRate(rate, this); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 145 | return NO_ERROR; |
| 146 | } |
| 147 | |
| 148 | void EventThreadConnection::requestNextVsync() { |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 149 | ATRACE_NAME("requestNextVsync"); |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 150 | mEventThread->requestNextVsync(this); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 153 | void EventThreadConnection::toggleConfigEvents(ISurfaceComposer::ConfigChanged configChangeFlag) { |
| 154 | ATRACE_NAME("enableConfigEvents"); |
| 155 | mConfigChanged = configChangeFlag; |
| 156 | |
| 157 | // In principle it's possible for rapidly toggling config events to drop an |
| 158 | // event here, but it's unlikely in practice. |
| 159 | if (configChangeFlag == ISurfaceComposer::eConfigChangedDispatch) { |
| 160 | mForcedConfigChangeDispatch = true; |
| 161 | mEventThread->requestLatestConfig(); |
| 162 | } |
| 163 | } |
| 164 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 165 | status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) { |
| 166 | ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1); |
| 167 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 168 | } |
| 169 | |
| 170 | // --------------------------------------------------------------------------- |
| 171 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 172 | EventThread::~EventThread() = default; |
| 173 | |
| 174 | namespace impl { |
| 175 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 176 | EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource, |
| 177 | InterceptVSyncsCallback interceptVSyncsCallback) |
| 178 | : mVSyncSource(std::move(vsyncSource)), |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 179 | mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)), |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 180 | mThreadName(mVSyncSource->getName()) { |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 181 | mVSyncSource->setCallback(this); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 182 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 183 | mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS { |
| 184 | std::unique_lock<std::mutex> lock(mMutex); |
| 185 | threadMain(lock); |
| 186 | }); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 187 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 188 | pthread_setname_np(mThread.native_handle(), mThreadName); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 189 | |
| 190 | pid_t tid = pthread_gettid_np(mThread.native_handle()); |
| 191 | |
| 192 | // Use SCHED_FIFO to minimize jitter |
| 193 | constexpr int EVENT_THREAD_PRIORITY = 2; |
| 194 | struct sched_param param = {0}; |
| 195 | param.sched_priority = EVENT_THREAD_PRIORITY; |
| 196 | if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, ¶m) != 0) { |
| 197 | ALOGE("Couldn't set SCHED_FIFO for EventThread"); |
| 198 | } |
| 199 | |
| 200 | set_sched_policy(tid, SP_FOREGROUND); |
| 201 | } |
| 202 | |
| 203 | EventThread::~EventThread() { |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 204 | mVSyncSource->setCallback(nullptr); |
| 205 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 206 | { |
| 207 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 208 | mState = State::Quit; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 209 | mCondition.notify_all(); |
| 210 | } |
| 211 | mThread.join(); |
Ruchi Kandoi | ef472ec | 2014-04-02 12:50:06 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 214 | void EventThread::setPhaseOffset(nsecs_t phaseOffset) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 215 | std::lock_guard<std::mutex> lock(mMutex); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 216 | mVSyncSource->setPhaseOffset(phaseOffset); |
| 217 | } |
| 218 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 219 | sp<EventThreadConnection> EventThread::createEventConnection( |
| 220 | ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const { |
| 221 | return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback), |
| 222 | configChanged); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 225 | status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 226 | std::lock_guard<std::mutex> lock(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 227 | |
| 228 | // this should never happen |
| 229 | auto it = std::find(mDisplayEventConnections.cbegin(), |
| 230 | mDisplayEventConnections.cend(), connection); |
| 231 | if (it != mDisplayEventConnections.cend()) { |
| 232 | ALOGW("DisplayEventConnection %p already exists", connection.get()); |
| 233 | mCondition.notify_all(); |
| 234 | return ALREADY_EXISTS; |
| 235 | } |
| 236 | |
| 237 | mDisplayEventConnections.push_back(connection); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 238 | mCondition.notify_all(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 239 | return NO_ERROR; |
| 240 | } |
| 241 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 242 | void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) { |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 243 | auto it = std::find(mDisplayEventConnections.cbegin(), |
| 244 | mDisplayEventConnections.cend(), connection); |
| 245 | if (it != mDisplayEventConnections.cend()) { |
| 246 | mDisplayEventConnections.erase(it); |
| 247 | } |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 250 | void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) { |
| 251 | if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | std::lock_guard<std::mutex> lock(mMutex); |
| 256 | |
| 257 | const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate); |
| 258 | if (connection->vsyncRequest != request) { |
| 259 | connection->vsyncRequest = request; |
| 260 | mCondition.notify_all(); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 264 | void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) { |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 265 | if (connection->resyncCallback) { |
| 266 | connection->resyncCallback(); |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 267 | } |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 268 | |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 269 | std::lock_guard<std::mutex> lock(mMutex); |
| 270 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 271 | if (connection->vsyncRequest == VSyncRequest::None) { |
| 272 | connection->vsyncRequest = VSyncRequest::Single; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 273 | mCondition.notify_all(); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 274 | } |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 277 | void EventThread::requestLatestConfig() { |
| 278 | std::lock_guard<std::mutex> lock(mMutex); |
| 279 | auto pendingConfigChange = |
| 280 | std::find_if(std::begin(mPendingEvents), std::end(mPendingEvents), |
| 281 | [&](const DisplayEventReceiver::Event& event) { |
| 282 | return event.header.type == |
| 283 | DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED; |
| 284 | }); |
| 285 | |
| 286 | // If we didn't find a pending config change event, then push out the |
| 287 | // latest one we've ever seen. |
| 288 | if (pendingConfigChange == std::end(mPendingEvents)) { |
| 289 | mPendingEvents.push_back(mLastConfigChangeEvent); |
| 290 | } |
| 291 | |
| 292 | mCondition.notify_all(); |
| 293 | } |
| 294 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 295 | void EventThread::onScreenReleased() { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 296 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 297 | if (!mVSyncState || mVSyncState->synthetic) { |
| 298 | return; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 299 | } |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 300 | |
| 301 | mVSyncState->synthetic = true; |
| 302 | mCondition.notify_all(); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void EventThread::onScreenAcquired() { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 306 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 307 | if (!mVSyncState || !mVSyncState->synthetic) { |
| 308 | return; |
Mathias Agopian | 7d88647 | 2012-06-14 23:39:35 -0700 | [diff] [blame] | 309 | } |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 310 | |
| 311 | mVSyncState->synthetic = false; |
| 312 | mCondition.notify_all(); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 315 | void EventThread::onVSyncEvent(nsecs_t timestamp) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 316 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 317 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 318 | LOG_FATAL_IF(!mVSyncState); |
| 319 | mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count)); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 320 | mCondition.notify_all(); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 323 | void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 324 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 325 | |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 326 | mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected)); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 327 | mCondition.notify_all(); |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 330 | void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId, |
| 331 | nsecs_t vsyncPeriod) { |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 332 | std::lock_guard<std::mutex> lock(mMutex); |
| 333 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 334 | mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod)); |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 335 | mCondition.notify_all(); |
| 336 | } |
| 337 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 338 | size_t EventThread::getEventThreadConnectionCount() { |
| 339 | std::lock_guard<std::mutex> lock(mMutex); |
| 340 | return mDisplayEventConnections.size(); |
| 341 | } |
| 342 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 343 | void EventThread::threadMain(std::unique_lock<std::mutex>& lock) { |
| 344 | DisplayEventConsumers consumers; |
| 345 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 346 | while (mState != State::Quit) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 347 | std::optional<DisplayEventReceiver::Event> event; |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 348 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 349 | // Determine next event to dispatch. |
| 350 | if (!mPendingEvents.empty()) { |
| 351 | event = mPendingEvents.front(); |
| 352 | mPendingEvents.pop_front(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 353 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 354 | switch (event->header.type) { |
| 355 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
| 356 | if (event->hotplug.connected && !mVSyncState) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 357 | mVSyncState.emplace(event->header.displayId); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 358 | } else if (!event->hotplug.connected && mVSyncState && |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 359 | mVSyncState->displayId == event->header.displayId) { |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 360 | mVSyncState.reset(); |
| 361 | } |
| 362 | break; |
| 363 | |
| 364 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 365 | if (mInterceptVSyncsCallback) { |
| 366 | mInterceptVSyncsCallback(event->header.timestamp); |
| 367 | } |
| 368 | break; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 369 | case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: |
| 370 | mLastConfigChangeEvent = *event; |
| 371 | break; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 372 | } |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 375 | bool vsyncRequested = false; |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 376 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 377 | // Find connections that should consume this event. |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 378 | auto it = mDisplayEventConnections.begin(); |
| 379 | while (it != mDisplayEventConnections.end()) { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 380 | if (const auto connection = it->promote()) { |
| 381 | vsyncRequested |= connection->vsyncRequest != VSyncRequest::None; |
| 382 | |
| 383 | if (event && shouldConsumeEvent(*event, connection)) { |
| 384 | consumers.push_back(connection); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 385 | } |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 386 | |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 387 | ++it; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 388 | } else { |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 389 | it = mDisplayEventConnections.erase(it); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 393 | if (!consumers.empty()) { |
| 394 | dispatchEvent(*event, consumers); |
| 395 | consumers.clear(); |
| 396 | } |
| 397 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 398 | State nextState; |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 399 | if (mVSyncState && vsyncRequested) { |
| 400 | nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync; |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 401 | } else { |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 402 | ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected"); |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 403 | nextState = State::Idle; |
| 404 | } |
| 405 | |
| 406 | if (mState != nextState) { |
| 407 | if (mState == State::VSync) { |
| 408 | mVSyncSource->setVSyncEnabled(false); |
| 409 | } else if (nextState == State::VSync) { |
| 410 | mVSyncSource->setVSyncEnabled(true); |
| 411 | } |
| 412 | |
| 413 | mState = nextState; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 416 | if (event) { |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | // Wait for event or client registration/request. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 421 | if (mState == State::Idle) { |
| 422 | mCondition.wait(lock); |
| 423 | } else { |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 424 | // Generate a fake VSYNC after a long timeout in case the driver stalls. When the |
| 425 | // display is off, keep feeding clients at 60 Hz. |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 426 | const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 427 | if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) { |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame^] | 428 | if (mState == State::VSync) { |
| 429 | ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName); |
| 430 | std::string debugInfo = "VsyncSource debug info:\n"; |
| 431 | mVSyncSource->dump(debugInfo); |
| 432 | ALOGW("%s", debugInfo.c_str()); |
| 433 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 434 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 435 | LOG_FATAL_IF(!mVSyncState); |
| 436 | mPendingEvents.push_back(makeVSync(mVSyncState->displayId, |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 437 | systemTime(SYSTEM_TIME_MONOTONIC), |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 438 | ++mVSyncState->count)); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 441 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 442 | } |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 443 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 444 | bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event, |
| 445 | const sp<EventThreadConnection>& connection) const { |
| 446 | switch (event.header.type) { |
| 447 | case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: |
| 448 | return true; |
| 449 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 450 | case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: { |
| 451 | const bool forcedDispatch = connection->mForcedConfigChangeDispatch.exchange(false); |
| 452 | return forcedDispatch || |
| 453 | connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch; |
| 454 | } |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 455 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 456 | case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: |
| 457 | switch (connection->vsyncRequest) { |
| 458 | case VSyncRequest::None: |
| 459 | return false; |
| 460 | case VSyncRequest::Single: |
| 461 | connection->vsyncRequest = VSyncRequest::None; |
| 462 | return true; |
| 463 | case VSyncRequest::Periodic: |
| 464 | return true; |
| 465 | default: |
| 466 | return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0; |
| 467 | } |
| 468 | |
| 469 | default: |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event, |
| 475 | const DisplayEventConsumers& consumers) { |
| 476 | for (const auto& consumer : consumers) { |
| 477 | switch (consumer->postEvent(event)) { |
| 478 | case NO_ERROR: |
| 479 | break; |
| 480 | |
| 481 | case -EAGAIN: |
| 482 | // TODO: Try again if pipe is full. |
| 483 | ALOGW("Failed dispatching %s for %s", toString(event).c_str(), |
| 484 | toString(*consumer).c_str()); |
| 485 | break; |
| 486 | |
| 487 | default: |
| 488 | // Treat EPIPE and other errors as fatal. |
| 489 | removeDisplayEventConnectionLocked(consumer); |
| 490 | } |
| 491 | } |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 494 | void EventThread::dump(std::string& result) const { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 495 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 496 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 497 | StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState)); |
| 498 | if (mVSyncState) { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 499 | StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n", |
| 500 | mVSyncState->displayId, mVSyncState->count, |
| 501 | mVSyncState->synthetic ? ", synthetic" : ""); |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 502 | } else { |
| 503 | StringAppendF(&result, "none\n"); |
| 504 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 505 | |
| 506 | StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size()); |
| 507 | for (const auto& event : mPendingEvents) { |
| 508 | StringAppendF(&result, " %s\n", toString(event).c_str()); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 509 | } |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 510 | |
| 511 | StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size()); |
| 512 | for (const auto& ptr : mDisplayEventConnections) { |
| 513 | if (const auto connection = ptr.promote()) { |
| 514 | StringAppendF(&result, " %s\n", toString(*connection).c_str()); |
| 515 | } |
| 516 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 519 | const char* EventThread::toCString(State state) { |
| 520 | switch (state) { |
| 521 | case State::Idle: |
| 522 | return "Idle"; |
| 523 | case State::Quit: |
| 524 | return "Quit"; |
| 525 | case State::SyntheticVSync: |
| 526 | return "SyntheticVSync"; |
| 527 | case State::VSync: |
| 528 | return "VSync"; |
| 529 | } |
| 530 | } |
| 531 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 532 | } // namespace impl |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 533 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 534 | |
| 535 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 536 | #pragma clang diagnostic pop // ignored "-Wconversion" |