color, stroke, font, effects
Anytime you draw something in Skia, and want to specify what color it is, or how it blends with the background, or what style or font to draw it in, you specify those attributes in a paint.
Unlike SkCanvas
, paints do not maintain an internal stack of state (i.e. there is no save/restore on a paint). However, paints are relatively light-weight, so the client may create and maintain any number of paint objects, each set up for a particular use. Factoring all of these color and stylistic attributes out of the canvas state, and into (multiple) paint objects, allows canvas' save/restore to be that much more efficient, as all they have to do is maintain the stack of matrix and clip settings.
This shows three different paints, each set up to draw in a different style. Now the caller can intermix these paints freely, either using them as is, or modifying them as the drawing proceeds.
Beyond simple attributes such as color, strokes, and text values, paints support effects. These are subclasses of different aspects of the drawing pipeline, that when referenced by a paint (each of them is reference-counted), are called to override some part of the drawing pipeline.
For example, to draw using a gradient instead of a single color, assign a SkShader to the paint.
Now, anything drawn with that paint will be drawn with the gradient specified in the call to MakeLinear()
. The shader object that is returned is reference-counted. Whenever any effects object, like a shader, is assigned to a paint, its reference-count is increased by the paint. To balance this, the caller in the above example calls unref()
on the shader once it has assigned it to the paint. Now the paint is the only “owner” of that shader, and it will automatically call unref()
on the shader when either the paint goes out of scope, or if another shader (or null) is assigned to it.
There are 6 types of effects that can be assigned to a paint:
Paints also hold a reference to a SkTypeface. The typeface represents a specific font style, to be used for measuring and drawing text. Speaking of which, paints are used not only for drawing text, but also for measuring it.
paint.measureText(...); paint.getTextBounds(...); paint.textToGlyphs(...); paint.getFontMetrics(...);
The following example demonstrates all of the Skia's standard transfer modes. In this example the source is a solid magenta color with a horizontal alpha gradient and the destination is a solid cyan color with a vertical alpha gradient.
Several shaders are defined (besides the linear gradient already mentioned):
Bitmap Shader
Radial Gradient Shader
Two-Point Conical Gradient Shader
Sweep Gradient Shader
Fractal Perlin Noise Shader
Turbulence Perlin Noise Shader
Compose Shader
Blur Mask Filter
Color Matrix Color Filter
Color Table Color Filter
SkPath2DPathEffect: Stamp the specified path to fill the shape, using the matrix to define the latice.
SkLine2DPathEffect: a special case of SkPath2DPathEffect where the path is a straight line to be stroked, not a path to be filled.
SkPath1DPathEffect: create dash-like effects by replicating the specified path along the drawn path.
SkArcToPathEffect
The following few examples use this function:
SkCornerPathEffect: a path effect that can turn sharp corners into various treatments (e.g. rounded corners).
SkDashPathEffect: a path effect that implements dashing.
SkDiscretePathEffect: This path effect chops a path into discrete segments, and randomly displaces them.
SkComposePathEffect: a pathEffect whose effect is to apply first the inner pathEffect and the the outer pathEffect (i.e. outer(inner(path))).
SkSumPathEffect: a pathEffect whose effect is to apply two effects, in sequence (i.e. first(path) + second(path)).
<fiddle-embed name='@skpaint_sum_path_effect'></fiddle-embed>