EventThread.cpp revision f6bbd44a23c2791277db7814a894633de04cd460
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 17#define ATRACE_TAG ATRACE_TAG_GRAPHICS 18 19#include <stdint.h> 20#include <sys/types.h> 21 22#include <cutils/compiler.h> 23 24#include <gui/BitTube.h> 25#include <gui/IDisplayEventConnection.h> 26#include <gui/DisplayEventReceiver.h> 27 28#include <utils/Errors.h> 29#include <utils/String8.h> 30#include <utils/Trace.h> 31 32#include "EventThread.h" 33#include "SurfaceFlinger.h" 34 35// --------------------------------------------------------------------------- 36namespace android { 37// --------------------------------------------------------------------------- 38 39EventThread::EventThread(const sp<SurfaceFlinger>& flinger) 40 : mFlinger(flinger), 41 mVSyncTimestamp(0), 42 mUseSoftwareVSync(false), 43 mVSyncCount(0), 44 mDebugVsyncEnabled(false) { 45} 46 47void EventThread::onFirstRef() { 48 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); 49} 50 51sp<EventThread::Connection> EventThread::createEventConnection() const { 52 return new Connection(const_cast<EventThread*>(this)); 53} 54 55status_t EventThread::registerDisplayEventConnection( 56 const sp<EventThread::Connection>& connection) { 57 Mutex::Autolock _l(mLock); 58 mDisplayEventConnections.add(connection); 59 mCondition.broadcast(); 60 return NO_ERROR; 61} 62 63void EventThread::removeDisplayEventConnection( 64 const wp<EventThread::Connection>& connection) { 65 Mutex::Autolock _l(mLock); 66 mDisplayEventConnections.remove(connection); 67} 68 69void EventThread::setVsyncRate(uint32_t count, 70 const sp<EventThread::Connection>& connection) { 71 if (int32_t(count) >= 0) { // server must protect against bad params 72 Mutex::Autolock _l(mLock); 73 const int32_t new_count = (count == 0) ? -1 : count; 74 if (connection->count != new_count) { 75 connection->count = new_count; 76 mCondition.broadcast(); 77 } 78 } 79} 80 81void EventThread::requestNextVsync( 82 const sp<EventThread::Connection>& connection) { 83 Mutex::Autolock _l(mLock); 84 if (connection->count < 0) { 85 connection->count = 0; 86 mCondition.broadcast(); 87 } 88} 89 90void EventThread::onScreenReleased() { 91 Mutex::Autolock _l(mLock); 92 if (!mUseSoftwareVSync) { 93 // disable reliance on h/w vsync 94 mUseSoftwareVSync = true; 95 mCondition.broadcast(); 96 } 97} 98 99void EventThread::onScreenAcquired() { 100 Mutex::Autolock _l(mLock); 101 if (mUseSoftwareVSync) { 102 // resume use of h/w vsync 103 mUseSoftwareVSync = false; 104 mCondition.broadcast(); 105 } 106} 107 108 109void EventThread::onVSyncReceived(int, nsecs_t timestamp) { 110 Mutex::Autolock _l(mLock); 111 mVSyncTimestamp = timestamp; 112 mVSyncCount++; 113 mCondition.broadcast(); 114} 115 116bool EventThread::threadLoop() { 117 DisplayEventReceiver::Event vsync; 118 Vector< sp<EventThread::Connection> > signalConnections; 119 signalConnections = waitForEvent(&vsync); 120 121 // dispatch vsync events to listeners... 122 const size_t count = signalConnections.size(); 123 for (size_t i=0 ; i<count ; i++) { 124 const sp<Connection>& conn(signalConnections[i]); 125 // now see if we still need to report this VSYNC event 126 status_t err = conn->postEvent(vsync); 127 if (err == -EAGAIN || err == -EWOULDBLOCK) { 128 // The destination doesn't accept events anymore, it's probably 129 // full. For now, we just drop the events on the floor. 130 // Note that some events cannot be dropped and would have to be 131 // re-sent later. Right-now we don't have the ability to do 132 // this, but it doesn't matter for VSYNC. 133 } else if (err < 0) { 134 // handle any other error on the pipe as fatal. the only 135 // reasonable thing to do is to clean-up this connection. 136 // The most common error we'll get here is -EPIPE. 137 removeDisplayEventConnection(signalConnections[i]); 138 } 139 } 140 return true; 141} 142 143Vector< sp<EventThread::Connection> > EventThread::waitForEvent( 144 DisplayEventReceiver::Event* event) 145{ 146 Mutex::Autolock _l(mLock); 147 148 size_t vsyncCount; 149 nsecs_t timestamp; 150 Vector< sp<EventThread::Connection> > signalConnections; 151 152 do { 153 // latch VSYNC event if any 154 bool waitForVSync = false; 155 vsyncCount = mVSyncCount; 156 timestamp = mVSyncTimestamp; 157 mVSyncTimestamp = 0; 158 159 // find out connections waiting for events 160 size_t count = mDisplayEventConnections.size(); 161 for (size_t i=0 ; i<count ; i++) { 162 sp<Connection> connection(mDisplayEventConnections[i].promote()); 163 if (connection != NULL) { 164 if (connection->count >= 0) { 165 // we need vsync events because at least 166 // one connection is waiting for it 167 waitForVSync = true; 168 if (timestamp) { 169 // we consume the event only if it's time 170 // (ie: we received a vsync event) 171 if (connection->count == 0) { 172 // fired this time around 173 connection->count = -1; 174 signalConnections.add(connection); 175 } else if (connection->count == 1 || 176 (vsyncCount % connection->count) == 0) { 177 // continuous event, and time to report it 178 signalConnections.add(connection); 179 } 180 } 181 } 182 } else { 183 // we couldn't promote this reference, the connection has 184 // died, so clean-up! 185 mDisplayEventConnections.removeAt(i); 186 --i; --count; 187 } 188 } 189 190 // Here we figure out if we need to enable or disable vsyncs 191 if (timestamp && !waitForVSync) { 192 // we received a VSYNC but we have no clients 193 // don't report it, and disable VSYNC events 194 disableVSyncLocked(); 195 } else if (!timestamp && waitForVSync) { 196 enableVSyncLocked(); 197 } 198 199 // note: !timestamp implies signalConnections.isEmpty() 200 if (!timestamp) { 201 // wait for something to happen 202 if (CC_UNLIKELY(mUseSoftwareVSync && waitForVSync)) { 203 // h/w vsync cannot be used (screen is off), so we use 204 // a timeout instead. it doesn't matter how imprecise this 205 // is, we just need to make sure to serve the clients 206 if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) { 207 mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC); 208 mVSyncCount++; 209 } 210 } else { 211 // This is where we spend most of our time, waiting 212 // for a vsync events and registered clients 213 mCondition.wait(mLock); 214 } 215 } 216 } while (signalConnections.isEmpty()); 217 218 // here we're guaranteed to have a timestamp and some connections to signal 219 220 // dispatch vsync events to listeners... 221 event->header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC; 222 event->header.timestamp = timestamp; 223 event->vsync.count = vsyncCount; 224 225 return signalConnections; 226} 227 228void EventThread::enableVSyncLocked() { 229 if (!mUseSoftwareVSync) { 230 // never enable h/w VSYNC when screen is off 231 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true); 232 mPowerHAL.vsyncHint(true); 233 } 234 mDebugVsyncEnabled = true; 235} 236 237void EventThread::disableVSyncLocked() { 238 mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false); 239 mPowerHAL.vsyncHint(false); 240 mDebugVsyncEnabled = false; 241} 242 243status_t EventThread::readyToRun() { 244 ALOGI("EventThread ready to run."); 245 return NO_ERROR; 246} 247 248void EventThread::dump(String8& result, char* buffer, size_t SIZE) const { 249 Mutex::Autolock _l(mLock); 250 result.appendFormat("VSYNC state: %s\n", 251 mDebugVsyncEnabled?"enabled":"disabled"); 252 result.appendFormat(" soft-vsync: %s\n", 253 mUseSoftwareVSync?"enabled":"disabled"); 254 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n", 255 mDisplayEventConnections.size(), mVSyncCount); 256 for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) { 257 sp<Connection> connection = 258 mDisplayEventConnections.itemAt(i).promote(); 259 result.appendFormat(" %p: count=%d\n", 260 connection.get(), connection!=NULL ? connection->count : 0); 261 } 262} 263 264// --------------------------------------------------------------------------- 265 266EventThread::Connection::Connection( 267 const sp<EventThread>& eventThread) 268 : count(-1), mEventThread(eventThread), mChannel(new BitTube()) 269{ 270} 271 272EventThread::Connection::~Connection() { 273 // do nothing here -- clean-up will happen automatically 274 // when the main thread wakes up 275} 276 277void EventThread::Connection::onFirstRef() { 278 // NOTE: mEventThread doesn't hold a strong reference on us 279 mEventThread->registerDisplayEventConnection(this); 280} 281 282sp<BitTube> EventThread::Connection::getDataChannel() const { 283 return mChannel; 284} 285 286void EventThread::Connection::setVsyncRate(uint32_t count) { 287 mEventThread->setVsyncRate(count, this); 288} 289 290void EventThread::Connection::requestNextVsync() { 291 mEventThread->requestNextVsync(this); 292} 293 294status_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 302}; // namespace android 303