2011-11-18 01:49:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-03-01 23:44:37 +00:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
|
|
|
|
2011-11-18 01:49:17 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-04-04 00:48:03 +00:00
|
|
|
#include <gui/BitTube.h>
|
2011-11-18 01:49:17 +00:00
|
|
|
#include <gui/IDisplayEventConnection.h>
|
|
|
|
#include <gui/DisplayEventReceiver.h>
|
|
|
|
|
|
|
|
#include <utils/Errors.h>
|
2012-07-24 06:11:29 +00:00
|
|
|
#include <utils/String8.h>
|
2012-03-01 23:44:37 +00:00
|
|
|
#include <utils/Trace.h>
|
2011-11-18 01:49:17 +00:00
|
|
|
|
|
|
|
#include "EventThread.h"
|
|
|
|
#include "SurfaceFlinger.h"
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
EventThread::EventThread(const sp<SurfaceFlinger>& flinger)
|
2012-07-25 05:46:10 +00:00
|
|
|
: mFlinger(flinger),
|
2012-01-25 00:39:14 +00:00
|
|
|
mLastVSyncTimestamp(0),
|
2012-04-04 05:09:52 +00:00
|
|
|
mVSyncTimestamp(0),
|
2012-04-11 04:04:02 +00:00
|
|
|
mUseSoftwareVSync(false),
|
2012-04-11 01:25:31 +00:00
|
|
|
mDeliveredEvents(0),
|
2012-07-24 06:11:29 +00:00
|
|
|
mDebugVsyncEnabled(false) {
|
2011-11-18 01:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::onFirstRef() {
|
|
|
|
run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
|
|
|
|
}
|
|
|
|
|
2012-04-04 00:48:03 +00:00
|
|
|
sp<EventThread::Connection> EventThread::createEventConnection() const {
|
|
|
|
return new Connection(const_cast<EventThread*>(this));
|
2012-01-25 00:39:14 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 01:49:17 +00:00
|
|
|
status_t EventThread::registerDisplayEventConnection(
|
2012-04-04 00:48:03 +00:00
|
|
|
const sp<EventThread::Connection>& connection) {
|
2011-11-18 01:49:17 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-04-04 00:48:03 +00:00
|
|
|
mDisplayEventConnections.add(connection);
|
2012-06-15 06:39:35 +00:00
|
|
|
mCondition.broadcast();
|
2011-11-18 01:49:17 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t EventThread::unregisterDisplayEventConnection(
|
2012-04-04 00:48:03 +00:00
|
|
|
const wp<EventThread::Connection>& connection) {
|
2011-11-18 01:49:17 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-04-04 00:48:03 +00:00
|
|
|
mDisplayEventConnections.remove(connection);
|
2012-06-15 06:39:35 +00:00
|
|
|
mCondition.broadcast();
|
2011-11-18 01:49:17 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-12-07 01:22:19 +00:00
|
|
|
void EventThread::removeDisplayEventConnection(
|
2012-04-04 00:48:03 +00:00
|
|
|
const wp<EventThread::Connection>& connection) {
|
2011-12-05 22:33:34 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-04-04 00:48:03 +00:00
|
|
|
mDisplayEventConnections.remove(connection);
|
2011-12-07 01:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::setVsyncRate(uint32_t count,
|
2012-04-04 00:48:03 +00:00
|
|
|
const sp<EventThread::Connection>& connection) {
|
2011-12-07 01:22:19 +00:00
|
|
|
if (int32_t(count) >= 0) { // server must protect against bad params
|
|
|
|
Mutex::Autolock _l(mLock);
|
2012-04-04 00:48:03 +00:00
|
|
|
const int32_t new_count = (count == 0) ? -1 : count;
|
|
|
|
if (connection->count != new_count) {
|
|
|
|
connection->count = new_count;
|
2012-06-15 06:39:35 +00:00
|
|
|
mCondition.broadcast();
|
2011-12-07 01:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::requestNextVsync(
|
2012-04-04 00:48:03 +00:00
|
|
|
const sp<EventThread::Connection>& connection) {
|
2011-12-07 01:22:19 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
2012-04-04 00:48:03 +00:00
|
|
|
if (connection->count < 0) {
|
|
|
|
connection->count = 0;
|
2012-06-15 06:39:35 +00:00
|
|
|
mCondition.broadcast();
|
2011-12-07 01:22:19 +00:00
|
|
|
}
|
2011-12-05 22:33:34 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 04:04:02 +00:00
|
|
|
void EventThread::onScreenReleased() {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
if (!mUseSoftwareVSync) {
|
2012-06-15 06:39:35 +00:00
|
|
|
// disable reliance on h/w vsync
|
|
|
|
mUseSoftwareVSync = true;
|
|
|
|
mCondition.broadcast();
|
2012-04-11 04:04:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::onScreenAcquired() {
|
|
|
|
Mutex::Autolock _l(mLock);
|
2012-06-15 06:39:35 +00:00
|
|
|
if (mUseSoftwareVSync) {
|
|
|
|
// resume use of h/w vsync
|
|
|
|
mUseSoftwareVSync = false;
|
|
|
|
mCondition.broadcast();
|
|
|
|
}
|
2012-04-11 04:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-04 05:09:52 +00:00
|
|
|
void EventThread::onVSyncReceived(int, nsecs_t timestamp) {
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
mVSyncTimestamp = timestamp;
|
2012-06-15 06:39:35 +00:00
|
|
|
mCondition.broadcast();
|
2012-04-04 05:09:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-18 01:49:17 +00:00
|
|
|
bool EventThread::threadLoop() {
|
|
|
|
|
|
|
|
nsecs_t timestamp;
|
2011-12-05 22:33:34 +00:00
|
|
|
DisplayEventReceiver::Event vsync;
|
2012-04-04 00:48:03 +00:00
|
|
|
Vector< wp<EventThread::Connection> > displayEventConnections;
|
2011-12-05 22:33:34 +00:00
|
|
|
|
2012-04-04 05:09:52 +00:00
|
|
|
do {
|
2011-12-05 22:33:34 +00:00
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
do {
|
2012-04-08 22:01:31 +00:00
|
|
|
// latch VSYNC event if any
|
|
|
|
timestamp = mVSyncTimestamp;
|
|
|
|
mVSyncTimestamp = 0;
|
2011-11-18 01:49:17 +00:00
|
|
|
|
2012-04-04 05:09:52 +00:00
|
|
|
// check if we should be waiting for VSYNC events
|
|
|
|
bool waitForNextVsync = false;
|
|
|
|
size_t count = mDisplayEventConnections.size();
|
2012-01-10 02:19:18 +00:00
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
2012-04-04 00:48:03 +00:00
|
|
|
sp<Connection> connection =
|
|
|
|
mDisplayEventConnections.itemAt(i).promote();
|
2012-04-04 05:09:52 +00:00
|
|
|
if (connection!=0 && connection->count >= 0) {
|
|
|
|
// at least one continuous mode or active one-shot event
|
|
|
|
waitForNextVsync = true;
|
|
|
|
break;
|
2012-01-10 02:19:18 +00:00
|
|
|
}
|
2012-04-04 05:09:52 +00:00
|
|
|
}
|
|
|
|
|
2012-04-08 22:01:31 +00:00
|
|
|
if (timestamp) {
|
|
|
|
if (!waitForNextVsync) {
|
|
|
|
// we received a VSYNC but we have no clients
|
|
|
|
// don't report it, and disable VSYNC events
|
2012-04-11 04:04:02 +00:00
|
|
|
disableVSyncLocked();
|
2012-04-08 22:01:31 +00:00
|
|
|
} else {
|
|
|
|
// report VSYNC event
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// never disable VSYNC events immediately, instead
|
|
|
|
// we'll wait to receive the event and we'll
|
|
|
|
// reevaluate whether we need to dispatch it and/or
|
|
|
|
// disable VSYNC events then.
|
|
|
|
if (waitForNextVsync) {
|
|
|
|
// enable
|
2012-04-11 04:04:02 +00:00
|
|
|
enableVSyncLocked();
|
2012-04-08 22:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-04 05:09:52 +00:00
|
|
|
|
|
|
|
// wait for something to happen
|
2012-04-13 03:43:16 +00:00
|
|
|
if (mUseSoftwareVSync && waitForNextVsync) {
|
2012-04-11 04:04:02 +00:00
|
|
|
// h/w vsync cannot be used (screen is off), so we use
|
|
|
|
// a timeout instead. it doesn't matter how imprecise this
|
|
|
|
// is, we just need to make sure to serve the clients
|
|
|
|
if (mCondition.waitRelative(mLock, ms2ns(16)) == TIMED_OUT) {
|
|
|
|
mVSyncTimestamp = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mCondition.wait(mLock);
|
|
|
|
}
|
2012-04-04 05:09:52 +00:00
|
|
|
} while(true);
|
|
|
|
|
|
|
|
// process vsync event
|
|
|
|
mDeliveredEvents++;
|
|
|
|
mLastVSyncTimestamp = timestamp;
|
|
|
|
|
|
|
|
// now see if we still need to report this VSYNC event
|
|
|
|
const size_t count = mDisplayEventConnections.size();
|
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
|
|
|
bool reportVsync = false;
|
|
|
|
sp<Connection> connection =
|
|
|
|
mDisplayEventConnections.itemAt(i).promote();
|
|
|
|
if (connection == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const int32_t count = connection->count;
|
|
|
|
if (count >= 1) {
|
|
|
|
if (count==1 || (mDeliveredEvents % count) == 0) {
|
|
|
|
// continuous event, and time to report it
|
|
|
|
reportVsync = true;
|
2012-01-13 00:13:54 +00:00
|
|
|
}
|
2012-04-04 05:09:52 +00:00
|
|
|
} else if (count >= -1) {
|
|
|
|
if (count == 0) {
|
|
|
|
// fired this time around
|
|
|
|
reportVsync = true;
|
|
|
|
}
|
|
|
|
connection->count--;
|
|
|
|
}
|
|
|
|
if (reportVsync) {
|
|
|
|
displayEventConnections.add(connection);
|
2012-01-10 02:19:18 +00:00
|
|
|
}
|
2012-04-04 05:09:52 +00:00
|
|
|
}
|
|
|
|
} while (!displayEventConnections.size());
|
2011-11-18 01:49:17 +00:00
|
|
|
|
2012-04-04 05:09:52 +00:00
|
|
|
// dispatch vsync events to listeners...
|
|
|
|
vsync.header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
|
|
|
|
vsync.header.timestamp = timestamp;
|
|
|
|
vsync.vsync.count = mDeliveredEvents;
|
2011-12-05 22:33:34 +00:00
|
|
|
|
|
|
|
const size_t count = displayEventConnections.size();
|
2011-11-18 01:49:17 +00:00
|
|
|
for (size_t i=0 ; i<count ; i++) {
|
2012-04-04 00:48:03 +00:00
|
|
|
sp<Connection> conn(displayEventConnections[i].promote());
|
2011-11-18 01:49:17 +00:00
|
|
|
// make sure the connection didn't die
|
|
|
|
if (conn != NULL) {
|
|
|
|
status_t err = conn->postEvent(vsync);
|
|
|
|
if (err == -EAGAIN || err == -EWOULDBLOCK) {
|
|
|
|
// The destination doesn't accept events anymore, it's probably
|
|
|
|
// full. For now, we just drop the events on the floor.
|
|
|
|
// Note that some events cannot be dropped and would have to be
|
|
|
|
// re-sent later. Right-now we don't have the ability to do
|
|
|
|
// this, but it doesn't matter for VSYNC.
|
|
|
|
} else if (err < 0) {
|
|
|
|
// handle any other error on the pipe as fatal. the only
|
|
|
|
// reasonable thing to do is to clean-up this connection.
|
|
|
|
// The most common error we'll get here is -EPIPE.
|
2012-01-13 00:13:54 +00:00
|
|
|
removeDisplayEventConnection(displayEventConnections[i]);
|
2011-11-18 01:49:17 +00:00
|
|
|
}
|
2011-12-05 22:33:34 +00:00
|
|
|
} else {
|
|
|
|
// somehow the connection is dead, but we still have it in our list
|
|
|
|
// just clean the list.
|
2012-01-13 00:13:54 +00:00
|
|
|
removeDisplayEventConnection(displayEventConnections[i]);
|
2011-11-18 01:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 22:33:34 +00:00
|
|
|
// clear all our references without holding mLock
|
|
|
|
displayEventConnections.clear();
|
|
|
|
|
2011-11-18 01:49:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-11 04:04:02 +00:00
|
|
|
void EventThread::enableVSyncLocked() {
|
|
|
|
if (!mUseSoftwareVSync) {
|
|
|
|
// never enable h/w VSYNC when screen is off
|
2012-07-25 05:46:10 +00:00
|
|
|
mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, true);
|
|
|
|
mPowerHAL.vsyncHint(true);
|
2012-04-11 04:04:02 +00:00
|
|
|
}
|
2012-04-11 01:25:31 +00:00
|
|
|
mDebugVsyncEnabled = true;
|
|
|
|
}
|
|
|
|
|
2012-04-11 04:04:02 +00:00
|
|
|
void EventThread::disableVSyncLocked() {
|
2012-07-25 05:46:10 +00:00
|
|
|
mFlinger->eventControl(SurfaceFlinger::EVENT_VSYNC, false);
|
|
|
|
mPowerHAL.vsyncHint(false);
|
2012-04-11 01:25:31 +00:00
|
|
|
mDebugVsyncEnabled = false;
|
|
|
|
}
|
|
|
|
|
2011-11-18 01:49:17 +00:00
|
|
|
status_t EventThread::readyToRun() {
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("EventThread ready to run.");
|
2011-11-18 01:49:17 +00:00
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::dump(String8& result, char* buffer, size_t SIZE) const {
|
|
|
|
Mutex::Autolock _l(mLock);
|
2012-04-11 01:25:31 +00:00
|
|
|
result.appendFormat("VSYNC state: %s\n",
|
|
|
|
mDebugVsyncEnabled?"enabled":"disabled");
|
2012-04-11 04:04:02 +00:00
|
|
|
result.appendFormat(" soft-vsync: %s\n",
|
|
|
|
mUseSoftwareVSync?"enabled":"disabled");
|
2012-04-11 01:25:31 +00:00
|
|
|
result.appendFormat(" numListeners=%u,\n events-delivered: %u\n",
|
2011-11-18 01:49:17 +00:00
|
|
|
mDisplayEventConnections.size(), mDeliveredEvents);
|
2012-04-11 01:25:31 +00:00
|
|
|
for (size_t i=0 ; i<mDisplayEventConnections.size() ; i++) {
|
|
|
|
sp<Connection> connection =
|
|
|
|
mDisplayEventConnections.itemAt(i).promote();
|
|
|
|
result.appendFormat(" %p: count=%d\n",
|
|
|
|
connection.get(), connection!=NULL ? connection->count : 0);
|
|
|
|
}
|
2011-11-18 01:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2012-04-04 00:48:03 +00:00
|
|
|
EventThread::Connection::Connection(
|
|
|
|
const sp<EventThread>& eventThread)
|
|
|
|
: count(-1), mEventThread(eventThread), mChannel(new BitTube())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventThread::Connection::~Connection() {
|
|
|
|
mEventThread->unregisterDisplayEventConnection(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::Connection::onFirstRef() {
|
|
|
|
// NOTE: mEventThread doesn't hold a strong reference on us
|
|
|
|
mEventThread->registerDisplayEventConnection(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
sp<BitTube> EventThread::Connection::getDataChannel() const {
|
|
|
|
return mChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::Connection::setVsyncRate(uint32_t count) {
|
|
|
|
mEventThread->setVsyncRate(count, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventThread::Connection::requestNextVsync() {
|
|
|
|
mEventThread->requestNextVsync(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t EventThread::Connection::postEvent(
|
|
|
|
const DisplayEventReceiver::Event& event) {
|
|
|
|
ssize_t size = DisplayEventReceiver::sendEvents(mChannel, &event, 1);
|
|
|
|
return size < 0 ? status_t(size) : status_t(NO_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2011-11-18 01:49:17 +00:00
|
|
|
}; // namespace android
|