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