2018正版葡京赌侠诗Android Render(二)7.1源码硬件加速下draw绘制流程分析


阅读者三篇Android绘制作品,会让你对了然Android绘制有帮扶:


剖析从draw(Canvas canvas)和draw(Canvas canvas, ViewGroup parent, long
drawing提姆(Tim)e)四个法子出手:

draw(Canvas canvas)和draw(Canvas canvas, ViewGroup parent, long
drawing提姆e)方法从表面看来就是接受的参数不同等,
其实二期有一个先后顺序,从属关系,不过也要分情状,就看脚下的View是不是五星级的DecorView了,就是说一个View有没有parent
view,View的六个draw方法是有不等同的调用顺序的,当然只有DecorView是一级的View,DecorView没有parent
view。

正常情状下,draw(Canvas canvas, ViewGroup parent, long
drawing提姆e)方法被眼前view的parentView调用,在draw(Canvas canvas,
ViewGroup parent, long
drawing提姆(Tim)e)方法中会按照是否匡助硬件加速来走不通的流水线最终都会调用到draw(Canvas canvas)方法来做正在的绘图,draw(Canvas canvas)主意中会调用到dispatchDraw(canvas)措施,来向下分发绘制,dispatchDraw(canvas)形式中会调用draw(Canvas
canvas, ViewGroup parent, long drawing提姆e)。绘制就少有向下传递。

可是作为甲级的DecorView就不同了,ViewRootImpl调用DecorView的draw(Canvas
canvas)方法直接打开所有view tree的绘图,DecorView的draw(Canvas
canvas)方法中调用dispatchDraw(canvas)开头向下分发绘制。一稀有传到到view
tree的最底部。

图1 View Hierarchy

漫天View Hierarchy真正绘制开首是从DecorView的draw(Canvas
canvas)方法起先的,下边描述一下Activity的开行到调用到DecorView的draw(Canvas
canvas)方法的流水线:

/**1*/ ApplicationThread的onTransact方法接收到SystemServer进程的SCHEDULE_LAUNCH_ACTIVITY_TRANSACTION启动Activity的Binder信息
                ↓
/**2*/ ApplicationThread.scheduleLaunchActivity() //
                ↓
/**3*/ ActivityThread.scheduleLaunchActivity() //安排启动Activity
                ↓
/**4*/ ActivityThread.handleLaunchActivity()  //处理启动Activity
                ↓
/**5*/ ActivityThread.handleResumeActivity() // Activity 的Resume会使DecorView跟ViewRootImpl关联
                ↓
/**6*/ WindowManagerGlobal.addView() //全局保存窗口的信息
                ↓
/**7*/ ViewRootImpl.setView()  //使DecorView和ViewRootImpl关联并绘制界面
                ↓
/**8*/ ViewRootImpl.requestLayout() //请求绘制ViewTree
                ↓
/**9*/ ViewRootImpl.scheduleTraversals() // 安排遍历 
                ↓
/**10*/ ViewRootImpl.doTraversal() //
                ↓
/**11*/ ViewRootImpl.performTraversals() //执行遍历 会根据情况调用relayoutWindow performMeasure performLayout performDraw 等方法 这四个方法跟绘制是紧密相关的
                ↓
/**12*/ ViewRootImpl.performDraw() //执行绘制
                ↓
/**13*/ ViewRootImpl.draw(boolean fullRedrawNeeded)//区分是否支持硬件加速来走不同的绘制流程                
                ↓
             .........
                ↓
/**14*/ DecorView.draw(Canvas canvas)  //不管走不走硬件加速都会调到这里

全部界面的绘图从 DecorView.draw(Canvas canvas)方法开始紧张!

一、draw(canvas,parent,drawing提姆e)和draw(canvas)效能不同

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {
  /**
     * This method is called by ViewGroup.drawChild() to have each child view draw itself.
     * 此方法是被父控件ViewGroup.drawChild()调用的
     * drawChild()方法又是被ViewGroup中的dispatchDraw(Canvas canvas)方法调用的
     * This is where the View specializes rendering behavior based on layer type,
     * and hardware acceleration.
     */
    boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
        final boolean hardwareAcceleratedCanvas = canvas.isHardwareAccelerated();
        /* If an attached view draws to a HW canvas, it may use its RenderNode + DisplayList.
         *
         * If a view is dettached, its DisplayList shouldn't exist. If the canvas isn't
         * HW accelerated, it can't handle drawing RenderNodes.
         */
         //判断当前View是否支持硬件加速绘制
        boolean drawingWithRenderNode = mAttachInfo != null
                && mAttachInfo.mHardwareAccelerated
                && hardwareAcceleratedCanvas;

        ......略

        //硬件加速绘制用到的绘制节点
        RenderNode renderNode = null;
        //cpu绘制用到的绘制缓存
        Bitmap cache = null;
        //获取当前View的绘制类型 LAYER_TYPE_NONE LAYER_TYPE_SOFTWARE LAYER_TYPE_HARDWARE
        int layerType = getLayerType(); // TODO: signify cache state with just 'cache' local
        if (layerType == LAYER_TYPE_SOFTWARE || !drawingWithRenderNode) {  //如果是cpu绘制类型
             if (layerType != LAYER_TYPE_NONE) {
                 // If not drawing with RenderNode, treat HW layers as SW
                 layerType = LAYER_TYPE_SOFTWARE;
                 //开始cpu绘制缓存构建
                 buildDrawingCache(true);
            }
            //获得cpu绘制缓存结果 存储在Bitmap中
            cache = getDrawingCache(true);
        }

        if (drawingWithRenderNode) { //支持硬件加速
            // Delay getting the display list until animation-driven alpha values are
            // set up and possibly passed on to the view
            //更新gpu绘制列表 保存在RenderNode中
            renderNode = updateDisplayListIfDirty();
            if (!renderNode.isValid()) {
                // Uncommon, but possible. If a view is removed from the hierarchy during the call
                // to getDisplayList(), the display list will be marked invalid and we should not
                // try to use it again.
                renderNode = null;
                //gpu绘制失败标识
                drawingWithRenderNode = false;
            }
        }

        ......略

        //cpu绘制成功并且gpu绘制失败了
        final boolean drawingWithDrawingCache = cache != null && !drawingWithRenderNode;

        ......略


        if (!drawingWithDrawingCache) { //走gpu绘制
            if (drawingWithRenderNode) { //支持gpu绘制
                mPrivateFlags &= ~PFLAG_DIRTY_MASK;
                //gpu绘制收集到的DisplayList
                ((DisplayListCanvas) canvas).drawRenderNode(renderNode);
            } else { //走gpu绘制又突然不支持gpu绘制(可能是极限情况下)
                // Fast path for layouts with no backgrounds
                if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
                    mPrivateFlags &= ~PFLAG_DIRTY_MASK;
                    //没有内容不需要绘制自己,就直接向下分发绘制子View
                    dispatchDraw(canvas);
                } else {
                    //绘制自己后再分发绘制子View
                    draw(canvas);
                }
            }
        } else if (cache != null) { //走cpu绘制且cpu绘制缓存不为null

         ......略

            //把存储cpu绘制缓存的Bitmap用canvas走cpu绘制(skia渲染引擎)
            canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint);

        }
         ......略

        return more;
    }

    /**
     * Manually render this view (and all of its children) to the given Canvas.
     * The view must have already done a full layout before this function is
     * called.  When implementing a view, implement
     * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
     * If you do need to override this method, call the superclass version.
     *
     * @param canvas The Canvas to which the View is rendered.
     */
    @CallSuper
    public void draw(Canvas canvas) {
        final int privateFlags = mPrivateFlags;
        final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
                (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
        mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

        int saveCount;
        // Step 1 绘制背景
        if (!dirtyOpaque) {
            drawBackground(canvas);
        }

        //Step 2,必要时,保存画布的图层为褪色做准备
        final int viewFlags = mViewFlags;
        boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
        boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
        if (!verticalEdges && !horizontalEdges) {
            // Step 3, 绘制View自身内容
            if (!dirtyOpaque) onDraw(canvas);

            //Step 4, 绘制子View的内容
            dispatchDraw(canvas);

            // Overlay is part of the content and draws beneath Foreground
            //Step 5, 必要时,绘制褪色边缘并恢复图层,通过        getOverlay().add(drawable); 添加图片什么的
            if (mOverlay != null && !mOverlay.isEmpty()) {
                mOverlay.getOverlayView().dispatchDraw(canvas);
            }

            // Step 6, 绘制装饰(列如滚动条)
            onDrawForeground(canvas);

            // we're done...
            return;
        }

        ......略
}

可以看来View中的
updateDisplayListIfDirty()办法是gpu绘制的基本点,buildDrawingCache()措施是cpu绘制的紧要。
updateDisplayListIfDirty()buildDrawingCache()模式都会调用到View的draw(canvas)方法,可是updateDisplayListIfDirty()主意中给draw(canvas)传的是DisplayListCanvas参数,使其负有HWUI的效用。buildDrawingCache()艺术中给draw(canvas)格局传入的是普通的Canvas。

也得以很清楚滴看到,对于我们开发者来说,draw(Canvas canvas, ViewGroup parent, long drawingTime)艺术就是一个View的绘图入口,从那么些艺术中决定了走cpu绘制仍然gpu绘制。

draw(Canvas canvas)艺术是现实性的绘图工作,如假使gpu硬件加速绘制,则接纳DisplayListCanvas画布绘制,会把绘制DisplayList保存在绘制节点RenderNode中。如若是CPU软绘制,则运用普通的Canvas画布绘制,把绘制缓存保存在一个Bitmap中,最终会利用canvas.drawBitmap()措施运用skia渲染引擎cpu绘制缓存Bitmap中的数据。

二、一级DecorView硬件加速调用draw(canvas)

注意:
DecorView实在是一个FrameLayout,FrameLayout是一个ViewGroup,ViewGroup是一个接续View的抽象类,draw(canvas)情势只在View类中有落实,所以调用DecorViewdraw(canvas)事实上最后调用的是Viewdraw(canvas)方法。

地方已经说了,DecorView是顶级View,它的draw(canvas)形式是绘制的开始,那么在硬件加速下ViewRootImpl是怎么调用到DecorView的draw(canvas)的呢?

得从ViewRootImpl的draw(boolean fullRedrawNeeded)办法开端分析:

/***********************************************************************        
   /**1*/ ApplicationThread的onTransact方法接收到SystemServer进程的SCHEDULE_LAUNCH_ACTIVITY_TRANSACTION启动Activity的Binder信息
                ↓
   /**2*/ ApplicationThread.scheduleLaunchActivity() //
                   ↓
   /**3*/ ActivityThread.scheduleLaunchActivity() //安排启动Activity
                   ↓
   /**4*/ ActivityThread.handleLaunchActivity()  //处理启动Activity
                   ↓
   /**5*/ ActivityThread.handleResumeActivity() // Activity 的Resume会使DecorView跟ViewRootImpl关联
                   ↓
   /**6*/ WindowManagerGlobal.addView() //全局保存窗口的信息
                   ↓
   /**7*/ ViewRootImpl.setView()  //使DecorView和ViewRootImpl关联并绘制界面
                   ↓
   /**8*/ ViewRootImpl.requestLayout() //请求绘制ViewTree
                   ↓
   /**9*/ ViewRootImpl.scheduleTraversals() // 安排遍历 
                   ↓
   /**10*/ ViewRootImpl.doTraversal() //
                   ↓
   /**11*/ ViewRootImpl.performTraversals() //执行遍历 会根据情况调用relayoutWindow performMeasure performLayout performDraw 等方法 这四个方法跟绘制是紧密相关的
                   ↓
   /**12*/ ViewRootImpl.performDraw() //执行绘制
                   ↓
   /**13*/ 区分是否支持硬件加速来走不同的绘制流程*********************************/  

   private void draw(boolean fullRedrawNeeded) {

      ......略

        if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) { //支持硬件加速并且需要绘制
            if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {

                ......略
                //1 硬件加速调DecorView 的draw(canvas)方法的关键
                mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
            } else {

                ......略

                //2 非硬件加速调DecorView的draw(canvas)方法的关键
                if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)) {
                    return;
                }
            }
        }
               ......略
    }
}

ThreadedRenderer是5.0上为每个过程新增了一个RenderThread线程,既一个渲染线程,RenderThread线程可以确保在主线程阻塞的情事下动画执行依然流畅顺滑。就是一个异步绘制的拍卖线程。
更多请参见:
http://www.jianshu.com/p/bc1c1d2fadd1
http://blog.csdn.net/guoqifa29/article/details/45131099

咱俩先分析硬件加速调用DecorViewdraw(canvas)方法,先看mAttachInfo.mHardwareRenderer.draw(mView,
mAttachInfo, this)里面的流程:

/**
 *5.0新增的渲染线程
 */
public final class ThreadedRenderer {
    ......略 
    /**
     * Draws the specified view.
     *
     * @param view The view to draw.
     * @param attachInfo AttachInfo tied to the specified view.
     * @param callbacks Callbacks invoked when drawing happens.
     */
    void draw(View view, AttachInfo attachInfo, HardwareDrawCallbacks callbacks) {

        //1 圈起来 终点 要考的 ,其实就是更新`DecorView`的`DisplayList`
        updateRootDisplayList(view, callbacks);

    }

    //其实就是更新`DecorView`的`DisplayList`
    private void updateRootDisplayList(View view, HardwareDrawCallbacks callbacks) {

        //更新`DecorView`的`DisplayList`
        updateViewTreeDisplayList(view);

        if (mRootNodeNeedsUpdate || !mRootNode.isValid()) {
            //1 获取一个DisplayListCanvas画布
            DisplayListCanvas canvas = mRootNode.start(mSurfaceWidth, mSurfaceHeight);
            try {
                final int saveCount = canvas.save();
                canvas.translate(mInsetLeft, mInsetTop);
                callbacks.onHardwarePreDraw(canvas);

                canvas.insertReorderBarrier();

                //2 绘制获取到的DecorView的RenderNode
                //view.updateDisplayListIfDirty()其实是调用的DecorView的updateDisplayListIfDirty方法,
                //通过层层调用updateDisplayListIfDirty方法最终会获取整个view tree的绘制节点`RenderNode`

                canvas.drawRenderNode(view.updateDisplayListIfDirty());

                canvas.insertInorderBarrier();

                callbacks.onHardwarePostDraw(canvas);
                canvas.restoreToCount(saveCount);
                mRootNodeNeedsUpdate = false;
            } finally {
                //3 整个View tree绘制结束后回收资源
                mRootNode.end(canvas);
            }
        }
    }
  ......略
}

从上边可以看来 更新DecorViewDisplayList而调用
updateViewTreeDisplayList(view)方法,这些措施请看:

/**
 *5.0新增的渲染线程
 */
public final class ThreadedRenderer {
    ......略
    private void updateViewTreeDisplayList(View view) {
        view.mPrivateFlags |= View.PFLAG_DRAWN;
        view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
                == View.PFLAG_INVALIDATED;
        view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
    //其实也是更新DecorView的DisplayList而调用view.updateDisplayListIfDirty()方法
        view.updateDisplayListIfDirty();
        view.mRecreateDisplayList = false;
    }
    ......略
}

总的来看此间那么可以了解,硬件加速的情况下,DecorViewupdateDisplayListIfDirty措施是首要,也是从这里调用到DecorView的的draw(canvas)方法开启绘制的,请看源代码:

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {

   ......略

    /**
     * 更新一个View的绘制DisplayList保存在RenderNode返回 
     * 返回的RenderNode是被ThreadedRenderer线程的drawRenderNode(RenderNode)方法绘制的
     * Gets the RenderNode for the view, and updates its DisplayList (if needed and supported)
     * @hide
     */
    @NonNull
    public RenderNode updateDisplayListIfDirty() {
        final RenderNode renderNode = mRenderNode;

               ......略

            //从renderNode中获取一个DisplayListCanvas
            final DisplayListCanvas canvas = renderNode.start(width, height);
            canvas.setHighContrastText(mAttachInfo.mHighContrastText);

            try {
                if (layerType == LAYER_TYPE_SOFTWARE) { //为CPU绘制draw(canvas)方法
                    buildDrawingCache(true); //创建CPU绘制缓存会调用到View的
                    Bitmap cache = getDrawingCache(true);  //保存CPU绘制缓存
                    if (cache != null) {
                        canvas.drawBitmap(cache, 0, 0, mLayerPaint); //skia绘制收集到的Bitmap缓存数据
                    }
                } else { //为硬件加速GPU绘制
                    computeScroll();

                    canvas.translate(-mScrollX, -mScrollY);
                    mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
                    mPrivateFlags &= ~PFLAG_DIRTY_MASK;

                    // Fast path for layouts with no backgrounds
                    if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
                        dispatchDraw(canvas); //View本身不需要绘制 直接分发给子View绘制
                        if (mOverlay != null && !mOverlay.isEmpty()) {
                            mOverlay.getOverlayView().draw(canvas);
                        }
                    } else {
                        //使用DisplayListCanvas绘制,需要的绘制会保存在DisplayList
                        draw(canvas);
                    }
                }
            } finally {
                renderNode.end(canvas);  //回收资源
                setDisplayListProperties(renderNode);
            }
        } else {
            mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
            mPrivateFlags &= ~PFLAG_DIRTY_MASK;
        }
        return renderNode; //返回保存有GPU绘制数据DisplayList的绘制节点renderNode
    }

   ......略

}

可以寓目在View的updateDisplayListIfDirty措施中,辅助硬件加速的场馆下准备好RenderNodeDisplayListCanvas后一向调用了View的draw(canvas)方法。

总结流程:

//前提是需要支持硬件加速
ViewRootImpl.draw(boolean fullRedrawNeeded) 
                   ↓
ThreadedRenderer.draw(view,attachInfo,hardwareDrawCallbacks)                   
                   ↓           
ThreadedRenderer.updateRootDisplayList(view, callbacks)
                   ↓
DecorView.updateDisplayListIfDirty()
                   ↓  
        DecorView.draw(canvas)

三、一级DecorView非硬件加速调用draw(canvas)

从地方的ViewRootImpldraw(boolean fullRedrawNeeded)艺术中能够看出,假诺是CPU绘制,就会走drawSoftware()方法。那么我们看一下drawSoftware()中是怎么调到DecorView的draw(canvas)方法的:

ViewRootImpl``drawSoftware()方法:

    /**
     * @return true if drawing was successful, false if an error occurred
     */
    private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
            boolean scalingRequired, Rect dirty) {

        // Draw with software renderer.
        final Canvas canvas;

            ......略

            //从Surface中获取一个普通的Canvas
            canvas = mSurface.lockCanvas(dirty);

            ......略

            //调用DecorView的draw(canvas)方法
            mView.draw(canvas);

            ......略

        return true;
    }

小结流程:

//前提是不支持硬件加速
ViewRootImpl.draw(boolean fullRedrawNeeded) 
                   ↓
ViewRootImpl.drawSoftware(surface, mAttachInfo, xOffset, yOffset, scalingRequired, dirty)                  
                   ↓  
        DecorView.draw(canvas)

DecorView的draw(canvas)方法调用总括:

DecorView作为头等View的留存,它的绘图是由ViewRootImpl判断是CPU依然GPU绘制然后调用DecorViewdraw(canvas)艺术,开启所有界面的绘图。

其他的View,都有是协调的父控件调用
draw(canvas,parent,drawingTime)措施,在draw(canvas,parent,drawing提姆(Tim)e)方法中判断当前View是CPU依旧GPU绘制然后调用draw(canvas)

四、非顶尖View硬件加速draw(canvas, parent, drawing提姆(Tim)e)调用draw(Canvas canvas)

下边我一度说了,整个界面的绘图是从DecorViewdraw(canvas)形式开始,普通View的绘图是从draw(canvas, parent, drawingTime)开始。
View的draw(canvas, parent, drawingTime)、draw(Canvas
canvas)和updateDisplayListIfDirty()多少个法子本身这边就不粘贴了,上一度有了,直接给出流程吧:

//GPU绘制
Vew.draw(Canvas canvas, ViewGroup parent, long drawingTime)
     ↓
Vew.updateDisplayListIfDirty()
     ↓
Vew.draw(displayListCanvas)

五、非顶尖View非硬件加速draw(canvas, parent, drawing提姆e)调用draw(Canvas canvas)

先交由CPU绘制流程:

//CPU绘制
Vew.draw(Canvas canvas, ViewGroup parent, long drawingTime)
     ↓
Vew.buildDrawingCache(boolean autoScale)
     ↓
Vew.buildDrawingCacheImpl(boolean autoScale)
     ↓ 
Vew.draw(displayListCanvas)

draw(canvas, parent,
drawing提姆(Tim)e)方法调用到buildDrawingCache在下边的代码中可以看到,这里就看一下buildDrawingCachebuildDrawingCacheImpl方法:

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {

   ......略

       public void buildDrawingCache(boolean autoScale) {
        if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ?
                mDrawingCache == null : mUnscaledDrawingCache == null)) {
            if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) {
                Trace.traceBegin(Trace.TRACE_TAG_VIEW,
                        "buildDrawingCache/SW Layer for " + getClass().getSimpleName());
            }
            try {
                //执行CPU绘制缓存创建
                buildDrawingCacheImpl(autoScale);
            } finally {
                Trace.traceEnd(Trace.TRACE_TAG_VIEW);
            }
        }
    }

       ......略

    /**
     * private, internal implementation of buildDrawingCache, used to enable tracing
     */
    private void buildDrawingCacheImpl(boolean autoScale) {

           ......略

            //保存CPU绘制缓存的Bitmap
            Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;

           ......略
        if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {

            ......略

            try {
                //如果缓存Bitmap为空就重新创建和赋值
                bitmap = Bitmap.createBitmap(mResources.getDisplayMetrics(),
                        width, height, quality);
                bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
                if (autoScale) {
                    mDrawingCache = bitmap;
                } else {
                    mUnscaledDrawingCache = bitmap;
                }
                if (opaque && use32BitCache) bitmap.setHasAlpha(false);
            } catch (OutOfMemoryError e) {

                ......略
            }

            clear = drawingCacheBackgroundColor != 0;
        }

        Canvas canvas;
        if (attachInfo != null) { //处理Canvas
            canvas = attachInfo.mCanvas;
            if (canvas == null) {
                canvas = new Canvas();
            }
            //canvas的Bitmap设置为我们创建的缓存Bitmap
            canvas.setBitmap(bitmap);

            ......略

        } else {
            // This case should hopefully never or seldom happen
            canvas = new Canvas(bitmap);
        }
            ......略

        // Fast path for layouts with no backgrounds
        if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
            mPrivateFlags &= ~PFLAG_DIRTY_MASK;

            dispatchDraw(canvas); //自己不需要绘制,直接分发子View绘制

            if (mOverlay != null && !mOverlay.isEmpty()) {
                mOverlay.getOverlayView().draw(canvas);
            }
        } else {
            //自己需要绘制,然后再分发子View绘制,所有的绘制都会画在缓存Bitmap上面
            draw(canvas);
        }

            ......略
    }

   ......略

   }

绘制总括:

随便是支撑依旧不帮助硬件加速,都会调用到View的draw(canvas)方法。
只是硬件加速的情形下为DisplayListCanvas画布,得到的DisplayList数码保存在每一个View的绘图节点RenderNode中,最终交给DisplayListCanvasdrawRenderNode(renderNode)主意处理渲染操作。
非硬件加速的情景下,会把具有的绘图缓存数据保存到一个缓存Bitmap中,然后由Canvas.drawBitmap(cache,
0.0f, 0.0f, mLayerPaint)负责把数量提交skia渲染。

DisplayList构建分析请看:http://www.jianshu.com/p/7bf306c09c7e

整体测量定位绘制流程总括总计:

其实draw流程相对于MeasureLayout来说特别一些,为何这样说?即使你询问view的成套工艺流程就知道,MeasureLayout流程在ViewViewGroup这几个主导控件中都从不具体落实,而View的绘图在View这一个主题的类中都兑现了,而且在View的raw(Canvas
canvas, ViewGroup parent, long drawing提姆e)
方法中区别了cpugpu制图来走不同的流水线。View的draw(Canvas
canvas)方法实现了切实可行的6个绘制步骤,ViewGroup中的dispatchDraw(Canvas
canvas)方法实现了切实的子View的绘图分发。

为啥是如此?

因为为着力的ViewViewGroup控件不可以操纵具体的金科玉律,这里说的指南更多偏重于控件的排列,大小宽高,之间的相对地点,这一个属性都是由,MeasureLayout流程控制的。所以无论你怎么走MeasureLayout流程,其draw绘制都是同一的。draw绘制出来的规范是由具体的LinearLayoutFrameLayoutRelativeLayoutRecyclerView等等控件的MeasureLayout流程控制的。所以MeasureLayout流程需要在切实的控件中实际实现。

本来下面说的是系统的控件,LinearLayoutFrameLayout这多少个,自定义的View或者ViewGroup的流程那就完全由开发者自己说了。

不清楚绘制流程的请看:http://blog.csdn.net/yanbober/article/details/46128379

硬件绘制软件绘不足之处

当前硬件绘制软件绘图都设有不足,比如(来自:http://blog.csdn.net/jxt1234and2010/article/details/47326411):
脏区域识别之后并从未丰裕地优化 。

软件渲染时,尽管限制了渲染区域,但装有ViewonDraw艺术一个不丢的推行了四遍。
硬件渲染时,防止了没刷新的ViewonDraw主意革新呈现列表,但出示列表中的命令依旧一个不落的在全屏幕上推行了五遍。

一个相比易于想到的优化方案就是为主流程中的View建立一个R-Tree索引,invalidate这一接口修改为可以流传一个矩形范围R,更新时,利用R-Tree索引找出含有R的具备叶子View,令这多少个View在R范围重绘几遍即可。

本条槽点其实影响倒不是很大,大部分状态下View不多,且一旦出现性能问题,基本上都是一半上述的屏幕刷新。

对不住我们了,有一个硬件绘制很重点大方法dispatchGetDisplayList()没讲到,抱歉了。罪过罪过呀!

相关文章