EventThread.cpp revision fca660cf730161f823e770ad1693fab441477edd
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 <gui/BitTube.h> 23#include <gui/IDisplayEventConnection.h> 24#include <gui/DisplayEventReceiver.h> 25 26#include <utils/Errors.h> 27#include <utils/Trace.h> 28 29#include "DisplayHardware/DisplayHardware.h" 30#include "EventThread.h" 31#include "SurfaceFlinger.h" 32 33// --------------------------------------------------------------------------- 34 35namespace android { 36 37// --------------------------------------------------------------------------- 38 39EventThread::EventThread(const sp<SurfaceFlinger>& flinger) 40 : mFlinger(flinger), 41 mHw(flinger->graphicPlane(0).editDisplayHardware()), 42 mLastVSyncTimestamp(0), 43 mVSyncTimestamp(0), 44 mUseSoftwareVSync(false), 45 mDeliveredEvents(0), 46 mDebugVsyncEnabled(false) 47{ 48} 49 50void EventThread::onFirstRef() { 51 mHw.setVSyncHandler(this); 52 run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE); 53} 54 55sp<EventThread::Connection> EventThread::createEventConnection() const { 56 return new Connection(const_cast<EventThread*>(this)); 57} 58 59status_t EventThread::registerDisplayEventConnection( 60 const sp<EventThread::Connection>& connection) { 61 Mutex::Autolock _l(mLock); 62 mDisplayEventConnections.add(connection); 63 mCondition.signal(); 64 return NO_ERROR; 65} 66 67status_t EventThread::unregisterDisplayEventConnection( 68 const wp<EventThread::Connection>& connection) { 69 Mutex::Autolock _l(mLock); 70 mDisplayEventConnections.remove(connection); 71 mCondition.signal(); 72 return NO_ERROR; 73} 74 75void EventThread::removeDisplayEventConnection( 76 const wp<EventThread::Connection>& connection) { 77 Mutex::Autolock _l(mLock); 78 mDisplayEventConnections.remove(connection); 79} 80 81void EventThread::setVsyncRate(uint32_t count, 82 const sp<EventThread::Connection>& connection) { 83 if (int32_t(count) >= 0) { // server must protect against bad params 84 Mutex::Autolock _l(mLock); 85 const int32_t new_count = (count == 0) ? -1 : count; 86 if (connection->count != new_count) { 87 connection->count = new_count; 88 mCondition.signal(); 89 } 90 } 91} 92 93void EventThread::requestNextVsync( 94 const sp<EventThread::Connection>& connection) { 95 Mutex::Autolock _l(mLock); 96 if (connection->count < 0) { 97 connection->count = 0; 98 mCondition.signal(); 99 } 100} 101 102void 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 114void EventThread::onScreenAcquired() { 115 Mutex::Autolock _l(mLock); 116 mUseSoftwareVSync = false; 117} 118 119 120void EventThread::onVSyncReceived(int, nsecs_t timestamp) { 121 Mutex::Autolock _l(mLock); 122 mVSyncTimestamp = timestamp; 123 mCondition.signal(); 124} 125 126bool EventThread::threadLoop() { 127 128 nsecs_t timestamp; 129 DisplayEventReceiver::Event vsync; 130 Vector< wp<EventThread::Connection> > displayEventConnections; 131 132 do { 133 Mutex::Autolock _l(mLock); 134 do { 135 // latch VSYNC event if any 136 timestamp = mVSyncTimestamp; 137 mVSyncTimestamp = 0; 138 139 // check if we should be waiting for VSYNC events 140 bool waitForNextVsync = false; 141 size_t count = mDisplayEventConnections.size(); 142 for (size_t i=0 ; i<count ; i++) { 143 sp<Connection> connection = 144 mDisplayEventConnections.itemAt(i).promote(); 145 if (connection!=0 && connection->count >= 0) { 146 // at least one continuous mode or active one-shot event 147 waitForNextVsync = true; 148 break; 149 } 150 } 151 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 156 disableVSyncLocked(); 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 168 enableVSyncLocked(); 169 } 170 } 171 172 // wait for something to happen 173 if (mUseSoftwareVSync && waitForNextVsync) { 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 } 183 } while(true); 184 185 // process vsync event 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; 221 222 const size_t count = displayEventConnections.size(); 223 for (size_t i=0 ; i<count ; i++) { 224 sp<Connection> conn(displayEventConnections[i].promote()); 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. 238 removeDisplayEventConnection(displayEventConnections[i]); 239 } 240 } else { 241 // somehow the connection is dead, but we still have it in our list 242 // just clean the list. 243 removeDisplayEventConnection(displayEventConnections[i]); 244 } 245 } 246 247 // clear all our references without holding mLock 248 displayEventConnections.clear(); 249 250 return true; 251} 252 253void EventThread::enableVSyncLocked() { 254 if (!mUseSoftwareVSync) { 255 // never enable h/w VSYNC when screen is off 256 mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, true); 257 } 258 mDebugVsyncEnabled = true; 259} 260 261void EventThread::disableVSyncLocked() { 262 mHw.getHwComposer().eventControl(HWComposer::EVENT_VSYNC, false); 263 mDebugVsyncEnabled = false; 264} 265 266status_t EventThread::readyToRun() { 267 ALOGI("EventThread ready to run."); 268 return NO_ERROR; 269} 270 271void EventThread::dump(String8& result, char* buffer, size_t SIZE) const { 272 Mutex::Autolock _l(mLock); 273 result.appendFormat("VSYNC state: %s\n", 274 mDebugVsyncEnabled?"enabled":"disabled"); 275 result.appendFormat(" soft-vsync: %s\n", 276 mUseSoftwareVSync?"enabled":"disabled"); 277 result.appendFormat(" numListeners=%u,\n events-delivered: %u\n", 278 mDisplayEventConnections.size(), mDeliveredEvents); 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 } 285} 286 287// --------------------------------------------------------------------------- 288 289EventThread::Connection::Connection( 290 const sp<EventThread>& eventThread) 291 : count(-1), mEventThread(eventThread), mChannel(new BitTube()) 292{ 293} 294 295EventThread::Connection::~Connection() { 296 mEventThread->unregisterDisplayEventConnection(this); 297} 298 299void EventThread::Connection::onFirstRef() { 300 // NOTE: mEventThread doesn't hold a strong reference on us 301 mEventThread->registerDisplayEventConnection(this); 302} 303 304sp<BitTube> EventThread::Connection::getDataChannel() const { 305 return mChannel; 306} 307 308void EventThread::Connection::setVsyncRate(uint32_t count) { 309 mEventThread->setVsyncRate(count, this); 310} 311 312void EventThread::Connection::requestNextVsync() { 313 mEventThread->requestNextVsync(this); 314} 315 316status_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 324}; // namespace android 325