http://developer.android.com/guide/topics/graphics/hardware-accel.html
Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline is designed to better support hardware acceleration. Hardware acceleration carries out all drawing operations that are performed on a View
's canvas using the GPU. Because of the increased resources required to enable hardware acceleration, your app will consume more RAM.
The easiest way to enable hardware acceleration is to turn it on globally for your entire application. If your application uses only standard views and Drawable
s, turning it on globally should not cause any adverse drawing effects. However, because hardware acceleration is not supported for all of the 2D drawing operations, turning it on might affect some of your applications that use custom views or drawing calls. Problems usually manifest themselves as invisible elements, exceptions, or wrongly rendered pixels. To remedy this, Android gives you the option to enable or disable hardware acceleration at the following levels:
If your application performs custom drawing, test your application on actual hardwaredevices with hardware acceleration turned on to find any problems. The Unsupported drawing operations section describes known issues withdrawing operations that cannot be hardware accelerated and how to work around them.
You can control hardware acceleration at the following levels:
In your Android manifest file, add the following attribute to the
tag to enable hardware acceleration for your entire application:
android:hardwareAccelerated="true" ...>
If your application does not behave properly with hardware acceleration turned on globally, you can control it for individual activities as well. To enable or disable hardware acceleration at the activity level, you can use the android:hardwareAccelerated
attribute for the
element. The following example enables hardware accelerationfor the entire application but disables it for one activity:
android:hardwareAccelerated="true">
... />
android:hardwareAccelerated="false" />
If you need even more fine-grained control, you can enable hardware acceleration for a given window with the following code:
getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Note: You currently cannot disable hardware acceleration atthe window level.
You can disable hardware acceleration for an individual view at runtime with thefollowing code:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Note: You currently cannot enable hardware acceleration atthe view level. View layers have other functions besides disabling hardware acceleration. See View layers for more information about their uses.
It is sometimes useful for an application to know whether it is currently hardware accelerated, especially for things such as custom views. This is particularly useful if your application does a lot of custom drawing and not all operations are properly supported by the new rendering pipeline.
There are two different ways to check whether the application is hardware accelerated:
View.isHardwareAccelerated()
returns true
if the View
is attached to a hardware accelerated window.Canvas.isHardwareAccelerated()
returns true
if the Canvas
is hardware acceleratedIf you must do this check in your drawing code, use Canvas.isHardwareAccelerated()
instead of View.isHardwareAccelerated()
when possible. When a view is attached to a hardware accelerated window, it can still be drawn using a non-hardware accelerated Canvas. This happens, for instance, when drawing a view into a bitmap for caching purposes.
When hardware acceleration is enabled, the Android framework utilizes a new drawing model that utilizes display lists to render your application to the screen. To fully understand display lists and how they might affect your application, it is useful to understand how Android draws views without hardware acceleration as well. The following sections describe the software-based and hardware-accelerated drawing models.
In the software drawing model, views are drawn with the following two steps:
Whenever an application needs to update a part of its UI, it invokes invalidate()
(or one of its variants) on any view that has changed content. The invalidation messages are propagated all the way up the view hierarchy to compute the regions of the screen that need to be redrawn (the dirty region). The Android system then draws any view in the hierarchy that intersects with the dirty region. Unfortunately, there are two drawbacks to this drawing model:
invalidate()
on a button and thatbutton sits on top of another view, the Android system redraws the view even though it hasn'tchanged.invalidate()
was not called on it. When this happens, you are relying on another view being invalidated to obtain the proper behavior. This behavior can change every time you modify your application. Because of this, you should always call invalidate()
on your custom views whenever you modify data or state that affects the view’s drawing code.Note: Android views automatically call invalidate()
when their properties change, such as the background color or the text in a TextView
.
The Android system still uses invalidate()
and draw()
to request screen updates and to render views, but handles the actual drawing differently. Instead of executing the drawing commands immediately, the Android system records them inside display lists, which contain the output of the view hierarchy’s drawing code. Another optimization is that the Android system only needs to record and update display lists for views marked dirty by an invalidate()
call. Views that have not been invalidated can be redrawn simply by re-issuing the previously recorded display list. The new drawing model contains three stages:
With this model, you cannot rely on a view intersecting the dirty region to have its draw()
method executed. To ensure that the Android system records a view’s display list, you must call invalidate()
. Forgetting to do so causes a view to look the same even after changing it, which is an easier bug to find if it happens.
Using display lists also benefits animation performance because setting specific properties, such as alpha or rotation, does not require invalidating the targeted view (it is done automatically). This optimization also applies to views with display lists (any view when your application is hardware accelerated.) For example, assume there is a LinearLayout
that contains a ListView
above a Button
. The display list for the LinearLayout
looks like this:
Assume now that you want to change the ListView
's opacity. After invoking setAlpha(0.5f)
on the ListView
, the display list now contains this:
The complex drawing code of ListView
was not executed. Instead, the system only updated the display list of the much simpler LinearLayout
. In an application without hardware acceleration enabled, the drawing code of both the list and its parent are executed again.
When hardware accelerated, the 2D rendering pipeline supports the most commonly used Canvas
drawing operations as well as many less-used operations. All of the drawing operations that are used to render applications that ship with Android, default widgets and layouts, and common advanced visual effects such as reflections and tiled textures are supported. The following list describes known operations that are not supported with hardware acceleration:
clipPath()
clipRegion()
drawPicture()
drawTextOnPath()
drawVertices()
setLinearText()
setMaskFilter()
setRasterizer()
AvoidXfermode
PixelXorXfermode
In addition, some operations behave differently with hardware acceleration enabled:
clipRect()
: XOR
, Difference
and ReverseDifference
clip modes are ignored. 3D transforms do not apply to the clip rectangledrawBitmapMesh()
: colors array is ignoredsetDither()
: ignoredsetFilterBitmap()
: filtering is always onsetShadowLayer()
: works with text onlyPorterDuff.Mode.DARKEN
will be equivalent to SRC_OVER
when blending against the framebuffer.PorterDuff.Mode.LIGHTEN
will be equivalent to SRC_OVER
when blending against the framebuffer.PorterDuff.Mode.OVERLAY
will be equivalent to SRC_OVER
when blending against the framebuffer.ComposeShader
can only contain shaders of different types (a BitmapShader
and a LinearGradient
for instance, but not two instances of BitmapShader
)ComposeShader
cannot contain a ComposeShader
If your application is affected by any of these missing features or limitations, you can turn off hardware acceleration for just the affected portion of your application by calling setLayerType(View.LAYER_TYPE_SOFTWARE, null)
. This way,you can still take advantage of hardware acceleratin everywhere else. See Controlling Hardware Acceleration for more information on how to enable anddisable hardware acceleration at different levels in your application.
In all versions of Android, views have had the ability to render into off-screen buffers,either by using a view's drawing cache, or by using Canvas.saveLayer()
. Off-screen buffers, or layers, have several uses. You can use them to get better performance when animating complex views or to apply composition effects. For instance, you can implement fade effects using Canvas.saveLayer()
to temporarily render a view into a layer and then composite it back on screen with an opacity factor.
Beginning in Android 3.0 (API level 11), you have more control on how and when to use layers with the View.setLayerType()
method. This API takes two parameters: the type of layer you want to use and an optional Paint
object that describes how the layer should be composited. You can use the Paint
parameter to apply color filters, special blending modes, or opacity to a layer. A view can use one of three layer types:
LAYER_TYPE_NONE
: The view is rendered normally and is not backed by an off-screen buffer. This is the default behavior.LAYER_TYPE_HARDWARE
: The view is rendered in hardware into a hardware texture if the application is hardware accelerated. If the application is not hardware accelerated, this layer type behaves the same as LAYER_TYPE_SOFTWARE
.LAYER_TYPE_SOFTWARE
: The view is rendered in software into a bitmap.The type of layer you use depends on your goal:
invalidate()
. Some animations, such as alpha animations, can then be applied directly onto the layer, which is very efficient for the GPU to do. Paint
to apply special visual treatments to a view. For instance, you can draw a view in black and white using a ColorMatrixColorFilter
.Hardware layers can deliver faster and smoother animations when your applicationis hardware accelerated. Running an animation at 60 frames per second is not always possible whenanimating complex views that issue a lot of drawing operations. This can be alleviated byusing hardware layers to render the view to a hardware texture. The hardware texture canthen be used to animate the view, eliminating the need for the view to constantly redraw itselfwhen it is being animated. The view is not redrawn unless you change the view'sproperties, which calls invalidate()
, or if you call invalidate()
manually. If you are running an animation inyour application and do not obtain the smooth results you want, consider enabling hardware layers onyour animated views.
When a view is backed by a hardware layer, some of its properties are handled by the way the layer is composited on screen. Setting these properties will be efficient because they do not require the view to be invalidated and redrawn. The following list of properties affect the way the layer is composited. Calling the setter for any of these properties results in optimal invalidation and no redrawing of the targeted view:
alpha
: Changes the layer's opacityx
, y
, translationX
, translationY
:Changes the layer's positionscaleX
, scaleY
: Changes the layer's sizerotation
, rotationX
, rotationY
: Changes the layer's orientation in 3D spacepivotX
, pivotY
: Changes the layer's transformations originThese properties are the names used when animating a view with an ObjectAnimator
. If you want to access these properties, call the appropriate setter or getter. For instance, to modify the alpha property, call setAlpha()
. The following code snippet shows the most efficient way to rotate a viewiew in 3D around the Y-axis:
view.setLayerType(View.LAYER_TYPE_HARDWARE, null); ObjectAnimator.ofFloat(view, "rotationY", 180).start();
Because hardware layers consume video memory, it is highly recommended that you enable themonly for the duration of the animation and then disable them after the animation is done. Youcan accomplish this using animation listeners:
View.setLayerType(View.LAYER_TYPE_HARDWARE, null); ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", 180); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { view.setLayerType(View.LAYER_TYPE_NONE, null); } }); animator.start();
For more information on property animation, see Property Animation.
Switching to hardware accelerated 2D graphics can instantly increase performance, but you should still design your application to use the GPU effectively by following these recommendations:
Paint
or a new
Path
every time a rendering method is invoked. This forces the garbagecollector to run more often and also bypasses caches and optimizations in the hardwarepipeline.
setAlpha()
,
AlphaAnimation
, or
ObjectAnimator
, it is 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
.