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