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
}
});
}
}
效果: