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