xref: /aosp_15_r20/external/skia/site/docs/user/api/skpaint_overview.md (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1---
2title: 'SkPaint Overview'
3linkTitle: 'SkPaint Overview'
4
5weight: 280
6---
7
8Anytime you draw something in Skia, and want to specify what color it is, or how
9it blends with the background, or what style or font to draw it in, you specify
10those attributes in a paint.
11
12Unlike `SkCanvas`, paints do not maintain an internal stack of state (i.e. there
13is no save/restore on a paint). However, paints are relatively light-weight, so
14the client may create and maintain any number of paint objects, each set up for
15a particular use. Factoring all of these color and stylistic attributes out of
16the canvas state, and into (multiple) paint objects, allows canvas' save/restore
17to be that much more efficient, as all they have to do is maintain the stack of
18matrix and clip settings.
19
20<fiddle-embed-sk name='@skpaint_skia'></fiddle-embed-sk>
21
22This shows three different paints, each set up to draw in a different style. Now
23the caller can intermix these paints freely, either using them as is, or
24modifying them as the drawing proceeds.
25
26<fiddle-embed-sk name='@skpaint_mix'></fiddle-embed-sk>
27
28Beyond simple attributes such as color, strokes, and text values, paints support
29effects. These are subclasses of different aspects of the drawing pipeline, that
30when referenced by a paint (each of them is reference-counted), are called to
31override some part of the drawing pipeline.
32
33For example, to draw using a gradient instead of a single color, assign a
34SkShader to the paint.
35
36<fiddle-embed-sk name='@skpaint_shader'></fiddle-embed-sk>
37
38Now, anything drawn with that paint will be drawn with the gradient specified in
39the call to `MakeLinear()`. The shader object that is returned is
40reference-counted. Whenever any effects object, like a shader, is assigned to a
41paint, its reference-count is increased by the paint. To balance this, the
42caller in the above example calls `unref()` on the shader once it has assigned
43it to the paint. Now the paint is the only "owner" of that shader, and it will
44automatically call `unref()` on the shader when either the paint goes out of
45scope, or if another shader (or null) is assigned to it.
46
47There are 6 types of effects that can be assigned to a paint:
48
49- **SkPathEffect** - modifications to the geometry (path) before it generates an
50  alpha mask (e.g. dashing)
51- **SkRasterizer** - composing custom mask layers (e.g. shadows)
52- **SkMaskFilter** - modifications to the alpha mask before it is colorized and
53  drawn (e.g. blur)
54- **SkShader** - e.g. gradients (linear, radial, sweep), bitmap patterns (clamp,
55  repeat, mirror)
56- **SkColorFilter** - modify the source color(s) before applying the blend
57  (e.g. color matrix)
58- **SkBlendMode** - e.g. porter-duff transfermodes, blend modes
59
60Paints also hold a reference to a SkTypeface. The typeface represents a specific
61font style, to be used for measuring and drawing text. Speaking of which, paints
62are used not only for drawing text, but also for measuring it.
63
64<!--?prettify lang=cc?-->
65
66    paint.measureText(...);
67    paint.getTextBounds(...);
68    paint.textToGlyphs(...);
69    paint.getFontMetrics(...);
70
71## SkBlendMode
72
73The following example demonstrates all of the Skia's standard blend modes. In
74this example the source is a solid magenta color with a horizontal alpha
75gradient and the destination is a solid cyan color with a vertical alpha
76gradient.
77
78<fiddle-embed-sk name='@skpaint_xfer'></fiddle-embed-sk>
79
80## SkShader
81
82Several shaders are defined (besides the linear gradient already mentioned):
83
84- Bitmap Shader
85
86  <fiddle-embed-sk name='@skpaint_bitmap_shader'></fiddle-embed-sk>
87
88- Radial Gradient Shader
89
90  <fiddle-embed-sk name='@skpaint_radial'></fiddle-embed-sk>
91
92- Two-Point Conical Gradient Shader
93
94  <fiddle-embed-sk name='@skpaint_2pt'></fiddle-embed-sk>
95
96- Sweep Gradient Shader
97
98  <fiddle-embed-sk name='@skpaint_sweep'></fiddle-embed-sk>
99
100- Fractal Perlin Noise Shader
101
102  <fiddle-embed-sk name='@skpaint_perlin'></fiddle-embed-sk>
103
104- Turbulence Perlin Noise Shader
105
106  <fiddle-embed-sk name='@skpaint_turb'></fiddle-embed-sk>
107
108- Compose Shader
109
110  <fiddle-embed-sk name='@skpaint_compose_shader'></fiddle-embed-sk>
111
112## SkMaskFilter
113
114- Blur Mask Filter
115
116  <fiddle-embed-sk name='@skpaint_blur_mask_filter'></fiddle-embed-sk>
117
118## SkColorFilter
119
120- Color Matrix Color Filter
121
122  <fiddle-embed-sk name='@skpaint_matrix_color_filter'></fiddle-embed-sk>
123
124- Color Table Color Filter
125
126  <fiddle-embed-sk name='@skpaint_color_table_filter'></fiddle-embed-sk>
127
128## SkPathEffect
129
130- SkPath2DPathEffect: Stamp the specified path to fill the shape, using the
131  matrix to define the latice.
132
133  <fiddle-embed-sk name='@skpaint_path_2d_path_effect'></fiddle-embed-sk>
134
135- SkLine2DPathEffect: a special case of SkPath2DPathEffect where the path is a
136  straight line to be stroked, not a path to be filled.
137
138  <fiddle-embed-sk name='@skpaint_line_2d_path_effect'></fiddle-embed-sk>
139
140- SkPath1DPathEffect: create dash-like effects by replicating the specified path
141  along the drawn path.
142
143  <fiddle-embed-sk name='@skpaint_path_1d_path_effect'></fiddle-embed-sk>
144
145- SkCornerPathEffect: a path effect that can turn sharp corners into various
146  treatments (e.g. rounded corners).
147
148  <fiddle-embed-sk name='@skpaint_corner_path_effects'></fiddle-embed-sk>
149
150- SkDashPathEffect: a path effect that implements dashing.
151
152  <fiddle-embed-sk name='@skpaint_dash_path_effect'></fiddle-embed-sk>
153
154- SkDiscretePathEffect: This path effect chops a path into discrete segments,
155  and randomly displaces them.
156
157  <fiddle-embed-sk name='@skpaint_discrete_path_effect'></fiddle-embed-sk>
158
159- SkComposePathEffect: a pathEffect whose effect is to apply first the inner
160  pathEffect and the the outer pathEffect (i.e. outer(inner(path))).
161
162  <fiddle-embed-sk name='@skpaint_compose_path_effect'></fiddle-embed-sk>
163
164- SkSumPathEffect: a pathEffect whose effect is to apply two effects, in
165  sequence (i.e. first(path) + second(path)).
166
167  <fiddle-embed-sk name='@skpaint_sum_path_effect'></fiddle-embed-sk>
168