The current hardware accelerated 2D pipeline supports the most
commonly used Canvas operations, and then some. We implemented all the
operations needed to render the built-in applications, all the default
widgets and layouts, and common advanced visual effects (reflections,
tiled textures, etc.) There are however a few operations that are
currently not supported, but might be in a future version of Android:
- Canvas
clipPathclipRegiondrawPicturedrawPointsdrawPosTextdrawTextOnPathdrawVertices
- Paint
setLinearTextsetMaskFiltersetRasterizer
- Canvas
clipRect:XOR,DifferenceandReverseDifferenceclip modes are ignored; 3D transforms do not apply to the clip rectangledrawBitmapMesh: colors array is ignoreddrawLines: anti-aliasing is not supportedsetDrawFilter: can be set, but ignored
- Paint
setDither: ignoredsetFilterBitmap: filtering is always onsetShadowLayer: works with text only
- ComposeShader
- A
ComposeShadercan only contain shaders of different types (aBitmapShaderand aLinearGradientShaderfor instance, but not two instances ofBitmapShader) - A
ComposeShadercannot contain aComposeShader
- A
LAYER_TYPE_SOFTWARE. In both cases, you will switch back to the software rendering pipeline.Dos and don'ts
Switching to hardware accelerated 2D graphics is a great way to get smoother animations and faster rendering in your application but it is by no means a magic bullet. Your application should be designed and implemented to be GPU friendly. It is easier than you might think if you follow these recommendations:- Reduce the number of Views in your application: the more Views the system has to draw, the slower it will be. This applies to the software pipeline as well; it is one of the easiest ways to optimize your UI.
- Avoid overdraw: always make sure that you are not drawing too many layers on top of each other. In particular, make sure to remove any Views that are 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 is not 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(), anAlphaAnimationor anObjectAnimatoranimating the “alpha” property, 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 toLAYER_TYPE_HARDWARE.
No comments:
Post a Comment