【Android】消息队列之异步消息 |
您所在的位置:网站首页 › 甘雨暴击率和暴击伤害 › 【Android】消息队列之异步消息 |
异步消息可以理解为是用来提升消息的重要性,在有异步消息时,会优先处理距当前时间最近的异步消息,同步消息都不会被处理,绘制界面就是一个异步消息; 同步消息:没有设置为异步的消息都是同步消息; Message提供setAsynchronous用来设置异步消息; 一、异步消息的使用流程1.使用MessageQueue的postSyncBarrier() 来加入同步屏障;同步屏障其实就是一个handler为空的消息,它也存放在消息队列中; 2.往消息队列中加入异步消息,跟平时创建消息没有什么不同,只是额外调用setAsynchronous(true)来设置异步消息; 3.线程执行过程中,是怎么取消息的,异步消息为什么会优先于同步消息;【介绍下原理,可以不关注】 Looper的loop会一直循环取消息进行处理,我们主要关注的是MessageQueue的next方法,它在消息队列头部是屏障消息的时候,会在它后面找最近的异步消息,如果时间合适的话就返回这个消息,如果时间不ok就休眠等待; 这里有个隐含意思:同步屏障消息在队列头部才有效,才需要处理异步消息,如果同步屏障消息不在头部的话,不影响它前面的同步消息的处理; 可以看下next关键代码: Message msg = mMessages; //target为空表示是同步屏障消息 if (msg != null && msg.target == null) { // Stalled by a barrier. Find the next asynchronous message in the queue. do { prevMsg = msg; msg = msg.next; } while (msg != null && !msg.isAsynchronous()); }4.异步消息执行时,使用MessageQueue的removeSyncBarrier来删除同步屏障; 二、绘制消息的处理过程1.加入同步屏障; 我们都知道绘制消息都是在ViewRootImpl中处理的,这里我们只看scheduleTraversals方法; void scheduleTraversals() { if (!mTraversalScheduled) { mTraversalScheduled = true; //添加同步屏障 mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); //设置绘制处理函数 mChoreographer.postCallback( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); if (!mUnbufferedInputDispatch) { scheduleConsumeBatchedInput(); } notifyRendererOfFramePending(); pokeDrawLockIfNeeded(); } }2.设置异步消息; 硬件底层向应用层发出垂直同步信号后,会回调Choreographer内部类FrameDisplayEventReceiver的onVsync方法,其内部主要是向消息队列中放入异步消息;异步消息执行时会调用FrameDisplayEventReceiver的run方法来处理; private final class FrameDisplayEventReceiver extends DisplayEventReceiver implements Runnable { private boolean mHavePendingVsync; private long mTimestampNanos; private int mFrame; public FrameDisplayEventReceiver(Looper looper, int vsyncSource) { super(looper, vsyncSource); } // TODO(b/116025192): physicalDisplayId is ignored because SF only emits VSYNC events for // the internal display and DisplayEventReceiver#scheduleVsync only allows requesting VSYNC // for the internal display implicitly. @Override public void onVsync(long timestampNanos, long physicalDisplayId, int frame) { // Post the vsync event to the Handler. // The idea is to prevent incoming vsync events from completely starving // the message queue. If there are no messages in the queue with timestamps // earlier than the frame time, then the vsync event will be processed immediately. // Otherwise, messages that predate the vsync event will be handled first. long now = System.nanoTime(); if (timestampNanos > now) { Log.w(TAG, "Frame time is " + ((timestampNanos - now) * 0.000001f) + " ms in the future! Check that graphics HAL is generating vsync " + "timestamps using the correct timebase."); timestampNanos = now; } if (mHavePendingVsync) { Log.w(TAG, "Already have a pending vsync event. There should only be " + "one at a time."); } else { mHavePendingVsync = true; } mTimestampNanos = timestampNanos; mFrame = frame; Message msg = Message.obtain(mHandler, this); msg.setAsynchronous(true); mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS); } @Override public void run() { mHavePendingVsync = false; doFrame(mTimestampNanos, mFrame); } }3.删除同步屏障; 第2步中的doFrame会执行Choreographer里设置的所有回调消息,包括第1步中设置的Choreographer.CALLBACK_TRAVERSAL消息; 这会执行到ViewRootImpl的TraversalRunnable,TraversalRunnable主要就是调用doTraversal方法; void doTraversal() { if (mTraversalScheduled) { mTraversalScheduled = false; //删除同步屏障 mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); if (mProfile) { Debug.startMethodTracing("ViewAncestor"); } performTraversals(); if (mProfile) { Debug.stopMethodTracing(); mProfile = false; } } }
|
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |