Android播放器之SurfaceView与GLSurfaceView

概述先看SurfaceSurface的官方介绍:Handleontoarawbufferthatisbeingmanagedbythescreencompositor,Surface是一个rawbuffer的句柄,通过它在rawbuffer上进行绘制,可以通过Surface获得一个Canvas。Canvascanvas=mSurface.lockCanvas(null);mSurface.unlockCanvasAndP 先看Surface

Surface的官方介绍:Handle onto a raw buffer that is being managed by the screen compositor,Surface是一个raw buffer的句柄,通过它在raw buffer上进行绘制,可以通过Surface获得一个Canvas。

Canvas canvas = mSurface.lockCanvas(null);mSurface.unlockCanvasAndPost(canvas);
SurfaceVIEw

SurfaceVIEw对Surface进行了一次封装,它内部帮我们管理了一个Surface。我们使用SurfaceVIEw其实最终都是获取到这个Surface去绘制,可参看官方解释:

ProvIDes a dedicated drawing surface embedded insIDe of a vIEw hIErarchy. You can control the format of this surface and, if you like, its size; the SurfaceVIEw takes care of placing the surface at the correct location on the screen

The surface is Z ordered so that it is behind the window holding its SurfaceVIEw; the SurfaceVIEw punches a hole in its window to allow its surface to be displayed. The vIEw hIErarchy will take care of correctly compositing with the Surface any siblings of the SurfaceVIEw that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full Alpha-blended composite will be performed each time the Surface changes.

The transparent region that makes the surface visible is based on the layout positions in the vIEw hIErarchy. If the post-layout @R_301_4154@ propertIEs are used to draw a sibling vIEw on top of the SurfaceVIEw, the vIEw may not be properly composited with the surface.

Access to the underlying surface is provIDed via the SurfaceHolder interface, which can be retrIEved by calling getHolder().

The Surface will be created for you while the SurfaceVIEw's window is visible; you should implement SurfaceHolder.Callback#surfaceCreated and SurfaceHolder.Callback#surfaceDestroyed to discover when the Surface is created and destroyed as the window is shown and hIDden.

One of the purposes of this class is to provIDe a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics:

All SurfaceVIEw and SurfaceHolder.Callback methods will be called from the thread running the SurfaceVIEw's window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread.You must ensure that the drawing thread only touches the underlying Surface while it is valID -- between @L_403_4@ and SurfaceHolder.Callback.surfaceDestroyed().

绘制过程:

通过SurfaceHolder.getSurface可以获取到Surface;通过Surface.lockCanvas可以获取到Surface的Canvas;使用Canvas绘制图像;使用Surface.unlockCanvasAndPost可以释放Canvas。GLSurfaceVIEw

GLSurfaceVIEw继承自SurfaceVIEw,对SurfaceVIEw又做了一次封装,方便我们在安卓中使用OpenGL。

GLSurfaceVIEw提供了以下特性:

提供并且管理一个独立的Surface;提供并且管理一个EGL display,它能让opengl把内容渲染到上述的Surface上;支持用户自定义渲染器(Render),通过setRenderer设置一个自定义的Renderer;让渲染器在独立的GLThread线程里运作,和UI线程分离;支持按需渲染(on-demand)和连续渲染(continuous)两种模式;GPU加速:GLSurfaceVIEw的效率是SurfaceVIEw的30倍以上,SurfaceVIEw使用画布进行绘制,GLSurfaceVIEw利用GPU加速提高了绘制效率;VIEw的绘制onDraw(Canvas canvas)使用Skia渲染引擎渲染,而GLSurfaceVIEw的渲染器Renderer的onDrawFrame(GL10 gl)使用opengl绘制引擎进行渲染。

参看官方解释:

An implementation of SurfaceVIEw that uses the dedicated surface for displaying OpenGL rendering.

A GLSurfaceVIEw provIDes the following features:

Manages a surface, which is a special pIEce of memory that can be composited into the AndroID vIEw system.Manages an EGL display, which enables OpenGL to render into a surface.Accepts a user-provIDed Renderer object that does the actual rendering.Renders on a dedicated thread to decouple rendering performance from the UI thread.Supports both on-demand and continuous rendering.Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.总结

除了上述区别外,SurfaceVIEw通用性更好,GLSurfaceVIEw渲染更细腻,如果想让普通的SurfaceVIEw渲染效果更好,可以加抗锯齿效果,不过抗锯齿效果会有一定的性能消耗,硬解码设置surface模式的话,直接用普通的SurfaceVIEw。

一般兼容性比较好的播放器,会同时支持SurfaceVIEw和GLSurfaceVIEw两种模式供用户根据实际场景选择,以大牛直播SDK(Github)的AndroID平台RTSP和RTMP播放端为例:

    /* Create rendering */    private boolean CreateVIEw() {        if (sSurfaceVIEw == null) {            Log.i(TAG, "CreateVIEw..");            String manufacturer = Build.MANUFACTURER;            Log.i(TAG, "CreateVIEw, current manufacturer: " + manufacturer);            if (is_enable_harDWare_render_mode) {                //harDWare render模式,第二个参数设置为false                sSurfaceVIEw = NTRenderer.CreateRenderer(this, false);            } else {                //这个字符串可以自己定义,例如判断华为就填写huawei,魅族就填写meizu                if ("huawei".equalsIgnoreCase(manufacturer)) {                    sSurfaceVIEw = NTRenderer.CreateRenderer(this, true);                } else {                    /*                     * uSEOpenGLES2: If with true: Check if system supports openGLES, if                     * supported, it will choose openGLES. If with false: it will set                     * with default surfaceVIEw;                     */                    sSurfaceVIEw = NTRenderer.CreateRenderer(this, true);                }            }        }        if (sSurfaceVIEw == null) {            Log.i(TAG, "Create render Failed..");            return false;        }        if (is_enable_harDWare_render_mode) {            SurfaceHolder surfaceHolder = sSurfaceVIEw.getHolder();            if (surfaceHolder == null) {                Log.e(TAG, "CreateVIEw, surfaceHolder with null..");            }            surfaceHolder.addCallback(this);        }        return true;    }

感兴趣的开发者可以参考官方文档。

总结

以上是内存溢出为你收集整理的Android播放器之SurfaceView与GLSurfaceView全部内容,希望文章能够帮你解决Android播放器之SurfaceView与GLSurfaceView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1032049.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存