Note
This article is under construction.
Khronos Group publishes a specification called EGL, which is an API that handles (among other tasks) graphics context creation, rendering surface management, and interop between different Khronos Group graphics APIs (OpenGL, OpenGL ES, OpenVG). For detailed information, see the Khronos EGL webpage.
Currently, EGL is not very widely used across operating systems/graphics driver vendors. The most notable adoption is in the Android architecture, where EGL is the primary method for creating rendering contexts for OpenGL ES 1&2 when using the Android NDK. Also, Mesa has an implementation of the EGL specification in its graphics driver.
Emscripten also supplies an implementation of the EGL v1.4 specification. This allows C/C++ client code to use a (nearly) unified codebase for creating a GLES2 (WebGL) rendering context across Web, Linux (with Mesa) and Android NDK. The implementation of the EGL specification in Emscripten is not perfect, see the end of this page for a status chart.
Somewhat disappointingly, EGL is not a self-sufficient complete solution for initializing GLES2 graphics rendering (on any platform, not just Emscripten) and overseeing various associated tasks. The specification is limited in its scope and lacks some features. In particular, EGL cannot help with the following tasks:
Therefore, for each platform, including Emscripten, there exists platform-specific means to perform these tasks.
In the web environment, WebGL is the technology used for 3D-accelerated rendering. WebGL is almost identical to GLES2, and because EGL does not apply at all for WebGL, for all purposes in this page, the terms WebGL and GLES2 are used interchangeably. Therefore to create a WebGL context, one uses EGL, and according to its wording, creates a GLES2 context.
Perform the following steps to create a GLES2 context using EGL:
After these steps, you have a set of EGL objects EGLDisplay, EGLConfig, EGLSurface and EGLContext that represent the main GLES2 rendering context.
The sequence to clean up at de-initialization is as follows:
Example code for using EGL to initialize a WebGL context can be found in the sample applications in the emscripten/test/glbook directory, more specifically in the file esUtil.c.
This section lists all EGL v1.4 functions and describes their current implementation status in Emscripten.
eglInitialize, eglGetConfigs, eglQueryContext, eglQueryString, eglQuerySurface, eglGetCurrentContext, glGetCurrentSurface, eglGetCurrentDisplay, eglReleaseThread, eglDestroySurface, eglDestroyContext: Implemented and should work according to the EGL v1.4 specification.
eglSwapBuffers: Implemented, but this function cannot really control the swap behavior under WebGL. Calling this function is optional under Emscripten. In WebGL, the contents of the display are always presented to the screen only after the code yields its execution back to the browser, that is, when you return from the tick callback handler you passed to emscripten_set_main_loop(). The eglSwapBuffers function can however still be used to detect when a GL context loss event occurs.
eglGetDisplay: Implemented according to the specification. Emscripten does not utilize multiple EGLNativeDisplayType objects, so pass in EGL_DEFAULT_DISPLAY here. Emscripten currently actually ignores any value passed in here for Linux emulation purposes, but you should not rely on this in the future.
eglGetError: Implemented according to the specification.
Important
According to the specification, eglGetError reports the single most recent error rather than the list of all previous errors. Don’t call this function in a loop in the same way you would call glGetError.
The following functions are currently not implemented:
Important
Do not call these functions in Emscripten code, or the application will halt on trying to execute an undefined function.
Currently, Emscripten does not implement any extensions in the EGL Extension Registry.