Android 3.0 (Honeycomb): Introducing Hardware Accelerated 2D Graphics and Tips to Use it Properly

One of the biggest changes made to Android 3.0 Honeycomb is the addition of a new rendering pipeline so that applications can benefit from hardware accelerated 2D graphics. Hardware accelerated graphics is nothing new to the Android platform, it has always been used for windows composition or OpenGL games for instance, but with this new […]

One of the biggest changes made to Android 3.0 Honeycomb is the addition of a new rendering pipeline so that applications can benefit from hardware accelerated 2D graphics. Hardware accelerated graphics is nothing new to the Android platform, it has always been used for windows composition or OpenGL games for instance, but with this new rendering pipeline apps can benefit from an extra boost in performance. On a Motorola Xoom device, all the standard apps like Browser and Calendar use hardware-accelerated 2D graphics.

Dos and don'ts

Switching to hardware accelerated 2D graphics is a great way to get smoother animations and faster rendering in your app but it's by no means a magic bullet. Your app should be designed and implemented to be GPU friendly. It's easier than you might think if you follow these recommendations:

  • Reduce number of Views in your app: the more Views the system has to draw, the slower it'll be. This applies to software pipeline as well; it is one of the easiest ways to optimize your UI.
  • Avoid overdraw: always make sure that you're not drawing too many layers on top of each other. In particular, make sure to remove any Views that're completely obscured by other opaque views on top of it. If you need to draw several layers blended on top of each other consider merging them into a single one. A good rule of thumb with current hardware is to not draw more than 2.5 times the number of pixels on screen per frame (and transparent pixels in a bitmap count!)
  • Don't create render objects in draw methods: a common mistake is to create a new Paint, or a new Path, every time a rendering method is invoked. This isn't only wasteful, forcing the system to run the GC more often, it also bypasses caches and optimizations in the hardware pipeline.
  • Don't modify shapes too often: complex shapes, paths and circles for instance, are rendered using texture masks. Every time you create or modify a Path, the hardware pipeline must create a new mask, which can be expensive.
  • Don't modify bitmaps too often: every time you change the content of a bitmap, it needs to be uploaded again as a GPU texture the next time you draw it.
  • Use alpha with care: when a View is made translucent using View.setAlpha(), an AlphaAnimation or an ObjectAnimator animating the "alpha" property, it's rendered in an off-screen buffer which doubles the required fill-rate. When applying alpha on very large views, consider setting the View's layer type to LAYER_TYPE_HARDWARE.

To know how to enable the hardware accelerated 2D graphics pipeline in your application and a few tips on how to use it properly, read here.