作为Khronos Group提供的API族的一部分,EGL是一个平台无关、管理绘制界面(drawing surface)的API。它提供如下功能:
1. Communicating with the native windowing system of your system.
EGL是OpenGL ES 2.0与本地窗口系统的粘合剂。在EGL决定使用哪种类型的绘制界面(drawing surface)之前,必须先建立与本地窗口系统的连接。而建立EGL与本地窗口系统的连接,可通过调用以下两个函数实现:
/*
basic opaque type : EGLDisplay
for portability : EGL_DEFAULT_DISPLAY
open a connection to the EGL display server
*/
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
/*
initializes EGL’s internal data structures and returns the major and minor version numbers of the EGL implementation
*/
EGLint majorVersion;
EGLint minorVersion;
eglInitialize(display, &majorVersion, &minorVersion)
2. Querying the available types and configurations of drawing surfaces.
完成了EGL的初始化后,需进一步确认渲染界面的类型与配置。配置对应EGLConfig结构,它包含了界面及其特征信息,如RGB颜色比特数,及是否有深度缓冲区。有两种确认方法:
2.1 遍历所有界面的配置项,找出最佳界面。
先通过eglGetConfigs获取所有本地窗口系统支持的EGLConfig:
/*
若configs设置为NULL,则该函数只支持查询可用EGLConfig的数量,而无具体EGLConfig信息
*/
EGLBoolean eglGetConfigs
(
EGLDisplay display,
EGLconfig* configs,
EGLint maxReturnConfigs,
EGLint* numConfigs
);
再通过eglGetConfigAttrib来查询EGLConfig中特定attribute对应的值:
EGLBoolean eglGetConfigAttrib
(
EGLDisplay display,
EGLConfig config,
EGLint attribute,
EGLint *value
);
2.2 给出明确的配置项,让EGL推荐出最匹配的界面
/*
1. attribList以EGL_NONE结束,属性项很多,具体可参见P40
2. 若返回结果有多个,则遵循一定的排序策略,具体可参见P42
3. 若未设置EGL_SURFACE_TYPE属性,EGL默认将会创建on-screen windows
*/
EGLBoolean eglChooseConfig
(
EGLDispay display,
const EGLint *attribList,
EGLConfig *config,
EGLint maxReturnConfigs,
EGLint *numConfigs
);
3. Creating drawing surfaces.
3.1 Creating an On-Screen Rendering Area: The EGL Window
/*
对于OpenGL ES 2.0,绘制界面仅支持双缓冲机制
attribList:
key : EGL_RENDER_BUFFER
value: EGL_BACK_BUFFER
*/
EGLSurface eglCreateWindowSurface
(
EGLDisplay display,
EGLConfig config,
EGLNatvieWindowType window,
const EGLint *attribList
);
3.2 Creating an Off-Screen Rendering Area: EGL Pbuffers
pbuffers是pixel buffer的简称,可以充分利用硬件加速的优势,它经常被用来生成纹理映射。
生成pbuffers与生成EGL window几乎一样,除了
- EGL_SURFACE_TYPE属性的值要设置为EGL_PBUFFER_BIT
/*
atrribList可另外设置新的属性,参见P47
*/
EGLSurface eglCreatePbufferSurface
(
EGLDisplay display,
EGLConfig config,
const EGLint *attribList
);
4. Creating a rendering context.
EGLContext eglCreateContext
(
EGLDisplay display,
EGLConfig config,
EGLContext shareContext,
const EGLint* attribList
);
5. Making an EGLContext Current
EGLBoolean eglmakeCurrent
(
EGLDisplay display,
EGLSurface draw,
EGLSurface read,
EGLContext context
);