Advanced AOSP Subsystems
3 min read

EGL

Overview

EGL is an interface between Khronos rendering APIs (such as OpenGL ES) and the underlying native platform windowing system. In Android, EGL is the crucial glue that allows OpenGL ES to render into graphic buffers allocated by the system.

EGL as Platform Abstraction

OpenGL ES is platform-agnostic; it only knows how to draw triangles and execute shaders. It does not know how to allocate memory for a window or how to present a finished frame to a screen. EGL solves this by providing mechanisms to create rendering surfaces, manage rendering contexts, and synchronize drawing with the display hardware.

Android implements the EGL standard, ensuring that game engines and UI toolkits can seamlessly interface with the Android graphics stack.

EGLDisplay, EGLContext, EGLSurface

The EGL API revolves around three core concepts:

  • EGLDisplay: Represents the physical screen or display connection. It is the root object required for any EGL operation.
  • EGLContext: Holds all OpenGL ES state, including shaders, textures, and vertex data. A context must be made "current" on a thread before any OpenGL ES calls can be made.
  • EGLSurface: Represents the drawable destination. It is the memory buffer where the rendering output is written.
// Typical EGL Initialization sequence
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
eglInitialize(display, &major, &minor);

EGLConfig config;
eglChooseConfig(display, attribs, &config, 1, &numConfigs);

EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctxAttribs);
EGLSurface surface = eglCreateWindowSurface(display, config, nativeWindow, NULL);

eglMakeCurrent(display, surface, surface, context);
// ... perform OpenGL ES rendering ...
eglSwapBuffers(display, surface);

Pbuffer and Window Surfaces

EGL supports different types of rendering surfaces:

  • Window Surfaces: The most common surface type in Android. They are backed by a Surface (an IGraphicBufferProducer) that routes the rendered frames to SurfaceFlinger for display composition.
  • Pbuffer Surfaces: Off-screen rendering buffers managed entirely within GPU memory. They are useful for background processing, generating textures dynamically, or headless rendering tasks.

EGL Extensions Important for Android

Android heavily relies on EGL extensions to integrate smoothly with its unique architecture. Some critical extensions include:

  • EGL_KHR_image_base and EGL_KHR_gl_texture_2D_image: Allow sharing graphic buffers efficiently between different processes and APIs (e.g., rendering a video frame from MediaCodec into an OpenGL texture).
  • EGL_ANDROID_presentation_time: Allows the application to specify exactly when a frame should be presented on the screen, which is vital for smooth video playback and UI animations.
  • EGL_ANDROID_get_frame_timestamps: Used by Choreographer and developers to profile rendering latency and optimize frame pacing.

To inspect the EGL configuration and supported extensions on a device:

adb shell dumpsys SurfaceFlinger | grep -i egl

This command helps identify the driver version and the specific extensions supported by the SoC's GPU driver.