Thursday, January 22, 2009

Android 3D graphics and SurfaceView

If you want to use OpenGL/ES, you have to use a SurfaceView class. The problem is that there are various parameters for the SurfaceView class. This, for example may work fine in the emulator:

getHolder().setType(android.view.SurfaceHolder.SURFACE_TYPE_NORMAL);

on a real device, however, you may get a blank screen, or very slow redraws, so you should use this:

getHolder().setType(android.view.SurfaceHolder.
SURFACE_TYPE_GPU);

or even this:

getHolder().setType(android.view.SurfaceHolder.
SURFACE_TYPE_HARDWARE);

So i came up with this solution for the SurfaceView initialization:

public GameSurfaceView(Context context,GameOverlay overlay,Sound soundManager)
{
super(context);
this.soundManager = soundManager;
this.context = context;
this.overlay = overlay;
this.viewType = GLThread.VIEW_INTRO;
this.gameType = Game.GAME_MONOLITH;
getHolder().addCallback(this);
try
{
getHolder().setType(android.view.SurfaceHolder.
SURFACE_TYPE_GPU);
}
catch(Exception e)
{
try
{
getHolder().setType(android.view.SurfaceHolder.
SURFACE_TYPE_HARDWARE);
}
catch(Exception e2)
{
try
{
getHolder().setType(android.view.SurfaceHolder.SURFACE_TYPE_NORMAL);
}
catch(Exception e3)
{
}
}
}
}

So in the code above, we first try to get a hardware accelerated surface (fastest), if that fails a gpu memory surface(still fast) and if that fails a normal software surface.

2 comments:

nEx.Software said...

The source version doesn't run on a G1. The OpenGL surface never gets initialized. This is not the case for downloadable APK you have posted, which works. Did you do something different?

Tasos Kleisas said...

I think that this particular blog entry is wrong. The apk 1.0.4 beta, uses the line getHolder.setType(SURFACE_TYPE_GPU); only. This works on the G1 and on most emulator setups and should be used. However, it did not work correctly in an ubuntu linux emulator setup. It works fine on windows and macosx setups though. I will update the post when I have more information on the matter.

Search the web