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