After searching, I came to the conclusion that I need a valid GL context before I can query it for its capability. That, in turn requires a Surface, and so on. Basicly, you need to create the OpenGL surface before you can check what it supports.
So here is what I ended up doing: I created a new activity (GraphicChooser, I need to work on my classes names...) that is launched instead of my normal activity. This activity creates a GLSurfaceView, which check the capability of the device in onSurfaceCreated. Depending on what is found, it starts the main activity with some flags about the graphic options to use, then exits. Each activity mode is set to singleTask so quitting one doesn't affect the other and there can only be a single instance of each. For example, after hitting the home button and you restart the GraphicChooser activity, it will fire a new intent to main activity, which is still active but will not create a new one.
It is very crude and there certainly is a better way to do it, but I couldn't find it. The main downside is that each time you start the activity, there is the overhead of creating an extra one.
package com.greencod.pinball.android;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
public class GraphicChooser extends Activity {
private GLSurfaceView mGLView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Graphic Chooser", "onCreate: create view and renderer.");
// get the screen size
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
mGLView = new GLSurfaceView(this);
mGLView.setRenderer(new GraphicChooserRenderer(this, width, height));
setContentView(mGLView);
}
@Override
protected void onResume() {
super.onResume();
Log.d("Graphic Chooser", "onResume: purely for testing purpose.");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("Graphic Chooser", "onDestroy: Bye bye.");
}
static final int GAME_ACTIVITY_REQUEST_CODE = 10;
public void launchGraphics(int type)
{
// launch game activity and kill this activity
Intent i = new Intent(this, PinballActivity.class);
i.putExtra("Graphics", type);
// the activity requested should go in a new task, so even if we are passing
// a request code, we will not get it when the new activity stops, but right now
// as a 'cancel' request. That is ok, just quit this activity then.
startActivityForResult(i, GAME_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == GAME_ACTIVITY_REQUEST_CODE )
finish();
}
}
class GraphicChooserRenderer implements GLSurfaceView.Renderer{
GraphicChooser _activity;
final int _width, _height;
public GraphicChooserRenderer( GraphicChooser activity, int width, int height )
{
_activity = activity;
_width = width;
_height = height;
}
final int GRAPHICS_CANVAS = 0;
final int GRAPHICS_OPENGL_DRAW_TEXTURE = 1;
public void determineGraphicSupport(GL10 gl)
{
int _graphicEngine = GRAPHICS_CANVAS;
String extensions = gl.glGetString(GL10.GL_EXTENSIONS);
// String version = GLES10.glGetString(GL10.GL_VERSION);
String renderer = gl.glGetString(GL10.GL_RENDERER);
boolean isSoftwareRenderer = renderer.contains("PixelFlinger");
boolean supportsDrawTexture = extensions.contains("draw_texture");
int[] arGlMaxTextureSize = new int[1];
gl.glGetIntegerv( GL10.GL_MAX_TEXTURE_SIZE, arGlMaxTextureSize, 0 );
if( !isSoftwareRenderer && supportsDrawTexture && _width >= 480 && _height >= 800
&& arGlMaxTextureSize[0] >= 2048 )
_graphicEngine = GRAPHICS_OPENGL_DRAW_TEXTURE;
else
_graphicEngine = GRAPHICS_CANVAS;
Log.i("pinball", "Graphics using " + (_graphicEngine==GRAPHICS_CANVAS?"Canvas":"OpenGL EOS draw texture")
+ ". OpenGL renderer: " + renderer
+ ". Software renderer: " + isSoftwareRenderer
+ ". Support draw texture: " + supportsDrawTexture
+ ". Texture max size: " + arGlMaxTextureSize[0]
+ ". Resolution: " + _width + "x" + _height );
_activity.launchGraphics(_graphicEngine);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
determineGraphicSupport(gl);
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
}