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