surfaceflinger创建Connection对象的过程前面已经论述过,在此就不赘述了。
Surfaceflinger中接收vsync信号的入口为MessageQueue的cb_eventReceiver方法,调用流程图如下,
cb_eventReceiver方法如下,直接调用eventReceiver方法,
MessageQueue* queue = reinterpret_cast(data);
return queue->eventReceiver(fd, events);
eventReceiver方法主要逻辑如下,
1,调用DisplayEventReceiver的getEvents方法循环读取mEventTube对象中的数据,
while ((n = DisplayEventReceiver::getEvents(&mEventTube, buffer, 8)) > 0) {
2,如果是vsync信号,就调用Handler的dispatchInvalidate处理,
if (buffer[i].header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
mHandler->dispatchInvalidate();
break;
}
此处的Handler是MessageQueue.cpp中的一个类,其dispatchInvalidate方法如下,
if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
}
调用主线程的Looper调用sendMessage方法发送INVALIDATE消息,切换到主线程中执行。Handler Looper机制再次不论述了,只需要知道会执行handleMessage方法, Handler的handleMessage方法对INVALIDATE消息处理如下,
case INVALIDATE:
android_atomic_and(~eventMaskInvalidate, &mEventMask);
mQueue.mFlinger->onMessageReceived(message.what);
break;
此处的mFlinger是SurfaceFlinger对象, SurfaceFlinger的onMessageReceived方法对INVALIDATE消息处理如下,
switch (what) {
case MessageQueue::INVALIDATE: {
bool refreshNeeded = handleMessageTransaction();
refreshNeeded |= handleMessageInvalidate();
refreshNeeded |= mRepaintEverything;
if (refreshNeeded) {
•••
signalRefresh();
}
break;
signalRefresh方法后面会详细论述。