Improving SurfaceView FPS

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Improving SurfaceView FPS

Postby Izzy545 » Sun May 30, 2010 7:11 am

Hi all,

I've been working on a sidescrolling game for Android phones and have run into problems in making it run efficiently. The most annoying bug is when starting the game it pauses seemingly at random when changing the content view from the menu to the view where the SurfaceView resides. I don't know how to judge why this is happening. Perhaps it's when registering the sensor listener?

Beyond that, I'm getting around 40 FPS when drawing one layer of tiles on the screen, the player, and one enemy. This seems quite low, and drops to around 25-30 when drawing a 320x240 background and three 150x50 clouds on the screen. Adding a second layer of tiles would drop it to below 20 which seems unacceptable. Is this normal for Android games?

My screen is using 16x16 tiles by running through a 2D array, but only the pertinent tiles are being accessed and drawn through the array. I don't create any new objects in functions (most specifically, draw and update functions), only use existing ones belonging to each class in my game.

What sort of things can I do to increase performance in SurfaceView games in Android?
Posts: 3
Joined: Mon Apr 26, 2010 9:45 pm


Re: Improving SurfaceView FPS

Postby astrath » Mon May 31, 2010 7:33 pm

One rather quick thing you could try is employ the method home computer games used in the 80es (yep, I'm that old).
Draw the current tiled background into a bitmap +1 or +2 tiles at every direction you want to scroll into (so it's a rect which is 1 or 2 tiles bigger then the screen). Then just draw this one bitmap and scroll it until you hit it's border then at this moment redraw the bitmap. As you are no longer drawing individual tiles this should somewhat improve the fps, only problem which could arise is that the redraw of the bitmap every x tiles is too noticeable a drop in the framerate.

Apart from this this seems like the perfect case for the famoust Google IO video about Replica Island:

Although I have to say that from my own experience the performance improvement gained by moving to OpenGL is quite a bit less impressive then this video makes it out to be.
Using Canvas is much less of a hassle and is guaranteed to work on every device. I would recommend first trying to optimize the hell out of your game. Personally I have found that with the abysmal DalvikVM the smallest things can eat up a ton of CPU time.
Junior Developer
Junior Developer
Posts: 12
Joined: Thu Apr 08, 2010 1:03 pm

Re: Improving SurfaceView FPS

Postby IronChef253 » Sun Jun 13, 2010 7:34 am

Astrath is right in suggesting you go look at the google io video...start there.

My input is for your comment "I don't know how to judge why my game is slow."

Google included something called "TraceView" with Android (located in the tools folder) that can help you by providing graphical profiles of what is slowing you down. To implement TraceView, you have to include the Debug class and then start it when your game starts (just do a search for trace view in google) and it will output a trace file you can read with the traveciew tool. Innparticular, the profile panel might help find what is slowing you down...
Junior Developer
Junior Developer
Posts: 11
Joined: Sun Jun 13, 2010 6:51 am

Re: Improving SurfaceView FPS

Postby KarlKarlsom » Sun Jun 20, 2010 8:27 pm

I hade similar Problems:
At my very first atempt to make a game I had a Framerate of 20fps on the milestone. After optimizing whatever I found I now got it to 50fps.
Some of the steps I did:
-dont do anything that would cause the GC to collect memory
-like astrath suggested: draw as much as possible before and only draw in you gameloop everything together (pre draw Bitmaps, means exchange memory usage with speed
-several 1 dimensional arrays are faster than 1x2 dimensional (especially this speeded my app up like hell... :)
Experienced Developer
Experienced Developer
Posts: 55
Joined: Mon Dec 14, 2009 8:40 pm

Re: Improving SurfaceView FPS

Postby RedToasty » Thu Jul 08, 2010 10:15 pm

Check out your render states are all set up as tightly as you can go as well, lighting disabled, depth testing disabled (unless you're relying on it, you should be able to just render in order for 2d though), ensure textures are being loaded as 16 bit, blending disabled when possible, changing states and binding textures as little as possible in the draw cycle.

You've used some curious sizes for textures, try and snap them all to powers of 2 to make sure they're not sitting in bigger allocations of memory when uploaded, the 150x150s down to 128x128 for example. 256x256 for the background?

Stating the obviously slightly too, but I take it you're also culling any textures yourself which would be off the side of the screen? There's no point even setting everything up to render if it's going to be off the side.
Posts: 9
Joined: Thu Jun 17, 2010 7:27 pm

Re: Improving SurfaceView FPS

Postby asystat » Mon Sep 27, 2010 3:52 pm

-Avoid divisions (find a way to calculate or precalculate divisions)
-In your gameloop avoid getting random numbers (r.nextint...) prebuild a random array instead
-use unidimensional arrays
-don't use the activity context. use the application context instead. (getApplicationContext())
-when loading the images, don't load all images at once in static vars. Load just the bitmaps you need and be sure to release the instance when you don't need them anymore.

Use the Android NDK for heavy methods.
Posts: 3
Joined: Mon Aug 16, 2010 3:35 pm


Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests