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> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 22 | #include <chrono> |
| 23 | #include <cstdint> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 24 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
| 26 | |
Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 27 | #include <cutils/compiler.h> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 28 | #include <cutils/sched_policy.h> |
Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 29 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | #include <gui/DisplayEventReceiver.h> |
| 31 | |
| 32 | #include <utils/Errors.h> |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 34 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 35 | #include "EventThread.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 36 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 37 | using namespace std::chrono_literals; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 38 | using android::base::StringAppendF; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 39 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 40 | // --------------------------------------------------------------------------- |
| 41 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
| 44 | // --------------------------------------------------------------------------- |
| 45 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 46 | EventThreadConnection::EventThreadConnection(EventThread* eventThread) |
| 47 | : count(-1), mEventThread(eventThread), mChannel(gui::BitTube::DefaultSize) {} |
| 48 | |
| 49 | EventThreadConnection::~EventThreadConnection() { |
| 50 | // do nothing here -- clean-up will happen automatically |
| 51 | // when the main thread wakes up |
| 52 | } |
| 53 | |
| 54 | void EventThreadConnection::onFirstRef() { |
| 55 | // NOTE: mEventThread doesn't hold a strong reference on us |
| 56 | mEventThread->registerDisplayEventConnection(this); |
| 57 | } |
| 58 | |
| 59 | status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) { |
| 60 | outChannel->setReceiveFd(mChannel.moveReceiveFd()); |
| 61 | return NO_ERROR; |
| 62 | } |
| 63 | |
| 64 | status_t EventThreadConnection::setVsyncRate(uint32_t count) { |
| 65 | mEventThread->setVsyncRate(count, this); |
| 66 | return NO_ERROR; |
| 67 | } |
| 68 | |
| 69 | void EventThreadConnection::requestNextVsync() { |
| 70 | mEventThread->requestNextVsync(this); |
| 71 | } |
| 72 | |
| 73 | status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) { |
| 74 | ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1); |
| 75 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 76 | } |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 80 | EventThread::~EventThread() = default; |
| 81 | |
| 82 | namespace impl { |
| 83 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 84 | EventThread::EventThread(std::unique_ptr<VSyncSource> src, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 85 | const ResyncWithRateLimitCallback& resyncWithRateLimitCallback, |
| 86 | const InterceptVSyncsCallback& interceptVSyncsCallback, |
| 87 | const ResetIdleTimerCallback& resetIdleTimerCallback, |
| 88 | const char* threadName) |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 89 | : EventThread(nullptr, std::move(src), resyncWithRateLimitCallback, interceptVSyncsCallback, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 90 | threadName) { |
| 91 | mResetIdleTimer = resetIdleTimerCallback; |
| 92 | } |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 93 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 94 | EventThread::EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback, |
| 95 | InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName) |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 96 | : EventThread(src, nullptr, resyncWithRateLimitCallback, interceptVSyncsCallback, |
| 97 | threadName) {} |
| 98 | |
| 99 | EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc, |
| 100 | ResyncWithRateLimitCallback resyncWithRateLimitCallback, |
| 101 | InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName) |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 102 | : mVSyncSource(src), |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 103 | mVSyncSourceUnique(std::move(uniqueSrc)), |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 104 | mResyncWithRateLimitCallback(resyncWithRateLimitCallback), |
| 105 | mInterceptVSyncsCallback(interceptVSyncsCallback) { |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 106 | if (src == nullptr) { |
| 107 | mVSyncSource = mVSyncSourceUnique.get(); |
| 108 | } |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 109 | for (auto& event : mVSyncEvent) { |
| 110 | event.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC; |
| 111 | event.header.id = 0; |
| 112 | event.header.timestamp = 0; |
| 113 | event.vsync.count = 0; |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 114 | } |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 115 | |
| 116 | mThread = std::thread(&EventThread::threadMain, this); |
| 117 | |
| 118 | pthread_setname_np(mThread.native_handle(), threadName); |
| 119 | |
| 120 | pid_t tid = pthread_gettid_np(mThread.native_handle()); |
| 121 | |
| 122 | // Use SCHED_FIFO to minimize jitter |
| 123 | constexpr int EVENT_THREAD_PRIORITY = 2; |
| 124 | struct sched_param param = {0}; |
| 125 | param.sched_priority = EVENT_THREAD_PRIORITY; |
| 126 | if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, ¶m) != 0) { |
| 127 | ALOGE("Couldn't set SCHED_FIFO for EventThread"); |
| 128 | } |
| 129 | |
| 130 | set_sched_policy(tid, SP_FOREGROUND); |
| 131 | } |
| 132 | |
| 133 | EventThread::~EventThread() { |
| 134 | { |
| 135 | std::lock_guard<std::mutex> lock(mMutex); |
| 136 | mKeepRunning = false; |
| 137 | mCondition.notify_all(); |
| 138 | } |
| 139 | mThread.join(); |
Ruchi Kandoi | ef472ec | 2014-04-02 12:50:06 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 142 | void EventThread::setPhaseOffset(nsecs_t phaseOffset) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 143 | std::lock_guard<std::mutex> lock(mMutex); |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 144 | mVSyncSource->setPhaseOffset(phaseOffset); |
| 145 | } |
| 146 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 147 | sp<EventThreadConnection> EventThread::createEventConnection() const { |
| 148 | return new EventThreadConnection(const_cast<EventThread*>(this)); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 151 | status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 152 | std::lock_guard<std::mutex> lock(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 153 | |
| 154 | // this should never happen |
| 155 | auto it = std::find(mDisplayEventConnections.cbegin(), |
| 156 | mDisplayEventConnections.cend(), connection); |
| 157 | if (it != mDisplayEventConnections.cend()) { |
| 158 | ALOGW("DisplayEventConnection %p already exists", connection.get()); |
| 159 | mCondition.notify_all(); |
| 160 | return ALREADY_EXISTS; |
| 161 | } |
| 162 | |
| 163 | mDisplayEventConnections.push_back(connection); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 164 | mCondition.notify_all(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 165 | return NO_ERROR; |
| 166 | } |
| 167 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 168 | void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) { |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 169 | auto it = std::find(mDisplayEventConnections.cbegin(), |
| 170 | mDisplayEventConnections.cend(), connection); |
| 171 | if (it != mDisplayEventConnections.cend()) { |
| 172 | mDisplayEventConnections.erase(it); |
| 173 | } |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 176 | void EventThread::setVsyncRate(uint32_t count, const sp<EventThreadConnection>& connection) { |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 177 | if (int32_t(count) >= 0) { // server must protect against bad params |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 178 | std::lock_guard<std::mutex> lock(mMutex); |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 179 | const int32_t new_count = (count == 0) ? -1 : count; |
| 180 | if (connection->count != new_count) { |
| 181 | connection->count = new_count; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 182 | mCondition.notify_all(); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 187 | void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 188 | std::lock_guard<std::mutex> lock(mMutex); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 189 | if (mResetIdleTimer) { |
| 190 | mResetIdleTimer(); |
| 191 | } |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 192 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 193 | if (mResyncWithRateLimitCallback) { |
| 194 | mResyncWithRateLimitCallback(); |
| 195 | } |
Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 196 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 197 | if (connection->count < 0) { |
| 198 | connection->count = 0; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 199 | mCondition.notify_all(); |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 200 | } |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 203 | void EventThread::onScreenReleased() { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 204 | std::lock_guard<std::mutex> lock(mMutex); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 205 | if (!mUseSoftwareVSync) { |
Mathias Agopian | 7d88647 | 2012-06-14 23:39:35 -0700 | [diff] [blame] | 206 | // disable reliance on h/w vsync |
| 207 | mUseSoftwareVSync = true; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 208 | mCondition.notify_all(); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 209 | } |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void EventThread::onScreenAcquired() { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 213 | std::lock_guard<std::mutex> lock(mMutex); |
Mathias Agopian | 7d88647 | 2012-06-14 23:39:35 -0700 | [diff] [blame] | 214 | if (mUseSoftwareVSync) { |
| 215 | // resume use of h/w vsync |
| 216 | mUseSoftwareVSync = false; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 217 | mCondition.notify_all(); |
Mathias Agopian | 7d88647 | 2012-06-14 23:39:35 -0700 | [diff] [blame] | 218 | } |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 221 | void EventThread::onVSyncEvent(nsecs_t timestamp) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 222 | std::lock_guard<std::mutex> lock(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 223 | mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC; |
| 224 | mVSyncEvent[0].header.id = 0; |
| 225 | mVSyncEvent[0].header.timestamp = timestamp; |
| 226 | mVSyncEvent[0].vsync.count++; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 227 | mCondition.notify_all(); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 230 | void EventThread::onHotplugReceived(DisplayType displayType, bool connected) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 231 | std::lock_guard<std::mutex> lock(mMutex); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 232 | |
| 233 | DisplayEventReceiver::Event event; |
| 234 | event.header.type = DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG; |
| 235 | event.header.id = displayType == DisplayType::Primary ? 0 : 1; |
| 236 | event.header.timestamp = systemTime(); |
| 237 | event.hotplug.connected = connected; |
| 238 | |
Chia-I Wu | eac3db2 | 2018-09-14 11:28:03 -0700 | [diff] [blame] | 239 | mPendingEvents.push(event); |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 240 | mCondition.notify_all(); |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 243 | void EventThread::threadMain() NO_THREAD_SAFETY_ANALYSIS { |
| 244 | std::unique_lock<std::mutex> lock(mMutex); |
| 245 | while (mKeepRunning) { |
| 246 | DisplayEventReceiver::Event event; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 247 | std::vector<sp<EventThreadConnection>> signalConnections; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 248 | signalConnections = waitForEventLocked(&lock, &event); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 249 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 250 | // dispatch events to listeners... |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 251 | for (const sp<EventThreadConnection>& conn : signalConnections) { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 252 | // now see if we still need to report this event |
| 253 | status_t err = conn->postEvent(event); |
| 254 | if (err == -EAGAIN || err == -EWOULDBLOCK) { |
| 255 | // The destination doesn't accept events anymore, it's probably |
| 256 | // full. For now, we just drop the events on the floor. |
| 257 | // FIXME: Note that some events cannot be dropped and would have |
| 258 | // to be re-sent later. |
| 259 | // Right-now we don't have the ability to do this. |
| 260 | ALOGW("EventThread: dropping event (%08x) for connection %p", event.header.type, |
| 261 | conn.get()); |
| 262 | } else if (err < 0) { |
| 263 | // handle any other error on the pipe as fatal. the only |
| 264 | // reasonable thing to do is to clean-up this connection. |
| 265 | // The most common error we'll get here is -EPIPE. |
Chia-I Wu | b02d51c | 2018-09-14 11:20:48 -0700 | [diff] [blame] | 266 | removeDisplayEventConnectionLocked(conn); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 267 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 272 | // This will return when (1) a vsync event has been received, and (2) there was |
| 273 | // at least one connection interested in receiving it when we started waiting. |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 274 | std::vector<sp<EventThreadConnection>> EventThread::waitForEventLocked( |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 275 | std::unique_lock<std::mutex>* lock, DisplayEventReceiver::Event* outEvent) { |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 276 | std::vector<sp<EventThreadConnection>> signalConnections; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 277 | |
Chia-I Wu | b02d51c | 2018-09-14 11:20:48 -0700 | [diff] [blame] | 278 | while (signalConnections.empty() && mKeepRunning) { |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 279 | bool eventPending = false; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 280 | bool waitForVSync = false; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 281 | |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 282 | size_t vsyncCount = 0; |
| 283 | nsecs_t timestamp = 0; |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 284 | for (auto& event : mVSyncEvent) { |
| 285 | timestamp = event.header.timestamp; |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 286 | if (timestamp) { |
| 287 | // we have a vsync event to dispatch |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 288 | if (mInterceptVSyncsCallback) { |
| 289 | mInterceptVSyncsCallback(timestamp); |
Irvel | ab04685 | 2016-07-28 11:23:08 -0700 | [diff] [blame] | 290 | } |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 291 | *outEvent = event; |
| 292 | event.header.timestamp = 0; |
| 293 | vsyncCount = event.vsync.count; |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (!timestamp) { |
| 299 | // no vsync event, see if there are some other event |
Chia-I Wu | eac3db2 | 2018-09-14 11:28:03 -0700 | [diff] [blame] | 300 | eventPending = !mPendingEvents.empty(); |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 301 | if (eventPending) { |
| 302 | // we have some other event to dispatch |
Chia-I Wu | eac3db2 | 2018-09-14 11:28:03 -0700 | [diff] [blame] | 303 | *outEvent = mPendingEvents.front(); |
| 304 | mPendingEvents.pop(); |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 308 | // find out connections waiting for events |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 309 | auto it = mDisplayEventConnections.begin(); |
| 310 | while (it != mDisplayEventConnections.end()) { |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 311 | sp<EventThreadConnection> connection(it->promote()); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 312 | if (connection != nullptr) { |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 313 | bool added = false; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 314 | if (connection->count >= 0) { |
| 315 | // we need vsync events because at least |
| 316 | // one connection is waiting for it |
| 317 | waitForVSync = true; |
| 318 | if (timestamp) { |
| 319 | // we consume the event only if it's time |
| 320 | // (ie: we received a vsync event) |
| 321 | if (connection->count == 0) { |
| 322 | // fired this time around |
| 323 | connection->count = -1; |
Chia-I Wu | b02d51c | 2018-09-14 11:20:48 -0700 | [diff] [blame] | 324 | signalConnections.push_back(connection); |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 325 | added = true; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 326 | } else if (connection->count == 1 || |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 327 | (vsyncCount % connection->count) == 0) { |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 328 | // continuous event, and time to report it |
Chia-I Wu | b02d51c | 2018-09-14 11:20:48 -0700 | [diff] [blame] | 329 | signalConnections.push_back(connection); |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 330 | added = true; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 334 | |
| 335 | if (eventPending && !timestamp && !added) { |
| 336 | // we don't have a vsync event to process |
| 337 | // (timestamp==0), but we have some pending |
| 338 | // messages. |
Chia-I Wu | b02d51c | 2018-09-14 11:20:48 -0700 | [diff] [blame] | 339 | signalConnections.push_back(connection); |
Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 340 | } |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 341 | ++it; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 342 | } else { |
| 343 | // we couldn't promote this reference, the connection has |
| 344 | // died, so clean-up! |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 345 | it = mDisplayEventConnections.erase(it); |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | // Here we figure out if we need to enable or disable vsyncs |
| 350 | if (timestamp && !waitForVSync) { |
| 351 | // we received a VSYNC but we have no clients |
| 352 | // don't report it, and disable VSYNC events |
| 353 | disableVSyncLocked(); |
| 354 | } else if (!timestamp && waitForVSync) { |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 355 | // we have at least one client, so we want vsync enabled |
| 356 | // (TODO: this function is called right after we finish |
| 357 | // notifying clients of a vsync, so this call will be made |
| 358 | // at the vsync rate, e.g. 60fps. If we can accurately |
| 359 | // track the current state we could avoid making this call |
| 360 | // so often.) |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 361 | enableVSyncLocked(); |
| 362 | } |
| 363 | |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 364 | // note: !timestamp implies signalConnections.isEmpty(), because we |
| 365 | // don't populate signalConnections if there's no vsync pending |
Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 366 | if (!timestamp && !eventPending) { |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 367 | // wait for something to happen |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 368 | if (waitForVSync) { |
| 369 | // This is where we spend most of our time, waiting |
| 370 | // for vsync events and new client registrations. |
| 371 | // |
| 372 | // If the screen is off, we can't use h/w vsync, so we |
| 373 | // use a 16ms timeout instead. It doesn't need to be |
| 374 | // precise, we just need to keep feeding our clients. |
| 375 | // |
| 376 | // We don't want to stall if there's a driver bug, so we |
| 377 | // use a (long) timeout when waiting for h/w vsync, and |
| 378 | // generate fake events when necessary. |
| 379 | bool softwareSync = mUseSoftwareVSync; |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 380 | auto timeout = softwareSync ? 16ms : 1000ms; |
| 381 | if (mCondition.wait_for(*lock, timeout) == std::cv_status::timeout) { |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 382 | if (!softwareSync) { |
| 383 | ALOGW("Timed out waiting for hw vsync; faking it"); |
| 384 | } |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 385 | // FIXME: how do we decide which display id the fake |
| 386 | // vsync came from ? |
| 387 | mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC; |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 388 | mVSyncEvent[0].header.id = 0; |
Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 389 | mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 390 | mVSyncEvent[0].vsync.count++; |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 391 | } |
| 392 | } else { |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 393 | // Nobody is interested in vsync, so we just want to sleep. |
| 394 | // h/w vsync should be disabled, so this will wait until we |
| 395 | // get a new connection, or an existing connection becomes |
| 396 | // interested in receiving vsync again. |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 397 | mCondition.wait(*lock); |
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 | } |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 401 | |
| 402 | // here we're guaranteed to have a timestamp and some connections to signal |
Andy McFadden | 6bf552e | 2012-08-30 16:34:41 -0700 | [diff] [blame] | 403 | // (The connections might have dropped out of mDisplayEventConnections |
| 404 | // while we were asleep, but we'll still have strong references to them.) |
Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 405 | return signalConnections; |
| 406 | } |
| 407 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 408 | void EventThread::enableVSyncLocked() { |
| 409 | if (!mUseSoftwareVSync) { |
| 410 | // never enable h/w VSYNC when screen is off |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 411 | if (!mVsyncEnabled) { |
| 412 | mVsyncEnabled = true; |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 413 | mVSyncSource->setCallback(this); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 414 | mVSyncSource->setVSyncEnabled(true); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 415 | } |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 416 | } |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 417 | mDebugVsyncEnabled = true; |
| 418 | } |
| 419 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 420 | void EventThread::disableVSyncLocked() { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 421 | if (mVsyncEnabled) { |
| 422 | mVsyncEnabled = false; |
| 423 | mVSyncSource->setVSyncEnabled(false); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 424 | mDebugVsyncEnabled = false; |
| 425 | } |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 428 | void EventThread::dump(std::string& result) const { |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 429 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 430 | StringAppendF(&result, "VSYNC state: %s\n", mDebugVsyncEnabled ? "enabled" : "disabled"); |
| 431 | StringAppendF(&result, " soft-vsync: %s\n", mUseSoftwareVSync ? "enabled" : "disabled"); |
| 432 | StringAppendF(&result, " numListeners=%zu,\n events-delivered: %u\n", |
| 433 | mDisplayEventConnections.size(), mVSyncEvent[0].vsync.count); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame^] | 434 | for (const wp<EventThreadConnection>& weak : mDisplayEventConnections) { |
| 435 | sp<EventThreadConnection> connection = weak.promote(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 436 | StringAppendF(&result, " %p: count=%d\n", connection.get(), |
| 437 | connection != nullptr ? connection->count : 0); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 438 | } |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 439 | StringAppendF(&result, " other-events-pending: %zu\n", mPendingEvents.size()); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 442 | } // namespace impl |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 443 | } // namespace android |