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