Getting an EGL Context

In the Android SDK distribution, the EGL API is available in the package javax.microedition. khronos.egl.EGL10. You can read more about using this package at javame/reference/apis/jsr239/javax/microedition/khronos/egl/EGL10.html.

Note Although this URL points to a web site about Java, it's relevant because Android uses the same API. All of the site's information on EGL is applicable to Android OpenGL programming as well.

Getting an EGL context involves the following steps:

1. Get an implementation of EGL10.

2. Get a display to use.

3. Initialize the display.

4. Specify a device-specific configuration to EGL.

5. Use an initialized display and a configuration to get an EGL context.

Once you have the context, you can bind the context to a window surface every time a window is created or changed, and then tear it down at the end. We will look at preparing the window surface and tearing it down in the next subsection.

But first take a look at some boilerplate code to get an EGL context (see Listing 10-2). Please note that we're providing this code for illustration purposes only; it's not meant to be compiled. You can compile the version of it that we'll use in the test harness later.

Listing 10-2. Sample Code to Get an EGL Context

//Ask for an implementation of EGL10

EGL10 mEgl = (EGL10) EGLContext.getEGL();

//get the default display

EGLDisplay mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

//initialize the display int[] version = new int[2]; mEgl.eglInitialize(mEglDisplay, version);


EGLConfig[] configs = new EGLConfig[1]; int[] num_config = new int[1];

mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, num_config); mEglConfig = configs[0];

//Create EGL Context mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig, EGL10.EGL_NO_CONTEXT, null);

The code in Listing 10-2 is pretty standard for establishing a working EGL context, except for the part where a drawing configuration could differ depending on an application. The method getEGL returns an implementation for the EGL10 interface. The rest of the methods use this EGL10 interface in an implementation-independent manner to get to the EGL context.

The method eglGetDisplay returns a default display to connect to, if an EGL_DEFAULT_ DISPLAY constant is passed in. The eglInitialize method initializes the display and returns major and minor version numbers of the OpenGL implementation.

The next method, eglChooseConfig, is more involved. This method wants you to specify the types of things that are critical to you as you draw. For example, if you want color sizes with a bit depth of 8, you might use this configuration spec:


And here's a suitable configuration spec in which the depth is zero:


Refer to an OpenGL book to get a better understanding of configuration management under OpenGL. You can also check out the Android SDK OpenGL samples to get a feel for possible configurations. Based on these configuration specs, the EGL10 implementation returns a series of suitable EGLConfig references. In this case, the first configuration is chosen.

Finally, you get the needed EGL context by passing an EGLDisplay and an EGLConfig to eglCreateContext. The third argument of the eglCreateContext method indicates sharing (see Listing 10-2). Here, we've used EGL10.EGL_NO_CONTEXT as the third argument to specify that we don't want to share objects with any other context. The last argument is a set of additional attributes, which we have specified as null.

Once you have this EGL context, you can use it to associate a drawing surface with the OpenGL ES interface. This interface allows you to call the OpenGL drawing methods. In the next subsection, you'll learn how to set up this association.

0 0


  • ilmari
    How to set background image in egl10 in android?
    6 years ago
  • eliseo
    How to gwt context3d on android?
    2 years ago

Post a comment