1. 程式人生 > >OpenGL ES(18): 核心:自定義EGL環境

OpenGL ES(18): 核心:自定義EGL環境

1.簡介


EGL:
是OpenGL ES和本地視窗系統的介面,不同平臺上EGL配置是不一樣的,而
OpenGL的呼叫方式是一致的,就是說:OpenGL跨平臺就是依賴於EGL介面。

為什麼要自己建立EGL環境?
當我們需要把同一個場景渲染到不同的Surface上時,此時系統GLSurfaceView
就不能滿足需求了,所以我們需要自己建立EGL環境來實現渲染操作。

注意:OpenGL整體是一個狀態機,通過改變狀態就能改變後續的渲染方式,而
EGLContext(EgL上下文)就儲存有所有狀態,因此可以通過共享EGLContext
來實現同一場景渲染到不同的Surface上。

步驟:

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");
        }

//version中存放EGL 版本號,int[0]為主版本號,int[1]為子版本號

4、設定顯示裝置的屬性,根據設定的屬性尋找相匹配的配置的個數

int[] attrbutes = 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,attrbutes,null,1,num_config)){
            throw new IllegalArgumentException("eglChooseConfig failes");
        }
int numConfigs = num_config[0];
        if (numConfigs <= 0){
            throw new IllegalArgumentException("No configs match configSpec");
        }

//eglChooseConfig()引數解釋:
//null,1  表示找到了配置不存放,表示只找一個匹配的配置就行了
//num_config[0] 用於存放匹配的配置的個數,如果連一個也找不到,那就是沒有。


5、從系統中獲取對應屬性的配置,(如果對應的配置數大於0)

EGLConfig[] configs = new EGLConfig[numConfigs];
        if (!mEgl.eglChooseConfig(mEglDisplay,attrbutes,configs,numConfigs,num_config)){
            throw new IllegalArgumentException("eglChooseConfig get failed");
        }

//這裡用EGLConfig[] 存放相匹配的配置


6、建立EglContext

                                                        ※ 需要用EGLDisPlay和對應的配置外加eglContext 來 建立EGLContext

 if (eglContext != null){
            mEglContext = mEgl.eglCreateContext(mEglDisplay,configs[0],eglContext,null);
        }else {
            mEglContext = mEgl.eglCreateContext(mEglDisplay,configs[0],EGL10.EGL_NO_CONTEXT,null);
        }

//eglCreateContext()引數解釋:
//由於傳入的 eglContext == null ,所以需要傳入 EGL10.EGL_NO_CONTEXT
//如果傳入的 eglContext != null ,就傳入 eglContext


7、建立渲染的Surface

                                                        ※ 需要用EGLDisPlay和對應的配置外加surface 來 建立EGLSurface

                                                            surface可以由SurfaceView的SurfaceHolder來得到。

 mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,configs[0],surface,null);


8、繫結EglContext和Surface到顯示裝置中

if (!mEgl.eglMakeCurrent(mEglDisplay,mEglSurface,mEglSurface,mEglContext)){
            throw new RuntimeException("eglMakeCurrent fail");
        }


9、重新整理資料,顯示渲染場景

if (mEgl != null){
            return mEgl.eglSwapBuffers(mEglDisplay,mEglSurface);
        }else {
            throw new RuntimeException("egl is null");
        }

 

GLSurfaceView原始碼中三個重要的類的分析:之後的自定義都是借鑑原始碼

GLThread:OpenGL ES的執行執行緒。包含建立EGL環境、呼叫GLRender的onSurfaceCreated、onSurfaceChanged和onDrawFrame方法以及生命週期的管理。繪製需要線上程中。

EglHelper:負責建立EGL環境。

GLSurfaceView:負責提供Surface和狀態改變。

 

2.自定義EglHelper 示例程式碼如下


EglHelper.java


import android.view.Surface;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;

public class EglHelper {
    private EGL10 mEgl;
    private EGLDisplay mEglDisplay;
    private EGLContext mEglContext;
    private EGLSurface mEglSurface;

    public void initEgl(Surface surface , EGLContext eglContext){
        mEgl = (EGL10) EGLContext.getEGL();

        mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        if (mEglDisplay == EGL10.EGL_NO_DISPLAY){
            throw new RuntimeException("eglGetDisplay failed");
        }

        int[] version = new int[2];
        if (!mEgl.eglInitialize(mEglDisplay,version)){
            throw new RuntimeException("eglInitialize failed");
        }

        int[] attrbutes = 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,attrbutes,null,1,num_config)){
            throw new IllegalArgumentException("eglChooseConfig failes");
        }
        int numConfigs = num_config[0];
        if (numConfigs <= 0){
            throw new IllegalArgumentException("No configs match configSpec");
        }

        EGLConfig[] configs = new EGLConfig[numConfigs];
        if (!mEgl.eglChooseConfig(mEglDisplay,attrbutes,configs,numConfigs,num_config)){
            throw new IllegalArgumentException("eglChooseConfig get failed");
        }

        if (eglContext != null){
            mEglContext = mEgl.eglCreateContext(mEglDisplay,configs[0],eglContext,null);
        }else {
            mEglContext = mEgl.eglCreateContext(mEglDisplay,configs[0],EGL10.EGL_NO_CONTEXT,null);
        }

        mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,configs[0],surface,null);

        if (!mEgl.eglMakeCurrent(mEglDisplay,mEglSurface,mEglSurface,mEglContext)){
            throw new RuntimeException("eglMakeCurrent fail");
        }
    }

    public EGLContext getmEglContext(){
        return mEglContext;
    }

    public boolean swapBuffers(){
        if (mEgl != null){
            return mEgl.eglSwapBuffers(mEglDisplay,mEglSurface);
        }else {
            throw new RuntimeException("egl is null");
        }
    }

    public void destoryEgl(){
        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;
        }
    }

}

使用自定義的EGL環境如下:


import android.opengl.GLES20;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends AppCompatActivity {

    private SurfaceView surfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        surfaceView = (SurfaceView) findViewById(R.id.mysurfaceView);

        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();
                        //建立EGL環境
                        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(0.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) {
                //egl.destroy
            }
        });


    }
}

效果: