# Mesa EGL

The current version of EGL in Mesa implements EGL 1.4. More information about EGL can be found at http://www.khronos.org/egl/.

The Mesa's implementation of EGL uses a driver architecture. The main library (`libEGL`) is window system neutral. It provides the EGL API entry points and helper functions for use by the drivers. Drivers are dynamically loaded by the main library and most of the EGL API calls are directly dispatched to the drivers.

The driver in use decides the window system to support. For drivers that support hardware rendering, there are usually multiple drivers supporting the same window system. Each one of of them supports a certain range of graphics cards.

## Build EGL

1. Run `configure` with the desired state trackers and enable the Gallium driver for your hardware. For example

         $ ./configure --with-state-trackers=egl,es,vega --enable-gallium-{swrast,intel}

   The main library will be enabled by default. The `egl` state tracker is needed by a number of EGL drivers. EGL drivers will be covered later. The es state tracker provides OpenGL ES 1.x and 2.x and the vega state tracker provides OpenVG 1.x.

2. Build and install Mesa as usual.

In the given example, it will build and install `libEGL`, `libGLESv1_CM`, `libGLESv2`, `libOpenVG`, and one or more EGL drivers.

### Configure Options

There are several options that control the build of EGL at configuration time

- `--enable-egl`

  By default, EGL is enabled. When disabled, the main library and the drivers will not be built.

- `--with-egl-driver-dir`

  The directory EGL drivers should be installed to. If not specified, EGL drivers will be installed to `${libdir}/egl`.

- `--with-egl-displays`

  List the window system(s) to support. It is by default `x11`, which supports the X Window System. Its argument is a comma separated string like, for example, `--with-egl-displays=x11,kms`. Because an EGL driver decides which window system to support, this example will enable two (sets of) EGL drivers. One supports the X window system and the other supports bare KMS (kernel modesetting).

- `--with-state-trackers`

  The argument is a comma separated string. It is usually used to specify the rendering APIs, like OpenGL ES or OpenVG, to build. But it should be noted that a number of EGL drivers depend on the `egl` state tracker. They will not be built without the `egl` state tracker.

- `--enable-gallium-swrast`

  This option is not specific to EGL. But if there is no driver for your hardware, or you are experiencing problems with the hardware driver, you can enable the swrast DRM driver. It is a dummy driver and EGL will fallback to software rendering automatically.

### OpenGL

The OpenGL state tracker is not built in the above example. It should be noted that the classic `libGL` is not a state tracker and cannot be used with EGL (unless the EGL driver in use is `egl_glx`). To build the OpenGL state tracker, one may append `glx` to `--with-state-trackers` and manually build `src/gallium/winsys/xlib/`.

## Use EGL

The demos for OpenGL ES and OpenVG can be found in `progs/es1/`, `progs/es2/` and `progs/openvg/`. You can use them to test your build. For example,

      $ cd progs/es1/xegl
      $ make
      $ ./torus

### Environment Variables

There are several environment variables that control the behavior of EGL at runtime

- `EGL_DRIVERS_PATH`

  By default, the main library will look for drivers in the directory where the drivers are installed to. This variable specifies a list of colon-separated directories where the main library will look for drivers, in addition to the default directory. This variable is ignored for setuid/setgid binaries.

- `EGL_DRIVER`

  This variable specifies a full path to an EGL driver and it forces the specified EGL driver to be loaded. It comes in handy when one wants to test a specific driver. This variable is ignored for setuid/setgid binaries.

- `EGL_DISPLAY`

  When `EGL_DRIVER` is not set, the main library loads all EGL drivers that support a certain window system. `EGL_DISPLAY` can be used to specify the window system and the valid values are, for example, `x11` or `kms`. When the variable is not set, the main library defaults the value to the first window system listed in `--with-egl-displays` at configuration time.

- `EGL_LOG_LEVEL`

  This changes the log level of the main library and the drivers. The valid values are: `debug`, `info`, `warning`, and `fatal`.

- `EGL_SOFTWARE`

  For drivers that support both hardware and software rendering, setting this variable to true forces the use of software rendering.

## EGL Drivers

There are two categories of EGL drivers: Gallium and classic.

Gallium EGL drivers supports all rendering APIs specified in EGL 1.4. The support for optional EGL functions and EGL extensions is usually more complete than the classic ones. These drivers depend on the `egl` state tracker to build. The available drivers are

- `egl_<dpy>_i915`
- `egl_<dpy>_i965`
- `egl_<dpy>_radeon`
- `egl_<dpy>_nouveau`
- `egl_<dpy>_swrast`
- `egl_<dpy>_vmwgfx`

`<dpy>` is given by `--with-egl-displays` at configuration time. There will be one EGL driver for each combination of the displays listed and the hardware drivers enabled.

Classic EGL drivers, on the other hand, supports only OpenGL as its rendering API. They can be found under `src/egl/drivers/`. There are 3 of them

- `egl_glx`

  This driver provides a wrapper to GLX. It uses exclusively GLX to implement the EGL API. It supports both direct and indirect rendering when the GLX does. It is accelerated when the GLX is. As such, it cannot provide functions that is not available in GLX or GLX extensions.

- `egl_dri2`

  This driver supports the X Window System as its window system. It functions as a DRI2 driver loader. Unlike `egl_glx`, it has no dependency on `libGL`. It talks to the X server directly using DRI2 protocol.

- `egl_dri`

  This driver lacks maintenance and does not build. It is similiar to `egl_dri2` in that it functions as a DRI(1) driver loader. But unlike `egl_dri2`, it supports Linux framebuffer devices as its window system and supports EGL_MESA_screen_surface extension. As DRI1 drivers are phasing out, it might eventually be replaced by `egl_dri2`.

To use the classic drivers, one must manually set `EGL_DRIVER` at runtime.

## Developers

The sources of the main library and the classic drivers can be found at `src/egl/`. The sources of the `egl` state tracker can be found at `src/gallium/state_trackers/egl/`.

The suggested way to learn to write a EGL driver is to see how other drivers are written. `egl_glx` should be a good reference. It works in any environment that has GLX support, and it is simpler than most drivers.

### Lifetime of Display Resources

Contexts and surfaces are examples of display resources. They might live longer than the display that creates them.

In EGL, when a display is terminated through `eglTerminate`, all display resources should be destroyed. Similarly, when a thread is released throught `eglReleaseThread`, all current display resources should be released. Another way to destory or release resources is through functions such as `eglDestroySurface` or `eglMakeCurrent`.

When a resource that is current to some thread is destroyed, the resource should not be destroyed immediately. EGL requires the resource to live until it is no longer current. A driver usually calls `eglIs<Resource>Bound` to check if a resource is bound (current) to any thread in the destroy callbacks. If it is still bound, the resource is not destroyed.

The main library will mark destroyed current resources as unlinked. In a driver's `MakeCurrent` callback, `eglIs<Resource>Linked` can then be called to check if a newly released resource is linked to a display. If it is not, the last reference to the resource is removed and the driver should destroy the resource. But it should be careful here because `MakeCurrent` might be called with an uninitialized display.

This is the only mechanism provided by the main library to help manage the resources. The drivers are responsible to the correct behavior as defined by EGL.

### `EGL_RENDER_BUFFER`

In EGL, the color buffer a context should try to render to is decided by the binding surface. It should try to render to the front buffer if the binding surface has `EGL_RENDER_BUFFER` set to `EGL_SINGLE_BUFFER`; If the same context is later bound to a surface with `EGL_RENDER_BUFFER` set to `EGL_BACK_BUFFER`, the context should try to render to the back buffer. However, the context is allowed to make the final decision as to which color buffer it wants to or is able to render to.

For pbuffer surfaces, the render buffer is always `EGL_BACK_BUFFER`. And for pixmap surfaces, the render buffer is always `EGL_SINGLE_BUFFER`. Unlike window surfaces, EGL spec requires their `EGL_RENDER_BUFFER` values to be honored. As a result, a driver should never set `EGL_PIXMAP_BIT` or `EGL_PBUFFER_BIT` bits of a config if the contexts created with the config won't be able to honor the `EGL_RENDER_BUFFER` of pixmap or pbuffer surfaces.

It should also be noted that pixmap and pbuffer surfaces are assumed to be single-buffered, in that `eglSwapBuffers` has no effect on them. It is desirable that a driver allocates a private color buffer for each pbuffer surface created. If the window system the driver supports has native pbuffers, or if the native pixmaps have more than one color buffers, the driver should carefully attach the native color buffers to the EGL surfaces, re-route them if required.

There is no defined behavior as to, for example, how `glDrawBuffer` interacts with `EGL_RENDER_BUFFER`. Right now, it is desired that the draw buffer in a client API be fixed for pixmap and pbuffer surfaces. Therefore, the driver is responsible to guarantee that the client API renders to the specified render buffer for pixmap and pbuffer surfaces.

### `EGLDisplay` Mutex

The `EGLDisplay` will be locked before calling any of the dispatch functions (well, except for GetProcAddress which does not take an `EGLDisplay`). This guarantees that the same dispatch function will not be called with the sample display at the same time. If a driver has access to an `EGLDisplay` without going through the EGL APIs, the driver should as well lock the display before using it.

### TODOs

- Pass the conformance tests
- Better automatic driver selection: `EGL_DISPLAY` loads all drivers and might eat too much memory.