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