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