Querying for OpenGL ES 2.0 Support

From Android Game Development Wiki

Jump to: navigation, search

[edit] The Problem

Since the release on Android NDK r3 developers have access to the OpenGL ES 2.0 API from native code on Android version >= 2.0. Since Froyo (Android 2.2) developers can access the API in Java as well via the GLES20 static class methods.

Before using OpenGL ES 2.0 in native code or via the Java bindings one has to determine whether OpenGL ES 2.0 is supported on the device. If it is one can create a fitting OpenGL ES context.

[edit] The Solution

The maximum OpenGL ES version supported by a device can be queried as follows:

private boolean detectOpenGLES20() 
{
	ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	ConfigurationInfo info = am.getDeviceConfigurationInfo();
	return (info.reqGlEsVersion >= 0x20000);
}

This works on all Android version starting from 2.0 (API level 5). For Android versions <= 2.0 one can either query the Android version installed on the device or use the following snippet:

private boolean checkGL20Support( Context context )
{
	EGL10 egl = (EGL10) EGLContext.getEGL();       
	EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
 
	int[] version = new int[2];
	egl.eglInitialize(display, version);
 
	int EGL_OPENGL_ES2_BIT = 4;
	int[] configAttribs =
	{
		EGL10.EGL_RED_SIZE, 4,
		EGL10.EGL_GREEN_SIZE, 4,
		EGL10.EGL_BLUE_SIZE, 4,
		EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
		EGL10.EGL_NONE
	};
 
	EGLConfig[] configs = new EGLConfig[10];
	int[] num_config = new int[1];
	egl.eglChooseConfig(display, configAttribs, configs, 10, num_config);     
	egl.eglTerminate(display);
	return num_config[0] > 0;
}

[edit] External Links

GLES20Activity.java from the Android API demos project

Inofficial Complete OpenGL ES 2.0 Java Bindings

Personal tools