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