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 | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 22 | #include <gui/BitTube.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 23 | #include <gui/IDisplayEventConnection.h> |
| 24 | #include <gui/DisplayEventReceiver.h> |
| 25 | |
| 26 | #include <utils/Errors.h> |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 27 | #include <utils/Trace.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 28 | |
| 29 | #include "DisplayHardware/DisplayHardware.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | #include "EventThread.h" |
| 31 | #include "SurfaceFlinger.h" |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | // --------------------------------------------------------------------------- |
| 38 | |
| 39 | EventThread::EventThread(const sp<SurfaceFlinger>& flinger) |
| 40 | : mFlinger(flinger), |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 41 | mHw(flinger->graphicPlane(0).editDisplayHardware()), |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 42 | mLastVSyncTimestamp(0), |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 43 | mVSyncTimestamp(0), |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 44 | mUseSoftwareVSync(false), |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 45 | mDeliveredEvents(0), |
| 46 | mDebugVsyncEnabled(false) |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | void EventThread::onFirstRef() { |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 51 | mHw.setVSyncHandler(this); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 52 | run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); |
| 53 | } |
| 54 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 55 | sp<EventThread::Connection> EventThread::createEventConnection() const { |
| 56 | return new Connection(const_cast<EventThread*>(this)); |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 59 | status_t EventThread::registerDisplayEventConnection( |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 60 | const sp<EventThread::Connection>& connection) { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 61 | Mutex::Autolock _l(mLock); |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 62 | mDisplayEventConnections.add(connection); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 63 | mCondition.signal(); |
| 64 | return NO_ERROR; |
| 65 | } |
| 66 | |
| 67 | status_t EventThread::unregisterDisplayEventConnection( |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 68 | const wp<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.remove(connection); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 71 | mCondition.signal(); |
| 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; |
| 88 | mCondition.signal(); |
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); |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 96 | if (connection->count < 0) { |
| 97 | connection->count = 0; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 98 | mCondition.signal(); |
| 99 | } |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 102 | void EventThread::onScreenReleased() { |
| 103 | Mutex::Autolock _l(mLock); |
| 104 | // wait for an eventual pending vsync to be serviced |
| 105 | if (!mUseSoftwareVSync) { |
| 106 | while (mVSyncTimestamp) { |
| 107 | mCondition.wait(mLock); |
| 108 | } |
| 109 | } |
| 110 | // disable reliance on h/w vsync |
| 111 | mUseSoftwareVSync = true; |
| 112 | } |
| 113 | |
| 114 | void EventThread::onScreenAcquired() { |
| 115 | Mutex::Autolock _l(mLock); |
| 116 | mUseSoftwareVSync = false; |
| 117 | } |
| 118 | |
| 119 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 120 | void EventThread::onVSyncReceived(int, nsecs_t timestamp) { |
| 121 | Mutex::Autolock _l(mLock); |
| 122 | mVSyncTimestamp = timestamp; |
| 123 | mCondition.signal(); |
| 124 | } |
| 125 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 126 | bool EventThread::threadLoop() { |
| 127 | |
| 128 | nsecs_t timestamp; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 129 | DisplayEventReceiver::Event vsync; |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 130 | Vector< wp<EventThread::Connection> > displayEventConnections; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 131 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 132 | do { |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 133 | Mutex::Autolock _l(mLock); |
| 134 | do { |
Mathias Agopian | d94d3b8 | 2012-04-08 15:01:31 -0700 | [diff] [blame] | 135 | // latch VSYNC event if any |
| 136 | timestamp = mVSyncTimestamp; |
| 137 | mVSyncTimestamp = 0; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 138 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 139 | // check if we should be waiting for VSYNC events |
| 140 | bool waitForNextVsync = false; |
| 141 | size_t count = mDisplayEventConnections.size(); |
Mathias Agopian | a72d0db | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 142 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 143 | sp<Connection> connection = |
| 144 | mDisplayEventConnections.itemAt(i).promote(); |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 145 | if (connection!=0 && connection->count >= 0) { |
| 146 | // at least one continuous mode or active one-shot event |
| 147 | waitForNextVsync = true; |
| 148 | break; |
Mathias Agopian | 616c0cd | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 149 | } |
Mathias Agopian | a72d0db | 2012-01-09 18:19:18 -0800 | [diff] [blame] | 150 | } |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 151 | |
Mathias Agopian | d94d3b8 | 2012-04-08 15:01:31 -0700 | [diff] [blame] | 152 | if (timestamp) { |
| 153 | if (!waitForNextVsync) { |
| 154 | // we received a VSYNC but we have no clients |
| 155 | // don't report it, and disable VSYNC events |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 156 | disableVSyncLocked(); |
Mathias Agopian | d94d3b8 | 2012-04-08 15:01:31 -0700 | [diff] [blame] | 157 | } else { |
| 158 | // report VSYNC event |
| 159 | break; |
| 160 | } |
| 161 | } else { |
| 162 | // never disable VSYNC events immediately, instead |
| 163 | // we'll wait to receive the event and we'll |
| 164 | // reevaluate whether we need to dispatch it and/or |
| 165 | // disable VSYNC events then. |
| 166 | if (waitForNextVsync) { |
| 167 | // enable |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 168 | enableVSyncLocked(); |
Mathias Agopian | d94d3b8 | 2012-04-08 15:01:31 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 171 | |
| 172 | // wait for something to happen |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 173 | if (mUseSoftwareVSync == true) { |
| 174 | // h/w vsync cannot be used (screen is off), so we use |
| 175 | // a timeout instead. it doesn't matter how imprecise this |
| 176 | // is, we just need to make sure to serve the clients |
| 177 | if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) { |
| 178 | mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 179 | } |
| 180 | } else { |
| 181 | mCondition.wait(mLock); |
| 182 | } |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 183 | } while(true); |
| 184 | |
| 185 | // process vsync event |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 186 | mDeliveredEvents++; |
| 187 | mLastVSyncTimestamp = timestamp; |
| 188 | |
| 189 | // now see if we still need to report this VSYNC event |
| 190 | const size_t count = mDisplayEventConnections.size(); |
| 191 | for (size_t i=0 ; i<count ; i++) { |
| 192 | bool reportVsync = false; |
| 193 | sp<Connection> connection = |
| 194 | mDisplayEventConnections.itemAt(i).promote(); |
| 195 | if (connection == 0) |
| 196 | continue; |
| 197 | |
| 198 | const int32_t count = connection->count; |
| 199 | if (count >= 1) { |
| 200 | if (count==1 || (mDeliveredEvents % count) == 0) { |
| 201 | // continuous event, and time to report it |
| 202 | reportVsync = true; |
| 203 | } |
| 204 | } else if (count >= -1) { |
| 205 | if (count == 0) { |
| 206 | // fired this time around |
| 207 | reportVsync = true; |
| 208 | } |
| 209 | connection->count--; |
| 210 | } |
| 211 | if (reportVsync) { |
| 212 | displayEventConnections.add(connection); |
| 213 | } |
| 214 | } |
| 215 | } while (!displayEventConnections.size()); |
| 216 | |
| 217 | // dispatch vsync events to listeners... |
| 218 | vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC; |
| 219 | vsync.header.timestamp = timestamp; |
| 220 | vsync.vsync.count = mDeliveredEvents; |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 221 | |
| 222 | const size_t count = displayEventConnections.size(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 223 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 224 | sp<Connection> conn(displayEventConnections[i].promote()); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 225 | // make sure the connection didn't die |
| 226 | if (conn != NULL) { |
| 227 | status_t err = conn->postEvent(vsync); |
| 228 | if (err == -EAGAIN || err == -EWOULDBLOCK) { |
| 229 | // The destination doesn't accept events anymore, it's probably |
| 230 | // full. For now, we just drop the events on the floor. |
| 231 | // Note that some events cannot be dropped and would have to be |
| 232 | // re-sent later. Right-now we don't have the ability to do |
| 233 | // this, but it doesn't matter for VSYNC. |
| 234 | } else if (err < 0) { |
| 235 | // handle any other error on the pipe as fatal. the only |
| 236 | // reasonable thing to do is to clean-up this connection. |
| 237 | // The most common error we'll get here is -EPIPE. |
Mathias Agopian | 616c0cd | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 238 | removeDisplayEventConnection(displayEventConnections[i]); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 239 | } |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 240 | } else { |
| 241 | // somehow the connection is dead, but we still have it in our list |
| 242 | // just clean the list. |
Mathias Agopian | 616c0cd | 2012-01-12 16:13:54 -0800 | [diff] [blame] | 243 | removeDisplayEventConnection(displayEventConnections[i]); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 247 | // clear all our references without holding mLock |
| 248 | displayEventConnections.clear(); |
| 249 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 253 | void EventThread::enableVSyncLocked() { |
| 254 | if (!mUseSoftwareVSync) { |
| 255 | // never enable h/w VSYNC when screen is off |
| 256 | mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, true); |
| 257 | } |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 258 | mDebugVsyncEnabled = true; |
| 259 | } |
| 260 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 261 | void EventThread::disableVSyncLocked() { |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 262 | mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, false); |
| 263 | mDebugVsyncEnabled = false; |
| 264 | } |
| 265 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 266 | status_t EventThread::readyToRun() { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 267 | ALOGI("EventThread ready to run."); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 268 | return NO_ERROR; |
| 269 | } |
| 270 | |
| 271 | void EventThread::dump(String8& result, char* buffer, size_t SIZE) const { |
| 272 | Mutex::Autolock _l(mLock); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 273 | result.appendFormat("VSYNC state: %s\n", |
| 274 | mDebugVsyncEnabled?"enabled":"disabled"); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame^] | 275 | result.appendFormat(" soft-vsync: %s\n", |
| 276 | mUseSoftwareVSync?"enabled":"disabled"); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 277 | result.appendFormat(" numListeners=%u,\n events-delivered: %u\n", |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 278 | mDisplayEventConnections.size(), mDeliveredEvents); |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 279 | for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) { |
| 280 | sp<Connection> connection = |
| 281 | mDisplayEventConnections.itemAt(i).promote(); |
| 282 | result.appendFormat(" %p: count=%d\n", |
| 283 | connection.get(), connection!=NULL ? connection->count : 0); |
| 284 | } |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // --------------------------------------------------------------------------- |
| 288 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 289 | EventThread::Connection::Connection( |
| 290 | const sp<EventThread>& eventThread) |
| 291 | : count(-1), mEventThread(eventThread), mChannel(new BitTube()) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | EventThread::Connection::~Connection() { |
| 296 | mEventThread->unregisterDisplayEventConnection(this); |
| 297 | } |
| 298 | |
| 299 | void EventThread::Connection::onFirstRef() { |
| 300 | // NOTE: mEventThread doesn't hold a strong reference on us |
| 301 | mEventThread->registerDisplayEventConnection(this); |
| 302 | } |
| 303 | |
| 304 | sp<BitTube> EventThread::Connection::getDataChannel() const { |
| 305 | return mChannel; |
| 306 | } |
| 307 | |
| 308 | void EventThread::Connection::setVsyncRate(uint32_t count) { |
| 309 | mEventThread->setVsyncRate(count, this); |
| 310 | } |
| 311 | |
| 312 | void EventThread::Connection::requestNextVsync() { |
| 313 | mEventThread->requestNextVsync(this); |
| 314 | } |
| 315 | |
| 316 | status_t EventThread::Connection::postEvent( |
| 317 | const DisplayEventReceiver::Event& event) { |
| 318 | ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1); |
| 319 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 320 | } |
| 321 | |
| 322 | // --------------------------------------------------------------------------- |
| 323 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 324 | }; // namespace android |