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提姆(Tim)e)方法被眼前view的parentView调用,在draw(Canvas canvas,
ViewGroup parent, long
drawing提姆e)方法中会依照是否协理硬件加速来走不通的流水线最后都会调用到draw(Canvas canvas)形式来做正在的绘图,draw(Canvas canvas)艺术中会调用到dispatchDraw(canvas)办法,来向下分发绘制,dispatchDraw(canvas)艺术中会调用draw(Canvas
canvas, ViewGroup parent, long drawing提姆(Tim)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提姆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提姆(Tim)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()没讲到,抱歉了。罪过罪过啊!

发表评论

电子邮件地址不会被公开。 必填项已用*标注