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