Android EGL環(huán)境的搭建
1.概述
1.1 EGL是什么?
???????想要了解EGL是什么叼丑,我覺得我們首先就必須要了解什么是OpenGLES扯罐。通俗上講,OpenGL是一個操作GPU的API肥照,它通過驅(qū)動向GPU發(fā)送相關(guān)指令脚仔,控制圖形渲染管線狀態(tài)機的運行狀態(tài)。但OpenGL需要本地視窗系統(tǒng)進行交互舆绎,這就需要一個中間控制層鲤脏,最好與平臺無關(guān)。EGL——因此被獨立的設(shè)計出來吕朵,它作為OpenGL ES和本地窗口的橋梁猎醇。EGL 是 OpenGL ES(嵌入式)和底層 Native 平臺視窗系統(tǒng)之間的接口。EGL API 是獨立于OpenGL ES各版本標(biāo)準(zhǔn)的獨立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進行創(chuàng)建、管理和銷毀彻桃。 EGL 同時也控制和提供了對設(shè)備顯示和可能的設(shè)備渲染配置的訪問坛善。
1.2 為什要搭建EGL環(huán)境。
???????目前在Android系統(tǒng)上已經(jīng)有了GLSurfaceView供我們?nèi)粘J褂昧诰欤窃谝恍┨囟ǖ沫h(huán)境下系統(tǒng)提供的GLSurfaceView就無能為力了眠屎,比如說一個畫面同時渲染到多個SurfaceView上,這個時候就需要我們自己搭建EGL環(huán)境肆饶,進行EGLContext的共享來達到畫面在多個SurfaceView之間共享的目的改衩。除了這種需求之外,從0搭建EGL環(huán)境也是最快了解EGL的方式之一驯镊,我們要有造輪子的精神葫督。
2.EGL環(huán)境的搭建
2.1 獲取EGL實例
//1.得到Egl實例
EGL10 mEgl = (EGL10) EGLContext.getEGL();
2.2 獲取EGL實例
//2.得到默認的顯示設(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,//藍色
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的意思就是我們獲取的配置信息不進行存儲
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實例
mEgl = (EGL10) EGLContext.getEGL();
//2.得到默認的顯示設(shè)備(就是窗口)
mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
//判斷獲取默認顯示設(shè)備是否成功
if (mEglDisplay==EGL10.EGL_NO_DISPLAY){
throw new RuntimeException("eglGetDisplay failed");
}
//3.初始化默認顯示設(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中提取而出姊氓,里面有完整的實例過程。如有錯誤的地方喷好,希望不吝指出翔横,要是有贊就更好了。睡覺9=痢:萄洹!
參考資料 https://blog.csdn.net/qq_38261174/article/details/84102154