Android13 EventThread threadMain流程分析

EventThread的threadMain方法无限循环处理pendingEvents,对Vsync类型的Event分发到消费者,通过往消费者的FD写数据,通知APP有Vsync信号到来。pendingEvents中的消息处理完了,分发线程等待mCondition的通知,EventThread的threadMain方法代码如下:

//frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp
std::vector<DisplayEventReceiver::Event> mPendingEvents;
void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
    // consumers 表示即将消费事件的 connection 集合
    DisplayEventConsumers consumers;


    // 状态值不等于 State::Quit 则一直循环遍历,死循环
    while (mState != State::Quit) {
        std::optional<DisplayEventReceiver::Event> event;


        // Determine next event to dispatch.
 //  确定下一个要调度的 Event
        if (!mPendingEvents.empty()) {
            event = mPendingEvents.front(); // 获取头部 Event
            mPendingEvents.pop_front(); // 将头部 Event 弹出


            switch (event->header.type) { // 根据 Event 类型分别对应处理
                case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:  //Event类型为DISPLAY_EVENT_HOTPLUG,即当显示设备(如显示器)被插入或拔出时产生
                    if (event->hotplug.connected && !mVSyncState) {
                        mVSyncState.emplace(event->header.displayId);
                    } else if (!event->hotplug.connected && mVSyncState &&
                               mVSyncState->displayId == event->header.displayId) {
                        mVSyncState.reset();
                    }
                    break;


                case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:  //Event类型为DISPLAY_EVENT_VSYNC,即Vsync( 垂直同步)事件
                    if (mInterceptVSyncsCallback) {
                        mInterceptVSyncsCallback(event->header.timestamp);
                    }
                    break;
            }
        }


        // 标志位:是否有 VSync 请求,默认 false
        bool vsyncRequested = false;


        // Find connections that should consume this event.
 // 循环遍历存储 EventThreadConnection 的 vector 容器 mDisplayEventConnections,查找要消费事件的连接
         // begin()函数用于返回指向向量容器的第一个元素的迭代器
        auto it = mDisplayEventConnections.begin();
        while (it != mDisplayEventConnections.end()) {
            if (const auto c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值