Android VSYNC事件传递流程

我一直以为DisplayEventReceiver是跟Input一样,通过BitTube的两个socket进行跨进程的通信,也就是SF-APP之间的通信,但实际上不是的。
实际上的逻辑是,每次VSYNC事件到来从HAL的线程调之后,
01-01 00:35:01.163  2963  3017 D SurfaceFlinger: onVSyncEvent
EventThread会将这个vsync事件记录到自己的mVSyncEvent中,并释放Condition锁。
void EventThread::onVSyncEvent(nsecs_t timestamp) {
    ALOGD("EventThread::onVSyncEvent");
    std::lock_guard lock(mMutex);
    mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
    mVSyncEvent[0].header.id = 0;
    mVSyncEvent[0].header.timestamp = timestamp;
    mVSyncEvent[0].vsync.count++;
    mCondition.notify_all();
}
然后在EventThread一直threadLoop的waitForEvent函数中,此时如果有有效的connection连接的话就会真正拿到这个事件。前面的逻辑是判断mDisplayEventConnections列表里面是不是存在有效的Connection。
if (mCondition.wait_for(*lock, timeout) == std::cv_status::timeout) {
    if (!softwareSync) {
        ALOGW("Timed out waiting for hw vsync; faking it");
    }
    // FIXME: how do we decide which display id the fake
    // vsync came from ?
    ALOGD("EventThread::waitForEventLocked get new vsyncevent and release mCondition")
    mVSyncEvent[0].header.type = DisplayEventReceiver::DISPLAY_EVENT_VSYNC;
    mVSyncEvent[0].header.id = DisplayDevice::DISPLAY_PRIMARY;
    mVSyncEvent[0].header.timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
    mVSyncEvent[0].vsync.count++;
}
由于正常情况下,至少会存在一个MessageQueue setEventThread时创建一个EventConnection。
    mEventThread = eventThread;
    mEvents = eventThread->createEventConnection();
    mEvents->stealReceiveChannel(&mEventTube);
    mLooper->addFd(mEventTube.getFd(), 0, Looper::EVENT_INPUT, MessageQueue::cb_eventReceiver,
                   this);
而应用app侧在创建Choreographer时,会创建FrameDisplayEventReceiver,FrameDisplayEventReceiver继承DisplayEventReceiver。通过jni函数nativeInit继续创建NativeDisplayEventReceiver,并调用其父类DisplayEventDispatcher initialize进行初始化。
    private Choreographer(Looper looper, int vsyncSource) {
        mLooper = looper;
        mHandler = new FrameHandler(looper);
        mDisplayEventReceiver = USE_VSYNC
                ? new FrameDisplayEventReceiver(looper, vsyncSource)
                : null;
                
    sp receiver = new NativeDisplayEventReceiver(env,
            receiverWeak, messageQueue, vsyncSource);
    status_t status = receiver->initialize();
DisplayEventDispatcher构造时会创建成员DisplayEventDispatcher,然后再在initialize里面,将receiveFd也放到应用主线程的Looper中进行监听。
status_t DisplayEventDispatcher::initialize() {
    status_t result = mReceiver.initCheck();
    if (result) {
        ALOGW("Failed to initialize display event receiver, status=%d", result);
        return result;
    }
    ALOGW("DisplayEventDispatcher::initialize addFd %d ", mReceiver.getFd());

    int rc = mLooper->addFd(mReceiver.getFd(), 0, Looper::EVENT_INPUT,
            this, NULL);
    if (rc < 0) {
        return UNKNOWN_ERROR;
    }
    return OK;
}
DisplayEventDispatcher在创建时也会默认通过sf创建一个EventConnection.
DisplayEventReceiver::DisplayEventReceiver(ISurfaceComposer::VsyncSource vsyncSource) {
    sp sf(ComposerService::getComposerService());
    if (sf != NULL) {
        mEventConnection = sf->createDisplayEventConnection(vsyncSource);
        if (mEventConnection != NULL) {
            mDataChannel = std::make_unique();
            ALOGD("DisplayEventReceiver init stealReceiveChannel %d", getFd());
            mEventConnection->stealReceiveChannel(mDataChannel.get());
        }
    }
}
这样再回到EventThread线程,在waitForEvent拿到VSYNC事件之后,就会遍历signalConnections将VSYNC event post过去,这样SF和各个应用的Choreographer都收到了VYSNC事件。并非是之前理解的SF再把这个事件通过一个Bittub传给应用端,所以一直也找不到设置sendFd的地方。
const size_t count = signalConnections.size();
for (size_t i = 0; i < count; i++) {
    const sp& conn(signalConnections[i]);
    ALOGD("EventThread:::threadMain conn->postEvent %p", conn);
    // now see if we still need to report this event
    status_t err = conn->postEvent(event);

你可能感兴趣的:(Graphics)