Android P 显示流程分析(三)---EventThread MessageQueue 交互分析

2024-04-20 06:32

本文主要是介绍Android P 显示流程分析(三)---EventThread MessageQueue 交互分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上篇分析SurfaceFlinger的init()里创建了几个线程,主要用于界面刷新。里面涉及了一个EventThread和MessageQueue。我们来看看像界面刷新这种高频的事件通知及处理,Google是如何设计的。

EventThread的初始化

EventThread::EventThread(VSyncSource* src, ResyncWithRateLimitCallback resyncWithRateLimitCallback,InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName){//创建一个线程,实际做事的也是这个线程处理的mThread = std::thread(&EventThread::threadMain, this);...                    
}                         

创建connection

其它一些线程需要将事件推给EventThread去处理,需要中间有一个连接者与EventThread交互,EventThread里面就创建出来了一个内部类Connection,负责连接交互的工作

sp<BnDisplayEventConnection> EventThread::createEventConnection() const {return new Connection(const_cast<EventThread*>(this));
}
class Connection : public BnDisplayEventConnection {
public:virtual status_t postEvent(const DisplayEventReceiver::Event& event);
private:void requestNextVsync() override; 
}

上篇SurfaceFlinger.init()中实例化一个EventThread之后,就调用了mEventQueue->setEventThread(mSFEventThread.get()),

void MessageQueue::setEventThread(android::EventThread* eventThread) {
...mEventThread = eventThread;mEvents = eventThread->createEventConnection();
...
}
sp<BnDisplayEventConnection> EventThread::createEventConnection() const {return new Connection(const_cast<EventThread*>(this));
}    

MessageQueue中的mEvents就是一个connection对象。就是它就将MessageQueue与EventThread连接起来了。
我们再来看看EventHandler 的那个线程启动之后,做了哪些事情:

void EventThread::threadMain() NO_THREAD_SAFETY_ANALYSIS {std::unique_lock<std::mutex> lock(mMutex);while (mKeepRunning) {DisplayEventReceiver::Event event;Vector<sp<EventThread::Connection> > signalConnections;signalConnections = waitForEventLocked(&lock, &event);// dispatch events to listeners...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 eventstatus_t err = conn->postEvent(event);if (err == -EAGAIN || err == -EWOULDBLOCK) {ALOGW("EventThread: dropping event (%08x) for connection %p", event.header.type,conn.get());} else if (err < 0) {removeDisplayEventConnectionLocked(signalConnections[i]);}}}
}

总体来说是获取事件,将获取到的事件放到BitTube中去,具体的获取事件的waitForEventLocked是如何做的呢?

Vector<sp<EventThread::Connection> > EventThread::waitForEventLocked(std::unique_lock<std::mutex>* lock, DisplayEventReceiver::Event* event) {Vector<sp<EventThread::Connection> > signalConnections;while (signalConnections.isEmpty() && mKeepRunning) {for (int32_t i = 0; i < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES; i++) {timestamp = mVSyncEvent[i].header.timestamp;if (timestamp) {// we have a vsync event to dispatchif (mInterceptVSyncsCallback) {mInterceptVSyncsCallback(timestamp);}*event = mVSyncEvent[i];mVSyncEvent[i].header.timestamp = 0;vsyncCount = mVSyncEvent[i].vsync.count;break;}} if (!timestamp) {// no vsync event, see if there are some other eventeventPending = !mPendingEvents.isEmpty();if (eventPending) {// we have some other event to dispatch*event = mPendingEvents[0];mPendingEvents.removeAt(0);}}        // find out connections waiting for eventssize_t count = mDisplayEventConnections.size();for (size_t i = 0; i < count;) {sp<Connection> connection(mDisplayEventConnections[i].promote()); if (connection != nullptr) {bool added = false;if (connection->count >= 0) {// we need vsync events because at least// one connection is waiting for itwaitForVSync = true;if (timestamp) {// we consume the event only if it's time// (ie: we received a vsync event)if (connection->count == 0) {// fired this time aroundconnection->count = -1;signalConnections.add(connection);added = true;} else if (connection->count == 1 ||(vsyncCount % connection->count) == 0) {// continuous event, and time to report itsignalConnections.add(connection);added = true;}}}if (eventPending && !timestamp && !added) {// we don't have a vsync event to process// (timestamp==0), but we have some pending// messages.signalConnections.add(connection);}++i; } else {// we couldn't promote this reference, the connection has// died, so clean-up!mDisplayEventConnections.removeAt(i);--count;}}   ...// note: !timestamp implies signalConnections.isEmpty(), because we// don't populate signalConnections if there's no vsync pendingif (!timestamp && !eventPending) {// wait for something to happenif (waitForVSync) {// This is where we spend most of our time, waiting// for vsync events and new client registrations.//// If the screen is off, we can't use h/w vsync, so we// use a 16ms timeout instead.  It doesn't need to be// precise, we just need to keep feeding our clients.//// We don't want to stall if there's a driver bug, so we// use a (long) timeout when waiting for h/w vsync, and// generate fake events when necessary.bool softwareSync = mUseSoftwareVSync;auto timeout = softwareSync ? 16ms : 1000ms;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 ?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++;}} else {// Nobody is interested in vsync, so we just want to sleep.// h/w vsync should be disabled, so this will wait until we// get a new connection, or an existing connection becomes// interested in receiving vsync again.mCondition.wait(*lock);}}}// here we're guaranteed to have a timestamp and some connections to signal// (The connections might have dropped out of mDisplayEventConnections// while we were asleep, but we'll still have strong references to them.)return signalConnections;
}                     

waitForEventLocked 就如果vsyncEvent和pendingEvent里已经存在事件,就将其取出指定给event,然后遍历出所有的mDisplayEventConnections, 找且需要的connection, 将他们一一添加到signalConnections中, 如果没有找到需要connection, 就设置mCondition.wait(*lock), 条件加锁等待,直到此条件被唤醒。
我们上面EventThread的threadMain分析到将获取的signalConnections中的connection遍历出来,然后将通过调用 connection的postEvent 将event事件加入到BitTube中。

MessageQueue的作用

surfaceFlinger很多事件都是通过MessageQueue来处理的,SurfaceFlinger里的mEventQueue就是MessageQueue的对象指针。

void SurfaceFlinger::waitForEvent() {mEventQueue->waitMessage();
}
void SurfaceFlinger::signalTransaction() {mEventQueue->invalidate();
}
void SurfaceFlinger::signalLayerUpdate() {mEventQueue->invalidate();
}
void SurfaceFlinger::signalRefresh() {mRefreshPending = true;mEventQueue->refresh();
}
void SurfaceFlinger::run() {do {waitForEvent();} while (true);
}

以上这些都是通过MessageQueue去调用实现的。那我们再具体看看MessageQueue里是如何实现的。

void MessageQueue::invalidate() {mEvents->requestNextVsync();
}
void EventThread::Connection::requestNextVsync() {mEventThread->requestNextVsync(this);
}
void EventThread::requestNextVsync(const sp<EventThread::Connection>& connection) {std::lock_guard<std::mutex> lock(mMutex);if (connection->count < 0) {connection->count = 0;mCondition.notify_all();}
}

invalidate 主要是为了唤醒waitForEventLocked , 让EventThread继续执行。

void MessageQueue::refresh() {mHandler->dispatchRefresh();
}
void MessageQueue::Handler::dispatchRefresh() {if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0) {mQueue.mLooper->sendMessage(this, Message(MessageQueue::REFRESH));}
}
void MessageQueue::Handler::handleMessage(const Message& message) {switch (message.what) {...case REFRESH:android_atomic_and(~eventMaskRefresh, &mEventMask);mQueue.mFlinger->onMessageReceived(message.what);break;}
}
void SurfaceFlinger::onMessageReceived(int32_t what) {... case MessageQueue::REFRESH: {handleMessageRefresh();break;}
}
void SurfaceFlinger::handleMessageRefresh() {ATRACE_CALL();mRefreshPending = false;nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);preComposition(refreshStartTime);rebuildLayerStacks();setUpHWComposer();doDebugFlashRegions();doTracing("handleRefresh");logLayerStats();doComposition();postComposition(refreshStartTime);mPreviousPresentFence = getBE().mHwc->getPresentFence(HWC_DISPLAY_PRIMARY);mHadClientComposition = false;for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {const sp<DisplayDevice>& displayDevice = mDisplays[displayId];mHadClientComposition = mHadClientComposition ||getBE().mHwc->hasClientComposition(displayDevice->getHwcDisplayId());}mVsyncModulator.onRefreshed(mHadClientComposition);mLayersWithQueuedFrames.clear();
}    	   

最后调用到了SurfaceFlinger的handleMessageRefresh(), 里面涉及到图层的合成了。
到这里EventThread和MessageQueue就分析完了。

这篇关于Android P 显示流程分析(三)---EventThread MessageQueue 交互分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/919518

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4