我们来看看其代码的实现.
void SurfaceFlinger::init()
{
…
// start the EventThread
sp<VSyncSource>vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
vsyncPhaseOffsetNs, true,"app");
mEventThread = newEventThread(vsyncSrc);
sp<VSyncSource>sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
sfVsyncPhaseOffsetNs, true, "sf");
mSFEventThread= new EventThread(sfVsyncSrc);
mEventQueue.setEventThread(mSFEventThread);
}
当surfaceFlinger 初始化时,其创建了两个DispSyncSource 和两个EventThread.一个用于APP, 而另一个用于SurfaceFlinger 本身.我们知道DispSyncSource和DispSync协同工作, 共同完成Vsyncevent的fire.而EventThread会triggerApp draw frame和surfaceFlinger合成”dirty”layer当SW-VSYNCevent 产生时.
我们在这里列出DispSyncSource和Vsync-sf 有关系的code.
class DispSyncSource : publicVSyncSource, private DispSync::Callback {
public:
DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
…
virtual void setVSyncEnabled(bool enable) {
Mutex::Autolock lock(mVsyncMutex);
if (enable) {
status_t err =mDispSync->addEventListener(mPhaseOffset,
static_cast<DispSync::Callback*>(this));
….
} else {
status_t err = mDispSync->removeEventListener(
static_cast<DispSync::Callback*>(this));
….
}
virtual void onDispSyncEvent(nsecs_t when) {
sp<VSyncSource::Callback> callback;
{
Mutex::Autolocklock(mCallbackMutex);
callback = mCallback;
if (mTraceVsync) {
mValue = (mValue + 1) % 2;
ATRACE_INT(mVsyncEventLabel.string(), mValue);
}
}
if (callback != NULL) {
callback->onVSyncEvent(when);
}
}
….
}
其中最重要的两个function是setVSyncEnabled(bool enable)和onDispSyncEvent(nsecs_twhen). setVSyncEnabled()的功能是把自己注册到DispSync(enable=true)中或者从DispSync中remove掉自己(enale=false),当DispSync产生了SW-VSYNCevent时,会check对SW-VSYNC感兴趣的DispSyncSource并回调其onDispSyncEvent().onDispSyncEvent()主要的功能一是enableVsync-sf 在sytrace中,一是callback EventThread中的onVSyncEvent(),从而触发SurfaceFlinger 合成所有更新的layer.
下面我们来看看EventThread 的重要code
…
EventThread::Connection::Connection(
constsp<EventThread>& eventThread)
: count(-1),mEventThread(eventThread), mChannel(new BitTube())
{
}
sp<EventThread::Connection> EventThread::createEventConnection()const {
return new Connection(const_cast<EventThread*>(this));
}
status_tEventThread::registerDisplayEventConnection(
constsp<EventThread::Connection>& connection) {
Mutex::Autolock _l(mLock);
mDisplayEventConnections.add(connection);
…
}
void EventThread::requestNextVsync(
…
if (connection->count < 0) {
connection->count = 0;
mCondition.broadcast();
}
}
void EventThread::onVSyncEvent(nsecs_t timestamp){
Mutex::Autolock _l(mLock);
mCondition.broadcast();
}
bool EventThread::threadLoop() {
DisplayEventReceiver::Event event;
Vector< sp<EventThread::Connection> > signalConnections;
signalConnections =waitForEvent(&event);
// dispatch events to listeners...
const size_t count= signalConnections.size();
for (size_t i=0 ; i<count ; i++) {
const sp<Connection>&conn(signalConnections[i]);
// now see if we still need to reportthis event
status_t err = conn->postEvent(event);
…
}
// This will return when (1) avsync event has been received, and (2) there was
// at least one connectioninterested in receiving it when we started waiting.
Vector<sp<EventThread::Connection> > EventThread::waitForEvent(
DisplayEventReceiver::Event* event)
{
Mutex::Autolock _l(mLock);
Vector< sp<EventThread::Connection>> signalConnections;
do {
….
// find out connections waiting forevents
size_t count = mDisplayEventConnections.size();
for (size_t i=0 ; i<count ; i++) {
….
if (connection->count >= 0) {
// we need vsync eventsbecause at least
// one connection iswaiting for it
waitForVSync = true;
if (timestamp) {
if(connection->count == 0) {
// fired this timearound
connection->count = -1;
signalConnections.add(connection);
}
// Here we figure out if we need toenable or disable vsyncs
if (timestamp && !waitForVSync) {
// we received a VSYNC but we haveno clients
// don't report it, and disableVSYNC events
disableVSyncLocked();
} else if (!timestamp &&waitForVSync) {
// we have at least one client, sowe want vsync enabled
// (TODO: this function is calledright after we finish
// notifying clients of a vsync, sothis call will be made
// at the vsync rate, e.g.60fps. If we can accurately
// track the currentstate we could avoid making this call
// so often.)
enableVSyncLocked();
}
if (!timestamp &&!eventPending) {
// wait for something to happen
if (waitForVSync) {
…
if (mCondition.waitRelative(mLock, timeout) ==TIMED_OUT) {
…..
}
} else {
// Nobody is interested invsync, so we just want to sleep.
mCondition.wait(mLock);
}
}
} while (signalConnections.isEmpty());
…
return signalConnections;
}
我们列出了EventThread 的重要function.下面一一说明其功能.
下图是big pic.