Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1 | #Topic Canvas |
Cary Clark | 12799e1 | 2017-07-28 15:18:29 -0400 | [diff] [blame] | 2 | #Alias Canvas_Reference |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3 | |
Cary Clark | e4aa371 | 2017-09-15 02:56:12 -0400 | [diff] [blame] | 4 | #Class SkCanvas |
| 5 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6 | Canvas provides an interface for drawing, and how the drawing is clipped and transformed. |
| 7 | Canvas contains a stack of Matrix and Clip values. |
| 8 | |
| 9 | Canvas and Paint together provide the state to draw into Surface or Device. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 10 | Each Canvas draw call transforms the geometry of the object by the concatenation of all |
| 11 | Matrix values in the stack. The transformed geometry is clipped by the intersection |
| 12 | of all of Clip values in the stack. The Canvas draw calls use Paint to supply drawing |
| 13 | state such as Color, Typeface, text size, stroke width, Shader and so on. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 14 | |
| 15 | To draw to a pixel-based destination, create Raster_Surface or GPU_Surface. |
| 16 | Request Canvas from Surface to obtain the interface to draw. |
| 17 | Canvas generated by Raster_Surface draws to memory visible to the CPU. |
| 18 | Canvas generated by GPU_Surface uses Vulkan or OpenGL to draw to the GPU. |
| 19 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 20 | To draw to a document, obtain Canvas from SVG_Canvas, Document_PDF, or Picture_Recorder. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 21 | Document based Canvas and other Canvas Subclasses reference Device describing the |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 22 | destination. |
| 23 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 24 | Canvas can be constructed to draw to Bitmap without first creating Raster_Surface. |
| 25 | This approach may be deprecated in the future. |
| 26 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 27 | #Topic Overview |
| 28 | |
| 29 | #Subtopic Subtopics |
| 30 | #Table |
| 31 | #Legend |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 32 | # name # description ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 33 | #Legend ## |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 34 | # Classes_and_Structs # embedded struct and class members ## |
| 35 | # Constants # enum and enum class, const values ## |
| 36 | # Constructors # functions that construct SkPath ## |
| 37 | # Member_Functions # static functions and member methods ## |
| 38 | # Operators # operator overloading methods ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 39 | #Table ## |
| 40 | #Subtopic ## |
| 41 | |
| 42 | #Subtopic Constants |
| 43 | #Table |
| 44 | #Legend |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 45 | # name # description ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 46 | #Legend ## |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 47 | # Lattice::Flags # controls Lattice transparency ## |
| 48 | # PointMode # sets drawPoints options ## |
| 49 | # SaveLayerFlags # sets SaveLayerRec options ## |
| 50 | # SrcRectConstraint # sets drawImageRect options ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 51 | #Table ## |
| 52 | #Subtopic ## |
| 53 | |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 54 | #Subtopic Classes_and_Structs |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 55 | #Table |
| 56 | #Legend |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 57 | # name # description ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 58 | #Legend ## |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 59 | # Lattice # divides Bitmap, Image into a rectangular grid ## |
| 60 | # SaveLayerRec # contains state to create Layer ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 61 | #Table ## |
| 62 | #Subtopic ## |
| 63 | |
| 64 | #Subtopic Constructors |
| 65 | |
| 66 | Create the desired type of Surface to obtain its Canvas when possible. Constructors are useful |
| 67 | when no Surface is required, and some helpers implicitly create Raster_Surface. |
| 68 | |
| 69 | #Table |
| 70 | #Legend |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 71 | # name # description ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 72 | #Legend ## |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 73 | # MakeRasterDirect # creates from SkImageInfo and Pixel_Storage ## |
| 74 | # MakeRasterDirectN32 # creates from image data and Pixel_Storage ## |
| 75 | # SkCanvas() # creates with no Surface, no dimensions ## |
| 76 | # SkCanvas(SkBaseDevice* device) # to be deprecated ## |
| 77 | # SkCanvas(const SkBitmap& bitmap) # uses existing Bitmap ## |
| 78 | # SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior) # Android framework only ## |
| 79 | # SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) # uses existing Bitmap and Surface_Properties ## |
| 80 | # SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr) # no Surface, set dimensions, Surface_Properties ## |
| 81 | # ~SkCanvas() # draws saved Layers, frees resources ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 82 | #Table ## |
| 83 | #Subtopic ## |
| 84 | |
| 85 | #Subtopic Member_Functions |
| 86 | #Table |
| 87 | #Legend |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 88 | # name # description ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 89 | #Legend ## |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 90 | # MakeRasterDirect # creates Canvas from SkImageInfo and pixel data ## |
| 91 | # MakeRasterDirectN32 # creates Canvas from image specifications and pixel data ## |
| 92 | # accessTopLayerPixels # returns writable pixel access if available ## |
| 93 | # accessTopRasterHandle # returns context that tracks Clip and Matrix ## |
| 94 | # clear() # fills Clip with Color ## |
| 95 | # clipPath # combines Clip with Path ## |
| 96 | # clipRRect # combines Clip with Round_Rect ## |
| 97 | # clipRect # combines Clip with Rect ## |
| 98 | # clipRegion # combines Clip with Region ## |
| 99 | # concat() # multiplies Matrix by Matrix ## |
| 100 | # discard() # makes Canvas contents undefined ## |
| 101 | # drawAnnotation # associates a Rect with a key-value pair ## |
| 102 | # drawArc # draws Arc using Clip, Matrix, and Paint ## |
| 103 | # drawAtlas # draws sprites using Clip, Matrix, and Paint ## |
| 104 | # drawBitmap # draws Bitmap at (x, y) position ## |
| 105 | # drawBitmapLattice # draws proportionally stretched Bitmap ## |
| 106 | # drawBitmapNine # draws Nine_Patch Bitmap ## |
| 107 | # drawBitmapRect # draws Bitmap, source Rect to destination Rect ## |
| 108 | # drawCircle # draws Circle using Clip, Matrix, and Paint ## |
| 109 | # drawColor # fills Clip with Color and Blend_Mode ## |
| 110 | # drawDRRect # draws double Round_Rect stroked or filled ## |
| 111 | # drawDrawable # draws Drawable, encapsulated drawing commands ## |
| 112 | # drawIRect # draws IRect using Clip, Matrix, and Paint ## |
| 113 | # drawImage # draws Image at (x, y) position ## |
| 114 | # drawImageLattice # draws proportionally stretched Image ## |
| 115 | # drawImageNine # draws Nine_Patch Image ## |
| 116 | # drawImageRect # draws Image, source Rect to destination Rect ## |
| 117 | # drawLine # draws line segment between two points ## |
| 118 | # drawOval # draws Oval using Clip, Matrix, and Paint ## |
| 119 | # drawPaint # fills Clip with Paint ## |
| 120 | # drawPatch # draws Coons_Patch ## |
| 121 | # drawPath # draws Path using Clip, Matrix, and Paint ## |
| 122 | # drawPicture # draws Picture using Clip and Matrix ## |
| 123 | # drawPoint # draws point at (x, y) position ## |
| 124 | # drawPoints # draws array as points, lines, polygon ## |
| 125 | # drawPosText # draws text at array of (x, y) positions ## |
| 126 | # drawPosTextH # draws text at x positions with common baseline ## |
| 127 | # drawRRect # draws Round_Rect using Clip, Matrix, and Paint ## |
| 128 | # drawRect # draws Rect using Clip, Matrix, and Paint ## |
| 129 | # drawRegion # draws Region using Clip, Matrix, and Paint ## |
| 130 | # drawRoundRect # draws Round_Rect using Clip, Matrix, and Paint ## |
| 131 | # drawString # draws null terminated string at (x, y) using font advance ## |
| 132 | # drawText # draws text at (x, y), using font advance ## |
| 133 | # drawTextBlob # draws text with arrays of positions and Paint ## |
| 134 | # drawTextOnPath # draws text following Path contour ## |
| 135 | # drawTextOnPathHV # draws text following Path with offsets ## |
| 136 | # drawTextRSXform # draws text with array of RSXform ## |
| 137 | # drawVertices # draws Vertices, a triangle mesh ## |
| 138 | # flush() # triggers execution of all pending draw operations ## |
| 139 | # getBaseLayerSize # returns size of base Layer in global coordinates ## |
| 140 | # getDeviceClipBounds # returns IRect bounds of Clip ## |
| 141 | # getDrawFilter # legacy; to be deprecated ## |
| 142 | # getGrContext # returns GPU_Context of the GPU_Surface ## |
| 143 | # getLocalClipBounds # returns Clip bounds in source coordinates ## |
| 144 | # getMetaData # associates additional data with the canvas ## |
| 145 | # getProps # copies Surface_Properties if available ## |
| 146 | # getSaveCount # returns depth of stack containing Clip and Matrix ## |
| 147 | # getTotalMatrix # returns Matrix ## |
| 148 | # imageInfo # returns Image_Info for Canvas ## |
| 149 | # isClipEmpty # returns if Clip is empty ## |
| 150 | # isClipRect # returns if Clip is Rect and not empty ## |
| 151 | # makeSurface # creates Surface matching SkImageInfo and SkSurfaceProps ## |
| 152 | # peekPixels # returns if Canvas has direct access to its pixels ## |
| 153 | # quickReject # returns if Rect is outside Clip ## |
| 154 | # readPixels # copies and converts rectangle of pixels from Canvas ## |
| 155 | # resetMatrix # resets Matrix to identity ## |
| 156 | # restore() # restores changes to Clip and Matrix, pops save stack ## |
| 157 | # restoreToCount # restores changes to Clip and Matrix to given depth ## |
| 158 | # rotate() # rotates Matrix ## |
| 159 | # save() # saves Clip and Matrix on stack ## |
| 160 | # saveLayer # saves Clip and Matrix on stack; creates Layer ## |
| 161 | # saveLayerAlpha # saves Clip and Matrix on stack; creates Layer; sets opacity ## |
| 162 | # saveLayerPreserveLCDTextRequests # saves Clip and Matrix on stack; creates Layer for LCD text ## |
| 163 | # scale() # scales Matrix ## |
| 164 | # setAllowSimplifyClip # experimental ## |
| 165 | # setDrawFilter # legacy; to be deprecated ## |
| 166 | # setMatrix # sets Matrix ## |
| 167 | # skew() # skews Matrix. # |
| 168 | # translate() # translates Matrix ## |
| 169 | # writePixels # copies and converts rectangle of pixels to Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 170 | #Table ## |
| 171 | #Subtopic ## |
| 172 | |
| 173 | #Topic Overview ## |
| 174 | |
| 175 | # ------------------------------------------------------------------------------ |
| 176 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 177 | #Method static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels, |
| 178 | size_t rowBytes, |
| 179 | const SkSurfaceProps* props = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 180 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 181 | Allocates raster Canvas that will draw directly into pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 182 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 183 | Canvas is returned if all parameters are valid. |
| 184 | Valid parameters include: |
| 185 | info dimensions are zero or positive; |
| 186 | info contains Image_Color_Type and Image_Alpha_Type supported by Raster_Surface; |
| 187 | pixels is not nullptr; |
| 188 | rowBytes is zero or large enough to contain info width pixels of Image_Color_Type. |
| 189 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 190 | Pass zero for rowBytes to compute rowBytes from info width and size of pixel. |
| 191 | If rowBytes is greater than zero, it must be equal to or greater than |
| 192 | info width times bytes required for Image_Color_Type. |
| 193 | |
| 194 | Pixel buffer size should be info height times computed rowBytes. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 195 | Pixels are not initialized. |
| 196 | To access pixels after drawing, call flush() or peekPixels. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 197 | |
| 198 | #Param info width, height, Image_Color_Type, Image_Alpha_Type, Color_Space, of Raster_Surface; |
| 199 | width, or height, or both, may be zero |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 200 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 201 | #Param pixels pointer to destination pixels buffer |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 202 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 203 | #Param rowBytes interval from one Surface row to the next, or zero |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 204 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 205 | #Param props LCD striping orientation and setting for device independent fonts; |
| 206 | may be nullptr |
| 207 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 208 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 209 | #Return Canvas if all parameters are valid; otherwise, nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 210 | |
| 211 | #Example |
| 212 | #Description |
| 213 | Allocates a three by three bitmap, clears it to white, and draws a black pixel |
| 214 | in the center. |
| 215 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 216 | void draw(SkCanvas* ) { |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 217 | SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3); // device aligned, 32 bpp, Premultiplied |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 218 | const size_t minRowBytes = info.minRowBytes(); // bytes used by one bitmap row |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 219 | const size_t size = info.computeMinByteSize(); // bytes used by all rows |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 220 | SkAutoTMalloc<SkPMColor> storage(size); // allocate storage for pixels |
| 221 | SkPMColor* pixels = storage.get(); // get pointer to allocated storage |
| 222 | // create a SkCanvas backed by a raster device, and delete it when the |
| 223 | // function goes out of scope. |
| 224 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, pixels, minRowBytes); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 225 | canvas->clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 226 | canvas->flush(); // ensure that pixels are cleared |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 227 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 228 | SkPaint paint; // by default, draws black |
| 229 | canvas->drawPoint(1, 1, paint); // draw in the center |
| 230 | canvas->flush(); // ensure that point was drawn |
| 231 | for (int y = 0; y < info.height(); ++y) { |
| 232 | for (int x = 0; x < info.width(); ++x) { |
| 233 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 234 | } |
| 235 | SkDebugf("\n"); |
| 236 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 237 | } |
| 238 | #StdOut |
| 239 | --- |
| 240 | -x- |
| 241 | --- |
| 242 | ## |
| 243 | ## |
| 244 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 245 | #SeeAlso MakeRasterDirectN32 SkSurface::MakeRasterDirect |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 246 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 247 | ## |
| 248 | |
| 249 | # ------------------------------------------------------------------------------ |
| 250 | |
| 251 | #Method static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels, |
| 252 | size_t rowBytes) |
| 253 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 254 | Allocates raster Canvas specified by inline image specification. Subsequent Canvas |
| 255 | calls draw into pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 256 | Image_Color_Type is set to kN32_SkColorType. |
| 257 | Image_Alpha_Type is set to kPremul_SkAlphaType. |
| 258 | To access pixels after drawing, call flush() or peekPixels. |
| 259 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 260 | Canvas is returned if all parameters are valid. |
| 261 | Valid parameters include: |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 262 | width and height are zero or positive; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 263 | pixels is not nullptr; |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 264 | rowBytes is zero or large enough to contain width pixels of kN32_SkColorType. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 265 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 266 | Pass zero for rowBytes to compute rowBytes from width and size of pixel. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 267 | If rowBytes is greater than zero, it must be equal to or greater than |
| 268 | width times bytes required for Image_Color_Type. |
| 269 | |
| 270 | Pixel buffer size should be height times rowBytes. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 271 | |
| 272 | #Param width pixel column count on Raster_Surface created; must be zero or greater ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 273 | #Param height pixel row count on Raster_Surface created; must be zero or greater ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 274 | #Param pixels pointer to destination pixels buffer; buffer size should be height |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 275 | times rowBytes |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 276 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 277 | #Param rowBytes interval from one Surface row to the next, or zero |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 278 | ## |
| 279 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 280 | #Return Canvas if all parameters are valid; otherwise, nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 281 | |
| 282 | #Example |
| 283 | #Description |
| 284 | Allocates a three by three bitmap, clears it to white, and draws a black pixel |
| 285 | in the center. |
| 286 | ## |
| 287 | void draw(SkCanvas* ) { |
| 288 | const int width = 3; |
| 289 | const int height = 3; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 290 | SkPMColor pixels[height][width]; // allocate a 3x3 Premultiplied bitmap on the stack |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 291 | // create a SkCanvas backed by a raster device, and delete it when the |
| 292 | // function goes out of scope. |
| 293 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirectN32( |
| 294 | width, |
| 295 | height, |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 296 | pixels[0], // top-left of the bitmap |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 297 | sizeof(pixels[0])); // byte width of the each row |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 298 | // write a premultiplied value for white into all pixels in the bitmap |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 299 | canvas->clear(SK_ColorWHITE); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 300 | SkPMColor pmWhite = pixels[0][0]; // the Premultiplied format may vary |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 301 | SkPaint paint; // by default, draws black |
| 302 | canvas->drawPoint(1, 1, paint); // draw in the center |
| 303 | canvas->flush(); // ensure that pixels is ready to be read |
| 304 | for (int y = 0; y < height; ++y) { |
| 305 | for (int x = 0; x < width; ++x) { |
| 306 | SkDebugf("%c", pixels[y][x] == pmWhite ? '-' : 'x'); |
| 307 | } |
| 308 | SkDebugf("\n"); |
| 309 | } |
| 310 | } |
| 311 | #StdOut |
| 312 | --- |
| 313 | -x- |
| 314 | --- |
| 315 | ## |
| 316 | ## |
| 317 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 318 | #SeeAlso MakeRasterDirect SkSurface::MakeRasterDirect SkImageInfo::MakeN32Premul |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 319 | |
| 320 | ## |
| 321 | |
| 322 | # ------------------------------------------------------------------------------ |
| 323 | |
| 324 | #Method SkCanvas() |
| 325 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 326 | Creates an empty Canvas with no backing device or pixels, with |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 327 | a width and height of zero. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 328 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 329 | #Return empty Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 330 | |
| 331 | #Example |
| 332 | |
| 333 | #Description |
| 334 | Passes a placeholder to a function that requires one. |
| 335 | ## |
| 336 | |
| 337 | #Function |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 338 | // Returns true if either the canvas rotates the text by 90 degrees, or the paint does. |
| 339 | static void check_for_up_and_down_text(const SkCanvas* canvas, const SkPaint& paint) { |
| 340 | bool paintHasVertical = paint.isVerticalText(); |
| 341 | const SkMatrix& matrix = canvas->getTotalMatrix(); |
| 342 | bool matrixIsVertical = matrix.preservesRightAngles() && !matrix.isScaleTranslate(); |
| 343 | SkDebugf("paint draws text %s\n", paintHasVertical != matrixIsVertical ? |
| 344 | "top to bottom" : "left to right"); |
| 345 | } |
| 346 | |
| 347 | static void check_for_up_and_down_text(const SkPaint& paint) { |
| 348 | SkCanvas canvas; // placeholder only, does not have an associated device |
| 349 | check_for_up_and_down_text(&canvas, paint); |
| 350 | } |
| 351 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 352 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 353 | void draw(SkCanvas* canvas) { |
| 354 | SkPaint paint; |
| 355 | check_for_up_and_down_text(paint); // paint draws text left to right |
| 356 | paint.setVerticalText(true); |
| 357 | check_for_up_and_down_text(paint); // paint draws text top to bottom |
| 358 | paint.setVerticalText(false); |
| 359 | canvas->rotate(90); |
| 360 | check_for_up_and_down_text(canvas, paint); // paint draws text top to bottom |
| 361 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 362 | |
| 363 | #StdOut |
| 364 | paint draws text left to right |
| 365 | paint draws text top to bottom |
| 366 | paint draws text top to bottom |
| 367 | ## |
| 368 | ## |
| 369 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 370 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 371 | |
| 372 | ## |
| 373 | |
| 374 | # ------------------------------------------------------------------------------ |
| 375 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 376 | #Method SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 377 | |
| 378 | Creates Canvas of the specified dimensions without a Surface. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 379 | Used by Subclasses with custom implementations for draw methods. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 380 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 381 | If props equals nullptr, Surface_Properties are created with |
| 382 | Surface_Properties_Legacy_Font_Host settings, which choose the pixel striping |
| 383 | direction and order. Since a platform may dynamically change its direction when |
| 384 | the device is rotated, and since a platform may have multiple monitors with |
| 385 | different characteristics, it is best not to rely on this legacy behavior. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 386 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 387 | #Param width zero or greater ## |
| 388 | #Param height zero or greater ## |
| 389 | #Param props LCD striping orientation and setting for device independent fonts; |
| 390 | may be nullptr |
| 391 | ## |
| 392 | |
| 393 | #Return Canvas placeholder with dimensions ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 394 | |
| 395 | #Example |
| 396 | SkCanvas canvas(10, 20); // 10 units wide, 20 units high |
| 397 | canvas.clipRect(SkRect::MakeXYWH(30, 40, 5, 10)); // clip is outside canvas' device |
| 398 | SkDebugf("canvas %s empty\n", canvas.getDeviceClipBounds().isEmpty() ? "is" : "is not"); |
| 399 | |
| 400 | #StdOut |
| 401 | canvas is empty |
| 402 | ## |
| 403 | ## |
| 404 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 405 | #SeeAlso MakeRasterDirect SkSurfaceProps SkPixelGeometry SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 406 | |
| 407 | ## |
| 408 | |
| 409 | # ------------------------------------------------------------------------------ |
| 410 | |
| 411 | #Method explicit SkCanvas(SkBaseDevice* device) |
| 412 | |
| 413 | Construct a canvas that draws into device. |
| 414 | Used by child classes of SkCanvas. |
| 415 | |
| 416 | #ToDo Since SkBaseDevice is private, shouldn't this be private also? ## |
| 417 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 418 | #Param device specifies a device for the canvas to draw into ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 419 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 420 | #Return Canvas that can be used to draw into device ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 421 | |
| 422 | #Example |
| 423 | #Error "Unsure how to create a meaningful example." |
| 424 | SkPDFCanvas::SkPDFCanvas(const sk_sp<SkPDFDevice>& dev) |
| 425 | : SkCanvas(dev.get()) {} |
| 426 | ## |
| 427 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 428 | #ToDo either remove doc or figure out a way to fiddle it ## |
| 429 | |
| 430 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 431 | |
| 432 | ## |
| 433 | |
| 434 | # ------------------------------------------------------------------------------ |
| 435 | |
| 436 | #Method explicit SkCanvas(const SkBitmap& bitmap) |
| 437 | |
| 438 | Construct a canvas that draws into bitmap. |
| 439 | Sets SkSurfaceProps::kLegacyFontHost_InitType in constructed Surface. |
| 440 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 441 | Bitmap is copied so that subsequently editing bitmap will not affect |
| 442 | constructed Canvas. |
| 443 | |
| 444 | May be deprecated in the future. |
| 445 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 446 | #ToDo Should be deprecated? ## |
| 447 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 448 | #Param bitmap width, height, Image_Color_Type, Image_Alpha_Type, and pixel |
| 449 | storage of Raster_Surface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 450 | ## |
| 451 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 452 | #Return Canvas that can be used to draw into bitmap ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 453 | |
| 454 | #Example |
| 455 | #Description |
| 456 | The actual output depends on the installed fonts. |
| 457 | ## |
| 458 | SkBitmap bitmap; |
| 459 | // create a bitmap 5 wide and 11 high |
| 460 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(5, 11)); |
| 461 | SkCanvas canvas(bitmap); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 462 | canvas.clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 463 | SkPixmap pixmap; // provides guaranteed access to the drawn pixels |
| 464 | if (!canvas.peekPixels(&pixmap)) { |
| 465 | SkDebugf("peekPixels should never fail.\n"); |
| 466 | } |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 467 | const SkPMColor* pixels = pixmap.addr32(); // points to top-left of bitmap |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 468 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 469 | SkPaint paint; // by default, draws black, 12 point text |
| 470 | canvas.drawString("!", 1, 10, paint); // 1 char at baseline (1, 10) |
| 471 | for (int y = 0; y < bitmap.height(); ++y) { |
| 472 | for (int x = 0; x < bitmap.width(); ++x) { |
| 473 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 474 | } |
| 475 | SkDebugf("\n"); |
| 476 | } |
| 477 | |
| 478 | #StdOut |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 479 | ----- |
| 480 | ---x- |
| 481 | ---x- |
| 482 | ---x- |
| 483 | ---x- |
| 484 | ---x- |
| 485 | ---x- |
| 486 | ----- |
| 487 | ---x- |
| 488 | ---x- |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 489 | ----- |
| 490 | #StdOut ## |
| 491 | ## |
| 492 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 493 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 494 | |
| 495 | ## |
| 496 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 497 | #EnumClass ColorBehavior |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 498 | |
| 499 | #Private |
| 500 | Android framework only. |
| 501 | ## |
| 502 | |
| 503 | #Code |
| 504 | enum class ColorBehavior { |
| 505 | kLegacy, |
| 506 | }; |
| 507 | ## |
| 508 | #Const kLegacy 0 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 509 | Is a placeholder to allow specialized constructor; has no meaning. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 510 | ## |
| 511 | ## |
| 512 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 513 | #Method SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior) |
| 514 | |
| 515 | #Private |
| 516 | Android framework only. |
| 517 | ## |
| 518 | |
| 519 | #Param bitmap specifies a bitmap for the canvas to draw into ## |
| 520 | #Param behavior specializes this constructor; value is unused ## |
| 521 | #Return Canvas that can be used to draw into bitmap ## |
| 522 | |
| 523 | #NoExample |
| 524 | ## |
| 525 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 526 | |
| 527 | # ------------------------------------------------------------------------------ |
| 528 | |
| 529 | #Method SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) |
| 530 | |
| 531 | Construct a canvas that draws into bitmap. |
| 532 | Use props to match the device characteristics, like LCD striping. |
| 533 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 534 | bitmap is copied so that subsequently editing bitmap will not affect |
| 535 | constructed Canvas. |
| 536 | |
| 537 | #Param bitmap width, height, Image_Color_Type, Image_Alpha_Type, |
| 538 | and pixel storage of Raster_Surface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 539 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 540 | #Param props order and orientation of RGB striping; and whether to use |
| 541 | device independent fonts |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 542 | ## |
| 543 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 544 | #Return Canvas that can be used to draw into bitmap ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 545 | |
| 546 | #Example |
| 547 | #Description |
| 548 | The actual output depends on the installed fonts. |
| 549 | ## |
| 550 | SkBitmap bitmap; |
| 551 | // create a bitmap 5 wide and 11 high |
| 552 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(5, 11)); |
| 553 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 554 | canvas.clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 555 | SkPixmap pixmap; // provides guaranteed access to the drawn pixels |
| 556 | if (!canvas.peekPixels(&pixmap)) { |
| 557 | SkDebugf("peekPixels should never fail.\n"); |
| 558 | } |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 559 | const SkPMColor* pixels = pixmap.addr32(); // points to top-left of bitmap |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 560 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 561 | SkPaint paint; // by default, draws black, 12 point text |
| 562 | canvas.drawString("!", 1, 10, paint); // 1 char at baseline (1, 10) |
| 563 | for (int y = 0; y < bitmap.height(); ++y) { |
| 564 | for (int x = 0; x < bitmap.width(); ++x) { |
| 565 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 566 | } |
| 567 | SkDebugf("\n"); |
| 568 | } |
| 569 | |
| 570 | #StdOut |
| 571 | ----- |
| 572 | ---x- |
| 573 | ---x- |
| 574 | ---x- |
| 575 | ---x- |
| 576 | ---x- |
| 577 | ---x- |
| 578 | ----- |
| 579 | ---x- |
| 580 | ---x- |
| 581 | ----- |
| 582 | #StdOut ## |
| 583 | ## |
| 584 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 585 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 586 | |
| 587 | ## |
| 588 | |
| 589 | # ------------------------------------------------------------------------------ |
| 590 | |
| 591 | #Method virtual ~SkCanvas() |
| 592 | |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame^] | 593 | Draws saved Layers, if any. |
| 594 | Frees up resources used by Canvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 595 | |
| 596 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 597 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 598 | Canvas Layer draws into bitmap. saveLayerAlpha sets up an additional |
| 599 | drawing surface that blends with the bitmap. When Layer goes out of |
| 600 | scope, Layer Destructor is called. The saved Layer is restored, drawing |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 601 | transparent letters. |
| 602 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 603 | void draw(SkCanvas* canvas) { |
| 604 | SkBitmap bitmap; |
| 605 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(200, 200)); |
| 606 | { |
| 607 | SkCanvas offscreen(bitmap); |
| 608 | SkPaint paint; |
| 609 | paint.setTextSize(100); |
| 610 | offscreen.drawString("ABC", 20, 160, paint); |
| 611 | SkRect layerBounds = SkRect::MakeXYWH(32, 32, 192, 192); |
| 612 | offscreen.saveLayerAlpha(&layerBounds, 128); |
| 613 | offscreen.clear(SK_ColorWHITE); |
| 614 | offscreen.drawString("DEF", 20, 160, paint); |
| 615 | } |
| 616 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 617 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 618 | ## |
| 619 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 620 | #SeeAlso State_Stack |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 621 | |
| 622 | ## |
| 623 | |
| 624 | # ------------------------------------------------------------------------------ |
| 625 | |
| 626 | #Method SkMetaData& getMetaData() |
| 627 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 628 | Returns storage to associate additional data with the canvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 629 | The storage is freed when Canvas is deleted. |
| 630 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 631 | #Return storage that can be read from and written to ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 632 | |
| 633 | #Example |
| 634 | const char* kHelloMetaData = "HelloMetaData"; |
| 635 | SkCanvas canvas; |
| 636 | SkMetaData& metaData = canvas.getMetaData(); |
| 637 | SkDebugf("before: %s\n", metaData.findString(kHelloMetaData)); |
| 638 | metaData.setString(kHelloMetaData, "Hello!"); |
| 639 | SkDebugf("during: %s\n", metaData.findString(kHelloMetaData)); |
| 640 | metaData.removeString(kHelloMetaData); |
| 641 | SkDebugf("after: %s\n", metaData.findString(kHelloMetaData)); |
| 642 | |
| 643 | #StdOut |
| 644 | before: (null) |
| 645 | during: Hello! |
| 646 | after: (null) |
| 647 | #StdOut ## |
| 648 | ## |
| 649 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 650 | #SeeAlso SkMetaData |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 651 | |
| 652 | ## |
| 653 | |
| 654 | # ------------------------------------------------------------------------------ |
| 655 | |
| 656 | #Method SkImageInfo imageInfo() const |
| 657 | |
| 658 | Returns Image_Info for Canvas. If Canvas is not associated with Raster_Surface or |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 659 | GPU_Surface, returned Image_Color_Type is set to kUnknown_SkColorType. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 660 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 661 | #Return dimensions and Image_Color_Type of Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 662 | |
| 663 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 664 | SkCanvas emptyCanvas; |
| 665 | SkImageInfo canvasInfo = emptyCanvas.imageInfo(); |
| 666 | SkImageInfo emptyInfo; |
| 667 | SkDebugf("emptyInfo %c= canvasInfo\n", emptyInfo == canvasInfo ? '=' : '!'); |
| 668 | |
| 669 | #StdOut |
| 670 | emptyInfo == canvasInfo |
| 671 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 672 | ## |
| 673 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 674 | #SeeAlso SkImageInfo MakeRasterDirect makeSurface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 675 | |
| 676 | ## |
| 677 | |
| 678 | # ------------------------------------------------------------------------------ |
| 679 | |
| 680 | #Method bool getProps(SkSurfaceProps* props) const |
| 681 | |
| 682 | If Canvas is associated with Raster_Surface or |
| 683 | GPU_Surface, copies Surface_Properties and returns true. Otherwise, |
| 684 | return false and leave props unchanged. |
| 685 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 686 | #Param props storage for writable SkSurfaceProps ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 687 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 688 | #Return true if Surface_Properties was copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 689 | |
| 690 | #ToDo This seems old style. Deprecate? ## |
| 691 | |
| 692 | #Example |
| 693 | SkBitmap bitmap; |
| 694 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kRGB_V_SkPixelGeometry)); |
| 695 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
| 696 | SkDebugf("isRGB:%d\n", SkPixelGeometryIsRGB(surfaceProps.pixelGeometry())); |
| 697 | if (!canvas.getProps(&surfaceProps)) { |
| 698 | SkDebugf("getProps failed unexpectedly.\n"); |
| 699 | } |
| 700 | SkDebugf("isRGB:%d\n", SkPixelGeometryIsRGB(surfaceProps.pixelGeometry())); |
| 701 | |
| 702 | #StdOut |
| 703 | isRGB:0 |
| 704 | isRGB:1 |
| 705 | #StdOut ## |
| 706 | ## |
| 707 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 708 | #SeeAlso SkSurfaceProps makeSurface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 709 | |
| 710 | ## |
| 711 | |
| 712 | # ------------------------------------------------------------------------------ |
| 713 | |
| 714 | #Method void flush() |
| 715 | |
| 716 | Triggers the immediate execution of all pending draw operations. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 717 | If Canvas is associated with GPU_Surface, resolves all pending GPU operations. |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 718 | If Canvas is associated with Raster_Surface, has no effect; raster draw |
| 719 | operations are never deferred. |
| 720 | |
| 721 | #ToDo |
| 722 | In an overview section on managing the GPU, include: |
| 723 | - flush should never change what is drawn |
| 724 | - call to kick off gpu work |
| 725 | - calling too much impacts performance |
| 726 | - some calls (peekPixels, prepareForExternalIO) call it internally |
| 727 | - canvas call is local, GrContext::flush is global |
| 728 | - diffentiate between flush, flushAndSignalSemaphores |
| 729 | - normally never needs to be called |
| 730 | - call it when sharing gpu resources, feeling memory pressure, swapping out app, and before |
| 731 | abandoning context |
| 732 | - also call to say "I'm finished drawing here", e.g., when drawing to a GPU-backed offscreen surface |
| 733 | (created with SkSurface::MakeRenderTarget) |
| 734 | |
| 735 | for posterity: this doesn't show a difference: fiddle.skia.org/c/@flushfail |
| 736 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 737 | |
| 738 | #Example |
| 739 | #Error "haven't thought of a useful example to put here" |
| 740 | ## |
| 741 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 742 | #SeeAlso peekPixels SkSurface::flush() GrContext::flush() SkSurface::prepareForExternalIO GrContext::abandonContext() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 743 | |
| 744 | ## |
| 745 | |
| 746 | # ------------------------------------------------------------------------------ |
| 747 | |
| 748 | #Method virtual SkISize getBaseLayerSize() const |
| 749 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 750 | Gets the size of the base or root Layer in global canvas coordinates. The |
| 751 | origin of the base Layer is always (0,0). The area available for drawing may be |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 752 | smaller (due to clipping or saveLayer). |
| 753 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 754 | #Return integral width and height of base Layer ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 755 | |
| 756 | #Example |
| 757 | SkBitmap bitmap; |
| 758 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(20, 30)); |
| 759 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
| 760 | canvas.clipRect(SkRect::MakeWH(10, 40)); |
| 761 | SkIRect clipDeviceBounds = canvas.getDeviceClipBounds(); |
| 762 | if (clipDeviceBounds.isEmpty()) { |
| 763 | SkDebugf("Empty clip bounds is unexpected!\n"); |
| 764 | } |
| 765 | SkDebugf("clip=%d,%d\n", clipDeviceBounds.width(), clipDeviceBounds.height()); |
| 766 | SkISize baseLayerSize = canvas.getBaseLayerSize(); |
| 767 | SkDebugf("size=%d,%d\n", baseLayerSize.width(), baseLayerSize.height()); |
| 768 | |
| 769 | #StdOut |
| 770 | clip=10,30 |
| 771 | size=20,30 |
| 772 | ## |
| 773 | ## |
| 774 | |
| 775 | #ToDo is this the same as the width and height of surface? ## |
| 776 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 777 | #SeeAlso getDeviceClipBounds |
| 778 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 779 | ## |
| 780 | |
| 781 | # ------------------------------------------------------------------------------ |
| 782 | |
| 783 | #Method sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr) |
| 784 | |
| 785 | Creates Surface matching info and props, and associates it with Canvas. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 786 | Returns nullptr if no match found. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 787 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 788 | If props is nullptr, matches Surface_Properties in Canvas. If props is nullptr and Canvas |
| 789 | does not have Surface_Properties, creates Surface with default Surface_Properties. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 790 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 791 | #Param info width, height, Image_Color_Type, Image_Alpha_Type, and Color_Space ## |
| 792 | #Param props Surface_Properties to match; may be nullptr to match Canvas ## |
| 793 | |
| 794 | #Return Surface matching info and props, or nullptr if no match is available ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 795 | |
| 796 | #Example |
| 797 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(5, 6); |
| 798 | SkCanvas* smallCanvas = surface->getCanvas(); |
| 799 | SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(3, 4); |
| 800 | sk_sp<SkSurface> compatible = smallCanvas->makeSurface(imageInfo); |
| 801 | SkDebugf("compatible %c= nullptr\n", compatible == nullptr ? '=' : '!'); |
| 802 | SkDebugf("size = %d, %d\n", compatible->width(), compatible->height()); |
| 803 | |
| 804 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 805 | compatible != nullptr |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 806 | size = 3, 4 |
| 807 | ## |
| 808 | ## |
| 809 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 810 | #SeeAlso SkSurface SkSurface::makeSurface SkImageInfo SkSurfaceProps |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 811 | |
| 812 | ## |
| 813 | |
| 814 | # ------------------------------------------------------------------------------ |
| 815 | |
| 816 | #Method virtual GrContext* getGrContext() |
| 817 | |
| 818 | Returns GPU_Context of the GPU_Surface associated with Canvas. |
| 819 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 820 | #Return GPU_Context, if available; nullptr otherwise ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 821 | |
| 822 | #Example |
| 823 | void draw(SkCanvas* canvas) { |
| 824 | if (canvas->getGrContext()) { |
| 825 | canvas->clear(SK_ColorRED); |
| 826 | } else { |
| 827 | canvas->clear(SK_ColorBLUE); |
| 828 | } |
| 829 | } |
| 830 | ## |
| 831 | |
| 832 | #ToDo fiddle should show both CPU and GPU out ## |
| 833 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 834 | #SeeAlso GrContext |
| 835 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 836 | ## |
| 837 | |
| 838 | # ------------------------------------------------------------------------------ |
| 839 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 840 | #Method void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 841 | |
| 842 | Returns the pixel base address, Image_Info, rowBytes, and origin if the pixels |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 843 | can be read directly. The returned address is only valid |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 844 | while Canvas is in scope and unchanged. Any Canvas call or Surface call |
| 845 | may invalidate the returned address and other returned values. |
| 846 | |
| 847 | If pixels are inaccessible, info, rowBytes, and origin are unchanged. |
| 848 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 849 | #Param info storage for writable pixels' Image_Info; may be nullptr ## |
| 850 | #Param rowBytes storage for writable pixels' row bytes; may be nullptr ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 851 | #Param origin storage for Canvas top Layer origin, its top-left corner; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 852 | may be nullptr |
| 853 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 854 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 855 | #Return address of pixels, or nullptr if inaccessible ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 856 | |
| 857 | #Example |
| 858 | void draw(SkCanvas* canvas) { |
| 859 | if (canvas->accessTopLayerPixels(nullptr, nullptr)) { |
| 860 | canvas->clear(SK_ColorRED); |
| 861 | } else { |
| 862 | canvas->clear(SK_ColorBLUE); |
| 863 | } |
| 864 | } |
| 865 | ## |
| 866 | |
| 867 | #Example |
| 868 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 869 | Draws "ABC" on the device. Then draws "DEF" in Layer, and reads |
| 870 | Layer to add a large dotted "DEF". Finally blends Layer with the |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 871 | device. |
| 872 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 873 | The Layer and blended result appear on the CPU and GPU but the large dotted |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 874 | "DEF" appear only on the CPU. |
| 875 | ## |
| 876 | void draw(SkCanvas* canvas) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 877 | SkPaint paint; |
| 878 | paint.setTextSize(100); |
| 879 | canvas->drawString("ABC", 20, 160, paint); |
| 880 | SkRect layerBounds = SkRect::MakeXYWH(32, 32, 192, 192); |
| 881 | canvas->saveLayerAlpha(&layerBounds, 128); |
| 882 | canvas->clear(SK_ColorWHITE); |
| 883 | canvas->drawString("DEF", 20, 160, paint); |
| 884 | SkImageInfo imageInfo; |
| 885 | size_t rowBytes; |
| 886 | SkIPoint origin; |
| 887 | uint32_t* access = (uint32_t*) canvas->accessTopLayerPixels(&imageInfo, &rowBytes, &origin); |
| 888 | if (access) { |
| 889 | int h = imageInfo.height(); |
| 890 | int v = imageInfo.width(); |
| 891 | int rowWords = rowBytes / sizeof(uint32_t); |
| 892 | for (int y = 0; y < h; ++y) { |
| 893 | int newY = (y - h / 2) * 2 + h / 2; |
| 894 | if (newY < 0 || newY >= h) { |
| 895 | continue; |
| 896 | } |
| 897 | for (int x = 0; x < v; ++x) { |
| 898 | int newX = (x - v / 2) * 2 + v / 2; |
| 899 | if (newX < 0 || newX >= v) { |
| 900 | continue; |
| 901 | } |
| 902 | if (access[y * rowWords + x] == SK_ColorBLACK) { |
| 903 | access[newY * rowWords + newX] = SK_ColorGRAY; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 909 | canvas->restore(); |
| 910 | } |
| 911 | ## |
| 912 | |
| 913 | #ToDo there are no callers of this that I can find. Deprecate? ## |
| 914 | #ToDo fiddle should show both CPU and GPU out ## |
| 915 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 916 | #SeeAlso SkImageInfo SkPixmap |
| 917 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 918 | ## |
| 919 | |
| 920 | # ------------------------------------------------------------------------------ |
| 921 | |
| 922 | #Method SkRasterHandleAllocator::Handle accessTopRasterHandle() const |
| 923 | |
| 924 | Returns custom context that tracks the Matrix and Clip. |
| 925 | |
| 926 | Use Raster_Handle_Allocator to blend Skia drawing with custom drawing, typically performed |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 927 | by the host platform user interface. The custom context returned is generated by |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 928 | SkRasterHandleAllocator::MakeCanvas, which creates a custom canvas with raster storage for |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 929 | the drawing destination. |
| 930 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 931 | #Return context of custom allocation ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 932 | |
| 933 | #Example |
| 934 | #Description |
| 935 | #ToDo ## |
| 936 | ## |
| 937 | #Function |
| 938 | static void DeleteCallback(void*, void* context) { |
| 939 | delete (char*) context; |
| 940 | } |
| 941 | |
| 942 | class CustomAllocator : public SkRasterHandleAllocator { |
| 943 | public: |
| 944 | bool allocHandle(const SkImageInfo& info, Rec* rec) override { |
| 945 | char* context = new char[4]{'s', 'k', 'i', 'a'}; |
| 946 | rec->fReleaseProc = DeleteCallback; |
| 947 | rec->fReleaseCtx = context; |
| 948 | rec->fHandle = context; |
| 949 | rec->fPixels = context; |
| 950 | rec->fRowBytes = 4; |
| 951 | return true; |
| 952 | } |
| 953 | |
| 954 | void updateHandle(Handle handle, const SkMatrix& ctm, const SkIRect& clip_bounds) override { |
| 955 | // apply canvas matrix and clip to custom environment |
| 956 | } |
| 957 | }; |
| 958 | |
| 959 | ## |
| 960 | void draw(SkCanvas* canvas) { |
| 961 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 962 | std::unique_ptr<SkCanvas> c2 = |
| 963 | SkRasterHandleAllocator::MakeCanvas(std::unique_ptr<CustomAllocator>( |
| 964 | new CustomAllocator()), info); |
| 965 | char* context = (char*) c2->accessTopRasterHandle(); |
| 966 | SkDebugf("context = %.4s\n", context); |
| 967 | |
| 968 | } |
| 969 | #StdOut |
| 970 | context = skia |
| 971 | ## |
| 972 | #ToDo skstd::make_unique could not be used because def is private -- note to fix in c++14? ## |
| 973 | ## |
| 974 | |
| 975 | #SeeAlso SkRasterHandleAllocator |
| 976 | |
| 977 | ## |
| 978 | |
| 979 | # ------------------------------------------------------------------------------ |
| 980 | |
| 981 | #Method bool peekPixels(SkPixmap* pixmap) |
| 982 | |
| 983 | Returns true if Canvas has direct access to its pixels. |
| 984 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 985 | Pixels are readable when Device is raster. Pixels are not readable when Canvas |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 986 | is returned from GPU_Surface, returned by SkDocument::beginPage, returned by |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 987 | SkPictureRecorder::beginRecording, or Canvas is the base of a utility class |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 988 | like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 989 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 990 | pixmap is valid only while Canvas is in scope and unchanged. Any |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 991 | Canvas or Surface call may invalidate the pixmap values. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 992 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 993 | #Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 994 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 995 | #Return true if Canvas has direct access to pixels ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 996 | |
| 997 | #Example |
| 998 | SkPixmap pixmap; |
| 999 | if (canvas->peekPixels(&pixmap)) { |
| 1000 | SkDebugf("width=%d height=%d\n", pixmap.bounds().width(), pixmap.bounds().height()); |
| 1001 | } |
| 1002 | #StdOut |
| 1003 | width=256 height=256 |
| 1004 | ## |
| 1005 | ## |
| 1006 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1007 | #SeeAlso readPixels SkBitmap::peekPixels SkImage::peekPixels SkSurface::peekPixels |
| 1008 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1009 | ## |
| 1010 | |
| 1011 | # ------------------------------------------------------------------------------ |
| 1012 | |
| 1013 | #Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 1014 | int srcX, int srcY) |
| 1015 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1016 | Copies Rect of pixels from Canvas into dstPixels. Matrix and Clip are |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1017 | ignored. |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1018 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1019 | Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). |
| 1020 | Destination Rect corners are (0, 0) and (dstInfo.width(), dstInfo.height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1021 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1022 | converting to dstInfo.colorType() and dstInfo.alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1023 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1024 | Pixels are readable when Device is raster, or backed by a GPU. |
| 1025 | Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, |
| 1026 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1027 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1028 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1029 | The destination pixel storage must be allocated by the caller. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1030 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1031 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1032 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1033 | are copied. dstPixels contents outside Rect intersection are unchanged. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1034 | |
| 1035 | Pass negative values for srcX or srcY to offset pixels across or down destination. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1036 | |
| 1037 | Does not copy, and returns false if: |
| 1038 | |
| 1039 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1040 | # Source and destination rectangles do not intersect. ## |
| 1041 | # Canvas pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). ## |
| 1042 | # Canvas pixels are not readable; for instance, Canvas is document-based. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1043 | # dstRowBytes is too small to contain one row of pixels. ## |
| 1044 | ## |
| 1045 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1046 | #Param dstInfo width, height, Image_Color_Type, and Image_Alpha_Type of dstPixels ## |
| 1047 | #Param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger ## |
| 1048 | #Param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger ## |
| 1049 | #Param srcX offset into readable pixels in x; may be negative ## |
| 1050 | #Param srcY offset into readable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1051 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1052 | #Return true if pixels were copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1053 | |
| 1054 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1055 | #Width 64 |
| 1056 | #Height 64 |
| 1057 | #Description |
| 1058 | A black circle drawn on a blue background provides an image to copy. |
| 1059 | readPixels copies one quarter of the canvas into each of the four corners. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1060 | The copied quarter circles overdraw the original circle. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1061 | ## |
| 1062 | canvas->clear(SK_ColorBLUE); |
| 1063 | SkPaint paint; |
| 1064 | canvas->drawCircle(32, 32, 28, paint); |
| 1065 | SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType); |
| 1066 | sk_sp<SkData> data(SkData::MakeUninitialized(info.minRowBytes() * info.height())); |
| 1067 | sk_bzero(data->writable_data(), info.minRowBytes() * info.height()); |
| 1068 | for (int x : { 32, -32 } ) { |
| 1069 | for (int y : { 32, -32 } ) { |
| 1070 | canvas->readPixels(info, data->writable_data(), info.minRowBytes(), x, y); |
| 1071 | } |
| 1072 | } |
| 1073 | sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, info.minRowBytes()); |
| 1074 | canvas->drawImage(image, 0, 0); |
| 1075 | ## |
| 1076 | |
| 1077 | #Example |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1078 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1079 | Canvas returned by Raster_Surface has Premultiplied pixel values. |
| 1080 | clear() takes Unpremultiplied input with Color_Alpha equal 0x80 |
| 1081 | and Color_RGB equal 0x55, 0xAA, 0xFF. Color_RGB is multiplied by Color_Alpha |
| 1082 | to generate Premultiplied value 0x802B5580. readPixels converts pixel back |
| 1083 | to Unpremultiplied value 0x8056A9FF, introducing error. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1084 | ## |
| 1085 | canvas->clear(0x8055aaff); |
| 1086 | for (SkAlphaType alphaType : { kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) { |
| 1087 | uint32_t pixel = 0; |
| 1088 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, alphaType); |
| 1089 | if (canvas->readPixels(info, &pixel, 4, 0, 0)) { |
| 1090 | SkDebugf("pixel = %08x\n", pixel); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | #StdOut |
| 1095 | pixel = 802b5580 |
| 1096 | pixel = 8056a9ff |
| 1097 | ## |
| 1098 | ## |
| 1099 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1100 | #SeeAlso peekPixels writePixels drawBitmap drawImage SkBitmap::readPixels SkPixmap::readPixels SkImage::readPixels SkSurface::readPixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1101 | |
| 1102 | ## |
| 1103 | |
| 1104 | # ------------------------------------------------------------------------------ |
| 1105 | |
| 1106 | #Method bool readPixels(const SkPixmap& pixmap, int srcX, int srcY) |
| 1107 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1108 | Copies Rect of pixels from Canvas into pixmap. Matrix and Clip are |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1109 | ignored. |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1110 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1111 | Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). |
| 1112 | Destination Rect corners are (0, 0) and (pixmap.width(), pixmap.height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1113 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1114 | converting to pixmap.colorType() and pixmap.alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1115 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1116 | Pixels are readable when Device is raster, or backed by a GPU. |
| 1117 | Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, |
| 1118 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1119 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1120 | |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1121 | Caller must allocate pixel storage in pixmap if needed. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1122 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1123 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1124 | do not match. Only pixels within both source and destination Rects |
| 1125 | are copied. pixmap pixels contents outside Rect intersection are unchanged. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1126 | |
| 1127 | Pass negative values for srcX or srcY to offset pixels across or down pixmap. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1128 | |
| 1129 | Does not copy, and returns false if: |
| 1130 | |
| 1131 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1132 | # Source and destination rectangles do not intersect. ## |
| 1133 | # Canvas pixels could not be converted to pixmap.colorType() or pixmap.alphaType(). ## |
| 1134 | # Canvas pixels are not readable; for instance, Canvas is document-based. ## |
| 1135 | # Pixmap pixels could not be allocated. ## |
| 1136 | # pixmap.rowBytes() is too small to contain one row of pixels. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1137 | ## |
| 1138 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1139 | #Param pixmap storage for pixels copied from Canvas ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1140 | #Param srcX offset into readable pixels in x; may be negative ## |
| 1141 | #Param srcY offset into readable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1142 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1143 | #Return true if pixels were copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1144 | |
| 1145 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1146 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1147 | clear() takes Unpremultiplied input with Color_Alpha equal 0x80 |
| 1148 | and Color_RGB equal 0x55, 0xAA, 0xFF. Color_RGB is multiplied by Color_Alpha |
| 1149 | to generate Premultiplied value 0x802B5580. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1150 | ## |
| 1151 | void draw(SkCanvas* canvas) { |
| 1152 | canvas->clear(0x8055aaff); |
| 1153 | uint32_t pixels[1] = { 0 }; |
| 1154 | SkPixmap pixmap(SkImageInfo::MakeN32Premul(1, 1), pixels, 4); |
| 1155 | canvas->readPixels(pixmap, 0, 0); |
| 1156 | SkDebugf("pixel = %08x\n", pixels[0]); |
| 1157 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1158 | #StdOut |
| 1159 | pixel = 802b5580 |
| 1160 | ## |
| 1161 | ## |
| 1162 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1163 | #SeeAlso peekPixels writePixels drawBitmap drawImage SkBitmap::readPixels SkPixmap::readPixels SkImage::readPixels SkSurface::readPixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1164 | |
| 1165 | ## |
| 1166 | |
| 1167 | # ------------------------------------------------------------------------------ |
| 1168 | |
| 1169 | #Method bool readPixels(const SkBitmap& bitmap, int srcX, int srcY) |
| 1170 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1171 | Copies Rect of pixels from Canvas into bitmap. Matrix and Clip are |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1172 | ignored. |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1173 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1174 | Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1175 | Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1176 | Copies each readable pixel intersecting both rectangles, without scaling, |
| 1177 | converting to bitmap.colorType() and bitmap.alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1178 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1179 | Pixels are readable when Device is raster, or backed by a GPU. |
| 1180 | Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, |
| 1181 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1182 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1183 | |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1184 | Caller must allocate pixel storage in bitmap if needed. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1185 | |
| 1186 | Bitmap values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1187 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1188 | are copied. Bitmap pixels outside Rect intersection are unchanged. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1189 | |
| 1190 | Pass negative values for srcX or srcY to offset pixels across or down bitmap. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1191 | |
| 1192 | Does not copy, and returns false if: |
| 1193 | |
| 1194 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1195 | # Source and destination rectangles do not intersect. ## |
| 1196 | # Canvas pixels could not be converted to bitmap.colorType() or bitmap.alphaType(). ## |
| 1197 | # Canvas pixels are not readable; for instance, Canvas is document-based. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1198 | # bitmap pixels could not be allocated. ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1199 | # bitmap.rowBytes() is too small to contain one row of pixels. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1200 | ## |
| 1201 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1202 | #Param bitmap storage for pixels copied from Canvas ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1203 | #Param srcX offset into readable pixels in x; may be negative ## |
| 1204 | #Param srcY offset into readable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1205 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1206 | #Return true if pixels were copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1207 | |
| 1208 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1209 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1210 | clear() takes Unpremultiplied input with Color_Alpha equal 0x80 |
| 1211 | and Color_RGB equal 0x55, 0xAA, 0xFF. Color_RGB is multiplied by Color_Alpha |
| 1212 | to generate Premultiplied value 0x802B5580. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1213 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1214 | void draw(SkCanvas* canvas) { |
| 1215 | canvas->clear(0x8055aaff); |
| 1216 | SkBitmap bitmap; |
| 1217 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1)); |
| 1218 | canvas->readPixels(bitmap, 0, 0); |
| 1219 | SkDebugf("pixel = %08x\n", bitmap.getAddr32(0, 0)[0]); |
| 1220 | } |
| 1221 | #StdOut |
| 1222 | pixel = 802b5580 |
| 1223 | ## |
| 1224 | ## |
| 1225 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1226 | #SeeAlso peekPixels writePixels drawBitmap drawImage SkBitmap::readPixels SkPixmap::readPixels SkImage::readPixels SkSurface::readPixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1227 | |
| 1228 | ## |
| 1229 | |
| 1230 | # ------------------------------------------------------------------------------ |
| 1231 | |
| 1232 | #Method bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y) |
| 1233 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1234 | Copies Rect from pixels to Canvas. Matrix and Clip are ignored. |
| 1235 | Source Rect corners are (0, 0) and (info.width(), info.height()). |
| 1236 | Destination Rect corners are (x, y) and |
| 1237 | (imageInfo().width(), imageInfo().height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1238 | |
| 1239 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1240 | converting to imageInfo().colorType() and imageInfo().alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1241 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1242 | Pixels are writable when Device is raster, or backed by a GPU. |
| 1243 | Pixels are not writable when SkCanvas is returned by SkDocument::beginPage, |
| 1244 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1245 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1246 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1247 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1248 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1249 | are copied. Canvas pixels outside Rect intersection are unchanged. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1250 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1251 | Pass negative values for x or y to offset pixels to the left or |
| 1252 | above Canvas pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1253 | |
| 1254 | Does not copy, and returns false if: |
| 1255 | |
| 1256 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1257 | # Source and destination rectangles do not intersect. ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1258 | # pixels could not be converted to Canvas imageInfo().colorType() or |
| 1259 | imageInfo().alphaType(). ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1260 | # Canvas pixels are not writable; for instance, Canvas is document-based. ## |
| 1261 | # rowBytes is too small to contain one row of pixels. ## |
| 1262 | ## |
| 1263 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1264 | #Param info width, height, Image_Color_Type, and Image_Alpha_Type of pixels ## |
| 1265 | #Param pixels pixels to copy, of size info.height() times rowBytes, or larger ## |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1266 | #Param rowBytes size of one row of pixels; info.width() times pixel size, or larger ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1267 | #Param x offset into Canvas writable pixels in x; may be negative ## |
| 1268 | #Param y offset into Canvas writable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1269 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1270 | #Return true if pixels were written to Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1271 | |
| 1272 | #Example |
| 1273 | SkImageInfo imageInfo = SkImageInfo::MakeN32(256, 1, kPremul_SkAlphaType); |
| 1274 | for (int y = 0; y < 256; ++y) { |
| 1275 | uint32_t pixels[256]; |
| 1276 | for (int x = 0; x < 256; ++x) { |
| 1277 | pixels[x] = SkColorSetARGB(x, x + y, x, x - y); |
| 1278 | } |
| 1279 | canvas->writePixels(imageInfo, &pixels, sizeof(pixels), 0, y); |
| 1280 | } |
| 1281 | ## |
| 1282 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1283 | #SeeAlso readPixels drawBitmap drawImage SkBitmap::writePixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1284 | |
| 1285 | ## |
| 1286 | |
| 1287 | # ------------------------------------------------------------------------------ |
| 1288 | |
| 1289 | #Method bool writePixels(const SkBitmap& bitmap, int x, int y) |
| 1290 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1291 | Copies Rect from pixels to Canvas. Matrix and Clip are ignored. |
| 1292 | Source Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1293 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1294 | Destination Rect corners are (x, y) and |
| 1295 | (imageInfo().width(), imageInfo().height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1296 | |
| 1297 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1298 | converting to imageInfo().colorType() and imageInfo().alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1299 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1300 | Pixels are writable when Device is raster, or backed by a GPU. |
| 1301 | Pixels are not writable when SkCanvas is returned by SkDocument::beginPage, |
| 1302 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1303 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1304 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1305 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1306 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1307 | are copied. Canvas pixels outside Rect intersection are unchanged. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1308 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1309 | Pass negative values for x or y to offset pixels to the left or |
| 1310 | above Canvas pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1311 | |
| 1312 | Does not copy, and returns false if: |
| 1313 | |
| 1314 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1315 | # Source and destination rectangles do not intersect. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1316 | # bitmap does not have allocated pixels. ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1317 | # bitmap pixels could not be converted to Canvas imageInfo().colorType() or |
| 1318 | imageInfo().alphaType(). ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1319 | # Canvas pixels are not writable; for instance, Canvas is document based. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1320 | # bitmap pixels are inaccessible; for instance, bitmap wraps a texture. ## |
| 1321 | ## |
| 1322 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1323 | #Param bitmap contains pixels copied to Canvas ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1324 | #Param x offset into Canvas writable pixels in x; may be negative ## |
| 1325 | #Param y offset into Canvas writable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1326 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1327 | #Return true if pixels were written to Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1328 | |
| 1329 | #Example |
| 1330 | void draw(SkCanvas* canvas) { |
| 1331 | SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(2, 2); |
| 1332 | SkBitmap bitmap; |
| 1333 | bitmap.setInfo(imageInfo); |
| 1334 | uint32_t pixels[4]; |
| 1335 | bitmap.setPixels(pixels); |
| 1336 | for (int y = 0; y < 256; y += 2) { |
| 1337 | for (int x = 0; x < 256; x += 2) { |
| 1338 | pixels[0] = SkColorSetRGB(x, y, x | y); |
| 1339 | pixels[1] = SkColorSetRGB(x ^ y, y, x); |
| 1340 | pixels[2] = SkColorSetRGB(x, x & y, y); |
| 1341 | pixels[3] = SkColorSetRGB(~x, ~y, x); |
| 1342 | canvas->writePixels(bitmap, x, y); |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | ## |
| 1347 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1348 | #SeeAlso readPixels drawBitmap drawImage SkBitmap::writePixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1349 | |
| 1350 | ## |
| 1351 | |
| 1352 | # ------------------------------------------------------------------------------ |
| 1353 | #Topic State_Stack |
| 1354 | |
| 1355 | Canvas maintains a stack of state that allows hierarchical drawing, commonly used |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1356 | to implement windows and views. The initial state has an identity matrix and and |
| 1357 | an infinite clip. Even with a wide-open clip, drawing is constrained by the |
| 1358 | bounds of the Canvas Surface or Device. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1359 | |
| 1360 | Canvas savable state consists of Clip, Matrix, and Draw_Filter. |
| 1361 | Clip describes the area that may be drawn to. |
| 1362 | Matrix transforms the geometry. |
| 1363 | Draw_Filter (deprecated on most platforms) modifies the paint before drawing. |
| 1364 | |
| 1365 | save(), saveLayer, saveLayerPreserveLCDTextRequests, and saveLayerAlpha |
| 1366 | save state and return the depth of the stack. |
| 1367 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1368 | restore(), restoreToCount, and ~SkCanvas() revert state to its value when saved. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1369 | |
| 1370 | Each state on the stack intersects Clip with the previous Clip, |
| 1371 | and concatenates Matrix with the previous Matrix. |
| 1372 | The intersected Clip makes the drawing area the same or smaller; |
| 1373 | the concatenated Matrix may move the origin and potentially scale or rotate |
| 1374 | the coordinate space. |
| 1375 | |
| 1376 | Canvas does not require balancing the state stack but it is a good idea |
| 1377 | to do so. Calling save() without restore() will eventually cause Skia to fail; |
| 1378 | mismatched save() and restore() create hard to find bugs. |
| 1379 | |
| 1380 | It is not possible to use state to draw outside of the clip defined by the |
| 1381 | previous state. |
| 1382 | |
| 1383 | #Example |
| 1384 | #Description |
| 1385 | Draw to ever smaller clips; then restore drawing to full canvas. |
| 1386 | Note that the second clipRect is not permitted to enlarge Clip. |
| 1387 | ## |
| 1388 | #Height 160 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1389 | void draw(SkCanvas* canvas) { |
| 1390 | SkPaint paint; |
| 1391 | canvas->save(); // records stack depth to restore |
| 1392 | canvas->clipRect(SkRect::MakeWH(100, 100)); // constrains drawing to clip |
| 1393 | canvas->clear(SK_ColorRED); // draws to limit of clip |
| 1394 | canvas->save(); // records stack depth to restore |
| 1395 | canvas->clipRect(SkRect::MakeWH(50, 150)); // Rect below 100 is ignored |
| 1396 | canvas->clear(SK_ColorBLUE); // draws to smaller clip |
| 1397 | canvas->restore(); // enlarges clip |
| 1398 | canvas->drawLine(20, 20, 150, 150, paint); // line below 100 is not drawn |
| 1399 | canvas->restore(); // enlarges clip |
| 1400 | canvas->drawLine(150, 20, 50, 120, paint); // line below 100 is drawn |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1401 | } |
| 1402 | ## |
| 1403 | |
| 1404 | Each Clip uses the current Matrix for its coordinates. |
| 1405 | |
| 1406 | #Example |
| 1407 | #Description |
| 1408 | While clipRect is given the same rectangle twice, Matrix makes the second |
| 1409 | clipRect draw at half the size of the first. |
| 1410 | ## |
| 1411 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1412 | void draw(SkCanvas* canvas) { |
| 1413 | canvas->clipRect(SkRect::MakeWH(100, 100)); |
| 1414 | canvas->clear(SK_ColorRED); |
| 1415 | canvas->scale(.5, .5); |
| 1416 | canvas->clipRect(SkRect::MakeWH(100, 100)); |
| 1417 | canvas->clear(SK_ColorBLUE); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1418 | } |
| 1419 | ## |
| 1420 | |
| 1421 | #SeeAlso save() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha restore() restoreToCount |
| 1422 | |
| 1423 | #Method int save() |
| 1424 | |
| 1425 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms). |
| 1426 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1427 | restoring the Matrix, Clip, and Draw_Filter to their state when save() was called. |
| 1428 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1429 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix, |
| 1430 | and resetMatrix. Clip may be changed by clipRect, clipRRect, clipPath, clipRegion. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1431 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1432 | Saved Canvas state is put on a stack; multiple calls to save() should be balance |
| 1433 | by an equal number of calls to restore(). |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1434 | |
| 1435 | Call restoreToCount with result to restore this and subsequent saves. |
| 1436 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1437 | #Return depth of saved stack ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1438 | |
| 1439 | #Example |
| 1440 | #Description |
| 1441 | The black square is translated 50 pixels down and to the right. |
| 1442 | Restoring Canvas state removes translate() from Canvas stack; |
| 1443 | the red square is not translated, and is drawn at the origin. |
| 1444 | ## |
| 1445 | #Height 100 |
| 1446 | void draw(SkCanvas* canvas) { |
| 1447 | SkPaint paint; |
| 1448 | SkRect rect = { 0, 0, 25, 25 }; |
| 1449 | canvas->drawRect(rect, paint); |
| 1450 | canvas->save(); |
| 1451 | canvas->translate(50, 50); |
| 1452 | canvas->drawRect(rect, paint); |
| 1453 | canvas->restore(); |
| 1454 | paint.setColor(SK_ColorRED); |
| 1455 | canvas->drawRect(rect, paint); |
| 1456 | } |
| 1457 | ## |
| 1458 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1459 | #SeeAlso saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha restore() restoreToCount |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1460 | |
| 1461 | ## |
| 1462 | |
| 1463 | # ------------------------------------------------------------------------------ |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1464 | |
| 1465 | #Method void restore() |
| 1466 | |
| 1467 | Removes changes to Matrix, Clip, and Draw_Filter since Canvas state was |
| 1468 | last saved. The state is removed from the stack. |
| 1469 | |
| 1470 | Does nothing if the stack is empty. |
| 1471 | |
| 1472 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1473 | void draw(SkCanvas* canvas) { |
| 1474 | SkCanvas simple; |
| 1475 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
| 1476 | simple.restore(); |
| 1477 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1478 | } |
| 1479 | ## |
| 1480 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1481 | #SeeAlso save() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha restoreToCount |
| 1482 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1483 | ## |
| 1484 | |
| 1485 | # ------------------------------------------------------------------------------ |
| 1486 | |
| 1487 | #Method int getSaveCount() const |
| 1488 | |
| 1489 | Returns the number of saved states, each containing: Matrix, Clip, and Draw_Filter. |
| 1490 | Equals the number of save() calls less the number of restore() calls plus one. |
| 1491 | The save count of a new canvas is one. |
| 1492 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1493 | #Return depth of save state stack ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1494 | |
| 1495 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1496 | void draw(SkCanvas* canvas) { |
| 1497 | SkCanvas simple; |
| 1498 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
| 1499 | simple.save(); |
| 1500 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
| 1501 | simple.restore(); |
| 1502 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1503 | } |
| 1504 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1505 | depth = 1 |
| 1506 | depth = 2 |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1507 | depth = 1 |
| 1508 | ## |
| 1509 | ## |
| 1510 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1511 | #SeeAlso save() restore() restoreToCount |
| 1512 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1513 | ## |
| 1514 | |
| 1515 | # ------------------------------------------------------------------------------ |
| 1516 | |
| 1517 | #Method void restoreToCount(int saveCount) |
| 1518 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1519 | Restores state to Matrix, Clip, and Draw_Filter values when save(), saveLayer, |
| 1520 | saveLayerPreserveLCDTextRequests, or saveLayerAlpha returned saveCount. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1521 | |
| 1522 | Does nothing if saveCount is greater than state stack count. |
| 1523 | Restores state to initial values if saveCount is less than or equal to one. |
| 1524 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1525 | #Param saveCount depth of state stack to restore ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1526 | |
| 1527 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1528 | void draw(SkCanvas* canvas) { |
| 1529 | SkDebugf("depth = %d\n", canvas->getSaveCount()); |
| 1530 | canvas->save(); |
| 1531 | canvas->save(); |
| 1532 | SkDebugf("depth = %d\n", canvas->getSaveCount()); |
| 1533 | canvas->restoreToCount(0); |
| 1534 | SkDebugf("depth = %d\n", canvas->getSaveCount()); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1535 | } |
| 1536 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1537 | depth = 1 |
| 1538 | depth = 3 |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1539 | depth = 1 |
| 1540 | ## |
| 1541 | ## |
| 1542 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1543 | #SeeAlso restore() getSaveCount save() |
| 1544 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1545 | ## |
| 1546 | |
| 1547 | #Topic State_Stack ## |
| 1548 | |
| 1549 | # ------------------------------------------------------------------------------ |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1550 | |
| 1551 | #Topic Layer |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1552 | #Substitute layer |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1553 | #Alias Layers |
| 1554 | |
| 1555 | Layer allocates a temporary Bitmap to draw into. When the drawing is |
| 1556 | complete, the Bitmap is drawn into the Canvas. |
| 1557 | |
| 1558 | Layer is saved in a stack along with other saved state. When state with a Layer |
| 1559 | is restored, the Bitmap is drawn into the previous Layer. |
| 1560 | |
| 1561 | Layer may be initialized with the contents of the previous Layer. When Layer is |
| 1562 | restored, its Bitmap can be modified by Paint passed to Layer to apply |
| 1563 | Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode. |
| 1564 | |
| 1565 | #Method int saveLayer(const SkRect* bounds, const SkPaint* paint) |
| 1566 | |
| 1567 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1568 | and allocates a Bitmap for subsequent drawing. |
| 1569 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1570 | and draws the Bitmap. |
| 1571 | |
| 1572 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1573 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1574 | clipPath, clipRegion. |
| 1575 | |
| 1576 | Rect bounds suggests but does not define the Bitmap size. To clip drawing to |
| 1577 | a specific rectangle, use clipRect. |
| 1578 | |
| 1579 | Optional Paint paint applies Color_Alpha, Color_Filter, Image_Filter, and |
| 1580 | Blend_Mode when restore() is called. |
| 1581 | |
| 1582 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1583 | |
| 1584 | #Param bounds hint to limit the size of the Layer; may be nullptr ## |
| 1585 | #Param paint graphics state for Layer; may be nullptr ## |
| 1586 | |
| 1587 | #Return depth of saved stack ## |
| 1588 | |
| 1589 | #Example |
| 1590 | #Description |
| 1591 | Rectangles are blurred by Image_Filter when restore() draws Layer to main |
| 1592 | Canvas. |
| 1593 | ## |
| 1594 | #Height 128 |
| 1595 | void draw(SkCanvas* canvas) { |
| 1596 | SkPaint paint, blur; |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1597 | blur.setImageFilter(SkBlurImageFilter::Make(3, 3, nullptr)); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1598 | canvas->saveLayer(nullptr, &blur); |
| 1599 | SkRect rect = { 25, 25, 50, 50}; |
| 1600 | canvas->drawRect(rect, paint); |
| 1601 | canvas->translate(50, 50); |
| 1602 | paint.setColor(SK_ColorRED); |
| 1603 | canvas->drawRect(rect, paint); |
| 1604 | canvas->restore(); |
| 1605 | } |
| 1606 | ## |
| 1607 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1608 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1609 | |
| 1610 | ## |
| 1611 | |
| 1612 | #Method int saveLayer(const SkRect& bounds, const SkPaint* paint) |
| 1613 | |
| 1614 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1615 | and allocates a Bitmap for subsequent drawing. |
| 1616 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1617 | and draws the Bitmap. |
| 1618 | |
| 1619 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1620 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1621 | clipPath, clipRegion. |
| 1622 | |
| 1623 | Rect bounds suggests but does not define the Layer size. To clip drawing to |
| 1624 | a specific rectangle, use clipRect. |
| 1625 | |
| 1626 | Optional Paint paint applies Color_Alpha, Color_Filter, Image_Filter, and |
| 1627 | Blend_Mode when restore() is called. |
| 1628 | |
| 1629 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1630 | |
| 1631 | #Param bounds hint to limit the size of Layer; may be nullptr ## |
| 1632 | #Param paint graphics state for Layer; may be nullptr ## |
| 1633 | |
| 1634 | #Return depth of saved stack ## |
| 1635 | |
| 1636 | #Example |
| 1637 | #Description |
| 1638 | Rectangles are blurred by Image_Filter when restore() draws Layer to main Canvas. |
| 1639 | The red rectangle is clipped; it does not fully fit on Layer. |
| 1640 | Image_Filter blurs past edge of Layer so red rectangle is blurred on all sides. |
| 1641 | ## |
| 1642 | #Height 128 |
| 1643 | void draw(SkCanvas* canvas) { |
| 1644 | SkPaint paint, blur; |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1645 | blur.setImageFilter(SkBlurImageFilter::Make(3, 3, nullptr)); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1646 | canvas->saveLayer(SkRect::MakeWH(90, 90), &blur); |
| 1647 | SkRect rect = { 25, 25, 50, 50}; |
| 1648 | canvas->drawRect(rect, paint); |
| 1649 | canvas->translate(50, 50); |
| 1650 | paint.setColor(SK_ColorRED); |
| 1651 | canvas->drawRect(rect, paint); |
| 1652 | canvas->restore(); |
| 1653 | } |
| 1654 | ## |
| 1655 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1656 | #SeeAlso save() restore() saveLayerPreserveLCDTextRequests saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1657 | |
| 1658 | ## |
| 1659 | |
| 1660 | #Method int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint) |
| 1661 | |
| 1662 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1663 | and allocates a Bitmap for subsequent drawing. |
| 1664 | LCD_Text is preserved when the Layer is drawn to the prior Layer. |
| 1665 | |
| 1666 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1667 | and draws Layer. |
| 1668 | |
| 1669 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1670 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1671 | clipPath, clipRegion. |
| 1672 | |
| 1673 | Rect bounds suggests but does not define the Layer size. To clip drawing to |
| 1674 | a specific rectangle, use clipRect. |
| 1675 | |
| 1676 | Optional Paint paint applies Color_Alpha, Color_Filter, Image_Filter, and |
| 1677 | Blend_Mode when restore() is called. |
| 1678 | |
| 1679 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1680 | |
| 1681 | Draw text on an opaque background so that LCD_Text blends correctly with the |
| 1682 | prior Layer. LCD_Text drawn on a background with transparency may result in |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1683 | incorrect blending. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1684 | |
| 1685 | #Param bounds hint to limit the size of Layer; may be nullptr ## |
| 1686 | #Param paint graphics state for Layer; may be nullptr ## |
| 1687 | |
| 1688 | #Return depth of saved stack ## |
| 1689 | |
| 1690 | #Example |
| 1691 | SkPaint paint; |
| 1692 | paint.setAntiAlias(true); |
| 1693 | paint.setLCDRenderText(true); |
| 1694 | paint.setTextSize(20); |
| 1695 | for (auto preserve : { false, true } ) { |
| 1696 | preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr) |
| 1697 | : canvas->saveLayer(nullptr, nullptr); |
| 1698 | SkPaint p; |
| 1699 | p.setColor(SK_ColorWHITE); |
| 1700 | // Comment out the next line to draw on a non-opaque background. |
| 1701 | canvas->drawRect(SkRect::MakeLTRB(25, 40, 200, 70), p); |
| 1702 | canvas->drawString("Hamburgefons", 30, 60, paint); |
| 1703 | |
| 1704 | p.setColor(0xFFCCCCCC); |
| 1705 | canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p); |
| 1706 | canvas->drawString("Hamburgefons", 30, 90, paint); |
| 1707 | |
| 1708 | canvas->restore(); |
| 1709 | canvas->translate(0, 80); |
| 1710 | } |
| 1711 | ## |
| 1712 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1713 | #SeeAlso save() restore() saveLayer saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1714 | |
| 1715 | ## |
| 1716 | |
| 1717 | #Method int saveLayerAlpha(const SkRect* bounds, U8CPU alpha) |
| 1718 | |
| 1719 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1720 | and allocates Bitmap for subsequent drawing. |
| 1721 | |
| 1722 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1723 | and blends Layer with alpha opacity onto prior Layer. |
| 1724 | |
| 1725 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1726 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1727 | clipPath, clipRegion. |
| 1728 | |
| 1729 | Rect bounds suggests but does not define Layer size. To clip drawing to |
| 1730 | a specific rectangle, use clipRect. |
| 1731 | |
| 1732 | alpha of zero is fully transparent, 255 is fully opaque. |
| 1733 | |
| 1734 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1735 | |
| 1736 | #Param bounds hint to limit the size of Layer; may be nullptr ## |
| 1737 | #Param alpha opacity of Layer ## |
| 1738 | |
| 1739 | #Return depth of saved stack ## |
| 1740 | |
| 1741 | #Example |
| 1742 | SkPaint paint; |
| 1743 | paint.setColor(SK_ColorRED); |
| 1744 | canvas->drawCircle(50, 50, 50, paint); |
| 1745 | canvas->saveLayerAlpha(nullptr, 128); |
| 1746 | paint.setColor(SK_ColorBLUE); |
| 1747 | canvas->drawCircle(100, 50, 50, paint); |
| 1748 | paint.setColor(SK_ColorGREEN); |
| 1749 | paint.setAlpha(128); |
| 1750 | canvas->drawCircle(75, 90, 50, paint); |
| 1751 | canvas->restore(); |
| 1752 | ## |
| 1753 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1754 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1755 | |
| 1756 | ## |
| 1757 | |
| 1758 | #Enum |
| 1759 | |
| 1760 | #Code |
| 1761 | enum { |
| 1762 | kIsOpaque_SaveLayerFlag = 1 << 0, |
| 1763 | kPreserveLCDText_SaveLayerFlag = 1 << 1, |
| 1764 | kInitWithPrevious_SaveLayerFlag = 1 << 2, |
| 1765 | kDontClipToLayer_Legacy_SaveLayerFlag = kDontClipToLayer_PrivateSaveLayerFlag, |
| 1766 | }; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1767 | ## |
| 1768 | |
| 1769 | SaveLayerFlags provides options that may be used in any combination in SaveLayerRec, |
| 1770 | defining how Layer allocated by saveLayer operates. |
| 1771 | |
| 1772 | #Const kIsOpaque_SaveLayerFlag 1 |
| 1773 | Creates Layer without transparency. Flag is ignored if Layer Paint contains |
| 1774 | Image_Filter or Color_Filter. |
| 1775 | ## |
| 1776 | |
| 1777 | #Const kPreserveLCDText_SaveLayerFlag 2 |
| 1778 | Creates Layer for LCD text. Flag is ignored if Layer Paint contains |
| 1779 | Image_Filter or Color_Filter. |
| 1780 | ## |
| 1781 | |
| 1782 | #Const kInitWithPrevious_SaveLayerFlag 4 |
| 1783 | Initializes Layer with the contents of the previous Layer. |
| 1784 | ## |
| 1785 | |
| 1786 | #Const kDontClipToLayer_Legacy_SaveLayerFlag 0x80000000 |
| 1787 | #Private |
| 1788 | to be deprecated: bug.skia.org/2440 |
| 1789 | ## |
| 1790 | Only present on Android. |
| 1791 | Skips setting a clip to the Layer bounds. |
| 1792 | ## |
| 1793 | |
| 1794 | #Example |
| 1795 | #Height 160 |
| 1796 | #Description |
| 1797 | Canvas Layer captures red and blue circles scaled up by four. |
| 1798 | scalePaint blends Layer back with transparency. |
| 1799 | ## |
| 1800 | void draw(SkCanvas* canvas) { |
| 1801 | SkPaint redPaint, bluePaint, scalePaint; |
| 1802 | redPaint.setColor(SK_ColorRED); |
| 1803 | canvas->drawCircle(21, 21, 8, redPaint); |
| 1804 | bluePaint.setColor(SK_ColorBLUE); |
| 1805 | canvas->drawCircle(31, 21, 8, bluePaint); |
| 1806 | SkMatrix matrix; |
| 1807 | matrix.setScale(4, 4); |
| 1808 | scalePaint.setAlpha(0x40); |
| 1809 | scalePaint.setImageFilter( |
| 1810 | SkImageFilter::MakeMatrixFilter(matrix, kNone_SkFilterQuality, nullptr)); |
| 1811 | SkCanvas::SaveLayerRec saveLayerRec(nullptr, &scalePaint, |
| 1812 | SkCanvas::kInitWithPrevious_SaveLayerFlag); |
| 1813 | canvas->saveLayer(saveLayerRec); |
| 1814 | canvas->restore(); |
| 1815 | } |
| 1816 | ## |
| 1817 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1818 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1819 | |
| 1820 | #Enum ## |
| 1821 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1822 | #Typedef uint32_t SaveLayerFlags |
| 1823 | |
| 1824 | ## |
| 1825 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1826 | #Struct SaveLayerRec |
| 1827 | |
| 1828 | #Code |
| 1829 | struct SaveLayerRec { |
| 1830 | SaveLayerRec*(... |
| 1831 | |
| 1832 | const SkRect* fBounds; |
| 1833 | const SkPaint* fPaint; |
| 1834 | const SkImageFilter* fBackdrop; |
| 1835 | SaveLayerFlags fSaveLayerFlags; |
| 1836 | }; |
| 1837 | ## |
| 1838 | |
| 1839 | SaveLayerRec contains the state used to create the Layer. |
| 1840 | |
| 1841 | #Member const SkRect* fBounds |
| 1842 | fBounds is used as a hint to limit the size of Layer; may be nullptr. |
| 1843 | fBounds suggests but does not define Layer size. To clip drawing to |
| 1844 | a specific rectangle, use clipRect. |
| 1845 | ## |
| 1846 | |
| 1847 | #Member const SkPaint* fPaint |
| 1848 | fPaint modifies how Layer overlays the prior Layer; may be nullptr. |
| 1849 | Color_Alpha, Blend_Mode, Color_Filter, Draw_Looper, Image_Filter, and |
| 1850 | Mask_Filter affect Layer draw. |
| 1851 | ## |
| 1852 | |
| 1853 | #Member const SkImageFilter* fBackdrop |
| 1854 | fBackdrop applies Image_Filter to the prior Layer when copying to the Layer; |
| 1855 | may be nullptr. Use kInitWithPrevious_SaveLayerFlag to copy the |
| 1856 | prior Layer without an Image_Filter. |
| 1857 | ## |
| 1858 | |
| 1859 | #Member const SkImage* fClipMask |
| 1860 | restore() clips Layer by the Color_Alpha channel of fClipMask when |
| 1861 | Layer is copied to Device. fClipMask may be nullptr. . |
| 1862 | ## |
| 1863 | |
| 1864 | #Member const SkMatrix* fClipMatrix |
| 1865 | fClipMatrix transforms fClipMask before it clips Layer. If |
| 1866 | fClipMask describes a translucent gradient, it may be scaled and rotated |
| 1867 | without introducing artifacts. fClipMatrix may be nullptr. |
| 1868 | ## |
| 1869 | |
| 1870 | #Member SaveLayerFlags fSaveLayerFlags |
| 1871 | fSaveLayerFlags are used to create Layer without transparency, |
| 1872 | create Layer for LCD text, and to create Layer with the |
| 1873 | contents of the previous Layer. |
| 1874 | ## |
| 1875 | |
| 1876 | #Example |
| 1877 | #Height 160 |
| 1878 | #Description |
| 1879 | Canvas Layer captures a red Anti-aliased circle and a blue Aliased circle scaled |
| 1880 | up by four. After drawing another red circle without scaling on top, the Layer is |
| 1881 | transferred to the main canvas. |
| 1882 | ## |
| 1883 | void draw(SkCanvas* canvas) { |
| 1884 | SkPaint redPaint, bluePaint; |
| 1885 | redPaint.setAntiAlias(true); |
| 1886 | redPaint.setColor(SK_ColorRED); |
| 1887 | canvas->drawCircle(21, 21, 8, redPaint); |
| 1888 | bluePaint.setColor(SK_ColorBLUE); |
| 1889 | canvas->drawCircle(31, 21, 8, bluePaint); |
| 1890 | SkMatrix matrix; |
| 1891 | matrix.setScale(4, 4); |
| 1892 | auto scaler = SkImageFilter::MakeMatrixFilter(matrix, kNone_SkFilterQuality, nullptr); |
| 1893 | SkCanvas::SaveLayerRec saveLayerRec(nullptr, nullptr, scaler.get(), 0); |
| 1894 | canvas->saveLayer(saveLayerRec); |
| 1895 | canvas->drawCircle(125, 85, 8, redPaint); |
| 1896 | canvas->restore(); |
| 1897 | } |
| 1898 | ## |
| 1899 | |
| 1900 | #Method SaveLayerRec() |
| 1901 | |
| 1902 | Sets fBounds, fPaint, and fBackdrop to nullptr. Clears fSaveLayerFlags. |
| 1903 | |
| 1904 | #Return empty SaveLayerRec ## |
| 1905 | |
| 1906 | #Example |
| 1907 | SkCanvas::SaveLayerRec rec1; |
| 1908 | rec1.fSaveLayerFlags = SkCanvas::kIsOpaque_SaveLayerFlag; |
| 1909 | SkCanvas::SaveLayerRec rec2(nullptr, nullptr, SkCanvas::kIsOpaque_SaveLayerFlag); |
| 1910 | SkDebugf("rec1 %c= rec2\n", rec1.fBounds == rec2.fBounds |
| 1911 | && rec1.fPaint == rec2.fPaint |
| 1912 | && rec1.fBackdrop == rec2.fBackdrop |
| 1913 | && rec1.fSaveLayerFlags == rec2.fSaveLayerFlags ? '=' : '!'); |
| 1914 | #StdOut |
| 1915 | rec1 == rec2 |
| 1916 | ## |
| 1917 | ## |
| 1918 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1919 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 1920 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1921 | ## |
| 1922 | |
| 1923 | #Method SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0) |
| 1924 | |
| 1925 | Sets fBounds, fPaint, and fSaveLayerFlags; sets fBackdrop to nullptr. |
| 1926 | |
| 1927 | #Param bounds Layer dimensions; may be nullptr ## |
| 1928 | #Param paint applied to Layer when overlaying prior Layer; may be nullptr ## |
| 1929 | #Param saveLayerFlags SaveLayerRec options to modify Layer ## |
| 1930 | |
| 1931 | #Return SaveLayerRec with empty backdrop ## |
| 1932 | |
| 1933 | #Example |
| 1934 | SkCanvas::SaveLayerRec rec1; |
| 1935 | SkCanvas::SaveLayerRec rec2(nullptr, nullptr); |
| 1936 | SkDebugf("rec1 %c= rec2\n", rec1.fBounds == rec2.fBounds |
| 1937 | && rec1.fPaint == rec2.fPaint |
| 1938 | && rec1.fBackdrop == rec2.fBackdrop |
| 1939 | && rec1.fSaveLayerFlags == rec2.fSaveLayerFlags ? '=' : '!'); |
| 1940 | #StdOut |
| 1941 | rec1 == rec2 |
| 1942 | ## |
| 1943 | ## |
| 1944 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1945 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 1946 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1947 | ## |
| 1948 | |
| 1949 | #Method SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop, |
| 1950 | SaveLayerFlags saveLayerFlags) |
| 1951 | |
| 1952 | Sets fBounds, fPaint, fBackdrop, and fSaveLayerFlags. |
| 1953 | |
| 1954 | #Param bounds Layer dimensions; may be nullptr ## |
| 1955 | #Param paint applied to Layer when overlaying prior Layer; |
| 1956 | may be nullptr |
| 1957 | ## |
| 1958 | #Param backdrop prior Layer copied with Image_Filter; may be nullptr |
| 1959 | ## |
| 1960 | #Param saveLayerFlags SaveLayerRec options to modify Layer ## |
| 1961 | |
| 1962 | #Return SaveLayerRec fully specified ## |
| 1963 | |
| 1964 | #Example |
| 1965 | SkCanvas::SaveLayerRec rec1; |
| 1966 | SkCanvas::SaveLayerRec rec2(nullptr, nullptr, nullptr, 0); |
| 1967 | SkDebugf("rec1 %c= rec2\n", rec1.fBounds == rec2.fBounds |
| 1968 | && rec1.fPaint == rec2.fPaint |
| 1969 | && rec1.fBackdrop == rec2.fBackdrop |
| 1970 | && rec1.fSaveLayerFlags == rec2.fSaveLayerFlags ? '=' : '!'); |
| 1971 | #StdOut |
| 1972 | rec1 == rec2 |
| 1973 | ## |
| 1974 | ## |
| 1975 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1976 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 1977 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1978 | ## |
| 1979 | |
| 1980 | #Method SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop, |
| 1981 | const SkImage* clipMask, const SkMatrix* clipMatrix, |
| 1982 | SaveLayerFlags saveLayerFlags) |
| 1983 | |
| 1984 | #Experimental |
| 1985 | Not ready for general use. |
| 1986 | ## |
| 1987 | |
| 1988 | Sets fBounds, fPaint, fBackdrop, fClipMask, fClipMatrix, and fSaveLayerFlags. |
| 1989 | clipMatrix uses Color_Alpha channel of image, transformed by clipMatrix, to clip |
| 1990 | Layer when drawn to Canvas. |
| 1991 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1992 | Implementation is not complete; has no effect if Device is GPU-backed. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1993 | |
| 1994 | #Param bounds Layer dimensions; may be nullptr ## |
| 1995 | #Param paint graphics state applied to Layer when overlaying prior |
| 1996 | Layer; may be nullptr |
| 1997 | ## |
| 1998 | #Param backdrop prior Layer copied with Image_Filter; |
| 1999 | may be nullptr |
| 2000 | ## |
| 2001 | #Param clipMask clip applied to Layer; may be nullptr ## |
| 2002 | #Param clipMatrix matrix applied to clipMask; may be nullptr to use |
| 2003 | identity matrix |
| 2004 | ## |
| 2005 | #Param saveLayerFlags SaveLayerRec options to modify Layer ## |
| 2006 | |
| 2007 | #Return SaveLayerRec fully specified ## |
| 2008 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2009 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2010 | |
| 2011 | ## |
| 2012 | |
| 2013 | #Struct ## |
| 2014 | |
| 2015 | #Method int saveLayer(const SaveLayerRec& layerRec) |
| 2016 | |
| 2017 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 2018 | and allocates Bitmap for subsequent drawing. |
| 2019 | |
| 2020 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 2021 | and blends Bitmap with Color_Alpha opacity onto the prior Layer. |
| 2022 | |
| 2023 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 2024 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 2025 | clipPath, clipRegion. |
| 2026 | |
| 2027 | SaveLayerRec contains the state used to create the Layer. |
| 2028 | |
| 2029 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 2030 | |
| 2031 | #Param layerRec Layer state ## |
| 2032 | |
| 2033 | #Return depth of save state stack ## |
| 2034 | |
| 2035 | #Example |
| 2036 | #Description |
| 2037 | The example draws an image, and saves it into a Layer with kInitWithPrevious_SaveLayerFlag. |
| 2038 | Next it punches a hole in Layer and restore with SkBlendMode::kPlus. |
| 2039 | Where Layer was cleared, the original image will draw unchanged. |
| 2040 | Outside of the circle the mandrill is brightened. |
| 2041 | ## |
| 2042 | #Image 3 |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 2043 | // sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_256.png"); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2044 | canvas->drawImage(image, 0, 0, nullptr); |
| 2045 | SkCanvas::SaveLayerRec rec; |
| 2046 | SkPaint paint; |
| 2047 | paint.setBlendMode(SkBlendMode::kPlus); |
| 2048 | rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 2049 | rec.fPaint = &paint; |
| 2050 | canvas->saveLayer(rec); |
| 2051 | paint.setBlendMode(SkBlendMode::kClear); |
| 2052 | canvas->drawCircle(128, 128, 96, paint); |
| 2053 | canvas->restore(); |
| 2054 | ## |
| 2055 | |
| 2056 | #ToDo above example needs to replace GetResourceAsImage with way to select image in fiddle ## |
| 2057 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2058 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 2059 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2060 | ## |
| 2061 | |
| 2062 | #Topic Layer ## |
| 2063 | |
| 2064 | # ------------------------------------------------------------------------------ |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2065 | #Topic Matrix |
| 2066 | |
| 2067 | #Method void translate(SkScalar dx, SkScalar dy) |
| 2068 | |
| 2069 | Translate Matrix by dx along the x-axis and dy along the y-axis. |
| 2070 | |
| 2071 | Mathematically, replace Matrix with a translation matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2072 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2073 | |
| 2074 | This has the effect of moving the drawing by (dx, dy) before transforming |
| 2075 | the result with Matrix. |
| 2076 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2077 | #Param dx distance to translate in x ## |
| 2078 | #Param dy distance to translate in y ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2079 | |
| 2080 | #Example |
| 2081 | #Height 128 |
| 2082 | #Description |
| 2083 | scale() followed by translate() produces different results from translate() followed |
| 2084 | by scale(). |
| 2085 | |
| 2086 | The blue stroke follows translate of (50, 50); a black |
| 2087 | fill follows scale of (2, 1/2.f). After restoring the clip, which resets |
| 2088 | Matrix, a red frame follows the same scale of (2, 1/2.f); a gray fill |
| 2089 | follows translate of (50, 50). |
| 2090 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2091 | void draw(SkCanvas* canvas) { |
| 2092 | SkPaint filledPaint; |
| 2093 | SkPaint outlinePaint; |
| 2094 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 2095 | outlinePaint.setColor(SK_ColorBLUE); |
| 2096 | canvas->save(); |
| 2097 | canvas->translate(50, 50); |
| 2098 | canvas->drawCircle(28, 28, 15, outlinePaint); // blue center: (50+28, 50+28) |
| 2099 | canvas->scale(2, 1/2.f); |
| 2100 | canvas->drawCircle(28, 28, 15, filledPaint); // black center: (50+(28*2), 50+(28/2)) |
| 2101 | canvas->restore(); |
| 2102 | filledPaint.setColor(SK_ColorGRAY); |
| 2103 | outlinePaint.setColor(SK_ColorRED); |
| 2104 | canvas->scale(2, 1/2.f); |
| 2105 | canvas->drawCircle(28, 28, 15, outlinePaint); // red center: (28*2, 28/2) |
| 2106 | canvas->translate(50, 50); |
| 2107 | canvas->drawCircle(28, 28, 15, filledPaint); // gray center: ((50+28)*2, (50+28)/2) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2108 | } |
| 2109 | ## |
| 2110 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2111 | #SeeAlso concat() scale() skew() rotate() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2112 | |
| 2113 | ## |
| 2114 | |
| 2115 | # ------------------------------------------------------------------------------ |
| 2116 | |
| 2117 | #Method void scale(SkScalar sx, SkScalar sy) |
| 2118 | |
| 2119 | Scale Matrix by sx on the x-axis and sy on the y-axis. |
| 2120 | |
| 2121 | Mathematically, replace Matrix with a scale matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2122 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2123 | |
| 2124 | This has the effect of scaling the drawing by (sx, sy) before transforming |
| 2125 | the result with Matrix. |
| 2126 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2127 | #Param sx amount to scale in x ## |
| 2128 | #Param sy amount to scale in y ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2129 | |
| 2130 | #Example |
| 2131 | #Height 160 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2132 | void draw(SkCanvas* canvas) { |
| 2133 | SkPaint paint; |
| 2134 | SkRect rect = { 10, 20, 60, 120 }; |
| 2135 | canvas->translate(20, 20); |
| 2136 | canvas->drawRect(rect, paint); |
| 2137 | canvas->scale(2, .5f); |
| 2138 | paint.setColor(SK_ColorGRAY); |
| 2139 | canvas->drawRect(rect, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2140 | } |
| 2141 | ## |
| 2142 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2143 | #SeeAlso concat() translate() skew() rotate() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2144 | |
| 2145 | ## |
| 2146 | |
| 2147 | # ------------------------------------------------------------------------------ |
| 2148 | |
| 2149 | #Method void rotate(SkScalar degrees) |
| 2150 | |
| 2151 | Rotate Matrix by degrees. Positive degrees rotates clockwise. |
| 2152 | |
| 2153 | Mathematically, replace Matrix with a rotation matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2154 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2155 | |
| 2156 | This has the effect of rotating the drawing by degrees before transforming |
| 2157 | the result with Matrix. |
| 2158 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2159 | #Param degrees amount to rotate, in degrees ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2160 | |
| 2161 | #Example |
| 2162 | #Description |
| 2163 | Draw clock hands at time 5:10. The hour hand and minute hand point up and |
| 2164 | are rotated clockwise. |
| 2165 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2166 | void draw(SkCanvas* canvas) { |
| 2167 | SkPaint paint; |
| 2168 | paint.setStyle(SkPaint::kStroke_Style); |
| 2169 | canvas->translate(128, 128); |
| 2170 | canvas->drawCircle(0, 0, 60, paint); |
| 2171 | canvas->save(); |
| 2172 | canvas->rotate(10 * 360 / 60); // 10 minutes of 60 scaled to 360 degrees |
| 2173 | canvas->drawLine(0, 0, 0, -50, paint); |
| 2174 | canvas->restore(); |
| 2175 | canvas->rotate((5 + 10.f/60) * 360 / 12); // 5 and 10/60 hours of 12 scaled to 360 degrees |
| 2176 | canvas->drawLine(0, 0, 0, -30, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2177 | } |
| 2178 | ## |
| 2179 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2180 | #SeeAlso concat() translate() skew() scale() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2181 | |
| 2182 | ## |
| 2183 | |
| 2184 | # ------------------------------------------------------------------------------ |
| 2185 | |
| 2186 | #Method void rotate(SkScalar degrees, SkScalar px, SkScalar py) |
| 2187 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2188 | Rotate Matrix by degrees about a point at (px, py). Positive degrees rotates |
| 2189 | clockwise. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2190 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2191 | Mathematically, construct a rotation matrix. Premultiply the rotation matrix by |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2192 | a translation matrix, then replace Matrix with the resulting matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2193 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2194 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2195 | This has the effect of rotating the drawing about a given point before |
| 2196 | transforming the result with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2197 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2198 | #Param degrees amount to rotate, in degrees ## |
| 2199 | #Param px x-coordinate of the point to rotate about ## |
| 2200 | #Param py y-coordinate of the point to rotate about ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2201 | |
| 2202 | #Example |
| 2203 | #Height 192 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2204 | void draw(SkCanvas* canvas) { |
| 2205 | SkPaint paint; |
| 2206 | paint.setTextSize(96); |
| 2207 | canvas->drawString("A1", 130, 100, paint); |
| 2208 | canvas->rotate(180, 130, 100); |
| 2209 | canvas->drawString("A1", 130, 100, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2210 | } |
| 2211 | ## |
| 2212 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2213 | #SeeAlso concat() translate() skew() scale() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2214 | |
| 2215 | ## |
| 2216 | |
| 2217 | # ------------------------------------------------------------------------------ |
| 2218 | |
| 2219 | #Method void skew(SkScalar sx, SkScalar sy) |
| 2220 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2221 | Skew Matrix by sx on the x-axis and sy on the y-axis. A positive value of sx |
| 2222 | skews the drawing right as y increases; a positive value of sy skews the drawing |
| 2223 | down as x increases. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2224 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2225 | Mathematically, replace Matrix with a skew matrix Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2226 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2227 | This has the effect of skewing the drawing by (sx, sy) before transforming |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2228 | the result with Matrix. |
| 2229 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2230 | #Param sx amount to skew in x ## |
| 2231 | #Param sy amount to skew in y ## |
| 2232 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2233 | #Example |
| 2234 | #Description |
| 2235 | Black text mimics an oblique text style by using a negative skew in x that |
| 2236 | shifts the geometry to the right as the y values decrease. |
| 2237 | Red text uses a positive skew in y to shift the geometry down as the x values |
| 2238 | increase. |
| 2239 | Blue text combines x and y skew to rotate and scale. |
| 2240 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2241 | SkPaint paint; |
| 2242 | paint.setTextSize(128); |
| 2243 | canvas->translate(30, 130); |
| 2244 | canvas->save(); |
| 2245 | canvas->skew(-.5, 0); |
| 2246 | canvas->drawString("A1", 0, 0, paint); |
| 2247 | canvas->restore(); |
| 2248 | canvas->save(); |
| 2249 | canvas->skew(0, .5); |
| 2250 | paint.setColor(SK_ColorRED); |
| 2251 | canvas->drawString("A1", 0, 0, paint); |
| 2252 | canvas->restore(); |
| 2253 | canvas->skew(-.5, .5); |
| 2254 | paint.setColor(SK_ColorBLUE); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2255 | canvas->drawString("A1", 0, 0, paint); |
| 2256 | ## |
| 2257 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2258 | #SeeAlso concat() translate() rotate() scale() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2259 | |
| 2260 | ## |
| 2261 | |
| 2262 | # ------------------------------------------------------------------------------ |
| 2263 | |
| 2264 | #Method void concat(const SkMatrix& matrix) |
| 2265 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2266 | Replace Matrix with matrix Premultiplied with existing Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2267 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2268 | This has the effect of transforming the drawn geometry by matrix, before |
| 2269 | transforming the result with existing Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2270 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2271 | #Param matrix matrix to Premultiply with existing Matrix ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2272 | |
| 2273 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2274 | void draw(SkCanvas* canvas) { |
| 2275 | SkPaint paint; |
| 2276 | paint.setTextSize(80); |
| 2277 | paint.setTextScaleX(.3); |
| 2278 | SkMatrix matrix; |
| 2279 | SkRect rect[2] = {{ 10, 20, 90, 110 }, { 40, 130, 140, 180 }}; |
| 2280 | matrix.setRectToRect(rect[0], rect[1], SkMatrix::kFill_ScaleToFit); |
| 2281 | canvas->drawRect(rect[0], paint); |
| 2282 | canvas->drawRect(rect[1], paint); |
| 2283 | paint.setColor(SK_ColorWHITE); |
| 2284 | canvas->drawString("Here", rect[0].fLeft + 10, rect[0].fBottom - 10, paint); |
| 2285 | canvas->concat(matrix); |
| 2286 | canvas->drawString("There", rect[0].fLeft + 10, rect[0].fBottom - 10, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2287 | } |
| 2288 | ## |
| 2289 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2290 | #SeeAlso translate() rotate() scale() skew() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2291 | |
| 2292 | ## |
| 2293 | |
| 2294 | # ------------------------------------------------------------------------------ |
| 2295 | |
| 2296 | #Method void setMatrix(const SkMatrix& matrix) |
| 2297 | |
| 2298 | Replace Matrix with matrix. |
| 2299 | Unlike concat(), any prior matrix state is overwritten. |
| 2300 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2301 | #Param matrix matrix to copy, replacing existing Matrix ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2302 | |
| 2303 | #Example |
| 2304 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2305 | void draw(SkCanvas* canvas) { |
| 2306 | SkPaint paint; |
| 2307 | canvas->scale(4, 6); |
| 2308 | canvas->drawString("truth", 2, 10, paint); |
| 2309 | SkMatrix matrix; |
| 2310 | matrix.setScale(2.8f, 6); |
| 2311 | canvas->setMatrix(matrix); |
| 2312 | canvas->drawString("consequences", 2, 20, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2313 | } |
| 2314 | ## |
| 2315 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2316 | #SeeAlso resetMatrix concat() translate() rotate() scale() skew() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2317 | |
| 2318 | ## |
| 2319 | |
| 2320 | # ------------------------------------------------------------------------------ |
| 2321 | |
| 2322 | #Method void resetMatrix() |
| 2323 | |
| 2324 | Sets Matrix to the identity matrix. |
| 2325 | Any prior matrix state is overwritten. |
| 2326 | |
| 2327 | #Example |
| 2328 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2329 | void draw(SkCanvas* canvas) { |
| 2330 | SkPaint paint; |
| 2331 | canvas->scale(4, 6); |
| 2332 | canvas->drawString("truth", 2, 10, paint); |
| 2333 | canvas->resetMatrix(); |
| 2334 | canvas->scale(2.8f, 6); |
| 2335 | canvas->drawString("consequences", 2, 20, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2336 | } |
| 2337 | ## |
| 2338 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2339 | #SeeAlso setMatrix concat() translate() rotate() scale() skew() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2340 | |
| 2341 | ## |
| 2342 | |
| 2343 | # ------------------------------------------------------------------------------ |
| 2344 | |
| 2345 | #Method const SkMatrix& getTotalMatrix() const |
| 2346 | |
| 2347 | Returns Matrix. |
| 2348 | This does not account for translation by Device or Surface. |
| 2349 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2350 | #Return Matrix in Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2351 | |
| 2352 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2353 | SkDebugf("isIdentity %s\n", canvas->getTotalMatrix().isIdentity() ? "true" : "false"); |
| 2354 | #StdOut |
| 2355 | isIdentity true |
| 2356 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2357 | ## |
| 2358 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2359 | #SeeAlso setMatrix resetMatrix concat() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2360 | |
| 2361 | ## |
| 2362 | |
| 2363 | #Topic Matrix ## |
| 2364 | |
| 2365 | # ------------------------------------------------------------------------------ |
| 2366 | #Topic Clip |
| 2367 | |
| 2368 | Clip is built from a stack of clipping paths. Each Path in the |
| 2369 | stack can be constructed from one or more Path_Contour elements. The |
| 2370 | Path_Contour may be composed of any number of Path_Verb segments. Each |
| 2371 | Path_Contour forms a closed area; Path_Fill_Type defines the area enclosed |
| 2372 | by Path_Contour. |
| 2373 | |
| 2374 | Clip stack of Path elements successfully restrict the Path area. Each |
| 2375 | Path is transformed by Matrix, then intersected with or subtracted from the |
| 2376 | prior Clip to form the replacement Clip. Use SkClipOp::kDifference |
| 2377 | to subtract Path from Clip; use SkClipOp::kIntersect to intersect Path |
| 2378 | with Clip. |
| 2379 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2380 | A clipping Path may be Anti-aliased; if Path, after transformation, is |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2381 | composed of horizontal and vertical lines, clearing Anti-alias allows whole pixels |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2382 | to either be inside or outside the clip. The fastest drawing has a Aliased, |
| 2383 | rectangular clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2384 | |
| 2385 | If clipping Path has Anti-alias set, clip may partially clip a pixel, requiring |
| 2386 | that drawing blend partially with the destination along the edge. A rotated |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2387 | rectangular Anti-aliased clip looks smoother but draws slower. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2388 | |
| 2389 | Clip can combine with Rect and Round_Rect primitives; like |
| 2390 | Path, these are transformed by Matrix before they are combined with Clip. |
| 2391 | |
| 2392 | Clip can combine with Region. Region is assumed to be in Device coordinates |
| 2393 | and is unaffected by Matrix. |
| 2394 | |
| 2395 | #Example |
| 2396 | #Height 90 |
| 2397 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2398 | Draw a red circle with an Aliased clip and an Anti-aliased clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2399 | Use an image filter to zoom into the pixels drawn. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2400 | The edge of the Aliased clip fully draws pixels in the red circle. |
| 2401 | The edge of the Anti-aliased clip partially draws pixels in the red circle. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2402 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2403 | SkPaint redPaint, scalePaint; |
| 2404 | redPaint.setAntiAlias(true); |
| 2405 | redPaint.setColor(SK_ColorRED); |
| 2406 | canvas->save(); |
| 2407 | for (bool antialias : { false, true } ) { |
| 2408 | canvas->save(); |
| 2409 | canvas->clipRect(SkRect::MakeWH(19.5f, 11.5f), antialias); |
| 2410 | canvas->drawCircle(17, 11, 8, redPaint); |
| 2411 | canvas->restore(); |
| 2412 | canvas->translate(16, 0); |
| 2413 | } |
| 2414 | canvas->restore(); |
| 2415 | SkMatrix matrix; |
| 2416 | matrix.setScale(6, 6); |
| 2417 | scalePaint.setImageFilter( |
| 2418 | SkImageFilter::MakeMatrixFilter(matrix, kNone_SkFilterQuality, nullptr)); |
| 2419 | SkCanvas::SaveLayerRec saveLayerRec( |
| 2420 | nullptr, &scalePaint, SkCanvas::kInitWithPrevious_SaveLayerFlag); |
| 2421 | canvas->saveLayer(saveLayerRec); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2422 | canvas->restore(); |
| 2423 | ## |
| 2424 | |
| 2425 | #Method void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias) |
| 2426 | |
| 2427 | Replace Clip with the intersection or difference of Clip and rect, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2428 | with an Aliased or Anti-aliased clip edge. rect is transformed by Matrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2429 | before it is combined with Clip. |
| 2430 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 2431 | #Param rect Rect to combine with Clip ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2432 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2433 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2434 | |
| 2435 | #Example |
| 2436 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2437 | void draw(SkCanvas* canvas) { |
| 2438 | canvas->rotate(10); |
| 2439 | SkPaint paint; |
| 2440 | paint.setAntiAlias(true); |
| 2441 | for (auto alias: { false, true } ) { |
| 2442 | canvas->save(); |
| 2443 | canvas->clipRect(SkRect::MakeWH(90, 80), SkClipOp::kIntersect, alias); |
| 2444 | canvas->drawCircle(100, 60, 60, paint); |
| 2445 | canvas->restore(); |
| 2446 | canvas->translate(80, 0); |
| 2447 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2448 | } |
| 2449 | ## |
| 2450 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2451 | #SeeAlso clipRRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2452 | |
| 2453 | ## |
| 2454 | |
| 2455 | #Method void clipRect(const SkRect& rect, SkClipOp op) |
| 2456 | |
| 2457 | Replace Clip with the intersection or difference of Clip and rect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2458 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2459 | rect is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2460 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 2461 | #Param rect Rect to combine with Clip ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2462 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2463 | |
| 2464 | #Example |
| 2465 | #Height 192 |
| 2466 | #Width 280 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2467 | void draw(SkCanvas* canvas) { |
| 2468 | SkPaint paint; |
| 2469 | for (SkClipOp op: { SkClipOp::kIntersect, SkClipOp::kDifference } ) { |
| 2470 | canvas->save(); |
| 2471 | canvas->clipRect(SkRect::MakeWH(90, 120), op, false); |
| 2472 | canvas->drawCircle(100, 100, 60, paint); |
| 2473 | canvas->restore(); |
| 2474 | canvas->translate(80, 0); |
| 2475 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2476 | } |
| 2477 | ## |
| 2478 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2479 | #SeeAlso clipRRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2480 | |
| 2481 | ## |
| 2482 | |
| 2483 | #Method void clipRect(const SkRect& rect, bool doAntiAlias = false) |
| 2484 | |
| 2485 | Replace Clip with the intersection of Clip and rect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2486 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2487 | rect is transformed by Matrix |
| 2488 | before it is combined with Clip. |
| 2489 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 2490 | #Param rect Rect to combine with Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2491 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2492 | |
| 2493 | #Example |
| 2494 | #Height 133 |
| 2495 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2496 | A circle drawn in pieces looks uniform when drawn Aliased. |
| 2497 | The same circle pieces blend with pixels more than once when Anti-aliased, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2498 | visible as a thin pair of lines through the right circle. |
| 2499 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2500 | void draw(SkCanvas* canvas) { |
| 2501 | canvas->clear(SK_ColorWHITE); |
| 2502 | SkPaint paint; |
| 2503 | paint.setAntiAlias(true); |
| 2504 | paint.setColor(0x8055aaff); |
| 2505 | SkRect clipRect = { 0, 0, 87.4f, 87.4f }; |
| 2506 | for (auto alias: { false, true } ) { |
| 2507 | canvas->save(); |
| 2508 | canvas->clipRect(clipRect, SkClipOp::kIntersect, alias); |
| 2509 | canvas->drawCircle(67, 67, 60, paint); |
| 2510 | canvas->restore(); |
| 2511 | canvas->save(); |
| 2512 | canvas->clipRect(clipRect, SkClipOp::kDifference, alias); |
| 2513 | canvas->drawCircle(67, 67, 60, paint); |
| 2514 | canvas->restore(); |
| 2515 | canvas->translate(120, 0); |
| 2516 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2517 | } |
| 2518 | ## |
| 2519 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2520 | #SeeAlso clipRRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2521 | |
| 2522 | ## |
| 2523 | |
| 2524 | #Method void androidFramework_setDeviceClipRestriction(const SkIRect& rect) |
| 2525 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2526 | Sets the maximum clip rectangle, which can be set by clipRect, clipRRect and |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2527 | clipPath and intersect the current clip with the specified rect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2528 | The maximum clip affects only future clipping operations; it is not retroactive. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2529 | The clip restriction is not recorded in pictures. |
| 2530 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2531 | Pass an empty rect to disable maximum clip. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2532 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2533 | #Private |
| 2534 | This is private API to be used only by Android framework. |
| 2535 | ## |
| 2536 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2537 | #Param rect maximum allowed clip in device coordinates |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2538 | #Param ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2539 | |
| 2540 | ## |
| 2541 | |
| 2542 | #Method void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias) |
| 2543 | |
| 2544 | Replace Clip with the intersection or difference of Clip and rrect, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2545 | with an Aliased or Anti-aliased clip edge. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2546 | rrect is transformed by Matrix |
| 2547 | before it is combined with Clip. |
| 2548 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2549 | #Param rrect Round_Rect to combine with Clip ## |
| 2550 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2551 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2552 | |
| 2553 | #Example |
| 2554 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2555 | void draw(SkCanvas* canvas) { |
| 2556 | canvas->clear(SK_ColorWHITE); |
| 2557 | SkPaint paint; |
| 2558 | paint.setAntiAlias(true); |
| 2559 | paint.setColor(0x8055aaff); |
| 2560 | SkRRect oval; |
| 2561 | oval.setOval({10, 20, 90, 100}); |
| 2562 | canvas->clipRRect(oval, SkClipOp::kIntersect, true); |
| 2563 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2564 | } |
| 2565 | ## |
| 2566 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2567 | #SeeAlso clipRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2568 | |
| 2569 | ## |
| 2570 | |
| 2571 | #Method void clipRRect(const SkRRect& rrect, SkClipOp op) |
| 2572 | |
| 2573 | Replace Clip with the intersection or difference of Clip and rrect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2574 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2575 | rrect is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2576 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2577 | #Param rrect Round_Rect to combine with Clip ## |
| 2578 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2579 | |
| 2580 | #Example |
| 2581 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2582 | void draw(SkCanvas* canvas) { |
| 2583 | SkPaint paint; |
| 2584 | paint.setColor(0x8055aaff); |
| 2585 | auto oval = SkRRect::MakeOval({10, 20, 90, 100}); |
| 2586 | canvas->clipRRect(oval, SkClipOp::kIntersect); |
| 2587 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2588 | } |
| 2589 | ## |
| 2590 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2591 | #SeeAlso clipRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2592 | |
| 2593 | ## |
| 2594 | |
| 2595 | #Method void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) |
| 2596 | |
| 2597 | Replace Clip with the intersection of Clip and rrect, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2598 | with an Aliased or Anti-aliased clip edge. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2599 | rrect is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2600 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2601 | #Param rrect Round_Rect to combine with Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2602 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2603 | |
| 2604 | #Example |
| 2605 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2606 | void draw(SkCanvas* canvas) { |
| 2607 | SkPaint paint; |
| 2608 | paint.setAntiAlias(true); |
| 2609 | auto oval = SkRRect::MakeRectXY({10, 20, 90, 100}, 9, 13); |
| 2610 | canvas->clipRRect(oval, true); |
| 2611 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2612 | } |
| 2613 | ## |
| 2614 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2615 | #SeeAlso clipRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2616 | |
| 2617 | ## |
| 2618 | |
| 2619 | #Method void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias) |
| 2620 | |
| 2621 | Replace Clip with the intersection or difference of Clip and path, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2622 | with an Aliased or Anti-aliased clip edge. Path_Fill_Type determines if path |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2623 | describes the area inside or outside its contours; and if Path_Contour overlaps |
| 2624 | itself or another Path_Contour, whether the overlaps form part of the area. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2625 | path is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2626 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2627 | #Param path Path to combine with Clip ## |
| 2628 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2629 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2630 | |
| 2631 | #Example |
| 2632 | #Description |
| 2633 | Top figure uses SkPath::kInverseWinding_FillType and SkClipOp::kDifference; |
| 2634 | area outside clip is subtracted from circle. |
| 2635 | |
| 2636 | Bottom figure uses SkPath::kWinding_FillType and SkClipOp::kIntersect; |
| 2637 | area inside clip is intersected with circle. |
| 2638 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2639 | void draw(SkCanvas* canvas) { |
| 2640 | SkPaint paint; |
| 2641 | paint.setAntiAlias(true); |
| 2642 | SkPath path; |
| 2643 | path.addRect({20, 30, 100, 110}); |
| 2644 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 2645 | canvas->save(); |
| 2646 | canvas->clipPath(path, SkClipOp::kDifference, false); |
| 2647 | canvas->drawCircle(70, 100, 60, paint); |
| 2648 | canvas->restore(); |
| 2649 | canvas->translate(100, 100); |
| 2650 | path.setFillType(SkPath::kWinding_FillType); |
| 2651 | canvas->clipPath(path, SkClipOp::kIntersect, false); |
| 2652 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2653 | } |
| 2654 | ## |
| 2655 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2656 | #SeeAlso clipRect clipRRect clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2657 | |
| 2658 | ## |
| 2659 | |
| 2660 | #Method void clipPath(const SkPath& path, SkClipOp op) |
| 2661 | |
| 2662 | Replace Clip with the intersection or difference of Clip and path. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2663 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2664 | Path_Fill_Type determines if path |
| 2665 | describes the area inside or outside its contours; and if Path_Contour overlaps |
| 2666 | itself or another Path_Contour, whether the overlaps form part of the area. |
| 2667 | path is transformed by Matrix |
| 2668 | before it is combined with Clip. |
| 2669 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2670 | #Param path Path to combine with Clip ## |
| 2671 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2672 | |
| 2673 | #Example |
| 2674 | #Description |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2675 | Overlapping Rects form a clip. When clip Path_Fill_Type is set to |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2676 | SkPath::kWinding_FillType, the overlap is included. Set to |
| 2677 | SkPath::kEvenOdd_FillType, the overlap is excluded and forms a hole. |
| 2678 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2679 | void draw(SkCanvas* canvas) { |
| 2680 | SkPaint paint; |
| 2681 | paint.setAntiAlias(true); |
| 2682 | SkPath path; |
| 2683 | path.addRect({20, 15, 100, 95}); |
| 2684 | path.addRect({50, 65, 130, 135}); |
| 2685 | path.setFillType(SkPath::kWinding_FillType); |
| 2686 | canvas->save(); |
| 2687 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2688 | canvas->drawCircle(70, 85, 60, paint); |
| 2689 | canvas->restore(); |
| 2690 | canvas->translate(100, 100); |
| 2691 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 2692 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2693 | canvas->drawCircle(70, 85, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2694 | } |
| 2695 | ## |
| 2696 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2697 | #SeeAlso clipRect clipRRect clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2698 | |
| 2699 | ## |
| 2700 | |
| 2701 | #Method void clipPath(const SkPath& path, bool doAntiAlias = false) |
| 2702 | |
| 2703 | Replace Clip with the intersection of Clip and path. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2704 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2705 | Path_Fill_Type determines if path |
| 2706 | describes the area inside or outside its contours; and if Path_Contour overlaps |
| 2707 | itself or another Path_Contour, whether the overlaps form part of the area. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2708 | path is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2709 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2710 | #Param path Path to combine with Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2711 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2712 | |
| 2713 | #Example |
| 2714 | #Height 212 |
| 2715 | #Description |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2716 | Clip loops over itself covering its center twice. When clip Path_Fill_Type |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2717 | is set to SkPath::kWinding_FillType, the overlap is included. Set to |
| 2718 | SkPath::kEvenOdd_FillType, the overlap is excluded and forms a hole. |
| 2719 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2720 | void draw(SkCanvas* canvas) { |
| 2721 | SkPaint paint; |
| 2722 | paint.setAntiAlias(true); |
| 2723 | SkPath path; |
| 2724 | SkPoint poly[] = {{20, 20}, { 80, 20}, { 80, 80}, {40, 80}, |
| 2725 | {40, 40}, {100, 40}, {100, 100}, {20, 100}}; |
| 2726 | path.addPoly(poly, SK_ARRAY_COUNT(poly), true); |
| 2727 | path.setFillType(SkPath::kWinding_FillType); |
| 2728 | canvas->save(); |
| 2729 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2730 | canvas->drawCircle(50, 50, 45, paint); |
| 2731 | canvas->restore(); |
| 2732 | canvas->translate(100, 100); |
| 2733 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 2734 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2735 | canvas->drawCircle(50, 50, 45, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2736 | } |
| 2737 | ## |
| 2738 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2739 | #SeeAlso clipRect clipRRect clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2740 | |
| 2741 | ## |
| 2742 | |
| 2743 | # ------------------------------------------------------------------------------ |
| 2744 | |
| 2745 | #Method void setAllowSimplifyClip(bool allow) |
| 2746 | |
| 2747 | #Experimental |
| 2748 | Only used for testing. |
| 2749 | ## |
| 2750 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2751 | Set to simplify clip stack using PathOps. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2752 | |
| 2753 | ## |
| 2754 | |
| 2755 | # ------------------------------------------------------------------------------ |
| 2756 | |
| 2757 | #Method void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect) |
| 2758 | |
| 2759 | Replace Clip with the intersection or difference of Clip and Region deviceRgn. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2760 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2761 | deviceRgn is unaffected by Matrix. |
| 2762 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2763 | #Param deviceRgn Region to combine with Clip ## |
| 2764 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2765 | |
| 2766 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2767 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2768 | region is unaffected by canvas rotation; iRect is affected by canvas rotation. |
| 2769 | Both clips are Aliased; this is not noticeable on Region clip because it |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2770 | aligns to pixel boundaries. |
| 2771 | ## |
| 2772 | void draw(SkCanvas* canvas) { |
| 2773 | SkPaint paint; |
| 2774 | paint.setAntiAlias(true); |
| 2775 | SkIRect iRect = {30, 40, 120, 130 }; |
| 2776 | SkRegion region(iRect); |
| 2777 | canvas->rotate(10); |
| 2778 | canvas->save(); |
| 2779 | canvas->clipRegion(region, SkClipOp::kIntersect); |
| 2780 | canvas->drawCircle(50, 50, 45, paint); |
| 2781 | canvas->restore(); |
| 2782 | canvas->translate(100, 100); |
| 2783 | canvas->clipRect(SkRect::Make(iRect), SkClipOp::kIntersect); |
| 2784 | canvas->drawCircle(50, 50, 45, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2785 | } |
| 2786 | ## |
| 2787 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2788 | #SeeAlso clipRect clipRRect clipPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2789 | |
| 2790 | ## |
| 2791 | |
| 2792 | #Method bool quickReject(const SkRect& rect) const |
| 2793 | |
| 2794 | Return true if Rect rect, transformed by Matrix, can be quickly determined to be |
| 2795 | outside of Clip. May return false even though rect is outside of Clip. |
| 2796 | |
| 2797 | Use to check if an area to be drawn is clipped out, to skip subsequent draw calls. |
| 2798 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2799 | #Param rect Rect to compare with Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2800 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2801 | #Return true if rect, transformed by Matrix, does not intersect Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2802 | |
| 2803 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2804 | void draw(SkCanvas* canvas) { |
| 2805 | SkRect testRect = {30, 30, 120, 129 }; |
| 2806 | SkRect clipRect = {30, 130, 120, 230 }; |
| 2807 | canvas->save(); |
| 2808 | canvas->clipRect(clipRect); |
| 2809 | SkDebugf("quickReject %s\n", canvas->quickReject(testRect) ? "true" : "false"); |
| 2810 | canvas->restore(); |
| 2811 | canvas->rotate(10); |
| 2812 | canvas->clipRect(clipRect); |
| 2813 | SkDebugf("quickReject %s\n", canvas->quickReject(testRect) ? "true" : "false"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2814 | } |
| 2815 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2816 | quickReject true |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2817 | quickReject false |
| 2818 | ## |
| 2819 | ## |
| 2820 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2821 | #SeeAlso getLocalClipBounds getTotalMatrix SkBitmap::drawsNothing |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2822 | |
| 2823 | ## |
| 2824 | |
| 2825 | #Method bool quickReject(const SkPath& path) const |
| 2826 | |
| 2827 | Return true if path, transformed by Matrix, can be quickly determined to be |
| 2828 | outside of Clip. May return false even though path is outside of Clip. |
| 2829 | |
| 2830 | Use to check if an area to be drawn is clipped out, to skip subsequent draw calls. |
| 2831 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2832 | #Param path Path to compare with Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2833 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2834 | #Return true if path, transformed by Matrix, does not intersect Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2835 | |
| 2836 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2837 | void draw(SkCanvas* canvas) { |
| 2838 | SkPoint testPoints[] = {{30, 30}, {120, 30}, {120, 129} }; |
| 2839 | SkPoint clipPoints[] = {{30, 130}, {120, 130}, {120, 230} }; |
| 2840 | SkPath testPath, clipPath; |
| 2841 | testPath.addPoly(testPoints, SK_ARRAY_COUNT(testPoints), true); |
| 2842 | clipPath.addPoly(clipPoints, SK_ARRAY_COUNT(clipPoints), true); |
| 2843 | canvas->save(); |
| 2844 | canvas->clipPath(clipPath); |
| 2845 | SkDebugf("quickReject %s\n", canvas->quickReject(testPath) ? "true" : "false"); |
| 2846 | canvas->restore(); |
| 2847 | canvas->rotate(10); |
| 2848 | canvas->clipPath(clipPath); |
| 2849 | SkDebugf("quickReject %s\n", canvas->quickReject(testPath) ? "true" : "false"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2850 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2851 | quickReject true |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2852 | quickReject false |
| 2853 | ## |
| 2854 | } |
| 2855 | ## |
| 2856 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2857 | #SeeAlso getLocalClipBounds getTotalMatrix SkBitmap::drawsNothing |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2858 | |
| 2859 | ## |
| 2860 | |
| 2861 | #Method SkRect getLocalClipBounds() const |
| 2862 | |
| 2863 | Return bounds of Clip, transformed by inverse of Matrix. If Clip is empty, |
| 2864 | return SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2865 | |
| 2866 | Rect returned is outset by one to account for partial pixel coverage if Clip |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2867 | is Anti-aliased. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2868 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2869 | #Return bounds of Clip in local coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2870 | |
| 2871 | #Example |
| 2872 | #Description |
| 2873 | Initial bounds is device bounds outset by 1 on all sides. |
| 2874 | Clipped bounds is clipPath bounds outset by 1 on all sides. |
| 2875 | Scaling the canvas by two in x and y scales the local bounds by 1/2 in x and y. |
| 2876 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2877 | SkCanvas local(256, 256); |
| 2878 | canvas = &local; |
| 2879 | SkRect bounds = canvas->getLocalClipBounds(); |
| 2880 | SkDebugf("left:%g top:%g right:%g bottom:%g\n", |
| 2881 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2882 | SkPoint clipPoints[] = {{30, 130}, {120, 130}, {120, 230} }; |
| 2883 | SkPath clipPath; |
| 2884 | clipPath.addPoly(clipPoints, SK_ARRAY_COUNT(clipPoints), true); |
| 2885 | canvas->clipPath(clipPath); |
| 2886 | bounds = canvas->getLocalClipBounds(); |
| 2887 | SkDebugf("left:%g top:%g right:%g bottom:%g\n", |
| 2888 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2889 | canvas->scale(2, 2); |
| 2890 | bounds = canvas->getLocalClipBounds(); |
| 2891 | SkDebugf("left:%g top:%g right:%g bottom:%g\n", |
| 2892 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2893 | #StdOut |
| 2894 | left:-1 top:-1 right:257 bottom:257 |
| 2895 | left:29 top:129 right:121 bottom:231 |
| 2896 | left:14.5 top:64.5 right:60.5 bottom:115.5 |
| 2897 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2898 | ## |
| 2899 | |
| 2900 | # local canvas in example works around bug in fiddle ## |
| 2901 | #Bug 6524 ## |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2902 | #SeeAlso getDeviceClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2903 | |
| 2904 | ## |
| 2905 | |
| 2906 | #Method bool getLocalClipBounds(SkRect* bounds) const |
| 2907 | |
| 2908 | Return bounds of Clip, transformed by inverse of Matrix. If Clip is empty, |
| 2909 | return false, and set bounds to SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2910 | |
| 2911 | bounds is outset by one to account for partial pixel coverage if Clip |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2912 | is Anti-aliased. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2913 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2914 | #Param bounds Rect of Clip in local coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2915 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2916 | #Return true if Clip bounds is not empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2917 | |
| 2918 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2919 | void draw(SkCanvas* canvas) { |
| 2920 | SkCanvas local(256, 256); |
| 2921 | canvas = &local; |
| 2922 | SkRect bounds; |
| 2923 | SkDebugf("local bounds empty = %s\n", canvas->getLocalClipBounds(&bounds) |
| 2924 | ? "false" : "true"); |
| 2925 | SkPath path; |
| 2926 | canvas->clipPath(path); |
| 2927 | SkDebugf("local bounds empty = %s\n", canvas->getLocalClipBounds(&bounds) |
| 2928 | ? "false" : "true"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2929 | } |
| 2930 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2931 | local bounds empty = false |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2932 | local bounds empty = true |
| 2933 | ## |
| 2934 | ## |
| 2935 | |
| 2936 | # local canvas in example works around bug in fiddle ## |
| 2937 | #Bug 6524 ## |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2938 | #SeeAlso getDeviceClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2939 | |
| 2940 | ## |
| 2941 | |
| 2942 | #Method SkIRect getDeviceClipBounds() const |
| 2943 | |
| 2944 | Return IRect bounds of Clip, unaffected by Matrix. If Clip is empty, |
| 2945 | return SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2946 | |
| 2947 | Unlike getLocalClipBounds, returned IRect is not outset. |
| 2948 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2949 | #Return bounds of Clip in Device coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2950 | |
| 2951 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2952 | void draw(SkCanvas* canvas) { |
| 2953 | #Description |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2954 | Initial bounds is device bounds, not outset. |
| 2955 | Clipped bounds is clipPath bounds, not outset. |
| 2956 | Scaling the canvas by 1/2 in x and y scales the device bounds by 1/2 in x and y. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2957 | ## |
| 2958 | SkCanvas device(256, 256); |
| 2959 | canvas = &device; |
| 2960 | SkIRect bounds = canvas->getDeviceClipBounds(); |
| 2961 | SkDebugf("left:%d top:%d right:%d bottom:%d\n", |
| 2962 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2963 | SkPoint clipPoints[] = {{30, 130}, {120, 130}, {120, 230} }; |
| 2964 | SkPath clipPath; |
| 2965 | clipPath.addPoly(clipPoints, SK_ARRAY_COUNT(clipPoints), true); |
| 2966 | canvas->save(); |
| 2967 | canvas->clipPath(clipPath); |
| 2968 | bounds = canvas->getDeviceClipBounds(); |
| 2969 | SkDebugf("left:%d top:%d right:%d bottom:%d\n", |
| 2970 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2971 | canvas->restore(); |
| 2972 | canvas->scale(1.f/2, 1.f/2); |
| 2973 | canvas->clipPath(clipPath); |
| 2974 | bounds = canvas->getDeviceClipBounds(); |
| 2975 | SkDebugf("left:%d top:%d right:%d bottom:%d\n", |
| 2976 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2977 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2978 | left:0 top:0 right:256 bottom:256 |
| 2979 | left:30 top:130 right:120 bottom:230 |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2980 | left:15 top:65 right:60 bottom:115 |
| 2981 | ## |
| 2982 | } |
| 2983 | ## |
| 2984 | |
| 2985 | #ToDo some confusion on why with an identity Matrix local and device are different ## |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2986 | #SeeAlso getLocalClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2987 | |
| 2988 | # device canvas in example works around bug in fiddle ## |
| 2989 | #Bug 6524 ## |
| 2990 | |
| 2991 | ## |
| 2992 | |
| 2993 | #Method bool getDeviceClipBounds(SkIRect* bounds) const |
| 2994 | |
| 2995 | Return IRect bounds of Clip, unaffected by Matrix. If Clip is empty, |
| 2996 | return false, and set bounds to SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2997 | |
| 2998 | Unlike getLocalClipBounds, bounds is not outset. |
| 2999 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3000 | #Param bounds Rect of Clip in device coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3001 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3002 | #Return true if Clip bounds is not empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3003 | |
| 3004 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3005 | void draw(SkCanvas* canvas) { |
| 3006 | SkIRect bounds; |
| 3007 | SkDebugf("device bounds empty = %s\n", canvas->getDeviceClipBounds(&bounds) |
| 3008 | ? "false" : "true"); |
| 3009 | SkPath path; |
| 3010 | canvas->clipPath(path); |
| 3011 | SkDebugf("device bounds empty = %s\n", canvas->getDeviceClipBounds(&bounds) |
| 3012 | ? "false" : "true"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3013 | } |
| 3014 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3015 | device bounds empty = false |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3016 | device bounds empty = true |
| 3017 | ## |
| 3018 | ## |
| 3019 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3020 | #SeeAlso getLocalClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3021 | |
| 3022 | ## |
| 3023 | |
| 3024 | #Topic Clip ## |
| 3025 | |
| 3026 | # ------------------------------------------------------------------------------ |
| 3027 | |
| 3028 | #Method void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver) |
| 3029 | |
| 3030 | Fill Clip with Color color. |
| 3031 | mode determines how Color_ARGB is combined with destination. |
| 3032 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3033 | #Param color Unpremultiplied Color_ARGB ## |
| 3034 | #Param mode SkBlendMode used to combine source color and destination ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3035 | |
| 3036 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3037 | canvas->drawColor(SK_ColorRED); |
| 3038 | canvas->clipRect(SkRect::MakeWH(150, 150)); |
| 3039 | canvas->drawColor(SkColorSetARGB(0x80, 0x00, 0xFF, 0x00), SkBlendMode::kPlus); |
| 3040 | canvas->clipRect(SkRect::MakeWH(75, 75)); |
| 3041 | canvas->drawColor(SkColorSetARGB(0x80, 0x00, 0x00, 0xFF), SkBlendMode::kPlus); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3042 | ## |
| 3043 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3044 | #SeeAlso clear SkBitmap::erase drawPaint |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3045 | |
| 3046 | ## |
| 3047 | |
| 3048 | # ------------------------------------------------------------------------------ |
| 3049 | |
| 3050 | #Method void clear(SkColor color) |
| 3051 | |
| 3052 | Fill Clip with Color color using SkBlendMode::kSrc. |
| 3053 | This has the effect of replacing all pixels contained by Clip with color. |
| 3054 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3055 | #Param color Unpremultiplied Color_ARGB ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3056 | |
| 3057 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3058 | void draw(SkCanvas* canvas) { |
| 3059 | canvas->save(); |
| 3060 | canvas->clipRect(SkRect::MakeWH(256, 128)); |
| 3061 | canvas->clear(SkColorSetARGB(0x80, 0xFF, 0x00, 0x00)); |
| 3062 | canvas->restore(); |
| 3063 | canvas->save(); |
| 3064 | canvas->clipRect(SkRect::MakeWH(150, 192)); |
| 3065 | canvas->clear(SkColorSetARGB(0x80, 0x00, 0xFF, 0x00)); |
| 3066 | canvas->restore(); |
| 3067 | canvas->clipRect(SkRect::MakeWH(75, 256)); |
| 3068 | canvas->clear(SkColorSetARGB(0x80, 0x00, 0x00, 0xFF)); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3069 | } |
| 3070 | ## |
| 3071 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3072 | #SeeAlso drawColor SkBitmap::erase drawPaint |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3073 | |
| 3074 | ## |
| 3075 | |
| 3076 | # ------------------------------------------------------------------------------ |
| 3077 | |
| 3078 | #Method void discard() |
| 3079 | |
| 3080 | Make Canvas contents undefined. Subsequent calls that read Canvas pixels, |
| 3081 | such as drawing with SkBlendMode, return undefined results. discard() does |
| 3082 | not change Clip or Matrix. |
| 3083 | |
| 3084 | discard() may do nothing, depending on the implementation of Surface or Device |
| 3085 | that created Canvas. |
| 3086 | |
| 3087 | discard() allows optimized performance on subsequent draws by removing |
| 3088 | cached data associated with Surface or Device. |
| 3089 | It is not necessary to call discard() once done with Canvas; |
| 3090 | any cached data is deleted when owning Surface or Device is deleted. |
| 3091 | |
| 3092 | #ToDo example? not sure how to make this meaningful w/o more implementation detail ## |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3093 | #SeeAlso flush() SkSurface::prepareForExternalIO GrContext::abandonContext |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3094 | |
| 3095 | #NoExample |
| 3096 | ## |
| 3097 | |
| 3098 | ## |
| 3099 | |
| 3100 | # ------------------------------------------------------------------------------ |
| 3101 | |
| 3102 | #Method void drawPaint(const SkPaint& paint) |
| 3103 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 3104 | Fill Clip with Paint paint. Paint components Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3105 | Color_Filter, Image_Filter, and Blend_Mode affect drawing; |
| 3106 | Path_Effect in paint is ignored. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3107 | |
| 3108 | # can Path_Effect in paint ever alter drawPaint? |
| 3109 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3110 | #Param paint graphics state used to fill Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3111 | |
| 3112 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3113 | void draw(SkCanvas* canvas) { |
| 3114 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 3115 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 3116 | SkPaint paint; |
| 3117 | paint.setShader(SkGradientShader::MakeSweep(256, 256, colors, pos, SK_ARRAY_COUNT(colors))); |
| 3118 | canvas->drawPaint(paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3119 | } |
| 3120 | ## |
| 3121 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3122 | #SeeAlso clear drawColor SkBitmap::erase |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3123 | |
| 3124 | ## |
| 3125 | |
| 3126 | # ------------------------------------------------------------------------------ |
| 3127 | |
| 3128 | #Enum PointMode |
| 3129 | |
| 3130 | #Code |
| 3131 | enum PointMode { |
| 3132 | kPoints_PointMode, |
| 3133 | kLines_PointMode, |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 3134 | kPolygon_PointMode, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3135 | }; |
| 3136 | ## |
| 3137 | |
| 3138 | Selects if an array of points are drawn as discrete points, as lines, or as |
| 3139 | an open polygon. |
| 3140 | |
| 3141 | #Const kPoints_PointMode 0 |
| 3142 | Draw each point separately. |
| 3143 | ## |
| 3144 | |
| 3145 | #Const kLines_PointMode 1 |
| 3146 | Draw each pair of points as a line segment. |
| 3147 | ## |
| 3148 | |
| 3149 | #Const kPolygon_PointMode 2 |
| 3150 | Draw the array of points as a open polygon. |
| 3151 | ## |
| 3152 | |
| 3153 | #Example |
| 3154 | #Description |
| 3155 | The upper left corner shows three squares when drawn as points. |
| 3156 | The upper right corner shows one line; when drawn as lines, two points are required per line. |
| 3157 | The lower right corner shows two lines; when draw as polygon, no miter is drawn at the corner. |
| 3158 | The lower left corner shows two lines with a miter when path contains polygon. |
| 3159 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3160 | void draw(SkCanvas* canvas) { |
| 3161 | SkPaint paint; |
| 3162 | paint.setStyle(SkPaint::kStroke_Style); |
| 3163 | paint.setStrokeWidth(10); |
| 3164 | SkPoint points[] = {{64, 32}, {96, 96}, {32, 96}}; |
| 3165 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 3, points, paint); |
| 3166 | canvas->translate(128, 0); |
| 3167 | canvas->drawPoints(SkCanvas::kLines_PointMode, 3, points, paint); |
| 3168 | canvas->translate(0, 128); |
| 3169 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, paint); |
| 3170 | SkPath path; |
| 3171 | path.addPoly(points, 3, false); |
| 3172 | canvas->translate(-128, 0); |
| 3173 | canvas->drawPath(path, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3174 | } |
| 3175 | ## |
| 3176 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3177 | #SeeAlso drawLine drawPoint drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3178 | |
| 3179 | ## |
| 3180 | |
| 3181 | # ------------------------------------------------------------------------------ |
| 3182 | |
| 3183 | #Method void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) |
| 3184 | |
| 3185 | Draw pts using Clip, Matrix and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3186 | count is the number of points; if count is less than one, has no effect. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3187 | mode may be one of: kPoints_PointMode, kLines_PointMode, or kPolygon_PointMode. |
| 3188 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3189 | If mode is kPoints_PointMode, the shape of point drawn depends on paint |
| 3190 | Paint_Stroke_Cap. If paint is set to SkPaint::kRound_Cap, each point draws a |
| 3191 | circle of diameter Paint_Stroke_Width. If paint is set to SkPaint::kSquare_Cap |
| 3192 | or SkPaint::kButt_Cap, each point draws a square of width and height |
| 3193 | Paint_Stroke_Width. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3194 | |
| 3195 | If mode is kLines_PointMode, each pair of points draws a line segment. |
| 3196 | One line is drawn for every two points; each point is used once. If count is odd, |
| 3197 | the final point is ignored. |
| 3198 | |
| 3199 | If mode is kPolygon_PointMode, each adjacent pair of points draws a line segment. |
| 3200 | count minus one lines are drawn; the first and last point are used once. |
| 3201 | |
| 3202 | Each line segment respects paint Paint_Stroke_Cap and Paint_Stroke_Width. |
| 3203 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3204 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3205 | Always draws each element one at a time; is not affected by |
| 3206 | Paint_Stroke_Join, and unlike drawPath, does not create a mask from all points |
| 3207 | and lines before drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3208 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 3209 | #Param mode whether pts draws points or lines ## |
| 3210 | #Param count number of points in the array ## |
| 3211 | #Param pts array of points to draw ## |
| 3212 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3213 | |
| 3214 | #Example |
| 3215 | #Height 200 |
| 3216 | #Description |
| 3217 | #List |
| 3218 | # The first column draws points. ## |
| 3219 | # The second column draws points as lines. ## |
| 3220 | # The third column draws points as a polygon. ## |
| 3221 | # The fourth column draws points as a polygonal path. ## |
| 3222 | # The first row uses a round cap and round join. ## |
| 3223 | # The second row uses a square cap and a miter join. ## |
| 3224 | # The third row uses a butt cap and a bevel join. ## |
| 3225 | ## |
| 3226 | The transparent color makes multiple line draws visible; |
| 3227 | the path is drawn all at once. |
| 3228 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3229 | void draw(SkCanvas* canvas) { |
| 3230 | SkPaint paint; |
| 3231 | paint.setAntiAlias(true); |
| 3232 | paint.setStyle(SkPaint::kStroke_Style); |
| 3233 | paint.setStrokeWidth(10); |
| 3234 | paint.setColor(0x80349a45); |
| 3235 | const SkPoint points[] = {{32, 16}, {48, 48}, {16, 32}}; |
| 3236 | const SkPaint::Join join[] = { SkPaint::kRound_Join, |
| 3237 | SkPaint::kMiter_Join, |
| 3238 | SkPaint::kBevel_Join }; |
| 3239 | int joinIndex = 0; |
| 3240 | SkPath path; |
| 3241 | path.addPoly(points, 3, false); |
| 3242 | for (const auto cap : { SkPaint::kRound_Cap, SkPaint::kSquare_Cap, SkPaint::kButt_Cap } ) { |
| 3243 | paint.setStrokeCap(cap); |
| 3244 | paint.setStrokeJoin(join[joinIndex++]); |
| 3245 | for (const auto mode : { SkCanvas::kPoints_PointMode, |
| 3246 | SkCanvas::kLines_PointMode, |
| 3247 | SkCanvas::kPolygon_PointMode } ) { |
| 3248 | canvas->drawPoints(mode, 3, points, paint); |
| 3249 | canvas->translate(64, 0); |
| 3250 | } |
| 3251 | canvas->drawPath(path, paint); |
| 3252 | canvas->translate(-192, 64); |
| 3253 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3254 | } |
| 3255 | ## |
| 3256 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3257 | #SeeAlso drawLine drawPoint drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3258 | |
| 3259 | ## |
| 3260 | |
| 3261 | # ------------------------------------------------------------------------------ |
| 3262 | |
| 3263 | #Method void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) |
| 3264 | |
| 3265 | Draw point at (x, y) using Clip, Matrix and Paint paint. |
| 3266 | |
| 3267 | The shape of point drawn depends on paint Paint_Stroke_Cap. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3268 | If paint is set to SkPaint::kRound_Cap, draw a circle of diameter |
| 3269 | Paint_Stroke_Width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3270 | draw a square of width and height Paint_Stroke_Width. |
| 3271 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3272 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3273 | #Param x left edge of circle or square ## |
| 3274 | #Param y top edge of circle or square ## |
| 3275 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3276 | |
| 3277 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3278 | void draw(SkCanvas* canvas) { |
| 3279 | SkPaint paint; |
| 3280 | paint.setAntiAlias(true); |
| 3281 | paint.setColor(0x80349a45); |
| 3282 | paint.setStyle(SkPaint::kStroke_Style); |
| 3283 | paint.setStrokeWidth(100); |
| 3284 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 3285 | canvas->scale(1, 1.2f); |
| 3286 | canvas->drawPoint(64, 96, paint); |
| 3287 | canvas->scale(.6f, .8f); |
| 3288 | paint.setColor(SK_ColorWHITE); |
| 3289 | canvas->drawPoint(106, 120, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3290 | } |
| 3291 | ## |
| 3292 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3293 | #SeeAlso drawPoints drawCircle drawRect drawLine drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3294 | |
| 3295 | ## |
| 3296 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3297 | #Method void drawPoint(SkPoint p, const SkPaint& paint) |
| 3298 | |
| 3299 | Draw point p using Clip, Matrix and Paint paint. |
| 3300 | |
| 3301 | The shape of point drawn depends on paint Paint_Stroke_Cap. |
| 3302 | If paint is set to SkPaint::kRound_Cap, draw a circle of diameter |
| 3303 | Paint_Stroke_Width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap, |
| 3304 | draw a square of width and height Paint_Stroke_Width. |
| 3305 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3306 | |
| 3307 | #Param p top-left edge of circle or square ## |
| 3308 | #Param paint stroke, blend, color, and so on, used to draw ## |
| 3309 | |
| 3310 | #Example |
| 3311 | void draw(SkCanvas* canvas) { |
| 3312 | SkPaint paint; |
| 3313 | paint.setAntiAlias(true); |
| 3314 | paint.setColor(0x80349a45); |
| 3315 | paint.setStyle(SkPaint::kStroke_Style); |
| 3316 | paint.setStrokeWidth(100); |
| 3317 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 3318 | canvas->scale(1, 1.2f); |
| 3319 | canvas->drawPoint({64, 96}, paint); |
| 3320 | canvas->scale(.6f, .8f); |
| 3321 | paint.setColor(SK_ColorWHITE); |
| 3322 | canvas->drawPoint(106, 120, paint); |
| 3323 | } |
| 3324 | ## |
| 3325 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3326 | #SeeAlso drawPoints drawCircle drawRect drawLine drawPath |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3327 | |
| 3328 | ## |
| 3329 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3330 | # ------------------------------------------------------------------------------ |
| 3331 | |
| 3332 | #Method void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint) |
| 3333 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3334 | Draws line segment from (x0, y0) to (x1, y1) using Clip, Matrix, and Paint paint. |
| 3335 | In paint: Paint_Stroke_Width describes the line thickness; |
| 3336 | Paint_Stroke_Cap draws the end rounded or square; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3337 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3338 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3339 | #Param x0 start of line segment on x-axis ## |
| 3340 | #Param y0 start of line segment on y-axis ## |
| 3341 | #Param x1 end of line segment on x-axis ## |
| 3342 | #Param y1 end of line segment on y-axis ## |
| 3343 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3344 | |
| 3345 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3346 | SkPaint paint; |
| 3347 | paint.setAntiAlias(true); |
| 3348 | paint.setColor(0xFF9a67be); |
| 3349 | paint.setStrokeWidth(20); |
| 3350 | canvas->skew(1, 0); |
| 3351 | canvas->drawLine(32, 96, 32, 160, paint); |
| 3352 | canvas->skew(-2, 0); |
| 3353 | canvas->drawLine(288, 96, 288, 160, paint); |
| 3354 | ## |
| 3355 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3356 | #SeeAlso drawPoint drawCircle drawRect drawPath |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3357 | |
| 3358 | ## |
| 3359 | |
| 3360 | #Method void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) |
| 3361 | |
| 3362 | Draws line segment from p0 to p1 using Clip, Matrix, and Paint paint. |
| 3363 | In paint: Paint_Stroke_Width describes the line thickness; |
| 3364 | Paint_Stroke_Cap draws the end rounded or square; |
| 3365 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3366 | |
| 3367 | #Param p0 start of line segment ## |
| 3368 | #Param p1 end of line segment ## |
| 3369 | #Param paint stroke, blend, color, and so on, used to draw ## |
| 3370 | |
| 3371 | #Example |
| 3372 | SkPaint paint; |
| 3373 | paint.setAntiAlias(true); |
| 3374 | paint.setColor(0xFF9a67be); |
| 3375 | paint.setStrokeWidth(20); |
| 3376 | canvas->skew(1, 0); |
| 3377 | canvas->drawLine({32, 96}, {32, 160}, paint); |
| 3378 | canvas->skew(-2, 0); |
| 3379 | canvas->drawLine({288, 96}, {288, 160}, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3380 | ## |
| 3381 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3382 | #SeeAlso drawPoint drawCircle drawRect drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3383 | |
| 3384 | ## |
| 3385 | |
| 3386 | # ------------------------------------------------------------------------------ |
| 3387 | |
| 3388 | #Method void drawRect(const SkRect& rect, const SkPaint& paint) |
| 3389 | |
| 3390 | Draw Rect rect using Clip, Matrix, and Paint paint. |
| 3391 | In paint: Paint_Style determines if rectangle is stroked or filled; |
| 3392 | if stroked, Paint_Stroke_Width describes the line thickness, and |
| 3393 | Paint_Stroke_Join draws the corners rounded or square. |
| 3394 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3395 | #Param rect rectangle to draw ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3396 | #Param paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3397 | |
| 3398 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3399 | void draw(SkCanvas* canvas) { |
| 3400 | SkPoint rectPts[] = { {64, 48}, {192, 160} }; |
| 3401 | SkPaint paint; |
| 3402 | paint.setAntiAlias(true); |
| 3403 | paint.setStyle(SkPaint::kStroke_Style); |
| 3404 | paint.setStrokeWidth(20); |
| 3405 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3406 | SkMatrix rotator; |
| 3407 | rotator.setRotate(30, 128, 128); |
| 3408 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorMAGENTA } ) { |
| 3409 | paint.setColor(color); |
| 3410 | SkRect rect; |
| 3411 | rect.set(rectPts[0], rectPts[1]); |
| 3412 | canvas->drawRect(rect, paint); |
| 3413 | rotator.mapPoints(rectPts, 2); |
| 3414 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3415 | } |
| 3416 | ## |
| 3417 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3418 | #SeeAlso drawIRect drawRRect drawRoundRect drawRegion drawPath drawLine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3419 | |
| 3420 | ## |
| 3421 | |
| 3422 | # ------------------------------------------------------------------------------ |
| 3423 | |
| 3424 | #Method void drawIRect(const SkIRect& rect, const SkPaint& paint) |
| 3425 | |
| 3426 | Draw IRect rect using Clip, Matrix, and Paint paint. |
| 3427 | In paint: Paint_Style determines if rectangle is stroked or filled; |
| 3428 | if stroked, Paint_Stroke_Width describes the line thickness, and |
| 3429 | Paint_Stroke_Join draws the corners rounded or square. |
| 3430 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3431 | #Param rect rectangle to draw ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3432 | #Param paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3433 | |
| 3434 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3435 | SkIRect rect = { 64, 48, 192, 160 }; |
| 3436 | SkPaint paint; |
| 3437 | paint.setAntiAlias(true); |
| 3438 | paint.setStyle(SkPaint::kStroke_Style); |
| 3439 | paint.setStrokeWidth(20); |
| 3440 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3441 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorMAGENTA } ) { |
| 3442 | paint.setColor(color); |
| 3443 | canvas->drawIRect(rect, paint); |
| 3444 | canvas->rotate(30, 128, 128); |
| 3445 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3446 | ## |
| 3447 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3448 | #SeeAlso drawRect drawRRect drawRoundRect drawRegion drawPath drawLine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3449 | |
| 3450 | ## |
| 3451 | |
| 3452 | # ------------------------------------------------------------------------------ |
| 3453 | |
| 3454 | #Method void drawRegion(const SkRegion& region, const SkPaint& paint) |
| 3455 | |
| 3456 | Draw Region region using Clip, Matrix, and Paint paint. |
| 3457 | In paint: Paint_Style determines if rectangle is stroked or filled; |
| 3458 | if stroked, Paint_Stroke_Width describes the line thickness, and |
| 3459 | Paint_Stroke_Join draws the corners rounded or square. |
| 3460 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3461 | #Param region region to draw ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3462 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3463 | |
| 3464 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3465 | void draw(SkCanvas* canvas) { |
| 3466 | SkRegion region; |
| 3467 | region.op( 10, 10, 50, 50, SkRegion::kUnion_Op); |
| 3468 | region.op( 10, 50, 90, 90, SkRegion::kUnion_Op); |
| 3469 | SkPaint paint; |
| 3470 | paint.setAntiAlias(true); |
| 3471 | paint.setStyle(SkPaint::kStroke_Style); |
| 3472 | paint.setStrokeWidth(20); |
| 3473 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3474 | canvas->drawRegion(region, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3475 | } |
| 3476 | ## |
| 3477 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3478 | #SeeAlso drawRect drawIRect drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3479 | |
| 3480 | ## |
| 3481 | |
| 3482 | # ------------------------------------------------------------------------------ |
| 3483 | |
| 3484 | #Method void drawOval(const SkRect& oval, const SkPaint& paint) |
| 3485 | |
| 3486 | Draw Oval oval using Clip, Matrix, and Paint. |
| 3487 | In paint: Paint_Style determines if Oval is stroked or filled; |
| 3488 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3489 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3490 | #Param oval Rect bounds of Oval ## |
| 3491 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3492 | |
| 3493 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3494 | void draw(SkCanvas* canvas) { |
| 3495 | canvas->clear(0xFF3f5f9f); |
| 3496 | SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0x7f, 0); |
| 3497 | SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; |
| 3498 | SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; |
| 3499 | SkScalar pos[] = { 0.2f, 1.0f }; |
| 3500 | SkRect bounds = SkRect::MakeWH(80, 70); |
| 3501 | SkPaint paint; |
| 3502 | paint.setAntiAlias(true); |
| 3503 | paint.setShader(SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors), |
| 3504 | SkShader::kClamp_TileMode)); |
| 3505 | canvas->drawOval(bounds , paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3506 | } |
| 3507 | ## |
| 3508 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3509 | #SeeAlso drawCircle drawPoint drawPath drawRRect drawRoundRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3510 | |
| 3511 | ## |
| 3512 | |
| 3513 | # ------------------------------------------------------------------------------ |
| 3514 | |
| 3515 | #Method void drawRRect(const SkRRect& rrect, const SkPaint& paint) |
| 3516 | |
| 3517 | Draw Round_Rect rrect using Clip, Matrix, and Paint paint. |
| 3518 | In paint: Paint_Style determines if rrect is stroked or filled; |
| 3519 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3520 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3521 | rrect may represent a rectangle, circle, oval, uniformly rounded rectangle, or |
| 3522 | may have any combination of positive non-square radii for the four corners. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3523 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3524 | #Param rrect Round_Rect with up to eight corner radii to draw ## |
| 3525 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3526 | |
| 3527 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3528 | void draw(SkCanvas* canvas) { |
| 3529 | SkPaint paint; |
| 3530 | paint.setAntiAlias(true); |
| 3531 | SkRect outer = {30, 40, 210, 220}; |
| 3532 | SkRect radii = {30, 50, 70, 90 }; |
| 3533 | SkRRect rRect; |
| 3534 | rRect.setNinePatch(outer, radii.fLeft, radii.fTop, radii.fRight, radii.fBottom); |
| 3535 | canvas->drawRRect(rRect, paint); |
| 3536 | paint.setColor(SK_ColorWHITE); |
| 3537 | canvas->drawLine(outer.fLeft + radii.fLeft, outer.fTop, |
| 3538 | outer.fLeft + radii.fLeft, outer.fBottom, paint); |
| 3539 | canvas->drawLine(outer.fRight - radii.fRight, outer.fTop, |
| 3540 | outer.fRight - radii.fRight, outer.fBottom, paint); |
| 3541 | canvas->drawLine(outer.fLeft, outer.fTop + radii.fTop, |
| 3542 | outer.fRight, outer.fTop + radii.fTop, paint); |
| 3543 | canvas->drawLine(outer.fLeft, outer.fBottom - radii.fBottom, |
| 3544 | outer.fRight, outer.fBottom - radii.fBottom, paint); |
| 3545 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3546 | ## |
| 3547 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3548 | #SeeAlso drawRect drawRoundRect drawDRRect drawCircle drawOval drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3549 | |
| 3550 | ## |
| 3551 | |
| 3552 | # ------------------------------------------------------------------------------ |
| 3553 | |
| 3554 | #Method void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) |
| 3555 | |
| 3556 | Draw Round_Rect outer and inner |
| 3557 | using Clip, Matrix, and Paint paint. |
| 3558 | outer must contain inner or the drawing is undefined. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3559 | In paint: Paint_Style determines if Round_Rect is stroked or filled; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3560 | if stroked, Paint_Stroke_Width describes the line thickness. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3561 | If stroked and Round_Rect corner has zero length radii, Paint_Stroke_Join can |
| 3562 | draw corners rounded or square. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3563 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3564 | GPU-backed platforms optimize drawing when both outer and inner are |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3565 | concave and outer contains inner. These platforms may not be able to draw |
| 3566 | Path built with identical data as fast. |
| 3567 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3568 | #Param outer Round_Rect outer bounds to draw ## |
| 3569 | #Param inner Round_Rect inner bounds to draw ## |
| 3570 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3571 | |
| 3572 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3573 | void draw(SkCanvas* canvas) { |
| 3574 | SkRRect outer = SkRRect::MakeRect({20, 40, 210, 200}); |
| 3575 | SkRRect inner = SkRRect::MakeOval({60, 70, 170, 160}); |
| 3576 | SkPaint paint; |
| 3577 | canvas->drawDRRect(outer, inner, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3578 | } |
| 3579 | ## |
| 3580 | |
| 3581 | #Example |
| 3582 | #Description |
| 3583 | Outer Rect has no corner radii, but stroke join is rounded. |
| 3584 | Inner Round_Rect has corner radii; outset stroke increases radii of corners. |
| 3585 | Stroke join does not affect inner Round_Rect since it has no sharp corners. |
| 3586 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3587 | void draw(SkCanvas* canvas) { |
| 3588 | SkRRect outer = SkRRect::MakeRect({20, 40, 210, 200}); |
| 3589 | SkRRect inner = SkRRect::MakeRectXY({60, 70, 170, 160}, 10, 10); |
| 3590 | SkPaint paint; |
| 3591 | paint.setAntiAlias(true); |
| 3592 | paint.setStyle(SkPaint::kStroke_Style); |
| 3593 | paint.setStrokeWidth(20); |
| 3594 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3595 | canvas->drawDRRect(outer, inner, paint); |
| 3596 | paint.setStrokeWidth(1); |
| 3597 | paint.setColor(SK_ColorWHITE); |
| 3598 | canvas->drawDRRect(outer, inner, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3599 | } |
| 3600 | ## |
| 3601 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3602 | #SeeAlso drawRect drawRoundRect drawRRect drawCircle drawOval drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3603 | |
| 3604 | ## |
| 3605 | |
| 3606 | # ------------------------------------------------------------------------------ |
| 3607 | |
| 3608 | #Method void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint) |
| 3609 | |
| 3610 | Draw Circle at (cx, cy) with radius using Clip, Matrix, and Paint paint. |
| 3611 | If radius is zero or less, nothing is drawn. |
| 3612 | In paint: Paint_Style determines if Circle is stroked or filled; |
| 3613 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3614 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3615 | #Param cx Circle center on the x-axis ## |
| 3616 | #Param cy Circle center on the y-axis ## |
| 3617 | #Param radius half the diameter of Circle ## |
| 3618 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3619 | |
| 3620 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3621 | void draw(SkCanvas* canvas) { |
| 3622 | SkPaint paint; |
| 3623 | paint.setAntiAlias(true); |
| 3624 | canvas->drawCircle(128, 128, 90, paint); |
| 3625 | paint.setColor(SK_ColorWHITE); |
| 3626 | canvas->drawCircle(86, 86, 20, paint); |
| 3627 | canvas->drawCircle(160, 76, 20, paint); |
| 3628 | canvas->drawCircle(140, 150, 35, paint); |
| 3629 | } |
| 3630 | ## |
| 3631 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3632 | #SeeAlso drawOval drawRRect drawRoundRect drawPath drawArc drawPoint drawLine |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3633 | |
| 3634 | ## |
| 3635 | |
| 3636 | #Method void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) |
| 3637 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3638 | Draw Circle at center with radius using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3639 | If radius is zero or less, nothing is drawn. |
| 3640 | In paint: Paint_Style determines if Circle is stroked or filled; |
| 3641 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3642 | |
| 3643 | #Param center Circle center ## |
| 3644 | #Param radius half the diameter of Circle ## |
| 3645 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
| 3646 | |
| 3647 | #Example |
| 3648 | void draw(SkCanvas* canvas) { |
| 3649 | SkPaint paint; |
| 3650 | paint.setAntiAlias(true); |
| 3651 | canvas->drawCircle(128, 128, 90, paint); |
| 3652 | paint.setColor(SK_ColorWHITE); |
| 3653 | canvas->drawCircle({86, 86}, 20, paint); |
| 3654 | canvas->drawCircle({160, 76}, 20, paint); |
| 3655 | canvas->drawCircle({140, 150}, 35, paint); |
| 3656 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3657 | ## |
| 3658 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3659 | #SeeAlso drawOval drawRRect drawRoundRect drawPath drawArc drawPoint drawLine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3660 | |
| 3661 | ## |
| 3662 | |
| 3663 | # ------------------------------------------------------------------------------ |
| 3664 | |
| 3665 | #Method void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 3666 | bool useCenter, const SkPaint& paint) |
| 3667 | |
| 3668 | Draw Arc using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3669 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3670 | Arc is part of Oval bounded by oval, sweeping from startAngle to startAngle plus |
| 3671 | sweepAngle. startAngle and sweepAngle are in degrees. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3672 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3673 | startAngle of zero places start point at the right middle edge of oval. |
| 3674 | A positive sweepAngle places Arc end point clockwise from start point; |
| 3675 | a negative sweepAngle places Arc end point counterclockwise from start point. |
| 3676 | sweepAngle may exceed 360 degrees, a full circle. |
| 3677 | If useCenter is true, draw a wedge that includes lines from oval |
| 3678 | center to Arc end points. If useCenter is false, draw Arc between end points. |
| 3679 | |
| 3680 | If Rect oval is empty or sweepAngle is zero, nothing is drawn. |
| 3681 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3682 | #Param oval Rect bounds of Oval containing Arc to draw ## |
| 3683 | #Param startAngle angle in degrees where Arc begins ## |
| 3684 | #Param sweepAngle sweep angle in degrees; positive is clockwise ## |
| 3685 | #Param useCenter if true, include the center of the oval ## |
| 3686 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3687 | |
| 3688 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3689 | void draw(SkCanvas* canvas) { |
| 3690 | SkPaint paint; |
| 3691 | paint.setAntiAlias(true); |
| 3692 | SkRect oval = { 4, 4, 60, 60}; |
| 3693 | for (auto useCenter : { false, true } ) { |
| 3694 | for (auto style : { SkPaint::kFill_Style, SkPaint::kStroke_Style } ) { |
| 3695 | paint.setStyle(style); |
| 3696 | for (auto degrees : { 45, 90, 180, 360} ) { |
| 3697 | canvas->drawArc(oval, 0, degrees , useCenter, paint); |
| 3698 | canvas->translate(64, 0); |
| 3699 | } |
| 3700 | canvas->translate(-256, 64); |
| 3701 | } |
| 3702 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3703 | } |
| 3704 | ## |
| 3705 | |
| 3706 | #Example |
| 3707 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3708 | void draw(SkCanvas* canvas) { |
| 3709 | SkPaint paint; |
| 3710 | paint.setAntiAlias(true); |
| 3711 | paint.setStyle(SkPaint::kStroke_Style); |
| 3712 | paint.setStrokeWidth(4); |
| 3713 | SkRect oval = { 4, 4, 60, 60}; |
| 3714 | float intervals[] = { 5, 5 }; |
| 3715 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 2.5f)); |
| 3716 | for (auto degrees : { 270, 360, 540, 720 } ) { |
| 3717 | canvas->drawArc(oval, 0, degrees, false, paint); |
| 3718 | canvas->translate(64, 0); |
| 3719 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3720 | } |
| 3721 | ## |
| 3722 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3723 | #SeeAlso SkPath::arcTo drawCircle drawOval drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3724 | |
| 3725 | ## |
| 3726 | |
| 3727 | # ------------------------------------------------------------------------------ |
| 3728 | |
| 3729 | #Method void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint) |
| 3730 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3731 | Draw Round_Rect bounded by Rect rect, with corner radii (rx, ry) using Clip, |
| 3732 | Matrix, and Paint paint. |
| 3733 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3734 | In paint: Paint_Style determines if Round_Rect is stroked or filled; |
| 3735 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3736 | If rx or ry are less than zero, they are treated as if they are zero. |
| 3737 | If rx plus ry exceeds rect width or rect height, radii are scaled down to fit. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3738 | If rx and ry are zero, Round_Rect is drawn as Rect and if stroked is affected by |
| 3739 | Paint_Stroke_Join. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3740 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3741 | #Param rect Rect bounds of Round_Rect to draw ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3742 | #Param rx axis length in x of oval describing rounded corners ## |
| 3743 | #Param ry axis length in y of oval describing rounded corners ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3744 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3745 | |
| 3746 | #Example |
| 3747 | #Description |
| 3748 | Top row has a zero radius a generates a rectangle. |
| 3749 | Second row radii sum to less than sides. |
| 3750 | Third row radii sum equals sides. |
| 3751 | Fourth row radii sum exceeds sides; radii are scaled to fit. |
| 3752 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3753 | void draw(SkCanvas* canvas) { |
| 3754 | SkVector radii[] = { {0, 20}, {10, 10}, {10, 20}, {10, 40} }; |
| 3755 | SkPaint paint; |
| 3756 | paint.setStrokeWidth(15); |
| 3757 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3758 | paint.setAntiAlias(true); |
| 3759 | for (auto style : { SkPaint::kStroke_Style, SkPaint::kFill_Style } ) { |
| 3760 | paint.setStyle(style ); |
| 3761 | for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) { |
| 3762 | canvas->drawRoundRect({10, 10, 60, 40}, radii[i].fX, radii[i].fY, paint); |
| 3763 | canvas->translate(0, 60); |
| 3764 | } |
| 3765 | canvas->translate(80, -240); |
| 3766 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3767 | } |
| 3768 | ## |
| 3769 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3770 | #SeeAlso DrawRRect drawRect drawDRRect drawPath drawCircle drawOval drawPoint |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3771 | |
| 3772 | ## |
| 3773 | |
| 3774 | # ------------------------------------------------------------------------------ |
| 3775 | |
| 3776 | #Method void drawPath(const SkPath& path, const SkPaint& paint) |
| 3777 | |
| 3778 | Draw Path path using Clip, Matrix, and Paint paint. |
| 3779 | Path contains an array of Path_Contour, each of which may be open or closed. |
| 3780 | |
| 3781 | In paint: Paint_Style determines if Round_Rect is stroked or filled: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3782 | if filled, Path_Fill_Type determines whether Path_Contour describes inside or |
| 3783 | outside of fill; if stroked, Paint_Stroke_Width describes the line thickness, |
| 3784 | Paint_Stroke_Cap describes line ends, and Paint_Stroke_Join describes how |
| 3785 | corners are drawn. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3786 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3787 | #Param path Path to draw ## |
| 3788 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3789 | |
| 3790 | #Example |
| 3791 | #Description |
| 3792 | Top rows draw stroked path with combinations of joins and caps. The open contour |
| 3793 | is affected by caps; the closed contour is affected by joins. |
| 3794 | Bottom row draws fill the same for open and closed contour. |
| 3795 | First bottom column shows winding fills overlap. |
| 3796 | Second bottom column shows even odd fills exclude overlap. |
| 3797 | Third bottom column shows inverse winding fills area outside both contours. |
| 3798 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3799 | void draw(SkCanvas* canvas) { |
| 3800 | SkPath path; |
| 3801 | path.moveTo(20, 20); |
| 3802 | path.quadTo(60, 20, 60, 60); |
| 3803 | path.close(); |
| 3804 | path.moveTo(60, 20); |
| 3805 | path.quadTo(60, 60, 20, 60); |
| 3806 | SkPaint paint; |
| 3807 | paint.setStrokeWidth(10); |
| 3808 | paint.setAntiAlias(true); |
| 3809 | paint.setStyle(SkPaint::kStroke_Style); |
| 3810 | for (auto join: { SkPaint::kBevel_Join, SkPaint::kRound_Join, SkPaint::kMiter_Join } ) { |
| 3811 | paint.setStrokeJoin(join); |
| 3812 | for (auto cap: { SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap } ) { |
| 3813 | paint.setStrokeCap(cap); |
| 3814 | canvas->drawPath(path, paint); |
| 3815 | canvas->translate(80, 0); |
| 3816 | } |
| 3817 | canvas->translate(-240, 60); |
| 3818 | } |
| 3819 | paint.setStyle(SkPaint::kFill_Style); |
| 3820 | for (auto fill : { SkPath::kWinding_FillType, |
| 3821 | SkPath::kEvenOdd_FillType, |
| 3822 | SkPath::kInverseWinding_FillType } ) { |
| 3823 | path.setFillType(fill); |
| 3824 | canvas->save(); |
| 3825 | canvas->clipRect({0, 10, 80, 70}); |
| 3826 | canvas->drawPath(path, paint); |
| 3827 | canvas->restore(); |
| 3828 | canvas->translate(80, 0); |
| 3829 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3830 | } |
| 3831 | ## |
| 3832 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3833 | #SeeAlso SkPath drawLine drawArc drawRect drawPoints |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3834 | |
| 3835 | ## |
| 3836 | |
| 3837 | # ------------------------------------------------------------------------------ |
| 3838 | #Topic Draw_Image |
| 3839 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3840 | drawImage, drawImageRect, and drawImageNine can be called with a bare pointer or |
| 3841 | a smart pointer as a convenience. The pairs of calls are otherwise identical. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3842 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 3843 | #Method void drawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3844 | |
| 3845 | Draw Image image, with its top-left corner at (left, top), |
| 3846 | using Clip, Matrix, and optional Paint paint. |
| 3847 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3848 | If paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, Blend_Mode, |
| 3849 | and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 3850 | If paint contains Mask_Filter, generate mask from image bounds. If generated |
| 3851 | mask extends beyond image bounds, replicate image edge colors, just as Shader |
| 3852 | made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3853 | image edge color when it samples outside of its bounds. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3854 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3855 | #Param image uncompressed rectangular map of pixels ## |
| 3856 | #Param left left side of image ## |
| 3857 | #Param top top side of image ## |
| 3858 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 3859 | and so on; or nullptr |
| 3860 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3861 | |
| 3862 | #Example |
| 3863 | #Height 64 |
| 3864 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3865 | void draw(SkCanvas* canvas) { |
| 3866 | // sk_sp<SkImage> image; |
| 3867 | SkImage* imagePtr = image.get(); |
| 3868 | canvas->drawImage(imagePtr, 0, 0); |
| 3869 | SkPaint paint; |
| 3870 | canvas->drawImage(imagePtr, 80, 0, &paint); |
| 3871 | paint.setAlpha(0x80); |
| 3872 | canvas->drawImage(imagePtr, 160, 0, &paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3873 | } |
| 3874 | ## |
| 3875 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3876 | #SeeAlso drawBitmap drawImageLattice drawImageNine drawImageRect SkPaint::setImageFilter |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3877 | |
| 3878 | ## |
| 3879 | |
| 3880 | # ------------------------------------------------------------------------------ |
| 3881 | |
| 3882 | #Method void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top, |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 3883 | const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3884 | |
| 3885 | Draw Image image, with its top-left corner at (left, top), |
| 3886 | using Clip, Matrix, and optional Paint paint. |
| 3887 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3888 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 3889 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 3890 | If paint contains Mask_Filter, generate mask from image bounds. If generated |
| 3891 | mask extends beyond image bounds, replicate image edge colors, just as Shader |
| 3892 | made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3893 | image edge color when it samples outside of its bounds. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3894 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3895 | #Param image uncompressed rectangular map of pixels ## |
| 3896 | #Param left left side of image ## |
| 3897 | #Param top pop side of image ## |
| 3898 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 3899 | and so on; or nullptr |
| 3900 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3901 | |
| 3902 | #Example |
| 3903 | #Height 64 |
| 3904 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3905 | void draw(SkCanvas* canvas) { |
| 3906 | // sk_sp<SkImage> image; |
| 3907 | canvas->drawImage(image, 0, 0); |
| 3908 | SkPaint paint; |
| 3909 | canvas->drawImage(image, 80, 0, &paint); |
| 3910 | paint.setAlpha(0x80); |
| 3911 | canvas->drawImage(image, 160, 0, &paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3912 | } |
| 3913 | ## |
| 3914 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3915 | #SeeAlso drawBitmap drawImageLattice drawImageNine drawImageRect SkPaint::setImageFilter |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3916 | |
| 3917 | ## |
| 3918 | |
| 3919 | # ------------------------------------------------------------------------------ |
| 3920 | |
| 3921 | #Enum SrcRectConstraint |
| 3922 | |
| 3923 | #Code |
| 3924 | enum SrcRectConstraint { |
| 3925 | kStrict_SrcRectConstraint, |
| 3926 | kFast_SrcRectConstraint, |
| 3927 | }; |
| 3928 | ## |
| 3929 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3930 | SrcRectConstraint controls the behavior at the edge of source Rect, |
| 3931 | provided to drawImageRect, trading off speed for precision. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3932 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3933 | Image_Filter in Paint may sample multiple pixels in the image. Source Rect |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3934 | restricts the bounds of pixels that may be read. Image_Filter may slow down if |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3935 | it cannot read outside the bounds, when sampling near the edge of source Rect. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3936 | SrcRectConstraint specifies whether an Image_Filter is allowed to read pixels |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3937 | outside source Rect. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3938 | |
| 3939 | #Const kStrict_SrcRectConstraint |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3940 | Requires Image_Filter to respect source Rect, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3941 | sampling only inside of its bounds, possibly with a performance penalty. |
| 3942 | ## |
| 3943 | |
| 3944 | #Const kFast_SrcRectConstraint |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3945 | Permits Image_Filter to sample outside of source Rect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3946 | by half the width of Image_Filter, permitting it to run faster but with |
| 3947 | error at the image edges. |
| 3948 | ## |
| 3949 | |
| 3950 | #Example |
| 3951 | #Height 64 |
| 3952 | #Description |
| 3953 | redBorder contains a black and white checkerboard bordered by red. |
| 3954 | redBorder is drawn scaled by 16 on the left. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3955 | The middle and right bitmaps are filtered checkerboards. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3956 | Drawing the checkerboard with kStrict_SrcRectConstraint shows only a blur of black and white. |
| 3957 | Drawing the checkerboard with kFast_SrcRectConstraint allows red to bleed in the corners. |
| 3958 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3959 | void draw(SkCanvas* canvas) { |
| 3960 | SkBitmap redBorder; |
| 3961 | redBorder.allocPixels(SkImageInfo::MakeN32Premul(4, 4)); |
| 3962 | SkCanvas checkRed(redBorder); |
| 3963 | checkRed.clear(SK_ColorRED); |
| 3964 | uint32_t checkers[][2] = { { SK_ColorBLACK, SK_ColorWHITE }, |
| 3965 | { SK_ColorWHITE, SK_ColorBLACK } }; |
| 3966 | checkRed.writePixels( |
| 3967 | SkImageInfo::MakeN32Premul(2, 2), (void*) checkers, sizeof(checkers[0]), 1, 1); |
| 3968 | canvas->scale(16, 16); |
| 3969 | canvas->drawBitmap(redBorder, 0, 0, nullptr); |
| 3970 | canvas->resetMatrix(); |
| 3971 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(redBorder); |
| 3972 | SkPaint lowPaint; |
| 3973 | lowPaint.setFilterQuality(kLow_SkFilterQuality); |
| 3974 | for (auto constraint : { SkCanvas::kStrict_SrcRectConstraint, |
| 3975 | SkCanvas::kFast_SrcRectConstraint } ) { |
| 3976 | canvas->translate(80, 0); |
| 3977 | canvas->drawImageRect(image.get(), SkRect::MakeLTRB(1, 1, 3, 3), |
| 3978 | SkRect::MakeLTRB(16, 16, 48, 48), &lowPaint, constraint); |
| 3979 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3980 | } |
| 3981 | ## |
| 3982 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3983 | #SeeAlso drawImageRect drawImage SkPaint::setImageFilter |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3984 | |
| 3985 | ## |
| 3986 | |
| 3987 | # ------------------------------------------------------------------------------ |
| 3988 | |
| 3989 | #Method void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst, |
| 3990 | const SkPaint* paint, |
| 3991 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 3992 | |
| 3993 | Draw Rect src of Image image, scaled and translated to fill Rect dst. |
| 3994 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3995 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3996 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 3997 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 3998 | If paint contains Mask_Filter, generate mask from image bounds. |
| 3999 | |
| 4000 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4001 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4002 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4003 | |
| 4004 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
| 4005 | sample within src; set to kFast_SrcRectConstraint allows sampling outside to |
| 4006 | improve performance. |
| 4007 | |
| 4008 | #Param image Image containing pixels, dimensions, and format ## |
| 4009 | #Param src source Rect of image to draw from ## |
| 4010 | #Param dst destination Rect of image to draw to ## |
| 4011 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4012 | and so on; or nullptr |
| 4013 | ## |
| 4014 | #Param constraint filter strictly within src or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4015 | |
| 4016 | #Example |
| 4017 | #Height 64 |
| 4018 | #Description |
| 4019 | The left bitmap draws with Paint default kNone_SkFilterQuality, and stays within |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4020 | its bounds; there is no bleeding with kFast_SrcRectConstraint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4021 | the middle and right bitmaps draw with kLow_SkFilterQuality; with |
| 4022 | kStrict_SrcRectConstraint, the filter remains within the checkerboard, and |
| 4023 | with kFast_SrcRectConstraint red bleeds on the edges. |
| 4024 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4025 | void draw(SkCanvas* canvas) { |
| 4026 | uint32_t pixels[][4] = { |
| 4027 | { 0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000 }, |
| 4028 | { 0xFFFF0000, 0xFF000000, 0xFFFFFFFF, 0xFFFF0000 }, |
| 4029 | { 0xFFFF0000, 0xFFFFFFFF, 0xFF000000, 0xFFFF0000 }, |
| 4030 | { 0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000 } }; |
| 4031 | SkBitmap redBorder; |
| 4032 | redBorder.installPixels(SkImageInfo::MakeN32Premul(4, 4), |
| 4033 | (void*) pixels, sizeof(pixels[0])); |
| 4034 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(redBorder); |
| 4035 | SkPaint lowPaint; |
| 4036 | for (auto constraint : { |
| 4037 | SkCanvas::kFast_SrcRectConstraint, |
| 4038 | SkCanvas::kStrict_SrcRectConstraint, |
| 4039 | SkCanvas::kFast_SrcRectConstraint } ) { |
| 4040 | canvas->drawImageRect(image.get(), SkRect::MakeLTRB(1, 1, 3, 3), |
| 4041 | SkRect::MakeLTRB(16, 16, 48, 48), &lowPaint, constraint); |
| 4042 | lowPaint.setFilterQuality(kLow_SkFilterQuality); |
| 4043 | canvas->translate(80, 0); |
| 4044 | } |
| 4045 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4046 | ## |
| 4047 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4048 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4049 | |
| 4050 | ## |
| 4051 | |
| 4052 | # ------------------------------------------------------------------------------ |
| 4053 | |
| 4054 | #Method void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst, |
| 4055 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4056 | |
| 4057 | Draw IRect isrc of Image image, scaled and translated to fill Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4058 | Note that isrc is on integer pixel boundaries; dst may include fractional |
| 4059 | boundaries. Additionally transform draw using Clip, Matrix, and optional Paint |
| 4060 | paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4061 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4062 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4063 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4064 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4065 | |
| 4066 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4067 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4068 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4069 | |
| 4070 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4071 | sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4072 | improve performance. |
| 4073 | |
| 4074 | #Param image Image containing pixels, dimensions, and format ## |
| 4075 | #Param isrc source IRect of image to draw from ## |
| 4076 | #Param dst destination Rect of image to draw to ## |
| 4077 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4078 | and so on; or nullptr |
| 4079 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4080 | #Param constraint filter strictly within isrc or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4081 | |
| 4082 | #Example |
| 4083 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4084 | void draw(SkCanvas* canvas) { |
| 4085 | // sk_sp<SkImage> image; |
| 4086 | for (auto i : { 1, 2, 4, 8 } ) { |
| 4087 | canvas->drawImageRect(image.get(), SkIRect::MakeLTRB(0, 0, 100, 100), |
| 4088 | SkRect::MakeXYWH(i * 20, i * 20, i * 20, i * 20), nullptr); |
| 4089 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4090 | } |
| 4091 | ## |
| 4092 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4093 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4094 | |
| 4095 | ## |
| 4096 | |
| 4097 | # ------------------------------------------------------------------------------ |
| 4098 | |
| 4099 | #Method void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint, |
| 4100 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4101 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4102 | Draw Image image, scaled and translated to fill Rect dst, using Clip, Matrix, |
| 4103 | and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4104 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4105 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4106 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4107 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4108 | |
| 4109 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4110 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4111 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4112 | |
| 4113 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4114 | sample within image; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4115 | improve performance. |
| 4116 | |
| 4117 | #Param image Image containing pixels, dimensions, and format ## |
| 4118 | #Param dst destination Rect of image to draw to ## |
| 4119 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4120 | and so on; or nullptr |
| 4121 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4122 | #Param constraint filter strictly within image or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4123 | |
| 4124 | #Example |
| 4125 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4126 | void draw(SkCanvas* canvas) { |
| 4127 | // sk_sp<SkImage> image; |
| 4128 | for (auto i : { 20, 40, 80, 160 } ) { |
| 4129 | canvas->drawImageRect(image.get(), SkRect::MakeXYWH(i, i, i, i), nullptr); |
| 4130 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4131 | } |
| 4132 | ## |
| 4133 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4134 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4135 | |
| 4136 | ## |
| 4137 | |
| 4138 | # ------------------------------------------------------------------------------ |
| 4139 | |
| 4140 | #Method void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst, |
| 4141 | const SkPaint* paint, |
| 4142 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4143 | |
| 4144 | Draw Rect src of Image image, scaled and translated to fill Rect dst. |
| 4145 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4146 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4147 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4148 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4149 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4150 | |
| 4151 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4152 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4153 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4154 | |
| 4155 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
| 4156 | sample within src; set to kFast_SrcRectConstraint allows sampling outside to |
| 4157 | improve performance. |
| 4158 | |
| 4159 | #Param image Image containing pixels, dimensions, and format ## |
| 4160 | #Param src source Rect of image to draw from ## |
| 4161 | #Param dst destination Rect of image to draw to ## |
| 4162 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4163 | and so on; or nullptr |
| 4164 | ## |
| 4165 | #Param constraint filter strictly within src or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4166 | |
| 4167 | #Example |
| 4168 | #Height 64 |
| 4169 | #Description |
| 4170 | Canvas scales and translates; transformation from src to dst also scales. |
| 4171 | The two matrices are concatenated to create the final transformation. |
| 4172 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4173 | void draw(SkCanvas* canvas) { |
| 4174 | uint32_t pixels[][2] = { { SK_ColorBLACK, SK_ColorWHITE }, |
| 4175 | { SK_ColorWHITE, SK_ColorBLACK } }; |
| 4176 | SkBitmap bitmap; |
| 4177 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4178 | (void*) pixels, sizeof(pixels[0])); |
| 4179 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4180 | SkPaint paint; |
| 4181 | canvas->scale(4, 4); |
| 4182 | for (auto alpha : { 50, 100, 150, 255 } ) { |
| 4183 | paint.setAlpha(alpha); |
| 4184 | canvas->drawImageRect(image, SkRect::MakeWH(2, 2), SkRect::MakeWH(8, 8), &paint); |
| 4185 | canvas->translate(8, 0); |
| 4186 | } |
| 4187 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4188 | ## |
| 4189 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4190 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4191 | |
| 4192 | ## |
| 4193 | |
| 4194 | # ------------------------------------------------------------------------------ |
| 4195 | |
| 4196 | #Method void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst, |
| 4197 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4198 | |
| 4199 | Draw IRect isrc of Image image, scaled and translated to fill Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4200 | isrc is on integer pixel boundaries; dst may include fractional boundaries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4201 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4202 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4203 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4204 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4205 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4206 | |
| 4207 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4208 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4209 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4210 | |
| 4211 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4212 | sample within image; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4213 | improve performance. |
| 4214 | |
| 4215 | #Param image Image containing pixels, dimensions, and format ## |
| 4216 | #Param isrc source IRect of image to draw from ## |
| 4217 | #Param dst destination Rect of image to draw to ## |
| 4218 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4219 | and so on; or nullptr |
| 4220 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4221 | #Param constraint filter strictly within image or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4222 | |
| 4223 | #Example |
| 4224 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4225 | void draw(SkCanvas* canvas) { |
| 4226 | uint32_t pixels[][2] = { { 0x00000000, 0x55555555}, |
| 4227 | { 0xAAAAAAAA, 0xFFFFFFFF} }; |
| 4228 | SkBitmap bitmap; |
| 4229 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4230 | (void*) pixels, sizeof(pixels[0])); |
| 4231 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4232 | SkPaint paint; |
| 4233 | canvas->scale(4, 4); |
| 4234 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN } ) { |
| 4235 | paint.setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kPlus)); |
| 4236 | canvas->drawImageRect(image, SkIRect::MakeWH(2, 2), SkRect::MakeWH(8, 8), &paint); |
| 4237 | canvas->translate(8, 0); |
| 4238 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4239 | } |
| 4240 | ## |
| 4241 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4242 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
| 4243 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4244 | ## |
| 4245 | |
| 4246 | # ------------------------------------------------------------------------------ |
| 4247 | |
| 4248 | #Method void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint, |
| 4249 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4250 | |
| 4251 | Draw Image image, scaled and translated to fill Rect dst, |
| 4252 | using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4253 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4254 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4255 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4256 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4257 | |
| 4258 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4259 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4260 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4261 | |
| 4262 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4263 | sample within image; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4264 | improve performance. |
| 4265 | |
| 4266 | #Param image Image containing pixels, dimensions, and format ## |
| 4267 | #Param dst destination Rect of image to draw to ## |
| 4268 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4269 | and so on; or nullptr |
| 4270 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4271 | #Param constraint filter strictly within image or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4272 | |
| 4273 | #Example |
| 4274 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4275 | void draw(SkCanvas* canvas) { |
| 4276 | uint32_t pixels[][2] = { { 0x00000000, 0x55550000}, |
| 4277 | { 0xAAAA0000, 0xFFFF0000} }; |
| 4278 | SkBitmap bitmap; |
| 4279 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4280 | (void*) pixels, sizeof(pixels[0])); |
| 4281 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4282 | SkPaint paint; |
| 4283 | canvas->scale(4, 4); |
| 4284 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN } ) { |
| 4285 | paint.setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kPlus)); |
| 4286 | canvas->drawImageRect(image, SkRect::MakeWH(8, 8), &paint); |
| 4287 | canvas->translate(8, 0); |
| 4288 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4289 | } |
| 4290 | ## |
| 4291 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4292 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4293 | |
| 4294 | ## |
| 4295 | |
| 4296 | # ------------------------------------------------------------------------------ |
| 4297 | |
| 4298 | #Method void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 4299 | const SkPaint* paint = nullptr) |
| 4300 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4301 | Draw Image image stretched proportionally to fit into Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4302 | IRect center divides the image into nine sections: four sides, four corners, and |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4303 | the center. Corners are unmodified or scaled down proportionately if their sides |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4304 | are larger than dst; center and four sides are scaled to fit remaining space, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4305 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4306 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
| 4307 | |
| 4308 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4309 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4310 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4311 | |
| 4312 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4313 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4314 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4315 | |
| 4316 | #Param image Image containing pixels, dimensions, and format ## |
| 4317 | #Param center IRect edge of image corners and sides ## |
| 4318 | #Param dst destination Rect of image to draw to ## |
| 4319 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4320 | and so on; or nullptr |
| 4321 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4322 | |
| 4323 | #Example |
| 4324 | #Height 128 |
| 4325 | #Description |
| 4326 | The leftmost image is smaller than center; only corners are drawn, all scaled to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4327 | The second image equals the size of center; only corners are drawn without scaling. |
| 4328 | The remaining images are larger than center. All corners draw without scaling. |
| 4329 | The sides and center are scaled if needed to take up the remaining space. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4330 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4331 | void draw(SkCanvas* canvas) { |
| 4332 | SkIRect center = { 20, 10, 50, 40 }; |
| 4333 | SkBitmap bitmap; |
| 4334 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4335 | SkCanvas bitCanvas(bitmap); |
| 4336 | SkPaint paint; |
| 4337 | SkColor gray = 0xFF000000; |
| 4338 | int left = 0; |
| 4339 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4340 | int top = 0; |
| 4341 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4342 | paint.setColor(gray); |
| 4343 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4344 | gray += 0x001f1f1f; |
| 4345 | top = bottom; |
| 4346 | } |
| 4347 | left = right; |
| 4348 | } |
| 4349 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4350 | SkImage* imagePtr = image.get(); |
| 4351 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4352 | canvas->drawImageNine(imagePtr, center, SkRect::MakeWH(dest, dest), nullptr); |
| 4353 | canvas->translate(dest + 4, 0); |
| 4354 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4355 | } |
| 4356 | ## |
| 4357 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4358 | #SeeAlso drawImage drawBitmapNine drawImageLattice drawImageRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4359 | |
| 4360 | ## |
| 4361 | |
| 4362 | # ------------------------------------------------------------------------------ |
| 4363 | |
| 4364 | #Method void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst, |
| 4365 | const SkPaint* paint = nullptr) |
| 4366 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4367 | Draw Image image stretched proportionally to fit into Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4368 | IRect center divides the image into nine sections: four sides, four corners, and |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4369 | the center. Corners are not scaled, or scaled down proportionately if their sides |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4370 | are larger than dst; center and four sides are scaled to fit remaining space, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4371 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4372 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
| 4373 | |
| 4374 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4375 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4376 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4377 | |
| 4378 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4379 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4380 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4381 | |
| 4382 | #Param image Image containing pixels, dimensions, and format ## |
| 4383 | #Param center IRect edge of image corners and sides ## |
| 4384 | #Param dst destination Rect of image to draw to ## |
| 4385 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4386 | and so on; or nullptr |
| 4387 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4388 | |
| 4389 | #Example |
| 4390 | #Height 128 |
| 4391 | #Description |
| 4392 | The two leftmost images has four corners and sides to the left and right of center. |
| 4393 | The leftmost image scales the width of corners proportionately to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4394 | The third and fourth image corners are not scaled; the sides and center are scaled to |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4395 | fill the remaining space. |
| 4396 | The rightmost image has four corners scaled vertically to fit, and uses sides above |
| 4397 | and below center to fill the remaining space. |
| 4398 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4399 | void draw(SkCanvas* canvas) { |
| 4400 | SkIRect center = { 20, 10, 50, 40 }; |
| 4401 | SkBitmap bitmap; |
| 4402 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4403 | SkCanvas bitCanvas(bitmap); |
| 4404 | SkPaint paint; |
| 4405 | SkColor gray = 0xFF000000; |
| 4406 | int left = 0; |
| 4407 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4408 | int top = 0; |
| 4409 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4410 | paint.setColor(gray); |
| 4411 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4412 | gray += 0x001f1f1f; |
| 4413 | top = bottom; |
| 4414 | } |
| 4415 | left = right; |
| 4416 | } |
| 4417 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4418 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4419 | canvas->drawImageNine(image, center, SkRect::MakeWH(dest, 110 - dest), nullptr); |
| 4420 | canvas->translate(dest + 4, 0); |
| 4421 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4422 | } |
| 4423 | ## |
| 4424 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4425 | #SeeAlso drawImage drawBitmapNine drawImageLattice drawImageRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4426 | |
| 4427 | ## |
| 4428 | |
| 4429 | # ------------------------------------------------------------------------------ |
| 4430 | |
| 4431 | #Method void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 4432 | const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4433 | |
| 4434 | Draw Bitmap bitmap, with its top-left corner at (left, top), |
| 4435 | using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4436 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 4437 | If Paint paint is not nullptr, apply Color_Filter, Color_Alpha, Image_Filter, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4438 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4439 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4440 | |
| 4441 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4442 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4443 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4444 | outside of its bounds. |
| 4445 | |
| 4446 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4447 | #Param left left side of bitmap ## |
| 4448 | #Param top top side of bitmap ## |
| 4449 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4450 | and so on; or nullptr |
| 4451 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4452 | |
| 4453 | #Example |
| 4454 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4455 | void draw(SkCanvas* canvas) { |
| 4456 | uint8_t pixels[][8] = { { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00}, |
| 4457 | { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}, |
| 4458 | { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00}, |
| 4459 | { 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF}, |
| 4460 | { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 4461 | { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00}, |
| 4462 | { 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00}, |
| 4463 | { 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF} }; |
| 4464 | SkBitmap bitmap; |
| 4465 | bitmap.installPixels(SkImageInfo::MakeA8(8, 8), |
| 4466 | (void*) pixels, sizeof(pixels[0])); |
| 4467 | SkPaint paint; |
| 4468 | canvas->scale(4, 4); |
| 4469 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xFF007F00} ) { |
| 4470 | paint.setColor(color); |
| 4471 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 4472 | canvas->translate(12, 0); |
| 4473 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4474 | } |
| 4475 | ## |
| 4476 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4477 | #SeeAlso drawImage drawBitmapLattice drawBitmapNine drawBitmapRect SkBitmap::readPixels SkBitmap::writePixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4478 | |
| 4479 | ## |
| 4480 | |
| 4481 | # ------------------------------------------------------------------------------ |
| 4482 | |
| 4483 | #Method void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst, |
| 4484 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4485 | |
| 4486 | Draw Rect src of Bitmap bitmap, scaled and translated to fill Rect dst. |
| 4487 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4488 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4489 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4490 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4491 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4492 | |
| 4493 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4494 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4495 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4496 | outside of its bounds. |
| 4497 | |
| 4498 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
| 4499 | sample within src; set to kFast_SrcRectConstraint allows sampling outside to |
| 4500 | improve performance. |
| 4501 | |
| 4502 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4503 | #Param src source Rect of image to draw from ## |
| 4504 | #Param dst destination Rect of image to draw to ## |
| 4505 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4506 | and so on; or nullptr |
| 4507 | ## |
| 4508 | #Param constraint filter strictly within src or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4509 | |
| 4510 | #Example |
| 4511 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4512 | void draw(SkCanvas* canvas) { |
| 4513 | uint8_t pixels[][8] = { { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00}, |
| 4514 | { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}, |
| 4515 | { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00}, |
| 4516 | { 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF}, |
| 4517 | { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 4518 | { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00}, |
| 4519 | { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00}, |
| 4520 | { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00} }; |
| 4521 | SkBitmap bitmap; |
| 4522 | bitmap.installPixels(SkImageInfo::MakeA8(8, 8), |
| 4523 | (void*) pixels, sizeof(pixels[0])); |
| 4524 | SkPaint paint; |
| 4525 | paint.setMaskFilter(SkBlurMaskFilter::Make(kSolid_SkBlurStyle, 6)); |
| 4526 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xFF007F00} ) { |
| 4527 | paint.setColor(color); |
| 4528 | canvas->drawBitmapRect(bitmap, SkRect::MakeWH(8, 8), SkRect::MakeWH(32, 32), &paint); |
| 4529 | canvas->translate(48, 0); |
| 4530 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4531 | } |
| 4532 | ## |
| 4533 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4534 | #SeeAlso drawImageRect drawBitmap drawBitmapLattice drawBitmapNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4535 | |
| 4536 | ## |
| 4537 | |
| 4538 | # ------------------------------------------------------------------------------ |
| 4539 | |
| 4540 | #Method void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst, |
| 4541 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4542 | |
| 4543 | Draw IRect isrc of Bitmap bitmap, scaled and translated to fill Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4544 | isrc is on integer pixel boundaries; dst may include fractional boundaries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4545 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4546 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4547 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4548 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4549 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4550 | |
| 4551 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4552 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4553 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4554 | outside of its bounds. |
| 4555 | |
| 4556 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4557 | sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4558 | improve performance. |
| 4559 | |
| 4560 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4561 | #Param isrc source IRect of image to draw from ## |
| 4562 | #Param dst destination Rect of image to draw to ## |
| 4563 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4564 | and so on; or nullptr |
| 4565 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4566 | #Param constraint sample strictly within isrc, or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4567 | |
| 4568 | #Example |
| 4569 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4570 | void draw(SkCanvas* canvas) { |
| 4571 | uint8_t pixels[][8] = { { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}, |
| 4572 | { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00}, |
| 4573 | { 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF}, |
| 4574 | { 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF}, |
| 4575 | { 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF}, |
| 4576 | { 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF}, |
| 4577 | { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00}, |
| 4578 | { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00} }; |
| 4579 | SkBitmap bitmap; |
| 4580 | bitmap.installPixels(SkImageInfo::MakeA8(8, 8), |
| 4581 | (void*) pixels, sizeof(pixels[0])); |
| 4582 | SkPaint paint; |
| 4583 | paint.setFilterQuality(kHigh_SkFilterQuality); |
| 4584 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xFF007F00, 0xFF7f007f} ) { |
| 4585 | paint.setColor(color); |
| 4586 | canvas->drawBitmapRect(bitmap, SkIRect::MakeWH(8, 8), SkRect::MakeWH(32, 32), &paint); |
| 4587 | canvas->translate(48.25f, 0); |
| 4588 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4589 | } |
| 4590 | ## |
| 4591 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4592 | #SeeAlso drawImageRect drawBitmap drawBitmapLattice drawBitmapNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4593 | |
| 4594 | ## |
| 4595 | |
| 4596 | # ------------------------------------------------------------------------------ |
| 4597 | |
| 4598 | #Method void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint, |
| 4599 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4600 | |
| 4601 | Draw Bitmap bitmap, scaled and translated to fill Rect dst. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4602 | bitmap bounds is on integer pixel boundaries; dst may include fractional boundaries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4603 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4604 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4605 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4606 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4607 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4608 | |
| 4609 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4610 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4611 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4612 | outside of its bounds. |
| 4613 | |
| 4614 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4615 | sample within bitmap; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4616 | improve performance. |
| 4617 | |
| 4618 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4619 | #Param dst destination Rect of image to draw to ## |
| 4620 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4621 | and so on; or nullptr |
| 4622 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4623 | #Param constraint filter strictly within bitmap or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4624 | |
| 4625 | #Example |
| 4626 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4627 | void draw(SkCanvas* canvas) { |
| 4628 | uint32_t pixels[][2] = { { 0x00000000, 0x55550000}, |
| 4629 | { 0xAAAA0000, 0xFFFF0000} }; |
| 4630 | SkBitmap bitmap; |
| 4631 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4632 | (void*) pixels, sizeof(pixels[0])); |
| 4633 | SkPaint paint; |
| 4634 | canvas->scale(4, 4); |
| 4635 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN } ) { |
| 4636 | paint.setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kPlus)); |
| 4637 | canvas->drawBitmapRect(bitmap, SkRect::MakeWH(8, 8), &paint); |
| 4638 | canvas->translate(8, 0); |
| 4639 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4640 | } |
| 4641 | ## |
| 4642 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4643 | #SeeAlso drawImageRect drawBitmap drawBitmapLattice drawBitmapNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4644 | |
| 4645 | ## |
| 4646 | |
| 4647 | # ------------------------------------------------------------------------------ |
| 4648 | |
| 4649 | #Method void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 4650 | const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4651 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4652 | Draw Bitmap bitmap stretched proportionally to fit into Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4653 | IRect center divides the bitmap into nine sections: four sides, four corners, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4654 | and the center. Corners are not scaled, or scaled down proportionately if their |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4655 | sides are larger than dst; center and four sides are scaled to fit remaining |
| 4656 | space, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4657 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4658 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
| 4659 | |
| 4660 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4661 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4662 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4663 | |
| 4664 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4665 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4666 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4667 | outside of its bounds. |
| 4668 | |
| 4669 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4670 | #Param center IRect edge of image corners and sides ## |
| 4671 | #Param dst destination Rect of image to draw to ## |
| 4672 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4673 | and so on; or nullptr |
| 4674 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4675 | |
| 4676 | #Example |
| 4677 | #Height 128 |
| 4678 | #Description |
| 4679 | The two leftmost bitmap draws has four corners and sides to the left and right of center. |
| 4680 | The leftmost bitmap draw scales the width of corners proportionately to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4681 | The third and fourth draw corners are not scaled; the sides and center are scaled to |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4682 | fill the remaining space. |
| 4683 | The rightmost bitmap draw has four corners scaled vertically to fit, and uses sides above |
| 4684 | and below center to fill the remaining space. |
| 4685 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4686 | void draw(SkCanvas* canvas) { |
| 4687 | SkIRect center = { 20, 10, 50, 40 }; |
| 4688 | SkBitmap bitmap; |
| 4689 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4690 | SkCanvas bitCanvas(bitmap); |
| 4691 | SkPaint paint; |
| 4692 | SkColor gray = 0xFF000000; |
| 4693 | int left = 0; |
| 4694 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4695 | int top = 0; |
| 4696 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4697 | paint.setColor(gray); |
| 4698 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4699 | gray += 0x001f1f1f; |
| 4700 | top = bottom; |
| 4701 | } |
| 4702 | left = right; |
| 4703 | } |
| 4704 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4705 | canvas->drawBitmapNine(bitmap, center, SkRect::MakeWH(dest, 110 - dest), nullptr); |
| 4706 | canvas->translate(dest + 4, 0); |
| 4707 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4708 | } |
| 4709 | ## |
| 4710 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4711 | #SeeAlso drawImageNine drawBitmap drawBitmapLattice drawBitmapRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4712 | |
| 4713 | ## |
| 4714 | |
| 4715 | # ------------------------------------------------------------------------------ |
| 4716 | #Struct Lattice |
| 4717 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4718 | #Code |
| 4719 | struct Lattice { |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4720 | enum RectType ... |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4721 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4722 | const int* fXDivs; |
| 4723 | const int* fYDivs; |
| 4724 | const RectType* fRectTypes; |
| 4725 | int fXCount; |
| 4726 | int fYCount; |
| 4727 | const SkIRect* fBounds; |
| 4728 | const SkColor* fColors; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4729 | }; |
| 4730 | ## |
| 4731 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 4732 | Lattice divides Bitmap or Image into a rectangular grid. |
| 4733 | Grid entries on even columns and even rows are fixed; these entries are |
| 4734 | always drawn at their original size if the destination is large enough. |
| 4735 | If the destination side is too small to hold the fixed entries, all fixed |
| 4736 | entries are proportionately scaled down to fit. |
| 4737 | The grid entries not on even columns and rows are scaled to fit the |
| 4738 | remaining space, if any. |
| 4739 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4740 | #Enum RectType |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4741 | #Code |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4742 | enum RectType : uint8_t { |
| 4743 | kDefault = 0, |
| 4744 | kTransparent, |
| 4745 | kFixedColor, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4746 | }; |
| 4747 | ## |
| 4748 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4749 | Optional setting per rectangular grid entry to make it transparent, |
| 4750 | or to fill the grid entry with a color. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4751 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4752 | #Const kDefault 0 |
| 4753 | Draws Bitmap into lattice rectangle. |
| 4754 | ## |
| 4755 | |
| 4756 | #Const kTransparent 1 |
| 4757 | Skips lattice rectangle by making it transparent. |
| 4758 | ## |
| 4759 | |
| 4760 | #Const kFixedColor 2 |
| 4761 | Draws one of fColors into lattice rectangle. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4762 | ## |
| 4763 | ## |
| 4764 | |
| 4765 | #Member const int* fXDivs |
| 4766 | Array of x-coordinates that divide the bitmap vertically. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4767 | Array entries must be unique, increasing, greater than or equal to |
| 4768 | fBounds left edge, and less than fBounds right edge. |
| 4769 | Set the first element to fBounds left to collapse the left column of |
| 4770 | fixed grid entries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4771 | ## |
| 4772 | |
| 4773 | #Member const int* fYDivs |
| 4774 | Array of y-coordinates that divide the bitmap horizontally. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4775 | Array entries must be unique, increasing, greater than or equal to |
| 4776 | fBounds top edge, and less than fBounds bottom edge. |
| 4777 | Set the first element to fBounds top to collapse the top row of fixed |
| 4778 | grid entries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4779 | ## |
| 4780 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4781 | #Member const RectType* fRectTypes |
| 4782 | Optional array of fill types, one per rectangular grid entry: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4783 | array length must be |
| 4784 | #Formula |
| 4785 | (fXCount + 1) * (fYCount + 1) |
| 4786 | ## |
| 4787 | . |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 4788 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4789 | Each RectType is one of: kDefault, kTransparent, kFixedColor. |
| 4790 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4791 | Array entries correspond to the rectangular grid entries, ascending |
| 4792 | left to right and then top to bottom. |
| 4793 | ## |
| 4794 | |
| 4795 | #Member int fXCount |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4796 | Number of entries in fXDivs array; one less than the number of |
| 4797 | horizontal divisions. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4798 | ## |
| 4799 | |
| 4800 | #Member int fYCount |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4801 | Number of entries in fYDivs array; one less than the number of vertical |
| 4802 | divisions. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4803 | ## |
| 4804 | |
| 4805 | #Member const SkIRect* fBounds |
| 4806 | Optional subset IRect source to draw from. |
| 4807 | If nullptr, source bounds is dimensions of Bitmap or Image. |
| 4808 | ## |
| 4809 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4810 | #Member const SkColor* fColors |
| 4811 | Optional array of colors, one per rectangular grid entry. |
| 4812 | Array length must be |
| 4813 | #Formula |
| 4814 | (fXCount + 1) * (fYCount + 1) |
| 4815 | ## |
| 4816 | . |
| 4817 | |
| 4818 | Array entries correspond to the rectangular grid entries, ascending |
| 4819 | left to right, then top to bottom. |
| 4820 | ## |
| 4821 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4822 | #Struct Lattice ## |
| 4823 | |
| 4824 | #Method void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst, |
| 4825 | const SkPaint* paint = nullptr) |
| 4826 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4827 | Draw Bitmap bitmap stretched proportionally to fit into Rect dst. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4828 | |
| 4829 | Lattice lattice divides bitmap into a rectangular grid. |
| 4830 | Each intersection of an even-numbered row and column is fixed; like the corners |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4831 | of drawBitmapNine, fixed lattice elements never scale larger than their initial |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4832 | size and shrink proportionately when all fixed elements exceed the bitmap |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4833 | dimension. All other grid elements scale to fill the available space, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4834 | |
| 4835 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4836 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4837 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4838 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4839 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4840 | |
| 4841 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4842 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4843 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4844 | outside of its bounds. |
| 4845 | |
| 4846 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4847 | #Param lattice division of bitmap into fixed and variable rectangles ## |
| 4848 | #Param dst destination Rect of image to draw to ## |
| 4849 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4850 | and so on; or nullptr |
| 4851 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4852 | |
| 4853 | #Example |
| 4854 | #Height 128 |
| 4855 | #Description |
| 4856 | The two leftmost bitmap draws has four corners and sides to the left and right of center. |
| 4857 | The leftmost bitmap draw scales the width of corners proportionately to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4858 | The third and fourth draw corners are not scaled; the sides are scaled to |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4859 | fill the remaining space; the center is transparent. |
| 4860 | The rightmost bitmap draw has four corners scaled vertically to fit, and uses sides above |
| 4861 | and below center to fill the remaining space. |
| 4862 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4863 | void draw(SkCanvas* canvas) { |
| 4864 | SkIRect center = { 20, 10, 50, 40 }; |
| 4865 | SkBitmap bitmap; |
| 4866 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4867 | SkCanvas bitCanvas(bitmap); |
| 4868 | SkPaint paint; |
| 4869 | SkColor gray = 0xFF000000; |
| 4870 | int left = 0; |
| 4871 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4872 | int top = 0; |
| 4873 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4874 | paint.setColor(gray); |
| 4875 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4876 | gray += 0x001f1f1f; |
| 4877 | top = bottom; |
| 4878 | } |
| 4879 | left = right; |
| 4880 | } |
| 4881 | const int xDivs[] = { center.fLeft, center.fRight }; |
| 4882 | const int yDivs[] = { center.fTop, center.fBottom }; |
Cary Clark | ca3ebcd | 2017-12-12 11:22:38 -0500 | [diff] [blame] | 4883 | SkCanvas::Lattice::RectType fillTypes[3][3]; |
| 4884 | memset(fillTypes, 0, sizeof(fillTypes)); |
| 4885 | fillTypes[1][1] = SkCanvas::Lattice::kTransparent; |
| 4886 | SkColor dummy[9]; // temporary pending bug fix |
| 4887 | SkCanvas::Lattice lattice = { xDivs, yDivs, fillTypes[0], SK_ARRAY_COUNT(xDivs), |
| 4888 | SK_ARRAY_COUNT(yDivs), nullptr, dummy }; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4889 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
Cary Clark | ca3ebcd | 2017-12-12 11:22:38 -0500 | [diff] [blame] | 4890 | canvas->drawBitmapLattice(bitmap, lattice, SkRect::MakeWH(dest, 110 - dest), nullptr); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4891 | canvas->translate(dest + 4, 0); |
| 4892 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4893 | } |
| 4894 | ## |
| 4895 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4896 | #SeeAlso drawImageLattice drawBitmap drawBitmapNine Lattice |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4897 | |
| 4898 | ## |
| 4899 | |
| 4900 | # ------------------------------------------------------------------------------ |
| 4901 | |
| 4902 | #Method void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 4903 | const SkPaint* paint = nullptr) |
| 4904 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4905 | Draw Image image stretched proportionally to fit into Rect dst. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4906 | |
| 4907 | Lattice lattice divides image into a rectangular grid. |
| 4908 | Each intersection of an even-numbered row and column is fixed; like the corners |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4909 | of drawBitmapNine, fixed lattice elements never scale larger than their initial |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4910 | size and shrink proportionately when all fixed elements exceed the bitmap |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4911 | dimension. All other grid elements scale to fill the available space, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4912 | |
| 4913 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4914 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4915 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4916 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4917 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4918 | |
| 4919 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4920 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4921 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4922 | outside of its bounds. |
| 4923 | |
| 4924 | #Param image Image containing pixels, dimensions, and format ## |
| 4925 | #Param lattice division of bitmap into fixed and variable rectangles ## |
| 4926 | #Param dst destination Rect of image to draw to ## |
| 4927 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4928 | and so on; or nullptr |
| 4929 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4930 | |
| 4931 | #Example |
| 4932 | #Height 128 |
| 4933 | #Description |
| 4934 | The leftmost image is smaller than center; only corners are drawn, all scaled to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4935 | The second image equals the size of center; only corners are drawn without scaling. |
| 4936 | The remaining images are larger than center. All corners draw without scaling. The sides |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4937 | are scaled if needed to take up the remaining space; the center is transparent. |
| 4938 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4939 | void draw(SkCanvas* canvas) { |
| 4940 | SkIRect center = { 20, 10, 50, 40 }; |
| 4941 | SkBitmap bitmap; |
| 4942 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4943 | SkCanvas bitCanvas(bitmap); |
| 4944 | SkPaint paint; |
| 4945 | SkColor gray = 0xFF000000; |
| 4946 | int left = 0; |
| 4947 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4948 | int top = 0; |
| 4949 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4950 | paint.setColor(gray); |
| 4951 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4952 | gray += 0x001f1f1f; |
| 4953 | top = bottom; |
| 4954 | } |
| 4955 | left = right; |
| 4956 | } |
| 4957 | const int xDivs[] = { center.fLeft, center.fRight }; |
| 4958 | const int yDivs[] = { center.fTop, center.fBottom }; |
Cary Clark | ca3ebcd | 2017-12-12 11:22:38 -0500 | [diff] [blame] | 4959 | SkCanvas::Lattice::RectType fillTypes[3][3]; |
| 4960 | memset(fillTypes, 0, sizeof(fillTypes)); |
| 4961 | fillTypes[1][1] = SkCanvas::Lattice::kTransparent; |
| 4962 | SkColor dummy[9]; // temporary pending bug fix |
| 4963 | SkCanvas::Lattice lattice = { xDivs, yDivs, fillTypes[0], SK_ARRAY_COUNT(xDivs), |
| 4964 | SK_ARRAY_COUNT(yDivs), nullptr, dummy }; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4965 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4966 | SkImage* imagePtr = image.get(); |
| 4967 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4968 | canvas->drawImageNine(imagePtr, center, SkRect::MakeWH(dest, dest), nullptr); |
| 4969 | canvas->translate(dest + 4, 0); |
| 4970 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4971 | } |
| 4972 | ## |
| 4973 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4974 | #SeeAlso drawBitmapLattice drawImage drawImageNine Lattice |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4975 | |
| 4976 | ## |
| 4977 | |
| 4978 | #Topic Draw_Image ## |
| 4979 | |
| 4980 | # ------------------------------------------------------------------------------ |
| 4981 | |
| 4982 | #Method void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 4983 | const SkPaint& paint) |
| 4984 | |
| 4985 | Draw text, with origin at (x, y), using Clip, Matrix, and Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4986 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4987 | text meaning depends on Paint_Text_Encoding; by default, text is encoded as |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4988 | UTF-8. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4989 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4990 | x and y meaning depends on Paint_Text_Align and Paint_Vertical_Text; by default |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4991 | text draws left to right, positioning the first glyph left side bearing at x |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4992 | and its baseline at y. Text size is affected by Matrix and Paint_Text_Size. |
| 4993 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 4994 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4995 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4996 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4997 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4998 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4999 | #Param byteLength byte length of text array ## |
| 5000 | #Param x start of text on x-axis ## |
| 5001 | #Param y start of text on y-axis ## |
| 5002 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5003 | |
| 5004 | #Example |
| 5005 | #Height 200 |
| 5006 | #Description |
| 5007 | The same text is drawn varying Paint_Text_Size and varying |
| 5008 | Matrix. |
| 5009 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5010 | void draw(SkCanvas* canvas) { |
| 5011 | SkPaint paint; |
| 5012 | paint.setAntiAlias(true); |
| 5013 | float textSizes[] = { 12, 18, 24, 36 }; |
| 5014 | for (auto size: textSizes ) { |
| 5015 | paint.setTextSize(size); |
| 5016 | canvas->drawText("Aa", 2, 10, 20, paint); |
| 5017 | canvas->translate(0, size * 2); |
| 5018 | } |
| 5019 | paint.reset(); |
| 5020 | paint.setAntiAlias(true); |
| 5021 | float yPos = 20; |
| 5022 | for (auto size: textSizes ) { |
| 5023 | float scale = size / 12.f; |
| 5024 | canvas->resetMatrix(); |
| 5025 | canvas->translate(100, 0); |
| 5026 | canvas->scale(scale, scale); |
| 5027 | canvas->drawText("Aa", 2, 10 / scale, yPos / scale, paint); |
| 5028 | yPos += size * 2; |
| 5029 | } |
| 5030 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5031 | ## |
| 5032 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5033 | #SeeAlso drawString drawPosText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5034 | |
| 5035 | ## |
| 5036 | |
| 5037 | #Method void drawString(const char* string, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5038 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5039 | Draw null terminated string, with origin at (x, y), using Clip, Matrix, and |
| 5040 | Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5041 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5042 | string meaning depends on Paint_Text_Encoding; by default, strings are encoded |
| 5043 | as UTF-8. Other values of Paint_Text_Encoding are unlikely to produce the desired |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5044 | results, since zero bytes may be embedded in the string. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5045 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5046 | x and y meaning depends on Paint_Text_Align and Paint_Vertical_Text; by default |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5047 | string draws left to right, positioning the first glyph left side bearing at x |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5048 | and its baseline at y. Text size is affected by Matrix and Paint_Text_Size. |
| 5049 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5050 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5051 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5052 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5053 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5054 | #Param string character code points or Glyphs drawn, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5055 | ending with a char value of zero |
| 5056 | ## |
| 5057 | #Param x start of string on x-axis ## |
| 5058 | #Param y start of string on y-axis ## |
| 5059 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5060 | |
| 5061 | #Example |
| 5062 | SkPaint paint; |
| 5063 | canvas->drawString("a small hello", 20, 20, paint); |
| 5064 | ## |
| 5065 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5066 | #SeeAlso drawText drawPosText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5067 | |
| 5068 | ## |
| 5069 | |
| 5070 | #Method void drawString(const SkString& string, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5071 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5072 | Draw null terminated string, with origin at (x, y), using Clip, Matrix, and |
| 5073 | Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5074 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5075 | string meaning depends on Paint_Text_Encoding; by default, strings are encoded |
| 5076 | as UTF-8. Other values of Paint_Text_Encoding are unlikely to produce the desired |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5077 | results, since zero bytes may be embedded in the string. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5078 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5079 | x and y meaning depends on Paint_Text_Align and Paint_Vertical_Text; by default |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5080 | string draws left to right, positioning the first glyph left side bearing at x |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5081 | and its baseline at y. Text size is affected by Matrix and Paint_Text_Size. |
| 5082 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5083 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5084 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5085 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5086 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5087 | #Param string character code points or Glyphs drawn, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5088 | ending with a char value of zero |
| 5089 | ## |
| 5090 | #Param x start of string on x-axis ## |
| 5091 | #Param y start of string on y-axis ## |
| 5092 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5093 | |
| 5094 | #Example |
| 5095 | SkPaint paint; |
| 5096 | SkString string("a small hello"); |
| 5097 | canvas->drawString(string, 20, 20, paint); |
| 5098 | ## |
| 5099 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5100 | #SeeAlso drawText drawPosText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5101 | |
| 5102 | ## |
| 5103 | |
| 5104 | # ------------------------------------------------------------------------------ |
| 5105 | |
| 5106 | #Method void drawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 5107 | const SkPaint& paint) |
| 5108 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5109 | Draw each glyph in text with the origin in pos array, using Clip, Matrix, and |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5110 | Paint paint. The number of entries in pos array must match the number of Glyphs |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5111 | described by byteLength of text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5112 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5113 | text meaning depends on Paint_Text_Encoding; by default, text is encoded as |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5114 | UTF-8. pos elements' meaning depends on Paint_Text_Align and Paint_Vertical_Text; |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5115 | by default each glyph left side bearing is positioned at x and its |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5116 | baseline is positioned at y. Text size is affected by Matrix and |
| 5117 | Paint_Text_Size. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5118 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5119 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5120 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5121 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5122 | |
| 5123 | Layout engines such as Harfbuzz typically position each glyph |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5124 | rather than using the font advance widths. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5125 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5126 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5127 | #Param byteLength byte length of text array ## |
| 5128 | #Param pos array of glyph origins ## |
| 5129 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5130 | |
| 5131 | #Example |
| 5132 | #Height 120 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5133 | void draw(SkCanvas* canvas) { |
| 5134 | const char hello[] = "HeLLo!"; |
| 5135 | const SkPoint pos[] = { {40, 100}, {82, 95}, {115, 110}, {130, 95}, {145, 85}, |
| 5136 | {172, 100} }; |
| 5137 | SkPaint paint; |
| 5138 | paint.setTextSize(60); |
| 5139 | canvas->drawPosText(hello, strlen(hello), pos, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5140 | } |
| 5141 | ## |
| 5142 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5143 | #SeeAlso drawText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5144 | |
| 5145 | ## |
| 5146 | |
| 5147 | # ------------------------------------------------------------------------------ |
| 5148 | |
| 5149 | #Method void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY, |
| 5150 | const SkPaint& paint) |
| 5151 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5152 | Draw each glyph in text with its (x, y) origin composed from xpos array and |
| 5153 | constY, using Clip, Matrix, and Paint paint. The number of entries in xpos array |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5154 | must match the number of Glyphs described by byteLength of text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5155 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5156 | text meaning depends on Paint_Text_Encoding; by default, text is encoded as |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5157 | UTF-8. xpos elements' meaning depends on Paint_Text_Align and Paint_Vertical_Text; |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5158 | by default each glyph left side bearing is positioned at an xpos element and |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5159 | its baseline is positioned at constY. Text size is affected by Matrix and |
| 5160 | Paint_Text_Size. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5161 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5162 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5163 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5164 | filled 12 point black Glyphs. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5165 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5166 | Layout engines such as Harfbuzz typically position each glyph |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5167 | rather than using the font advance widths if all Glyphs share the same |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5168 | baseline. |
| 5169 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5170 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5171 | #Param byteLength byte length of text array ## |
| 5172 | #Param xpos array of x positions, used to position each glyph ## |
| 5173 | #Param constY shared y coordinate for all of x positions ## |
| 5174 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5175 | |
| 5176 | #Example |
| 5177 | #Height 40 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5178 | void draw(SkCanvas* canvas) { |
| 5179 | SkScalar xpos[] = { 20, 40, 80, 160 }; |
| 5180 | SkPaint paint; |
| 5181 | canvas->drawPosTextH("XXXX", 4, xpos, 20, paint); |
| 5182 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5183 | ## |
| 5184 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5185 | #SeeAlso drawText drawPosText drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5186 | |
| 5187 | ## |
| 5188 | |
| 5189 | # ------------------------------------------------------------------------------ |
| 5190 | |
| 5191 | #Method void drawTextOnPathHV(const void* text, size_t byteLength, const SkPath& path, SkScalar hOffset, |
| 5192 | SkScalar vOffset, const SkPaint& paint) |
| 5193 | |
| 5194 | Draw text on Path path, using Clip, Matrix, and Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5195 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5196 | Origin of text is at distance hOffset along the path, offset by a perpendicular |
| 5197 | vector of length vOffset. If the path section corresponding the glyph advance is |
| 5198 | curved, the glyph is drawn curved to match; control points in the glyph are |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5199 | mapped to projected points parallel to the path. If the text advance is larger |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5200 | than the path length, the excess text is clipped. |
| 5201 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5202 | text meaning depends on Paint_Text_Encoding; by default, text is encoded as |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5203 | UTF-8. Origin meaning depends on Paint_Text_Align and Paint_Vertical_Text; by |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5204 | default text positions the first glyph left side bearing at origin x and its |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5205 | baseline at origin y. Text size is affected by Matrix and Paint_Text_Size. |
| 5206 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5207 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5208 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5209 | filled 12 point black Glyphs. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5210 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5211 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5212 | #Param byteLength byte length of text array ## |
| 5213 | #Param path Path providing text baseline ## |
| 5214 | #Param hOffset distance along path to offset origin ## |
| 5215 | #Param vOffset offset of text above (if negative) or below (if positive) the path ## |
| 5216 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5217 | |
| 5218 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5219 | void draw(SkCanvas* canvas) { |
| 5220 | const char aero[] = "correo a" "\xC3" "\xA9" "reo"; |
| 5221 | const size_t len = sizeof(aero) - 1; |
| 5222 | SkPath path; |
| 5223 | path.addOval({43-26, 43-26, 43+26, 43+26}, SkPath::kCW_Direction, 3); |
| 5224 | SkPaint paint; |
| 5225 | paint.setTextSize(24); |
| 5226 | for (auto offset : { 0, 10, 20 } ) { |
| 5227 | canvas->drawTextOnPathHV(aero, len, path, 0, -offset, paint); |
| 5228 | canvas->translate(70 + offset, 70 + offset); |
| 5229 | } |
| 5230 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5231 | ## |
| 5232 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5233 | #SeeAlso drawTextOnPath drawText drawPosTextH drawTextBlob drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5234 | |
| 5235 | ## |
| 5236 | |
| 5237 | # ------------------------------------------------------------------------------ |
| 5238 | |
| 5239 | #Method void drawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 5240 | const SkMatrix* matrix, const SkPaint& paint) |
| 5241 | |
| 5242 | Draw text on Path path, using Clip, Matrix, and Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5243 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5244 | Origin of text is at beginning of path offset by matrix, if provided, before it |
| 5245 | is mapped to path. If the path section corresponding the glyph advance is |
| 5246 | curved, the glyph is drawn curved to match; control points in the glyph are |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5247 | mapped to projected points parallel to the path. If the text advance is larger |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5248 | than the path length, the excess text is clipped. |
| 5249 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5250 | text meaning depends on Paint_Text_Encoding; by default, text is encoded as |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5251 | UTF-8. Origin meaning depends on Paint_Text_Align and Paint_Vertical_Text; by |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5252 | default text positions the first glyph left side bearing at origin x and its |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5253 | baseline at origin y. Text size is affected by Matrix and Paint_Text_Size. |
| 5254 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5255 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5256 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5257 | filled 12 point black Glyphs. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5258 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5259 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5260 | #Param byteLength byte length of text array ## |
| 5261 | #Param path Path providing text baseline ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5262 | #Param matrix transform of Glyphs before mapping to path; may be nullptr |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 5263 | to use identity Matrix |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5264 | ## |
| 5265 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5266 | |
| 5267 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5268 | void draw(SkCanvas* canvas) { |
| 5269 | const char roller[] = "rollercoaster"; |
| 5270 | const size_t len = sizeof(roller) - 1; |
| 5271 | SkPath path; |
| 5272 | path.cubicTo(40, -80, 120, 80, 160, -40); |
| 5273 | SkPaint paint; |
| 5274 | paint.setTextSize(32); |
| 5275 | paint.setStyle(SkPaint::kStroke_Style); |
| 5276 | SkMatrix matrix; |
| 5277 | matrix.setIdentity(); |
| 5278 | for (int i = 0; i < 3; ++i) { |
| 5279 | canvas->translate(25, 60); |
| 5280 | canvas->drawPath(path, paint); |
| 5281 | canvas->drawTextOnPath(roller, len, path, &matrix, paint); |
| 5282 | matrix.preTranslate(0, 10); |
| 5283 | } |
| 5284 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5285 | ## |
| 5286 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5287 | #SeeAlso drawTextOnPathHV drawText drawPosTextH drawTextBlob drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5288 | |
| 5289 | ## |
| 5290 | |
| 5291 | # ------------------------------------------------------------------------------ |
| 5292 | |
| 5293 | #Method void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], |
| 5294 | const SkRect* cullRect, const SkPaint& paint) |
| 5295 | |
| 5296 | Draw text, transforming each glyph by the corresponding SkRSXform, |
| 5297 | using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5298 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5299 | RSXform array specifies a separate square scale, rotation, and translation for |
| 5300 | each glyph. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5301 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5302 | Optional Rect cullRect is a conservative bounds of text, taking into account |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5303 | RSXform and paint. If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5304 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5305 | All elements of paint: Path_Effect, Mask_Filter, Shader, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5306 | Color_Filter, Image_Filter, and Draw_Looper; apply to text. By default, draws |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5307 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5308 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5309 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5310 | #Param byteLength byte length of text array ## |
| 5311 | #Param xform RSXform rotates, scales, and translates each glyph individually ## |
| 5312 | #Param cullRect Rect bounds of text for efficient clipping; or nullptr ## |
| 5313 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5314 | |
| 5315 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5316 | void draw(SkCanvas* canvas) { |
| 5317 | const int iterations = 26; |
| 5318 | SkRSXform transforms[iterations]; |
| 5319 | char alphabet[iterations]; |
| 5320 | SkScalar angle = 0; |
| 5321 | SkScalar scale = 1; |
| 5322 | for (size_t i = 0; i < SK_ARRAY_COUNT(transforms); ++i) { |
| 5323 | const SkScalar s = SkScalarSin(angle) * scale; |
| 5324 | const SkScalar c = SkScalarCos(angle) * scale; |
| 5325 | transforms[i] = SkRSXform::Make(-c, -s, -s * 16, c * 16); |
| 5326 | angle += .45; |
| 5327 | scale += .2; |
| 5328 | alphabet[i] = 'A' + i; |
| 5329 | } |
| 5330 | SkPaint paint; |
| 5331 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 5332 | canvas->translate(110, 138); |
| 5333 | canvas->drawTextRSXform(alphabet, sizeof(alphabet), transforms, nullptr, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5334 | } |
| 5335 | ## |
| 5336 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5337 | #SeeAlso drawTextOnPath drawTextOnPathHV drawText drawPosText drawTextBlob |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5338 | |
| 5339 | ## |
| 5340 | |
| 5341 | # ------------------------------------------------------------------------------ |
| 5342 | |
| 5343 | #Method void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5344 | |
| 5345 | Draw Text_Blob blob at (x, y), using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5346 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5347 | blob contains Glyphs, their positions, and paint attributes specific to text: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5348 | Typeface, Paint_Text_Size, Paint_Text_Scale_X, Paint_Text_Skew_X, |
| 5349 | Paint_Text_Align, Paint_Hinting, Anti-alias, Paint_Fake_Bold, |
| 5350 | Font_Embedded_Bitmaps, Full_Hinting_Spacing, LCD_Text, Linear_Text, |
| 5351 | Subpixel_Text, and Paint_Vertical_Text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5352 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 5353 | Paint_Text_Encoding must be set to SkPaint::kGlyphID_TextEncoding. |
| 5354 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5355 | Elements of paint: Path_Effect, Mask_Filter, Shader, Color_Filter, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5356 | Image_Filter, and Draw_Looper; apply to blob. |
| 5357 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5358 | #Param blob Glyphs, positions, and their paints' text size, typeface, and so on ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5359 | #Param x horizontal offset applied to blob ## |
| 5360 | #Param y vertical offset applied to blob ## |
| 5361 | #Param paint blend, color, stroking, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5362 | |
| 5363 | #Example |
| 5364 | #Height 120 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5365 | void draw(SkCanvas* canvas) { |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 5366 | SkTextBlobBuilder textBlobBuilder; |
| 5367 | const char bunny[] = "/(^x^)\\"; |
| 5368 | const int len = sizeof(bunny) - 1; |
| 5369 | uint16_t glyphs[len]; |
| 5370 | SkPaint paint; |
| 5371 | paint.textToGlyphs(bunny, len, glyphs); |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 5372 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 5373 | int runs[] = { 3, 1, 3 }; |
| 5374 | SkPoint textPos = { 20, 100 }; |
| 5375 | int glyphIndex = 0; |
| 5376 | for (auto runLen : runs) { |
| 5377 | paint.setTextSize(1 == runLen ? 20 : 50); |
| 5378 | const SkTextBlobBuilder::RunBuffer& run = |
| 5379 | textBlobBuilder.allocRun(paint, runLen, textPos.fX, textPos.fY); |
| 5380 | memcpy(run.glyphs, &glyphs[glyphIndex], sizeof(glyphs[0]) * runLen); |
| 5381 | textPos.fX += paint.measureText(&glyphs[glyphIndex], sizeof(glyphs[0]) * runLen, nullptr); |
| 5382 | glyphIndex += runLen; |
| 5383 | } |
| 5384 | sk_sp<const SkTextBlob> blob = textBlobBuilder.make(); |
| 5385 | paint.reset(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5386 | canvas->drawTextBlob(blob.get(), 0, 0, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5387 | } |
| 5388 | ## |
| 5389 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5390 | #SeeAlso drawText drawPosText drawPosTextH |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5391 | |
| 5392 | ## |
| 5393 | |
| 5394 | # ------------------------------------------------------------------------------ |
| 5395 | |
| 5396 | #Method void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5397 | |
| 5398 | Draw Text_Blob blob at (x, y), using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5399 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5400 | blob contains Glyphs, their positions, and paint attributes specific to text: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5401 | Typeface, Paint_Text_Size, Paint_Text_Scale_X, Paint_Text_Skew_X, |
| 5402 | Paint_Text_Align, Paint_Hinting, Anti-alias, Paint_Fake_Bold, |
| 5403 | Font_Embedded_Bitmaps, Full_Hinting_Spacing, LCD_Text, Linear_Text, |
| 5404 | Subpixel_Text, and Paint_Vertical_Text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5405 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 5406 | Paint_Text_Encoding must be set to SkPaint::kGlyphID_TextEncoding. |
| 5407 | |
Mike Reed | 8ad91a9 | 2018-01-19 19:09:32 -0500 | [diff] [blame] | 5408 | Elements of paint: Path_Effect, Mask_Filter, Shader, Color_Filter, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5409 | Image_Filter, and Draw_Looper; apply to blob. |
| 5410 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5411 | #Param blob Glyphs, positions, and their paints' text size, typeface, and so on ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5412 | #Param x horizontal offset applied to blob ## |
| 5413 | #Param y vertical offset applied to blob ## |
| 5414 | #Param paint blend, color, stroking, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5415 | |
| 5416 | #Example |
| 5417 | #Height 120 |
| 5418 | #Description |
| 5419 | Paint attributes unrelated to text, like color, have no effect on paint in allocated Text_Blob. |
| 5420 | Paint attributes related to text, like text size, have no effect on paint passed to drawTextBlob. |
| 5421 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5422 | void draw(SkCanvas* canvas) { |
| 5423 | SkTextBlobBuilder textBlobBuilder; |
| 5424 | SkPaint paint; |
| 5425 | paint.setTextSize(50); |
| 5426 | paint.setColor(SK_ColorRED); |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 5427 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5428 | const SkTextBlobBuilder::RunBuffer& run = |
| 5429 | textBlobBuilder.allocRun(paint, 1, 20, 100); |
| 5430 | run.glyphs[0] = 20; |
| 5431 | sk_sp<const SkTextBlob> blob = textBlobBuilder.make(); |
| 5432 | paint.setTextSize(10); |
| 5433 | paint.setColor(SK_ColorBLUE); |
| 5434 | canvas->drawTextBlob(blob.get(), 0, 0, paint); |
| 5435 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5436 | ## |
| 5437 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5438 | #SeeAlso drawText drawPosText drawPosTextH |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5439 | |
| 5440 | ## |
| 5441 | |
| 5442 | # ------------------------------------------------------------------------------ |
| 5443 | |
| 5444 | #Method void drawPicture(const SkPicture* picture) |
| 5445 | |
| 5446 | Draw Picture picture, using Clip and Matrix. |
| 5447 | Clip and Matrix are unchanged by picture contents, as if |
| 5448 | save() was called before and restore() was called after drawPicture. |
| 5449 | |
| 5450 | Picture records a series of draw commands for later playback. |
| 5451 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5452 | #Param picture recorded drawing commands to play ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5453 | |
| 5454 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5455 | void draw(SkCanvas* canvas) { |
| 5456 | SkPictureRecorder recorder; |
| 5457 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5458 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5459 | SkPaint paint; |
| 5460 | paint.setColor(color); |
| 5461 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5462 | recordingCanvas->translate(10, 10); |
| 5463 | recordingCanvas->scale(1.2f, 1.4f); |
| 5464 | } |
| 5465 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5466 | const SkPicture* playbackPtr = playback.get(); |
| 5467 | canvas->drawPicture(playback); |
| 5468 | canvas->scale(2, 2); |
| 5469 | canvas->translate(50, 0); |
| 5470 | canvas->drawPicture(playback); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5471 | } |
| 5472 | ## |
| 5473 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5474 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5475 | |
| 5476 | ## |
| 5477 | |
| 5478 | # ------------------------------------------------------------------------------ |
| 5479 | |
| 5480 | #Method void drawPicture(const sk_sp<SkPicture>& picture) |
| 5481 | |
| 5482 | Draw Picture picture, using Clip and Matrix. |
| 5483 | Clip and Matrix are unchanged by picture contents, as if |
| 5484 | save() was called before and restore() was called after drawPicture. |
| 5485 | |
| 5486 | Picture records a series of draw commands for later playback. |
| 5487 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5488 | #Param picture recorded drawing commands to play ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5489 | |
| 5490 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5491 | void draw(SkCanvas* canvas) { |
| 5492 | SkPictureRecorder recorder; |
| 5493 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5494 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5495 | SkPaint paint; |
| 5496 | paint.setColor(color); |
| 5497 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5498 | recordingCanvas->translate(10, 10); |
| 5499 | recordingCanvas->scale(1.2f, 1.4f); |
| 5500 | } |
| 5501 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5502 | canvas->drawPicture(playback); |
| 5503 | canvas->scale(2, 2); |
| 5504 | canvas->translate(50, 0); |
| 5505 | canvas->drawPicture(playback); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5506 | } |
| 5507 | ## |
| 5508 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5509 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5510 | |
| 5511 | ## |
| 5512 | |
| 5513 | # ------------------------------------------------------------------------------ |
| 5514 | |
| 5515 | #Method void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) |
| 5516 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5517 | Draw Picture picture, using Clip and Matrix; transforming picture with |
| 5518 | Matrix matrix, if provided; and use Paint paint Color_Alpha, Color_Filter, |
| 5519 | Image_Filter, and Blend_Mode, if provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5520 | |
| 5521 | matrix transformation is equivalent to: save(), concat(), drawPicture, restore(). |
| 5522 | paint use is equivalent to: saveLayer, drawPicture, restore(). |
| 5523 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5524 | #Param picture recorded drawing commands to play ## |
| 5525 | #Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ## |
| 5526 | #Param paint Paint to apply transparency, filtering, and so on; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5527 | |
| 5528 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5529 | void draw(SkCanvas* canvas) { |
| 5530 | SkPaint paint; |
| 5531 | SkPictureRecorder recorder; |
| 5532 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5533 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5534 | paint.setColor(color); |
| 5535 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5536 | recordingCanvas->translate(10, 10); |
| 5537 | recordingCanvas->scale(1.2f, 1.4f); |
| 5538 | } |
| 5539 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5540 | const SkPicture* playbackPtr = playback.get(); |
| 5541 | SkMatrix matrix; |
| 5542 | matrix.reset(); |
| 5543 | for (auto alpha : { 70, 140, 210 } ) { |
| 5544 | paint.setAlpha(alpha); |
| 5545 | canvas->drawPicture(playbackPtr, &matrix, &paint); |
| 5546 | matrix.preTranslate(70, 70); |
| 5547 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5548 | } |
| 5549 | ## |
| 5550 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5551 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5552 | |
| 5553 | ## |
| 5554 | |
| 5555 | # ------------------------------------------------------------------------------ |
| 5556 | |
| 5557 | #Method void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, const SkPaint* paint) |
| 5558 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5559 | Draw Picture picture, using Clip and Matrix; transforming picture with |
| 5560 | Matrix matrix, if provided; and use Paint paint Color_Alpha, Color_Filter, |
| 5561 | Image_Filter, and Blend_Mode, if provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5562 | |
| 5563 | matrix transformation is equivalent to: save(), concat(), drawPicture, restore(). |
| 5564 | paint use is equivalent to: saveLayer, drawPicture, restore(). |
| 5565 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5566 | #Param picture recorded drawing commands to play ## |
| 5567 | #Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ## |
| 5568 | #Param paint Paint to apply transparency, filtering, and so on; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5569 | |
| 5570 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5571 | void draw(SkCanvas* canvas) { |
| 5572 | SkPaint paint; |
| 5573 | SkPictureRecorder recorder; |
| 5574 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5575 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5576 | paint.setColor(color); |
| 5577 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5578 | recordingCanvas->translate(10, 10); |
| 5579 | recordingCanvas->scale(1.2f, 1.4f); |
| 5580 | } |
| 5581 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5582 | SkMatrix matrix; |
| 5583 | matrix.reset(); |
| 5584 | for (auto alpha : { 70, 140, 210 } ) { |
| 5585 | paint.setAlpha(alpha); |
| 5586 | canvas->drawPicture(playback, &matrix, &paint); |
| 5587 | matrix.preTranslate(70, 70); |
| 5588 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5589 | } |
| 5590 | ## |
| 5591 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5592 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5593 | |
| 5594 | ## |
| 5595 | |
| 5596 | # ------------------------------------------------------------------------------ |
| 5597 | |
| 5598 | #Method void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) |
| 5599 | |
| 5600 | Draw Vertices vertices, a triangle mesh, using Clip and Matrix. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5601 | If Vertices_Texs and Vertices_Colors are defined in vertices, and Paint paint |
| 5602 | contains Shader, Blend_Mode mode combines Vertices_Colors with Shader. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5603 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5604 | #Param vertices triangle mesh to draw ## |
| 5605 | #Param mode combines Vertices_Colors with Shader, if both are present ## |
| 5606 | #Param paint specifies the Shader, used as Vertices texture; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5607 | |
| 5608 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5609 | void draw(SkCanvas* canvas) { |
| 5610 | SkPaint paint; |
| 5611 | SkPoint points[] = { { 0, 0 }, { 250, 0 }, { 100, 100 }, { 0, 250 } }; |
| 5612 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN }; |
| 5613 | auto vertices = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, |
| 5614 | SK_ARRAY_COUNT(points), points, nullptr, colors); |
| 5615 | canvas->drawVertices(vertices.get(), SkBlendMode::kSrc, paint); |
| 5616 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5617 | ## |
| 5618 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5619 | #SeeAlso drawPatch drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5620 | |
| 5621 | ## |
| 5622 | |
| 5623 | # ------------------------------------------------------------------------------ |
| 5624 | |
| 5625 | #Method void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint) |
| 5626 | |
| 5627 | Draw Vertices vertices, a triangle mesh, using Clip and Matrix. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5628 | If Vertices_Texs and Vertices_Colors are defined in vertices, and Paint paint |
| 5629 | contains Shader, Blend_Mode mode combines Vertices_Colors with Shader. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5630 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5631 | #Param vertices triangle mesh to draw ## |
| 5632 | #Param mode combines Vertices_Colors with Shader, if both are present ## |
| 5633 | #Param paint specifies the Shader, used as Vertices texture, may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5634 | |
| 5635 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5636 | void draw(SkCanvas* canvas) { |
| 5637 | SkPaint paint; |
| 5638 | SkPoint points[] = { { 0, 0 }, { 250, 0 }, { 100, 100 }, { 0, 250 } }; |
| 5639 | SkPoint texs[] = { { 0, 0 }, { 0, 250 }, { 250, 250 }, { 250, 0 } }; |
| 5640 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN }; |
| 5641 | paint.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 4, |
| 5642 | SkShader::kClamp_TileMode)); |
| 5643 | auto vertices = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, |
| 5644 | SK_ARRAY_COUNT(points), points, texs, colors); |
| 5645 | canvas->drawVertices(vertices.get(), SkBlendMode::kDarken, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5646 | } |
| 5647 | ## |
| 5648 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5649 | #SeeAlso drawPatch drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5650 | |
| 5651 | ## |
| 5652 | |
| 5653 | # ------------------------------------------------------------------------------ |
| 5654 | |
| 5655 | #Method void drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 5656 | const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint) |
| 5657 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5658 | Draws a Coons_Patch: the interpolation of four cubics with shared corners, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5659 | associating a color, and optionally a texture coordinate, with each corner. |
| 5660 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5661 | Coons_Patch uses Clip and Matrix, paint Shader, Color_Filter, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5662 | Color_Alpha, Image_Filter, and Blend_Mode. If Shader is provided it is treated |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5663 | as Coons_Patch texture; Blend_Mode mode combines Color colors and Shader if |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5664 | both are provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5665 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5666 | Point array cubics specifies four Cubics starting at the top-left corner, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5667 | in clockwise order, sharing every fourth point. The last Cubic ends at the |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5668 | first point. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5669 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5670 | Color array color associates colors with corners in top-left, top-right, |
| 5671 | bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5672 | |
| 5673 | If paint contains Shader, Point array texCoords maps Shader as texture to |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5674 | corners in top-left, top-right, bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5675 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 5676 | #Param cubics Path_Cubic array, sharing common points ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5677 | #Param colors Color array, one for each corner ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5678 | #Param texCoords Point array of texture coordinates, mapping Shader to corners; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5679 | may be nullptr |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 5680 | #Param ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5681 | #Param mode Blend_Mode for colors, and for Shader if paint has one ## |
| 5682 | #Param paint Shader, Color_Filter, Blend_Mode, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5683 | |
| 5684 | #Example |
| 5685 | #Image 5 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5686 | void draw(SkCanvas* canvas) { |
| 5687 | // SkBitmap source = cmbkygk; |
| 5688 | SkPaint paint; |
| 5689 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 5690 | paint.setAntiAlias(true); |
| 5691 | SkPoint cubics[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 5692 | /* { 7, 3 }, */ { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 5693 | /* { 5, 7 }, */ { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 5694 | /* { 1, 5 }, */ { 2, 4 }, { 1, 3 }, /* { 3, 1 } */ }; |
| 5695 | SkColor colors[] = { 0xbfff0000, 0xbf0000ff, 0xbfff00ff, 0xbf00ffff }; |
| 5696 | SkPoint texCoords[] = { { -30, -30 }, { 162, -30}, { 162, 162}, { -30, 162} }; |
| 5697 | paint.setShader(SkShader::MakeBitmapShader(source, SkShader::kClamp_TileMode, |
| 5698 | SkShader::kClamp_TileMode, nullptr)); |
| 5699 | canvas->scale(15, 15); |
| 5700 | for (auto blend : { SkBlendMode::kSrcOver, SkBlendMode::kModulate, SkBlendMode::kXor } ) { |
| 5701 | canvas->drawPatch(cubics, colors, texCoords, blend, paint); |
| 5702 | canvas->translate(4, 4); |
| 5703 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5704 | } |
| 5705 | ## |
| 5706 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5707 | #ToDo can patch use image filter? ## |
| 5708 | #SeeAlso SeeAlso drawVertices drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5709 | |
| 5710 | ## |
| 5711 | |
| 5712 | # ------------------------------------------------------------------------------ |
| 5713 | |
| 5714 | #Method void drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 5715 | const SkPoint texCoords[4], const SkPaint& paint) |
| 5716 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5717 | Draws Cubic Coons_Patch: the interpolation of four cubics with shared corners, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5718 | associating a color, and optionally a texture coordinate, with each corner. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5719 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5720 | Coons_Patch uses Clip and Matrix, paint Shader, Color_Filter, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5721 | Color_Alpha, Image_Filter, and Blend_Mode. If Shader is provided it is treated |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5722 | as Coons_Patch texture; Blend_Mode mode combines Color colors and Shader if |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5723 | both are provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5724 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5725 | Point array cubics specifies four Cubics starting at the top-left corner, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5726 | in clockwise order, sharing every fourth point. The last Cubic ends at the |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5727 | first point. |
| 5728 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5729 | Color array color associates colors with corners in top-left, top-right, |
| 5730 | bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5731 | |
| 5732 | If paint contains Shader, Point array texCoords maps Shader as texture to |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5733 | corners in top-left, top-right, bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5734 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 5735 | #Param cubics Path_Cubic array, sharing common points ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5736 | #Param colors Color array, one for each corner ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5737 | #Param texCoords Point array of texture coordinates, mapping Shader to corners; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5738 | may be nullptr |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 5739 | #Param ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5740 | #Param paint Shader, Color_Filter, Blend_Mode, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5741 | |
| 5742 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5743 | void draw(SkCanvas* canvas) { |
| 5744 | SkPaint paint; |
| 5745 | paint.setAntiAlias(true); |
| 5746 | SkPoint cubics[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 5747 | /* { 7, 3 }, */ { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 5748 | /* { 5, 7 }, */ { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 5749 | /* { 1, 5 }, */ { 2, 4 }, { 1, 3 }, /* { 3, 1 } */ }; |
| 5750 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN }; |
| 5751 | canvas->scale(30, 30); |
| 5752 | canvas->drawPatch(cubics, colors, nullptr, paint); |
| 5753 | SkPoint text[] = { {3,0.9f}, {4,2.5f}, {5,0.9f}, {7.5f,3.2f}, {5.5f,4.2f}, |
| 5754 | {7.5f,5.2f}, {5,7.5f}, {4,5.9f}, {3,7.5f}, {0.5f,5.2f}, {2.5f,4.2f}, |
| 5755 | {0.5f,3.2f} }; |
| 5756 | paint.setTextSize(18.f / 30); |
| 5757 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 5758 | for (int i = 0; i< 10; ++i) { |
| 5759 | char digit = '0' + i; |
| 5760 | canvas->drawText(&digit, 1, text[i].fX, text[i].fY, paint); |
| 5761 | } |
| 5762 | canvas->drawString("10", text[10].fX, text[10].fY, paint); |
| 5763 | canvas->drawString("11", text[11].fX, text[11].fY, paint); |
| 5764 | paint.setStyle(SkPaint::kStroke_Style); |
| 5765 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 12, cubics, paint); |
| 5766 | canvas->drawLine(cubics[11].fX, cubics[11].fY, cubics[0].fX, cubics[0].fY, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5767 | } |
| 5768 | ## |
| 5769 | |
| 5770 | #Example |
| 5771 | #Image 6 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5772 | void draw(SkCanvas* canvas) { |
| 5773 | // SkBitmap source = checkerboard; |
| 5774 | SkPaint paint; |
| 5775 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 5776 | paint.setAntiAlias(true); |
| 5777 | SkPoint cubics[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 5778 | /* { 7, 3 }, */ { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 5779 | /* { 5, 7 }, */ { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 5780 | /* { 1, 5 }, */ { 2, 4 }, { 1, 3 }, /* { 3, 1 } */ }; |
| 5781 | SkPoint texCoords[] = { { 0, 0 }, { 0, 62}, { 62, 62}, { 62, 0 } }; |
| 5782 | paint.setShader(SkShader::MakeBitmapShader(source, SkShader::kClamp_TileMode, |
| 5783 | SkShader::kClamp_TileMode, nullptr)); |
| 5784 | canvas->scale(30, 30); |
| 5785 | canvas->drawPatch(cubics, nullptr, texCoords, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5786 | } |
| 5787 | ## |
| 5788 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5789 | #ToDo can patch use image filter? ## |
| 5790 | #SeeAlso SeeAlso drawVertices drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5791 | |
| 5792 | ## |
| 5793 | |
| 5794 | # ------------------------------------------------------------------------------ |
| 5795 | |
| 5796 | #Method void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
| 5797 | const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect, |
| 5798 | const SkPaint* paint) |
| 5799 | |
| 5800 | Draw a set of sprites from atlas, using Clip, Matrix, and optional Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5801 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5802 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5803 | atlas, and RSXform xform transforms it into destination space. |
| 5804 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5805 | xform, text, and colors if present, must contain count entries. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5806 | Optional colors are applied for each sprite using Blend_Mode. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5807 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5808 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5809 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5810 | #Param atlas Image containing sprites ## |
| 5811 | #Param xform RSXform mappings for sprites in atlas ## |
| 5812 | #Param tex Rect locations of sprites in atlas ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5813 | #Param colors one per sprite, blended with sprite using Blend_Mode; may be nullptr ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5814 | #Param count number of sprites to draw ## |
| 5815 | #Param mode Blend_Mode combining colors and sprites ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5816 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5817 | #Param paint Color_Filter, Image_Filter, Blend_Mode, and so on; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5818 | |
| 5819 | #Example |
| 5820 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5821 | void draw(SkCanvas* canvas) { |
| 5822 | // SkBitmap source = mandrill; |
| 5823 | SkRSXform xforms[] = { { .5f, 0, 0, 0 }, {0, .5f, 200, 100 } }; |
| 5824 | SkRect tex[] = { { 0, 0, 250, 250 }, { 0, 0, 250, 250 } }; |
| 5825 | SkColor colors[] = { 0x7f55aa00, 0x7f3333bf }; |
| 5826 | const SkImage* imagePtr = image.get(); |
| 5827 | canvas->drawAtlas(imagePtr, xforms, tex, colors, 2, SkBlendMode::kSrcOver, nullptr, nullptr); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5828 | } |
| 5829 | ## |
| 5830 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5831 | #SeeAlso drawBitmap drawImage |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5832 | |
| 5833 | ## |
| 5834 | |
| 5835 | # ------------------------------------------------------------------------------ |
| 5836 | |
| 5837 | #Method void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[], |
| 5838 | const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect, |
| 5839 | const SkPaint* paint) |
| 5840 | |
| 5841 | Draw a set of sprites from atlas, using Clip, Matrix, and optional Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5842 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5843 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5844 | atlas, and RSXform xform transforms it into destination space. |
| 5845 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5846 | xform, text, and colors if present, must contain count entries. |
| 5847 | Optional colors is applied for each sprite using Blend_Mode. |
| 5848 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5849 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5850 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5851 | #Param atlas Image containing sprites ## |
| 5852 | #Param xform RSXform mappings for sprites in atlas ## |
| 5853 | #Param tex Rect locations of sprites in atlas ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5854 | #Param colors one per sprite, blended with sprite using Blend_Mode; may be nullptr ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5855 | #Param count number of sprites to draw ## |
| 5856 | #Param mode Blend_Mode combining colors and sprites ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5857 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5858 | #Param paint Color_Filter, Image_Filter, Blend_Mode, and so on; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5859 | |
| 5860 | #Example |
| 5861 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5862 | void draw(SkCanvas* canvas) { |
| 5863 | // SkBitmap source = mandrill; |
| 5864 | SkRSXform xforms[] = { { .5f, 0, 0, 0 }, {0, .5f, 200, 100 } }; |
| 5865 | SkRect tex[] = { { 0, 0, 250, 250 }, { 0, 0, 250, 250 } }; |
| 5866 | SkColor colors[] = { 0x7f55aa00, 0x7f3333bf }; |
| 5867 | SkPaint paint; |
| 5868 | paint.setAlpha(127); |
| 5869 | canvas->drawAtlas(image, xforms, tex, colors, 2, SkBlendMode::kPlus, nullptr, &paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5870 | } |
| 5871 | ## |
| 5872 | |
| 5873 | #ToDo bug in example on cpu side, gpu looks ok ## |
| 5874 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5875 | #SeeAlso drawBitmap drawImage |
| 5876 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5877 | ## |
| 5878 | |
| 5879 | # ------------------------------------------------------------------------------ |
| 5880 | |
| 5881 | #Method void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count, |
| 5882 | const SkRect* cullRect, const SkPaint* paint) |
| 5883 | |
| 5884 | Draw a set of sprites from atlas, using Clip, Matrix, and optional Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5885 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5886 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5887 | atlas, and RSXform xform transforms it into destination space. |
| 5888 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5889 | xform and text must contain count entries. |
| 5890 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5891 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5892 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5893 | #Param atlas Image containing sprites ## |
| 5894 | #Param xform RSXform mappings for sprites in atlas ## |
| 5895 | #Param tex Rect locations of sprites in atlas ## |
| 5896 | #Param count number of sprites to draw ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5897 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5898 | #Param paint Color_Filter, Image_Filter, Blend_Mode, and so on; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5899 | |
| 5900 | #Example |
| 5901 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5902 | void draw(SkCanvas* canvas) { |
| 5903 | // sk_sp<SkImage> image = mandrill; |
| 5904 | SkRSXform xforms[] = { { .5f, 0, 0, 0 }, {0, .5f, 200, 100 } }; |
| 5905 | SkRect tex[] = { { 0, 0, 250, 250 }, { 0, 0, 250, 250 } }; |
| 5906 | const SkImage* imagePtr = image.get(); |
| 5907 | canvas->drawAtlas(imagePtr, xforms, tex, 2, nullptr, nullptr); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5908 | } |
| 5909 | ## |
| 5910 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5911 | #SeeAlso drawBitmap drawImage |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5912 | |
| 5913 | ## |
| 5914 | |
| 5915 | # ------------------------------------------------------------------------------ |
| 5916 | |
| 5917 | #Method void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[], |
| 5918 | int count, const SkRect* cullRect, const SkPaint* paint) |
| 5919 | |
| 5920 | Draw a set of sprites from atlas, using Clip, Matrix, and optional Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5921 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5922 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5923 | atlas, and RSXform xform transforms it into destination space. |
| 5924 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5925 | xform and text must contain count entries. |
| 5926 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5927 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5928 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5929 | #Param atlas Image containing sprites ## |
| 5930 | #Param xform RSXform mappings for sprites in atlas ## |
| 5931 | #Param tex Rect locations of sprites in atlas ## |
| 5932 | #Param count number of sprites to draw ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5933 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5934 | #Param paint Color_Filter, Image_Filter, Blend_Mode, and so on; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5935 | |
| 5936 | #Example |
| 5937 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5938 | void draw(SkCanvas* canvas) { |
| 5939 | // sk_sp<SkImage> image = mandrill; |
| 5940 | SkRSXform xforms[] = { { 1, 0, 0, 0 }, {0, 1, 300, 100 } }; |
| 5941 | SkRect tex[] = { { 0, 0, 200, 200 }, { 200, 0, 400, 200 } }; |
| 5942 | canvas->drawAtlas(image, xforms, tex, 2, nullptr, nullptr); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5943 | } |
| 5944 | ## |
| 5945 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5946 | #SeeAlso drawBitmap drawImage |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5947 | |
| 5948 | ## |
| 5949 | |
| 5950 | # ------------------------------------------------------------------------------ |
| 5951 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 5952 | #Method void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5953 | |
| 5954 | Draw Drawable drawable using Clip and Matrix, concatenated with |
| 5955 | optional matrix. |
| 5956 | |
| 5957 | If Canvas has an asynchronous implementation, as is the case |
| 5958 | when it is recording into Picture, then drawable will be referenced, |
| 5959 | so that SkDrawable::draw() can be called when the operation is finalized. To force |
| 5960 | immediate drawing, call SkDrawable::draw() instead. |
| 5961 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5962 | #Param drawable custom struct encapsulating drawing commands ## |
| 5963 | #Param matrix transformation applied to drawing; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5964 | |
| 5965 | #Example |
| 5966 | #Height 100 |
| 5967 | #Function |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5968 | struct MyDrawable : public SkDrawable { |
| 5969 | SkRect onGetBounds() override { return SkRect::MakeWH(50, 100); } |
| 5970 | |
| 5971 | void onDraw(SkCanvas* canvas) override { |
| 5972 | SkPath path; |
| 5973 | path.conicTo(10, 90, 50, 90, 0.9f); |
| 5974 | SkPaint paint; |
| 5975 | paint.setColor(SK_ColorBLUE); |
| 5976 | canvas->drawRect(path.getBounds(), paint); |
| 5977 | paint.setAntiAlias(true); |
| 5978 | paint.setColor(SK_ColorWHITE); |
| 5979 | canvas->drawPath(path, paint); |
| 5980 | } |
| 5981 | }; |
| 5982 | |
| 5983 | #Function ## |
| 5984 | void draw(SkCanvas* canvas) { |
| 5985 | sk_sp<SkDrawable> drawable(new MyDrawable); |
| 5986 | SkMatrix matrix; |
| 5987 | matrix.setTranslate(10, 10); |
| 5988 | canvas->drawDrawable(drawable.get(), &matrix); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5989 | } |
| 5990 | ## |
| 5991 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5992 | #SeeAlso SkDrawable drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5993 | |
| 5994 | ## |
| 5995 | |
| 5996 | # ------------------------------------------------------------------------------ |
| 5997 | |
| 5998 | #Method void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y) |
| 5999 | |
| 6000 | Draw Drawable drawable using Clip and Matrix, offset by (x, y). |
| 6001 | |
| 6002 | If Canvas has an asynchronous implementation, as is the case |
| 6003 | when it is recording into Picture, then drawable will be referenced, |
| 6004 | so that SkDrawable::draw() can be called when the operation is finalized. To force |
| 6005 | immediate drawing, call SkDrawable::draw() instead. |
| 6006 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6007 | #Param drawable custom struct encapsulating drawing commands ## |
| 6008 | #Param x offset into Canvas writable pixels in x ## |
| 6009 | #Param y offset into Canvas writable pixels in y ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6010 | |
| 6011 | #Example |
| 6012 | #Height 100 |
| 6013 | #Function |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6014 | struct MyDrawable : public SkDrawable { |
| 6015 | SkRect onGetBounds() override { return SkRect::MakeWH(50, 100); } |
| 6016 | |
| 6017 | void onDraw(SkCanvas* canvas) override { |
| 6018 | SkPath path; |
| 6019 | path.conicTo(10, 90, 50, 90, 0.9f); |
| 6020 | SkPaint paint; |
| 6021 | paint.setColor(SK_ColorBLUE); |
| 6022 | canvas->drawRect(path.getBounds(), paint); |
| 6023 | paint.setAntiAlias(true); |
| 6024 | paint.setColor(SK_ColorWHITE); |
| 6025 | canvas->drawPath(path, paint); |
| 6026 | } |
| 6027 | }; |
| 6028 | |
| 6029 | #Function ## |
| 6030 | void draw(SkCanvas* canvas) { |
| 6031 | sk_sp<SkDrawable> drawable(new MyDrawable); |
| 6032 | canvas->drawDrawable(drawable.get(), 10, 10); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6033 | } |
| 6034 | ## |
| 6035 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6036 | #SeeAlso SkDrawable drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6037 | |
| 6038 | ## |
| 6039 | |
| 6040 | # ------------------------------------------------------------------------------ |
| 6041 | |
| 6042 | #Method void drawAnnotation(const SkRect& rect, const char key[], SkData* value) |
| 6043 | |
| 6044 | Associate Rect on Canvas when an annotation; a key-value pair, where the key is |
| 6045 | a null-terminated utf8 string, and optional value is stored as Data. |
| 6046 | |
| 6047 | Only some canvas implementations, such as recording to Picture, or drawing to |
| 6048 | Document_PDF, use annotations. |
| 6049 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6050 | #Param rect Rect extent of canvas to annotate ## |
| 6051 | #Param key string used for lookup ## |
| 6052 | #Param value data holding value stored in annotation ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6053 | |
| 6054 | #Example |
| 6055 | #Height 1 |
| 6056 | const char text[] = "Click this link!"; |
| 6057 | SkRect bounds; |
| 6058 | SkPaint paint; |
| 6059 | paint.setTextSize(40); |
| 6060 | (void)paint.measureText(text, strlen(text), &bounds); |
| 6061 | const char url[] = "https://www.google.com/"; |
| 6062 | sk_sp<SkData> urlData(SkData::MakeWithCString(url)); |
| 6063 | canvas->drawAnnotation(bounds, "url_key", urlData.get()); |
| 6064 | ## |
| 6065 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6066 | #SeeAlso SkPicture SkDocument |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6067 | |
| 6068 | ## |
| 6069 | |
| 6070 | # ------------------------------------------------------------------------------ |
| 6071 | |
| 6072 | #Method void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) |
| 6073 | |
| 6074 | Associate Rect on Canvas when an annotation; a key-value pair, where the key is |
| 6075 | a null-terminated utf8 string, and optional value is stored as Data. |
| 6076 | |
| 6077 | Only some canvas implementations, such as recording to Picture, or drawing to |
| 6078 | Document_PDF, use annotations. |
| 6079 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6080 | #Param rect Rect extent of canvas to annotate ## |
| 6081 | #Param key string used for lookup ## |
| 6082 | #Param value data holding value stored in annotation ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6083 | |
| 6084 | #Example |
| 6085 | #Height 1 |
| 6086 | const char text[] = "Click this link!"; |
| 6087 | SkRect bounds; |
| 6088 | SkPaint paint; |
| 6089 | paint.setTextSize(40); |
| 6090 | (void)paint.measureText(text, strlen(text), &bounds); |
| 6091 | const char url[] = "https://www.google.com/"; |
| 6092 | sk_sp<SkData> urlData(SkData::MakeWithCString(url)); |
| 6093 | canvas->drawAnnotation(bounds, "url_key", urlData.get()); |
| 6094 | ## |
| 6095 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6096 | #SeeAlso SkPicture SkDocument |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6097 | |
| 6098 | ## |
| 6099 | |
| 6100 | #Method SkDrawFilter* getDrawFilter() const |
| 6101 | |
| 6102 | Legacy call to be deprecated. |
| 6103 | |
| 6104 | #Deprecated |
| 6105 | ## |
| 6106 | |
| 6107 | ## |
| 6108 | |
| 6109 | #Method virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) |
| 6110 | |
| 6111 | Legacy call to be deprecated. |
| 6112 | |
| 6113 | #Deprecated |
| 6114 | ## |
| 6115 | |
| 6116 | ## |
| 6117 | |
| 6118 | # ------------------------------------------------------------------------------ |
| 6119 | |
| 6120 | #Method virtual bool isClipEmpty() const |
| 6121 | |
| 6122 | Returns true if Clip is empty; that is, nothing will draw. |
| 6123 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6124 | May do work when called; it should not be called |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6125 | more often than needed. However, once called, subsequent calls perform no |
| 6126 | work until Clip changes. |
| 6127 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6128 | #Return true if Clip is empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6129 | |
| 6130 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6131 | void draw(SkCanvas* canvas) { |
| 6132 | SkDebugf("clip is%s empty\n", canvas->isClipEmpty() ? "" : " not"); |
| 6133 | SkPath path; |
| 6134 | canvas->clipPath(path); |
| 6135 | SkDebugf("clip is%s empty\n", canvas->isClipEmpty() ? "" : " not"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6136 | } |
| 6137 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6138 | clip is not empty |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6139 | clip is empty |
| 6140 | ## |
| 6141 | ## |
| 6142 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6143 | #SeeAlso isClipRect getLocalClipBounds getDeviceClipBounds |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6144 | |
| 6145 | ## |
| 6146 | |
| 6147 | # ------------------------------------------------------------------------------ |
| 6148 | |
| 6149 | #Method virtual bool isClipRect() const |
| 6150 | |
| 6151 | Returns true if Clip is Rect and not empty. |
| 6152 | Returns false if the clip is empty, or if it is not Rect. |
| 6153 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6154 | #Return true if Clip is Rect and not empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6155 | |
| 6156 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6157 | void draw(SkCanvas* canvas) { |
| 6158 | SkDebugf("clip is%s rect\n", canvas->isClipRect() ? "" : " not"); |
| 6159 | canvas->clipRect({0, 0, 0, 0}); |
| 6160 | SkDebugf("clip is%s rect\n", canvas->isClipRect() ? "" : " not"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6161 | } |
| 6162 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6163 | clip is rect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6164 | clip is not rect |
| 6165 | ## |
| 6166 | ## |
| 6167 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6168 | #SeeAlso isClipEmpty getLocalClipBounds getDeviceClipBounds |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6169 | |
| 6170 | ## |
| 6171 | |
| 6172 | #Class SkCanvas ## |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6173 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6174 | #Topic Canvas ## |