美文网首页音视频进阶
音视频开发 一:创建EGL环境

音视频开发 一:创建EGL环境

作者: WhenMeet | 来源:发表于2020-08-24 09:04 被阅读0次

    一、简介

      EGL 是渲染 API(如 OpenGL ES)和原生窗口系统之间的接口。
    通常来说,OpenGL 是一个操作 GPU 的 API,它通过驱动向 GPU 发送相关指令,控制图形渲染管线状态机的运行状态,但是当涉及到与本地窗口系统进行交互时,就需要这么一个中间层,且它最好是与平台无关的。
    因此 EGL 被设计出来,作为 OpenGL 和原生窗口系统之间的桥梁

    二、功能

      EGL API 是独立于 OpenGL ES 各版本标准的独立的一套 API,其主要作用是为 OpenGL 指令 创建 Context 、绘制目标 Surface 、配置 FrameBuffer 属性、Swap 提交绘制结果 等。

      EGL 提供如下机制:
    与设备原生窗口通信
    查询绘制 surface 的可用类型和配置
    创建绘制 surface
    在 OpenGL ES 或其他渲染 API 之间同步渲染
    管理纹理贴图等渲染资源

      那么在安卓中,已经提供了GLSurfaceView,当我们需要把同一个场景渲染到不同的Surface上时,此时系统GLSurfaceView
    就不能满足需求了,所以我们需要自己创建EGL环境来实现渲染操作。

      注意:OpenGL整体是一个状态机,通过改变状态就能改变后续的渲染方式,而
    EGLContext(EgL上下文)就保存有所有状态,因此可以通过共享EGLContext
    来实现同一场景渲染到不同的Surface上。

      看下在安卓设备中创建Egl环境的流程

    • 1、得到Egl实例:
    • 2、得到默认的显示设备(就是窗口)
    • 3、初始化默认显示设备
    • 4、设置显示设备的属性
    • 5、从系统中获取对应属性的配置
    • 6、创建EglContext
    • 7、创建渲染的Surface
    • 8、绑定EglContext和Surface到显示设备中
    • 9、刷新数据,显示渲染场景

      看下具体的执行代码

    public class EglHelper {
    
        private EGL10 mEgl;
        private EGLDisplay mEglDisplay;
        private EGLContext mEglContext;
        private EGLSurface mEglSurface;
    
        public void initEgl(Surface surface, EGLContext eglContext) {
    
            //1、得到Egl实例
            mEgl = (EGL10) EGLContext.getEGL();
    
            //2、得到默认的显示设备(就是窗口)
            mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
            if (mEglDisplay == EGL10.EGL_NO_DISPLAY) {
                throw new RuntimeException("eglGetDisplay failed");
            }
    
            //3、初始化默认显示设备
            int[] version = new int[2];
            if (!mEgl.eglInitialize(mEglDisplay, version)) {
                throw new RuntimeException("eglInitialize failed");
            }
    
            //4、设置显示设备的属性
            int[] attributes = new int[]{
                    EGL10.EGL_RED_SIZE, 8,
                    EGL10.EGL_GREEN_SIZE, 8,
                    EGL10.EGL_BLUE_SIZE, 8,
                    EGL10.EGL_ALPHA_SIZE, 8,
                    EGL10.EGL_DEPTH_SIZE, 8,
                    EGL10.EGL_STENCIL_SIZE, 8,
                    EGL10.EGL_RENDERABLE_TYPE, 4,
                    EGL10.EGL_NONE};
    
            int[] num_config = new int[1];
            if (!mEgl.eglChooseConfig(mEglDisplay, attributes, null, 1, num_config)) {
                throw new IllegalArgumentException("eglChooseConfig failed");
            }
    
            int numConfigs = num_config[0];
            if (numConfigs <= 0) {
                throw new IllegalArgumentException(
                        "No configs match configSpec");
            }
    
            //5、从系统中获取对应属性的配置
            EGLConfig[] configs = new EGLConfig[numConfigs];
            if (!mEgl.eglChooseConfig(mEglDisplay, attributes, configs, numConfigs,
                    num_config)) {
                throw new IllegalArgumentException("eglChooseConfig#2 failed");
            }
    
            //6、创建EglContext
            if (eglContext != null) {
                mEglContext = mEgl.eglCreateContext(mEglDisplay, configs[0], eglContext, null);
            } else {
                mEglContext = mEgl.eglCreateContext(mEglDisplay, configs[0], EGL10.EGL_NO_CONTEXT, null);
            }
    
            //7、创建渲染的Surface
            mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, configs[0], surface, null);
    
            //8、绑定EglContext和Surface到显示设备中
            if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
                throw new RuntimeException("eglMakeCurrent fail");
            }
        }
    
        /**
         * 9、刷新数据,显示渲染场景
         **/
        public boolean swapBuffers() {
            if (mEgl != null) {
                return mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
            } else {
                throw new RuntimeException("egl is null");
            }
        }
    
        public EGLContext getEglContext() {
            return mEglContext;
        }
    
        public void destroyEgl() {
            if (mEgl != null) {
                mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_SURFACE,
                        EGL10.EGL_NO_CONTEXT);
    
                mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
                mEglSurface = null;
    
                mEgl.eglDestroyContext(mEglDisplay, mEglContext);
                mEglContext = null;
    
                mEgl.eglTerminate(mEglDisplay);
                mEglDisplay = null;
                mEgl = null;
            }
        }
    
    }
    
    

      使用起来也比较简单

    public class MainActivity extends Activity {
    
        private SurfaceView surfaceView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            surfaceView = findViewById(R.id.surface_view);
    
            surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
    
                }
    
                @Override
                public void surfaceChanged(final SurfaceHolder holder, int format, final int width, final int height) {
    
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            EglHelper eglHelper = new EglHelper();
                            eglHelper.initEgl(holder.getSurface(), null);
    
                            while (true) {
                                GLES20.glViewport(0, 0, width, height);
    
                                GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                                GLES20.glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
                                eglHelper.swapBuffers();
    
                                try {
                                    Thread.sleep(16);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }.start();
    
    
                }
    
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                }
            });
    
    
        }
    }
    

      布局就一个SurfaceView即可

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <SurfaceView
            android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

      看下最终的渲染效果

    渲染效果图

      嗯,很黄很有效。

    相关文章

      网友评论

        本文标题:音视频开发 一:创建EGL环境

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