Android 2D Rendering Pipeline
Wednesday, November 30, 2011, Modified: Monday, December 19, 2011 Refs

The android rendering pipeline lacks the sort of overview documentation that comes with OpenGL, in the form of its blue and red books and state machine diagrams. Android's 2D graphics Java API is largely a thin wrapper around a native code library called skia. Its wiki page entry for the SkPaint class is quite informative.

A lot of the rendering state is factored out of Canvas and placed in Paint objects that are passed explicitly into draw calls. The rendering pipeline is composed of bits of state attached to these Paint objects. Having specified a primitive, you can modify the path generated from it with a PathEffect, the result of which passes through to rasterization. Rasterization produces a mask (a grid of flags defining which pixels are a part of the primitive) which is then subject to a MaskFilter object to further restrict the set of pixels to be touched. A Shader object, which works in spans (horizontal batches of pixels), is then able to define the colours of fragments corresponding to set rasterization mask entries. Coloured fragments then pass through a ColorFilter object, which is able to apply effects for which output colours are a pure function of input colour. The final step is to combine the colour dropping out of the end of this pipeline with the colour that is already in the destination bitmap. This operation is defined by Paint's Xfermode component using a subclass such as PorterDuffXfermode, which specifies one of the Porter-Duff blending modes.

For many of these stages, the Java API gives the developer the option of plugging in one of a set of canned implementation which are exposed as subclasses of the class expected by a paint set/get pair. For example, Shader doesn't expose an interface that allows user-written subclasses to receive spans of fragments/pixels and paint them how it likes. Instead, it is little more than an opaque handle with clients of Paint having the choice of plugging-in one of the pre-baked subclasses such as BitmapShader, ComposeShader, LinearGradient, RadialGradient, and SweepGradient.

Laurence Gonsalves has written a good explanation of the pipeline with a diagram after failing to get a satisfactory answer for his ambitiously open-ended Stack Overflow question.

Try Buffer, a smarter way to share links and pictures by spreading your tweets and posts out over time. Sign up from this link and we both get extra features.

Home :: Items