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