关于EGL的介绍:
EGL is an interface between Khronos rendering APIs such as OpenGL ES or OpenVG and the underlying native platform window system. It handles graphics context management, surface/buffer binding, and rendering synchronization and enables high-performance, accelerated, mixed-mode 2D and 3D rendering using other Khronos APIs. EGL also provides interop capability between Khronos to enable efficient transfer of data between APIs – for example between a video subsystem running OpenMAX AL and a GPU running OpenGL ES.
EGL can be implemented on multiple operating systems (such as Android and Linux) and native window systems (such as X and Microsoft Windows). Implementations may also choose to allow rendering into specific types of EGL surfaces via other supported native rendering APIs, such as Xlib or GDI. EGL provides:
- Mechanisms for creating rendering surfaces (windows, pbuffers, pixmaps) onto which client APIs can draw and share
- Methods to create and manage graphics contexts for client APIs
- Ways to synchronize drawing by client APIs as well as native platform rendering APIs.
官网:
ConfigChooser
定义配置信息的属性列表:
private static int EGL_OPENGL_ES2_BIT = 4;private static int[] s_configAttribs2 ={ EGL10.EGL_RED_SIZE, 4, EGL10.EGL_GREEN_SIZE, 4, EGL10.EGL_BLUE_SIZE, 4, EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL10.EGL_NONE};
配置EGL_RED_SIZE、EGL_GREEN_SIZE、EGL_BLUE_SIZE和EGL_RENDERABLE_TYPE。
EGL的属性列表:
public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { /* Get the number of minimally matching EGL configurations */ int[] num_config = new int[1]; egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config); int numConfigs = num_config[0]; if (numConfigs <= 0) { throw new IllegalArgumentException("No configs match configSpec"); } /* Allocate then read the array of minimally matching EGL configs */ EGLConfig[] configs = new EGLConfig[numConfigs]; egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config); if (DEBUG) { printConfigs(egl, display, configs); } /* Now return the "best" one */ return chooseConfig(egl, display, configs);}
GLSurfaceView.EGLConfigChooser:
首先,获得调用egl.eglChooseConfig(display, s_configAttribs2, null, 0, num_config);,获得配置信息的数量;
然后,调用egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config);,获得配置信息EGLConfig数组;
最后,调用私有方法chooseConfig(egl, display, configs);,取得最合适的配置信息EGLConfig。
EGL配置管理如下图:
ContextFactory
在EGL2JNIView.java文件中,私有内部类ConfigChooser定义如下:
private static class ContextFactory implements GLSurfaceView.EGLContextFactory { private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098; public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) { Log.w(TAG, "creating OpenGL ES 2.0 context"); checkEglError("Before eglCreateContext", egl); int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE }; EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list); checkEglError("After eglCreateContext", egl); return context; } public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) { egl.eglDestroyContext(display, context); }}
GLSurfaceView.EGLContextFactory:
int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);创建OpenGL ES 2.0版本的EGLContext。