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