Creating an EGL Display#

The steps shown below demonstrate how to create an EGL display from a native display and initialise the EGL API.

Note

EGL uses the concept of a “display”. This is an abstract object which will show the rendered graphical output. In most environments it corresponds to a single physical screen. After creating a native display for a given windowing system, EGL can use this handle to get a corresponding EGLDisplay handle for use in rendering.

An EGL display can simply be created by calling eglGetDisplay and passing the native display. The native display shown here was already created by the platform-specific wrapper code.

_eglDisplay = eglGetDisplay((EGLNativeDisplayType)_surfaceData.deviceContext);

Should this fail, EGL is usually able to provide access to a default display.

if (_eglDisplay == EGL_NO_DISPLAY)
{
    _eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);
}

If a display still could not be obtained then return an error.

if (_eglDisplay == EGL_NO_DISPLAY)
{
    Log(true, "Failed to get an EGLDisplay");
    return false;
}

EGL has to be initialised with the display obtained in the previous step. All EGL functions, other than eglGetDisplay and eglGetError, need an initialised EGLDisplay.

If an application is not interested in the EGL version number it can just pass NULL for the second and third parameters, but they are queried here for illustrative purposes.

EGLint eglMajorVersion = 0;
EGLint eglMinorVersion = 0;
if (!eglInitialize(_eglDisplay, &eglMajorVersion, &eglMinorVersion))
{
    Log(true, "Failed to initialize the EGLDisplay");
    return false;
}

EGL can interact with other APIs besides OpenGL ES, for example OpenVG or OpenCL. Binding the API means defining which API will subsequently be used.

int result = EGL_FALSE;

result = eglBindAPI(EGL_OPENGL_ES_API);

if (result != EGL_TRUE)
{
    return false;
}

return true;