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 |
| 32 | # topics # description ## |
| 33 | #Legend ## |
| 34 | #ToDo generate a TOC here ## |
| 35 | #Table ## |
| 36 | #Subtopic ## |
| 37 | |
| 38 | #Subtopic Constants |
| 39 | #Table |
| 40 | #Legend |
| 41 | # constants # description ## |
| 42 | #Legend ## |
| 43 | # Lattice::Flags # Controls Lattice transparency. ## |
| 44 | # PointMode # Sets drawPoints options. ## |
| 45 | # SaveLayerFlags # Sets SaveLayerRec options. ## |
| 46 | # SrcRectConstraint # Sets drawImageRect options. ## |
| 47 | #Table ## |
| 48 | #Subtopic ## |
| 49 | |
| 50 | #Subtopic Structs |
| 51 | #Table |
| 52 | #Legend |
| 53 | # struct # description ## |
| 54 | #Legend ## |
| 55 | # Lattice # Divides Bitmap, Image into a rectangular grid. ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 56 | # SaveLayerRec # Contains state to create Layer. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 57 | #Table ## |
| 58 | #Subtopic ## |
| 59 | |
| 60 | #Subtopic Constructors |
| 61 | |
| 62 | Create the desired type of Surface to obtain its Canvas when possible. Constructors are useful |
| 63 | when no Surface is required, and some helpers implicitly create Raster_Surface. |
| 64 | |
| 65 | #Table |
| 66 | #Legend |
| 67 | # # description ## |
| 68 | #Legend ## |
| 69 | # SkCanvas() # No Surface, no dimensions. ## |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 70 | # SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr) # No Surface, set dimensions, Surface_Properties. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 71 | # SkCanvas(SkBaseDevice* device) # Existing Device. (SkBaseDevice is private.) ## |
| 72 | # SkCanvas(const SkBitmap& bitmap) # Uses existing Bitmap. ## |
| 73 | # SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) # Uses existing Bitmap and Surface_Properties. ## |
| 74 | # MakeRasterDirect # Creates from SkImageInfo and Pixel_Storage. ## |
| 75 | # MakeRasterDirectN32 # Creates from image data and Pixel_Storage. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 76 | #Table ## |
| 77 | #Subtopic ## |
| 78 | |
| 79 | #Subtopic Member_Functions |
| 80 | #Table |
| 81 | #Legend |
| 82 | # function # description ## |
| 83 | #Legend ## |
| 84 | # accessTopLayerPixels # Returns writable pixel access if available. ## |
| 85 | # accessTopRasterHandle # Returns context that tracks Clip and Matrix. ## |
| 86 | # clear() # Fills Clip with Color. ## |
| 87 | # clipPath # Combines Clip with Path. ## |
| 88 | # clipRRect # Combines Clip with Round_Rect. ## |
| 89 | # clipRect # Combines Clip with Rect. ## |
| 90 | # clipRegion # Combines Clip with Region. ## |
| 91 | # concat() # Multiplies Matrix by Matrix. ## |
| 92 | # discard() # Makes Canvas contents undefined. ## |
| 93 | # drawAnnotation # Associates a Rect with a key-value pair.## |
| 94 | # drawArc # Draws Arc using Clip, Matrix, and Paint.## |
| 95 | # drawAtlas # Draws sprites using Clip, Matrix, and Paint.## |
| 96 | # drawBitmap # Draws Bitmap at (x, y) position. ## |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 97 | # drawBitmapLattice # Draws proportionally stretched Bitmap. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 98 | # drawBitmapNine # Draws Nine_Patch Bitmap. ## |
| 99 | # drawBitmapRect # Draws Bitmap, source Rect to destination Rect. ## |
| 100 | # drawCircle # Draws Circle using Clip, Matrix, and Paint. ## |
| 101 | # drawColor # Fills Clip with Color and Blend_Mode. ## |
| 102 | # drawDRRect # Draws double Round_Rect stroked or filled. ## |
| 103 | # drawDrawable # Draws Drawable, encapsulated drawing commands. ## |
| 104 | # drawIRect # Draws IRect using Clip, Matrix, and Paint. ## |
| 105 | # drawImage # Draws Image at (x, y) position. ## |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 106 | # drawImageLattice # Draws proportionally stretched Image. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 107 | # drawImageNine # Draws Nine_Patch Image. ## |
| 108 | # drawImageRect # Draws Image, source Rect to destination Rect. ## |
| 109 | # drawLine # Draws line segment between two points.## |
| 110 | # drawOval # Draws Oval using Clip, Matrix, and Paint. ## |
| 111 | # drawPaint # Fills Clip with Paint. ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 112 | # drawPatch # Draws Coons_Patch. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 113 | # drawPath # Draws Path using Clip, Matrix, and Paint. ## |
| 114 | # drawPicture # Draws Picture using Clip and Matrix. ## |
| 115 | # drawPoint # Draws point at (x, y) position. ## |
| 116 | # drawPoints # Draws array as points, lines, polygon. ## |
| 117 | # drawPosText # Draws text at array of (x, y) positions. ## |
| 118 | # drawPosTextH # Draws text at x positions with common baseline. ## |
| 119 | # drawRRect # Draws Round_Rect using Clip, Matrix, and Paint. ## |
| 120 | # drawRect # Draws Rect using Clip, Matrix, and Paint. ## |
| 121 | # drawRegion # Draws Region using Clip, Matrix, and Paint. ## |
| 122 | # drawRoundRect # Draws Round_Rect using Clip, Matrix, and Paint. ## |
| 123 | # drawText # Draws text at (x, y), using font advance. ## |
| 124 | # drawTextBlob # Draws text with arrays of positions and Paint. ## |
| 125 | # drawTextOnPath # Draws text following Path contour. ## |
| 126 | # drawTextOnPathHV # Draws text following Path with offsets. ## |
| 127 | # drawTextRSXform # Draws text with array of RSXform. ## |
| 128 | # drawString # Draws null terminated string at (x, y) using font advance. ## |
| 129 | # drawVertices # Draws Vertices, a triangle mesh. ## |
| 130 | # flush() # Triggers execution of all pending draw operations. ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 131 | # getBaseLayerSize # Gets size of base Layer in global coordinates. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 132 | # getDeviceClipBounds # Returns IRect bounds of Clip. ## |
| 133 | # getDrawFilter # Legacy; to be deprecated. ## |
| 134 | # getGrContext # Returns GPU_Context of the GPU_Surface. ## |
| 135 | # getLocalClipBounds # Returns Clip bounds in source coordinates. ## |
| 136 | # getMetaData # Associates additional data with the canvas. ## |
| 137 | # getProps # Copies Surface_Properties if available. ## |
| 138 | # getSaveCount # Returns depth of stack containing Clip and Matrix. ## |
| 139 | # getTotalMatrix # Returns Matrix. ## |
| 140 | # imageInfo # Returns Image_Info for Canvas. ## |
| 141 | # isClipEmpty # Returns if Clip is empty. ## |
| 142 | # isClipRect # Returns if Clip is Rect and not empty. ## |
| 143 | # MakeRasterDirect # Creates Canvas from SkImageInfo and pixel data. ## |
| 144 | # MakeRasterDirectN32 # Creates Canvas from image specifications and pixel data. ## |
| 145 | # makeSurface # Creates Surface matching SkImageInfo and SkSurfaceProps. ## |
| 146 | # peekPixels # Returns if Canvas has direct access to its pixels. ## |
| 147 | # quickReject # Returns if Rect is outside Clip. ## |
| 148 | # readPixels # Copies and converts rectangle of pixels from Canvas. ## |
| 149 | # resetMatrix # Resets Matrix to identity. ## |
| 150 | # restore() # Restores changes to Clip and Matrix, pops save stack. ## |
| 151 | # restoreToCount # Restores changes to Clip and Matrix to given depth. ## |
| 152 | # rotate() # Rotates Matrix. ## |
| 153 | # save() # Saves Clip and Matrix on stack. ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 154 | # saveLayer # Saves Clip and Matrix on stack; creates Layer. ## |
| 155 | # saveLayerAlpha # Saves Clip and Matrix on stack; creates Layer; sets opacity. ## |
| 156 | # saveLayerPreserveLCDTextRequests # Saves Clip and Matrix on stack; creates Layer for LCD text. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 157 | # scale() # Scales Matrix. ## |
| 158 | # setAllowSimplifyClip # Experimental. ## |
| 159 | # setDrawFilter # Legacy; to be deprecated. ## |
| 160 | # setMatrix # Sets Matrix. ## |
| 161 | # skew() # Skews Matrix. # |
| 162 | # translate() # Translates Matrix. ## |
| 163 | # writePixels # Copies and converts rectangle of pixels to Canvas. ## |
| 164 | #Table ## |
| 165 | #Subtopic ## |
| 166 | |
| 167 | #Topic Overview ## |
| 168 | |
| 169 | # ------------------------------------------------------------------------------ |
| 170 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 171 | #Method static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels, |
| 172 | size_t rowBytes, |
| 173 | const SkSurfaceProps* props = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 174 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 175 | Allocates raster Canvas that will draw directly into pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 176 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 177 | Canvas is returned if all parameters are valid. |
| 178 | Valid parameters include: |
| 179 | info dimensions are zero or positive; |
| 180 | info contains Image_Color_Type and Image_Alpha_Type supported by Raster_Surface; |
| 181 | pixels is not nullptr; |
| 182 | rowBytes is zero or large enough to contain info width pixels of Image_Color_Type. |
| 183 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 184 | Pass zero for rowBytes to compute rowBytes from info width and size of pixel. |
| 185 | If rowBytes is greater than zero, it must be equal to or greater than |
| 186 | info width times bytes required for Image_Color_Type. |
| 187 | |
| 188 | Pixel buffer size should be info height times computed rowBytes. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 189 | Pixels are not initialized. |
| 190 | To access pixels after drawing, call flush() or peekPixels. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 191 | |
| 192 | #Param info width, height, Image_Color_Type, Image_Alpha_Type, Color_Space, of Raster_Surface; |
| 193 | width, or height, or both, may be zero |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 194 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 195 | #Param pixels pointer to destination pixels buffer |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 196 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 197 | #Param rowBytes interval from one Surface row to the next, or zero |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 198 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 199 | #Param props LCD striping orientation and setting for device independent fonts; |
| 200 | may be nullptr |
| 201 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 202 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 203 | #Return Canvas if all parameters are valid; otherwise, nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 204 | |
| 205 | #Example |
| 206 | #Description |
| 207 | Allocates a three by three bitmap, clears it to white, and draws a black pixel |
| 208 | in the center. |
| 209 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 210 | void draw(SkCanvas* ) { |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 211 | SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3); // device aligned, 32 bpp, Premultiplied |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 212 | const size_t minRowBytes = info.minRowBytes(); // bytes used by one bitmap row |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 213 | const size_t size = info.computeMinByteSize(); // bytes used by all rows |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 214 | SkAutoTMalloc<SkPMColor> storage(size); // allocate storage for pixels |
| 215 | SkPMColor* pixels = storage.get(); // get pointer to allocated storage |
| 216 | // create a SkCanvas backed by a raster device, and delete it when the |
| 217 | // function goes out of scope. |
| 218 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, pixels, minRowBytes); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 219 | canvas->clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 220 | canvas->flush(); // ensure that pixels are cleared |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 221 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 222 | SkPaint paint; // by default, draws black |
| 223 | canvas->drawPoint(1, 1, paint); // draw in the center |
| 224 | canvas->flush(); // ensure that point was drawn |
| 225 | for (int y = 0; y < info.height(); ++y) { |
| 226 | for (int x = 0; x < info.width(); ++x) { |
| 227 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 228 | } |
| 229 | SkDebugf("\n"); |
| 230 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 231 | } |
| 232 | #StdOut |
| 233 | --- |
| 234 | -x- |
| 235 | --- |
| 236 | ## |
| 237 | ## |
| 238 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 239 | #SeeAlso MakeRasterDirectN32 SkSurface::MakeRasterDirect |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 240 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 241 | ## |
| 242 | |
| 243 | # ------------------------------------------------------------------------------ |
| 244 | |
| 245 | #Method static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels, |
| 246 | size_t rowBytes) |
| 247 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 248 | Allocates raster Canvas specified by inline image specification. Subsequent Canvas |
| 249 | calls draw into pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 250 | Image_Color_Type is set to kN32_SkColorType. |
| 251 | Image_Alpha_Type is set to kPremul_SkAlphaType. |
| 252 | To access pixels after drawing, call flush() or peekPixels. |
| 253 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 254 | Canvas is returned if all parameters are valid. |
| 255 | Valid parameters include: |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 256 | width and height are zero or positive; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 257 | pixels is not nullptr; |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 258 | 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] | 259 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 260 | 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] | 261 | If rowBytes is greater than zero, it must be equal to or greater than |
| 262 | width times bytes required for Image_Color_Type. |
| 263 | |
| 264 | Pixel buffer size should be height times rowBytes. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 265 | |
| 266 | #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] | 267 | #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] | 268 | #Param pixels pointer to destination pixels buffer; buffer size should be height |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 269 | times rowBytes |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 270 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 271 | #Param rowBytes interval from one Surface row to the next, or zero |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 272 | ## |
| 273 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 274 | #Return Canvas if all parameters are valid; otherwise, nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 275 | |
| 276 | #Example |
| 277 | #Description |
| 278 | Allocates a three by three bitmap, clears it to white, and draws a black pixel |
| 279 | in the center. |
| 280 | ## |
| 281 | void draw(SkCanvas* ) { |
| 282 | const int width = 3; |
| 283 | const int height = 3; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 284 | SkPMColor pixels[height][width]; // allocate a 3x3 Premultiplied bitmap on the stack |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 285 | // create a SkCanvas backed by a raster device, and delete it when the |
| 286 | // function goes out of scope. |
| 287 | std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirectN32( |
| 288 | width, |
| 289 | height, |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 290 | pixels[0], // top-left of the bitmap |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 291 | sizeof(pixels[0])); // byte width of the each row |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 292 | // write a premultiplied value for white into all pixels in the bitmap |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 293 | canvas->clear(SK_ColorWHITE); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 294 | SkPMColor pmWhite = pixels[0][0]; // the Premultiplied format may vary |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 295 | SkPaint paint; // by default, draws black |
| 296 | canvas->drawPoint(1, 1, paint); // draw in the center |
| 297 | canvas->flush(); // ensure that pixels is ready to be read |
| 298 | for (int y = 0; y < height; ++y) { |
| 299 | for (int x = 0; x < width; ++x) { |
| 300 | SkDebugf("%c", pixels[y][x] == pmWhite ? '-' : 'x'); |
| 301 | } |
| 302 | SkDebugf("\n"); |
| 303 | } |
| 304 | } |
| 305 | #StdOut |
| 306 | --- |
| 307 | -x- |
| 308 | --- |
| 309 | ## |
| 310 | ## |
| 311 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 312 | #SeeAlso MakeRasterDirect SkSurface::MakeRasterDirect SkImageInfo::MakeN32Premul |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 313 | |
| 314 | ## |
| 315 | |
| 316 | # ------------------------------------------------------------------------------ |
| 317 | |
| 318 | #Method SkCanvas() |
| 319 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 320 | Creates an empty Canvas with no backing device or pixels, with |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 321 | a width and height of zero. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 322 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 323 | #Return empty Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 324 | |
| 325 | #Example |
| 326 | |
| 327 | #Description |
| 328 | Passes a placeholder to a function that requires one. |
| 329 | ## |
| 330 | |
| 331 | #Function |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 332 | // Returns true if either the canvas rotates the text by 90 degrees, or the paint does. |
| 333 | static void check_for_up_and_down_text(const SkCanvas* canvas, const SkPaint& paint) { |
| 334 | bool paintHasVertical = paint.isVerticalText(); |
| 335 | const SkMatrix& matrix = canvas->getTotalMatrix(); |
| 336 | bool matrixIsVertical = matrix.preservesRightAngles() && !matrix.isScaleTranslate(); |
| 337 | SkDebugf("paint draws text %s\n", paintHasVertical != matrixIsVertical ? |
| 338 | "top to bottom" : "left to right"); |
| 339 | } |
| 340 | |
| 341 | static void check_for_up_and_down_text(const SkPaint& paint) { |
| 342 | SkCanvas canvas; // placeholder only, does not have an associated device |
| 343 | check_for_up_and_down_text(&canvas, paint); |
| 344 | } |
| 345 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 346 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 347 | void draw(SkCanvas* canvas) { |
| 348 | SkPaint paint; |
| 349 | check_for_up_and_down_text(paint); // paint draws text left to right |
| 350 | paint.setVerticalText(true); |
| 351 | check_for_up_and_down_text(paint); // paint draws text top to bottom |
| 352 | paint.setVerticalText(false); |
| 353 | canvas->rotate(90); |
| 354 | check_for_up_and_down_text(canvas, paint); // paint draws text top to bottom |
| 355 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 356 | |
| 357 | #StdOut |
| 358 | paint draws text left to right |
| 359 | paint draws text top to bottom |
| 360 | paint draws text top to bottom |
| 361 | ## |
| 362 | ## |
| 363 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 364 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 365 | |
| 366 | ## |
| 367 | |
| 368 | # ------------------------------------------------------------------------------ |
| 369 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 370 | #Method SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 371 | |
| 372 | Creates Canvas of the specified dimensions without a Surface. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 373 | Used by Subclasses with custom implementations for draw methods. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 374 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 375 | If props equals nullptr, Surface_Properties are created with |
| 376 | Surface_Properties_Legacy_Font_Host settings, which choose the pixel striping |
| 377 | direction and order. Since a platform may dynamically change its direction when |
| 378 | the device is rotated, and since a platform may have multiple monitors with |
| 379 | different characteristics, it is best not to rely on this legacy behavior. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 380 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 381 | #Param width zero or greater ## |
| 382 | #Param height zero or greater ## |
| 383 | #Param props LCD striping orientation and setting for device independent fonts; |
| 384 | may be nullptr |
| 385 | ## |
| 386 | |
| 387 | #Return Canvas placeholder with dimensions ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 388 | |
| 389 | #Example |
| 390 | SkCanvas canvas(10, 20); // 10 units wide, 20 units high |
| 391 | canvas.clipRect(SkRect::MakeXYWH(30, 40, 5, 10)); // clip is outside canvas' device |
| 392 | SkDebugf("canvas %s empty\n", canvas.getDeviceClipBounds().isEmpty() ? "is" : "is not"); |
| 393 | |
| 394 | #StdOut |
| 395 | canvas is empty |
| 396 | ## |
| 397 | ## |
| 398 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 399 | #SeeAlso MakeRasterDirect SkSurfaceProps SkPixelGeometry SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 400 | |
| 401 | ## |
| 402 | |
| 403 | # ------------------------------------------------------------------------------ |
| 404 | |
| 405 | #Method explicit SkCanvas(SkBaseDevice* device) |
| 406 | |
| 407 | Construct a canvas that draws into device. |
| 408 | Used by child classes of SkCanvas. |
| 409 | |
| 410 | #ToDo Since SkBaseDevice is private, shouldn't this be private also? ## |
| 411 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 412 | #Param device specifies a device for the canvas to draw into ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 413 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 414 | #Return Canvas that can be used to draw into device ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 415 | |
| 416 | #Example |
| 417 | #Error "Unsure how to create a meaningful example." |
| 418 | SkPDFCanvas::SkPDFCanvas(const sk_sp<SkPDFDevice>& dev) |
| 419 | : SkCanvas(dev.get()) {} |
| 420 | ## |
| 421 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 422 | #ToDo either remove doc or figure out a way to fiddle it ## |
| 423 | |
| 424 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 425 | |
| 426 | ## |
| 427 | |
| 428 | # ------------------------------------------------------------------------------ |
| 429 | |
| 430 | #Method explicit SkCanvas(const SkBitmap& bitmap) |
| 431 | |
| 432 | Construct a canvas that draws into bitmap. |
| 433 | Sets SkSurfaceProps::kLegacyFontHost_InitType in constructed Surface. |
| 434 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 435 | Bitmap is copied so that subsequently editing bitmap will not affect |
| 436 | constructed Canvas. |
| 437 | |
| 438 | May be deprecated in the future. |
| 439 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 440 | #ToDo Should be deprecated? ## |
| 441 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 442 | #Param bitmap width, height, Image_Color_Type, Image_Alpha_Type, and pixel |
| 443 | storage of Raster_Surface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 444 | ## |
| 445 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 446 | #Return Canvas that can be used to draw into bitmap ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 447 | |
| 448 | #Example |
| 449 | #Description |
| 450 | The actual output depends on the installed fonts. |
| 451 | ## |
| 452 | SkBitmap bitmap; |
| 453 | // create a bitmap 5 wide and 11 high |
| 454 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(5, 11)); |
| 455 | SkCanvas canvas(bitmap); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 456 | canvas.clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 457 | SkPixmap pixmap; // provides guaranteed access to the drawn pixels |
| 458 | if (!canvas.peekPixels(&pixmap)) { |
| 459 | SkDebugf("peekPixels should never fail.\n"); |
| 460 | } |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 461 | const SkPMColor* pixels = pixmap.addr32(); // points to top-left of bitmap |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 462 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 463 | SkPaint paint; // by default, draws black, 12 point text |
| 464 | canvas.drawString("!", 1, 10, paint); // 1 char at baseline (1, 10) |
| 465 | for (int y = 0; y < bitmap.height(); ++y) { |
| 466 | for (int x = 0; x < bitmap.width(); ++x) { |
| 467 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 468 | } |
| 469 | SkDebugf("\n"); |
| 470 | } |
| 471 | |
| 472 | #StdOut |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 473 | ----- |
| 474 | ---x- |
| 475 | ---x- |
| 476 | ---x- |
| 477 | ---x- |
| 478 | ---x- |
| 479 | ---x- |
| 480 | ----- |
| 481 | ---x- |
| 482 | ---x- |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 483 | ----- |
| 484 | #StdOut ## |
| 485 | ## |
| 486 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 487 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 488 | |
| 489 | ## |
| 490 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 491 | #EnumClass ColorBehavior |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 492 | |
| 493 | #Private |
| 494 | Android framework only. |
| 495 | ## |
| 496 | |
| 497 | #Code |
| 498 | enum class ColorBehavior { |
| 499 | kLegacy, |
| 500 | }; |
| 501 | ## |
| 502 | #Const kLegacy 0 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 503 | Is a placeholder to allow specialized constructor; has no meaning. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 504 | ## |
| 505 | ## |
| 506 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 507 | #Method SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior) |
| 508 | |
| 509 | #Private |
| 510 | Android framework only. |
| 511 | ## |
| 512 | |
| 513 | #Param bitmap specifies a bitmap for the canvas to draw into ## |
| 514 | #Param behavior specializes this constructor; value is unused ## |
| 515 | #Return Canvas that can be used to draw into bitmap ## |
| 516 | |
| 517 | #NoExample |
| 518 | ## |
| 519 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 520 | |
| 521 | # ------------------------------------------------------------------------------ |
| 522 | |
| 523 | #Method SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) |
| 524 | |
| 525 | Construct a canvas that draws into bitmap. |
| 526 | Use props to match the device characteristics, like LCD striping. |
| 527 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 528 | bitmap is copied so that subsequently editing bitmap will not affect |
| 529 | constructed Canvas. |
| 530 | |
| 531 | #Param bitmap width, height, Image_Color_Type, Image_Alpha_Type, |
| 532 | and pixel storage of Raster_Surface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 533 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 534 | #Param props order and orientation of RGB striping; and whether to use |
| 535 | device independent fonts |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 536 | ## |
| 537 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 538 | #Return Canvas that can be used to draw into bitmap ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 539 | |
| 540 | #Example |
| 541 | #Description |
| 542 | The actual output depends on the installed fonts. |
| 543 | ## |
| 544 | SkBitmap bitmap; |
| 545 | // create a bitmap 5 wide and 11 high |
| 546 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(5, 11)); |
| 547 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 548 | canvas.clear(SK_ColorWHITE); // white is Unpremultiplied, in ARGB order |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 549 | SkPixmap pixmap; // provides guaranteed access to the drawn pixels |
| 550 | if (!canvas.peekPixels(&pixmap)) { |
| 551 | SkDebugf("peekPixels should never fail.\n"); |
| 552 | } |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 553 | const SkPMColor* pixels = pixmap.addr32(); // points to top-left of bitmap |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 554 | SkPMColor pmWhite = pixels[0]; // the Premultiplied format may vary |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 555 | SkPaint paint; // by default, draws black, 12 point text |
| 556 | canvas.drawString("!", 1, 10, paint); // 1 char at baseline (1, 10) |
| 557 | for (int y = 0; y < bitmap.height(); ++y) { |
| 558 | for (int x = 0; x < bitmap.width(); ++x) { |
| 559 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 560 | } |
| 561 | SkDebugf("\n"); |
| 562 | } |
| 563 | |
| 564 | #StdOut |
| 565 | ----- |
| 566 | ---x- |
| 567 | ---x- |
| 568 | ---x- |
| 569 | ---x- |
| 570 | ---x- |
| 571 | ---x- |
| 572 | ----- |
| 573 | ---x- |
| 574 | ---x- |
| 575 | ----- |
| 576 | #StdOut ## |
| 577 | ## |
| 578 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 579 | #SeeAlso MakeRasterDirect SkRasterHandleAllocator::MakeCanvas SkSurface::getCanvas SkCreateColorSpaceXformCanvas |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 580 | |
| 581 | ## |
| 582 | |
| 583 | # ------------------------------------------------------------------------------ |
| 584 | |
| 585 | #Method virtual ~SkCanvas() |
| 586 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 587 | Draw saved Layers, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 588 | Free up resources used by Canvas. |
| 589 | |
| 590 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 591 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 592 | Canvas Layer draws into bitmap. saveLayerAlpha sets up an additional |
| 593 | drawing surface that blends with the bitmap. When Layer goes out of |
| 594 | scope, Layer Destructor is called. The saved Layer is restored, drawing |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 595 | transparent letters. |
| 596 | ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 597 | void draw(SkCanvas* canvas) { |
| 598 | SkBitmap bitmap; |
| 599 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(200, 200)); |
| 600 | { |
| 601 | SkCanvas offscreen(bitmap); |
| 602 | SkPaint paint; |
| 603 | paint.setTextSize(100); |
| 604 | offscreen.drawString("ABC", 20, 160, paint); |
| 605 | SkRect layerBounds = SkRect::MakeXYWH(32, 32, 192, 192); |
| 606 | offscreen.saveLayerAlpha(&layerBounds, 128); |
| 607 | offscreen.clear(SK_ColorWHITE); |
| 608 | offscreen.drawString("DEF", 20, 160, paint); |
| 609 | } |
| 610 | canvas->drawBitmap(bitmap, 0, 0, nullptr); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 611 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 612 | ## |
| 613 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 614 | #SeeAlso State_Stack |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 615 | |
| 616 | ## |
| 617 | |
| 618 | # ------------------------------------------------------------------------------ |
| 619 | |
| 620 | #Method SkMetaData& getMetaData() |
| 621 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 622 | Returns storage to associate additional data with the canvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 623 | The storage is freed when Canvas is deleted. |
| 624 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 625 | #Return storage that can be read from and written to ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 626 | |
| 627 | #Example |
| 628 | const char* kHelloMetaData = "HelloMetaData"; |
| 629 | SkCanvas canvas; |
| 630 | SkMetaData& metaData = canvas.getMetaData(); |
| 631 | SkDebugf("before: %s\n", metaData.findString(kHelloMetaData)); |
| 632 | metaData.setString(kHelloMetaData, "Hello!"); |
| 633 | SkDebugf("during: %s\n", metaData.findString(kHelloMetaData)); |
| 634 | metaData.removeString(kHelloMetaData); |
| 635 | SkDebugf("after: %s\n", metaData.findString(kHelloMetaData)); |
| 636 | |
| 637 | #StdOut |
| 638 | before: (null) |
| 639 | during: Hello! |
| 640 | after: (null) |
| 641 | #StdOut ## |
| 642 | ## |
| 643 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 644 | #SeeAlso SkMetaData |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 645 | |
| 646 | ## |
| 647 | |
| 648 | # ------------------------------------------------------------------------------ |
| 649 | |
| 650 | #Method SkImageInfo imageInfo() const |
| 651 | |
| 652 | 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] | 653 | GPU_Surface, returned Image_Color_Type is set to kUnknown_SkColorType. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 654 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 655 | #Return dimensions and Image_Color_Type of Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 656 | |
| 657 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 658 | SkCanvas emptyCanvas; |
| 659 | SkImageInfo canvasInfo = emptyCanvas.imageInfo(); |
| 660 | SkImageInfo emptyInfo; |
| 661 | SkDebugf("emptyInfo %c= canvasInfo\n", emptyInfo == canvasInfo ? '=' : '!'); |
| 662 | |
| 663 | #StdOut |
| 664 | emptyInfo == canvasInfo |
| 665 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 666 | ## |
| 667 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 668 | #SeeAlso SkImageInfo MakeRasterDirect makeSurface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 669 | |
| 670 | ## |
| 671 | |
| 672 | # ------------------------------------------------------------------------------ |
| 673 | |
| 674 | #Method bool getProps(SkSurfaceProps* props) const |
| 675 | |
| 676 | If Canvas is associated with Raster_Surface or |
| 677 | GPU_Surface, copies Surface_Properties and returns true. Otherwise, |
| 678 | return false and leave props unchanged. |
| 679 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 680 | #Param props storage for writable SkSurfaceProps ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 681 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 682 | #Return true if Surface_Properties was copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 683 | |
| 684 | #ToDo This seems old style. Deprecate? ## |
| 685 | |
| 686 | #Example |
| 687 | SkBitmap bitmap; |
| 688 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kRGB_V_SkPixelGeometry)); |
| 689 | SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); |
| 690 | SkDebugf("isRGB:%d\n", SkPixelGeometryIsRGB(surfaceProps.pixelGeometry())); |
| 691 | if (!canvas.getProps(&surfaceProps)) { |
| 692 | SkDebugf("getProps failed unexpectedly.\n"); |
| 693 | } |
| 694 | SkDebugf("isRGB:%d\n", SkPixelGeometryIsRGB(surfaceProps.pixelGeometry())); |
| 695 | |
| 696 | #StdOut |
| 697 | isRGB:0 |
| 698 | isRGB:1 |
| 699 | #StdOut ## |
| 700 | ## |
| 701 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 702 | #SeeAlso SkSurfaceProps makeSurface |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 703 | |
| 704 | ## |
| 705 | |
| 706 | # ------------------------------------------------------------------------------ |
| 707 | |
| 708 | #Method void flush() |
| 709 | |
| 710 | Triggers the immediate execution of all pending draw operations. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 711 | If Canvas is associated with GPU_Surface, resolves all pending GPU operations. |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 712 | If Canvas is associated with Raster_Surface, has no effect; raster draw |
| 713 | operations are never deferred. |
| 714 | |
| 715 | #ToDo |
| 716 | In an overview section on managing the GPU, include: |
| 717 | - flush should never change what is drawn |
| 718 | - call to kick off gpu work |
| 719 | - calling too much impacts performance |
| 720 | - some calls (peekPixels, prepareForExternalIO) call it internally |
| 721 | - canvas call is local, GrContext::flush is global |
| 722 | - diffentiate between flush, flushAndSignalSemaphores |
| 723 | - normally never needs to be called |
| 724 | - call it when sharing gpu resources, feeling memory pressure, swapping out app, and before |
| 725 | abandoning context |
| 726 | - also call to say "I'm finished drawing here", e.g., when drawing to a GPU-backed offscreen surface |
| 727 | (created with SkSurface::MakeRenderTarget) |
| 728 | |
| 729 | for posterity: this doesn't show a difference: fiddle.skia.org/c/@flushfail |
| 730 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 731 | |
| 732 | #Example |
| 733 | #Error "haven't thought of a useful example to put here" |
| 734 | ## |
| 735 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 736 | #SeeAlso peekPixels SkSurface::flush() GrContext::flush() SkSurface::prepareForExternalIO GrContext::abandonContext() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 737 | |
| 738 | ## |
| 739 | |
| 740 | # ------------------------------------------------------------------------------ |
| 741 | |
| 742 | #Method virtual SkISize getBaseLayerSize() const |
| 743 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 744 | Gets the size of the base or root Layer in global canvas coordinates. The |
| 745 | 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] | 746 | smaller (due to clipping or saveLayer). |
| 747 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 748 | #Return integral width and height of base Layer ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 749 | |
| 750 | #Example |
| 751 | SkBitmap bitmap; |
| 752 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(20, 30)); |
| 753 | SkCanvas canvas(bitmap, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
| 754 | canvas.clipRect(SkRect::MakeWH(10, 40)); |
| 755 | SkIRect clipDeviceBounds = canvas.getDeviceClipBounds(); |
| 756 | if (clipDeviceBounds.isEmpty()) { |
| 757 | SkDebugf("Empty clip bounds is unexpected!\n"); |
| 758 | } |
| 759 | SkDebugf("clip=%d,%d\n", clipDeviceBounds.width(), clipDeviceBounds.height()); |
| 760 | SkISize baseLayerSize = canvas.getBaseLayerSize(); |
| 761 | SkDebugf("size=%d,%d\n", baseLayerSize.width(), baseLayerSize.height()); |
| 762 | |
| 763 | #StdOut |
| 764 | clip=10,30 |
| 765 | size=20,30 |
| 766 | ## |
| 767 | ## |
| 768 | |
| 769 | #ToDo is this the same as the width and height of surface? ## |
| 770 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 771 | #SeeAlso getDeviceClipBounds |
| 772 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 773 | ## |
| 774 | |
| 775 | # ------------------------------------------------------------------------------ |
| 776 | |
| 777 | #Method sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr) |
| 778 | |
| 779 | Creates Surface matching info and props, and associates it with Canvas. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 780 | Returns nullptr if no match found. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 781 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 782 | If props is nullptr, matches Surface_Properties in Canvas. If props is nullptr and Canvas |
| 783 | does not have Surface_Properties, creates Surface with default Surface_Properties. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 784 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 785 | #Param info width, height, Image_Color_Type, Image_Alpha_Type, and Color_Space ## |
| 786 | #Param props Surface_Properties to match; may be nullptr to match Canvas ## |
| 787 | |
| 788 | #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] | 789 | |
| 790 | #Example |
| 791 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(5, 6); |
| 792 | SkCanvas* smallCanvas = surface->getCanvas(); |
| 793 | SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(3, 4); |
| 794 | sk_sp<SkSurface> compatible = smallCanvas->makeSurface(imageInfo); |
| 795 | SkDebugf("compatible %c= nullptr\n", compatible == nullptr ? '=' : '!'); |
| 796 | SkDebugf("size = %d, %d\n", compatible->width(), compatible->height()); |
| 797 | |
| 798 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 799 | compatible != nullptr |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 800 | size = 3, 4 |
| 801 | ## |
| 802 | ## |
| 803 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 804 | #SeeAlso SkSurface SkSurface::makeSurface SkImageInfo SkSurfaceProps |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 805 | |
| 806 | ## |
| 807 | |
| 808 | # ------------------------------------------------------------------------------ |
| 809 | |
| 810 | #Method virtual GrContext* getGrContext() |
| 811 | |
| 812 | Returns GPU_Context of the GPU_Surface associated with Canvas. |
| 813 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 814 | #Return GPU_Context, if available; nullptr otherwise ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 815 | |
| 816 | #Example |
| 817 | void draw(SkCanvas* canvas) { |
| 818 | if (canvas->getGrContext()) { |
| 819 | canvas->clear(SK_ColorRED); |
| 820 | } else { |
| 821 | canvas->clear(SK_ColorBLUE); |
| 822 | } |
| 823 | } |
| 824 | ## |
| 825 | |
| 826 | #ToDo fiddle should show both CPU and GPU out ## |
| 827 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 828 | #SeeAlso GrContext |
| 829 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 830 | ## |
| 831 | |
| 832 | # ------------------------------------------------------------------------------ |
| 833 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 834 | #Method void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 835 | |
| 836 | 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] | 837 | can be read directly. The returned address is only valid |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 838 | while Canvas is in scope and unchanged. Any Canvas call or Surface call |
| 839 | may invalidate the returned address and other returned values. |
| 840 | |
| 841 | If pixels are inaccessible, info, rowBytes, and origin are unchanged. |
| 842 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 843 | #Param info storage for writable pixels' Image_Info; may be nullptr ## |
| 844 | #Param rowBytes storage for writable pixels' row bytes; may be nullptr ## |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 845 | #Param origin storage for Canvas top Layer origin, its top-left corner; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 846 | may be nullptr |
| 847 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 848 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 849 | #Return address of pixels, or nullptr if inaccessible ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 850 | |
| 851 | #Example |
| 852 | void draw(SkCanvas* canvas) { |
| 853 | if (canvas->accessTopLayerPixels(nullptr, nullptr)) { |
| 854 | canvas->clear(SK_ColorRED); |
| 855 | } else { |
| 856 | canvas->clear(SK_ColorBLUE); |
| 857 | } |
| 858 | } |
| 859 | ## |
| 860 | |
| 861 | #Example |
| 862 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 863 | Draws "ABC" on the device. Then draws "DEF" in Layer, and reads |
| 864 | Layer to add a large dotted "DEF". Finally blends Layer with the |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 865 | device. |
| 866 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 867 | 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] | 868 | "DEF" appear only on the CPU. |
| 869 | ## |
| 870 | void draw(SkCanvas* canvas) { |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 871 | SkPaint paint; |
| 872 | paint.setTextSize(100); |
| 873 | canvas->drawString("ABC", 20, 160, paint); |
| 874 | SkRect layerBounds = SkRect::MakeXYWH(32, 32, 192, 192); |
| 875 | canvas->saveLayerAlpha(&layerBounds, 128); |
| 876 | canvas->clear(SK_ColorWHITE); |
| 877 | canvas->drawString("DEF", 20, 160, paint); |
| 878 | SkImageInfo imageInfo; |
| 879 | size_t rowBytes; |
| 880 | SkIPoint origin; |
| 881 | uint32_t* access = (uint32_t*) canvas->accessTopLayerPixels(&imageInfo, &rowBytes, &origin); |
| 882 | if (access) { |
| 883 | int h = imageInfo.height(); |
| 884 | int v = imageInfo.width(); |
| 885 | int rowWords = rowBytes / sizeof(uint32_t); |
| 886 | for (int y = 0; y < h; ++y) { |
| 887 | int newY = (y - h / 2) * 2 + h / 2; |
| 888 | if (newY < 0 || newY >= h) { |
| 889 | continue; |
| 890 | } |
| 891 | for (int x = 0; x < v; ++x) { |
| 892 | int newX = (x - v / 2) * 2 + v / 2; |
| 893 | if (newX < 0 || newX >= v) { |
| 894 | continue; |
| 895 | } |
| 896 | if (access[y * rowWords + x] == SK_ColorBLACK) { |
| 897 | access[newY * rowWords + newX] = SK_ColorGRAY; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 903 | canvas->restore(); |
| 904 | } |
| 905 | ## |
| 906 | |
| 907 | #ToDo there are no callers of this that I can find. Deprecate? ## |
| 908 | #ToDo fiddle should show both CPU and GPU out ## |
| 909 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 910 | #SeeAlso SkImageInfo SkPixmap |
| 911 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 912 | ## |
| 913 | |
| 914 | # ------------------------------------------------------------------------------ |
| 915 | |
| 916 | #Method SkRasterHandleAllocator::Handle accessTopRasterHandle() const |
| 917 | |
| 918 | Returns custom context that tracks the Matrix and Clip. |
| 919 | |
| 920 | 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] | 921 | 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] | 922 | SkRasterHandleAllocator::MakeCanvas, which creates a custom canvas with raster storage for |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 923 | the drawing destination. |
| 924 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 925 | #Return context of custom allocation ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 926 | |
| 927 | #Example |
| 928 | #Description |
| 929 | #ToDo ## |
| 930 | ## |
| 931 | #Function |
| 932 | static void DeleteCallback(void*, void* context) { |
| 933 | delete (char*) context; |
| 934 | } |
| 935 | |
| 936 | class CustomAllocator : public SkRasterHandleAllocator { |
| 937 | public: |
| 938 | bool allocHandle(const SkImageInfo& info, Rec* rec) override { |
| 939 | char* context = new char[4]{'s', 'k', 'i', 'a'}; |
| 940 | rec->fReleaseProc = DeleteCallback; |
| 941 | rec->fReleaseCtx = context; |
| 942 | rec->fHandle = context; |
| 943 | rec->fPixels = context; |
| 944 | rec->fRowBytes = 4; |
| 945 | return true; |
| 946 | } |
| 947 | |
| 948 | void updateHandle(Handle handle, const SkMatrix& ctm, const SkIRect& clip_bounds) override { |
| 949 | // apply canvas matrix and clip to custom environment |
| 950 | } |
| 951 | }; |
| 952 | |
| 953 | ## |
| 954 | void draw(SkCanvas* canvas) { |
| 955 | const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 956 | std::unique_ptr<SkCanvas> c2 = |
| 957 | SkRasterHandleAllocator::MakeCanvas(std::unique_ptr<CustomAllocator>( |
| 958 | new CustomAllocator()), info); |
| 959 | char* context = (char*) c2->accessTopRasterHandle(); |
| 960 | SkDebugf("context = %.4s\n", context); |
| 961 | |
| 962 | } |
| 963 | #StdOut |
| 964 | context = skia |
| 965 | ## |
| 966 | #ToDo skstd::make_unique could not be used because def is private -- note to fix in c++14? ## |
| 967 | ## |
| 968 | |
| 969 | #SeeAlso SkRasterHandleAllocator |
| 970 | |
| 971 | ## |
| 972 | |
| 973 | # ------------------------------------------------------------------------------ |
| 974 | |
| 975 | #Method bool peekPixels(SkPixmap* pixmap) |
| 976 | |
| 977 | Returns true if Canvas has direct access to its pixels. |
| 978 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 979 | 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] | 980 | is returned from GPU_Surface, returned by SkDocument::beginPage, returned by |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 981 | SkPictureRecorder::beginRecording, or Canvas is the base of a utility class |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 982 | like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 983 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 984 | pixmap is valid only while Canvas is in scope and unchanged. Any |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 985 | Canvas or Surface call may invalidate the pixmap values. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 986 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 987 | #Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 988 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 989 | #Return true if Canvas has direct access to pixels ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 990 | |
| 991 | #Example |
| 992 | SkPixmap pixmap; |
| 993 | if (canvas->peekPixels(&pixmap)) { |
| 994 | SkDebugf("width=%d height=%d\n", pixmap.bounds().width(), pixmap.bounds().height()); |
| 995 | } |
| 996 | #StdOut |
| 997 | width=256 height=256 |
| 998 | ## |
| 999 | ## |
| 1000 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1001 | #SeeAlso readPixels SkBitmap::peekPixels SkImage::peekPixels SkSurface::peekPixels |
| 1002 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1003 | ## |
| 1004 | |
| 1005 | # ------------------------------------------------------------------------------ |
| 1006 | |
| 1007 | #Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 1008 | int srcX, int srcY) |
| 1009 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1010 | Copies Rect of pixels from Canvas into dstPixels. Matrix and Clip are |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1011 | ignored. |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1012 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1013 | Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). |
| 1014 | Destination Rect corners are (0, 0) and (dstInfo.width(), dstInfo.height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1015 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1016 | converting to dstInfo.colorType() and dstInfo.alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1017 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1018 | Pixels are readable when Device is raster, or backed by a GPU. |
| 1019 | Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, |
| 1020 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1021 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1022 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1023 | The destination pixel storage must be allocated by the caller. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1024 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1025 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1026 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1027 | are copied. dstPixels contents outside Rect intersection are unchanged. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1028 | |
| 1029 | 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] | 1030 | |
| 1031 | Does not copy, and returns false if: |
| 1032 | |
| 1033 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1034 | # Source and destination rectangles do not intersect. ## |
| 1035 | # Canvas pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). ## |
| 1036 | # Canvas pixels are not readable; for instance, Canvas is document-based. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1037 | # dstRowBytes is too small to contain one row of pixels. ## |
| 1038 | ## |
| 1039 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1040 | #Param dstInfo width, height, Image_Color_Type, and Image_Alpha_Type of dstPixels ## |
| 1041 | #Param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger ## |
| 1042 | #Param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger ## |
| 1043 | #Param srcX offset into readable pixels in x; may be negative ## |
| 1044 | #Param srcY offset into readable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1045 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1046 | #Return true if pixels were copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1047 | |
| 1048 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1049 | #Width 64 |
| 1050 | #Height 64 |
| 1051 | #Description |
| 1052 | A black circle drawn on a blue background provides an image to copy. |
| 1053 | 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] | 1054 | The copied quarter circles overdraw the original circle. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1055 | ## |
| 1056 | canvas->clear(SK_ColorBLUE); |
| 1057 | SkPaint paint; |
| 1058 | canvas->drawCircle(32, 32, 28, paint); |
| 1059 | SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType); |
| 1060 | sk_sp<SkData> data(SkData::MakeUninitialized(info.minRowBytes() * info.height())); |
| 1061 | sk_bzero(data->writable_data(), info.minRowBytes() * info.height()); |
| 1062 | for (int x : { 32, -32 } ) { |
| 1063 | for (int y : { 32, -32 } ) { |
| 1064 | canvas->readPixels(info, data->writable_data(), info.minRowBytes(), x, y); |
| 1065 | } |
| 1066 | } |
| 1067 | sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, info.minRowBytes()); |
| 1068 | canvas->drawImage(image, 0, 0); |
| 1069 | ## |
| 1070 | |
| 1071 | #Example |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1072 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1073 | Canvas returned by Raster_Surface has Premultiplied pixel values. |
| 1074 | clear() takes Unpremultiplied input with Color_Alpha equal 0x80 |
| 1075 | and Color_RGB equal 0x55, 0xAA, 0xFF. Color_RGB is multiplied by Color_Alpha |
| 1076 | to generate Premultiplied value 0x802B5580. readPixels converts pixel back |
| 1077 | to Unpremultiplied value 0x8056A9FF, introducing error. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1078 | ## |
| 1079 | canvas->clear(0x8055aaff); |
| 1080 | for (SkAlphaType alphaType : { kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) { |
| 1081 | uint32_t pixel = 0; |
| 1082 | SkImageInfo info = SkImageInfo::Make(1, 1, kBGRA_8888_SkColorType, alphaType); |
| 1083 | if (canvas->readPixels(info, &pixel, 4, 0, 0)) { |
| 1084 | SkDebugf("pixel = %08x\n", pixel); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | #StdOut |
| 1089 | pixel = 802b5580 |
| 1090 | pixel = 8056a9ff |
| 1091 | ## |
| 1092 | ## |
| 1093 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1094 | #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] | 1095 | |
| 1096 | ## |
| 1097 | |
| 1098 | # ------------------------------------------------------------------------------ |
| 1099 | |
| 1100 | #Method bool readPixels(const SkPixmap& pixmap, int srcX, int srcY) |
| 1101 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1102 | Copies Rect of pixels from Canvas into pixmap. Matrix and Clip are |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1103 | ignored. |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1104 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1105 | Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). |
| 1106 | Destination Rect corners are (0, 0) and (pixmap.width(), pixmap.height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1107 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1108 | converting to pixmap.colorType() and pixmap.alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1109 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1110 | Pixels are readable when Device is raster, or backed by a GPU. |
| 1111 | Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, |
| 1112 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1113 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1114 | |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1115 | Caller must allocate pixel storage in pixmap if needed. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1116 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1117 | 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] | 1118 | do not match. Only pixels within both source and destination Rects |
| 1119 | are copied. pixmap pixels contents outside Rect intersection are unchanged. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1120 | |
| 1121 | 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] | 1122 | |
| 1123 | Does not copy, and returns false if: |
| 1124 | |
| 1125 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1126 | # Source and destination rectangles do not intersect. ## |
| 1127 | # Canvas pixels could not be converted to pixmap.colorType() or pixmap.alphaType(). ## |
| 1128 | # Canvas pixels are not readable; for instance, Canvas is document-based. ## |
| 1129 | # Pixmap pixels could not be allocated. ## |
| 1130 | # pixmap.rowBytes() is too small to contain one row of pixels. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1131 | ## |
| 1132 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1133 | #Param pixmap storage for pixels copied from Canvas ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1134 | #Param srcX offset into readable pixels in x; may be negative ## |
| 1135 | #Param srcY offset into readable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1136 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1137 | #Return true if pixels were copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1138 | |
| 1139 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1140 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1141 | clear() takes Unpremultiplied input with Color_Alpha equal 0x80 |
| 1142 | and Color_RGB equal 0x55, 0xAA, 0xFF. Color_RGB is multiplied by Color_Alpha |
| 1143 | to generate Premultiplied value 0x802B5580. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1144 | ## |
| 1145 | void draw(SkCanvas* canvas) { |
| 1146 | canvas->clear(0x8055aaff); |
| 1147 | uint32_t pixels[1] = { 0 }; |
| 1148 | SkPixmap pixmap(SkImageInfo::MakeN32Premul(1, 1), pixels, 4); |
| 1149 | canvas->readPixels(pixmap, 0, 0); |
| 1150 | SkDebugf("pixel = %08x\n", pixels[0]); |
| 1151 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1152 | #StdOut |
| 1153 | pixel = 802b5580 |
| 1154 | ## |
| 1155 | ## |
| 1156 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1157 | #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] | 1158 | |
| 1159 | ## |
| 1160 | |
| 1161 | # ------------------------------------------------------------------------------ |
| 1162 | |
| 1163 | #Method bool readPixels(const SkBitmap& bitmap, int srcX, int srcY) |
| 1164 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1165 | Copies Rect of pixels from Canvas into bitmap. Matrix and Clip are |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1166 | ignored. |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1167 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1168 | Source Rect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1169 | Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1170 | Copies each readable pixel intersecting both rectangles, without scaling, |
| 1171 | converting to bitmap.colorType() and bitmap.alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1172 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1173 | Pixels are readable when Device is raster, or backed by a GPU. |
| 1174 | Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, |
| 1175 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1176 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1177 | |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1178 | Caller must allocate pixel storage in bitmap if needed. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1179 | |
| 1180 | Bitmap values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1181 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1182 | are copied. Bitmap pixels outside Rect intersection are unchanged. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1183 | |
| 1184 | 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] | 1185 | |
| 1186 | Does not copy, and returns false if: |
| 1187 | |
| 1188 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1189 | # Source and destination rectangles do not intersect. ## |
| 1190 | # Canvas pixels could not be converted to bitmap.colorType() or bitmap.alphaType(). ## |
| 1191 | # Canvas pixels are not readable; for instance, Canvas is document-based. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1192 | # bitmap pixels could not be allocated. ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1193 | # bitmap.rowBytes() is too small to contain one row of pixels. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1194 | ## |
| 1195 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1196 | #Param bitmap storage for pixels copied from Canvas ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1197 | #Param srcX offset into readable pixels in x; may be negative ## |
| 1198 | #Param srcY offset into readable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1199 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1200 | #Return true if pixels were copied ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1201 | |
| 1202 | #Example |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1203 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1204 | clear() takes Unpremultiplied input with Color_Alpha equal 0x80 |
| 1205 | and Color_RGB equal 0x55, 0xAA, 0xFF. Color_RGB is multiplied by Color_Alpha |
| 1206 | to generate Premultiplied value 0x802B5580. |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1207 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1208 | void draw(SkCanvas* canvas) { |
| 1209 | canvas->clear(0x8055aaff); |
| 1210 | SkBitmap bitmap; |
| 1211 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(1, 1)); |
| 1212 | canvas->readPixels(bitmap, 0, 0); |
| 1213 | SkDebugf("pixel = %08x\n", bitmap.getAddr32(0, 0)[0]); |
| 1214 | } |
| 1215 | #StdOut |
| 1216 | pixel = 802b5580 |
| 1217 | ## |
| 1218 | ## |
| 1219 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1220 | #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] | 1221 | |
| 1222 | ## |
| 1223 | |
| 1224 | # ------------------------------------------------------------------------------ |
| 1225 | |
| 1226 | #Method bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y) |
| 1227 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1228 | Copies Rect from pixels to Canvas. Matrix and Clip are ignored. |
| 1229 | Source Rect corners are (0, 0) and (info.width(), info.height()). |
| 1230 | Destination Rect corners are (x, y) and |
| 1231 | (imageInfo().width(), imageInfo().height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1232 | |
| 1233 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1234 | converting to imageInfo().colorType() and imageInfo().alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1235 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1236 | Pixels are writable when Device is raster, or backed by a GPU. |
| 1237 | Pixels are not writable when SkCanvas is returned by SkDocument::beginPage, |
| 1238 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1239 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1240 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1241 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1242 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1243 | are copied. Canvas pixels outside Rect intersection are unchanged. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1244 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1245 | Pass negative values for x or y to offset pixels to the left or |
| 1246 | above Canvas pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1247 | |
| 1248 | Does not copy, and returns false if: |
| 1249 | |
| 1250 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1251 | # Source and destination rectangles do not intersect. ## |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1252 | # pixels could not be converted to this->imageInfo().colorType() or |
| 1253 | this->imageInfo().alphaType(). ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1254 | # Canvas pixels are not writable; for instance, Canvas is document-based. ## |
| 1255 | # rowBytes is too small to contain one row of pixels. ## |
| 1256 | ## |
| 1257 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1258 | #Param info width, height, Image_Color_Type, and Image_Alpha_Type of pixels ## |
| 1259 | #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] | 1260 | #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] | 1261 | #Param x offset into Canvas writable pixels in x; may be negative ## |
| 1262 | #Param y offset into Canvas writable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1263 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1264 | #Return true if pixels were written to Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1265 | |
| 1266 | #Example |
| 1267 | SkImageInfo imageInfo = SkImageInfo::MakeN32(256, 1, kPremul_SkAlphaType); |
| 1268 | for (int y = 0; y < 256; ++y) { |
| 1269 | uint32_t pixels[256]; |
| 1270 | for (int x = 0; x < 256; ++x) { |
| 1271 | pixels[x] = SkColorSetARGB(x, x + y, x, x - y); |
| 1272 | } |
| 1273 | canvas->writePixels(imageInfo, &pixels, sizeof(pixels), 0, y); |
| 1274 | } |
| 1275 | ## |
| 1276 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1277 | #SeeAlso readPixels drawBitmap drawImage SkBitmap::writePixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1278 | |
| 1279 | ## |
| 1280 | |
| 1281 | # ------------------------------------------------------------------------------ |
| 1282 | |
| 1283 | #Method bool writePixels(const SkBitmap& bitmap, int x, int y) |
| 1284 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1285 | Copies Rect from pixels to Canvas. Matrix and Clip are ignored. |
| 1286 | Source Rect corners are (0, 0) and (bitmap.width(), bitmap.height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1287 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1288 | Destination Rect corners are (x, y) and |
| 1289 | (imageInfo().width(), imageInfo().height()). |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 1290 | |
| 1291 | Copies each readable pixel intersecting both rectangles, without scaling, |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1292 | converting to imageInfo().colorType() and imageInfo().alphaType() if required. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1293 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1294 | Pixels are writable when Device is raster, or backed by a GPU. |
| 1295 | Pixels are not writable when SkCanvas is returned by SkDocument::beginPage, |
| 1296 | returned by SkPictureRecorder::beginRecording, or Canvas is the base of a utility |
| 1297 | class like SkDumpCanvas. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1298 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1299 | Pixel values are converted only if Image_Color_Type and Image_Alpha_Type |
| 1300 | do not match. Only pixels within both source and destination rectangles |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1301 | are copied. Canvas pixels outside Rect intersection are unchanged. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1302 | |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1303 | Pass negative values for x or y to offset pixels to the left or |
| 1304 | above Canvas pixels. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1305 | |
| 1306 | Does not copy, and returns false if: |
| 1307 | |
| 1308 | #List |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1309 | # Source and destination rectangles do not intersect. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1310 | # bitmap does not have allocated pixels. ## |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 1311 | # bitmap pixels could not be converted to this->imageInfo().colorType() or |
| 1312 | this->imageInfo().alphaType(). ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1313 | # Canvas pixels are not writable; for instance, Canvas is document based. ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1314 | # bitmap pixels are inaccessible; for instance, bitmap wraps a texture. ## |
| 1315 | ## |
| 1316 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1317 | #Param bitmap contains pixels copied to Canvas ## |
Cary Clark | f05bdda | 2017-08-24 12:59:48 -0400 | [diff] [blame] | 1318 | #Param x offset into Canvas writable pixels in x; may be negative ## |
| 1319 | #Param y offset into Canvas writable pixels in y; may be negative ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1320 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1321 | #Return true if pixels were written to Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1322 | |
| 1323 | #Example |
| 1324 | void draw(SkCanvas* canvas) { |
| 1325 | SkImageInfo imageInfo = SkImageInfo::MakeN32Premul(2, 2); |
| 1326 | SkBitmap bitmap; |
| 1327 | bitmap.setInfo(imageInfo); |
| 1328 | uint32_t pixels[4]; |
| 1329 | bitmap.setPixels(pixels); |
| 1330 | for (int y = 0; y < 256; y += 2) { |
| 1331 | for (int x = 0; x < 256; x += 2) { |
| 1332 | pixels[0] = SkColorSetRGB(x, y, x | y); |
| 1333 | pixels[1] = SkColorSetRGB(x ^ y, y, x); |
| 1334 | pixels[2] = SkColorSetRGB(x, x & y, y); |
| 1335 | pixels[3] = SkColorSetRGB(~x, ~y, x); |
| 1336 | canvas->writePixels(bitmap, x, y); |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | ## |
| 1341 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1342 | #SeeAlso readPixels drawBitmap drawImage SkBitmap::writePixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1343 | |
| 1344 | ## |
| 1345 | |
| 1346 | # ------------------------------------------------------------------------------ |
| 1347 | #Topic State_Stack |
| 1348 | |
| 1349 | Canvas maintains a stack of state that allows hierarchical drawing, commonly used |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1350 | to implement windows and views. The initial state has an identity matrix and and |
| 1351 | an infinite clip. Even with a wide-open clip, drawing is constrained by the |
| 1352 | bounds of the Canvas Surface or Device. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1353 | |
| 1354 | Canvas savable state consists of Clip, Matrix, and Draw_Filter. |
| 1355 | Clip describes the area that may be drawn to. |
| 1356 | Matrix transforms the geometry. |
| 1357 | Draw_Filter (deprecated on most platforms) modifies the paint before drawing. |
| 1358 | |
| 1359 | save(), saveLayer, saveLayerPreserveLCDTextRequests, and saveLayerAlpha |
| 1360 | save state and return the depth of the stack. |
| 1361 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1362 | restore(), restoreToCount, and ~SkCanvas() revert state to its value when saved. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1363 | |
| 1364 | Each state on the stack intersects Clip with the previous Clip, |
| 1365 | and concatenates Matrix with the previous Matrix. |
| 1366 | The intersected Clip makes the drawing area the same or smaller; |
| 1367 | the concatenated Matrix may move the origin and potentially scale or rotate |
| 1368 | the coordinate space. |
| 1369 | |
| 1370 | Canvas does not require balancing the state stack but it is a good idea |
| 1371 | to do so. Calling save() without restore() will eventually cause Skia to fail; |
| 1372 | mismatched save() and restore() create hard to find bugs. |
| 1373 | |
| 1374 | It is not possible to use state to draw outside of the clip defined by the |
| 1375 | previous state. |
| 1376 | |
| 1377 | #Example |
| 1378 | #Description |
| 1379 | Draw to ever smaller clips; then restore drawing to full canvas. |
| 1380 | Note that the second clipRect is not permitted to enlarge Clip. |
| 1381 | ## |
| 1382 | #Height 160 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1383 | void draw(SkCanvas* canvas) { |
| 1384 | SkPaint paint; |
| 1385 | canvas->save(); // records stack depth to restore |
| 1386 | canvas->clipRect(SkRect::MakeWH(100, 100)); // constrains drawing to clip |
| 1387 | canvas->clear(SK_ColorRED); // draws to limit of clip |
| 1388 | canvas->save(); // records stack depth to restore |
| 1389 | canvas->clipRect(SkRect::MakeWH(50, 150)); // Rect below 100 is ignored |
| 1390 | canvas->clear(SK_ColorBLUE); // draws to smaller clip |
| 1391 | canvas->restore(); // enlarges clip |
| 1392 | canvas->drawLine(20, 20, 150, 150, paint); // line below 100 is not drawn |
| 1393 | canvas->restore(); // enlarges clip |
| 1394 | canvas->drawLine(150, 20, 50, 120, paint); // line below 100 is drawn |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1395 | } |
| 1396 | ## |
| 1397 | |
| 1398 | Each Clip uses the current Matrix for its coordinates. |
| 1399 | |
| 1400 | #Example |
| 1401 | #Description |
| 1402 | While clipRect is given the same rectangle twice, Matrix makes the second |
| 1403 | clipRect draw at half the size of the first. |
| 1404 | ## |
| 1405 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1406 | void draw(SkCanvas* canvas) { |
| 1407 | canvas->clipRect(SkRect::MakeWH(100, 100)); |
| 1408 | canvas->clear(SK_ColorRED); |
| 1409 | canvas->scale(.5, .5); |
| 1410 | canvas->clipRect(SkRect::MakeWH(100, 100)); |
| 1411 | canvas->clear(SK_ColorBLUE); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1412 | } |
| 1413 | ## |
| 1414 | |
| 1415 | #SeeAlso save() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha restore() restoreToCount |
| 1416 | |
| 1417 | #Method int save() |
| 1418 | |
| 1419 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms). |
| 1420 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1421 | restoring the Matrix, Clip, and Draw_Filter to their state when save() was called. |
| 1422 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1423 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix, |
| 1424 | and resetMatrix. Clip may be changed by clipRect, clipRRect, clipPath, clipRegion. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1425 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1426 | Saved Canvas state is put on a stack; multiple calls to save() should be balance |
| 1427 | by an equal number of calls to restore(). |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1428 | |
| 1429 | Call restoreToCount with result to restore this and subsequent saves. |
| 1430 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1431 | #Return depth of saved stack ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1432 | |
| 1433 | #Example |
| 1434 | #Description |
| 1435 | The black square is translated 50 pixels down and to the right. |
| 1436 | Restoring Canvas state removes translate() from Canvas stack; |
| 1437 | the red square is not translated, and is drawn at the origin. |
| 1438 | ## |
| 1439 | #Height 100 |
| 1440 | void draw(SkCanvas* canvas) { |
| 1441 | SkPaint paint; |
| 1442 | SkRect rect = { 0, 0, 25, 25 }; |
| 1443 | canvas->drawRect(rect, paint); |
| 1444 | canvas->save(); |
| 1445 | canvas->translate(50, 50); |
| 1446 | canvas->drawRect(rect, paint); |
| 1447 | canvas->restore(); |
| 1448 | paint.setColor(SK_ColorRED); |
| 1449 | canvas->drawRect(rect, paint); |
| 1450 | } |
| 1451 | ## |
| 1452 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1453 | #SeeAlso saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha restore() restoreToCount |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1454 | |
| 1455 | ## |
| 1456 | |
| 1457 | # ------------------------------------------------------------------------------ |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1458 | |
| 1459 | #Method void restore() |
| 1460 | |
| 1461 | Removes changes to Matrix, Clip, and Draw_Filter since Canvas state was |
| 1462 | last saved. The state is removed from the stack. |
| 1463 | |
| 1464 | Does nothing if the stack is empty. |
| 1465 | |
| 1466 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1467 | void draw(SkCanvas* canvas) { |
| 1468 | SkCanvas simple; |
| 1469 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
| 1470 | simple.restore(); |
| 1471 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1472 | } |
| 1473 | ## |
| 1474 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1475 | #SeeAlso save() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha restoreToCount |
| 1476 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1477 | ## |
| 1478 | |
| 1479 | # ------------------------------------------------------------------------------ |
| 1480 | |
| 1481 | #Method int getSaveCount() const |
| 1482 | |
| 1483 | Returns the number of saved states, each containing: Matrix, Clip, and Draw_Filter. |
| 1484 | Equals the number of save() calls less the number of restore() calls plus one. |
| 1485 | The save count of a new canvas is one. |
| 1486 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1487 | #Return depth of save state stack ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1488 | |
| 1489 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1490 | void draw(SkCanvas* canvas) { |
| 1491 | SkCanvas simple; |
| 1492 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
| 1493 | simple.save(); |
| 1494 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
| 1495 | simple.restore(); |
| 1496 | SkDebugf("depth = %d\n", simple.getSaveCount()); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1497 | } |
| 1498 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1499 | depth = 1 |
| 1500 | depth = 2 |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1501 | depth = 1 |
| 1502 | ## |
| 1503 | ## |
| 1504 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1505 | #SeeAlso save() restore() restoreToCount |
| 1506 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1507 | ## |
| 1508 | |
| 1509 | # ------------------------------------------------------------------------------ |
| 1510 | |
| 1511 | #Method void restoreToCount(int saveCount) |
| 1512 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1513 | Restores state to Matrix, Clip, and Draw_Filter values when save(), saveLayer, |
| 1514 | saveLayerPreserveLCDTextRequests, or saveLayerAlpha returned saveCount. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1515 | |
| 1516 | Does nothing if saveCount is greater than state stack count. |
| 1517 | Restores state to initial values if saveCount is less than or equal to one. |
| 1518 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1519 | #Param saveCount depth of state stack to restore ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1520 | |
| 1521 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1522 | void draw(SkCanvas* canvas) { |
| 1523 | SkDebugf("depth = %d\n", canvas->getSaveCount()); |
| 1524 | canvas->save(); |
| 1525 | canvas->save(); |
| 1526 | SkDebugf("depth = %d\n", canvas->getSaveCount()); |
| 1527 | canvas->restoreToCount(0); |
| 1528 | SkDebugf("depth = %d\n", canvas->getSaveCount()); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1529 | } |
| 1530 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 1531 | depth = 1 |
| 1532 | depth = 3 |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1533 | depth = 1 |
| 1534 | ## |
| 1535 | ## |
| 1536 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1537 | #SeeAlso restore() getSaveCount save() |
| 1538 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 1539 | ## |
| 1540 | |
| 1541 | #Topic State_Stack ## |
| 1542 | |
| 1543 | # ------------------------------------------------------------------------------ |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1544 | |
| 1545 | #Topic Layer |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 1546 | #Substitute layer |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1547 | #Alias Layers |
| 1548 | |
| 1549 | Layer allocates a temporary Bitmap to draw into. When the drawing is |
| 1550 | complete, the Bitmap is drawn into the Canvas. |
| 1551 | |
| 1552 | Layer is saved in a stack along with other saved state. When state with a Layer |
| 1553 | is restored, the Bitmap is drawn into the previous Layer. |
| 1554 | |
| 1555 | Layer may be initialized with the contents of the previous Layer. When Layer is |
| 1556 | restored, its Bitmap can be modified by Paint passed to Layer to apply |
| 1557 | Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode. |
| 1558 | |
| 1559 | #Method int saveLayer(const SkRect* bounds, const SkPaint* paint) |
| 1560 | |
| 1561 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1562 | and allocates a Bitmap for subsequent drawing. |
| 1563 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1564 | and draws the Bitmap. |
| 1565 | |
| 1566 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1567 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1568 | clipPath, clipRegion. |
| 1569 | |
| 1570 | Rect bounds suggests but does not define the Bitmap size. To clip drawing to |
| 1571 | a specific rectangle, use clipRect. |
| 1572 | |
| 1573 | Optional Paint paint applies Color_Alpha, Color_Filter, Image_Filter, and |
| 1574 | Blend_Mode when restore() is called. |
| 1575 | |
| 1576 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1577 | |
| 1578 | #Param bounds hint to limit the size of the Layer; may be nullptr ## |
| 1579 | #Param paint graphics state for Layer; may be nullptr ## |
| 1580 | |
| 1581 | #Return depth of saved stack ## |
| 1582 | |
| 1583 | #Example |
| 1584 | #Description |
| 1585 | Rectangles are blurred by Image_Filter when restore() draws Layer to main |
| 1586 | Canvas. |
| 1587 | ## |
| 1588 | #Height 128 |
| 1589 | void draw(SkCanvas* canvas) { |
| 1590 | SkPaint paint, blur; |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1591 | blur.setImageFilter(SkBlurImageFilter::Make(3, 3, nullptr)); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1592 | canvas->saveLayer(nullptr, &blur); |
| 1593 | SkRect rect = { 25, 25, 50, 50}; |
| 1594 | canvas->drawRect(rect, paint); |
| 1595 | canvas->translate(50, 50); |
| 1596 | paint.setColor(SK_ColorRED); |
| 1597 | canvas->drawRect(rect, paint); |
| 1598 | canvas->restore(); |
| 1599 | } |
| 1600 | ## |
| 1601 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1602 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1603 | |
| 1604 | ## |
| 1605 | |
| 1606 | #Method int saveLayer(const SkRect& bounds, const SkPaint* paint) |
| 1607 | |
| 1608 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1609 | and allocates a Bitmap for subsequent drawing. |
| 1610 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1611 | and draws the Bitmap. |
| 1612 | |
| 1613 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1614 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1615 | clipPath, clipRegion. |
| 1616 | |
| 1617 | Rect bounds suggests but does not define the Layer size. To clip drawing to |
| 1618 | a specific rectangle, use clipRect. |
| 1619 | |
| 1620 | Optional Paint paint applies Color_Alpha, Color_Filter, Image_Filter, and |
| 1621 | Blend_Mode when restore() is called. |
| 1622 | |
| 1623 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1624 | |
| 1625 | #Param bounds hint to limit the size of Layer; may be nullptr ## |
| 1626 | #Param paint graphics state for Layer; may be nullptr ## |
| 1627 | |
| 1628 | #Return depth of saved stack ## |
| 1629 | |
| 1630 | #Example |
| 1631 | #Description |
| 1632 | Rectangles are blurred by Image_Filter when restore() draws Layer to main Canvas. |
| 1633 | The red rectangle is clipped; it does not fully fit on Layer. |
| 1634 | Image_Filter blurs past edge of Layer so red rectangle is blurred on all sides. |
| 1635 | ## |
| 1636 | #Height 128 |
| 1637 | void draw(SkCanvas* canvas) { |
| 1638 | SkPaint paint, blur; |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1639 | blur.setImageFilter(SkBlurImageFilter::Make(3, 3, nullptr)); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1640 | canvas->saveLayer(SkRect::MakeWH(90, 90), &blur); |
| 1641 | SkRect rect = { 25, 25, 50, 50}; |
| 1642 | canvas->drawRect(rect, paint); |
| 1643 | canvas->translate(50, 50); |
| 1644 | paint.setColor(SK_ColorRED); |
| 1645 | canvas->drawRect(rect, paint); |
| 1646 | canvas->restore(); |
| 1647 | } |
| 1648 | ## |
| 1649 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1650 | #SeeAlso save() restore() saveLayerPreserveLCDTextRequests saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1651 | |
| 1652 | ## |
| 1653 | |
| 1654 | #Method int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint) |
| 1655 | |
| 1656 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1657 | and allocates a Bitmap for subsequent drawing. |
| 1658 | LCD_Text is preserved when the Layer is drawn to the prior Layer. |
| 1659 | |
| 1660 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1661 | and draws Layer. |
| 1662 | |
| 1663 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1664 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1665 | clipPath, clipRegion. |
| 1666 | |
| 1667 | Rect bounds suggests but does not define the Layer size. To clip drawing to |
| 1668 | a specific rectangle, use clipRect. |
| 1669 | |
| 1670 | Optional Paint paint applies Color_Alpha, Color_Filter, Image_Filter, and |
| 1671 | Blend_Mode when restore() is called. |
| 1672 | |
| 1673 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1674 | |
| 1675 | Draw text on an opaque background so that LCD_Text blends correctly with the |
| 1676 | 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] | 1677 | incorrect blending. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1678 | |
| 1679 | #Param bounds hint to limit the size of Layer; may be nullptr ## |
| 1680 | #Param paint graphics state for Layer; may be nullptr ## |
| 1681 | |
| 1682 | #Return depth of saved stack ## |
| 1683 | |
| 1684 | #Example |
| 1685 | SkPaint paint; |
| 1686 | paint.setAntiAlias(true); |
| 1687 | paint.setLCDRenderText(true); |
| 1688 | paint.setTextSize(20); |
| 1689 | for (auto preserve : { false, true } ) { |
| 1690 | preserve ? canvas->saveLayerPreserveLCDTextRequests(nullptr, nullptr) |
| 1691 | : canvas->saveLayer(nullptr, nullptr); |
| 1692 | SkPaint p; |
| 1693 | p.setColor(SK_ColorWHITE); |
| 1694 | // Comment out the next line to draw on a non-opaque background. |
| 1695 | canvas->drawRect(SkRect::MakeLTRB(25, 40, 200, 70), p); |
| 1696 | canvas->drawString("Hamburgefons", 30, 60, paint); |
| 1697 | |
| 1698 | p.setColor(0xFFCCCCCC); |
| 1699 | canvas->drawRect(SkRect::MakeLTRB(25, 70, 200, 100), p); |
| 1700 | canvas->drawString("Hamburgefons", 30, 90, paint); |
| 1701 | |
| 1702 | canvas->restore(); |
| 1703 | canvas->translate(0, 80); |
| 1704 | } |
| 1705 | ## |
| 1706 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1707 | #SeeAlso save() restore() saveLayer saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1708 | |
| 1709 | ## |
| 1710 | |
| 1711 | #Method int saveLayerAlpha(const SkRect* bounds, U8CPU alpha) |
| 1712 | |
| 1713 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 1714 | and allocates Bitmap for subsequent drawing. |
| 1715 | |
| 1716 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 1717 | and blends Layer with alpha opacity onto prior Layer. |
| 1718 | |
| 1719 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 1720 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 1721 | clipPath, clipRegion. |
| 1722 | |
| 1723 | Rect bounds suggests but does not define Layer size. To clip drawing to |
| 1724 | a specific rectangle, use clipRect. |
| 1725 | |
| 1726 | alpha of zero is fully transparent, 255 is fully opaque. |
| 1727 | |
| 1728 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 1729 | |
| 1730 | #Param bounds hint to limit the size of Layer; may be nullptr ## |
| 1731 | #Param alpha opacity of Layer ## |
| 1732 | |
| 1733 | #Return depth of saved stack ## |
| 1734 | |
| 1735 | #Example |
| 1736 | SkPaint paint; |
| 1737 | paint.setColor(SK_ColorRED); |
| 1738 | canvas->drawCircle(50, 50, 50, paint); |
| 1739 | canvas->saveLayerAlpha(nullptr, 128); |
| 1740 | paint.setColor(SK_ColorBLUE); |
| 1741 | canvas->drawCircle(100, 50, 50, paint); |
| 1742 | paint.setColor(SK_ColorGREEN); |
| 1743 | paint.setAlpha(128); |
| 1744 | canvas->drawCircle(75, 90, 50, paint); |
| 1745 | canvas->restore(); |
| 1746 | ## |
| 1747 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1748 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1749 | |
| 1750 | ## |
| 1751 | |
| 1752 | #Enum |
| 1753 | |
| 1754 | #Code |
| 1755 | enum { |
| 1756 | kIsOpaque_SaveLayerFlag = 1 << 0, |
| 1757 | kPreserveLCDText_SaveLayerFlag = 1 << 1, |
| 1758 | kInitWithPrevious_SaveLayerFlag = 1 << 2, |
| 1759 | kDontClipToLayer_Legacy_SaveLayerFlag = kDontClipToLayer_PrivateSaveLayerFlag, |
| 1760 | }; |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1761 | ## |
| 1762 | |
| 1763 | SaveLayerFlags provides options that may be used in any combination in SaveLayerRec, |
| 1764 | defining how Layer allocated by saveLayer operates. |
| 1765 | |
| 1766 | #Const kIsOpaque_SaveLayerFlag 1 |
| 1767 | Creates Layer without transparency. Flag is ignored if Layer Paint contains |
| 1768 | Image_Filter or Color_Filter. |
| 1769 | ## |
| 1770 | |
| 1771 | #Const kPreserveLCDText_SaveLayerFlag 2 |
| 1772 | Creates Layer for LCD text. Flag is ignored if Layer Paint contains |
| 1773 | Image_Filter or Color_Filter. |
| 1774 | ## |
| 1775 | |
| 1776 | #Const kInitWithPrevious_SaveLayerFlag 4 |
| 1777 | Initializes Layer with the contents of the previous Layer. |
| 1778 | ## |
| 1779 | |
| 1780 | #Const kDontClipToLayer_Legacy_SaveLayerFlag 0x80000000 |
| 1781 | #Private |
| 1782 | to be deprecated: bug.skia.org/2440 |
| 1783 | ## |
| 1784 | Only present on Android. |
| 1785 | Skips setting a clip to the Layer bounds. |
| 1786 | ## |
| 1787 | |
| 1788 | #Example |
| 1789 | #Height 160 |
| 1790 | #Description |
| 1791 | Canvas Layer captures red and blue circles scaled up by four. |
| 1792 | scalePaint blends Layer back with transparency. |
| 1793 | ## |
| 1794 | void draw(SkCanvas* canvas) { |
| 1795 | SkPaint redPaint, bluePaint, scalePaint; |
| 1796 | redPaint.setColor(SK_ColorRED); |
| 1797 | canvas->drawCircle(21, 21, 8, redPaint); |
| 1798 | bluePaint.setColor(SK_ColorBLUE); |
| 1799 | canvas->drawCircle(31, 21, 8, bluePaint); |
| 1800 | SkMatrix matrix; |
| 1801 | matrix.setScale(4, 4); |
| 1802 | scalePaint.setAlpha(0x40); |
| 1803 | scalePaint.setImageFilter( |
| 1804 | SkImageFilter::MakeMatrixFilter(matrix, kNone_SkFilterQuality, nullptr)); |
| 1805 | SkCanvas::SaveLayerRec saveLayerRec(nullptr, &scalePaint, |
| 1806 | SkCanvas::kInitWithPrevious_SaveLayerFlag); |
| 1807 | canvas->saveLayer(saveLayerRec); |
| 1808 | canvas->restore(); |
| 1809 | } |
| 1810 | ## |
| 1811 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1812 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha SaveLayerRec |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1813 | |
| 1814 | #Enum ## |
| 1815 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1816 | #Typedef uint32_t SaveLayerFlags |
| 1817 | |
| 1818 | ## |
| 1819 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1820 | #Struct SaveLayerRec |
| 1821 | |
| 1822 | #Code |
| 1823 | struct SaveLayerRec { |
| 1824 | SaveLayerRec*(... |
| 1825 | |
| 1826 | const SkRect* fBounds; |
| 1827 | const SkPaint* fPaint; |
| 1828 | const SkImageFilter* fBackdrop; |
| 1829 | SaveLayerFlags fSaveLayerFlags; |
| 1830 | }; |
| 1831 | ## |
| 1832 | |
| 1833 | SaveLayerRec contains the state used to create the Layer. |
| 1834 | |
| 1835 | #Member const SkRect* fBounds |
| 1836 | fBounds is used as a hint to limit the size of Layer; may be nullptr. |
| 1837 | fBounds suggests but does not define Layer size. To clip drawing to |
| 1838 | a specific rectangle, use clipRect. |
| 1839 | ## |
| 1840 | |
| 1841 | #Member const SkPaint* fPaint |
| 1842 | fPaint modifies how Layer overlays the prior Layer; may be nullptr. |
| 1843 | Color_Alpha, Blend_Mode, Color_Filter, Draw_Looper, Image_Filter, and |
| 1844 | Mask_Filter affect Layer draw. |
| 1845 | ## |
| 1846 | |
| 1847 | #Member const SkImageFilter* fBackdrop |
| 1848 | fBackdrop applies Image_Filter to the prior Layer when copying to the Layer; |
| 1849 | may be nullptr. Use kInitWithPrevious_SaveLayerFlag to copy the |
| 1850 | prior Layer without an Image_Filter. |
| 1851 | ## |
| 1852 | |
| 1853 | #Member const SkImage* fClipMask |
| 1854 | restore() clips Layer by the Color_Alpha channel of fClipMask when |
| 1855 | Layer is copied to Device. fClipMask may be nullptr. . |
| 1856 | ## |
| 1857 | |
| 1858 | #Member const SkMatrix* fClipMatrix |
| 1859 | fClipMatrix transforms fClipMask before it clips Layer. If |
| 1860 | fClipMask describes a translucent gradient, it may be scaled and rotated |
| 1861 | without introducing artifacts. fClipMatrix may be nullptr. |
| 1862 | ## |
| 1863 | |
| 1864 | #Member SaveLayerFlags fSaveLayerFlags |
| 1865 | fSaveLayerFlags are used to create Layer without transparency, |
| 1866 | create Layer for LCD text, and to create Layer with the |
| 1867 | contents of the previous Layer. |
| 1868 | ## |
| 1869 | |
| 1870 | #Example |
| 1871 | #Height 160 |
| 1872 | #Description |
| 1873 | Canvas Layer captures a red Anti-aliased circle and a blue Aliased circle scaled |
| 1874 | up by four. After drawing another red circle without scaling on top, the Layer is |
| 1875 | transferred to the main canvas. |
| 1876 | ## |
| 1877 | void draw(SkCanvas* canvas) { |
| 1878 | SkPaint redPaint, bluePaint; |
| 1879 | redPaint.setAntiAlias(true); |
| 1880 | redPaint.setColor(SK_ColorRED); |
| 1881 | canvas->drawCircle(21, 21, 8, redPaint); |
| 1882 | bluePaint.setColor(SK_ColorBLUE); |
| 1883 | canvas->drawCircle(31, 21, 8, bluePaint); |
| 1884 | SkMatrix matrix; |
| 1885 | matrix.setScale(4, 4); |
| 1886 | auto scaler = SkImageFilter::MakeMatrixFilter(matrix, kNone_SkFilterQuality, nullptr); |
| 1887 | SkCanvas::SaveLayerRec saveLayerRec(nullptr, nullptr, scaler.get(), 0); |
| 1888 | canvas->saveLayer(saveLayerRec); |
| 1889 | canvas->drawCircle(125, 85, 8, redPaint); |
| 1890 | canvas->restore(); |
| 1891 | } |
| 1892 | ## |
| 1893 | |
| 1894 | #Method SaveLayerRec() |
| 1895 | |
| 1896 | Sets fBounds, fPaint, and fBackdrop to nullptr. Clears fSaveLayerFlags. |
| 1897 | |
| 1898 | #Return empty SaveLayerRec ## |
| 1899 | |
| 1900 | #Example |
| 1901 | SkCanvas::SaveLayerRec rec1; |
| 1902 | rec1.fSaveLayerFlags = SkCanvas::kIsOpaque_SaveLayerFlag; |
| 1903 | SkCanvas::SaveLayerRec rec2(nullptr, nullptr, SkCanvas::kIsOpaque_SaveLayerFlag); |
| 1904 | SkDebugf("rec1 %c= rec2\n", rec1.fBounds == rec2.fBounds |
| 1905 | && rec1.fPaint == rec2.fPaint |
| 1906 | && rec1.fBackdrop == rec2.fBackdrop |
| 1907 | && rec1.fSaveLayerFlags == rec2.fSaveLayerFlags ? '=' : '!'); |
| 1908 | #StdOut |
| 1909 | rec1 == rec2 |
| 1910 | ## |
| 1911 | ## |
| 1912 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1913 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 1914 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1915 | ## |
| 1916 | |
| 1917 | #Method SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0) |
| 1918 | |
| 1919 | Sets fBounds, fPaint, and fSaveLayerFlags; sets fBackdrop to nullptr. |
| 1920 | |
| 1921 | #Param bounds Layer dimensions; may be nullptr ## |
| 1922 | #Param paint applied to Layer when overlaying prior Layer; may be nullptr ## |
| 1923 | #Param saveLayerFlags SaveLayerRec options to modify Layer ## |
| 1924 | |
| 1925 | #Return SaveLayerRec with empty backdrop ## |
| 1926 | |
| 1927 | #Example |
| 1928 | SkCanvas::SaveLayerRec rec1; |
| 1929 | SkCanvas::SaveLayerRec rec2(nullptr, nullptr); |
| 1930 | SkDebugf("rec1 %c= rec2\n", rec1.fBounds == rec2.fBounds |
| 1931 | && rec1.fPaint == rec2.fPaint |
| 1932 | && rec1.fBackdrop == rec2.fBackdrop |
| 1933 | && rec1.fSaveLayerFlags == rec2.fSaveLayerFlags ? '=' : '!'); |
| 1934 | #StdOut |
| 1935 | rec1 == rec2 |
| 1936 | ## |
| 1937 | ## |
| 1938 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1939 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 1940 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1941 | ## |
| 1942 | |
| 1943 | #Method SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop, |
| 1944 | SaveLayerFlags saveLayerFlags) |
| 1945 | |
| 1946 | Sets fBounds, fPaint, fBackdrop, and fSaveLayerFlags. |
| 1947 | |
| 1948 | #Param bounds Layer dimensions; may be nullptr ## |
| 1949 | #Param paint applied to Layer when overlaying prior Layer; |
| 1950 | may be nullptr |
| 1951 | ## |
| 1952 | #Param backdrop prior Layer copied with Image_Filter; may be nullptr |
| 1953 | ## |
| 1954 | #Param saveLayerFlags SaveLayerRec options to modify Layer ## |
| 1955 | |
| 1956 | #Return SaveLayerRec fully specified ## |
| 1957 | |
| 1958 | #Example |
| 1959 | SkCanvas::SaveLayerRec rec1; |
| 1960 | SkCanvas::SaveLayerRec rec2(nullptr, nullptr, nullptr, 0); |
| 1961 | SkDebugf("rec1 %c= rec2\n", rec1.fBounds == rec2.fBounds |
| 1962 | && rec1.fPaint == rec2.fPaint |
| 1963 | && rec1.fBackdrop == rec2.fBackdrop |
| 1964 | && rec1.fSaveLayerFlags == rec2.fSaveLayerFlags ? '=' : '!'); |
| 1965 | #StdOut |
| 1966 | rec1 == rec2 |
| 1967 | ## |
| 1968 | ## |
| 1969 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1970 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 1971 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1972 | ## |
| 1973 | |
| 1974 | #Method SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop, |
| 1975 | const SkImage* clipMask, const SkMatrix* clipMatrix, |
| 1976 | SaveLayerFlags saveLayerFlags) |
| 1977 | |
| 1978 | #Experimental |
| 1979 | Not ready for general use. |
| 1980 | ## |
| 1981 | |
| 1982 | Sets fBounds, fPaint, fBackdrop, fClipMask, fClipMatrix, and fSaveLayerFlags. |
| 1983 | clipMatrix uses Color_Alpha channel of image, transformed by clipMatrix, to clip |
| 1984 | Layer when drawn to Canvas. |
| 1985 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 1986 | Implementation is not complete; has no effect if Device is GPU-backed. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 1987 | |
| 1988 | #Param bounds Layer dimensions; may be nullptr ## |
| 1989 | #Param paint graphics state applied to Layer when overlaying prior |
| 1990 | Layer; may be nullptr |
| 1991 | ## |
| 1992 | #Param backdrop prior Layer copied with Image_Filter; |
| 1993 | may be nullptr |
| 1994 | ## |
| 1995 | #Param clipMask clip applied to Layer; may be nullptr ## |
| 1996 | #Param clipMatrix matrix applied to clipMask; may be nullptr to use |
| 1997 | identity matrix |
| 1998 | ## |
| 1999 | #Param saveLayerFlags SaveLayerRec options to modify Layer ## |
| 2000 | |
| 2001 | #Return SaveLayerRec fully specified ## |
| 2002 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2003 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2004 | |
| 2005 | ## |
| 2006 | |
| 2007 | #Struct ## |
| 2008 | |
| 2009 | #Method int saveLayer(const SaveLayerRec& layerRec) |
| 2010 | |
| 2011 | Saves Matrix, Clip, and Draw_Filter (Draw_Filter deprecated on most platforms), |
| 2012 | and allocates Bitmap for subsequent drawing. |
| 2013 | |
| 2014 | Calling restore() discards changes to Matrix, Clip, and Draw_Filter, |
| 2015 | and blends Bitmap with Color_Alpha opacity onto the prior Layer. |
| 2016 | |
| 2017 | Matrix may be changed by translate(), scale(), rotate(), skew(), concat(), |
| 2018 | setMatrix, and resetMatrix. Clip may be changed by clipRect, clipRRect, |
| 2019 | clipPath, clipRegion. |
| 2020 | |
| 2021 | SaveLayerRec contains the state used to create the Layer. |
| 2022 | |
| 2023 | Call restoreToCount with returned value to restore this and subsequent saves. |
| 2024 | |
| 2025 | #Param layerRec Layer state ## |
| 2026 | |
| 2027 | #Return depth of save state stack ## |
| 2028 | |
| 2029 | #Example |
| 2030 | #Description |
| 2031 | The example draws an image, and saves it into a Layer with kInitWithPrevious_SaveLayerFlag. |
| 2032 | Next it punches a hole in Layer and restore with SkBlendMode::kPlus. |
| 2033 | Where Layer was cleared, the original image will draw unchanged. |
| 2034 | Outside of the circle the mandrill is brightened. |
| 2035 | ## |
| 2036 | #Image 3 |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 2037 | // sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_256.png"); |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2038 | canvas->drawImage(image, 0, 0, nullptr); |
| 2039 | SkCanvas::SaveLayerRec rec; |
| 2040 | SkPaint paint; |
| 2041 | paint.setBlendMode(SkBlendMode::kPlus); |
| 2042 | rec.fSaveLayerFlags = SkCanvas::kInitWithPrevious_SaveLayerFlag; |
| 2043 | rec.fPaint = &paint; |
| 2044 | canvas->saveLayer(rec); |
| 2045 | paint.setBlendMode(SkBlendMode::kClear); |
| 2046 | canvas->drawCircle(128, 128, 96, paint); |
| 2047 | canvas->restore(); |
| 2048 | ## |
| 2049 | |
| 2050 | #ToDo above example needs to replace GetResourceAsImage with way to select image in fiddle ## |
| 2051 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2052 | #SeeAlso save() restore() saveLayer saveLayerPreserveLCDTextRequests saveLayerAlpha |
| 2053 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2054 | ## |
| 2055 | |
| 2056 | #Topic Layer ## |
| 2057 | |
| 2058 | # ------------------------------------------------------------------------------ |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2059 | #Topic Matrix |
| 2060 | |
| 2061 | #Method void translate(SkScalar dx, SkScalar dy) |
| 2062 | |
| 2063 | Translate Matrix by dx along the x-axis and dy along the y-axis. |
| 2064 | |
| 2065 | Mathematically, replace Matrix with a translation matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2066 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2067 | |
| 2068 | This has the effect of moving the drawing by (dx, dy) before transforming |
| 2069 | the result with Matrix. |
| 2070 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2071 | #Param dx distance to translate in x ## |
| 2072 | #Param dy distance to translate in y ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2073 | |
| 2074 | #Example |
| 2075 | #Height 128 |
| 2076 | #Description |
| 2077 | scale() followed by translate() produces different results from translate() followed |
| 2078 | by scale(). |
| 2079 | |
| 2080 | The blue stroke follows translate of (50, 50); a black |
| 2081 | fill follows scale of (2, 1/2.f). After restoring the clip, which resets |
| 2082 | Matrix, a red frame follows the same scale of (2, 1/2.f); a gray fill |
| 2083 | follows translate of (50, 50). |
| 2084 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2085 | void draw(SkCanvas* canvas) { |
| 2086 | SkPaint filledPaint; |
| 2087 | SkPaint outlinePaint; |
| 2088 | outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 2089 | outlinePaint.setColor(SK_ColorBLUE); |
| 2090 | canvas->save(); |
| 2091 | canvas->translate(50, 50); |
| 2092 | canvas->drawCircle(28, 28, 15, outlinePaint); // blue center: (50+28, 50+28) |
| 2093 | canvas->scale(2, 1/2.f); |
| 2094 | canvas->drawCircle(28, 28, 15, filledPaint); // black center: (50+(28*2), 50+(28/2)) |
| 2095 | canvas->restore(); |
| 2096 | filledPaint.setColor(SK_ColorGRAY); |
| 2097 | outlinePaint.setColor(SK_ColorRED); |
| 2098 | canvas->scale(2, 1/2.f); |
| 2099 | canvas->drawCircle(28, 28, 15, outlinePaint); // red center: (28*2, 28/2) |
| 2100 | canvas->translate(50, 50); |
| 2101 | 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] | 2102 | } |
| 2103 | ## |
| 2104 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2105 | #SeeAlso concat() scale() skew() rotate() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2106 | |
| 2107 | ## |
| 2108 | |
| 2109 | # ------------------------------------------------------------------------------ |
| 2110 | |
| 2111 | #Method void scale(SkScalar sx, SkScalar sy) |
| 2112 | |
| 2113 | Scale Matrix by sx on the x-axis and sy on the y-axis. |
| 2114 | |
| 2115 | Mathematically, replace Matrix with a scale matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2116 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2117 | |
| 2118 | This has the effect of scaling the drawing by (sx, sy) before transforming |
| 2119 | the result with Matrix. |
| 2120 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2121 | #Param sx amount to scale in x ## |
| 2122 | #Param sy amount to scale in y ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2123 | |
| 2124 | #Example |
| 2125 | #Height 160 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2126 | void draw(SkCanvas* canvas) { |
| 2127 | SkPaint paint; |
| 2128 | SkRect rect = { 10, 20, 60, 120 }; |
| 2129 | canvas->translate(20, 20); |
| 2130 | canvas->drawRect(rect, paint); |
| 2131 | canvas->scale(2, .5f); |
| 2132 | paint.setColor(SK_ColorGRAY); |
| 2133 | canvas->drawRect(rect, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2134 | } |
| 2135 | ## |
| 2136 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2137 | #SeeAlso concat() translate() skew() rotate() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2138 | |
| 2139 | ## |
| 2140 | |
| 2141 | # ------------------------------------------------------------------------------ |
| 2142 | |
| 2143 | #Method void rotate(SkScalar degrees) |
| 2144 | |
| 2145 | Rotate Matrix by degrees. Positive degrees rotates clockwise. |
| 2146 | |
| 2147 | Mathematically, replace Matrix with a rotation matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2148 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2149 | |
| 2150 | This has the effect of rotating the drawing by degrees before transforming |
| 2151 | the result with Matrix. |
| 2152 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2153 | #Param degrees amount to rotate, in degrees ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2154 | |
| 2155 | #Example |
| 2156 | #Description |
| 2157 | Draw clock hands at time 5:10. The hour hand and minute hand point up and |
| 2158 | are rotated clockwise. |
| 2159 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2160 | void draw(SkCanvas* canvas) { |
| 2161 | SkPaint paint; |
| 2162 | paint.setStyle(SkPaint::kStroke_Style); |
| 2163 | canvas->translate(128, 128); |
| 2164 | canvas->drawCircle(0, 0, 60, paint); |
| 2165 | canvas->save(); |
| 2166 | canvas->rotate(10 * 360 / 60); // 10 minutes of 60 scaled to 360 degrees |
| 2167 | canvas->drawLine(0, 0, 0, -50, paint); |
| 2168 | canvas->restore(); |
| 2169 | canvas->rotate((5 + 10.f/60) * 360 / 12); // 5 and 10/60 hours of 12 scaled to 360 degrees |
| 2170 | canvas->drawLine(0, 0, 0, -30, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2171 | } |
| 2172 | ## |
| 2173 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2174 | #SeeAlso concat() translate() skew() scale() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2175 | |
| 2176 | ## |
| 2177 | |
| 2178 | # ------------------------------------------------------------------------------ |
| 2179 | |
| 2180 | #Method void rotate(SkScalar degrees, SkScalar px, SkScalar py) |
| 2181 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2182 | Rotate Matrix by degrees about a point at (px, py). Positive degrees rotates |
| 2183 | clockwise. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2184 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2185 | Mathematically, construct a rotation matrix. Premultiply the rotation matrix by |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2186 | a translation matrix, then replace Matrix with the resulting matrix |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2187 | Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2188 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2189 | This has the effect of rotating the drawing about a given point before |
| 2190 | transforming the result with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2191 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2192 | #Param degrees amount to rotate, in degrees ## |
| 2193 | #Param px x-coordinate of the point to rotate about ## |
| 2194 | #Param py y-coordinate of the point to rotate about ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2195 | |
| 2196 | #Example |
| 2197 | #Height 192 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2198 | void draw(SkCanvas* canvas) { |
| 2199 | SkPaint paint; |
| 2200 | paint.setTextSize(96); |
| 2201 | canvas->drawString("A1", 130, 100, paint); |
| 2202 | canvas->rotate(180, 130, 100); |
| 2203 | canvas->drawString("A1", 130, 100, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2204 | } |
| 2205 | ## |
| 2206 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2207 | #SeeAlso concat() translate() skew() scale() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2208 | |
| 2209 | ## |
| 2210 | |
| 2211 | # ------------------------------------------------------------------------------ |
| 2212 | |
| 2213 | #Method void skew(SkScalar sx, SkScalar sy) |
| 2214 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2215 | Skew Matrix by sx on the x-axis and sy on the y-axis. A positive value of sx |
| 2216 | skews the drawing right as y increases; a positive value of sy skews the drawing |
| 2217 | down as x increases. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2218 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2219 | Mathematically, replace Matrix with a skew matrix Premultiplied with Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2220 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2221 | 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] | 2222 | the result with Matrix. |
| 2223 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2224 | #Param sx amount to skew in x ## |
| 2225 | #Param sy amount to skew in y ## |
| 2226 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2227 | #Example |
| 2228 | #Description |
| 2229 | Black text mimics an oblique text style by using a negative skew in x that |
| 2230 | shifts the geometry to the right as the y values decrease. |
| 2231 | Red text uses a positive skew in y to shift the geometry down as the x values |
| 2232 | increase. |
| 2233 | Blue text combines x and y skew to rotate and scale. |
| 2234 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2235 | SkPaint paint; |
| 2236 | paint.setTextSize(128); |
| 2237 | canvas->translate(30, 130); |
| 2238 | canvas->save(); |
| 2239 | canvas->skew(-.5, 0); |
| 2240 | canvas->drawString("A1", 0, 0, paint); |
| 2241 | canvas->restore(); |
| 2242 | canvas->save(); |
| 2243 | canvas->skew(0, .5); |
| 2244 | paint.setColor(SK_ColorRED); |
| 2245 | canvas->drawString("A1", 0, 0, paint); |
| 2246 | canvas->restore(); |
| 2247 | canvas->skew(-.5, .5); |
| 2248 | paint.setColor(SK_ColorBLUE); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2249 | canvas->drawString("A1", 0, 0, paint); |
| 2250 | ## |
| 2251 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2252 | #SeeAlso concat() translate() rotate() scale() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2253 | |
| 2254 | ## |
| 2255 | |
| 2256 | # ------------------------------------------------------------------------------ |
| 2257 | |
| 2258 | #Method void concat(const SkMatrix& matrix) |
| 2259 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2260 | Replace Matrix with matrix Premultiplied with existing Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2261 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2262 | This has the effect of transforming the drawn geometry by matrix, before |
| 2263 | transforming the result with existing Matrix. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2264 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2265 | #Param matrix matrix to Premultiply with existing Matrix ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2266 | |
| 2267 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2268 | void draw(SkCanvas* canvas) { |
| 2269 | SkPaint paint; |
| 2270 | paint.setTextSize(80); |
| 2271 | paint.setTextScaleX(.3); |
| 2272 | SkMatrix matrix; |
| 2273 | SkRect rect[2] = {{ 10, 20, 90, 110 }, { 40, 130, 140, 180 }}; |
| 2274 | matrix.setRectToRect(rect[0], rect[1], SkMatrix::kFill_ScaleToFit); |
| 2275 | canvas->drawRect(rect[0], paint); |
| 2276 | canvas->drawRect(rect[1], paint); |
| 2277 | paint.setColor(SK_ColorWHITE); |
| 2278 | canvas->drawString("Here", rect[0].fLeft + 10, rect[0].fBottom - 10, paint); |
| 2279 | canvas->concat(matrix); |
| 2280 | canvas->drawString("There", rect[0].fLeft + 10, rect[0].fBottom - 10, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2281 | } |
| 2282 | ## |
| 2283 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2284 | #SeeAlso translate() rotate() scale() skew() setMatrix |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2285 | |
| 2286 | ## |
| 2287 | |
| 2288 | # ------------------------------------------------------------------------------ |
| 2289 | |
| 2290 | #Method void setMatrix(const SkMatrix& matrix) |
| 2291 | |
| 2292 | Replace Matrix with matrix. |
| 2293 | Unlike concat(), any prior matrix state is overwritten. |
| 2294 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2295 | #Param matrix matrix to copy, replacing existing Matrix ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2296 | |
| 2297 | #Example |
| 2298 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2299 | void draw(SkCanvas* canvas) { |
| 2300 | SkPaint paint; |
| 2301 | canvas->scale(4, 6); |
| 2302 | canvas->drawString("truth", 2, 10, paint); |
| 2303 | SkMatrix matrix; |
| 2304 | matrix.setScale(2.8f, 6); |
| 2305 | canvas->setMatrix(matrix); |
| 2306 | canvas->drawString("consequences", 2, 20, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2307 | } |
| 2308 | ## |
| 2309 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2310 | #SeeAlso resetMatrix concat() translate() rotate() scale() skew() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2311 | |
| 2312 | ## |
| 2313 | |
| 2314 | # ------------------------------------------------------------------------------ |
| 2315 | |
| 2316 | #Method void resetMatrix() |
| 2317 | |
| 2318 | Sets Matrix to the identity matrix. |
| 2319 | Any prior matrix state is overwritten. |
| 2320 | |
| 2321 | #Example |
| 2322 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2323 | void draw(SkCanvas* canvas) { |
| 2324 | SkPaint paint; |
| 2325 | canvas->scale(4, 6); |
| 2326 | canvas->drawString("truth", 2, 10, paint); |
| 2327 | canvas->resetMatrix(); |
| 2328 | canvas->scale(2.8f, 6); |
| 2329 | canvas->drawString("consequences", 2, 20, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2330 | } |
| 2331 | ## |
| 2332 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2333 | #SeeAlso setMatrix concat() translate() rotate() scale() skew() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2334 | |
| 2335 | ## |
| 2336 | |
| 2337 | # ------------------------------------------------------------------------------ |
| 2338 | |
| 2339 | #Method const SkMatrix& getTotalMatrix() const |
| 2340 | |
| 2341 | Returns Matrix. |
| 2342 | This does not account for translation by Device or Surface. |
| 2343 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2344 | #Return Matrix in Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2345 | |
| 2346 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2347 | SkDebugf("isIdentity %s\n", canvas->getTotalMatrix().isIdentity() ? "true" : "false"); |
| 2348 | #StdOut |
| 2349 | isIdentity true |
| 2350 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2351 | ## |
| 2352 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2353 | #SeeAlso setMatrix resetMatrix concat() |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2354 | |
| 2355 | ## |
| 2356 | |
| 2357 | #Topic Matrix ## |
| 2358 | |
| 2359 | # ------------------------------------------------------------------------------ |
| 2360 | #Topic Clip |
| 2361 | |
| 2362 | Clip is built from a stack of clipping paths. Each Path in the |
| 2363 | stack can be constructed from one or more Path_Contour elements. The |
| 2364 | Path_Contour may be composed of any number of Path_Verb segments. Each |
| 2365 | Path_Contour forms a closed area; Path_Fill_Type defines the area enclosed |
| 2366 | by Path_Contour. |
| 2367 | |
| 2368 | Clip stack of Path elements successfully restrict the Path area. Each |
| 2369 | Path is transformed by Matrix, then intersected with or subtracted from the |
| 2370 | prior Clip to form the replacement Clip. Use SkClipOp::kDifference |
| 2371 | to subtract Path from Clip; use SkClipOp::kIntersect to intersect Path |
| 2372 | with Clip. |
| 2373 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2374 | A clipping Path may be Anti-aliased; if Path, after transformation, is |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2375 | composed of horizontal and vertical lines, clearing Anti-alias allows whole pixels |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2376 | to either be inside or outside the clip. The fastest drawing has a Aliased, |
| 2377 | rectangular clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2378 | |
| 2379 | If clipping Path has Anti-alias set, clip may partially clip a pixel, requiring |
| 2380 | that drawing blend partially with the destination along the edge. A rotated |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2381 | rectangular Anti-aliased clip looks smoother but draws slower. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2382 | |
| 2383 | Clip can combine with Rect and Round_Rect primitives; like |
| 2384 | Path, these are transformed by Matrix before they are combined with Clip. |
| 2385 | |
| 2386 | Clip can combine with Region. Region is assumed to be in Device coordinates |
| 2387 | and is unaffected by Matrix. |
| 2388 | |
| 2389 | #Example |
| 2390 | #Height 90 |
| 2391 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2392 | 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] | 2393 | Use an image filter to zoom into the pixels drawn. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2394 | The edge of the Aliased clip fully draws pixels in the red circle. |
| 2395 | 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] | 2396 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2397 | SkPaint redPaint, scalePaint; |
| 2398 | redPaint.setAntiAlias(true); |
| 2399 | redPaint.setColor(SK_ColorRED); |
| 2400 | canvas->save(); |
| 2401 | for (bool antialias : { false, true } ) { |
| 2402 | canvas->save(); |
| 2403 | canvas->clipRect(SkRect::MakeWH(19.5f, 11.5f), antialias); |
| 2404 | canvas->drawCircle(17, 11, 8, redPaint); |
| 2405 | canvas->restore(); |
| 2406 | canvas->translate(16, 0); |
| 2407 | } |
| 2408 | canvas->restore(); |
| 2409 | SkMatrix matrix; |
| 2410 | matrix.setScale(6, 6); |
| 2411 | scalePaint.setImageFilter( |
| 2412 | SkImageFilter::MakeMatrixFilter(matrix, kNone_SkFilterQuality, nullptr)); |
| 2413 | SkCanvas::SaveLayerRec saveLayerRec( |
| 2414 | nullptr, &scalePaint, SkCanvas::kInitWithPrevious_SaveLayerFlag); |
| 2415 | canvas->saveLayer(saveLayerRec); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2416 | canvas->restore(); |
| 2417 | ## |
| 2418 | |
| 2419 | #Method void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias) |
| 2420 | |
| 2421 | Replace Clip with the intersection or difference of Clip and rect, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2422 | 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] | 2423 | before it is combined with Clip. |
| 2424 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 2425 | #Param rect Rect to combine with Clip ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2426 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2427 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2428 | |
| 2429 | #Example |
| 2430 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2431 | void draw(SkCanvas* canvas) { |
| 2432 | canvas->rotate(10); |
| 2433 | SkPaint paint; |
| 2434 | paint.setAntiAlias(true); |
| 2435 | for (auto alias: { false, true } ) { |
| 2436 | canvas->save(); |
| 2437 | canvas->clipRect(SkRect::MakeWH(90, 80), SkClipOp::kIntersect, alias); |
| 2438 | canvas->drawCircle(100, 60, 60, paint); |
| 2439 | canvas->restore(); |
| 2440 | canvas->translate(80, 0); |
| 2441 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2442 | } |
| 2443 | ## |
| 2444 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2445 | #SeeAlso clipRRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2446 | |
| 2447 | ## |
| 2448 | |
| 2449 | #Method void clipRect(const SkRect& rect, SkClipOp op) |
| 2450 | |
| 2451 | Replace Clip with the intersection or difference of Clip and rect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2452 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2453 | rect is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2454 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 2455 | #Param rect Rect to combine with Clip ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2456 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2457 | |
| 2458 | #Example |
| 2459 | #Height 192 |
| 2460 | #Width 280 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2461 | void draw(SkCanvas* canvas) { |
| 2462 | SkPaint paint; |
| 2463 | for (SkClipOp op: { SkClipOp::kIntersect, SkClipOp::kDifference } ) { |
| 2464 | canvas->save(); |
| 2465 | canvas->clipRect(SkRect::MakeWH(90, 120), op, false); |
| 2466 | canvas->drawCircle(100, 100, 60, paint); |
| 2467 | canvas->restore(); |
| 2468 | canvas->translate(80, 0); |
| 2469 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2470 | } |
| 2471 | ## |
| 2472 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2473 | #SeeAlso clipRRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2474 | |
| 2475 | ## |
| 2476 | |
| 2477 | #Method void clipRect(const SkRect& rect, bool doAntiAlias = false) |
| 2478 | |
| 2479 | Replace Clip with the intersection of Clip and rect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2480 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2481 | rect is transformed by Matrix |
| 2482 | before it is combined with Clip. |
| 2483 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 2484 | #Param rect Rect to combine with Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2485 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2486 | |
| 2487 | #Example |
| 2488 | #Height 133 |
| 2489 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2490 | A circle drawn in pieces looks uniform when drawn Aliased. |
| 2491 | 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] | 2492 | visible as a thin pair of lines through the right circle. |
| 2493 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2494 | void draw(SkCanvas* canvas) { |
| 2495 | canvas->clear(SK_ColorWHITE); |
| 2496 | SkPaint paint; |
| 2497 | paint.setAntiAlias(true); |
| 2498 | paint.setColor(0x8055aaff); |
| 2499 | SkRect clipRect = { 0, 0, 87.4f, 87.4f }; |
| 2500 | for (auto alias: { false, true } ) { |
| 2501 | canvas->save(); |
| 2502 | canvas->clipRect(clipRect, SkClipOp::kIntersect, alias); |
| 2503 | canvas->drawCircle(67, 67, 60, paint); |
| 2504 | canvas->restore(); |
| 2505 | canvas->save(); |
| 2506 | canvas->clipRect(clipRect, SkClipOp::kDifference, alias); |
| 2507 | canvas->drawCircle(67, 67, 60, paint); |
| 2508 | canvas->restore(); |
| 2509 | canvas->translate(120, 0); |
| 2510 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2511 | } |
| 2512 | ## |
| 2513 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2514 | #SeeAlso clipRRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2515 | |
| 2516 | ## |
| 2517 | |
| 2518 | #Method void androidFramework_setDeviceClipRestriction(const SkIRect& rect) |
| 2519 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2520 | 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] | 2521 | clipPath and intersect the current clip with the specified rect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2522 | The maximum clip affects only future clipping operations; it is not retroactive. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2523 | The clip restriction is not recorded in pictures. |
| 2524 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2525 | Pass an empty rect to disable maximum clip. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2526 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2527 | #Private |
| 2528 | This is private API to be used only by Android framework. |
| 2529 | ## |
| 2530 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2531 | #Param rect maximum allowed clip in device coordinates |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 2532 | #Param ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2533 | |
| 2534 | ## |
| 2535 | |
| 2536 | #Method void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias) |
| 2537 | |
| 2538 | Replace Clip with the intersection or difference of Clip and rrect, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2539 | with an Aliased or Anti-aliased clip edge. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2540 | rrect is transformed by Matrix |
| 2541 | before it is combined with Clip. |
| 2542 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2543 | #Param rrect Round_Rect to combine with Clip ## |
| 2544 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2545 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2546 | |
| 2547 | #Example |
| 2548 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2549 | void draw(SkCanvas* canvas) { |
| 2550 | canvas->clear(SK_ColorWHITE); |
| 2551 | SkPaint paint; |
| 2552 | paint.setAntiAlias(true); |
| 2553 | paint.setColor(0x8055aaff); |
| 2554 | SkRRect oval; |
| 2555 | oval.setOval({10, 20, 90, 100}); |
| 2556 | canvas->clipRRect(oval, SkClipOp::kIntersect, true); |
| 2557 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2558 | } |
| 2559 | ## |
| 2560 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2561 | #SeeAlso clipRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2562 | |
| 2563 | ## |
| 2564 | |
| 2565 | #Method void clipRRect(const SkRRect& rrect, SkClipOp op) |
| 2566 | |
| 2567 | Replace Clip with the intersection or difference of Clip and rrect. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2568 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2569 | rrect is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2570 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2571 | #Param rrect Round_Rect to combine with Clip ## |
| 2572 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2573 | |
| 2574 | #Example |
| 2575 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2576 | void draw(SkCanvas* canvas) { |
| 2577 | SkPaint paint; |
| 2578 | paint.setColor(0x8055aaff); |
| 2579 | auto oval = SkRRect::MakeOval({10, 20, 90, 100}); |
| 2580 | canvas->clipRRect(oval, SkClipOp::kIntersect); |
| 2581 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2582 | } |
| 2583 | ## |
| 2584 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2585 | #SeeAlso clipRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2586 | |
| 2587 | ## |
| 2588 | |
| 2589 | #Method void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) |
| 2590 | |
| 2591 | Replace Clip with the intersection of Clip and rrect, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2592 | with an Aliased or Anti-aliased clip edge. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2593 | rrect is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2594 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2595 | #Param rrect Round_Rect to combine with Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2596 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2597 | |
| 2598 | #Example |
| 2599 | #Height 128 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2600 | void draw(SkCanvas* canvas) { |
| 2601 | SkPaint paint; |
| 2602 | paint.setAntiAlias(true); |
| 2603 | auto oval = SkRRect::MakeRectXY({10, 20, 90, 100}, 9, 13); |
| 2604 | canvas->clipRRect(oval, true); |
| 2605 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2606 | } |
| 2607 | ## |
| 2608 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2609 | #SeeAlso clipRect clipPath clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2610 | |
| 2611 | ## |
| 2612 | |
| 2613 | #Method void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias) |
| 2614 | |
| 2615 | Replace Clip with the intersection or difference of Clip and path, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2616 | 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] | 2617 | describes the area inside or outside its contours; and if Path_Contour overlaps |
| 2618 | 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] | 2619 | path is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2620 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2621 | #Param path Path to combine with Clip ## |
| 2622 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2623 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2624 | |
| 2625 | #Example |
| 2626 | #Description |
| 2627 | Top figure uses SkPath::kInverseWinding_FillType and SkClipOp::kDifference; |
| 2628 | area outside clip is subtracted from circle. |
| 2629 | |
| 2630 | Bottom figure uses SkPath::kWinding_FillType and SkClipOp::kIntersect; |
| 2631 | area inside clip is intersected with circle. |
| 2632 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2633 | void draw(SkCanvas* canvas) { |
| 2634 | SkPaint paint; |
| 2635 | paint.setAntiAlias(true); |
| 2636 | SkPath path; |
| 2637 | path.addRect({20, 30, 100, 110}); |
| 2638 | path.setFillType(SkPath::kInverseWinding_FillType); |
| 2639 | canvas->save(); |
| 2640 | canvas->clipPath(path, SkClipOp::kDifference, false); |
| 2641 | canvas->drawCircle(70, 100, 60, paint); |
| 2642 | canvas->restore(); |
| 2643 | canvas->translate(100, 100); |
| 2644 | path.setFillType(SkPath::kWinding_FillType); |
| 2645 | canvas->clipPath(path, SkClipOp::kIntersect, false); |
| 2646 | canvas->drawCircle(70, 100, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2647 | } |
| 2648 | ## |
| 2649 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2650 | #SeeAlso clipRect clipRRect clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2651 | |
| 2652 | ## |
| 2653 | |
| 2654 | #Method void clipPath(const SkPath& path, SkClipOp op) |
| 2655 | |
| 2656 | Replace Clip with the intersection or difference of Clip and path. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2657 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2658 | Path_Fill_Type determines if path |
| 2659 | describes the area inside or outside its contours; and if Path_Contour overlaps |
| 2660 | itself or another Path_Contour, whether the overlaps form part of the area. |
| 2661 | path is transformed by Matrix |
| 2662 | before it is combined with Clip. |
| 2663 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2664 | #Param path Path to combine with Clip ## |
| 2665 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2666 | |
| 2667 | #Example |
| 2668 | #Description |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2669 | 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] | 2670 | SkPath::kWinding_FillType, the overlap is included. Set to |
| 2671 | SkPath::kEvenOdd_FillType, the overlap is excluded and forms a hole. |
| 2672 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2673 | void draw(SkCanvas* canvas) { |
| 2674 | SkPaint paint; |
| 2675 | paint.setAntiAlias(true); |
| 2676 | SkPath path; |
| 2677 | path.addRect({20, 15, 100, 95}); |
| 2678 | path.addRect({50, 65, 130, 135}); |
| 2679 | path.setFillType(SkPath::kWinding_FillType); |
| 2680 | canvas->save(); |
| 2681 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2682 | canvas->drawCircle(70, 85, 60, paint); |
| 2683 | canvas->restore(); |
| 2684 | canvas->translate(100, 100); |
| 2685 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 2686 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2687 | canvas->drawCircle(70, 85, 60, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2688 | } |
| 2689 | ## |
| 2690 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2691 | #SeeAlso clipRect clipRRect clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2692 | |
| 2693 | ## |
| 2694 | |
| 2695 | #Method void clipPath(const SkPath& path, bool doAntiAlias = false) |
| 2696 | |
| 2697 | Replace Clip with the intersection of Clip and path. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2698 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2699 | Path_Fill_Type determines if path |
| 2700 | describes the area inside or outside its contours; and if Path_Contour overlaps |
| 2701 | 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] | 2702 | path is transformed by Matrix before it is combined with Clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2703 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2704 | #Param path Path to combine with Clip ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2705 | #Param doAntiAlias true if Clip is to be Anti-aliased ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2706 | |
| 2707 | #Example |
| 2708 | #Height 212 |
| 2709 | #Description |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 2710 | 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] | 2711 | is set to SkPath::kWinding_FillType, the overlap is included. Set to |
| 2712 | SkPath::kEvenOdd_FillType, the overlap is excluded and forms a hole. |
| 2713 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2714 | void draw(SkCanvas* canvas) { |
| 2715 | SkPaint paint; |
| 2716 | paint.setAntiAlias(true); |
| 2717 | SkPath path; |
| 2718 | SkPoint poly[] = {{20, 20}, { 80, 20}, { 80, 80}, {40, 80}, |
| 2719 | {40, 40}, {100, 40}, {100, 100}, {20, 100}}; |
| 2720 | path.addPoly(poly, SK_ARRAY_COUNT(poly), true); |
| 2721 | path.setFillType(SkPath::kWinding_FillType); |
| 2722 | canvas->save(); |
| 2723 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2724 | canvas->drawCircle(50, 50, 45, paint); |
| 2725 | canvas->restore(); |
| 2726 | canvas->translate(100, 100); |
| 2727 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 2728 | canvas->clipPath(path, SkClipOp::kIntersect); |
| 2729 | canvas->drawCircle(50, 50, 45, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2730 | } |
| 2731 | ## |
| 2732 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2733 | #SeeAlso clipRect clipRRect clipRegion |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2734 | |
| 2735 | ## |
| 2736 | |
| 2737 | # ------------------------------------------------------------------------------ |
| 2738 | |
| 2739 | #Method void setAllowSimplifyClip(bool allow) |
| 2740 | |
| 2741 | #Experimental |
| 2742 | Only used for testing. |
| 2743 | ## |
| 2744 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2745 | Set to simplify clip stack using PathOps. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2746 | |
| 2747 | ## |
| 2748 | |
| 2749 | # ------------------------------------------------------------------------------ |
| 2750 | |
| 2751 | #Method void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect) |
| 2752 | |
| 2753 | Replace Clip with the intersection or difference of Clip and Region deviceRgn. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2754 | Resulting Clip is Aliased; pixels are fully contained by the clip. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2755 | deviceRgn is unaffected by Matrix. |
| 2756 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2757 | #Param deviceRgn Region to combine with Clip ## |
| 2758 | #Param op Clip_Op to apply to Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2759 | |
| 2760 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2761 | #Description |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 2762 | region is unaffected by canvas rotation; iRect is affected by canvas rotation. |
| 2763 | 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] | 2764 | aligns to pixel boundaries. |
| 2765 | ## |
| 2766 | void draw(SkCanvas* canvas) { |
| 2767 | SkPaint paint; |
| 2768 | paint.setAntiAlias(true); |
| 2769 | SkIRect iRect = {30, 40, 120, 130 }; |
| 2770 | SkRegion region(iRect); |
| 2771 | canvas->rotate(10); |
| 2772 | canvas->save(); |
| 2773 | canvas->clipRegion(region, SkClipOp::kIntersect); |
| 2774 | canvas->drawCircle(50, 50, 45, paint); |
| 2775 | canvas->restore(); |
| 2776 | canvas->translate(100, 100); |
| 2777 | canvas->clipRect(SkRect::Make(iRect), SkClipOp::kIntersect); |
| 2778 | canvas->drawCircle(50, 50, 45, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2779 | } |
| 2780 | ## |
| 2781 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2782 | #SeeAlso clipRect clipRRect clipPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2783 | |
| 2784 | ## |
| 2785 | |
| 2786 | #Method bool quickReject(const SkRect& rect) const |
| 2787 | |
| 2788 | Return true if Rect rect, transformed by Matrix, can be quickly determined to be |
| 2789 | outside of Clip. May return false even though rect is outside of Clip. |
| 2790 | |
| 2791 | Use to check if an area to be drawn is clipped out, to skip subsequent draw calls. |
| 2792 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2793 | #Param rect Rect to compare with Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2794 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2795 | #Return true if rect, transformed by Matrix, does not intersect Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2796 | |
| 2797 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2798 | void draw(SkCanvas* canvas) { |
| 2799 | SkRect testRect = {30, 30, 120, 129 }; |
| 2800 | SkRect clipRect = {30, 130, 120, 230 }; |
| 2801 | canvas->save(); |
| 2802 | canvas->clipRect(clipRect); |
| 2803 | SkDebugf("quickReject %s\n", canvas->quickReject(testRect) ? "true" : "false"); |
| 2804 | canvas->restore(); |
| 2805 | canvas->rotate(10); |
| 2806 | canvas->clipRect(clipRect); |
| 2807 | SkDebugf("quickReject %s\n", canvas->quickReject(testRect) ? "true" : "false"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2808 | } |
| 2809 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2810 | quickReject true |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2811 | quickReject false |
| 2812 | ## |
| 2813 | ## |
| 2814 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2815 | #SeeAlso getLocalClipBounds getTotalMatrix SkBitmap::drawsNothing |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2816 | |
| 2817 | ## |
| 2818 | |
| 2819 | #Method bool quickReject(const SkPath& path) const |
| 2820 | |
| 2821 | Return true if path, transformed by Matrix, can be quickly determined to be |
| 2822 | outside of Clip. May return false even though path is outside of Clip. |
| 2823 | |
| 2824 | Use to check if an area to be drawn is clipped out, to skip subsequent draw calls. |
| 2825 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2826 | #Param path Path to compare with Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2827 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2828 | #Return true if path, transformed by Matrix, does not intersect Clip ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2829 | |
| 2830 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2831 | void draw(SkCanvas* canvas) { |
| 2832 | SkPoint testPoints[] = {{30, 30}, {120, 30}, {120, 129} }; |
| 2833 | SkPoint clipPoints[] = {{30, 130}, {120, 130}, {120, 230} }; |
| 2834 | SkPath testPath, clipPath; |
| 2835 | testPath.addPoly(testPoints, SK_ARRAY_COUNT(testPoints), true); |
| 2836 | clipPath.addPoly(clipPoints, SK_ARRAY_COUNT(clipPoints), true); |
| 2837 | canvas->save(); |
| 2838 | canvas->clipPath(clipPath); |
| 2839 | SkDebugf("quickReject %s\n", canvas->quickReject(testPath) ? "true" : "false"); |
| 2840 | canvas->restore(); |
| 2841 | canvas->rotate(10); |
| 2842 | canvas->clipPath(clipPath); |
| 2843 | SkDebugf("quickReject %s\n", canvas->quickReject(testPath) ? "true" : "false"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2844 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2845 | quickReject true |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2846 | quickReject false |
| 2847 | ## |
| 2848 | } |
| 2849 | ## |
| 2850 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2851 | #SeeAlso getLocalClipBounds getTotalMatrix SkBitmap::drawsNothing |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2852 | |
| 2853 | ## |
| 2854 | |
| 2855 | #Method SkRect getLocalClipBounds() const |
| 2856 | |
| 2857 | Return bounds of Clip, transformed by inverse of Matrix. If Clip is empty, |
| 2858 | return SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2859 | |
| 2860 | 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] | 2861 | is Anti-aliased. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2862 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2863 | #Return bounds of Clip in local coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2864 | |
| 2865 | #Example |
| 2866 | #Description |
| 2867 | Initial bounds is device bounds outset by 1 on all sides. |
| 2868 | Clipped bounds is clipPath bounds outset by 1 on all sides. |
| 2869 | Scaling the canvas by two in x and y scales the local bounds by 1/2 in x and y. |
| 2870 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2871 | SkCanvas local(256, 256); |
| 2872 | canvas = &local; |
| 2873 | SkRect bounds = canvas->getLocalClipBounds(); |
| 2874 | SkDebugf("left:%g top:%g right:%g bottom:%g\n", |
| 2875 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2876 | SkPoint clipPoints[] = {{30, 130}, {120, 130}, {120, 230} }; |
| 2877 | SkPath clipPath; |
| 2878 | clipPath.addPoly(clipPoints, SK_ARRAY_COUNT(clipPoints), true); |
| 2879 | canvas->clipPath(clipPath); |
| 2880 | bounds = canvas->getLocalClipBounds(); |
| 2881 | SkDebugf("left:%g top:%g right:%g bottom:%g\n", |
| 2882 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2883 | canvas->scale(2, 2); |
| 2884 | bounds = canvas->getLocalClipBounds(); |
| 2885 | SkDebugf("left:%g top:%g right:%g bottom:%g\n", |
| 2886 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2887 | #StdOut |
| 2888 | left:-1 top:-1 right:257 bottom:257 |
| 2889 | left:29 top:129 right:121 bottom:231 |
| 2890 | left:14.5 top:64.5 right:60.5 bottom:115.5 |
| 2891 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2892 | ## |
| 2893 | |
| 2894 | # local canvas in example works around bug in fiddle ## |
| 2895 | #Bug 6524 ## |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2896 | #SeeAlso getDeviceClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2897 | |
| 2898 | ## |
| 2899 | |
| 2900 | #Method bool getLocalClipBounds(SkRect* bounds) const |
| 2901 | |
| 2902 | Return bounds of Clip, transformed by inverse of Matrix. If Clip is empty, |
| 2903 | return false, and set bounds to SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2904 | |
| 2905 | 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] | 2906 | is Anti-aliased. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2907 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2908 | #Param bounds Rect of Clip in local coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2909 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2910 | #Return true if Clip bounds is not empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2911 | |
| 2912 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2913 | void draw(SkCanvas* canvas) { |
| 2914 | SkCanvas local(256, 256); |
| 2915 | canvas = &local; |
| 2916 | SkRect bounds; |
| 2917 | SkDebugf("local bounds empty = %s\n", canvas->getLocalClipBounds(&bounds) |
| 2918 | ? "false" : "true"); |
| 2919 | SkPath path; |
| 2920 | canvas->clipPath(path); |
| 2921 | SkDebugf("local bounds empty = %s\n", canvas->getLocalClipBounds(&bounds) |
| 2922 | ? "false" : "true"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2923 | } |
| 2924 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2925 | local bounds empty = false |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2926 | local bounds empty = true |
| 2927 | ## |
| 2928 | ## |
| 2929 | |
| 2930 | # local canvas in example works around bug in fiddle ## |
| 2931 | #Bug 6524 ## |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 2932 | #SeeAlso getDeviceClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2933 | |
| 2934 | ## |
| 2935 | |
| 2936 | #Method SkIRect getDeviceClipBounds() const |
| 2937 | |
| 2938 | Return IRect bounds of Clip, unaffected by Matrix. If Clip is empty, |
| 2939 | return SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2940 | |
| 2941 | Unlike getLocalClipBounds, returned IRect is not outset. |
| 2942 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2943 | #Return bounds of Clip in Device coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2944 | |
| 2945 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2946 | void draw(SkCanvas* canvas) { |
| 2947 | #Description |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2948 | Initial bounds is device bounds, not outset. |
| 2949 | Clipped bounds is clipPath bounds, not outset. |
| 2950 | 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] | 2951 | ## |
| 2952 | SkCanvas device(256, 256); |
| 2953 | canvas = &device; |
| 2954 | SkIRect bounds = canvas->getDeviceClipBounds(); |
| 2955 | SkDebugf("left:%d top:%d right:%d bottom:%d\n", |
| 2956 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2957 | SkPoint clipPoints[] = {{30, 130}, {120, 130}, {120, 230} }; |
| 2958 | SkPath clipPath; |
| 2959 | clipPath.addPoly(clipPoints, SK_ARRAY_COUNT(clipPoints), true); |
| 2960 | canvas->save(); |
| 2961 | canvas->clipPath(clipPath); |
| 2962 | bounds = canvas->getDeviceClipBounds(); |
| 2963 | SkDebugf("left:%d top:%d right:%d bottom:%d\n", |
| 2964 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 2965 | canvas->restore(); |
| 2966 | canvas->scale(1.f/2, 1.f/2); |
| 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); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2971 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2972 | left:0 top:0 right:256 bottom:256 |
| 2973 | left:30 top:130 right:120 bottom:230 |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2974 | left:15 top:65 right:60 bottom:115 |
| 2975 | ## |
| 2976 | } |
| 2977 | ## |
| 2978 | |
| 2979 | #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] | 2980 | #SeeAlso getLocalClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2981 | |
| 2982 | # device canvas in example works around bug in fiddle ## |
| 2983 | #Bug 6524 ## |
| 2984 | |
| 2985 | ## |
| 2986 | |
| 2987 | #Method bool getDeviceClipBounds(SkIRect* bounds) const |
| 2988 | |
| 2989 | Return IRect bounds of Clip, unaffected by Matrix. If Clip is empty, |
| 2990 | return false, and set bounds to SkRect::MakeEmpty, where all Rect sides equal zero. |
| 2991 | |
| 2992 | Unlike getLocalClipBounds, bounds is not outset. |
| 2993 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2994 | #Param bounds Rect of Clip in device coordinates ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2995 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2996 | #Return true if Clip bounds is not empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 2997 | |
| 2998 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 2999 | void draw(SkCanvas* canvas) { |
| 3000 | SkIRect bounds; |
| 3001 | SkDebugf("device bounds empty = %s\n", canvas->getDeviceClipBounds(&bounds) |
| 3002 | ? "false" : "true"); |
| 3003 | SkPath path; |
| 3004 | canvas->clipPath(path); |
| 3005 | SkDebugf("device bounds empty = %s\n", canvas->getDeviceClipBounds(&bounds) |
| 3006 | ? "false" : "true"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3007 | } |
| 3008 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3009 | device bounds empty = false |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3010 | device bounds empty = true |
| 3011 | ## |
| 3012 | ## |
| 3013 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3014 | #SeeAlso getLocalClipBounds getBaseLayerSize quickReject |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3015 | |
| 3016 | ## |
| 3017 | |
| 3018 | #Topic Clip ## |
| 3019 | |
| 3020 | # ------------------------------------------------------------------------------ |
| 3021 | |
| 3022 | #Method void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver) |
| 3023 | |
| 3024 | Fill Clip with Color color. |
| 3025 | mode determines how Color_ARGB is combined with destination. |
| 3026 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3027 | #Param color Unpremultiplied Color_ARGB ## |
| 3028 | #Param mode SkBlendMode used to combine source color and destination ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3029 | |
| 3030 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3031 | canvas->drawColor(SK_ColorRED); |
| 3032 | canvas->clipRect(SkRect::MakeWH(150, 150)); |
| 3033 | canvas->drawColor(SkColorSetARGB(0x80, 0x00, 0xFF, 0x00), SkBlendMode::kPlus); |
| 3034 | canvas->clipRect(SkRect::MakeWH(75, 75)); |
| 3035 | canvas->drawColor(SkColorSetARGB(0x80, 0x00, 0x00, 0xFF), SkBlendMode::kPlus); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3036 | ## |
| 3037 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3038 | #SeeAlso clear SkBitmap::erase drawPaint |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3039 | |
| 3040 | ## |
| 3041 | |
| 3042 | # ------------------------------------------------------------------------------ |
| 3043 | |
| 3044 | #Method void clear(SkColor color) |
| 3045 | |
| 3046 | Fill Clip with Color color using SkBlendMode::kSrc. |
| 3047 | This has the effect of replacing all pixels contained by Clip with color. |
| 3048 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3049 | #Param color Unpremultiplied Color_ARGB ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3050 | |
| 3051 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3052 | void draw(SkCanvas* canvas) { |
| 3053 | canvas->save(); |
| 3054 | canvas->clipRect(SkRect::MakeWH(256, 128)); |
| 3055 | canvas->clear(SkColorSetARGB(0x80, 0xFF, 0x00, 0x00)); |
| 3056 | canvas->restore(); |
| 3057 | canvas->save(); |
| 3058 | canvas->clipRect(SkRect::MakeWH(150, 192)); |
| 3059 | canvas->clear(SkColorSetARGB(0x80, 0x00, 0xFF, 0x00)); |
| 3060 | canvas->restore(); |
| 3061 | canvas->clipRect(SkRect::MakeWH(75, 256)); |
| 3062 | canvas->clear(SkColorSetARGB(0x80, 0x00, 0x00, 0xFF)); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3063 | } |
| 3064 | ## |
| 3065 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3066 | #SeeAlso drawColor SkBitmap::erase drawPaint |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3067 | |
| 3068 | ## |
| 3069 | |
| 3070 | # ------------------------------------------------------------------------------ |
| 3071 | |
| 3072 | #Method void discard() |
| 3073 | |
| 3074 | Make Canvas contents undefined. Subsequent calls that read Canvas pixels, |
| 3075 | such as drawing with SkBlendMode, return undefined results. discard() does |
| 3076 | not change Clip or Matrix. |
| 3077 | |
| 3078 | discard() may do nothing, depending on the implementation of Surface or Device |
| 3079 | that created Canvas. |
| 3080 | |
| 3081 | discard() allows optimized performance on subsequent draws by removing |
| 3082 | cached data associated with Surface or Device. |
| 3083 | It is not necessary to call discard() once done with Canvas; |
| 3084 | any cached data is deleted when owning Surface or Device is deleted. |
| 3085 | |
| 3086 | #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] | 3087 | #SeeAlso flush() SkSurface::prepareForExternalIO GrContext::abandonContext |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3088 | |
| 3089 | #NoExample |
| 3090 | ## |
| 3091 | |
| 3092 | ## |
| 3093 | |
| 3094 | # ------------------------------------------------------------------------------ |
| 3095 | |
| 3096 | #Method void drawPaint(const SkPaint& paint) |
| 3097 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3098 | Fill Clip with Paint paint. Paint components Rasterizer, Mask_Filter, Shader, |
| 3099 | Color_Filter, Image_Filter, and Blend_Mode affect drawing; |
| 3100 | Path_Effect in paint is ignored. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3101 | |
| 3102 | # can Path_Effect in paint ever alter drawPaint? |
| 3103 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3104 | #Param paint graphics state used to fill Canvas ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3105 | |
| 3106 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3107 | void draw(SkCanvas* canvas) { |
| 3108 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 3109 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 3110 | SkPaint paint; |
| 3111 | paint.setShader(SkGradientShader::MakeSweep(256, 256, colors, pos, SK_ARRAY_COUNT(colors))); |
| 3112 | canvas->drawPaint(paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3113 | } |
| 3114 | ## |
| 3115 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3116 | #SeeAlso clear drawColor SkBitmap::erase |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3117 | |
| 3118 | ## |
| 3119 | |
| 3120 | # ------------------------------------------------------------------------------ |
| 3121 | |
| 3122 | #Enum PointMode |
| 3123 | |
| 3124 | #Code |
| 3125 | enum PointMode { |
| 3126 | kPoints_PointMode, |
| 3127 | kLines_PointMode, |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 3128 | kPolygon_PointMode, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3129 | }; |
| 3130 | ## |
| 3131 | |
| 3132 | Selects if an array of points are drawn as discrete points, as lines, or as |
| 3133 | an open polygon. |
| 3134 | |
| 3135 | #Const kPoints_PointMode 0 |
| 3136 | Draw each point separately. |
| 3137 | ## |
| 3138 | |
| 3139 | #Const kLines_PointMode 1 |
| 3140 | Draw each pair of points as a line segment. |
| 3141 | ## |
| 3142 | |
| 3143 | #Const kPolygon_PointMode 2 |
| 3144 | Draw the array of points as a open polygon. |
| 3145 | ## |
| 3146 | |
| 3147 | #Example |
| 3148 | #Description |
| 3149 | The upper left corner shows three squares when drawn as points. |
| 3150 | The upper right corner shows one line; when drawn as lines, two points are required per line. |
| 3151 | The lower right corner shows two lines; when draw as polygon, no miter is drawn at the corner. |
| 3152 | The lower left corner shows two lines with a miter when path contains polygon. |
| 3153 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3154 | void draw(SkCanvas* canvas) { |
| 3155 | SkPaint paint; |
| 3156 | paint.setStyle(SkPaint::kStroke_Style); |
| 3157 | paint.setStrokeWidth(10); |
| 3158 | SkPoint points[] = {{64, 32}, {96, 96}, {32, 96}}; |
| 3159 | canvas->drawPoints(SkCanvas::kPoints_PointMode, 3, points, paint); |
| 3160 | canvas->translate(128, 0); |
| 3161 | canvas->drawPoints(SkCanvas::kLines_PointMode, 3, points, paint); |
| 3162 | canvas->translate(0, 128); |
| 3163 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 3, points, paint); |
| 3164 | SkPath path; |
| 3165 | path.addPoly(points, 3, false); |
| 3166 | canvas->translate(-128, 0); |
| 3167 | canvas->drawPath(path, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3168 | } |
| 3169 | ## |
| 3170 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3171 | #SeeAlso drawLine drawPoint drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3172 | |
| 3173 | ## |
| 3174 | |
| 3175 | # ------------------------------------------------------------------------------ |
| 3176 | |
| 3177 | #Method void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) |
| 3178 | |
| 3179 | Draw pts using Clip, Matrix and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3180 | 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] | 3181 | mode may be one of: kPoints_PointMode, kLines_PointMode, or kPolygon_PointMode. |
| 3182 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3183 | If mode is kPoints_PointMode, the shape of point drawn depends on paint |
| 3184 | Paint_Stroke_Cap. If paint is set to SkPaint::kRound_Cap, each point draws a |
| 3185 | circle of diameter Paint_Stroke_Width. If paint is set to SkPaint::kSquare_Cap |
| 3186 | or SkPaint::kButt_Cap, each point draws a square of width and height |
| 3187 | Paint_Stroke_Width. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3188 | |
| 3189 | If mode is kLines_PointMode, each pair of points draws a line segment. |
| 3190 | One line is drawn for every two points; each point is used once. If count is odd, |
| 3191 | the final point is ignored. |
| 3192 | |
| 3193 | If mode is kPolygon_PointMode, each adjacent pair of points draws a line segment. |
| 3194 | count minus one lines are drawn; the first and last point are used once. |
| 3195 | |
| 3196 | Each line segment respects paint Paint_Stroke_Cap and Paint_Stroke_Width. |
| 3197 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3198 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3199 | Always draws each element one at a time; is not affected by |
| 3200 | Paint_Stroke_Join, and unlike drawPath, does not create a mask from all points |
| 3201 | and lines before drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3202 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 3203 | #Param mode whether pts draws points or lines ## |
| 3204 | #Param count number of points in the array ## |
| 3205 | #Param pts array of points to draw ## |
| 3206 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3207 | |
| 3208 | #Example |
| 3209 | #Height 200 |
| 3210 | #Description |
| 3211 | #List |
| 3212 | # The first column draws points. ## |
| 3213 | # The second column draws points as lines. ## |
| 3214 | # The third column draws points as a polygon. ## |
| 3215 | # The fourth column draws points as a polygonal path. ## |
| 3216 | # The first row uses a round cap and round join. ## |
| 3217 | # The second row uses a square cap and a miter join. ## |
| 3218 | # The third row uses a butt cap and a bevel join. ## |
| 3219 | ## |
| 3220 | The transparent color makes multiple line draws visible; |
| 3221 | the path is drawn all at once. |
| 3222 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3223 | void draw(SkCanvas* canvas) { |
| 3224 | SkPaint paint; |
| 3225 | paint.setAntiAlias(true); |
| 3226 | paint.setStyle(SkPaint::kStroke_Style); |
| 3227 | paint.setStrokeWidth(10); |
| 3228 | paint.setColor(0x80349a45); |
| 3229 | const SkPoint points[] = {{32, 16}, {48, 48}, {16, 32}}; |
| 3230 | const SkPaint::Join join[] = { SkPaint::kRound_Join, |
| 3231 | SkPaint::kMiter_Join, |
| 3232 | SkPaint::kBevel_Join }; |
| 3233 | int joinIndex = 0; |
| 3234 | SkPath path; |
| 3235 | path.addPoly(points, 3, false); |
| 3236 | for (const auto cap : { SkPaint::kRound_Cap, SkPaint::kSquare_Cap, SkPaint::kButt_Cap } ) { |
| 3237 | paint.setStrokeCap(cap); |
| 3238 | paint.setStrokeJoin(join[joinIndex++]); |
| 3239 | for (const auto mode : { SkCanvas::kPoints_PointMode, |
| 3240 | SkCanvas::kLines_PointMode, |
| 3241 | SkCanvas::kPolygon_PointMode } ) { |
| 3242 | canvas->drawPoints(mode, 3, points, paint); |
| 3243 | canvas->translate(64, 0); |
| 3244 | } |
| 3245 | canvas->drawPath(path, paint); |
| 3246 | canvas->translate(-192, 64); |
| 3247 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3248 | } |
| 3249 | ## |
| 3250 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3251 | #SeeAlso drawLine drawPoint drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3252 | |
| 3253 | ## |
| 3254 | |
| 3255 | # ------------------------------------------------------------------------------ |
| 3256 | |
| 3257 | #Method void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) |
| 3258 | |
| 3259 | Draw point at (x, y) using Clip, Matrix and Paint paint. |
| 3260 | |
| 3261 | The shape of point drawn depends on paint Paint_Stroke_Cap. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3262 | If paint is set to SkPaint::kRound_Cap, draw a circle of diameter |
| 3263 | 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] | 3264 | draw a square of width and height Paint_Stroke_Width. |
| 3265 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3266 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3267 | #Param x left edge of circle or square ## |
| 3268 | #Param y top edge of circle or square ## |
| 3269 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3270 | |
| 3271 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3272 | void draw(SkCanvas* canvas) { |
| 3273 | SkPaint paint; |
| 3274 | paint.setAntiAlias(true); |
| 3275 | paint.setColor(0x80349a45); |
| 3276 | paint.setStyle(SkPaint::kStroke_Style); |
| 3277 | paint.setStrokeWidth(100); |
| 3278 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 3279 | canvas->scale(1, 1.2f); |
| 3280 | canvas->drawPoint(64, 96, paint); |
| 3281 | canvas->scale(.6f, .8f); |
| 3282 | paint.setColor(SK_ColorWHITE); |
| 3283 | canvas->drawPoint(106, 120, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3284 | } |
| 3285 | ## |
| 3286 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3287 | #SeeAlso drawPoints drawCircle drawRect drawLine drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3288 | |
| 3289 | ## |
| 3290 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3291 | #Method void drawPoint(SkPoint p, const SkPaint& paint) |
| 3292 | |
| 3293 | Draw point p using Clip, Matrix and Paint paint. |
| 3294 | |
| 3295 | The shape of point drawn depends on paint Paint_Stroke_Cap. |
| 3296 | If paint is set to SkPaint::kRound_Cap, draw a circle of diameter |
| 3297 | Paint_Stroke_Width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap, |
| 3298 | draw a square of width and height Paint_Stroke_Width. |
| 3299 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3300 | |
| 3301 | #Param p top-left edge of circle or square ## |
| 3302 | #Param paint stroke, blend, color, and so on, used to draw ## |
| 3303 | |
| 3304 | #Example |
| 3305 | void draw(SkCanvas* canvas) { |
| 3306 | SkPaint paint; |
| 3307 | paint.setAntiAlias(true); |
| 3308 | paint.setColor(0x80349a45); |
| 3309 | paint.setStyle(SkPaint::kStroke_Style); |
| 3310 | paint.setStrokeWidth(100); |
| 3311 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 3312 | canvas->scale(1, 1.2f); |
| 3313 | canvas->drawPoint({64, 96}, paint); |
| 3314 | canvas->scale(.6f, .8f); |
| 3315 | paint.setColor(SK_ColorWHITE); |
| 3316 | canvas->drawPoint(106, 120, paint); |
| 3317 | } |
| 3318 | ## |
| 3319 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3320 | #SeeAlso drawPoints drawCircle drawRect drawLine drawPath |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3321 | |
| 3322 | ## |
| 3323 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3324 | # ------------------------------------------------------------------------------ |
| 3325 | |
| 3326 | #Method void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint) |
| 3327 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3328 | Draws line segment from (x0, y0) to (x1, y1) using Clip, Matrix, and Paint paint. |
| 3329 | In paint: Paint_Stroke_Width describes the line thickness; |
| 3330 | Paint_Stroke_Cap draws the end rounded or square; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3331 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3332 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3333 | #Param x0 start of line segment on x-axis ## |
| 3334 | #Param y0 start of line segment on y-axis ## |
| 3335 | #Param x1 end of line segment on x-axis ## |
| 3336 | #Param y1 end of line segment on y-axis ## |
| 3337 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3338 | |
| 3339 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3340 | SkPaint paint; |
| 3341 | paint.setAntiAlias(true); |
| 3342 | paint.setColor(0xFF9a67be); |
| 3343 | paint.setStrokeWidth(20); |
| 3344 | canvas->skew(1, 0); |
| 3345 | canvas->drawLine(32, 96, 32, 160, paint); |
| 3346 | canvas->skew(-2, 0); |
| 3347 | canvas->drawLine(288, 96, 288, 160, paint); |
| 3348 | ## |
| 3349 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3350 | #SeeAlso drawPoint drawCircle drawRect drawPath |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3351 | |
| 3352 | ## |
| 3353 | |
| 3354 | #Method void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) |
| 3355 | |
| 3356 | Draws line segment from p0 to p1 using Clip, Matrix, and Paint paint. |
| 3357 | In paint: Paint_Stroke_Width describes the line thickness; |
| 3358 | Paint_Stroke_Cap draws the end rounded or square; |
| 3359 | Paint_Style is ignored, as if were set to SkPaint::kStroke_Style. |
| 3360 | |
| 3361 | #Param p0 start of line segment ## |
| 3362 | #Param p1 end of line segment ## |
| 3363 | #Param paint stroke, blend, color, and so on, used to draw ## |
| 3364 | |
| 3365 | #Example |
| 3366 | SkPaint paint; |
| 3367 | paint.setAntiAlias(true); |
| 3368 | paint.setColor(0xFF9a67be); |
| 3369 | paint.setStrokeWidth(20); |
| 3370 | canvas->skew(1, 0); |
| 3371 | canvas->drawLine({32, 96}, {32, 160}, paint); |
| 3372 | canvas->skew(-2, 0); |
| 3373 | canvas->drawLine({288, 96}, {288, 160}, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3374 | ## |
| 3375 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3376 | #SeeAlso drawPoint drawCircle drawRect drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3377 | |
| 3378 | ## |
| 3379 | |
| 3380 | # ------------------------------------------------------------------------------ |
| 3381 | |
| 3382 | #Method void drawRect(const SkRect& rect, const SkPaint& paint) |
| 3383 | |
| 3384 | Draw Rect rect using Clip, Matrix, and Paint paint. |
| 3385 | In paint: Paint_Style determines if rectangle is stroked or filled; |
| 3386 | if stroked, Paint_Stroke_Width describes the line thickness, and |
| 3387 | Paint_Stroke_Join draws the corners rounded or square. |
| 3388 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3389 | #Param rect rectangle to draw ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3390 | #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] | 3391 | |
| 3392 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3393 | void draw(SkCanvas* canvas) { |
| 3394 | SkPoint rectPts[] = { {64, 48}, {192, 160} }; |
| 3395 | SkPaint paint; |
| 3396 | paint.setAntiAlias(true); |
| 3397 | paint.setStyle(SkPaint::kStroke_Style); |
| 3398 | paint.setStrokeWidth(20); |
| 3399 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3400 | SkMatrix rotator; |
| 3401 | rotator.setRotate(30, 128, 128); |
| 3402 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorMAGENTA } ) { |
| 3403 | paint.setColor(color); |
| 3404 | SkRect rect; |
| 3405 | rect.set(rectPts[0], rectPts[1]); |
| 3406 | canvas->drawRect(rect, paint); |
| 3407 | rotator.mapPoints(rectPts, 2); |
| 3408 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3409 | } |
| 3410 | ## |
| 3411 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3412 | #SeeAlso drawIRect drawRRect drawRoundRect drawRegion drawPath drawLine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3413 | |
| 3414 | ## |
| 3415 | |
| 3416 | # ------------------------------------------------------------------------------ |
| 3417 | |
| 3418 | #Method void drawIRect(const SkIRect& rect, const SkPaint& paint) |
| 3419 | |
| 3420 | Draw IRect rect using Clip, Matrix, and Paint paint. |
| 3421 | In paint: Paint_Style determines if rectangle is stroked or filled; |
| 3422 | if stroked, Paint_Stroke_Width describes the line thickness, and |
| 3423 | Paint_Stroke_Join draws the corners rounded or square. |
| 3424 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3425 | #Param rect rectangle to draw ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3426 | #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] | 3427 | |
| 3428 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3429 | SkIRect rect = { 64, 48, 192, 160 }; |
| 3430 | SkPaint paint; |
| 3431 | paint.setAntiAlias(true); |
| 3432 | paint.setStyle(SkPaint::kStroke_Style); |
| 3433 | paint.setStrokeWidth(20); |
| 3434 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3435 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorMAGENTA } ) { |
| 3436 | paint.setColor(color); |
| 3437 | canvas->drawIRect(rect, paint); |
| 3438 | canvas->rotate(30, 128, 128); |
| 3439 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3440 | ## |
| 3441 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3442 | #SeeAlso drawRect drawRRect drawRoundRect drawRegion drawPath drawLine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3443 | |
| 3444 | ## |
| 3445 | |
| 3446 | # ------------------------------------------------------------------------------ |
| 3447 | |
| 3448 | #Method void drawRegion(const SkRegion& region, const SkPaint& paint) |
| 3449 | |
| 3450 | Draw Region region using Clip, Matrix, and Paint paint. |
| 3451 | In paint: Paint_Style determines if rectangle is stroked or filled; |
| 3452 | if stroked, Paint_Stroke_Width describes the line thickness, and |
| 3453 | Paint_Stroke_Join draws the corners rounded or square. |
| 3454 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3455 | #Param region region to draw ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3456 | #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] | 3457 | |
| 3458 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3459 | void draw(SkCanvas* canvas) { |
| 3460 | SkRegion region; |
| 3461 | region.op( 10, 10, 50, 50, SkRegion::kUnion_Op); |
| 3462 | region.op( 10, 50, 90, 90, SkRegion::kUnion_Op); |
| 3463 | SkPaint paint; |
| 3464 | paint.setAntiAlias(true); |
| 3465 | paint.setStyle(SkPaint::kStroke_Style); |
| 3466 | paint.setStrokeWidth(20); |
| 3467 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3468 | canvas->drawRegion(region, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3469 | } |
| 3470 | ## |
| 3471 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3472 | #SeeAlso drawRect drawIRect drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3473 | |
| 3474 | ## |
| 3475 | |
| 3476 | # ------------------------------------------------------------------------------ |
| 3477 | |
| 3478 | #Method void drawOval(const SkRect& oval, const SkPaint& paint) |
| 3479 | |
| 3480 | Draw Oval oval using Clip, Matrix, and Paint. |
| 3481 | In paint: Paint_Style determines if Oval is stroked or filled; |
| 3482 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3483 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3484 | #Param oval Rect bounds of Oval ## |
| 3485 | #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] | 3486 | |
| 3487 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3488 | void draw(SkCanvas* canvas) { |
| 3489 | canvas->clear(0xFF3f5f9f); |
| 3490 | SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0x7f, 0); |
| 3491 | SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) }; |
| 3492 | SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } }; |
| 3493 | SkScalar pos[] = { 0.2f, 1.0f }; |
| 3494 | SkRect bounds = SkRect::MakeWH(80, 70); |
| 3495 | SkPaint paint; |
| 3496 | paint.setAntiAlias(true); |
| 3497 | paint.setShader(SkGradientShader::MakeLinear(g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors), |
| 3498 | SkShader::kClamp_TileMode)); |
| 3499 | canvas->drawOval(bounds , paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3500 | } |
| 3501 | ## |
| 3502 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3503 | #SeeAlso drawCircle drawPoint drawPath drawRRect drawRoundRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3504 | |
| 3505 | ## |
| 3506 | |
| 3507 | # ------------------------------------------------------------------------------ |
| 3508 | |
| 3509 | #Method void drawRRect(const SkRRect& rrect, const SkPaint& paint) |
| 3510 | |
| 3511 | Draw Round_Rect rrect using Clip, Matrix, and Paint paint. |
| 3512 | In paint: Paint_Style determines if rrect is stroked or filled; |
| 3513 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3514 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3515 | rrect may represent a rectangle, circle, oval, uniformly rounded rectangle, or |
| 3516 | 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] | 3517 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3518 | #Param rrect Round_Rect with up to eight corner radii to draw ## |
| 3519 | #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] | 3520 | |
| 3521 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3522 | void draw(SkCanvas* canvas) { |
| 3523 | SkPaint paint; |
| 3524 | paint.setAntiAlias(true); |
| 3525 | SkRect outer = {30, 40, 210, 220}; |
| 3526 | SkRect radii = {30, 50, 70, 90 }; |
| 3527 | SkRRect rRect; |
| 3528 | rRect.setNinePatch(outer, radii.fLeft, radii.fTop, radii.fRight, radii.fBottom); |
| 3529 | canvas->drawRRect(rRect, paint); |
| 3530 | paint.setColor(SK_ColorWHITE); |
| 3531 | canvas->drawLine(outer.fLeft + radii.fLeft, outer.fTop, |
| 3532 | outer.fLeft + radii.fLeft, outer.fBottom, paint); |
| 3533 | canvas->drawLine(outer.fRight - radii.fRight, outer.fTop, |
| 3534 | outer.fRight - radii.fRight, outer.fBottom, paint); |
| 3535 | canvas->drawLine(outer.fLeft, outer.fTop + radii.fTop, |
| 3536 | outer.fRight, outer.fTop + radii.fTop, paint); |
| 3537 | canvas->drawLine(outer.fLeft, outer.fBottom - radii.fBottom, |
| 3538 | outer.fRight, outer.fBottom - radii.fBottom, paint); |
| 3539 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3540 | ## |
| 3541 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3542 | #SeeAlso drawRect drawRoundRect drawDRRect drawCircle drawOval drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3543 | |
| 3544 | ## |
| 3545 | |
| 3546 | # ------------------------------------------------------------------------------ |
| 3547 | |
| 3548 | #Method void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) |
| 3549 | |
| 3550 | Draw Round_Rect outer and inner |
| 3551 | using Clip, Matrix, and Paint paint. |
| 3552 | outer must contain inner or the drawing is undefined. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3553 | In paint: Paint_Style determines if Round_Rect is stroked or filled; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3554 | if stroked, Paint_Stroke_Width describes the line thickness. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3555 | If stroked and Round_Rect corner has zero length radii, Paint_Stroke_Join can |
| 3556 | draw corners rounded or square. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3557 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3558 | GPU-backed platforms optimize drawing when both outer and inner are |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3559 | concave and outer contains inner. These platforms may not be able to draw |
| 3560 | Path built with identical data as fast. |
| 3561 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3562 | #Param outer Round_Rect outer bounds to draw ## |
| 3563 | #Param inner Round_Rect inner bounds to draw ## |
| 3564 | #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] | 3565 | |
| 3566 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3567 | void draw(SkCanvas* canvas) { |
| 3568 | SkRRect outer = SkRRect::MakeRect({20, 40, 210, 200}); |
| 3569 | SkRRect inner = SkRRect::MakeOval({60, 70, 170, 160}); |
| 3570 | SkPaint paint; |
| 3571 | canvas->drawDRRect(outer, inner, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3572 | } |
| 3573 | ## |
| 3574 | |
| 3575 | #Example |
| 3576 | #Description |
| 3577 | Outer Rect has no corner radii, but stroke join is rounded. |
| 3578 | Inner Round_Rect has corner radii; outset stroke increases radii of corners. |
| 3579 | Stroke join does not affect inner Round_Rect since it has no sharp corners. |
| 3580 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3581 | void draw(SkCanvas* canvas) { |
| 3582 | SkRRect outer = SkRRect::MakeRect({20, 40, 210, 200}); |
| 3583 | SkRRect inner = SkRRect::MakeRectXY({60, 70, 170, 160}, 10, 10); |
| 3584 | SkPaint paint; |
| 3585 | paint.setAntiAlias(true); |
| 3586 | paint.setStyle(SkPaint::kStroke_Style); |
| 3587 | paint.setStrokeWidth(20); |
| 3588 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3589 | canvas->drawDRRect(outer, inner, paint); |
| 3590 | paint.setStrokeWidth(1); |
| 3591 | paint.setColor(SK_ColorWHITE); |
| 3592 | canvas->drawDRRect(outer, inner, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3593 | } |
| 3594 | ## |
| 3595 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3596 | #SeeAlso drawRect drawRoundRect drawRRect drawCircle drawOval drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3597 | |
| 3598 | ## |
| 3599 | |
| 3600 | # ------------------------------------------------------------------------------ |
| 3601 | |
| 3602 | #Method void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint) |
| 3603 | |
| 3604 | Draw Circle at (cx, cy) with radius using Clip, Matrix, and Paint paint. |
| 3605 | If radius is zero or less, nothing is drawn. |
| 3606 | In paint: Paint_Style determines if Circle is stroked or filled; |
| 3607 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3608 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3609 | #Param cx Circle center on the x-axis ## |
| 3610 | #Param cy Circle center on the y-axis ## |
| 3611 | #Param radius half the diameter of Circle ## |
| 3612 | #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] | 3613 | |
| 3614 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3615 | void draw(SkCanvas* canvas) { |
| 3616 | SkPaint paint; |
| 3617 | paint.setAntiAlias(true); |
| 3618 | canvas->drawCircle(128, 128, 90, paint); |
| 3619 | paint.setColor(SK_ColorWHITE); |
| 3620 | canvas->drawCircle(86, 86, 20, paint); |
| 3621 | canvas->drawCircle(160, 76, 20, paint); |
| 3622 | canvas->drawCircle(140, 150, 35, paint); |
| 3623 | } |
| 3624 | ## |
| 3625 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3626 | #SeeAlso drawOval drawRRect drawRoundRect drawPath drawArc drawPoint drawLine |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3627 | |
| 3628 | ## |
| 3629 | |
| 3630 | #Method void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) |
| 3631 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3632 | Draw Circle at center with radius using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3633 | If radius is zero or less, nothing is drawn. |
| 3634 | In paint: Paint_Style determines if Circle is stroked or filled; |
| 3635 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3636 | |
| 3637 | #Param center Circle center ## |
| 3638 | #Param radius half the diameter of Circle ## |
| 3639 | #Param paint Paint stroke or fill, blend, color, and so on, used to draw ## |
| 3640 | |
| 3641 | #Example |
| 3642 | void draw(SkCanvas* canvas) { |
| 3643 | SkPaint paint; |
| 3644 | paint.setAntiAlias(true); |
| 3645 | canvas->drawCircle(128, 128, 90, paint); |
| 3646 | paint.setColor(SK_ColorWHITE); |
| 3647 | canvas->drawCircle({86, 86}, 20, paint); |
| 3648 | canvas->drawCircle({160, 76}, 20, paint); |
| 3649 | canvas->drawCircle({140, 150}, 35, paint); |
| 3650 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3651 | ## |
| 3652 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3653 | #SeeAlso drawOval drawRRect drawRoundRect drawPath drawArc drawPoint drawLine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3654 | |
| 3655 | ## |
| 3656 | |
| 3657 | # ------------------------------------------------------------------------------ |
| 3658 | |
| 3659 | #Method void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 3660 | bool useCenter, const SkPaint& paint) |
| 3661 | |
| 3662 | Draw Arc using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3663 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3664 | Arc is part of Oval bounded by oval, sweeping from startAngle to startAngle plus |
| 3665 | sweepAngle. startAngle and sweepAngle are in degrees. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3666 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3667 | startAngle of zero places start point at the right middle edge of oval. |
| 3668 | A positive sweepAngle places Arc end point clockwise from start point; |
| 3669 | a negative sweepAngle places Arc end point counterclockwise from start point. |
| 3670 | sweepAngle may exceed 360 degrees, a full circle. |
| 3671 | If useCenter is true, draw a wedge that includes lines from oval |
| 3672 | center to Arc end points. If useCenter is false, draw Arc between end points. |
| 3673 | |
| 3674 | If Rect oval is empty or sweepAngle is zero, nothing is drawn. |
| 3675 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3676 | #Param oval Rect bounds of Oval containing Arc to draw ## |
| 3677 | #Param startAngle angle in degrees where Arc begins ## |
| 3678 | #Param sweepAngle sweep angle in degrees; positive is clockwise ## |
| 3679 | #Param useCenter if true, include the center of the oval ## |
| 3680 | #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] | 3681 | |
| 3682 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3683 | void draw(SkCanvas* canvas) { |
| 3684 | SkPaint paint; |
| 3685 | paint.setAntiAlias(true); |
| 3686 | SkRect oval = { 4, 4, 60, 60}; |
| 3687 | for (auto useCenter : { false, true } ) { |
| 3688 | for (auto style : { SkPaint::kFill_Style, SkPaint::kStroke_Style } ) { |
| 3689 | paint.setStyle(style); |
| 3690 | for (auto degrees : { 45, 90, 180, 360} ) { |
| 3691 | canvas->drawArc(oval, 0, degrees , useCenter, paint); |
| 3692 | canvas->translate(64, 0); |
| 3693 | } |
| 3694 | canvas->translate(-256, 64); |
| 3695 | } |
| 3696 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3697 | } |
| 3698 | ## |
| 3699 | |
| 3700 | #Example |
| 3701 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3702 | void draw(SkCanvas* canvas) { |
| 3703 | SkPaint paint; |
| 3704 | paint.setAntiAlias(true); |
| 3705 | paint.setStyle(SkPaint::kStroke_Style); |
| 3706 | paint.setStrokeWidth(4); |
| 3707 | SkRect oval = { 4, 4, 60, 60}; |
| 3708 | float intervals[] = { 5, 5 }; |
| 3709 | paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 2.5f)); |
| 3710 | for (auto degrees : { 270, 360, 540, 720 } ) { |
| 3711 | canvas->drawArc(oval, 0, degrees, false, paint); |
| 3712 | canvas->translate(64, 0); |
| 3713 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3714 | } |
| 3715 | ## |
| 3716 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3717 | #SeeAlso SkPath::arcTo drawCircle drawOval drawPath |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3718 | |
| 3719 | ## |
| 3720 | |
| 3721 | # ------------------------------------------------------------------------------ |
| 3722 | |
| 3723 | #Method void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint) |
| 3724 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3725 | Draw Round_Rect bounded by Rect rect, with corner radii (rx, ry) using Clip, |
| 3726 | Matrix, and Paint paint. |
| 3727 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3728 | In paint: Paint_Style determines if Round_Rect is stroked or filled; |
| 3729 | if stroked, Paint_Stroke_Width describes the line thickness. |
| 3730 | If rx or ry are less than zero, they are treated as if they are zero. |
| 3731 | 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] | 3732 | If rx and ry are zero, Round_Rect is drawn as Rect and if stroked is affected by |
| 3733 | Paint_Stroke_Join. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3734 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3735 | #Param rect Rect bounds of Round_Rect to draw ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3736 | #Param rx axis length in x of oval describing rounded corners ## |
| 3737 | #Param ry axis length in y of oval describing rounded corners ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3738 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3739 | |
| 3740 | #Example |
| 3741 | #Description |
| 3742 | Top row has a zero radius a generates a rectangle. |
| 3743 | Second row radii sum to less than sides. |
| 3744 | Third row radii sum equals sides. |
| 3745 | Fourth row radii sum exceeds sides; radii are scaled to fit. |
| 3746 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3747 | void draw(SkCanvas* canvas) { |
| 3748 | SkVector radii[] = { {0, 20}, {10, 10}, {10, 20}, {10, 40} }; |
| 3749 | SkPaint paint; |
| 3750 | paint.setStrokeWidth(15); |
| 3751 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 3752 | paint.setAntiAlias(true); |
| 3753 | for (auto style : { SkPaint::kStroke_Style, SkPaint::kFill_Style } ) { |
| 3754 | paint.setStyle(style ); |
| 3755 | for (size_t i = 0; i < SK_ARRAY_COUNT(radii); ++i) { |
| 3756 | canvas->drawRoundRect({10, 10, 60, 40}, radii[i].fX, radii[i].fY, paint); |
| 3757 | canvas->translate(0, 60); |
| 3758 | } |
| 3759 | canvas->translate(80, -240); |
| 3760 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3761 | } |
| 3762 | ## |
| 3763 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3764 | #SeeAlso DrawRRect drawRect drawDRRect drawPath drawCircle drawOval drawPoint |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3765 | |
| 3766 | ## |
| 3767 | |
| 3768 | # ------------------------------------------------------------------------------ |
| 3769 | |
| 3770 | #Method void drawPath(const SkPath& path, const SkPaint& paint) |
| 3771 | |
| 3772 | Draw Path path using Clip, Matrix, and Paint paint. |
| 3773 | Path contains an array of Path_Contour, each of which may be open or closed. |
| 3774 | |
| 3775 | In paint: Paint_Style determines if Round_Rect is stroked or filled: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3776 | if filled, Path_Fill_Type determines whether Path_Contour describes inside or |
| 3777 | outside of fill; if stroked, Paint_Stroke_Width describes the line thickness, |
| 3778 | Paint_Stroke_Cap describes line ends, and Paint_Stroke_Join describes how |
| 3779 | corners are drawn. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3780 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3781 | #Param path Path to draw ## |
| 3782 | #Param paint stroke, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3783 | |
| 3784 | #Example |
| 3785 | #Description |
| 3786 | Top rows draw stroked path with combinations of joins and caps. The open contour |
| 3787 | is affected by caps; the closed contour is affected by joins. |
| 3788 | Bottom row draws fill the same for open and closed contour. |
| 3789 | First bottom column shows winding fills overlap. |
| 3790 | Second bottom column shows even odd fills exclude overlap. |
| 3791 | Third bottom column shows inverse winding fills area outside both contours. |
| 3792 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3793 | void draw(SkCanvas* canvas) { |
| 3794 | SkPath path; |
| 3795 | path.moveTo(20, 20); |
| 3796 | path.quadTo(60, 20, 60, 60); |
| 3797 | path.close(); |
| 3798 | path.moveTo(60, 20); |
| 3799 | path.quadTo(60, 60, 20, 60); |
| 3800 | SkPaint paint; |
| 3801 | paint.setStrokeWidth(10); |
| 3802 | paint.setAntiAlias(true); |
| 3803 | paint.setStyle(SkPaint::kStroke_Style); |
| 3804 | for (auto join: { SkPaint::kBevel_Join, SkPaint::kRound_Join, SkPaint::kMiter_Join } ) { |
| 3805 | paint.setStrokeJoin(join); |
| 3806 | for (auto cap: { SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap } ) { |
| 3807 | paint.setStrokeCap(cap); |
| 3808 | canvas->drawPath(path, paint); |
| 3809 | canvas->translate(80, 0); |
| 3810 | } |
| 3811 | canvas->translate(-240, 60); |
| 3812 | } |
| 3813 | paint.setStyle(SkPaint::kFill_Style); |
| 3814 | for (auto fill : { SkPath::kWinding_FillType, |
| 3815 | SkPath::kEvenOdd_FillType, |
| 3816 | SkPath::kInverseWinding_FillType } ) { |
| 3817 | path.setFillType(fill); |
| 3818 | canvas->save(); |
| 3819 | canvas->clipRect({0, 10, 80, 70}); |
| 3820 | canvas->drawPath(path, paint); |
| 3821 | canvas->restore(); |
| 3822 | canvas->translate(80, 0); |
| 3823 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3824 | } |
| 3825 | ## |
| 3826 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3827 | #SeeAlso SkPath drawLine drawArc drawRect drawPoints |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3828 | |
| 3829 | ## |
| 3830 | |
| 3831 | # ------------------------------------------------------------------------------ |
| 3832 | #Topic Draw_Image |
| 3833 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3834 | drawImage, drawImageRect, and drawImageNine can be called with a bare pointer or |
| 3835 | 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] | 3836 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 3837 | #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] | 3838 | |
| 3839 | Draw Image image, with its top-left corner at (left, top), |
| 3840 | using Clip, Matrix, and optional Paint paint. |
| 3841 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3842 | If paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, Blend_Mode, |
| 3843 | and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 3844 | If paint contains Mask_Filter, generate mask from image bounds. If generated |
| 3845 | mask extends beyond image bounds, replicate image edge colors, just as Shader |
| 3846 | made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3847 | image edge color when it samples outside of its bounds. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3848 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3849 | #Param image uncompressed rectangular map of pixels ## |
| 3850 | #Param left left side of image ## |
| 3851 | #Param top top side of image ## |
| 3852 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 3853 | and so on; or nullptr |
| 3854 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3855 | |
| 3856 | #Example |
| 3857 | #Height 64 |
| 3858 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3859 | void draw(SkCanvas* canvas) { |
| 3860 | // sk_sp<SkImage> image; |
| 3861 | SkImage* imagePtr = image.get(); |
| 3862 | canvas->drawImage(imagePtr, 0, 0); |
| 3863 | SkPaint paint; |
| 3864 | canvas->drawImage(imagePtr, 80, 0, &paint); |
| 3865 | paint.setAlpha(0x80); |
| 3866 | canvas->drawImage(imagePtr, 160, 0, &paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3867 | } |
| 3868 | ## |
| 3869 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3870 | #SeeAlso drawBitmap drawImageLattice drawImageNine drawImageRect SkPaint::setImageFilter |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3871 | |
| 3872 | ## |
| 3873 | |
| 3874 | # ------------------------------------------------------------------------------ |
| 3875 | |
| 3876 | #Method void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top, |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 3877 | const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3878 | |
| 3879 | Draw Image image, with its top-left corner at (left, top), |
| 3880 | using Clip, Matrix, and optional Paint paint. |
| 3881 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3882 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 3883 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 3884 | If paint contains Mask_Filter, generate mask from image bounds. If generated |
| 3885 | mask extends beyond image bounds, replicate image edge colors, just as Shader |
| 3886 | made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3887 | image edge color when it samples outside of its bounds. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3888 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3889 | #Param image uncompressed rectangular map of pixels ## |
| 3890 | #Param left left side of image ## |
| 3891 | #Param top pop side of image ## |
| 3892 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 3893 | and so on; or nullptr |
| 3894 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3895 | |
| 3896 | #Example |
| 3897 | #Height 64 |
| 3898 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3899 | void draw(SkCanvas* canvas) { |
| 3900 | // sk_sp<SkImage> image; |
| 3901 | canvas->drawImage(image, 0, 0); |
| 3902 | SkPaint paint; |
| 3903 | canvas->drawImage(image, 80, 0, &paint); |
| 3904 | paint.setAlpha(0x80); |
| 3905 | canvas->drawImage(image, 160, 0, &paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3906 | } |
| 3907 | ## |
| 3908 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3909 | #SeeAlso drawBitmap drawImageLattice drawImageNine drawImageRect SkPaint::setImageFilter |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3910 | |
| 3911 | ## |
| 3912 | |
| 3913 | # ------------------------------------------------------------------------------ |
| 3914 | |
| 3915 | #Enum SrcRectConstraint |
| 3916 | |
| 3917 | #Code |
| 3918 | enum SrcRectConstraint { |
| 3919 | kStrict_SrcRectConstraint, |
| 3920 | kFast_SrcRectConstraint, |
| 3921 | }; |
| 3922 | ## |
| 3923 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3924 | SrcRectConstraint controls the behavior at the edge of source Rect, |
| 3925 | provided to drawImageRect, trading off speed for precision. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3926 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3927 | 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] | 3928 | 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] | 3929 | 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] | 3930 | SrcRectConstraint specifies whether an Image_Filter is allowed to read pixels |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3931 | outside source Rect. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3932 | |
| 3933 | #Const kStrict_SrcRectConstraint |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3934 | Requires Image_Filter to respect source Rect, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3935 | sampling only inside of its bounds, possibly with a performance penalty. |
| 3936 | ## |
| 3937 | |
| 3938 | #Const kFast_SrcRectConstraint |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3939 | Permits Image_Filter to sample outside of source Rect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3940 | by half the width of Image_Filter, permitting it to run faster but with |
| 3941 | error at the image edges. |
| 3942 | ## |
| 3943 | |
| 3944 | #Example |
| 3945 | #Height 64 |
| 3946 | #Description |
| 3947 | redBorder contains a black and white checkerboard bordered by red. |
| 3948 | redBorder is drawn scaled by 16 on the left. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 3949 | The middle and right bitmaps are filtered checkerboards. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3950 | Drawing the checkerboard with kStrict_SrcRectConstraint shows only a blur of black and white. |
| 3951 | Drawing the checkerboard with kFast_SrcRectConstraint allows red to bleed in the corners. |
| 3952 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3953 | void draw(SkCanvas* canvas) { |
| 3954 | SkBitmap redBorder; |
| 3955 | redBorder.allocPixels(SkImageInfo::MakeN32Premul(4, 4)); |
| 3956 | SkCanvas checkRed(redBorder); |
| 3957 | checkRed.clear(SK_ColorRED); |
| 3958 | uint32_t checkers[][2] = { { SK_ColorBLACK, SK_ColorWHITE }, |
| 3959 | { SK_ColorWHITE, SK_ColorBLACK } }; |
| 3960 | checkRed.writePixels( |
| 3961 | SkImageInfo::MakeN32Premul(2, 2), (void*) checkers, sizeof(checkers[0]), 1, 1); |
| 3962 | canvas->scale(16, 16); |
| 3963 | canvas->drawBitmap(redBorder, 0, 0, nullptr); |
| 3964 | canvas->resetMatrix(); |
| 3965 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(redBorder); |
| 3966 | SkPaint lowPaint; |
| 3967 | lowPaint.setFilterQuality(kLow_SkFilterQuality); |
| 3968 | for (auto constraint : { SkCanvas::kStrict_SrcRectConstraint, |
| 3969 | SkCanvas::kFast_SrcRectConstraint } ) { |
| 3970 | canvas->translate(80, 0); |
| 3971 | canvas->drawImageRect(image.get(), SkRect::MakeLTRB(1, 1, 3, 3), |
| 3972 | SkRect::MakeLTRB(16, 16, 48, 48), &lowPaint, constraint); |
| 3973 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3974 | } |
| 3975 | ## |
| 3976 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 3977 | #SeeAlso drawImageRect drawImage SkPaint::setImageFilter |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3978 | |
| 3979 | ## |
| 3980 | |
| 3981 | # ------------------------------------------------------------------------------ |
| 3982 | |
| 3983 | #Method void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst, |
| 3984 | const SkPaint* paint, |
| 3985 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 3986 | |
| 3987 | Draw Rect src of Image image, scaled and translated to fill Rect dst. |
| 3988 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 3989 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3990 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 3991 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 3992 | If paint contains Mask_Filter, generate mask from image bounds. |
| 3993 | |
| 3994 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 3995 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 3996 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 3997 | |
| 3998 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
| 3999 | sample within src; set to kFast_SrcRectConstraint allows sampling outside to |
| 4000 | improve performance. |
| 4001 | |
| 4002 | #Param image Image containing pixels, dimensions, and format ## |
| 4003 | #Param src source Rect of image to draw from ## |
| 4004 | #Param dst destination Rect of image to draw to ## |
| 4005 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4006 | and so on; or nullptr |
| 4007 | ## |
| 4008 | #Param constraint filter strictly within src or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4009 | |
| 4010 | #Example |
| 4011 | #Height 64 |
| 4012 | #Description |
| 4013 | The left bitmap draws with Paint default kNone_SkFilterQuality, and stays within |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4014 | its bounds; there is no bleeding with kFast_SrcRectConstraint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4015 | the middle and right bitmaps draw with kLow_SkFilterQuality; with |
| 4016 | kStrict_SrcRectConstraint, the filter remains within the checkerboard, and |
| 4017 | with kFast_SrcRectConstraint red bleeds on the edges. |
| 4018 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4019 | void draw(SkCanvas* canvas) { |
| 4020 | uint32_t pixels[][4] = { |
| 4021 | { 0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000 }, |
| 4022 | { 0xFFFF0000, 0xFF000000, 0xFFFFFFFF, 0xFFFF0000 }, |
| 4023 | { 0xFFFF0000, 0xFFFFFFFF, 0xFF000000, 0xFFFF0000 }, |
| 4024 | { 0xFFFF0000, 0xFFFF0000, 0xFFFF0000, 0xFFFF0000 } }; |
| 4025 | SkBitmap redBorder; |
| 4026 | redBorder.installPixels(SkImageInfo::MakeN32Premul(4, 4), |
| 4027 | (void*) pixels, sizeof(pixels[0])); |
| 4028 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(redBorder); |
| 4029 | SkPaint lowPaint; |
| 4030 | for (auto constraint : { |
| 4031 | SkCanvas::kFast_SrcRectConstraint, |
| 4032 | SkCanvas::kStrict_SrcRectConstraint, |
| 4033 | SkCanvas::kFast_SrcRectConstraint } ) { |
| 4034 | canvas->drawImageRect(image.get(), SkRect::MakeLTRB(1, 1, 3, 3), |
| 4035 | SkRect::MakeLTRB(16, 16, 48, 48), &lowPaint, constraint); |
| 4036 | lowPaint.setFilterQuality(kLow_SkFilterQuality); |
| 4037 | canvas->translate(80, 0); |
| 4038 | } |
| 4039 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4040 | ## |
| 4041 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4042 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4043 | |
| 4044 | ## |
| 4045 | |
| 4046 | # ------------------------------------------------------------------------------ |
| 4047 | |
| 4048 | #Method void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst, |
| 4049 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4050 | |
| 4051 | 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] | 4052 | Note that isrc is on integer pixel boundaries; dst may include fractional |
| 4053 | boundaries. Additionally transform draw using Clip, Matrix, and optional Paint |
| 4054 | paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4055 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4056 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4057 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4058 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4059 | |
| 4060 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4061 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4062 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4063 | |
| 4064 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4065 | sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4066 | improve performance. |
| 4067 | |
| 4068 | #Param image Image containing pixels, dimensions, and format ## |
| 4069 | #Param isrc source IRect of image to draw from ## |
| 4070 | #Param dst destination Rect of image to draw to ## |
| 4071 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4072 | and so on; or nullptr |
| 4073 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4074 | #Param constraint filter strictly within isrc or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4075 | |
| 4076 | #Example |
| 4077 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4078 | void draw(SkCanvas* canvas) { |
| 4079 | // sk_sp<SkImage> image; |
| 4080 | for (auto i : { 1, 2, 4, 8 } ) { |
| 4081 | canvas->drawImageRect(image.get(), SkIRect::MakeLTRB(0, 0, 100, 100), |
| 4082 | SkRect::MakeXYWH(i * 20, i * 20, i * 20, i * 20), nullptr); |
| 4083 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4084 | } |
| 4085 | ## |
| 4086 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4087 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4088 | |
| 4089 | ## |
| 4090 | |
| 4091 | # ------------------------------------------------------------------------------ |
| 4092 | |
| 4093 | #Method void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint, |
| 4094 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4095 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4096 | Draw Image image, scaled and translated to fill Rect dst, using Clip, Matrix, |
| 4097 | and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4098 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4099 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4100 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4101 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4102 | |
| 4103 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4104 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4105 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4106 | |
| 4107 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4108 | sample within image; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4109 | improve performance. |
| 4110 | |
| 4111 | #Param image Image containing pixels, dimensions, and format ## |
| 4112 | #Param dst destination Rect of image to draw to ## |
| 4113 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4114 | and so on; or nullptr |
| 4115 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4116 | #Param constraint filter strictly within image or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4117 | |
| 4118 | #Example |
| 4119 | #Image 4 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4120 | void draw(SkCanvas* canvas) { |
| 4121 | // sk_sp<SkImage> image; |
| 4122 | for (auto i : { 20, 40, 80, 160 } ) { |
| 4123 | canvas->drawImageRect(image.get(), SkRect::MakeXYWH(i, i, i, i), nullptr); |
| 4124 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4125 | } |
| 4126 | ## |
| 4127 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4128 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4129 | |
| 4130 | ## |
| 4131 | |
| 4132 | # ------------------------------------------------------------------------------ |
| 4133 | |
| 4134 | #Method void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst, |
| 4135 | const SkPaint* paint, |
| 4136 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4137 | |
| 4138 | Draw Rect src of Image image, scaled and translated to fill Rect dst. |
| 4139 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4140 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4141 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4142 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4143 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4144 | |
| 4145 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4146 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4147 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4148 | |
| 4149 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
| 4150 | sample within src; set to kFast_SrcRectConstraint allows sampling outside to |
| 4151 | improve performance. |
| 4152 | |
| 4153 | #Param image Image containing pixels, dimensions, and format ## |
| 4154 | #Param src source Rect of image to draw from ## |
| 4155 | #Param dst destination Rect of image to draw to ## |
| 4156 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4157 | and so on; or nullptr |
| 4158 | ## |
| 4159 | #Param constraint filter strictly within src or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4160 | |
| 4161 | #Example |
| 4162 | #Height 64 |
| 4163 | #Description |
| 4164 | Canvas scales and translates; transformation from src to dst also scales. |
| 4165 | The two matrices are concatenated to create the final transformation. |
| 4166 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4167 | void draw(SkCanvas* canvas) { |
| 4168 | uint32_t pixels[][2] = { { SK_ColorBLACK, SK_ColorWHITE }, |
| 4169 | { SK_ColorWHITE, SK_ColorBLACK } }; |
| 4170 | SkBitmap bitmap; |
| 4171 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4172 | (void*) pixels, sizeof(pixels[0])); |
| 4173 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4174 | SkPaint paint; |
| 4175 | canvas->scale(4, 4); |
| 4176 | for (auto alpha : { 50, 100, 150, 255 } ) { |
| 4177 | paint.setAlpha(alpha); |
| 4178 | canvas->drawImageRect(image, SkRect::MakeWH(2, 2), SkRect::MakeWH(8, 8), &paint); |
| 4179 | canvas->translate(8, 0); |
| 4180 | } |
| 4181 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4182 | ## |
| 4183 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4184 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4185 | |
| 4186 | ## |
| 4187 | |
| 4188 | # ------------------------------------------------------------------------------ |
| 4189 | |
| 4190 | #Method void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst, |
| 4191 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4192 | |
| 4193 | 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] | 4194 | isrc is on integer pixel boundaries; dst may include fractional boundaries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4195 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4196 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4197 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4198 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4199 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4200 | |
| 4201 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4202 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4203 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4204 | |
| 4205 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4206 | sample within image; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4207 | improve performance. |
| 4208 | |
| 4209 | #Param image Image containing pixels, dimensions, and format ## |
| 4210 | #Param isrc source IRect of image to draw from ## |
| 4211 | #Param dst destination Rect of image to draw to ## |
| 4212 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4213 | and so on; or nullptr |
| 4214 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4215 | #Param constraint filter strictly within image or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4216 | |
| 4217 | #Example |
| 4218 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4219 | void draw(SkCanvas* canvas) { |
| 4220 | uint32_t pixels[][2] = { { 0x00000000, 0x55555555}, |
| 4221 | { 0xAAAAAAAA, 0xFFFFFFFF} }; |
| 4222 | SkBitmap bitmap; |
| 4223 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4224 | (void*) pixels, sizeof(pixels[0])); |
| 4225 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4226 | SkPaint paint; |
| 4227 | canvas->scale(4, 4); |
| 4228 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN } ) { |
| 4229 | paint.setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kPlus)); |
| 4230 | canvas->drawImageRect(image, SkIRect::MakeWH(2, 2), SkRect::MakeWH(8, 8), &paint); |
| 4231 | canvas->translate(8, 0); |
| 4232 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4233 | } |
| 4234 | ## |
| 4235 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4236 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
| 4237 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4238 | ## |
| 4239 | |
| 4240 | # ------------------------------------------------------------------------------ |
| 4241 | |
| 4242 | #Method void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint, |
| 4243 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4244 | |
| 4245 | Draw Image image, scaled and translated to fill Rect dst, |
| 4246 | using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4247 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4248 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4249 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4250 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4251 | |
| 4252 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4253 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4254 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4255 | |
| 4256 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4257 | sample within image; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4258 | improve performance. |
| 4259 | |
| 4260 | #Param image Image containing pixels, dimensions, and format ## |
| 4261 | #Param dst destination Rect of image to draw to ## |
| 4262 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4263 | and so on; or nullptr |
| 4264 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4265 | #Param constraint filter strictly within image or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4266 | |
| 4267 | #Example |
| 4268 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4269 | void draw(SkCanvas* canvas) { |
| 4270 | uint32_t pixels[][2] = { { 0x00000000, 0x55550000}, |
| 4271 | { 0xAAAA0000, 0xFFFF0000} }; |
| 4272 | SkBitmap bitmap; |
| 4273 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4274 | (void*) pixels, sizeof(pixels[0])); |
| 4275 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4276 | SkPaint paint; |
| 4277 | canvas->scale(4, 4); |
| 4278 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN } ) { |
| 4279 | paint.setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kPlus)); |
| 4280 | canvas->drawImageRect(image, SkRect::MakeWH(8, 8), &paint); |
| 4281 | canvas->translate(8, 0); |
| 4282 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4283 | } |
| 4284 | ## |
| 4285 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4286 | #SeeAlso SrcRectConstraint drawImage drawImageLattice drawImageNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4287 | |
| 4288 | ## |
| 4289 | |
| 4290 | # ------------------------------------------------------------------------------ |
| 4291 | |
| 4292 | #Method void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 4293 | const SkPaint* paint = nullptr) |
| 4294 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4295 | Draw Image image stretched proportionally to fit into Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4296 | 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] | 4297 | the center. Corners are unmodified or scaled down proportionately if their sides |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4298 | 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] | 4299 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4300 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
| 4301 | |
| 4302 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4303 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4304 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4305 | |
| 4306 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4307 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4308 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4309 | |
| 4310 | #Param image Image containing pixels, dimensions, and format ## |
| 4311 | #Param center IRect edge of image corners and sides ## |
| 4312 | #Param dst destination Rect of image to draw to ## |
| 4313 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4314 | and so on; or nullptr |
| 4315 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4316 | |
| 4317 | #Example |
| 4318 | #Height 128 |
| 4319 | #Description |
| 4320 | 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] | 4321 | The second image equals the size of center; only corners are drawn without scaling. |
| 4322 | The remaining images are larger than center. All corners draw without scaling. |
| 4323 | 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] | 4324 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4325 | void draw(SkCanvas* canvas) { |
| 4326 | SkIRect center = { 20, 10, 50, 40 }; |
| 4327 | SkBitmap bitmap; |
| 4328 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4329 | SkCanvas bitCanvas(bitmap); |
| 4330 | SkPaint paint; |
| 4331 | SkColor gray = 0xFF000000; |
| 4332 | int left = 0; |
| 4333 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4334 | int top = 0; |
| 4335 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4336 | paint.setColor(gray); |
| 4337 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4338 | gray += 0x001f1f1f; |
| 4339 | top = bottom; |
| 4340 | } |
| 4341 | left = right; |
| 4342 | } |
| 4343 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4344 | SkImage* imagePtr = image.get(); |
| 4345 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4346 | canvas->drawImageNine(imagePtr, center, SkRect::MakeWH(dest, dest), nullptr); |
| 4347 | canvas->translate(dest + 4, 0); |
| 4348 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4349 | } |
| 4350 | ## |
| 4351 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4352 | #SeeAlso drawImage drawBitmapNine drawImageLattice drawImageRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4353 | |
| 4354 | ## |
| 4355 | |
| 4356 | # ------------------------------------------------------------------------------ |
| 4357 | |
| 4358 | #Method void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst, |
| 4359 | const SkPaint* paint = nullptr) |
| 4360 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4361 | Draw Image image stretched proportionally to fit into Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4362 | 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] | 4363 | 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] | 4364 | 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] | 4365 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4366 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
| 4367 | |
| 4368 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4369 | Blend_Mode, and Draw_Looper. If image is kAlpha_8_SkColorType, apply Shader. |
| 4370 | If paint contains Mask_Filter, generate mask from image bounds. |
| 4371 | |
| 4372 | If generated mask extends beyond image bounds, replicate image edge colors, just |
| 4373 | as Shader made from SkImage::makeShader with SkShader::kClamp_TileMode set |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4374 | replicates the image edge color when it samples outside of its bounds. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4375 | |
| 4376 | #Param image Image containing pixels, dimensions, and format ## |
| 4377 | #Param center IRect edge of image corners and sides ## |
| 4378 | #Param dst destination Rect of image to draw to ## |
| 4379 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4380 | and so on; or nullptr |
| 4381 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4382 | |
| 4383 | #Example |
| 4384 | #Height 128 |
| 4385 | #Description |
| 4386 | The two leftmost images has four corners and sides to the left and right of center. |
| 4387 | The leftmost image scales the width of corners proportionately to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4388 | 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] | 4389 | fill the remaining space. |
| 4390 | The rightmost image has four corners scaled vertically to fit, and uses sides above |
| 4391 | and below center to fill the remaining space. |
| 4392 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4393 | void draw(SkCanvas* canvas) { |
| 4394 | SkIRect center = { 20, 10, 50, 40 }; |
| 4395 | SkBitmap bitmap; |
| 4396 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4397 | SkCanvas bitCanvas(bitmap); |
| 4398 | SkPaint paint; |
| 4399 | SkColor gray = 0xFF000000; |
| 4400 | int left = 0; |
| 4401 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4402 | int top = 0; |
| 4403 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4404 | paint.setColor(gray); |
| 4405 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4406 | gray += 0x001f1f1f; |
| 4407 | top = bottom; |
| 4408 | } |
| 4409 | left = right; |
| 4410 | } |
| 4411 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4412 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4413 | canvas->drawImageNine(image, center, SkRect::MakeWH(dest, 110 - dest), nullptr); |
| 4414 | canvas->translate(dest + 4, 0); |
| 4415 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4416 | } |
| 4417 | ## |
| 4418 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4419 | #SeeAlso drawImage drawBitmapNine drawImageLattice drawImageRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4420 | |
| 4421 | ## |
| 4422 | |
| 4423 | # ------------------------------------------------------------------------------ |
| 4424 | |
| 4425 | #Method void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 4426 | const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4427 | |
| 4428 | Draw Bitmap bitmap, with its top-left corner at (left, top), |
| 4429 | using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4430 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 4431 | 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] | 4432 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4433 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4434 | |
| 4435 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4436 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4437 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4438 | outside of its bounds. |
| 4439 | |
| 4440 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4441 | #Param left left side of bitmap ## |
| 4442 | #Param top top side of bitmap ## |
| 4443 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4444 | and so on; or nullptr |
| 4445 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4446 | |
| 4447 | #Example |
| 4448 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4449 | void draw(SkCanvas* canvas) { |
| 4450 | uint8_t pixels[][8] = { { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00}, |
| 4451 | { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}, |
| 4452 | { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00}, |
| 4453 | { 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF}, |
| 4454 | { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 4455 | { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00}, |
| 4456 | { 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00}, |
| 4457 | { 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF} }; |
| 4458 | SkBitmap bitmap; |
| 4459 | bitmap.installPixels(SkImageInfo::MakeA8(8, 8), |
| 4460 | (void*) pixels, sizeof(pixels[0])); |
| 4461 | SkPaint paint; |
| 4462 | canvas->scale(4, 4); |
| 4463 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xFF007F00} ) { |
| 4464 | paint.setColor(color); |
| 4465 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 4466 | canvas->translate(12, 0); |
| 4467 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4468 | } |
| 4469 | ## |
| 4470 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4471 | #SeeAlso drawImage drawBitmapLattice drawBitmapNine drawBitmapRect SkBitmap::readPixels SkBitmap::writePixels |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4472 | |
| 4473 | ## |
| 4474 | |
| 4475 | # ------------------------------------------------------------------------------ |
| 4476 | |
| 4477 | #Method void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst, |
| 4478 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4479 | |
| 4480 | Draw Rect src of Bitmap bitmap, scaled and translated to fill Rect dst. |
| 4481 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4482 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4483 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4484 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4485 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4486 | |
| 4487 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4488 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4489 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4490 | outside of its bounds. |
| 4491 | |
| 4492 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
| 4493 | sample within src; set to kFast_SrcRectConstraint allows sampling outside to |
| 4494 | improve performance. |
| 4495 | |
| 4496 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4497 | #Param src source Rect of image to draw from ## |
| 4498 | #Param dst destination Rect of image to draw to ## |
| 4499 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4500 | and so on; or nullptr |
| 4501 | ## |
| 4502 | #Param constraint filter strictly within src or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4503 | |
| 4504 | #Example |
| 4505 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4506 | void draw(SkCanvas* canvas) { |
| 4507 | uint8_t pixels[][8] = { { 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00}, |
| 4508 | { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}, |
| 4509 | { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00}, |
| 4510 | { 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF}, |
| 4511 | { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 4512 | { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00}, |
| 4513 | { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00}, |
| 4514 | { 0x00, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00} }; |
| 4515 | SkBitmap bitmap; |
| 4516 | bitmap.installPixels(SkImageInfo::MakeA8(8, 8), |
| 4517 | (void*) pixels, sizeof(pixels[0])); |
| 4518 | SkPaint paint; |
| 4519 | paint.setMaskFilter(SkBlurMaskFilter::Make(kSolid_SkBlurStyle, 6)); |
| 4520 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xFF007F00} ) { |
| 4521 | paint.setColor(color); |
| 4522 | canvas->drawBitmapRect(bitmap, SkRect::MakeWH(8, 8), SkRect::MakeWH(32, 32), &paint); |
| 4523 | canvas->translate(48, 0); |
| 4524 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4525 | } |
| 4526 | ## |
| 4527 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4528 | #SeeAlso drawImageRect drawBitmap drawBitmapLattice drawBitmapNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4529 | |
| 4530 | ## |
| 4531 | |
| 4532 | # ------------------------------------------------------------------------------ |
| 4533 | |
| 4534 | #Method void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst, |
| 4535 | const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4536 | |
| 4537 | 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] | 4538 | isrc is on integer pixel boundaries; dst may include fractional boundaries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4539 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4540 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4541 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4542 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4543 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4544 | |
| 4545 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4546 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4547 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4548 | outside of its bounds. |
| 4549 | |
| 4550 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4551 | sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4552 | improve performance. |
| 4553 | |
| 4554 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4555 | #Param isrc source IRect of image to draw from ## |
| 4556 | #Param dst destination Rect of image to draw to ## |
| 4557 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4558 | and so on; or nullptr |
| 4559 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4560 | #Param constraint sample strictly within isrc, or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4561 | |
| 4562 | #Example |
| 4563 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4564 | void draw(SkCanvas* canvas) { |
| 4565 | uint8_t pixels[][8] = { { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00}, |
| 4566 | { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00}, |
| 4567 | { 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF}, |
| 4568 | { 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF}, |
| 4569 | { 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF}, |
| 4570 | { 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFF}, |
| 4571 | { 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00}, |
| 4572 | { 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00} }; |
| 4573 | SkBitmap bitmap; |
| 4574 | bitmap.installPixels(SkImageInfo::MakeA8(8, 8), |
| 4575 | (void*) pixels, sizeof(pixels[0])); |
| 4576 | SkPaint paint; |
| 4577 | paint.setFilterQuality(kHigh_SkFilterQuality); |
| 4578 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xFF007F00, 0xFF7f007f} ) { |
| 4579 | paint.setColor(color); |
| 4580 | canvas->drawBitmapRect(bitmap, SkIRect::MakeWH(8, 8), SkRect::MakeWH(32, 32), &paint); |
| 4581 | canvas->translate(48.25f, 0); |
| 4582 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4583 | } |
| 4584 | ## |
| 4585 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4586 | #SeeAlso drawImageRect drawBitmap drawBitmapLattice drawBitmapNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4587 | |
| 4588 | ## |
| 4589 | |
| 4590 | # ------------------------------------------------------------------------------ |
| 4591 | |
| 4592 | #Method void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint, |
| 4593 | SrcRectConstraint constraint = kStrict_SrcRectConstraint) |
| 4594 | |
| 4595 | Draw Bitmap bitmap, scaled and translated to fill Rect dst. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4596 | bitmap bounds is on integer pixel boundaries; dst may include fractional boundaries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4597 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4598 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4599 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4600 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4601 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4602 | |
| 4603 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4604 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4605 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4606 | outside of its bounds. |
| 4607 | |
| 4608 | constraint set to kStrict_SrcRectConstraint limits Paint Filter_Quality to |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4609 | sample within bitmap; set to kFast_SrcRectConstraint allows sampling outside to |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4610 | improve performance. |
| 4611 | |
| 4612 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4613 | #Param dst destination Rect of image to draw to ## |
| 4614 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4615 | and so on; or nullptr |
| 4616 | ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4617 | #Param constraint filter strictly within bitmap or draw faster ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4618 | |
| 4619 | #Example |
| 4620 | #Height 64 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4621 | void draw(SkCanvas* canvas) { |
| 4622 | uint32_t pixels[][2] = { { 0x00000000, 0x55550000}, |
| 4623 | { 0xAAAA0000, 0xFFFF0000} }; |
| 4624 | SkBitmap bitmap; |
| 4625 | bitmap.installPixels(SkImageInfo::MakeN32Premul(2, 2), |
| 4626 | (void*) pixels, sizeof(pixels[0])); |
| 4627 | SkPaint paint; |
| 4628 | canvas->scale(4, 4); |
| 4629 | for (auto color : { SK_ColorRED, SK_ColorBLUE, SK_ColorGREEN } ) { |
| 4630 | paint.setColorFilter(SkColorFilter::MakeModeFilter(color, SkBlendMode::kPlus)); |
| 4631 | canvas->drawBitmapRect(bitmap, SkRect::MakeWH(8, 8), &paint); |
| 4632 | canvas->translate(8, 0); |
| 4633 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4634 | } |
| 4635 | ## |
| 4636 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4637 | #SeeAlso drawImageRect drawBitmap drawBitmapLattice drawBitmapNine |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4638 | |
| 4639 | ## |
| 4640 | |
| 4641 | # ------------------------------------------------------------------------------ |
| 4642 | |
| 4643 | #Method void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 4644 | const SkPaint* paint = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4645 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4646 | Draw Bitmap bitmap stretched proportionally to fit into Rect dst. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4647 | IRect center divides the bitmap into nine sections: four sides, four corners, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4648 | 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] | 4649 | sides are larger than dst; center and four sides are scaled to fit remaining |
| 4650 | space, if any. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4651 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4652 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
| 4653 | |
| 4654 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4655 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4656 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4657 | |
| 4658 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4659 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4660 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4661 | outside of its bounds. |
| 4662 | |
| 4663 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4664 | #Param center IRect edge of image corners and sides ## |
| 4665 | #Param dst destination Rect of image to draw to ## |
| 4666 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4667 | and so on; or nullptr |
| 4668 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4669 | |
| 4670 | #Example |
| 4671 | #Height 128 |
| 4672 | #Description |
| 4673 | The two leftmost bitmap draws has four corners and sides to the left and right of center. |
| 4674 | The leftmost bitmap draw scales the width of corners proportionately to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4675 | 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] | 4676 | fill the remaining space. |
| 4677 | The rightmost bitmap draw has four corners scaled vertically to fit, and uses sides above |
| 4678 | and below center to fill the remaining space. |
| 4679 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4680 | void draw(SkCanvas* canvas) { |
| 4681 | SkIRect center = { 20, 10, 50, 40 }; |
| 4682 | SkBitmap bitmap; |
| 4683 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4684 | SkCanvas bitCanvas(bitmap); |
| 4685 | SkPaint paint; |
| 4686 | SkColor gray = 0xFF000000; |
| 4687 | int left = 0; |
| 4688 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4689 | int top = 0; |
| 4690 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4691 | paint.setColor(gray); |
| 4692 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4693 | gray += 0x001f1f1f; |
| 4694 | top = bottom; |
| 4695 | } |
| 4696 | left = right; |
| 4697 | } |
| 4698 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4699 | canvas->drawBitmapNine(bitmap, center, SkRect::MakeWH(dest, 110 - dest), nullptr); |
| 4700 | canvas->translate(dest + 4, 0); |
| 4701 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4702 | } |
| 4703 | ## |
| 4704 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4705 | #SeeAlso drawImageNine drawBitmap drawBitmapLattice drawBitmapRect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4706 | |
| 4707 | ## |
| 4708 | |
| 4709 | # ------------------------------------------------------------------------------ |
| 4710 | #Struct Lattice |
| 4711 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4712 | #Code |
| 4713 | struct Lattice { |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4714 | enum RectType ... |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4715 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4716 | const int* fXDivs; |
| 4717 | const int* fYDivs; |
| 4718 | const RectType* fRectTypes; |
| 4719 | int fXCount; |
| 4720 | int fYCount; |
| 4721 | const SkIRect* fBounds; |
| 4722 | const SkColor* fColors; |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4723 | }; |
| 4724 | ## |
| 4725 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 4726 | Lattice divides Bitmap or Image into a rectangular grid. |
| 4727 | Grid entries on even columns and even rows are fixed; these entries are |
| 4728 | always drawn at their original size if the destination is large enough. |
| 4729 | If the destination side is too small to hold the fixed entries, all fixed |
| 4730 | entries are proportionately scaled down to fit. |
| 4731 | The grid entries not on even columns and rows are scaled to fit the |
| 4732 | remaining space, if any. |
| 4733 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4734 | #Enum RectType |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4735 | #Code |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4736 | enum RectType : uint8_t { |
| 4737 | kDefault = 0, |
| 4738 | kTransparent, |
| 4739 | kFixedColor, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4740 | }; |
| 4741 | ## |
| 4742 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4743 | Optional setting per rectangular grid entry to make it transparent, |
| 4744 | or to fill the grid entry with a color. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4745 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4746 | #Const kDefault 0 |
| 4747 | Draws Bitmap into lattice rectangle. |
| 4748 | ## |
| 4749 | |
| 4750 | #Const kTransparent 1 |
| 4751 | Skips lattice rectangle by making it transparent. |
| 4752 | ## |
| 4753 | |
| 4754 | #Const kFixedColor 2 |
| 4755 | Draws one of fColors into lattice rectangle. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4756 | ## |
| 4757 | ## |
| 4758 | |
| 4759 | #Member const int* fXDivs |
| 4760 | Array of x-coordinates that divide the bitmap vertically. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4761 | Array entries must be unique, increasing, greater than or equal to |
| 4762 | fBounds left edge, and less than fBounds right edge. |
| 4763 | Set the first element to fBounds left to collapse the left column of |
| 4764 | fixed grid entries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4765 | ## |
| 4766 | |
| 4767 | #Member const int* fYDivs |
| 4768 | Array of y-coordinates that divide the bitmap horizontally. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4769 | Array entries must be unique, increasing, greater than or equal to |
| 4770 | fBounds top edge, and less than fBounds bottom edge. |
| 4771 | Set the first element to fBounds top to collapse the top row of fixed |
| 4772 | grid entries. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4773 | ## |
| 4774 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4775 | #Member const RectType* fRectTypes |
| 4776 | Optional array of fill types, one per rectangular grid entry: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4777 | array length must be |
| 4778 | #Formula |
| 4779 | (fXCount + 1) * (fYCount + 1) |
| 4780 | ## |
| 4781 | . |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 4782 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4783 | Each RectType is one of: kDefault, kTransparent, kFixedColor. |
| 4784 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4785 | Array entries correspond to the rectangular grid entries, ascending |
| 4786 | left to right and then top to bottom. |
| 4787 | ## |
| 4788 | |
| 4789 | #Member int fXCount |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4790 | Number of entries in fXDivs array; one less than the number of |
| 4791 | horizontal divisions. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4792 | ## |
| 4793 | |
| 4794 | #Member int fYCount |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4795 | Number of entries in fYDivs array; one less than the number of vertical |
| 4796 | divisions. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4797 | ## |
| 4798 | |
| 4799 | #Member const SkIRect* fBounds |
| 4800 | Optional subset IRect source to draw from. |
| 4801 | If nullptr, source bounds is dimensions of Bitmap or Image. |
| 4802 | ## |
| 4803 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 4804 | #Member const SkColor* fColors |
| 4805 | Optional array of colors, one per rectangular grid entry. |
| 4806 | Array length must be |
| 4807 | #Formula |
| 4808 | (fXCount + 1) * (fYCount + 1) |
| 4809 | ## |
| 4810 | . |
| 4811 | |
| 4812 | Array entries correspond to the rectangular grid entries, ascending |
| 4813 | left to right, then top to bottom. |
| 4814 | ## |
| 4815 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4816 | #Struct Lattice ## |
| 4817 | |
| 4818 | #Method void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst, |
| 4819 | const SkPaint* paint = nullptr) |
| 4820 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4821 | Draw Bitmap bitmap stretched proportionally to fit into Rect dst. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4822 | |
| 4823 | Lattice lattice divides bitmap into a rectangular grid. |
| 4824 | 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] | 4825 | of drawBitmapNine, fixed lattice elements never scale larger than their initial |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4826 | size and shrink proportionately when all fixed elements exceed the bitmap |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4827 | 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] | 4828 | |
| 4829 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4830 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4831 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4832 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4833 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4834 | |
| 4835 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4836 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4837 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4838 | outside of its bounds. |
| 4839 | |
| 4840 | #Param bitmap Bitmap containing pixels, dimensions, and format ## |
| 4841 | #Param lattice division of bitmap into fixed and variable rectangles ## |
| 4842 | #Param dst destination Rect of image to draw to ## |
| 4843 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4844 | and so on; or nullptr |
| 4845 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4846 | |
| 4847 | #Example |
| 4848 | #Height 128 |
| 4849 | #Description |
| 4850 | The two leftmost bitmap draws has four corners and sides to the left and right of center. |
| 4851 | The leftmost bitmap draw scales the width of corners proportionately to fit. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4852 | 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] | 4853 | fill the remaining space; the center is transparent. |
| 4854 | The rightmost bitmap draw has four corners scaled vertically to fit, and uses sides above |
| 4855 | and below center to fill the remaining space. |
| 4856 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4857 | void draw(SkCanvas* canvas) { |
| 4858 | SkIRect center = { 20, 10, 50, 40 }; |
| 4859 | SkBitmap bitmap; |
| 4860 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4861 | SkCanvas bitCanvas(bitmap); |
| 4862 | SkPaint paint; |
| 4863 | SkColor gray = 0xFF000000; |
| 4864 | int left = 0; |
| 4865 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4866 | int top = 0; |
| 4867 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4868 | paint.setColor(gray); |
| 4869 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4870 | gray += 0x001f1f1f; |
| 4871 | top = bottom; |
| 4872 | } |
| 4873 | left = right; |
| 4874 | } |
| 4875 | const int xDivs[] = { center.fLeft, center.fRight }; |
| 4876 | const int yDivs[] = { center.fTop, center.fBottom }; |
Cary Clark | ca3ebcd | 2017-12-12 11:22:38 -0500 | [diff] [blame^] | 4877 | SkCanvas::Lattice::RectType fillTypes[3][3]; |
| 4878 | memset(fillTypes, 0, sizeof(fillTypes)); |
| 4879 | fillTypes[1][1] = SkCanvas::Lattice::kTransparent; |
| 4880 | SkColor dummy[9]; // temporary pending bug fix |
| 4881 | SkCanvas::Lattice lattice = { xDivs, yDivs, fillTypes[0], SK_ARRAY_COUNT(xDivs), |
| 4882 | SK_ARRAY_COUNT(yDivs), nullptr, dummy }; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4883 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
Cary Clark | ca3ebcd | 2017-12-12 11:22:38 -0500 | [diff] [blame^] | 4884 | canvas->drawBitmapLattice(bitmap, lattice, SkRect::MakeWH(dest, 110 - dest), nullptr); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4885 | canvas->translate(dest + 4, 0); |
| 4886 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4887 | } |
| 4888 | ## |
| 4889 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4890 | #SeeAlso drawImageLattice drawBitmap drawBitmapNine Lattice |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4891 | |
| 4892 | ## |
| 4893 | |
| 4894 | # ------------------------------------------------------------------------------ |
| 4895 | |
| 4896 | #Method void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 4897 | const SkPaint* paint = nullptr) |
| 4898 | |
Cary Clark | d0530ba | 2017-09-14 11:25:39 -0400 | [diff] [blame] | 4899 | Draw Image image stretched proportionally to fit into Rect dst. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4900 | |
| 4901 | Lattice lattice divides image into a rectangular grid. |
| 4902 | 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] | 4903 | of drawBitmapNine, fixed lattice elements never scale larger than their initial |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4904 | size and shrink proportionately when all fixed elements exceed the bitmap |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4905 | 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] | 4906 | |
| 4907 | Additionally transform draw using Clip, Matrix, and optional Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4908 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4909 | If Paint paint is supplied, apply Color_Filter, Color_Alpha, Image_Filter, |
| 4910 | Blend_Mode, and Draw_Looper. If bitmap is kAlpha_8_SkColorType, apply Shader. |
| 4911 | If paint contains Mask_Filter, generate mask from bitmap bounds. |
| 4912 | |
| 4913 | If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, |
| 4914 | just as Shader made from SkShader::MakeBitmapShader with |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4915 | SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4916 | outside of its bounds. |
| 4917 | |
| 4918 | #Param image Image containing pixels, dimensions, and format ## |
| 4919 | #Param lattice division of bitmap into fixed and variable rectangles ## |
| 4920 | #Param dst destination Rect of image to draw to ## |
| 4921 | #Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter, |
| 4922 | and so on; or nullptr |
| 4923 | ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4924 | |
| 4925 | #Example |
| 4926 | #Height 128 |
| 4927 | #Description |
| 4928 | 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] | 4929 | The second image equals the size of center; only corners are drawn without scaling. |
| 4930 | 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] | 4931 | are scaled if needed to take up the remaining space; the center is transparent. |
| 4932 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4933 | void draw(SkCanvas* canvas) { |
| 4934 | SkIRect center = { 20, 10, 50, 40 }; |
| 4935 | SkBitmap bitmap; |
| 4936 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(60, 60)); |
| 4937 | SkCanvas bitCanvas(bitmap); |
| 4938 | SkPaint paint; |
| 4939 | SkColor gray = 0xFF000000; |
| 4940 | int left = 0; |
| 4941 | for (auto right: { center.fLeft, center.fRight, bitmap.width() } ) { |
| 4942 | int top = 0; |
| 4943 | for (auto bottom: { center.fTop, center.fBottom, bitmap.height() } ) { |
| 4944 | paint.setColor(gray); |
| 4945 | bitCanvas.drawIRect(SkIRect::MakeLTRB(left, top, right, bottom), paint); |
| 4946 | gray += 0x001f1f1f; |
| 4947 | top = bottom; |
| 4948 | } |
| 4949 | left = right; |
| 4950 | } |
| 4951 | const int xDivs[] = { center.fLeft, center.fRight }; |
| 4952 | const int yDivs[] = { center.fTop, center.fBottom }; |
Cary Clark | ca3ebcd | 2017-12-12 11:22:38 -0500 | [diff] [blame^] | 4953 | SkCanvas::Lattice::RectType fillTypes[3][3]; |
| 4954 | memset(fillTypes, 0, sizeof(fillTypes)); |
| 4955 | fillTypes[1][1] = SkCanvas::Lattice::kTransparent; |
| 4956 | SkColor dummy[9]; // temporary pending bug fix |
| 4957 | SkCanvas::Lattice lattice = { xDivs, yDivs, fillTypes[0], SK_ARRAY_COUNT(xDivs), |
| 4958 | SK_ARRAY_COUNT(yDivs), nullptr, dummy }; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4959 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 4960 | SkImage* imagePtr = image.get(); |
| 4961 | for (auto dest: { 20, 30, 40, 60, 90 } ) { |
| 4962 | canvas->drawImageNine(imagePtr, center, SkRect::MakeWH(dest, dest), nullptr); |
| 4963 | canvas->translate(dest + 4, 0); |
| 4964 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4965 | } |
| 4966 | ## |
| 4967 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 4968 | #SeeAlso drawBitmapLattice drawImage drawImageNine Lattice |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4969 | |
| 4970 | ## |
| 4971 | |
| 4972 | #Topic Draw_Image ## |
| 4973 | |
| 4974 | # ------------------------------------------------------------------------------ |
| 4975 | |
| 4976 | #Method void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 4977 | const SkPaint& paint) |
| 4978 | |
| 4979 | 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] | 4980 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 4981 | 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] | 4982 | UTF-8. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4983 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4984 | 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] | 4985 | 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] | 4986 | and its baseline at y. Text size is affected by Matrix and Paint_Text_Size. |
| 4987 | |
| 4988 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 4989 | 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] | 4990 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4991 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 4992 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 4993 | #Param byteLength byte length of text array ## |
| 4994 | #Param x start of text on x-axis ## |
| 4995 | #Param y start of text on y-axis ## |
| 4996 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 4997 | |
| 4998 | #Example |
| 4999 | #Height 200 |
| 5000 | #Description |
| 5001 | The same text is drawn varying Paint_Text_Size and varying |
| 5002 | Matrix. |
| 5003 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5004 | void draw(SkCanvas* canvas) { |
| 5005 | SkPaint paint; |
| 5006 | paint.setAntiAlias(true); |
| 5007 | float textSizes[] = { 12, 18, 24, 36 }; |
| 5008 | for (auto size: textSizes ) { |
| 5009 | paint.setTextSize(size); |
| 5010 | canvas->drawText("Aa", 2, 10, 20, paint); |
| 5011 | canvas->translate(0, size * 2); |
| 5012 | } |
| 5013 | paint.reset(); |
| 5014 | paint.setAntiAlias(true); |
| 5015 | float yPos = 20; |
| 5016 | for (auto size: textSizes ) { |
| 5017 | float scale = size / 12.f; |
| 5018 | canvas->resetMatrix(); |
| 5019 | canvas->translate(100, 0); |
| 5020 | canvas->scale(scale, scale); |
| 5021 | canvas->drawText("Aa", 2, 10 / scale, yPos / scale, paint); |
| 5022 | yPos += size * 2; |
| 5023 | } |
| 5024 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5025 | ## |
| 5026 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5027 | #SeeAlso drawString drawPosText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5028 | |
| 5029 | ## |
| 5030 | |
| 5031 | #Method void drawString(const char* string, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5032 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5033 | Draw null terminated string, with origin at (x, y), using Clip, Matrix, and |
| 5034 | Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5035 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5036 | string meaning depends on Paint_Text_Encoding; by default, strings are encoded |
| 5037 | 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] | 5038 | results, since zero bytes may be embedded in the string. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5039 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5040 | 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] | 5041 | 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] | 5042 | and its baseline at y. Text size is affected by Matrix and Paint_Text_Size. |
| 5043 | |
| 5044 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5045 | 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] | 5046 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5047 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5048 | #Param string character code points or Glyphs drawn, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5049 | ending with a char value of zero |
| 5050 | ## |
| 5051 | #Param x start of string on x-axis ## |
| 5052 | #Param y start of string on y-axis ## |
| 5053 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5054 | |
| 5055 | #Example |
| 5056 | SkPaint paint; |
| 5057 | canvas->drawString("a small hello", 20, 20, paint); |
| 5058 | ## |
| 5059 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5060 | #SeeAlso drawText drawPosText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5061 | |
| 5062 | ## |
| 5063 | |
| 5064 | #Method void drawString(const SkString& string, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5065 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5066 | Draw null terminated string, with origin at (x, y), using Clip, Matrix, and |
| 5067 | Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5068 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5069 | string meaning depends on Paint_Text_Encoding; by default, strings are encoded |
| 5070 | 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] | 5071 | results, since zero bytes may be embedded in the string. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5072 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5073 | 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] | 5074 | 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] | 5075 | and its baseline at y. Text size is affected by Matrix and Paint_Text_Size. |
| 5076 | |
| 5077 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5078 | 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] | 5079 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5080 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5081 | #Param string character code points or Glyphs drawn, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5082 | ending with a char value of zero |
| 5083 | ## |
| 5084 | #Param x start of string on x-axis ## |
| 5085 | #Param y start of string on y-axis ## |
| 5086 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5087 | |
| 5088 | #Example |
| 5089 | SkPaint paint; |
| 5090 | SkString string("a small hello"); |
| 5091 | canvas->drawString(string, 20, 20, paint); |
| 5092 | ## |
| 5093 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5094 | #SeeAlso drawText drawPosText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5095 | |
| 5096 | ## |
| 5097 | |
| 5098 | # ------------------------------------------------------------------------------ |
| 5099 | |
| 5100 | #Method void drawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 5101 | const SkPaint& paint) |
| 5102 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5103 | 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] | 5104 | 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] | 5105 | described by byteLength of text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5106 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5107 | 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] | 5108 | 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] | 5109 | 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] | 5110 | baseline is positioned at y. Text size is affected by Matrix and |
| 5111 | Paint_Text_Size. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5112 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5113 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5114 | 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] | 5115 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5116 | |
| 5117 | Layout engines such as Harfbuzz typically position each glyph |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5118 | rather than using the font advance widths. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5119 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5120 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5121 | #Param byteLength byte length of text array ## |
| 5122 | #Param pos array of glyph origins ## |
| 5123 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5124 | |
| 5125 | #Example |
| 5126 | #Height 120 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5127 | void draw(SkCanvas* canvas) { |
| 5128 | const char hello[] = "HeLLo!"; |
| 5129 | const SkPoint pos[] = { {40, 100}, {82, 95}, {115, 110}, {130, 95}, {145, 85}, |
| 5130 | {172, 100} }; |
| 5131 | SkPaint paint; |
| 5132 | paint.setTextSize(60); |
| 5133 | canvas->drawPosText(hello, strlen(hello), pos, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5134 | } |
| 5135 | ## |
| 5136 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5137 | #SeeAlso drawText drawPosTextH drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5138 | |
| 5139 | ## |
| 5140 | |
| 5141 | # ------------------------------------------------------------------------------ |
| 5142 | |
| 5143 | #Method void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY, |
| 5144 | const SkPaint& paint) |
| 5145 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5146 | Draw each glyph in text with its (x, y) origin composed from xpos array and |
| 5147 | 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] | 5148 | must match the number of Glyphs described by byteLength of text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5149 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5150 | 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] | 5151 | 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] | 5152 | 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] | 5153 | its baseline is positioned at constY. Text size is affected by Matrix and |
| 5154 | Paint_Text_Size. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5155 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5156 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5157 | 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] | 5158 | filled 12 point black Glyphs. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5159 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5160 | Layout engines such as Harfbuzz typically position each glyph |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5161 | 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] | 5162 | baseline. |
| 5163 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5164 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5165 | #Param byteLength byte length of text array ## |
| 5166 | #Param xpos array of x positions, used to position each glyph ## |
| 5167 | #Param constY shared y coordinate for all of x positions ## |
| 5168 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5169 | |
| 5170 | #Example |
| 5171 | #Height 40 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5172 | void draw(SkCanvas* canvas) { |
| 5173 | SkScalar xpos[] = { 20, 40, 80, 160 }; |
| 5174 | SkPaint paint; |
| 5175 | canvas->drawPosTextH("XXXX", 4, xpos, 20, paint); |
| 5176 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5177 | ## |
| 5178 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5179 | #SeeAlso drawText drawPosText drawTextBlob drawTextOnPath drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5180 | |
| 5181 | ## |
| 5182 | |
| 5183 | # ------------------------------------------------------------------------------ |
| 5184 | |
| 5185 | #Method void drawTextOnPathHV(const void* text, size_t byteLength, const SkPath& path, SkScalar hOffset, |
| 5186 | SkScalar vOffset, const SkPaint& paint) |
| 5187 | |
| 5188 | Draw text on Path path, using Clip, Matrix, and Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5189 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5190 | Origin of text is at distance hOffset along the path, offset by a perpendicular |
| 5191 | vector of length vOffset. If the path section corresponding the glyph advance is |
| 5192 | 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] | 5193 | 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] | 5194 | than the path length, the excess text is clipped. |
| 5195 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5196 | 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] | 5197 | 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] | 5198 | 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] | 5199 | baseline at origin y. Text size is affected by Matrix and Paint_Text_Size. |
| 5200 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5201 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5202 | 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] | 5203 | filled 12 point black Glyphs. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5204 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5205 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5206 | #Param byteLength byte length of text array ## |
| 5207 | #Param path Path providing text baseline ## |
| 5208 | #Param hOffset distance along path to offset origin ## |
| 5209 | #Param vOffset offset of text above (if negative) or below (if positive) the path ## |
| 5210 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5211 | |
| 5212 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5213 | void draw(SkCanvas* canvas) { |
| 5214 | const char aero[] = "correo a" "\xC3" "\xA9" "reo"; |
| 5215 | const size_t len = sizeof(aero) - 1; |
| 5216 | SkPath path; |
| 5217 | path.addOval({43-26, 43-26, 43+26, 43+26}, SkPath::kCW_Direction, 3); |
| 5218 | SkPaint paint; |
| 5219 | paint.setTextSize(24); |
| 5220 | for (auto offset : { 0, 10, 20 } ) { |
| 5221 | canvas->drawTextOnPathHV(aero, len, path, 0, -offset, paint); |
| 5222 | canvas->translate(70 + offset, 70 + offset); |
| 5223 | } |
| 5224 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5225 | ## |
| 5226 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5227 | #SeeAlso drawTextOnPath drawText drawPosTextH drawTextBlob drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5228 | |
| 5229 | ## |
| 5230 | |
| 5231 | # ------------------------------------------------------------------------------ |
| 5232 | |
| 5233 | #Method void drawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 5234 | const SkMatrix* matrix, const SkPaint& paint) |
| 5235 | |
| 5236 | Draw text on Path path, using Clip, Matrix, and Paint paint. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5237 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5238 | Origin of text is at beginning of path offset by matrix, if provided, before it |
| 5239 | is mapped to path. If the path section corresponding the glyph advance is |
| 5240 | 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] | 5241 | 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] | 5242 | than the path length, the excess text is clipped. |
| 5243 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5244 | 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] | 5245 | 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] | 5246 | 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] | 5247 | baseline at origin y. Text size is affected by Matrix and Paint_Text_Size. |
| 5248 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5249 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5250 | 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] | 5251 | filled 12 point black Glyphs. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5252 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5253 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5254 | #Param byteLength byte length of text array ## |
| 5255 | #Param path Path providing text baseline ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5256 | #Param matrix transform of Glyphs before mapping to path; may be nullptr |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 5257 | to use identity Matrix |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5258 | ## |
| 5259 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5260 | |
| 5261 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5262 | void draw(SkCanvas* canvas) { |
| 5263 | const char roller[] = "rollercoaster"; |
| 5264 | const size_t len = sizeof(roller) - 1; |
| 5265 | SkPath path; |
| 5266 | path.cubicTo(40, -80, 120, 80, 160, -40); |
| 5267 | SkPaint paint; |
| 5268 | paint.setTextSize(32); |
| 5269 | paint.setStyle(SkPaint::kStroke_Style); |
| 5270 | SkMatrix matrix; |
| 5271 | matrix.setIdentity(); |
| 5272 | for (int i = 0; i < 3; ++i) { |
| 5273 | canvas->translate(25, 60); |
| 5274 | canvas->drawPath(path, paint); |
| 5275 | canvas->drawTextOnPath(roller, len, path, &matrix, paint); |
| 5276 | matrix.preTranslate(0, 10); |
| 5277 | } |
| 5278 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5279 | ## |
| 5280 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5281 | #SeeAlso drawTextOnPathHV drawText drawPosTextH drawTextBlob drawTextRSXform |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5282 | |
| 5283 | ## |
| 5284 | |
| 5285 | # ------------------------------------------------------------------------------ |
| 5286 | |
| 5287 | #Method void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], |
| 5288 | const SkRect* cullRect, const SkPaint& paint) |
| 5289 | |
| 5290 | Draw text, transforming each glyph by the corresponding SkRSXform, |
| 5291 | using Clip, Matrix, and Paint paint. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5292 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5293 | RSXform array specifies a separate square scale, rotation, and translation for |
| 5294 | each glyph. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5295 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5296 | Optional Rect cullRect is a conservative bounds of text, taking into account |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5297 | 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] | 5298 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5299 | All elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, |
| 5300 | 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] | 5301 | filled 12 point black Glyphs. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5302 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5303 | #Param text character code points or Glyphs drawn ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5304 | #Param byteLength byte length of text array ## |
| 5305 | #Param xform RSXform rotates, scales, and translates each glyph individually ## |
| 5306 | #Param cullRect Rect bounds of text for efficient clipping; or nullptr ## |
| 5307 | #Param paint text size, blend, color, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5308 | |
| 5309 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5310 | void draw(SkCanvas* canvas) { |
| 5311 | const int iterations = 26; |
| 5312 | SkRSXform transforms[iterations]; |
| 5313 | char alphabet[iterations]; |
| 5314 | SkScalar angle = 0; |
| 5315 | SkScalar scale = 1; |
| 5316 | for (size_t i = 0; i < SK_ARRAY_COUNT(transforms); ++i) { |
| 5317 | const SkScalar s = SkScalarSin(angle) * scale; |
| 5318 | const SkScalar c = SkScalarCos(angle) * scale; |
| 5319 | transforms[i] = SkRSXform::Make(-c, -s, -s * 16, c * 16); |
| 5320 | angle += .45; |
| 5321 | scale += .2; |
| 5322 | alphabet[i] = 'A' + i; |
| 5323 | } |
| 5324 | SkPaint paint; |
| 5325 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 5326 | canvas->translate(110, 138); |
| 5327 | canvas->drawTextRSXform(alphabet, sizeof(alphabet), transforms, nullptr, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5328 | } |
| 5329 | ## |
| 5330 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5331 | #SeeAlso drawTextOnPath drawTextOnPathHV drawText drawPosText drawTextBlob |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5332 | |
| 5333 | ## |
| 5334 | |
| 5335 | # ------------------------------------------------------------------------------ |
| 5336 | |
| 5337 | #Method void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5338 | |
| 5339 | 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] | 5340 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5341 | blob contains Glyphs, their positions, and paint attributes specific to text: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5342 | Typeface, Paint_Text_Size, Paint_Text_Scale_X, Paint_Text_Skew_X, |
| 5343 | Paint_Text_Align, Paint_Hinting, Anti-alias, Paint_Fake_Bold, |
| 5344 | Font_Embedded_Bitmaps, Full_Hinting_Spacing, LCD_Text, Linear_Text, |
| 5345 | Subpixel_Text, and Paint_Vertical_Text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5346 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 5347 | Paint_Text_Encoding must be set to SkPaint::kGlyphID_TextEncoding. |
| 5348 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5349 | Elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, Color_Filter, |
| 5350 | Image_Filter, and Draw_Looper; apply to blob. |
| 5351 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5352 | #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] | 5353 | #Param x horizontal offset applied to blob ## |
| 5354 | #Param y vertical offset applied to blob ## |
| 5355 | #Param paint blend, color, stroking, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5356 | |
| 5357 | #Example |
| 5358 | #Height 120 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5359 | void draw(SkCanvas* canvas) { |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 5360 | SkTextBlobBuilder textBlobBuilder; |
| 5361 | const char bunny[] = "/(^x^)\\"; |
| 5362 | const int len = sizeof(bunny) - 1; |
| 5363 | uint16_t glyphs[len]; |
| 5364 | SkPaint paint; |
| 5365 | paint.textToGlyphs(bunny, len, glyphs); |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 5366 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 5367 | int runs[] = { 3, 1, 3 }; |
| 5368 | SkPoint textPos = { 20, 100 }; |
| 5369 | int glyphIndex = 0; |
| 5370 | for (auto runLen : runs) { |
| 5371 | paint.setTextSize(1 == runLen ? 20 : 50); |
| 5372 | const SkTextBlobBuilder::RunBuffer& run = |
| 5373 | textBlobBuilder.allocRun(paint, runLen, textPos.fX, textPos.fY); |
| 5374 | memcpy(run.glyphs, &glyphs[glyphIndex], sizeof(glyphs[0]) * runLen); |
| 5375 | textPos.fX += paint.measureText(&glyphs[glyphIndex], sizeof(glyphs[0]) * runLen, nullptr); |
| 5376 | glyphIndex += runLen; |
| 5377 | } |
| 5378 | sk_sp<const SkTextBlob> blob = textBlobBuilder.make(); |
| 5379 | paint.reset(); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5380 | canvas->drawTextBlob(blob.get(), 0, 0, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5381 | } |
| 5382 | ## |
| 5383 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5384 | #SeeAlso drawText drawPosText drawPosTextH |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5385 | |
| 5386 | ## |
| 5387 | |
| 5388 | # ------------------------------------------------------------------------------ |
| 5389 | |
| 5390 | #Method void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) |
| 5391 | |
| 5392 | 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] | 5393 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5394 | blob contains Glyphs, their positions, and paint attributes specific to text: |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5395 | Typeface, Paint_Text_Size, Paint_Text_Scale_X, Paint_Text_Skew_X, |
| 5396 | Paint_Text_Align, Paint_Hinting, Anti-alias, Paint_Fake_Bold, |
| 5397 | Font_Embedded_Bitmaps, Full_Hinting_Spacing, LCD_Text, Linear_Text, |
| 5398 | Subpixel_Text, and Paint_Vertical_Text. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5399 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 5400 | Paint_Text_Encoding must be set to SkPaint::kGlyphID_TextEncoding. |
| 5401 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5402 | Elements of paint: Path_Effect, Rasterizer, Mask_Filter, Shader, Color_Filter, |
| 5403 | Image_Filter, and Draw_Looper; apply to blob. |
| 5404 | |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5405 | #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] | 5406 | #Param x horizontal offset applied to blob ## |
| 5407 | #Param y vertical offset applied to blob ## |
| 5408 | #Param paint blend, color, stroking, and so on, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5409 | |
| 5410 | #Example |
| 5411 | #Height 120 |
| 5412 | #Description |
| 5413 | Paint attributes unrelated to text, like color, have no effect on paint in allocated Text_Blob. |
| 5414 | Paint attributes related to text, like text size, have no effect on paint passed to drawTextBlob. |
| 5415 | ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5416 | void draw(SkCanvas* canvas) { |
| 5417 | SkTextBlobBuilder textBlobBuilder; |
| 5418 | SkPaint paint; |
| 5419 | paint.setTextSize(50); |
| 5420 | paint.setColor(SK_ColorRED); |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 5421 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5422 | const SkTextBlobBuilder::RunBuffer& run = |
| 5423 | textBlobBuilder.allocRun(paint, 1, 20, 100); |
| 5424 | run.glyphs[0] = 20; |
| 5425 | sk_sp<const SkTextBlob> blob = textBlobBuilder.make(); |
| 5426 | paint.setTextSize(10); |
| 5427 | paint.setColor(SK_ColorBLUE); |
| 5428 | canvas->drawTextBlob(blob.get(), 0, 0, paint); |
| 5429 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5430 | ## |
| 5431 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5432 | #SeeAlso drawText drawPosText drawPosTextH |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5433 | |
| 5434 | ## |
| 5435 | |
| 5436 | # ------------------------------------------------------------------------------ |
| 5437 | |
| 5438 | #Method void drawPicture(const SkPicture* picture) |
| 5439 | |
| 5440 | Draw Picture picture, using Clip and Matrix. |
| 5441 | Clip and Matrix are unchanged by picture contents, as if |
| 5442 | save() was called before and restore() was called after drawPicture. |
| 5443 | |
| 5444 | Picture records a series of draw commands for later playback. |
| 5445 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5446 | #Param picture recorded drawing commands to play ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5447 | |
| 5448 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5449 | void draw(SkCanvas* canvas) { |
| 5450 | SkPictureRecorder recorder; |
| 5451 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5452 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5453 | SkPaint paint; |
| 5454 | paint.setColor(color); |
| 5455 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5456 | recordingCanvas->translate(10, 10); |
| 5457 | recordingCanvas->scale(1.2f, 1.4f); |
| 5458 | } |
| 5459 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5460 | const SkPicture* playbackPtr = playback.get(); |
| 5461 | canvas->drawPicture(playback); |
| 5462 | canvas->scale(2, 2); |
| 5463 | canvas->translate(50, 0); |
| 5464 | canvas->drawPicture(playback); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5465 | } |
| 5466 | ## |
| 5467 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5468 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5469 | |
| 5470 | ## |
| 5471 | |
| 5472 | # ------------------------------------------------------------------------------ |
| 5473 | |
| 5474 | #Method void drawPicture(const sk_sp<SkPicture>& picture) |
| 5475 | |
| 5476 | Draw Picture picture, using Clip and Matrix. |
| 5477 | Clip and Matrix are unchanged by picture contents, as if |
| 5478 | save() was called before and restore() was called after drawPicture. |
| 5479 | |
| 5480 | Picture records a series of draw commands for later playback. |
| 5481 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5482 | #Param picture recorded drawing commands to play ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5483 | |
| 5484 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5485 | void draw(SkCanvas* canvas) { |
| 5486 | SkPictureRecorder recorder; |
| 5487 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5488 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5489 | SkPaint paint; |
| 5490 | paint.setColor(color); |
| 5491 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5492 | recordingCanvas->translate(10, 10); |
| 5493 | recordingCanvas->scale(1.2f, 1.4f); |
| 5494 | } |
| 5495 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5496 | canvas->drawPicture(playback); |
| 5497 | canvas->scale(2, 2); |
| 5498 | canvas->translate(50, 0); |
| 5499 | canvas->drawPicture(playback); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5500 | } |
| 5501 | ## |
| 5502 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5503 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5504 | |
| 5505 | ## |
| 5506 | |
| 5507 | # ------------------------------------------------------------------------------ |
| 5508 | |
| 5509 | #Method void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) |
| 5510 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5511 | Draw Picture picture, using Clip and Matrix; transforming picture with |
| 5512 | Matrix matrix, if provided; and use Paint paint Color_Alpha, Color_Filter, |
| 5513 | Image_Filter, and Blend_Mode, if provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5514 | |
| 5515 | matrix transformation is equivalent to: save(), concat(), drawPicture, restore(). |
| 5516 | paint use is equivalent to: saveLayer, drawPicture, restore(). |
| 5517 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5518 | #Param picture recorded drawing commands to play ## |
| 5519 | #Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ## |
| 5520 | #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] | 5521 | |
| 5522 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5523 | void draw(SkCanvas* canvas) { |
| 5524 | SkPaint paint; |
| 5525 | SkPictureRecorder recorder; |
| 5526 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5527 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5528 | paint.setColor(color); |
| 5529 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5530 | recordingCanvas->translate(10, 10); |
| 5531 | recordingCanvas->scale(1.2f, 1.4f); |
| 5532 | } |
| 5533 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5534 | const SkPicture* playbackPtr = playback.get(); |
| 5535 | SkMatrix matrix; |
| 5536 | matrix.reset(); |
| 5537 | for (auto alpha : { 70, 140, 210 } ) { |
| 5538 | paint.setAlpha(alpha); |
| 5539 | canvas->drawPicture(playbackPtr, &matrix, &paint); |
| 5540 | matrix.preTranslate(70, 70); |
| 5541 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5542 | } |
| 5543 | ## |
| 5544 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5545 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5546 | |
| 5547 | ## |
| 5548 | |
| 5549 | # ------------------------------------------------------------------------------ |
| 5550 | |
| 5551 | #Method void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, const SkPaint* paint) |
| 5552 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5553 | Draw Picture picture, using Clip and Matrix; transforming picture with |
| 5554 | Matrix matrix, if provided; and use Paint paint Color_Alpha, Color_Filter, |
| 5555 | Image_Filter, and Blend_Mode, if provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5556 | |
| 5557 | matrix transformation is equivalent to: save(), concat(), drawPicture, restore(). |
| 5558 | paint use is equivalent to: saveLayer, drawPicture, restore(). |
| 5559 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5560 | #Param picture recorded drawing commands to play ## |
| 5561 | #Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ## |
| 5562 | #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] | 5563 | |
| 5564 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5565 | void draw(SkCanvas* canvas) { |
| 5566 | SkPaint paint; |
| 5567 | SkPictureRecorder recorder; |
| 5568 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 5569 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 5570 | paint.setColor(color); |
| 5571 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 5572 | recordingCanvas->translate(10, 10); |
| 5573 | recordingCanvas->scale(1.2f, 1.4f); |
| 5574 | } |
| 5575 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 5576 | SkMatrix matrix; |
| 5577 | matrix.reset(); |
| 5578 | for (auto alpha : { 70, 140, 210 } ) { |
| 5579 | paint.setAlpha(alpha); |
| 5580 | canvas->drawPicture(playback, &matrix, &paint); |
| 5581 | matrix.preTranslate(70, 70); |
| 5582 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5583 | } |
| 5584 | ## |
| 5585 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5586 | #SeeAlso drawDrawable SkPicture SkPicture::playback |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5587 | |
| 5588 | ## |
| 5589 | |
| 5590 | # ------------------------------------------------------------------------------ |
| 5591 | |
| 5592 | #Method void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) |
| 5593 | |
| 5594 | Draw Vertices vertices, a triangle mesh, using Clip and Matrix. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5595 | If Vertices_Texs and Vertices_Colors are defined in vertices, and Paint paint |
| 5596 | contains Shader, Blend_Mode mode combines Vertices_Colors with Shader. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5597 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5598 | #Param vertices triangle mesh to draw ## |
| 5599 | #Param mode combines Vertices_Colors with Shader, if both are present ## |
| 5600 | #Param paint specifies the Shader, used as Vertices texture; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5601 | |
| 5602 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5603 | void draw(SkCanvas* canvas) { |
| 5604 | SkPaint paint; |
| 5605 | SkPoint points[] = { { 0, 0 }, { 250, 0 }, { 100, 100 }, { 0, 250 } }; |
| 5606 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN }; |
| 5607 | auto vertices = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, |
| 5608 | SK_ARRAY_COUNT(points), points, nullptr, colors); |
| 5609 | canvas->drawVertices(vertices.get(), SkBlendMode::kSrc, paint); |
| 5610 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5611 | ## |
| 5612 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5613 | #SeeAlso drawPatch drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5614 | |
| 5615 | ## |
| 5616 | |
| 5617 | # ------------------------------------------------------------------------------ |
| 5618 | |
| 5619 | #Method void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint) |
| 5620 | |
| 5621 | Draw Vertices vertices, a triangle mesh, using Clip and Matrix. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5622 | If Vertices_Texs and Vertices_Colors are defined in vertices, and Paint paint |
| 5623 | contains Shader, Blend_Mode mode combines Vertices_Colors with Shader. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5624 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5625 | #Param vertices triangle mesh to draw ## |
| 5626 | #Param mode combines Vertices_Colors with Shader, if both are present ## |
| 5627 | #Param paint specifies the Shader, used as Vertices texture, may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5628 | |
| 5629 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5630 | void draw(SkCanvas* canvas) { |
| 5631 | SkPaint paint; |
| 5632 | SkPoint points[] = { { 0, 0 }, { 250, 0 }, { 100, 100 }, { 0, 250 } }; |
| 5633 | SkPoint texs[] = { { 0, 0 }, { 0, 250 }, { 250, 250 }, { 250, 0 } }; |
| 5634 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN }; |
| 5635 | paint.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 4, |
| 5636 | SkShader::kClamp_TileMode)); |
| 5637 | auto vertices = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode, |
| 5638 | SK_ARRAY_COUNT(points), points, texs, colors); |
| 5639 | canvas->drawVertices(vertices.get(), SkBlendMode::kDarken, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5640 | } |
| 5641 | ## |
| 5642 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5643 | #SeeAlso drawPatch drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5644 | |
| 5645 | ## |
| 5646 | |
| 5647 | # ------------------------------------------------------------------------------ |
| 5648 | |
| 5649 | #Method void drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 5650 | const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint) |
| 5651 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5652 | Draws a Coons_Patch: the interpolation of four cubics with shared corners, |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5653 | associating a color, and optionally a texture coordinate, with each corner. |
| 5654 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5655 | Coons_Patch uses Clip and Matrix, paint Shader, Color_Filter, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5656 | 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] | 5657 | 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] | 5658 | both are provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5659 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5660 | Point array cubics specifies four Cubics starting at the top-left corner, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5661 | 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] | 5662 | first point. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5663 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5664 | Color array color associates colors with corners in top-left, top-right, |
| 5665 | bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5666 | |
| 5667 | If paint contains Shader, Point array texCoords maps Shader as texture to |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5668 | corners in top-left, top-right, bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5669 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 5670 | #Param cubics Path_Cubic array, sharing common points ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5671 | #Param colors Color array, one for each corner ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5672 | #Param texCoords Point array of texture coordinates, mapping Shader to corners; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5673 | may be nullptr |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 5674 | #Param ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5675 | #Param mode Blend_Mode for colors, and for Shader if paint has one ## |
| 5676 | #Param paint Shader, Color_Filter, Blend_Mode, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5677 | |
| 5678 | #Example |
| 5679 | #Image 5 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5680 | void draw(SkCanvas* canvas) { |
| 5681 | // SkBitmap source = cmbkygk; |
| 5682 | SkPaint paint; |
| 5683 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 5684 | paint.setAntiAlias(true); |
| 5685 | SkPoint cubics[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 5686 | /* { 7, 3 }, */ { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 5687 | /* { 5, 7 }, */ { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 5688 | /* { 1, 5 }, */ { 2, 4 }, { 1, 3 }, /* { 3, 1 } */ }; |
| 5689 | SkColor colors[] = { 0xbfff0000, 0xbf0000ff, 0xbfff00ff, 0xbf00ffff }; |
| 5690 | SkPoint texCoords[] = { { -30, -30 }, { 162, -30}, { 162, 162}, { -30, 162} }; |
| 5691 | paint.setShader(SkShader::MakeBitmapShader(source, SkShader::kClamp_TileMode, |
| 5692 | SkShader::kClamp_TileMode, nullptr)); |
| 5693 | canvas->scale(15, 15); |
| 5694 | for (auto blend : { SkBlendMode::kSrcOver, SkBlendMode::kModulate, SkBlendMode::kXor } ) { |
| 5695 | canvas->drawPatch(cubics, colors, texCoords, blend, paint); |
| 5696 | canvas->translate(4, 4); |
| 5697 | } |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5698 | } |
| 5699 | ## |
| 5700 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5701 | #ToDo can patch use image filter? ## |
| 5702 | #SeeAlso SeeAlso drawVertices drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5703 | |
| 5704 | ## |
| 5705 | |
| 5706 | # ------------------------------------------------------------------------------ |
| 5707 | |
| 5708 | #Method void drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 5709 | const SkPoint texCoords[4], const SkPaint& paint) |
| 5710 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5711 | Draws Cubic Coons_Patch: the interpolation of four cubics with shared corners, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5712 | associating a color, and optionally a texture coordinate, with each corner. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5713 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 5714 | Coons_Patch uses Clip and Matrix, paint Shader, Color_Filter, |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5715 | 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] | 5716 | 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] | 5717 | both are provided. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5718 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5719 | Point array cubics specifies four Cubics starting at the top-left corner, |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5720 | 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] | 5721 | first point. |
| 5722 | |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5723 | Color array color associates colors with corners in top-left, top-right, |
| 5724 | bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5725 | |
| 5726 | If paint contains Shader, Point array texCoords maps Shader as texture to |
Cary Clark | bc5697d | 2017-10-04 14:31:33 -0400 | [diff] [blame] | 5727 | corners in top-left, top-right, bottom-right, bottom-left order. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5728 | |
Cary Clark | a523d2d | 2017-08-30 08:58:10 -0400 | [diff] [blame] | 5729 | #Param cubics Path_Cubic array, sharing common points ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5730 | #Param colors Color array, one for each corner ## |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5731 | #Param texCoords Point array of texture coordinates, mapping Shader to corners; |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5732 | may be nullptr |
Cary Clark | 579985c | 2017-07-31 11:48:27 -0400 | [diff] [blame] | 5733 | #Param ## |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5734 | #Param paint Shader, Color_Filter, Blend_Mode, used to draw ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5735 | |
| 5736 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5737 | void draw(SkCanvas* canvas) { |
| 5738 | SkPaint paint; |
| 5739 | paint.setAntiAlias(true); |
| 5740 | SkPoint cubics[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 5741 | /* { 7, 3 }, */ { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 5742 | /* { 5, 7 }, */ { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 5743 | /* { 1, 5 }, */ { 2, 4 }, { 1, 3 }, /* { 3, 1 } */ }; |
| 5744 | SkColor colors[] = { SK_ColorRED, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN }; |
| 5745 | canvas->scale(30, 30); |
| 5746 | canvas->drawPatch(cubics, colors, nullptr, paint); |
| 5747 | SkPoint text[] = { {3,0.9f}, {4,2.5f}, {5,0.9f}, {7.5f,3.2f}, {5.5f,4.2f}, |
| 5748 | {7.5f,5.2f}, {5,7.5f}, {4,5.9f}, {3,7.5f}, {0.5f,5.2f}, {2.5f,4.2f}, |
| 5749 | {0.5f,3.2f} }; |
| 5750 | paint.setTextSize(18.f / 30); |
| 5751 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 5752 | for (int i = 0; i< 10; ++i) { |
| 5753 | char digit = '0' + i; |
| 5754 | canvas->drawText(&digit, 1, text[i].fX, text[i].fY, paint); |
| 5755 | } |
| 5756 | canvas->drawString("10", text[10].fX, text[10].fY, paint); |
| 5757 | canvas->drawString("11", text[11].fX, text[11].fY, paint); |
| 5758 | paint.setStyle(SkPaint::kStroke_Style); |
| 5759 | canvas->drawPoints(SkCanvas::kPolygon_PointMode, 12, cubics, paint); |
| 5760 | 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] | 5761 | } |
| 5762 | ## |
| 5763 | |
| 5764 | #Example |
| 5765 | #Image 6 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5766 | void draw(SkCanvas* canvas) { |
| 5767 | // SkBitmap source = checkerboard; |
| 5768 | SkPaint paint; |
| 5769 | paint.setFilterQuality(kLow_SkFilterQuality); |
| 5770 | paint.setAntiAlias(true); |
| 5771 | SkPoint cubics[] = { { 3, 1 }, { 4, 2 }, { 5, 1 }, { 7, 3 }, |
| 5772 | /* { 7, 3 }, */ { 6, 4 }, { 7, 5 }, { 5, 7 }, |
| 5773 | /* { 5, 7 }, */ { 4, 6 }, { 3, 7 }, { 1, 5 }, |
| 5774 | /* { 1, 5 }, */ { 2, 4 }, { 1, 3 }, /* { 3, 1 } */ }; |
| 5775 | SkPoint texCoords[] = { { 0, 0 }, { 0, 62}, { 62, 62}, { 62, 0 } }; |
| 5776 | paint.setShader(SkShader::MakeBitmapShader(source, SkShader::kClamp_TileMode, |
| 5777 | SkShader::kClamp_TileMode, nullptr)); |
| 5778 | canvas->scale(30, 30); |
| 5779 | canvas->drawPatch(cubics, nullptr, texCoords, paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5780 | } |
| 5781 | ## |
| 5782 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5783 | #ToDo can patch use image filter? ## |
| 5784 | #SeeAlso SeeAlso drawVertices drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5785 | |
| 5786 | ## |
| 5787 | |
| 5788 | # ------------------------------------------------------------------------------ |
| 5789 | |
| 5790 | #Method void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
| 5791 | const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect, |
| 5792 | const SkPaint* paint) |
| 5793 | |
| 5794 | 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] | 5795 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5796 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5797 | atlas, and RSXform xform transforms it into destination space. |
| 5798 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5799 | xform, text, and colors if present, must contain count entries. |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5800 | Optional colors are applied for each sprite using Blend_Mode. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5801 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5802 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5803 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5804 | #Param atlas Image containing sprites ## |
| 5805 | #Param xform RSXform mappings for sprites in atlas ## |
| 5806 | #Param tex Rect locations of sprites in atlas ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5807 | #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] | 5808 | #Param count number of sprites to draw ## |
| 5809 | #Param mode Blend_Mode combining colors and sprites ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5810 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5811 | #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] | 5812 | |
| 5813 | #Example |
| 5814 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5815 | void draw(SkCanvas* canvas) { |
| 5816 | // SkBitmap source = mandrill; |
| 5817 | SkRSXform xforms[] = { { .5f, 0, 0, 0 }, {0, .5f, 200, 100 } }; |
| 5818 | SkRect tex[] = { { 0, 0, 250, 250 }, { 0, 0, 250, 250 } }; |
| 5819 | SkColor colors[] = { 0x7f55aa00, 0x7f3333bf }; |
| 5820 | const SkImage* imagePtr = image.get(); |
| 5821 | canvas->drawAtlas(imagePtr, xforms, tex, colors, 2, SkBlendMode::kSrcOver, nullptr, nullptr); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5822 | } |
| 5823 | ## |
| 5824 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5825 | #SeeAlso drawBitmap drawImage |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5826 | |
| 5827 | ## |
| 5828 | |
| 5829 | # ------------------------------------------------------------------------------ |
| 5830 | |
| 5831 | #Method void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[], |
| 5832 | const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect, |
| 5833 | const SkPaint* paint) |
| 5834 | |
| 5835 | 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] | 5836 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5837 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5838 | atlas, and RSXform xform transforms it into destination space. |
| 5839 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5840 | xform, text, and colors if present, must contain count entries. |
| 5841 | Optional colors is applied for each sprite using Blend_Mode. |
| 5842 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5843 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5844 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5845 | #Param atlas Image containing sprites ## |
| 5846 | #Param xform RSXform mappings for sprites in atlas ## |
| 5847 | #Param tex Rect locations of sprites in atlas ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5848 | #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] | 5849 | #Param count number of sprites to draw ## |
| 5850 | #Param mode Blend_Mode combining colors and sprites ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5851 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5852 | #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] | 5853 | |
| 5854 | #Example |
| 5855 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5856 | void draw(SkCanvas* canvas) { |
| 5857 | // SkBitmap source = mandrill; |
| 5858 | SkRSXform xforms[] = { { .5f, 0, 0, 0 }, {0, .5f, 200, 100 } }; |
| 5859 | SkRect tex[] = { { 0, 0, 250, 250 }, { 0, 0, 250, 250 } }; |
| 5860 | SkColor colors[] = { 0x7f55aa00, 0x7f3333bf }; |
| 5861 | SkPaint paint; |
| 5862 | paint.setAlpha(127); |
| 5863 | canvas->drawAtlas(image, xforms, tex, colors, 2, SkBlendMode::kPlus, nullptr, &paint); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5864 | } |
| 5865 | ## |
| 5866 | |
| 5867 | #ToDo bug in example on cpu side, gpu looks ok ## |
| 5868 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5869 | #SeeAlso drawBitmap drawImage |
| 5870 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5871 | ## |
| 5872 | |
| 5873 | # ------------------------------------------------------------------------------ |
| 5874 | |
| 5875 | #Method void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count, |
| 5876 | const SkRect* cullRect, const SkPaint* paint) |
| 5877 | |
| 5878 | 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] | 5879 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5880 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5881 | atlas, and RSXform xform transforms it into destination space. |
| 5882 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5883 | xform and text must contain count entries. |
| 5884 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5885 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5886 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5887 | #Param atlas Image containing sprites ## |
| 5888 | #Param xform RSXform mappings for sprites in atlas ## |
| 5889 | #Param tex Rect locations of sprites in atlas ## |
| 5890 | #Param count number of sprites to draw ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5891 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5892 | #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] | 5893 | |
| 5894 | #Example |
| 5895 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5896 | void draw(SkCanvas* canvas) { |
| 5897 | // sk_sp<SkImage> image = mandrill; |
| 5898 | SkRSXform xforms[] = { { .5f, 0, 0, 0 }, {0, .5f, 200, 100 } }; |
| 5899 | SkRect tex[] = { { 0, 0, 250, 250 }, { 0, 0, 250, 250 } }; |
| 5900 | const SkImage* imagePtr = image.get(); |
| 5901 | canvas->drawAtlas(imagePtr, xforms, tex, 2, nullptr, nullptr); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5902 | } |
| 5903 | ## |
| 5904 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5905 | #SeeAlso drawBitmap drawImage |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5906 | |
| 5907 | ## |
| 5908 | |
| 5909 | # ------------------------------------------------------------------------------ |
| 5910 | |
| 5911 | #Method void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[], |
| 5912 | int count, const SkRect* cullRect, const SkPaint* paint) |
| 5913 | |
| 5914 | 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] | 5915 | paint uses Anti-alias, Color_Alpha, Color_Filter, Image_Filter, and Blend_Mode |
| 5916 | to draw, if present. For each entry in the array, Rect tex locates sprite in |
| 5917 | atlas, and RSXform xform transforms it into destination space. |
| 5918 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5919 | xform and text must contain count entries. |
| 5920 | Optional cullRect is a conservative bounds of all transformed sprites. |
Cary Clark | ce10124 | 2017-09-01 15:51:02 -0400 | [diff] [blame] | 5921 | If cullRect is outside of Clip, canvas can skip drawing. |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5922 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5923 | #Param atlas Image containing sprites ## |
| 5924 | #Param xform RSXform mappings for sprites in atlas ## |
| 5925 | #Param tex Rect locations of sprites in atlas ## |
| 5926 | #Param count number of sprites to draw ## |
Cary Clark | 6fc5041 | 2017-09-21 12:31:06 -0400 | [diff] [blame] | 5927 | #Param cullRect bounds of transformed sprites for efficient clipping; may be nullptr ## |
| 5928 | #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] | 5929 | |
| 5930 | #Example |
| 5931 | #Image 3 |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5932 | void draw(SkCanvas* canvas) { |
| 5933 | // sk_sp<SkImage> image = mandrill; |
| 5934 | SkRSXform xforms[] = { { 1, 0, 0, 0 }, {0, 1, 300, 100 } }; |
| 5935 | SkRect tex[] = { { 0, 0, 200, 200 }, { 200, 0, 400, 200 } }; |
| 5936 | canvas->drawAtlas(image, xforms, tex, 2, nullptr, nullptr); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5937 | } |
| 5938 | ## |
| 5939 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5940 | #SeeAlso drawBitmap drawImage |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5941 | |
| 5942 | ## |
| 5943 | |
| 5944 | # ------------------------------------------------------------------------------ |
| 5945 | |
Cary Clark | 73fa972 | 2017-08-29 17:36:51 -0400 | [diff] [blame] | 5946 | #Method void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = nullptr) |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5947 | |
| 5948 | Draw Drawable drawable using Clip and Matrix, concatenated with |
| 5949 | optional matrix. |
| 5950 | |
| 5951 | If Canvas has an asynchronous implementation, as is the case |
| 5952 | when it is recording into Picture, then drawable will be referenced, |
| 5953 | so that SkDrawable::draw() can be called when the operation is finalized. To force |
| 5954 | immediate drawing, call SkDrawable::draw() instead. |
| 5955 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5956 | #Param drawable custom struct encapsulating drawing commands ## |
| 5957 | #Param matrix transformation applied to drawing; may be nullptr ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5958 | |
| 5959 | #Example |
| 5960 | #Height 100 |
| 5961 | #Function |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 5962 | struct MyDrawable : public SkDrawable { |
| 5963 | SkRect onGetBounds() override { return SkRect::MakeWH(50, 100); } |
| 5964 | |
| 5965 | void onDraw(SkCanvas* canvas) override { |
| 5966 | SkPath path; |
| 5967 | path.conicTo(10, 90, 50, 90, 0.9f); |
| 5968 | SkPaint paint; |
| 5969 | paint.setColor(SK_ColorBLUE); |
| 5970 | canvas->drawRect(path.getBounds(), paint); |
| 5971 | paint.setAntiAlias(true); |
| 5972 | paint.setColor(SK_ColorWHITE); |
| 5973 | canvas->drawPath(path, paint); |
| 5974 | } |
| 5975 | }; |
| 5976 | |
| 5977 | #Function ## |
| 5978 | void draw(SkCanvas* canvas) { |
| 5979 | sk_sp<SkDrawable> drawable(new MyDrawable); |
| 5980 | SkMatrix matrix; |
| 5981 | matrix.setTranslate(10, 10); |
| 5982 | canvas->drawDrawable(drawable.get(), &matrix); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5983 | } |
| 5984 | ## |
| 5985 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 5986 | #SeeAlso SkDrawable drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 5987 | |
| 5988 | ## |
| 5989 | |
| 5990 | # ------------------------------------------------------------------------------ |
| 5991 | |
| 5992 | #Method void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y) |
| 5993 | |
| 5994 | Draw Drawable drawable using Clip and Matrix, offset by (x, y). |
| 5995 | |
| 5996 | If Canvas has an asynchronous implementation, as is the case |
| 5997 | when it is recording into Picture, then drawable will be referenced, |
| 5998 | so that SkDrawable::draw() can be called when the operation is finalized. To force |
| 5999 | immediate drawing, call SkDrawable::draw() instead. |
| 6000 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6001 | #Param drawable custom struct encapsulating drawing commands ## |
| 6002 | #Param x offset into Canvas writable pixels in x ## |
| 6003 | #Param y offset into Canvas writable pixels in y ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6004 | |
| 6005 | #Example |
| 6006 | #Height 100 |
| 6007 | #Function |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6008 | struct MyDrawable : public SkDrawable { |
| 6009 | SkRect onGetBounds() override { return SkRect::MakeWH(50, 100); } |
| 6010 | |
| 6011 | void onDraw(SkCanvas* canvas) override { |
| 6012 | SkPath path; |
| 6013 | path.conicTo(10, 90, 50, 90, 0.9f); |
| 6014 | SkPaint paint; |
| 6015 | paint.setColor(SK_ColorBLUE); |
| 6016 | canvas->drawRect(path.getBounds(), paint); |
| 6017 | paint.setAntiAlias(true); |
| 6018 | paint.setColor(SK_ColorWHITE); |
| 6019 | canvas->drawPath(path, paint); |
| 6020 | } |
| 6021 | }; |
| 6022 | |
| 6023 | #Function ## |
| 6024 | void draw(SkCanvas* canvas) { |
| 6025 | sk_sp<SkDrawable> drawable(new MyDrawable); |
| 6026 | canvas->drawDrawable(drawable.get(), 10, 10); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6027 | } |
| 6028 | ## |
| 6029 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6030 | #SeeAlso SkDrawable drawPicture |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6031 | |
| 6032 | ## |
| 6033 | |
| 6034 | # ------------------------------------------------------------------------------ |
| 6035 | |
| 6036 | #Method void drawAnnotation(const SkRect& rect, const char key[], SkData* value) |
| 6037 | |
| 6038 | Associate Rect on Canvas when an annotation; a key-value pair, where the key is |
| 6039 | a null-terminated utf8 string, and optional value is stored as Data. |
| 6040 | |
| 6041 | Only some canvas implementations, such as recording to Picture, or drawing to |
| 6042 | Document_PDF, use annotations. |
| 6043 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6044 | #Param rect Rect extent of canvas to annotate ## |
| 6045 | #Param key string used for lookup ## |
| 6046 | #Param value data holding value stored in annotation ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6047 | |
| 6048 | #Example |
| 6049 | #Height 1 |
| 6050 | const char text[] = "Click this link!"; |
| 6051 | SkRect bounds; |
| 6052 | SkPaint paint; |
| 6053 | paint.setTextSize(40); |
| 6054 | (void)paint.measureText(text, strlen(text), &bounds); |
| 6055 | const char url[] = "https://www.google.com/"; |
| 6056 | sk_sp<SkData> urlData(SkData::MakeWithCString(url)); |
| 6057 | canvas->drawAnnotation(bounds, "url_key", urlData.get()); |
| 6058 | ## |
| 6059 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6060 | #SeeAlso SkPicture SkDocument |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6061 | |
| 6062 | ## |
| 6063 | |
| 6064 | # ------------------------------------------------------------------------------ |
| 6065 | |
| 6066 | #Method void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) |
| 6067 | |
| 6068 | Associate Rect on Canvas when an annotation; a key-value pair, where the key is |
| 6069 | a null-terminated utf8 string, and optional value is stored as Data. |
| 6070 | |
| 6071 | Only some canvas implementations, such as recording to Picture, or drawing to |
| 6072 | Document_PDF, use annotations. |
| 6073 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6074 | #Param rect Rect extent of canvas to annotate ## |
| 6075 | #Param key string used for lookup ## |
| 6076 | #Param value data holding value stored in annotation ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6077 | |
| 6078 | #Example |
| 6079 | #Height 1 |
| 6080 | const char text[] = "Click this link!"; |
| 6081 | SkRect bounds; |
| 6082 | SkPaint paint; |
| 6083 | paint.setTextSize(40); |
| 6084 | (void)paint.measureText(text, strlen(text), &bounds); |
| 6085 | const char url[] = "https://www.google.com/"; |
| 6086 | sk_sp<SkData> urlData(SkData::MakeWithCString(url)); |
| 6087 | canvas->drawAnnotation(bounds, "url_key", urlData.get()); |
| 6088 | ## |
| 6089 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6090 | #SeeAlso SkPicture SkDocument |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6091 | |
| 6092 | ## |
| 6093 | |
| 6094 | #Method SkDrawFilter* getDrawFilter() const |
| 6095 | |
| 6096 | Legacy call to be deprecated. |
| 6097 | |
| 6098 | #Deprecated |
| 6099 | ## |
| 6100 | |
| 6101 | ## |
| 6102 | |
| 6103 | #Method virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) |
| 6104 | |
| 6105 | Legacy call to be deprecated. |
| 6106 | |
| 6107 | #Deprecated |
| 6108 | ## |
| 6109 | |
| 6110 | ## |
| 6111 | |
| 6112 | # ------------------------------------------------------------------------------ |
| 6113 | |
| 6114 | #Method virtual bool isClipEmpty() const |
| 6115 | |
| 6116 | Returns true if Clip is empty; that is, nothing will draw. |
| 6117 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6118 | May do work when called; it should not be called |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6119 | more often than needed. However, once called, subsequent calls perform no |
| 6120 | work until Clip changes. |
| 6121 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6122 | #Return true if Clip is empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6123 | |
| 6124 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6125 | void draw(SkCanvas* canvas) { |
| 6126 | SkDebugf("clip is%s empty\n", canvas->isClipEmpty() ? "" : " not"); |
| 6127 | SkPath path; |
| 6128 | canvas->clipPath(path); |
| 6129 | SkDebugf("clip is%s empty\n", canvas->isClipEmpty() ? "" : " not"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6130 | } |
| 6131 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6132 | clip is not empty |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6133 | clip is empty |
| 6134 | ## |
| 6135 | ## |
| 6136 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6137 | #SeeAlso isClipRect getLocalClipBounds getDeviceClipBounds |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6138 | |
| 6139 | ## |
| 6140 | |
| 6141 | # ------------------------------------------------------------------------------ |
| 6142 | |
| 6143 | #Method virtual bool isClipRect() const |
| 6144 | |
| 6145 | Returns true if Clip is Rect and not empty. |
| 6146 | Returns false if the clip is empty, or if it is not Rect. |
| 6147 | |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6148 | #Return true if Clip is Rect and not empty ## |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6149 | |
| 6150 | #Example |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6151 | void draw(SkCanvas* canvas) { |
| 6152 | SkDebugf("clip is%s rect\n", canvas->isClipRect() ? "" : " not"); |
| 6153 | canvas->clipRect({0, 0, 0, 0}); |
| 6154 | SkDebugf("clip is%s rect\n", canvas->isClipRect() ? "" : " not"); |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6155 | } |
| 6156 | #StdOut |
Cary Clark | bad5ad7 | 2017-08-03 17:14:08 -0400 | [diff] [blame] | 6157 | clip is rect |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6158 | clip is not rect |
| 6159 | ## |
| 6160 | ## |
| 6161 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6162 | #SeeAlso isClipEmpty getLocalClipBounds getDeviceClipBounds |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6163 | |
| 6164 | ## |
| 6165 | |
| 6166 | #Class SkCanvas ## |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6167 | |
| 6168 | #Class SkAutoCanvasRestore |
| 6169 | |
| 6170 | Stack helper class calls SkCanvas::restoreToCount() when SkAutoCanvasRestore |
| 6171 | goes out of scope. Use this to guarantee that the canvas is restored to a known |
| 6172 | state. |
| 6173 | |
| 6174 | #Method SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) |
| 6175 | |
| 6176 | Preserves Canvas save count. Optionally saves Canvas Clip and Matrix. |
| 6177 | |
| 6178 | #Param canvas Canvas to guard ## |
| 6179 | #Param doSave call SkCanvas::save() ## |
| 6180 | |
| 6181 | #Return utility to restore Canvas state on destructor ## |
| 6182 | |
| 6183 | #Example |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6184 | #Height 128 |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 6185 | SkPaint p; |
| 6186 | p.setAntiAlias(true); |
| 6187 | p.setTextSize(64); |
| 6188 | for (SkScalar sx : { -1, 1 } ) { |
| 6189 | for (SkScalar sy : { -1, 1 } ) { |
| 6190 | SkAutoCanvasRestore autoRestore(canvas, true); |
| 6191 | SkMatrix m = SkMatrix::MakeAll(sx, 1, 96, 0, sy, 64, 0, 0, 1); |
| 6192 | canvas->concat(m); |
| 6193 | canvas->drawString("R", 0, 0, p); |
| 6194 | } |
| 6195 | } |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6196 | ## |
| 6197 | |
| 6198 | #SeeAlso SkCanvas::save SkCanvas::restore |
| 6199 | |
| 6200 | ## |
| 6201 | |
| 6202 | #Method ~SkAutoCanvasRestore() |
| 6203 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6204 | Restores Canvas to saved state. Destructor is called when container goes out of |
| 6205 | scope. |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6206 | |
Cary Clark | 2ade997 | 2017-11-02 17:49:34 -0400 | [diff] [blame] | 6207 | #NoExample |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6208 | ## |
| 6209 | |
| 6210 | #SeeAlso SkCanvas::save SkCanvas::restore |
| 6211 | |
| 6212 | ## |
| 6213 | |
| 6214 | #Method void restore() |
| 6215 | |
Cary Clark | 154beea | 2017-10-26 07:58:48 -0400 | [diff] [blame] | 6216 | Restores Canvas to saved state immediately. Subsequent calls and |
| 6217 | ~SkAutoCanvasRestore have no effect. |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6218 | |
| 6219 | #Example |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 6220 | for (bool callRestore : { false, true } ) { |
| 6221 | for (bool saveCanvas : {false, true} ) { |
| 6222 | SkAutoCanvasRestore autoRestore(canvas, saveCanvas); |
| 6223 | if (!saveCanvas) { |
| 6224 | canvas->save(); |
| 6225 | } |
| 6226 | SkDebugf("saveCanvas: %s before restore: %d\n", |
| 6227 | saveCanvas ? "true" : "false", canvas->getSaveCount()); |
| 6228 | if (callRestore) autoRestore.restore(); |
| 6229 | SkDebugf("saveCanvas: %s after restore: %d\n", |
| 6230 | saveCanvas ? "true" : "false", canvas->getSaveCount()); |
| 6231 | } |
| 6232 | } |
| 6233 | SkDebugf("final count: %d\n", canvas->getSaveCount()); |
| 6234 | #StdOut |
| 6235 | saveCanvas: false before restore: 2 |
| 6236 | saveCanvas: false after restore: 2 |
| 6237 | saveCanvas: true before restore: 2 |
| 6238 | saveCanvas: true after restore: 2 |
| 6239 | saveCanvas: false before restore: 2 |
| 6240 | saveCanvas: false after restore: 1 |
| 6241 | saveCanvas: true before restore: 2 |
| 6242 | saveCanvas: true after restore: 1 |
| 6243 | final count: 1 |
| 6244 | ## |
Cary Clark | 884dd7d | 2017-10-11 10:37:52 -0400 | [diff] [blame] | 6245 | ## |
| 6246 | |
| 6247 | #SeeAlso SkCanvas::save SkCanvas::restore |
| 6248 | |
| 6249 | ## |
| 6250 | |
| 6251 | #Class SkAutoCanvasRestore ## |
| 6252 | |
Cary Clark | 8032b98 | 2017-07-28 11:04:54 -0400 | [diff] [blame] | 6253 | #Topic Canvas ## |