Sunday, October 3, 2010

OpenGL For Android - GLSurfaceView

One of the fascinating features of Android platform is its support for OpenGL. OpenGL is a cross-platform 2D and 3D rendering graphics library. An industry consortium, called Khronos Group maintains the OpenGL specification.

OpenGL uses C-based flat APIs and hence we need to have a Java language binding to be able to use it with Android Application framework. Java ME has already defined this binding with JSR 231. (Java binding for OpenGL 1.5). What that means for you is - 
import javax.microedition.khronos.opengles.GL10;

Let's get started with the code. 
The main class we use is GLSurfaceView which extends from SurfaceView class. GLSurfaceView
  • Provides a Surface onto which we can render OpenGL scenes
  • Binds with customized Renderer which performs the actual drawing
  • Manages EGL display 
  • Uses a dedicated GUI thread, decouples the main application thread for user interactions
We need to call one method on its object i.e setRenderer() which registers a user specified Renderer object that knows how to draw the desired shapes. 
Activity's onCreate() will look like this - 
    public void onCreate(Bundle savedInstanceState) {
   GLSurfaceView view = new GLSurfaceView(this);
     view.setRenderer(new SimpleRenderer());
The next thing we need is SimpleRenderer which will be responsible for actual OpenGL rendering. It implements GLSurfaceView.Renderer interface and defines its three methods.

public abstract void onSurfaceCreated (GL10 gl, EGLConfig config)

This method is called at the very beginning e.g when a Surface is created or recreated ( when phone awakes after sleep), it's a good place for all initialization code. 

A typical initialization sequence - 
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  // Set the background color to black (RGBA).
  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
                // enable smooth shading
         // set up depth buffer and enable depth testing
  // use nice perspective calculations.

public abstract void onSurfaceChanged (GL10 gl, int width, int height)

This method is called when the Surface size changes. The size (width, height) affects the aspect ratio so this function should contain the code that depends upon aspect ratio such as GLU.gluPerspective() . A sample code - 
 public void onSurfaceChanged(GL10 gl, int width, int height) {  
  gl.glViewport(0, 0, width, height);
  GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,

public abstract void onDrawFrame (GL10 gl)

This method is called when the current frame is being drawn on the Surface and thus actual drawing code goes here. e.g 

 public void onDrawFrame(GL10 gl) {
  // Clears the color and depth buffer. -- clears the framebuffer
                gl.glTranslatef(0, 0, -5); 
  // Draw our square.
  /* square.draw() */
This code will create a blank frame with black color. We will see the actual drawing code in the part 2. So stay tuned!


  1. Can you explain the things under the hood - meaning how the SurfaceFlinger will take care of these gl calls?

  2. @Kinjal, ideally you should answer this question! But if I understand correctly, SurfaceFlinger creates a new Surface backed by two buffers in PMEM so that gl can directly work on this memory.

  3. Is supporting OpenGL or OpenGL ES? Also any plans in pipeline for supporting 2.0?

  4. @Zaphod It specifically supports OpenGL ES. This example was to show it's possible to have graphics without going through EGL which is required for ES

    Android supports hardware binding for 2.0 since donut release and there are some efforts to support software renderer too. They already have a class
    but I haven't tried yet and definitely not programmable shader capability!

  5. Cool just look through the reference, so they are expecting/implementing shader compiler and optimizer on phone hardware interesting! Bit different from D3D approach and also from IPhone approach as Cupertino has control over HW hence can always compile shaders offline.

  6. Interesting ... thanks for the insight :)