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