Associating a Drawing Surface with OpenGL ES Through the EGL Context

All of the Android SDK's samples use a view-related class called android.view.SurfaceHolder in order to draw using OpenGL ES. This class is closely related to android.view.SurfaceView, which is a variant of a regular view class that allows drawing from a separate thread. The documentation and samples don't make it clear whether you can use any other type of view for OpenGL drawing, so we'll stick to this android.view.SurfaceHolder object. Even if subsequent releases of Android were to allow another simpler object for this purpose, you'd just need to alter the object name and the rest of the explanation should still be valid. SurfaceHolder is also the recommended class to use for high-performance drawing, so learning about it should prove helpful to you in any case.

In the "Creating and Using the OpenGL Test Harness" section, we will give you the code necessary to get a SurfaceView and then get a SurfaceHolder from that, but for now assume that you have already obtained a SurfaceView. We'll explain how you use the SurfaceHolder object through the SurfaceView to get a reference to the OpenGL ES interface (see Listing 10-3).

Listing 10-3. Getting a Reference to the OpenGL ES Interface android.view.SurfaceHolder holder = surfaceView.getHolder();

// mEgl points to an EGL context interface EGL10 mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, mEglConfig, holder, null);

mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext); GL gl = mEgl.getGL();

In the preceding "Getting an EGL Context" subsection, we showed you how to obtain a reference to the variable mEgl. You will need to create this EGL context only once for an activity and keep it for the life of that activity.

The code at the beginning of Listing 10-3 gets a SurfaceHolder object from a previously obtained SurfaceView (getting the SurfaceView itself is not shown in Listing 10-3). You then pass the SurfaceHolder object to the eglCreateWindowSurface method to bind the SurfaceView to OpenGL ES for drawing.

You then use the eglMakeCurrent method to activate the drawing. Once that's done, you use the getGL method to get a reference to the OpenGL ES interface.

After executing the code in Listing 10-3, you'll have a reference to the OpenGL ES interface, which is identified by GL. This interface contains all the standard OpenGL ES APIs.

Note that as the window surface changes, either because of resizing or other reasons, you need to disassociate and reassociate the window surface so that OpenGL ES can readjust its internal configuration in response to a changed screen environment (see Listing 10-4).

Listing 10-4. Associating and Disassociating the Window Surface mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,

EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT); mEgl.eglDestroySurface(mEglDisplay, mEglSurface);

0 0


  • colin watt
    How to draw on surface view using egl?
    7 years ago

Post a comment