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:
EGLDisplayobject by calling
eglChooseConfigone or multiple times to find the
EGLConfigthat represents the desired main render target parameters. To examine the attributes of an
EGLSurface) by calling
eglCreateWindowSurfacewith a valid display and config parameters. Set window and attribute list parameters to null.
EGLContext) by calling
eglCreateContext, followed by a call to
eglMakeCurrentto activate the rendering context. When creating the context, specify the context attribute
EGL_CONTEXT_CLIENT_VERSION == 2.
After these steps, you have a set of EGL objects
EGLContext that represent the main GLES2 rendering context.
The sequence to clean up at de-initialization is as follows:
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT).
EGLContextobject by calling
EGLSurfaceobjects by calling
This section lists all EGL v1.4 functions and describes their current implementation status in Emscripten.
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
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.
According to the specification,
eglGetErrorreports 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
eglChooseConfig: Implemented as a stub, but this function does not do searching/filtering, and is at the moment identical to
eglGetConfigAttrib: Implemented. Querying for the attributes
EGL_STENCIL_SIZEcurrently return hardcoded default values (issue #644). The attributes
EGL_MAX_SWAP_INTERVALdon’t currently have any function. Instead, call
emscripten_set_main_loop()to specify the main loop update rate.
eglCreateWindowSurface: Implemented, except it is not possible to call this function multiple times to create multiple render windows.
eglCreateContext: Implemented as a stub. It is not possible to call this function multiple times to create multiple contexts.
eglQueryAPI: Implemented, although these functions have little utility on Emscripten, since only the GLES2 client API is supported.
eglWaitNative: Implemented as no-op functions. These have no meaning on Emscripten.
eglSwapInterval: Implemented as a no-op stub. Currently this function cannot set the vsync interval, or enable/disable it.
eglMakeCurrent: Implemented as a no-op stub.
eglGetProcAddress: Implemented, experimental.
The following functions are currently not implemented:
Do not call these functions in Emscripten code, or the application will halt on trying to execute an undefined function.