EventThread 分析

本文详细分析了Android系统中EventThread的工作原理,探讨了它如何处理Vsync信号,并通过Connection对象在SurfaceFlinger与客户端之间进行通信。在SurfaceFlinger的初始化过程中,EventThread被创建并启动,通过MessageQueue的setEventThread方法建立与客户端的连接。当Vsync信号到来时,EventThread唤醒并分发信号给监听者,涉及DispSyncSource和DispSyncThread的事件监听与注册机制。同时,文章提到了UI数据合成和应用进程如何接收Vsync信号。
摘要由CSDN通过智能技术生成

在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线程之后,就调

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值