Added in API level 3

GLSurfaceView


open class GLSurfaceView : SurfaceView, SurfaceHolder.Callback2
kotlin.Any
   ↳ android.view.View
   ↳ android.view.SurfaceView
   ↳ android.opengl.GLSurfaceView

An implementation of SurfaceView that uses the dedicated surface for displaying OpenGL rendering.

A GLSurfaceView provides the following features:

  • Manages a surface, which is a special piece of memory that can be composited into the Android view system.
  • Manages an EGL display, which enables OpenGL to render into a surface.
  • Accepts a user-provided Renderer object that does the actual rendering.
  • Renders on a dedicated thread to decouple rendering performance from the UI thread.
  • Supports both on-demand and continuous rendering.
  • Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.

Using GLSurfaceView

Typically you use GLSurfaceView by subclassing it and overriding one or more of the View system input event methods. If your application does not need to override event methods then GLSurfaceView can be used as-is. For the most part GLSurfaceView behavior is customized by calling "set" methods rather than by subclassing. For example, unlike a regular View, drawing is delegated to a separate Renderer object which is registered with the GLSurfaceView using the setRenderer(android.opengl.GLSurfaceView.Renderer) call.

Initializing GLSurfaceView

All you have to do to initialize a GLSurfaceView is call setRenderer(android.opengl.GLSurfaceView.Renderer). However, if desired, you can modify the default behavior of GLSurfaceView by calling one or more of these methods before calling setRenderer:

Specifying the android.view.Surface

By default GLSurfaceView will create a PixelFormat.RGB_888 format surface. If a translucent surface is required, call getHolder().setFormat(PixelFormat.TRANSLUCENT). The exact format of a TRANSLUCENT surface is device dependent, but it will be a 32-bit-per-pixel surface with 8 bits per component.

Choosing an EGL Configuration

A given Android device may support multiple EGLConfig rendering configurations. The available configurations may differ in how many channels of data are present, as well as how many bits are allocated to each channel. Therefore, the first thing GLSurfaceView has to do when starting to render is choose what EGLConfig to use.

By default GLSurfaceView chooses a EGLConfig that has an RGB_888 pixel format, with at least a 16-bit depth buffer and no stencil.

If you would prefer a different EGLConfig you can override the default behavior by calling one of the setEGLConfigChooser methods.

Debug Behavior

You can optionally modify the behavior of GLSurfaceView by calling one or more of the debugging methods setDebugFlags(int), and setGLWrapper. These methods may be called before and/or after setRenderer, but typically they are called before setRenderer so that they take effect immediately.

Setting a Renderer

Finally, you must call setRenderer to register a Renderer. The renderer is responsible for doing the actual OpenGL rendering.

Rendering Mode

Once the renderer is set, you can control whether the renderer draws continuously or on-demand by calling setRenderMode. The default is continuous rendering.

Activity Life-cycle

A GLSurfaceView must be notified when to pause and resume rendering. GLSurfaceView clients are required to call onPause() when the activity stops and onResume() when the activity starts. These calls allow GLSurfaceView to pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate the OpenGL display.

Handling events

To handle an event you will typically subclass GLSurfaceView and override the appropriate method, just as you would with any other View. However, when handling the event, you may need to communicate with the Renderer object that's running in the rendering thread. You can do this using any standard Java cross-thread communication mechanism. In addition, one relatively easy way to communicate with your renderer is to call queueEvent(java.lang.Runnable). For example:

class MyGLSurfaceView extends GLSurfaceView {
 
      private MyRenderer mMyRenderer;
 
      public void start() {
          mMyRenderer = ...;
          setRenderer(mMyRenderer);
      }
 
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
              queueEvent(new Runnable() {
                  // This method will be called on the rendering
                  // thread:
                  public void run() {
                      mMyRenderer.handleDpadCenter();
                  }});
              return true;
          }
          return super.onKeyDown(keyCode, event);
      }
  }
  

Summary

Nested classes
abstract

An interface for choosing an EGLConfig configuration from a list of potential configurations.

abstract

An interface for customizing the eglCreateContext and eglDestroyContext calls.

abstract

An interface for customizing the eglCreateWindowSurface and eglDestroySurface calls.

abstract

An interface used to wrap a GL interface.

abstract

A generic renderer interface.

Inherited XML attributes
Constants
static Int

Check glError() after every GL call and throw an exception if glError indicates that an error has occurred.

static Int

Log GL calls to the system log at "verbose" level with tag "GLSurfaceView".

static Int

The renderer is called continuously to re-render the scene.

static Int

The renderer only renders when the surface is created, or when requestRender is called.

Inherited constants
Public constructors

Standard View constructor.

GLSurfaceView(context: Context!, attrs: AttributeSet!)

Standard View constructor.

Public methods
open Int

Get the current value of the debug flags.

open Boolean

open Int

Get the current rendering mode.

open Unit

Pause the rendering thread, optionally tearing down the EGL context depending upon the value of setPreserveEGLContextOnPause(boolean).

open Unit

Resumes the rendering thread, re-creating the OpenGL context if necessary.

open Unit

Queue a runnable to be run on the GL rendering thread.

open Unit

Request that the renderer render a frame.

open Unit
setDebugFlags(debugFlags: Int)

Set the debug flags to a new value.

open Unit

Install a custom EGLConfigChooser.

open Unit

Install a config chooser which will choose a config as close to 16-bit RGB as possible, with or without an optional depth buffer as close to 16-bits as possible.

open Unit
setEGLConfigChooser(redSize: Int, greenSize: Int, blueSize: Int, alphaSize: Int, depthSize: Int, stencilSize: Int)

Install a config chooser which will choose a config with at least the specified depthSize and stencilSize, and exactly the specified redSize, greenSize, blueSize and alphaSize.

open Unit

Inform the default EGLContextFactory and default EGLConfigChooser which EGLContext client version to pick.

open Unit

Install a custom EGLContextFactory.

open Unit

Install a custom EGLWindowSurfaceFactory.

open Unit

Set the glWrapper.

open Unit

Control whether the EGL context is preserved when the GLSurfaceView is paused and resumed.

open Unit
setRenderMode(renderMode: Int)

Set the rendering mode.

open Unit

Set the renderer associated with this view.

open Unit
surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int)

This method is part of the SurfaceHolder.

open Unit

This method is part of the SurfaceHolder.

open Unit

This method is part of the SurfaceHolder.

open Unit

This method is part of the SurfaceHolder.

open Unit

This method is part of the SurfaceHolder.

Protected methods
open Unit

open Unit

This method is used as part of the View class and is not normally called or subclassed by clients of GLSurfaceView.

open Unit

Inherited functions