android SurfaceView、TextureView、GLSurfaceView

刚开始看这几个东西把我搞的晕头转向的…. 本文就来理一下它们的绘制原理:

SurfaceView

Core: 使用独立Surface渲染, 可以在子线程中更新UI, 纯软绘制

它继承自类View,因此它本质上是一个View。但与普通View不同的是,它有自己的Surface。我们知道,一般的Activity包含的多个View会组成View hierachy的树形结构,只有最顶层的DecorView,也就是根结点视图,才是对WMS可见的。这个DecorView在WMS中有一个对应的WindowState。相应地,在SF中对应的Layer。而SurfaceView自带一个Surface,这个Surface在WMS中有自己对应的WindowState,在SF中也会有自己的Layer。如下图所示:

由于是独立的Surface, 在7.0之前:

SurfaceView进行缩放操作时不会影响到其内显示的内容的

不过在7.0之后, 做了优化:

 * <p class="note"><strong>Note:</strong> Starting in platform version
 * {@link android.os.Build.VERSION_CODES#N}, SurfaceView's window position is
 * updated synchronously with other View rendering. This means that translating
 * and scaling a SurfaceView on screen will not cause rendering artifacts. Such
 * artifacts may occur on previous versions of the platform when its window is
 * positioned asynchronously.</p>

SurfaceView进行缩放操作时会影响到其内显示的内容的

TextureView

Core: 必须存在于硬件加速窗口中, 可以在子线程中更新UI, 以OpelGL纹理(SurfaceTexture)的形式硬件渲染, 缩放时内部的内容会跟随着缩放

它内部利用SurfaceTextureHardwareLayer完成渲染。

SurfaceTexture可以理解为一个OpenGL纹理(它可以直接转为一个GL纹理),HardwareLayerTextureView.draw()时会更新SurfaceTextureHardwareLayer, HardwareLayer会交给当前的ViewTreeDisplayListCanvas来协同渲染,即你可以异步绘制,不过渲染时还是要跟随父视图的DisplyList一块渲染的。

TextureView绘制流程梳理

Canvas -> SurfaceTexture

lockCanvas() -> Canvas和mNativeWindow(SurfaceTexture)绑定

public Canvas lockCanvas(Rect dirty) {
    if (!isAvailable()) return null;

    if (mCanvas == null) {
        mCanvas = new Canvas(); 
    }

    synchronized (mNativeWindowLock) {
        if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) { 
            return null;
        }
    }
    mSaveCount = mCanvas.save();

    return mCanvas;
}

unlockCanvasAndPost() -> 同步数据到SurfaceTexture, 触发绘制操作 draw()

public void unlockCanvasAndPost(Canvas canvas) {
    if (mCanvas != null && canvas == mCanvas) {
        canvas.restoreToCount(mSaveCount);
        mSaveCount = 0;

        synchronized (mNativeWindowLock) {
            nUnlockCanvasAndPost(mNativeWindow, mCanvas);
        }
    }
}

draw()

@Override
public final void draw(Canvas canvas) {
    ...
    if (canvas.isHardwareAccelerated()) {
        DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;

        HardwareLayer layer = getHardwareLayer();
        if (layer != null) {
            applyUpdate();  //更新SurfaceTexture到HardwareLayer
            applyTransformMatrix(); //应用当前的缩放的属性(ViewTree的缩放也会影响到TextureView)

            mLayer.setLayerPaint(mLayerPaint); // ensure layer paint is up to date
            displayListCanvas.drawHardwareLayer(layer);
        }
    }
}

GLSurfaceView

Core : 拥有EGL绘制能力的SurfaceView

它主要是在SurfaceView的基础上实现了一个GLThread(EGLContext创建GL环境所在线程即为GL线程),绘制的工作直接通过OpenGL来进行,绘制的内容默认情况下依旧是绘制到SurfaceView所提供的Surface上。

EGLHelper.createSurface()

public boolean createSurface() {

    /*
        * Create an EGL surface we can render into.
        */
    GLSurfaceView view = mGLSurfaceViewWeakRef.get();
    if (view != null) {
        mEglSurface = view.mEGLWindowSurfaceFactory.createWindowSurface(mEgl,
                mEglDisplay, mEglConfig, view.getHolder()); //这个Holder就是SurfaceHolder
    } else {
        mEglSurface = null;
    }
}

DefaultWindowSurfaceFactory.createWindowSurface()

public EGLSurface createWindowSurface(EGL10 egl, EGLDisplay display, EGLConfig config, Object nativeWindow) {
    EGLSurface result = null;
    try {
        result = egl.eglCreateWindowSurface(display, config, nativeWindow, null);
    } catch (IllegalArgumentException e) {
        // This exception indicates that the surface flinger surface
        // is not valid. This can happen if the surface flinger surface has
        // been torn down, but the application has not yet been
        // notified via SurfaceHolder.Callback.surfaceDestroyed.
        // In theory the application should be notified first,
        // but in practice sometimes it is not. See b/4588890
        Log.e(TAG, "eglCreateWindowSurface", e);
    }
    return result;
}
阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/archives/20133,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?