Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poor performance of Android Canvas.drawBitmap - switch to OpenGL?

I'm porting a 2D action game from Windows Phone 7 (developed in XNA 4.0) over to Android. I'm using a lot of Canvas.drawBitmap() calls - around 200-300 per frame update - with different Paints for each call to handle varying transparency and colourisation at draw-time. This is managing particle systems and various other overlays and in-game effects as well as a tiled background and in-game sprites. I'm not doing any on-demand resizing or rotating, it's simple src->dest rectangles of the same size.

On WP7 this runs at 30+fps but I'm struggling to get 12fps on my test 'droid hardware (Samsung Galaxy S). This is making the game unplayable. Having profiled the code, I've confirmed that all my time is being lost in Canvas.drawBitmap()

I seem to be following all the usual performance advice - using a SurfaceView, mindful of GC so not creating loads of throwaway objects, and avoiding Drawables.

Am I right in understanding that Canvas.drawBitmap() is CPU-bound, and if I want to improve performance I have to switch to OpenGL which will use the GPU? I can't find it stated that baldly anywhere, but reading between the lines of some comments I think that might have to be my next step...?

like image 673
Paul Harman Avatar asked Jul 15 '11 10:07

Paul Harman


1 Answers

This is normal. Canvas is amazingly slow when using transparency (like ARGB_8888).

2 options:

  • Switch to OpenGL ES
  • Use the least possible transparency on your bitmaps (i.e use RGB_565 the most you can).
like image 63
user703016 Avatar answered Oct 15 '22 05:10

user703016