美文网首页Android
Android 浅谈屏幕绘制机制

Android 浅谈屏幕绘制机制

作者: 五月天外飞仙 | 来源:发表于2020-07-29 15:22 被阅读0次

    之前我们说到handler的同步屏障在屏幕刷新机制里面有用到,今天我们就来看看这个屏幕刷新机制

    Android屏幕在很多时候都会进行刷新,来保证使用的流畅度。

    比较常见的就是调用invalidate()方法,但这个invalidate方法是不是立刻会刷新屏幕呢,那又未必。

        void invalidate() {
            mDirty.set(0, 0, mWidth, mHeight);
            if (!mWillDrawSoon) {
                scheduleTraversals();
            }
        }
    

    这里的dirty其实就是需要刷新的区域,不仅是android,在flutter里,需要刷新的view也叫dirty,因为屏幕刷新是局部的,如果每次刷新都要重新绘制整个页面,那开销未免太大,所以这里是用dirty来存储需要刷新的区域。

    接下来就走到了scheduleTraversals()方法,其实任何刷新请求都会走到这个方法,不仅仅是invalidate这个方法。

        void scheduleTraversals() {
            if (!mTraversalScheduled) {  // 注释1
                mTraversalScheduled = true;
                mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();  // 注释2
                mChoreographer.postCallback(
                        Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);  // 注释3
                if (!mUnbufferedInputDispatch) {// 注释1
                    scheduleConsumeBatchedInput();
                }
                notifyRendererOfFramePending();//底层渲染的方法,暂时不讨论这个
                pokeDrawLockIfNeeded();//一个window相关的方法,获取drawLock的,有兴趣的可以自己了解
            }
        }
    

    scheduleTraversals()方法中,需要注意的点我已经写上注释了。

    首先来看注释一:注释1有两处,主要的作用就是防止多次刷新请求。相当于一次Vsnyc信号发出后,在这16ms里面,我只需要刷新一次就行了,其他的请求等到下个16ms再说,这样保证了有序执行。

    mTraversalScheduled在unscheduleTraversals和doTraversal方法中会置为false,意思就是我开始刷新了,你(scheduleTraversals)可以着手计算下一个16ms需要刷新的view了

        void unscheduleTraversals() {
            if (mTraversalScheduled) {
                mTraversalScheduled = false;
                mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
                mChoreographer.removeCallbacks(
                        Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
            }
        }
    
        void doTraversal() {
            if (mTraversalScheduled) {
                mTraversalScheduled = false;
                mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
    
                if (mProfile) {
                    Debug.startMethodTracing("ViewAncestor");
                }
    
                performTraversals();
    
                if (mProfile) {
                    Debug.stopMethodTracing();
                    mProfile = false;
                }
            }
        }
    

    然后看注释二:这里就是发出同步屏障的地方,用来保证这个屏幕刷新事件优先执行。

    最后是注释三:准备好前期工作后,注释三这里就是实际执行刷新的地方,会把这个刷新的事件封装到runnable里,然后postCallBack发出去。

    先来看看postCallBack里面。

    /**
         * Posts a callback to run on the next frame.
         * <p>
         * The callback runs once then is automatically removed.
         * </p>
         *
         * @param callbackType The callback type.
         * @param action The callback action to run during the next frame.
         * @param token The callback token, or null if none.
         *
         * @see #removeCallbacks
         * @hide
         */
        @TestApi
        public void postCallback(int callbackType, Runnable action, Object token) {
            postCallbackDelayed(callbackType, action, token, 0);//这里进去
        }
        
        /**
         * Posts a callback to run on the next frame after the specified delay.
         * <p>
         * The callback runs once then is automatically removed.
         * </p>
         *
         * @param callbackType The callback type.
         * @param action The callback action to run during the next frame after the specified delay.
         * @param token The callback token, or null if none.
         * @param delayMillis The delay time in milliseconds.
         *
         * @see #removeCallback
         * @hide
         */
        @TestApi
        public void postCallbackDelayed(int callbackType,
                Runnable action, Object token, long delayMillis) {
            if (action == null) {
                throw new IllegalArgumentException("action must not be null");
            }
            if (callbackType < 0 || callbackType > CALLBACK_LAST) {
                throw new IllegalArgumentException("callbackType is invalid");
            }
    
            postCallbackDelayedInternal(callbackType, action, token, delayMillis); //这里进去
        }
        
        private void postCallbackDelayedInternal(int callbackType,
                Object action, Object token, long delayMillis) {
            if (DEBUG_FRAMES) {
                Log.d(TAG, "PostCallback: type=" + callbackType
                        + ", action=" + action + ", token=" + token
                        + ", delayMillis=" + delayMillis);
            }
    
            synchronized (mLock) {
                final long now = SystemClock.uptimeMillis();
                final long dueTime = now + delayMillis;
                mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);
    
                if (dueTime <= now) {
                    scheduleFrameLocked(now);
                } else {
                    Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
                    msg.arg1 = callbackType;
                    msg.setAsynchronous(true);
                    mHandler.sendMessageAtTime(msg, dueTime);
                }
            }
        }
    

    这里先看scheduleFrameLocked(now),前面postCallback传进来一个delayMillis为0的参数,所以这里dueTime是==now的,会走到这个方法里面。

        private void scheduleFrameLocked(long now) {
            if (!mFrameScheduled) {
                mFrameScheduled = true;
                if (USE_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.
                    if (isRunningOnLooperThreadLocked()) {
                        scheduleVsyncLocked();
                    } else {
                        Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
                        msg.setAsynchronous(true);
                        mHandler.sendMessageAtFrontOfQueue(msg);
                    }
                } else {
                    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垂直同步信号的时候的刷新情况。里面有个英文注释大概的就是如果当前在有looper的线程(一般是主线程)里就直接执行,如果不在,就post一个message到UI线程里执行。



    所以我们直接看执行的方法scheduleVsyncLocked()

        private void scheduleVsyncLocked() {
            mDisplayEventReceiver.scheduleVsync();
        }
        
            /**
         * Schedules a single vertical sync pulse to be delivered when the next
         * display frame begins.
         */
        public void scheduleVsync() {
            if (mReceiverPtr == 0) {
                Log.w(TAG, "Attempted to schedule a vertical sync pulse but the display event "
                        + "receiver has already been disposed.");
            } else {
                nativeScheduleVsync(mReceiverPtr);
            }
        }
    
        // Called from native code.
        @SuppressWarnings("unused")
        private void dispatchVsync(long timestampNanos, int builtInDisplayId, int frame) {
            onVsync(timestampNanos, builtInDisplayId, frame);
        }
        
            /**
         * Called when a vertical sync pulse is received.
         * The recipient should render a frame and then call {@link #scheduleVsync}
         * to schedule the next vertical sync pulse.
         *
         * @param timestampNanos The timestamp of the pulse, in the {@link System#nanoTime()}
         * timebase.
         * @param builtInDisplayId The surface flinger built-in display id such as
         * {@link SurfaceControl#BUILT_IN_DISPLAY_ID_MAIN}.
         * @param frame The frame number.  Increases by one for each vertical sync interval.
         */
        public void onVsync(long timestampNanos, int builtInDisplayId, int frame) {
        }
    

    看注释说是在下一个垂直同步信号到来的时候刷新,那这个native方法就是注册监听这个垂直同步信号的方法了,下面的called from native code也看得出来,native方法监听到信号后返回到java层。这个onVsync方法在choreographer中被重写了

      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);
            }
    
            @Override
            public void onVsync(long timestampNanos, int builtInDisplayId, int frame) {
                // Ignore vsync from secondary display.
                // This can be problematic because the call to scheduleVsync() is a one-shot.
                // We need to ensure that we will still receive the vsync from the primary
                // display which is the one we really care about.  Ideally we should schedule
                // vsync for a particular display.
                // At this time Surface Flinger won't send us vsyncs for secondary displays
                // but that could change in the future so let's log a message to help us remember
                // that we need to fix this.
                if (builtInDisplayId != SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
                    Log.d(TAG, "Received vsync from secondary display, but we don't support "
                            + "this case yet.  Choreographer needs a way to explicitly request "
                            + "vsync for a specific display to ensure it doesn't lose track "
                            + "of its scheduled vsync.");
                    scheduleVsync();
                    return;
                }
    
                // 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); //这里
            }
        }
    

    重点看中文注释的部分,收到vsync信号后,会发一个异步消息用来优先执行doFrame方法。

    上面scheduleFrameLocked方法里面,不用vsync机制的话,也是直接发消息到doFrame方法里面,所以说这个方法就是实际刷新页面的方法。(这个用不用垂直同步信号的区别就自己上网查了,这里就不深入讲了)

        void doFrame(long frameTimeNanos, int frame) {
            …………省略…………
    
            try {
                Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Choreographer#doFrame");
                AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);
    
                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);
            }
            
            
            以下省略
    
    

    这个方法有点长,我们看重要的地方。还记得在viewRootImpl发过来的那个callBack吗

    mChoreographer.postCallback(
                        Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
                        
    doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);
    

    在这里就doCallBack了,终于执行了这个runnable了。也就是说Choreographer这里主要做的就是调用底层方法监听垂直同步信号,等到下一个信号来的时候告诉viewRootImpl可以开始刷新了。

    回到viewRootImpl这里

    final TraversalRunnable mTraversalRunnable = new TraversalRunnable(); //就是这个runnable
    
    final class TraversalRunnable implements Runnable {
            @Override
            public void run() {
                doTraversal();
            }
        }
        
           void doTraversal() {
            if (mTraversalScheduled) {
                mTraversalScheduled = false;
                mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
    
                if (mProfile) {
                    Debug.startMethodTracing("ViewAncestor");
                }
    
                performTraversals(); //就是这里
    
                if (mProfile) {
                    Debug.stopMethodTracing();
                    mProfile = false;
                }
            }
        }
    

    终于来到了熟悉的方法了,performTraversals这个方法大家估计都听过了,代码比较长,这里就不贴了。他里面会遍历全部的view,根据状态来调用performMeasure() 测量、perfromLayout() 布局、performDraw() 绘制这些流程,分别对应一个view的三个方法。

    好了,这就是android一次屏幕刷新大概的流程。

    仅供参考,欢迎指正

    相关文章

      网友评论

        本文标题:Android 浅谈屏幕绘制机制

        本文链接:https://www.haomeiwen.com/subject/fsnhrktx.html