在SurfaceFlinger的init方法中,构造了2个EventThread类型的线程,
mEventThread = new EventThread(vsyncSrc, *this, false);
sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
sfVsyncPhaseOffsetNs, true, "sf");
mSFEventThread = new EventThread(sfVsyncSrc, *this, true);
mEventQueue.setEventThread(mSFEventThread);
EventThread也是一个线程类,继承于Thread,定义如下,
class EventThread : public Thread, private VSyncSource::Callback {
EventThread的构造方法没什么可论述的, onFirstRef方法如下,
void EventThread::onFirstRef() {
run("EventThread", PRIORITY_URGENT_DISPLAY + PRIORITY_MORE_FAVORABLE);
}
和一般C/C++ 线程一样,调用线程的run方法之后,会调用到threadLoop方法,该方法主要逻辑如下,
1,调用waitForEvent方法进入等待,
DisplayEventReceiver::Event event;
Vector< sp<EventThread::Connection> > signalConnections;
signalConnections = waitForEvent(&event);
2,调用Connection的postEvent方法,将vsync信号分发给监听者,
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 report this event
status_t err = conn->postEvent(event);
3,最后返回true,系统会再次调用这个线程的threadloop,从而形成一个循环,
return true;
在论述其他方法之前,先看下内部类Connection,其定义如下,
class Connection : public BnDisplayEventConnection {
听名字可知, Connection代表了SurfaceFlinger 进程和客户端的通道,主要用于SurfaceFlinger 进程 向 客户端分发vsync信号。也就是说,客户端注册一个监听器,就会创建一个Connection对象。例如, SurfaceFlinger。在SurfaceFlinger的init方法中,
mSFEventThread = new EventThread(sfVsyncSrc, *this, true);
mEventQueue.setEventThread(mSFEventThread);
创建vsync线程之后,就调