源码分析篇

  本文主要探讨能够触发performTraversals()执行的invalidate()、postInvalidate()和requestLayout()方法的流程。在调用这三个方法到最后执行到performTraversals()方法,涉及到到通过Choroegrapher请求Vsync信号,实现按帧绘制的流程,所以还会介绍Choroegrapher类的工作流程。

一、requestLayout()流程

  invalidate()和postInvalidate()能够触发View的重画,这两个方法最终会调用到performTraversals()中的performDraw()来完成重绘制,但是是否会执行onMeasure()和onLayout()过程要根据标志位的状况来决定;requesetLayout()方法也会调用到performTraversals()方法,但是只会执行measure和layout流程,不会调用到draw流程来触发重画动作。直接来看View.requestLayout()代码。

    @CallSuper
    public void requestLayout() {
        if (mMeasureCache != null) mMeasureCache.clear();
     
     //如果当前的整个View树在进行布局流程的话,则会调用requestLayoutDuringLayout()
//让这次的布局延时执行
if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) { // Only trigger request-during-layout logic if this is the view requesting it, // not the views in its parent hierarchy ViewRootImpl viewRoot = getViewRootImpl(); if (viewRoot != null && viewRoot.isInLayout()) { if (!viewRoot.requestLayoutDuringLayout(this)) { return; } } mAttachInfo.mViewRequestingLayout = this; }   
     //PFLAG_FORCE_LAYOUT会在执行View的measure()和layout()方法时判断
//只有设置过该标志位,才会执行measure()和layout()流程 mPrivateFlags
|= PFLAG_FORCE_LAYOUT; mPrivateFlags |= PFLAG_INVALIDATED; if (mParent != null && !mParent.isLayoutRequested()) { mParent.requestLayout(); } if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) { mAttachInfo.mViewRequestingLayout = null; } }

  该方法主要是设置了PFLAG_FORCE_LAYOUT和PFLAG_INVALIDATED到当前View的Flag中,然后调用到当前View(当前View可能是一个控件View,也可能是一个布局View,因为对于这两类View都能调用requestLayout()方法)的父布局View的requestLayout()方法,父布局View是ViewGroup类型,没有重写该requestLayout()方法,所以实际还是调回到View.requestLayout()方法的这套逻辑。这个过程,就是设置当前View标志位后,就不断的向上调用父布局View的requestLayout(),最后调用到根View即DecorView的requestLayout(),而DecorView的mParent变量指向的是当前窗口对应的ViewRootImpl对象,最后一次设置完DecorView标志位后,调用到ViewRootImpl.requestLayout()方法,进入该代码。

    @Override
    public void requestLayout() {
     //该boolean变量会在ViewRootImpl.performLayout()开始时置为ture,结束置false
//表示当前不处于Layout过程
     if (!mHandlingLayoutInLayoutRequest) { checkThread(); mLayoutRequested = true; scheduleTraversals(); } }

  如果当前不是正在执行layout过程,则会调用scheduleTraversals()方法,进入ViewRootImpl.scheduleTraversals()。

    void scheduleTraversals() {
        if (!mTraversalScheduled) {
       //在下一段代码处会置回false
       //表示在排好这次绘制请求前,不再排其它的绘制请求 mTraversalScheduled
= true; mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); mChoreographer.postCallback( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); if (!mUnbufferedInputDispatch) { scheduleConsumeBatchedInput(); } notifyRendererOfFramePending(); pokeDrawLockIfNeeded(); } }

  这里主要是调用到了ViewRootImpl的另一个重要的变量mChoreographer,它是Choreographer类型的,这个对象会请求Vsync信号来控制绘制的进行,实现了按帧进行绘制的机制,这个类会在后文进行介绍。该方法对于绘制的请求经过了Choreographer的编排后,最终会调用回ViewRootImpl.doTraversal()方法。

    void doTraversal() {
        if (mTraversalScheduled) {
            mTraversalScheduled = false;
            mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
        
       ... //用于调试相关代码
performTraversals(); ... //用于调试相关代码 } }

  然后调用到ViewRootImpl.performTraversals()方法。

二、invalidate()与postInvalidate()流程 

  invalidate()与postInvalidate()都是用于被调用来触发View的更新(重画)动作,区别在于invalidate()方法是在UI线程自身中使用,而postInvalidate()是非UI线程中使用。 首先来看View.postInvalidate()。

  public void postInvalidate() {
        postInvalidateDelayed(0);
    }
 
    public void postInvalidateDelayed(long delayMilliseconds) {
        // We try only with the AttachInfo because there's no point in invalidating
        // if we are not attached to our window
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            attachInfo.mViewRootImpl.dispatchInvalidateDelayed(this, delayMilliseconds);
        }
    }

  调用到了对应的ViewRootImpl对象的dispatchInvalidateDelayed()方法,进入该代码。

    public void dispatchInvalidateDelayed(View view, long delayMilliseconds) {
        Message msg = mHandler.obtainMessage(MSG_INVALIDATE, view);
        mHandler.sendMessageDelayed(msg, delayMilliseconds);
    }

  这里实现了一个消息机制,发送了MSG_INVSLIDSTE。进入处理消息的ViewRootImpl.handleMessage()方法。

       @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_INVALIDATE:
                ((View) msg.obj).invalidate();
                break;
             ...
    }       

  这里实际上就是调回了调用postInvalidate()方法的View的invalidate()方法。由于invalidate()方法只能在UI线程执行,所以postInvalidate只是实现了一个消息机制,让用户能够在非UI线程使用,最终还是调用到invalidate()方法来触发重画,实现界面更新动作。继续来看View.invalidate()方法,该方法逻辑的实际实际上时调用到invalidateInternal()方法来实现的。

   public void invalidate() {
        invalidate(true);
    }

    void invalidate(boolean invalidateCache) {
     //mLeft、mRigth、mTop、mBottom记录的是当前View边界距离其父布局View边界的距离 invalidateInternal(
0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true); } void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache, boolean fullInvalidate) { if (mGhostView != null) { mGhostView.invalidate(true); return; }
     //如果当前视图为不可见状态且没有动画正在执行,且其父布局也没有过渡动画执行,则跳过
if (skipInvalidate()) { return; }
     //当前View没有正在执行该方法
     //或绘制缓存可用或未重绘过或透明度发生改变
     //PFLAG_DRAWN会在该方法内去改标志位
     //PFLAG_INVALIDATED会在View.draw()方法执行时去掉该标志位

     if ((mPrivateFlags & (PFLAG_DRAWN | PFLAG_HAS_BOUNDS)) == (PFLAG_DRAWN | PFLAG_HAS_BOUNDS) || (invalidateCache && (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) || (mPrivateFlags & PFLAG_INVALIDATED) != PFLAG_INVALIDATED || (fullInvalidate && isOpaque() != mLastIsOpaque)) {
       //如果需要全部重绘,invalidate()未传参调用时默认为true
       if (fullInvalidate) { mLastIsOpaque = isOpaque(); mPrivateFlags &= ~PFLAG_DRAWN; } mPrivateFlags |= PFLAG_DIRTY; if (invalidateCache) { mPrivateFlags |= PFLAG_INVALIDATED; mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; } // Propagate the damage rectangle to the parent view.
       //damage记录的区域是需要更新的dirty区域,当前的坐标时相对于自身来设置的
       //通过不断调用到父类的invalidateChild()方法,来不断更新dirty区域的相对坐标
       final AttachInfo ai = mAttachInfo; final ViewParent p = mParent; if (p != null && ai != null && l < r && t < b) { final Rect damage = ai.mTmpInvalRect; damage.set(l, t, r, b); p.invalidateChild(this, damage); } // Damage the entire projection receiver, if necessary. if (mBackground != null && mBackground.isProjected()) { final View receiver = getProjectionReceiver(); if (receiver != null) { receiver.damageInParent(); } } // Damage the entire IsolatedZVolume receiving this view's shadow. if (isHardwareAccelerated() && getZ() != 0) { damageShadowReceiver(); } } }

  这里会通过调用mParent的invalidateChild()方法,来触发父类对于dirty区域的调整(可能会调整可能还是原区域)及改区域相对坐标的调整。进入ViewGroup.invalidateChild()方法。

@Override
    public final void invalidateChild(View child, final Rect dirty) {
        ViewParent parent = this;

        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            // If the child is drawing an animation, we want to copy this flag onto
            // ourselves and the parent to make sure the invalidate request goes
            // through
       //drawAnimation记录调用该方法的子View是否正在执行动画 final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) == PFLAG_DRAW_ANIMATION; // Check whether the child that requests the invalidate is fully opaque // Views being animated or transformed are not considered opaque because we may // be invalidating their old position and need the parent to paint behind them.
       //调用该方法的子View是否不透明:处于不透明状态且没有在执行动画且变化矩阵没有变化
       //Matrix可以用于View的平移、缩放、扩放、旋转等操作,比如某些应用上的双指缩放功能
Matrix childMatrix = child.getMatrix(); final boolean isOpaque = child.isOpaque() && !drawAnimation && child.getAnimation() == null && childMatrix.isIdentity(); // Mark the child as dirty, using the appropriate flag // Make sure we do not set both flags at the same time int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY; if (child.mLayerType != LAYER_TYPE_NONE) { mPrivateFlags |= PFLAG_INVALIDATED; mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; }
       final int[] location = attachInfo.mInvalidateChildLocation;
//记录子View边界距离父View左边界和上边界的距离到Location中,用于下一段代码中的计算 location[CHILD_LEFT_INDEX]
= child.mLeft; location[CHILD_TOP_INDEX] = child.mTop;
       //如果子View设置了变换矩阵,则根据变换矩阵调整dirty区域
if (!childMatrix.isIdentity() || (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { RectF boundingRect = attachInfo.mTmpTransformRect; boundingRect.set(dirty); Matrix transformMatrix; if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { Transformation t = attachInfo.mTmpTransformation; boolean transformed = getChildStaticTransformation(child, t); if (transformed) { transformMatrix = attachInfo.mTmpMatrix; transformMatrix.set(t.getMatrix()); if (!childMatrix.isIdentity()) { transformMatrix.preConcat(childMatrix); } } else { transformMatrix = childMatrix; } } else { transformMatrix = childMatrix; } transformMatrix.mapRect(boundingRect); dirty.set((int) Math.floor(boundingRect.left), (int) Math.floor(boundingRect.top), (int) Math.ceil(boundingRect.right), (int) Math.ceil(boundingRect.bottom)); }
       //这是一个从当前的布局View向上不断遍历当前布局View的父布局,最后遍历到ViewRootImpl的循环
do { View view = null;
          //parent可能为ViewGroup类型,也可能为ViewRootImpl类型
//最后一次循环执行时为ViewRootImpl类型
if (parent instanceof View) { view = (View) parent; }
          //如果子View正在执行动画,设置遍历的父布局View的动画标识
if (drawAnimation) { if (view != null) { view.mPrivateFlags |= PFLAG_DRAW_ANIMATION; } else if (parent instanceof ViewRootImpl) { ((ViewRootImpl) parent).mIsAnimating = true; } } // If the parent is dirty opaque or not dirty, mark it dirty with the opaque // flag coming from the child that initiated the invalidate
         
//设置当前ViewGroup的Dirty标识,表示当前的ViewGroup需要重绘 if (view != null) { if ((view.mViewFlags & FADING_EDGE_MASK) != 0 && view.getSolidColor() == 0) { opaqueFlag = PFLAG_DIRTY; } if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) { view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag; } }
          //调用当前布局View的invalidateChildParent()方法,返回的值为当前布局View的父布局
          //通过循环向上调用,最后返回的根布局是ViewRootImpl对象
          parent
= parent.invalidateChildInParent(location, dirty); if (view != null) { // Account for transform on current parent Matrix m = view.getMatrix(); if (!m.isIdentity()) { RectF boundingRect = attachInfo.mTmpTransformRect; boundingRect.set(dirty); m.mapRect(boundingRect); dirty.set((int) Math.floor(boundingRect.left), (int) Math.floor(boundingRect.top), (int) Math.ceil(boundingRect.right), (int) Math.ceil(boundingRect.bottom)); } } } while (parent != null); } }

  在do-while循环中会调用到parent = parent.invalidateChildInParent(location, dirty),这里执行到ViewGroup.invalidateChildInParent()方法。

@Override
    public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
     //
if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) {
        //如果ViewGroup有没有动画执行或者动画已经完成
if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) != FLAG_OPTIMIZE_INVALIDATE) {
          //dirty记录的是最开始调到invalidate()的View的区域
//dirty的四个坐标值值在执行下面代码是相对于当前循环到上一个ViewGroup来确定的
          //这里做了一个偏移动作,偏移的量是当前上一个ViewGroup相对于现在ViewGroup的偏移值
          //做完下面的偏移操作后,dirty的四个坐标就是想对于当前ViewGroup的坐标值了 dirty.offset([CHILD_LEFT_INDEX]
- mScrollX, location[CHILD_TOP_INDEX] - mScrollY);
          //如果当前ViewGroup需要裁剪View
          //则将当前ViewGroup的区域与View的区域做求并集的操作
if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { dirty.union(0, 0, mRight - mLeft, mBottom - mTop); } final int left = mLeft; final int top = mTop;
          //如果当前ViewGroup需要裁剪View,且ViewGroup区域与View区域没有并集,则dirty置空
if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) { dirty.setEmpty(); } } mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID;     
          //用于循环到下一个ViewGroup时做offset操作 location[CHILD_LEFT_INDEX]
= left; location[CHILD_TOP_INDEX] = top; if (mLayerType != LAYER_TYPE_NONE) { mPrivateFlags |= PFLAG_INVALIDATED; } return mParent; } else {//如果当前ViewGroup中有动画要执行 mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID; location[CHILD_LEFT_INDEX] = mLeft; location[CHILD_TOP_INDEX] = mTop;
          //如果需要对子View裁剪则设置dirty为当前ViewGroup区域
//如果不需要则求当前ViewGroup区域与原ditry区域并集
          if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { dirty.set(0, 0, mRight - mLeft, mBottom - mTop); } else { // in case the dirty rect extends outside the bounds of this container dirty.union(0, 0, mRight - mLeft, mBottom - mTop); } if (mLayerType != LAYER_TYPE_NONE) { mPrivateFlags |= PFLAG_INVALIDATED; } return mParent; } } return null; }

  invalidateChildInParent()主要是完成了dirty区域在调用该方法的ViewGroup中的更新,dirty指示的区域就是需要重绘制的区域。如果ViewGroup没有动画在执行,则dirty区域还是原来的区域,只需要通过偏移操作更改该区域的坐标值从相对于上一个ViewGroup(父ViewGroup),到相对于当前ViewGroup;如果有动画要执行,则表示当前整个ViewGroup都需要重绘,更改dirty值为当前ViewGroup 区域。

  do-while最后一次循环最后会调用到ViewRootImpl.invalidateChildInParent()方法,进入该代码。

    @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        checkThread();
        if (DEBUG_DRAW) Log.v(mTag, "Invalidate child: " + dirty);

     //如果传入一个null drity,则表示要重绘当前ViewRootImpl指示的整个区域
     //如果传入一个empty dirty,则表示经过计算需要重绘的区域不需要绘制
     if (dirty == null) { invalidate(); return null; } else if (dirty.isEmpty() && !mIsAnimating) { return null; }
     ...
invalidateRectOnScreen(dirty); return null; }

  调用到了ViewRootImpl.invalidateRectOnScreen()方法,进入该代码。

    private void invalidateRectOnScreen(Rect dirty) {
     //mDirty记录的是当前ViewRootImpl里还未进行重绘需要重绘的区域
//mDirty会在ViewRootImpl.draw()方法结尾处设置为empty
     final Rect localDirty = mDirty; if (!localDirty.isEmpty() && !localDirty.contains(dirty)) { mAttachInfo.mSetIgnoreDirtyState = true; mAttachInfo.mIgnoreDirtyState = true; } // Add the new dirty rect to the current one
//当前已有的dirty区域与此次dirty区域做并集 localDirty.union(dirty.left, dirty.top, dirty.right, dirty.bottom); // Intersect with the bounds of the window to skip // updates that lie outside of the visible region final float appScale = mAttachInfo.mApplicationScale;
     //处理窗口缩放与做完并集的localDirty做交集
final boolean intersected = localDirty.intersect(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f)); //如果没有交集
     
if (!intersected) { localDirty.setEmpty(); }

     //mWillDrawSoon在performTraversals()方法开始时置为true,结束时置false
     //如果没有在执行performTraversals &&(intersected || 正在执行动画)
if (!mWillDrawSoon && (intersected || mIsAnimating)) { scheduleTraversals(); } }

  最后会调用到scheduleTraversals()方法,后续在请求到Vsync信号后,便会调用到peformTraversals()方法。

三、Choreographer类分析

  “编舞类”Choreoprapher的作用是编排输入事件、动画事件和绘制事件的执行,通过调用Choreoprapher.postCallback()方法,向Choreoprapher加入需要编排的事件,而Choreoprapher则通过请求Vsync信号,来控制这些事件按照屏幕刷新周期有规律的执行,即是实现了按帧绘制的机制。

  在ViewRootImpl中,会调用mChoreographer = Choreographer.getInstance()来初始化一个Choreographer变量。进入Choreographer.getInstance()代码。

   private static final ThreadLocal<Choreographer> sThreadInstance =
            new ThreadLocal<Choreographer>() {
        @Override
        protected Choreographer initialValue() {
            Looper looper = Looper.myLooper();
            if (looper == null) {
                throw new IllegalStateException("The current thread must have a looper!");
            }
            return new Choreographer(looper);
        }
    };

    public static Choreographer getInstance() {
        return sThreadInstance.get();
    }

  这里实际调用了ThreadLocal类型的静态常量的get()方法,ThreadLocal中保存的类型是Choreographer类。根据ThreadLocal机制,sThreadInstance.get()方法会调用到上面代码中实现的initialValue()方法,该方法返回一个Choregrapher类型对象,返回的该对象即作为getInstance()方法的返回,也是最后赋值给了ViewRootImpl中的mChoreogropher变量。在initialValue()方法中会new一个Choreographer对象,进入构建方法。

    private Choreographer(Looper looper) {
     //调用该方法的源头是UI线程,所有looper为UI线程的looper
        mLooper = looper;
        mHandler = new FrameHandler(looper);
     //如果系统使用Vsync机制,则创建一个Vsync信号的接收器FrameDisplayEventReceiver类
        mDisplayEventReceiver = USE_VSYNC ? new FrameDisplayEventReceiver(looper) : null;
        mLastFrameTimeNanos = Long.MIN_VALUE;

        mFrameIntervalNanos = (long)(1000000000 / getRefreshRate());

     //创建回调数组,CALLBAKCK_LAST=3,后文详解
        mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1];
        for (int i = 0; i <= CALLBACK_LAST; i++) {
            mCallbackQueues[i] = new CallbackQueue();
        }
    }

  首先来说mCallbackQueues,这是一个长度为4的CallbackQueue类型的数组,即保存了四个回调队列。每个回调队列能够保存多个CallbackRecord,即是回调事件。这四个队列分别保存四类回调事件:Input事件、Animation事件、Draw事件,还有一种是用来解决动画启动问题的事件。在ViewRootImpl.scheduleTraversals()方法中,便会调用相关方法向队列中添加一个Draw事件,并触发后续到请求信号来处理事件的动作。

    void scheduleTraversals() {
         ...

         mChoreographer.postCallback(
                  Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
         ...  
    }

  继续来看Choreographer.postCallback()方法,该方法是调用到postCallbackDelayedInternal()方法来实现主要逻辑。

   public void postCallback(int callbackType, Runnable action, Object token) {
        postCallbackDelayed(callbackType, action, token, 0);
    }

    public void postCallbackDelayed(int callbackType,
            Runnable action, Object token, long delayMillis) {
        ... //异常情况判断
        postCallbackDelayedInternal(callbackType, action, token, delayMillis);
    }

    private void postCallbackDelayedInternal(int callbackType,
            Object action, Object token, long delayMillis) {
        ... // Debug log

        synchronized (mLock) {
            final long now = SystemClock.uptimeMillis();
            final long dueTime = now + delayMillis;
       //将此次回调事件添加到对应类型的事件队列
            mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);

            if (dueTime <= now) {
          //立刻安排执行
                scheduleFrameLocked(now);
            } else {
          //延时处理,还是会调用到scheduleFrameLocked()方法
                Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
                msg.arg1 = callbackType;
                msg.setAsynchronous(true);
                mHandler.sendMessageAtTime(msg, dueTime);
            }
        }
    }

  调用addCallbackLock()方法,会根据本次事件信息生成一个CallbackRecord,添加到队列中,但并不一定添加在队列到尾部。队列中所有事件的排列是按照dueTime的值由小到大排列大,即越快要求执行的事件排列得越前,所以在添加事件到队列时会根据dueTime插入到对应的位置。

  插入队列操作完成后,会调用scheduleFrameLoacked()方法。

  private void scheduleFrameLocked(long now) {
        if (!mFrameScheduled) {
            mFrameScheduled = true;
            if (USE_VSYNC) { //如果使用了Vsync机制
                if (DEBUG_FRAMES) {
                    Log.d(TAG, "Scheduling next frame on vsync.");
                }

                // If running on the Looper thread, then schedule the vsync immediately,
                // otherwise post a message to schedule the vsync from the UI thread
                // as soon as possible.
          //如果前线程开启了Looper,则调用scheduleVsyncLocked()请求Vsync信号
                if (isRunningOnLooperThreadLocked()) {
                    scheduleVsyncLocked();
                } else {//如果当前线程未启动Looper
            //则发消息到调用创建Choreographer的线程来请求Vsync信号
                    Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
                    msg.setAsynchronous(true);
                    mHandler.sendMessageAtFrontOfQueue(msg);
                }
            } else {//如果未使用Vsync机制,则手动计算下一次绘制时间,使用延时消息来控制
                final long nextFrameTime = Math.max(
                        mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now);
                if (DEBUG_FRAMES) {
                    Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms.");
                }
                Message msg = mHandler.obtainMessage(MSG_DO_FRAME);
                msg.setAsynchronous(true);
                mHandler.sendMessageAtTime(msg, nextFrameTime);
            }
        }
    }

  一般情况下是实用Vsync机制的,且scheduleFrameLocked()也是被UI线程调用执行的,所以直接调用到Choreographer.scheduleVsyncLocked()方法,进入该代码。

    private void scheduleVsyncLocked() {
        mDisplayEventReceiver.scheduleVsync();
    }

  这里直接调用到mDisplayEventReceiver的scheduleVsync()方法,该变量是FrameDisplayEventReceiver类型的,该类继承自DisplayEventReceiver类。scheduleVsync()相当于发起了一次Vsync请求,这样在请求之后下一个Vsync信号发出时,FrameDisplayEventReceiver类便能接收到这词Vsync信号,会调用到FrameDisplayEventReceiver类的onVsync()方法,在onVsync()方法中会发送消息到UI线程,调用到doFrame()方法,Frame是帧的意思,doFrame则表示这次接收到Vsync信号的这一帧内要做的事,进入FrameDisplayEventReceiver.doFrame()方法(FrameDisplayEventReceiver类时Choreographer内部类),

   void doFrame(long frameTimeNanos, int frame) {
        final long startNanos;
        synchronized (mLock) {
       //该变量会在scheduleFrameLocked()方法开始时设置为true,本方法结束置为false
       //表示有callback事件需要安排执行
if (!mFrameScheduled) { return; // no work to do } if (DEBUG_JANK && mDebugPrintNextFrameTimeDelta) { mDebugPrintNextFrameTimeDelta = false; Log.d(TAG, "Frame time delta: " + ((frameTimeNanos - mLastFrameTimeNanos) * 0.000001f) + " ms"); }
        //frameTimeNanos表示Vsync信号发出的时间或者帧开始的时间
long intendedFrameTimeNanos = frameTimeNanos;
        //当前时间 startNanos
= System.nanoTime(); final long jitterNanos = startNanos - frameTimeNanos;
        //当前时间距离Vsync信号时间超过了屏幕的刷新周期,即一帧16ms的时间
if (jitterNanos >= mFrameIntervalNanos) { final long skippedFrames = jitterNanos / mFrameIntervalNanos;
          //如果超过太多,即跳过了太多帧,则打出Log提示跳过了太多帧,可能是主线程做了太多事了
if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) { Log.i(TAG, "Skipped " + skippedFrames + " frames! " + "The application may be doing too much work on its main thread."); } final long lastFrameOffset = jitterNanos % mFrameIntervalNanos; if (DEBUG_JANK) { Log.d(TAG, "Missed vsync by " + (jitterNanos * 0.000001f) + " ms " + "which is more than the frame interval of " + (mFrameIntervalNanos * 0.000001f) + " ms! " + "Skipping " + skippedFrames + " frames and setting frame " + "time to " + (lastFrameOffset * 0.000001f) + " ms in the past."); } frameTimeNanos = startNanos - lastFrameOffset; }   
        //如果距离最后一帧时间未超过屏幕刷新周期,则重新请求Vsync信号
if (frameTimeNanos < mLastFrameTimeNanos) { if (DEBUG_JANK) { Log.d(TAG, "Frame time appears to be going backwards. May be due to a " + "previously skipped frame. Waiting for next vsync."); } scheduleVsyncLocked(); return; } mFrameInfo.setVsync(intendedFrameTimeNanos, frameTimeNanos); mFrameScheduled = false;
       //设置本次帧的执行时间为最后一次的帧执行时间 mLastFrameTimeNanos
= frameTimeNanos; } try { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Choreographer#doFrame"); AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);
       //依次从队列中取出这四类事件进行执行
       //但不一定都会执行这四类事件,要看队列中是否有post过且符合这一帧执行到条件的事件
mFrameInfo.markInputHandlingStart(); doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos); mFrameInfo.markAnimationsStart(); doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos); mFrameInfo.markPerformTraversalsStart(); doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos); doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos); }
finally { AnimationUtils.unlockAnimationClock(); Trace.traceEnd(Trace.TRACE_TAG_VIEW); } if (DEBUG_FRAMES) { final long endNanos = System.nanoTime(); Log.d(TAG, "Frame " + frame + ": Finished, took " + (endNanos - startNanos) * 0.000001f + " ms, latency " + (startNanos - frameTimeNanos) * 0.000001f + " ms."); } }

  该方法会调用doCallbacks方法来依次执行当前时间对应的四类事件。由于CALLBACK_COMMIT是一种修正属性动画启动事件过长导致掉帧问题的一种机制,并不是真正会执行在主线程的流程,这里不做详解。所以在执行事件时,主要是依次执行了input、animation和traversal事件。我们可以抓一个systrace来直观的了解这个过程,以UC浏览器双指扩放页面的绘制过程中的某一帧为例。

  doFrame()方法中首先执行来input事件的处理,然后后面有个很短的矩形体条,执行的是animation事件;之后便是执行到了traversal事件,在执行traversal流程中执行了draw流程,但并没有执行measure和layout流程,因为本次绘制不需要重新测量和布局;在执行draw流程过程中实际调用到了View的draw()方法。

  继续来看Choroegrapher.doCallbacks()方法的实现。

   void doCallbacks(int callbackType, long frameTimeNanos) {
        CallbackRecord callbacks;
        synchronized (mLock) {
            // We use "now" to determine when callbacks become due because it's possible
            // for earlier processing phases in a frame to post callbacks that should run
            // in a following phase, such as an input event that causes an animation to start.
            final long now = System.nanoTime();
       //根据帧开始的时间,取出当前该类型队列中的一个callback事件 callbacks
= mCallbackQueues[callbackType].extractDueCallbacksLocked( now / TimeUtils.NANOS_PER_MS); if (callbacks == null) { return; } mCallbacksRunning = true; ... //CALLBACK_COMMIT事件的处理try { Trace.traceBegin(Trace.TRACE_TAG_VIEW, CALLBACK_TRACE_TITLES[callbackType]); for (CallbackRecord c = callbacks; c != null; c = c.next) { if (DEBUG_FRAMES) { Log.d(TAG, "RunCallback: type=" + callbackType + ", action=" + c.action + ", token=" + c.token + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime)); } c.run(frameTimeNanos); } } finally { synchronized (mLock) { mCallbacksRunning = false; do { final CallbackRecord next = callbacks.next; recycleCallbackLocked(callbacks); callbacks = next; } while (callbacks != null); } Trace.traceEnd(Trace.TRACE_TAG_VIEW); } }

  首先来看下CallbackQueue.extractDueCallbacksLocked()方法,了解队列取事件执行的机制。

        public CallbackRecord extractDueCallbacksLocked(long now) {
       //返回队列头事件,即要求最快要执行的事件 CallbackRecord callbacks
= mHead; if (callbacks == null || callbacks.dueTime > now) { return null; }
       //把头回调事件后面所有执行时间已经到了事件全部舍弃
       CallbackRecord last
= callbacks; CallbackRecord next = last.next; while (next != null) { if (next.dueTime > now) { last.next = null; break; } last = next; next = next.next; }
       //next表示的是未到执行时间且要求执行到时间最早的事件 mHead
= next; return callbacks; }

  取出当前帧需要执行的回调事件后,便会执行到该事件的run()方法,在使用这里会调用到CallbackRecord的run()方法。

    private static final class CallbackRecord {
        public CallbackRecord next;
        public long dueTime;
        public Object action; // Runnable or FrameCallback
        public Object token;

        public void run(long frameTimeNanos) {
            if (token == FRAME_CALLBACK_TOKEN) {
                ((FrameCallback)action).doFrame(frameTimeNanos);
            } else {
                ((Runnable)action).run();
            }
        }
    }

  回想我们在ViewRootImpl中调用postCallback()方法的三个参数值,第一个事件类型为Choreographer.CALLBACK_TRAVERSAL,表示是绘制事件,用于指示该事件放入对应队列,第二个则是一个TraversalRunnable类型的Runnable,则赋值给了这里的action,第三个是null,所以上面代码的run()方法,实际执行到了TraversalRunnable的run()方法。

    final class TraversalRunnable implements Runnable {
        @Override
        public void run() {
            doTraversal();
        }
    }

  该方法则调用到了doTraversal()方法,后续则调用到了ViewRootImpl.performTraversals()方法。由于run在了UI线程,所以后续到绘制动作也是在UI线程执行到。至此完成了Choroegrapher类的分析。

原文地址:https://www.cnblogs.com/tiger-wang-ms/p/6592189.html