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