Android EGL環(huán)境的搭建

Android EGL環(huán)境的搭建

1.概述

1.1 EGL是什么?

???????想要了解EGL是什么,我覺得我們首先就必須要了解什么是OpenGLES。通俗上講,OpenGL是一個操作GPU的API,它通過驅(qū)動向GPU發(fā)送相關(guān)指令,控制圖形渲染管線狀態(tài)機(jī)的運(yùn)行狀態(tài)。但OpenGL需要本地視窗系統(tǒng)進(jìn)行交互,這就需要一個中間控制層,最好與平臺無關(guān)。EGL——因此被獨(dú)立的設(shè)計(jì)出來,它作為OpenGL ES和本地窗口的橋梁。EGL 是 OpenGL ES(嵌入式)和底層 Native 平臺視窗系統(tǒng)之間的接口。EGL API 是獨(dú)立于OpenGL ES各版本標(biāo)準(zhǔn)的獨(dú)立API ,其主要作用是為OpenGL指令創(chuàng)建 Context 、繪制目標(biāo)Surface 、配置Framebuffer屬性、Swap提交繪制結(jié)果等。此外,EGL為GPU廠商和OS窗口系統(tǒng)之間提供了一個標(biāo)準(zhǔn)配置接口。一般來說,OpenGL ES 圖形管線的狀態(tài)被存儲于 EGL 管理的一個Context中。而Frame Buffers 和其他繪制 Surfaces 通過 EGL API進(jìn)行創(chuàng)建、管理和銷毀。 EGL 同時(shí)也控制和提供了對設(shè)備顯示和可能的設(shè)備渲染配置的訪問。

1.2 為什要搭建EGL環(huán)境。

???????目前在Android系統(tǒng)上已經(jīng)有了GLSurfaceView供我們?nèi)粘J褂?,但是在一些特定的環(huán)境下系統(tǒng)提供的GLSurfaceView就無能為力了,比如說一個畫面同時(shí)渲染到多個SurfaceView上,這個時(shí)候就需要我們自己搭建EGL環(huán)境,進(jìn)行EGLContext的共享來達(dá)到畫面在多個SurfaceView之間共享的目的。除了這種需求之外,從0搭建EGL環(huán)境也是最快了解EGL的方式之一,我們要有造輪子的精神。

2.EGL環(huán)境的搭建

2.1 獲取EGL實(shí)例

//1.得到Egl實(shí)例
EGL10 mEgl = (EGL10) EGLContext.getEGL();

2.2 獲取EGL實(shí)例

//2.得到默認(rèn)的顯示設(shè)備
EGLDisplay mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

2.3 初始化EGL

 int[] version=new int[2];//該數(shù)組用于存放EGL初始化完畢后系統(tǒng)返回的EGL的主版本號和次版本號,int[0]為主版本號,int[1]為子版本號
 boolean initialize = mEgl.eglInitialize(mEglDisplay, version);

2.4 設(shè)置顯示設(shè)備的屬性

/4.設(shè)置顯示設(shè)備的屬性

int[] attribute=new int[] {
                EGL10.EGL_RED_SIZE, 8,//紅色
                EGL10.EGL_GREEN_SIZE, 8,//綠色
                EGL10.EGL_BLUE_SIZE, 8,//藍(lán)色
                EGL10.EGL_ALPHA_SIZE, 8,//通明度
                EGL10.EGL_DEPTH_SIZE, 8,//深度
                EGL10.EGL_STENCIL_SIZE, 4,//模板(和3d有關(guān))
                EGL10.EGL_NONE};//屬性結(jié)束符號
//根據(jù)屬性信息從系統(tǒng)的所有的配置信息中,獲取支持該屬性列表的配置信息的個數(shù)。一般來說就選取一個就好了
 int[] num_config = new int[1];
 boolean chooseConfig = mEgl.eglChooseConfig(mEglDisplay, attribute,
 null, //null的意思就是我們獲取的配置信息不進(jìn)行存儲
1, //獲取所需屬性的個數(shù)
num_config //存儲屬性返回值個數(shù)的數(shù)組
);

2.5 從系統(tǒng)中獲取對應(yīng)屬性的配置

EGLConfig[] eglConfigs=new EGLConfig[num_config[0]];
boolean eglChooseConfig = mEgl.eglChooseConfig(mEglDisplay, attribute, eglConfigs, num_config[0], num_config);

2.6 創(chuàng)建EglContext

    //如果eglContext==null則創(chuàng)建新的egl上下文
    //設(shè)置EGL的客戶端的版本號,如果不設(shè)置無法繪制出紋理
    int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2,
            EGL10.EGL_NONE };
    if (eglContext==null){
        mEglContext = mEgl.eglCreateContext(mEglDisplay, eglConfigs[0], EGL_NO_CONTEXT, null);

    }else {
        //根據(jù)傳入eglContext創(chuàng)建可以共享的egl上下文
        mEglContext = mEgl.eglCreateContext(mEglDisplay, eglConfigs[0], eglContext, null);
    }

2.7 創(chuàng)建surface

    eglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, eglConfigs[0], surface, null);

2.8.綁定EGLContext和surface到顯示設(shè)備

    boolean makeCurrent = mEgl.eglMakeCurrent(mEglDisplay, eglSurface, eglSurface, mEglContext);
    if (!makeCurrent){
        Log.e("ike","eglMakeCurrent failed");
    }

2.9 刷新數(shù)據(jù)

mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);

3 完整示例代碼


import android.icu.text.UFormat;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceView;

import javax.microedition.khronos.egl.EGL;
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;

import static javax.microedition.khronos.egl.EGL10.EGL_NO_CONTEXT;

/**
* author ike
* create time 21:16 2019/9/25
* function: EGL Helper
**/
public class EGLHelper {
    private EGL10 mEgl;
    private EGLDisplay mEglDisplay;
    private EGLContext mEglContext;
    private EGLSurface eglSurface;

    public void init(Surface surface, EGLContext eglContext){
        //1.得到Egl實(shí)例
        mEgl = (EGL10) EGLContext.getEGL();
        //2.得到默認(rèn)的顯示設(shè)備(就是窗口)
        mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        //判斷獲取默認(rèn)顯示設(shè)備是否成功
        if (mEglDisplay==EGL10.EGL_NO_DISPLAY){
            throw new RuntimeException("eglGetDisplay failed");
        }
        //3.初始化默認(rèn)顯示設(shè)備(初始化EGL)
        //主版本號和次版本號
       int[] version=new int[2];
        boolean initialize = mEgl.eglInitialize(mEglDisplay, version);
        if (!initialize){
            throw new RuntimeException("eglInitialize failed");
        }
        Log.e("ike","version:"+version[0]);
        Log.e("ike","version:"+version[1]);

        //4.設(shè)置顯示設(shè)備的屬性
        int[] attribute=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, 4,
                EGL10.EGL_NONE};
        //根據(jù)屬性信息從系統(tǒng)的所有的配置信息中,獲取支持該屬性列表的配置信息的個數(shù)。一般來說就選取一個就好了
        int[] num_config = new int[1];
        boolean chooseConfig = mEgl.eglChooseConfig(mEglDisplay, attribute, null, 1, num_config);
        if (!chooseConfig){
            throw new RuntimeException("eglChooseConfig failed");

        }
        //判斷是否選擇到符合傳入?yún)?shù)的配置信息
        if (num_config[0]<=0){
            throw new IllegalArgumentException(
                    "No configs match configSpec");
        }
        //5.從系統(tǒng)中獲取對應(yīng)屬性的配置
        EGLConfig[] eglConfigs=new EGLConfig[num_config[0]];
        boolean eglChooseConfig = mEgl.eglChooseConfig(mEglDisplay, attribute, eglConfigs, num_config[0], num_config);
        if (!eglChooseConfig){
            throw new RuntimeException("eglChooseConfig$2 failed");
        }
        //6. 創(chuàng)建EglContext
        //如果eglContext==null則創(chuàng)建新的egl上下文
        int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2,
            EGL10.EGL_NONE };
        if (eglContext==null){
            mEglContext = mEgl.eglCreateContext(mEglDisplay, eglConfigs[0], EGL_NO_CONTEXT, null);

        }else {
            //根據(jù)傳入eglContext創(chuàng)建可以共享的egl上下文
            mEglContext = mEgl.eglCreateContext(mEglDisplay, eglConfigs[0], eglContext, null);
        }
        if (mEglContext==null||mEglContext== EGL_NO_CONTEXT){
            mEglContext=null;
            throw new RuntimeException("eglCreateContext failed");
        }
        //7.創(chuàng)建surface
        eglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, eglConfigs[0], surface, null);
        if (eglSurface==null||eglSurface==EGL10.EGL_NO_SURFACE){
            eglSurface=null;
            throw new RuntimeException("eglCreateWindowSurface failed");
        }
        //8.綁定EGLContext和surface到顯示設(shè)備
        boolean makeCurrent = mEgl.eglMakeCurrent(mEglDisplay, eglSurface, eglSurface, mEglContext);
        if (!makeCurrent){
            Log.e("ike","eglMakeCurrent failed");
        }
    }

    /**
     * 刷新數(shù)據(jù)
     */
    public void swapBuffers(){
        if (mEgl!=null){
            boolean eglSwapBuffers = mEgl.eglSwapBuffers(mEglDisplay, eglSurface);
            if (!eglSwapBuffers){
                Log.e("ike","eglSwapBuffers failed");

            }
        }
    }

    /**
     * 獲取EGL上下文
     * @return
     */
    public EGLContext getEGLCOntext(){
        return mEglContext;
    }
    public void destoryEgl(){
       if (mEgl!=null){
           //與顯示設(shè)備解綁,銷毀eglsurface
           mEgl.eglMakeCurrent(mEglDisplay,EGL10.EGL_NO_SURFACE,EGL10.EGL_NO_SURFACE,EGL_NO_CONTEXT);
           mEgl.eglDestroySurface(mEglDisplay,eglSurface);
           eglSurface=null;
           //銷毀上下文
           mEgl.eglDestroyContext(mEglDisplay,mEglContext);
           mEglContext=null;
           //銷毀顯示設(shè)備
           mEgl.eglTerminate(mEglDisplay);
           mEglDisplay=null;
           mEgl=null;
       }
    }
}

4 結(jié)語

本文代碼多數(shù)是從GLSurfaceView中提取而出,里面有完整的實(shí)例過程。如有錯誤的地方,希望不吝指出,要是有贊就更好了。睡覺?。?!
參考資料 https://blog.csdn.net/qq_38261174/article/details/84102154

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容