1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 /* Generated by tools/bookmaker from include/core/SkCanvas.h and docs/SkCanvas_Reference.bmh 9 on 2018-08-28 10:32:58. Additional documentation and examples can be found at: 10 https://skia.org/user/api/SkCanvas_Reference 11 12 You may edit either file directly. Structural changes to public interfaces require 13 editing both files. After editing docs/SkCanvas_Reference.bmh, run: 14 bookmaker -b docs -i include/core/SkCanvas.h -p 15 to create an updated version of this file. 16 */ 17 18 #ifndef SkCanvas_DEFINED 19 #define SkCanvas_DEFINED 20 21 #include "../private/SkMacros.h" 22 #include "SkBlendMode.h" 23 #include "SkClipOp.h" 24 #include "SkDeque.h" 25 #include "SkFontTypes.h" 26 #include "SkPaint.h" 27 #include "SkRasterHandleAllocator.h" 28 #include "SkSurfaceProps.h" 29 #include "SkVertices.h" 30 31 class GrContext; 32 class GrRenderTargetContext; 33 class SkAndroidFrameworkUtils; 34 class SkBaseDevice; 35 class SkBitmap; 36 class SkData; 37 class SkDraw; 38 class SkDrawable; 39 struct SkDrawShadowRec; 40 class SkFont; 41 class SkGlyphRunBuilder; 42 class SkImage; 43 class SkImageFilter; 44 class SkMetaData; 45 class SkPath; 46 class SkPicture; 47 class SkPixmap; 48 class SkRegion; 49 class SkRRect; 50 struct SkRSXform; 51 class SkSurface; 52 class SkSurface_Base; 53 class SkTextBlob; 54 55 /** \class SkCanvas 56 SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed. 57 SkCanvas contains a stack of SkMatrix and clip values. 58 59 SkCanvas and SkPaint together provide the state to draw into SkSurface or SkBaseDevice. 60 Each SkCanvas draw call transforms the geometry of the object by the concatenation of all 61 SkMatrix values in the stack. The transformed geometry is clipped by the intersection 62 of all of clip values in the stack. The SkCanvas draw calls use SkPaint to supply drawing 63 state such as color, SkTypeface, text size, stroke width, SkShader and so on. 64 65 To draw to a pixel-based destination, create raster surface or GPU surface. 66 Request SkCanvas from SkSurface to obtain the interface to draw. 67 SkCanvas generated by raster surface draws to memory visible to the CPU. 68 SkCanvas generated by GPU surface uses Vulkan or OpenGL to draw to the GPU. 69 70 To draw to a document, obtain SkCanvas from SVG canvas, document PDF, or SkPictureRecorder. 71 SkDocument based SkCanvas and other SkCanvas subclasses reference SkBaseDevice describing the 72 destination. 73 74 SkCanvas can be constructed to draw to SkBitmap without first creating raster surface. 75 This approach may be deprecated in the future. 76 */ 77 class SK_API SkCanvas { 78 enum PrivateSaveLayerFlags { 79 kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31, 80 }; 81 82 public: 83 84 /** Allocates raster SkCanvas that will draw directly into pixels. 85 86 SkCanvas is returned if all parameters are valid. 87 Valid parameters include: 88 info dimensions are zero or positive; 89 info contains SkColorType and SkAlphaType supported by raster surface; 90 pixels is not nullptr; 91 rowBytes is zero or large enough to contain info width pixels of SkColorType. 92 93 Pass zero for rowBytes to compute rowBytes from info width and size of pixel. 94 If rowBytes is greater than zero, it must be equal to or greater than 95 info width times bytes required for SkColorType. 96 97 Pixel buffer size should be info height times computed rowBytes. 98 Pixels are not initialized. 99 To access pixels after drawing, call flush() or peekPixels(). 100 101 @param info width, height, SkColorType, SkAlphaType, SkColorSpace, of raster surface; 102 width, or height, or both, may be zero 103 @param pixels pointer to destination pixels buffer 104 @param rowBytes interval from one SkSurface row to the next, or zero 105 @param props LCD striping orientation and setting for device independent fonts; 106 may be nullptr 107 @return SkCanvas if all parameters are valid; otherwise, nullptr 108 */ 109 static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo& info, void* pixels, 110 size_t rowBytes, 111 const SkSurfaceProps* props = nullptr); 112 113 /** Allocates raster SkCanvas specified by inline image specification. Subsequent SkCanvas 114 calls draw into pixels. 115 SkColorType is set to kN32_SkColorType. 116 SkAlphaType is set to kPremul_SkAlphaType. 117 To access pixels after drawing, call flush() or peekPixels(). 118 119 SkCanvas is returned if all parameters are valid. 120 Valid parameters include: 121 width and height are zero or positive; 122 pixels is not nullptr; 123 rowBytes is zero or large enough to contain width pixels of kN32_SkColorType. 124 125 Pass zero for rowBytes to compute rowBytes from width and size of pixel. 126 If rowBytes is greater than zero, it must be equal to or greater than 127 width times bytes required for SkColorType. 128 129 Pixel buffer size should be height times rowBytes. 130 131 @param width pixel column count on raster surface created; must be zero or greater 132 @param height pixel row count on raster surface created; must be zero or greater 133 @param pixels pointer to destination pixels buffer; buffer size should be height 134 times rowBytes 135 @param rowBytes interval from one SkSurface row to the next, or zero 136 @return SkCanvas if all parameters are valid; otherwise, nullptr 137 */ MakeRasterDirectN32(int width,int height,SkPMColor * pixels,size_t rowBytes)138 static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels, 139 size_t rowBytes) { 140 return MakeRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes); 141 } 142 143 /** Creates an empty SkCanvas with no backing device or pixels, with 144 a width and height of zero. 145 146 @return empty SkCanvas 147 */ 148 SkCanvas(); 149 150 /** Creates SkCanvas of the specified dimensions without a SkSurface. 151 Used by subclasses with custom implementations for draw member functions. 152 153 If props equals nullptr, SkSurfaceProps are created with 154 SkSurfaceProps::InitType settings, which choose the pixel striping 155 direction and order. Since a platform may dynamically change its direction when 156 the device is rotated, and since a platform may have multiple monitors with 157 different characteristics, it is best not to rely on this legacy behavior. 158 159 @param width zero or greater 160 @param height zero or greater 161 @param props LCD striping orientation and setting for device independent fonts; 162 may be nullptr 163 @return SkCanvas placeholder with dimensions 164 */ 165 SkCanvas(int width, int height, const SkSurfaceProps* props = nullptr); 166 167 /** Private. For internal use only. 168 */ 169 explicit SkCanvas(sk_sp<SkBaseDevice> device); 170 171 /** Constructs a canvas that draws into bitmap. 172 Sets SkSurfaceProps::kLegacyFontHost_InitType in constructed SkSurface. 173 174 SkBitmap is copied so that subsequently editing bitmap will not affect 175 constructed SkCanvas. 176 177 May be deprecated in the future. 178 179 @param bitmap width, height, SkColorType, SkAlphaType, and pixel 180 storage of raster surface 181 @return SkCanvas that can be used to draw into bitmap 182 */ 183 explicit SkCanvas(const SkBitmap& bitmap); 184 185 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK 186 /** Private. 187 */ 188 enum class ColorBehavior { 189 kLegacy, //!< placeholder 190 }; 191 192 /** Private. For use by Android framework only. 193 194 @param bitmap specifies a bitmap for the canvas to draw into 195 @param behavior specializes this constructor; value is unused 196 @return SkCanvas that can be used to draw into bitmap 197 */ 198 SkCanvas(const SkBitmap& bitmap, ColorBehavior behavior); 199 #endif 200 201 /** Constructs a canvas that draws into bitmap. 202 Use props to match the device characteristics, like LCD striping. 203 204 bitmap is copied so that subsequently editing bitmap will not affect 205 constructed SkCanvas. 206 207 @param bitmap width, height, SkColorType, SkAlphaType, 208 and pixel storage of raster surface 209 @param props order and orientation of RGB striping; and whether to use 210 device independent fonts 211 @return SkCanvas that can be used to draw into bitmap 212 */ 213 SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props); 214 215 /** Draws saved layers, if any. 216 Frees up resources used by SkCanvas. 217 */ 218 virtual ~SkCanvas(); 219 220 /** Returns storage to associate additional data with the canvas. 221 The storage is freed when SkCanvas is deleted. 222 223 @return storage that can be read from and written to 224 */ 225 SkMetaData& getMetaData(); 226 227 /** Returns SkImageInfo for SkCanvas. If SkCanvas is not associated with raster surface or 228 GPU surface, returned SkColorType is set to kUnknown_SkColorType. 229 230 @return dimensions and SkColorType of SkCanvas 231 */ 232 SkImageInfo imageInfo() const; 233 234 /** Copies SkSurfaceProps, if SkCanvas is associated with raster surface or 235 GPU surface, and returns true. Otherwise, returns false and leave props unchanged. 236 237 @param props storage for writable SkSurfaceProps 238 @return true if SkSurfaceProps was copied 239 */ 240 bool getProps(SkSurfaceProps* props) const; 241 242 /** Triggers the immediate execution of all pending draw operations. 243 If SkCanvas is associated with GPU surface, resolves all pending GPU operations. 244 If SkCanvas is associated with raster surface, has no effect; raster draw 245 operations are never deferred. 246 */ 247 void flush(); 248 249 /** Gets the size of the base or root layer in global canvas coordinates. The 250 origin of the base layer is always (0,0). The area available for drawing may be 251 smaller (due to clipping or saveLayer). 252 253 @return integral width and height of base layer 254 */ 255 virtual SkISize getBaseLayerSize() const; 256 257 /** Creates SkSurface matching info and props, and associates it with SkCanvas. 258 Returns nullptr if no match found. 259 260 If props is nullptr, matches SkSurfaceProps in SkCanvas. If props is nullptr and SkCanvas 261 does not have SkSurfaceProps, creates SkSurface with default SkSurfaceProps. 262 263 @param info width, height, SkColorType, SkAlphaType, and SkColorSpace 264 @param props SkSurfaceProps to match; may be nullptr to match SkCanvas 265 @return SkSurface matching info and props, or nullptr if no match is available 266 */ 267 sk_sp<SkSurface> makeSurface(const SkImageInfo& info, const SkSurfaceProps* props = nullptr); 268 269 /** Returns GPU context of the GPU surface associated with SkCanvas. 270 271 @return GPU context, if available; nullptr otherwise 272 */ 273 virtual GrContext* getGrContext(); 274 275 /** Returns the pixel base address, SkImageInfo, rowBytes, and origin if the pixels 276 can be read directly. The returned address is only valid 277 while SkCanvas is in scope and unchanged. Any SkCanvas call or SkSurface call 278 may invalidate the returned address and other returned values. 279 280 If pixels are inaccessible, info, rowBytes, and origin are unchanged. 281 282 @param info storage for writable pixels' SkImageInfo; may be nullptr 283 @param rowBytes storage for writable pixels' row bytes; may be nullptr 284 @param origin storage for SkCanvas top layer origin, its top-left corner; 285 may be nullptr 286 @return address of pixels, or nullptr if inaccessible 287 */ 288 void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = nullptr); 289 290 /** Returns custom context that tracks the SkMatrix and clip. 291 292 Use SkRasterHandleAllocator to blend Skia drawing with custom drawing, typically performed 293 by the host platform user interface. The custom context returned is generated by 294 SkRasterHandleAllocator::MakeCanvas, which creates a custom canvas with raster storage for 295 the drawing destination. 296 297 @return context of custom allocation 298 */ 299 SkRasterHandleAllocator::Handle accessTopRasterHandle() const; 300 301 /** Returns true if SkCanvas has direct access to its pixels. 302 303 Pixels are readable when SkBaseDevice is raster. Pixels are not readable when SkCanvas 304 is returned from GPU surface, returned by SkDocument::beginPage, returned by 305 SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility class 306 like SkDebugCanvas. 307 308 pixmap is valid only while SkCanvas is in scope and unchanged. Any 309 SkCanvas or SkSurface call may invalidate the pixmap values. 310 311 @param pixmap storage for pixel state if pixels are readable; otherwise, ignored 312 @return true if SkCanvas has direct access to pixels 313 */ 314 bool peekPixels(SkPixmap* pixmap); 315 316 /** Copies SkRect of pixels from SkCanvas into dstPixels. SkMatrix and clip are 317 ignored. 318 319 Source SkRect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). 320 Destination SkRect corners are (0, 0) and (dstInfo.width(), dstInfo.height()). 321 Copies each readable pixel intersecting both rectangles, without scaling, 322 converting to dstInfo.colorType() and dstInfo.alphaType() if required. 323 324 Pixels are readable when SkBaseDevice is raster, or backed by a GPU. 325 Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, 326 returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility 327 class like SkDebugCanvas. 328 329 The destination pixel storage must be allocated by the caller. 330 331 Pixel values are converted only if SkColorType and SkAlphaType 332 do not match. Only pixels within both source and destination rectangles 333 are copied. dstPixels contents outside SkRect intersection are unchanged. 334 335 Pass negative values for srcX or srcY to offset pixels across or down destination. 336 337 Does not copy, and returns false if: 338 - Source and destination rectangles do not intersect. 339 - SkCanvas pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). 340 - SkCanvas pixels are not readable; for instance, SkCanvas is document-based. 341 - dstRowBytes is too small to contain one row of pixels. 342 343 @param dstInfo width, height, SkColorType, and SkAlphaType of dstPixels 344 @param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger 345 @param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger 346 @param srcX offset into readable pixels on x-axis; may be negative 347 @param srcY offset into readable pixels on y-axis; may be negative 348 @return true if pixels were copied 349 */ 350 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 351 int srcX, int srcY); 352 353 /** Copies SkRect of pixels from SkCanvas into pixmap. SkMatrix and clip are 354 ignored. 355 356 Source SkRect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). 357 Destination SkRect corners are (0, 0) and (pixmap.width(), pixmap.height()). 358 Copies each readable pixel intersecting both rectangles, without scaling, 359 converting to pixmap.colorType() and pixmap.alphaType() if required. 360 361 Pixels are readable when SkBaseDevice is raster, or backed by a GPU. 362 Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, 363 returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility 364 class like SkDebugCanvas. 365 366 Caller must allocate pixel storage in pixmap if needed. 367 368 Pixel values are converted only if SkColorType and SkAlphaType 369 do not match. Only pixels within both source and destination SkRect 370 are copied. pixmap pixels contents outside SkRect intersection are unchanged. 371 372 Pass negative values for srcX or srcY to offset pixels across or down pixmap. 373 374 Does not copy, and returns false if: 375 - Source and destination rectangles do not intersect. 376 - SkCanvas pixels could not be converted to pixmap.colorType() or pixmap.alphaType(). 377 - SkCanvas pixels are not readable; for instance, SkCanvas is document-based. 378 - SkPixmap pixels could not be allocated. 379 - pixmap.rowBytes() is too small to contain one row of pixels. 380 381 @param pixmap storage for pixels copied from SkCanvas 382 @param srcX offset into readable pixels on x-axis; may be negative 383 @param srcY offset into readable pixels on y-axis; may be negative 384 @return true if pixels were copied 385 */ 386 bool readPixels(const SkPixmap& pixmap, int srcX, int srcY); 387 388 /** Copies SkRect of pixels from SkCanvas into bitmap. SkMatrix and clip are 389 ignored. 390 391 Source SkRect corners are (srcX, srcY) and (imageInfo().width(), imageInfo().height()). 392 Destination SkRect corners are (0, 0) and (bitmap.width(), bitmap.height()). 393 Copies each readable pixel intersecting both rectangles, without scaling, 394 converting to bitmap.colorType() and bitmap.alphaType() if required. 395 396 Pixels are readable when SkBaseDevice is raster, or backed by a GPU. 397 Pixels are not readable when SkCanvas is returned by SkDocument::beginPage, 398 returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility 399 class like SkDebugCanvas. 400 401 Caller must allocate pixel storage in bitmap if needed. 402 403 SkBitmap values are converted only if SkColorType and SkAlphaType 404 do not match. Only pixels within both source and destination rectangles 405 are copied. SkBitmap pixels outside SkRect intersection are unchanged. 406 407 Pass negative values for srcX or srcY to offset pixels across or down bitmap. 408 409 Does not copy, and returns false if: 410 - Source and destination rectangles do not intersect. 411 - SkCanvas pixels could not be converted to bitmap.colorType() or bitmap.alphaType(). 412 - SkCanvas pixels are not readable; for instance, SkCanvas is document-based. 413 - bitmap pixels could not be allocated. 414 - bitmap.rowBytes() is too small to contain one row of pixels. 415 416 @param bitmap storage for pixels copied from SkCanvas 417 @param srcX offset into readable pixels on x-axis; may be negative 418 @param srcY offset into readable pixels on y-axis; may be negative 419 @return true if pixels were copied 420 */ 421 bool readPixels(const SkBitmap& bitmap, int srcX, int srcY); 422 423 /** Copies SkRect from pixels to SkCanvas. SkMatrix and clip are ignored. 424 Source SkRect corners are (0, 0) and (info.width(), info.height()). 425 Destination SkRect corners are (x, y) and 426 (imageInfo().width(), imageInfo().height()). 427 428 Copies each readable pixel intersecting both rectangles, without scaling, 429 converting to imageInfo().colorType() and imageInfo().alphaType() if required. 430 431 Pixels are writable when SkBaseDevice is raster, or backed by a GPU. 432 Pixels are not writable when SkCanvas is returned by SkDocument::beginPage, 433 returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility 434 class like SkDebugCanvas. 435 436 Pixel values are converted only if SkColorType and SkAlphaType 437 do not match. Only pixels within both source and destination rectangles 438 are copied. SkCanvas pixels outside SkRect intersection are unchanged. 439 440 Pass negative values for x or y to offset pixels to the left or 441 above SkCanvas pixels. 442 443 Does not copy, and returns false if: 444 - Source and destination rectangles do not intersect. 445 - pixels could not be converted to SkCanvas imageInfo().colorType() or 446 imageInfo().alphaType(). 447 - SkCanvas pixels are not writable; for instance, SkCanvas is document-based. 448 - rowBytes is too small to contain one row of pixels. 449 450 @param info width, height, SkColorType, and SkAlphaType of pixels 451 @param pixels pixels to copy, of size info.height() times rowBytes, or larger 452 @param rowBytes size of one row of pixels; info.width() times pixel size, or larger 453 @param x offset into SkCanvas writable pixels on x-axis; may be negative 454 @param y offset into SkCanvas writable pixels on y-axis; may be negative 455 @return true if pixels were written to SkCanvas 456 */ 457 bool writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, int x, int y); 458 459 /** Copies SkRect from pixels to SkCanvas. SkMatrix and clip are ignored. 460 Source SkRect corners are (0, 0) and (bitmap.width(), bitmap.height()). 461 462 Destination SkRect corners are (x, y) and 463 (imageInfo().width(), imageInfo().height()). 464 465 Copies each readable pixel intersecting both rectangles, without scaling, 466 converting to imageInfo().colorType() and imageInfo().alphaType() if required. 467 468 Pixels are writable when SkBaseDevice is raster, or backed by a GPU. 469 Pixels are not writable when SkCanvas is returned by SkDocument::beginPage, 470 returned by SkPictureRecorder::beginRecording, or SkCanvas is the base of a utility 471 class like SkDebugCanvas. 472 473 Pixel values are converted only if SkColorType and SkAlphaType 474 do not match. Only pixels within both source and destination rectangles 475 are copied. SkCanvas pixels outside SkRect intersection are unchanged. 476 477 Pass negative values for x or y to offset pixels to the left or 478 above SkCanvas pixels. 479 480 Does not copy, and returns false if: 481 - Source and destination rectangles do not intersect. 482 - bitmap does not have allocated pixels. 483 - bitmap pixels could not be converted to SkCanvas imageInfo().colorType() or 484 imageInfo().alphaType(). 485 - SkCanvas pixels are not writable; for instance, SkCanvas is document based. 486 - bitmap pixels are inaccessible; for instance, bitmap wraps a texture. 487 488 @param bitmap contains pixels copied to SkCanvas 489 @param x offset into SkCanvas writable pixels on x-axis; may be negative 490 @param y offset into SkCanvas writable pixels on y-axis; may be negative 491 @return true if pixels were written to SkCanvas 492 */ 493 bool writePixels(const SkBitmap& bitmap, int x, int y); 494 495 /** Saves SkMatrix and clip. 496 Calling restore() discards changes to SkMatrix and clip, 497 restoring the SkMatrix and clip to their state when save() was called. 498 499 SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(), setMatrix(), 500 and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), clipPath(), clipRegion(). 501 502 Saved SkCanvas state is put on a stack; multiple calls to save() should be balance 503 by an equal number of calls to restore(). 504 505 Call restoreToCount() with result to restore this and subsequent saves. 506 507 @return depth of saved stack 508 */ 509 int save(); 510 511 /** Saves SkMatrix and clip, and allocates a SkBitmap for subsequent drawing. 512 Calling restore() discards changes to SkMatrix and clip, and draws the SkBitmap. 513 514 SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(), 515 setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), 516 clipPath(), clipRegion(). 517 518 SkRect bounds suggests but does not define the SkBitmap size. To clip drawing to 519 a specific rectangle, use clipRect(). 520 521 Optional SkPaint paint applies alpha, SkColorFilter, SkImageFilter, and 522 SkBlendMode when restore() is called. 523 524 Call restoreToCount() with returned value to restore this and subsequent saves. 525 526 @param bounds hint to limit the size of the layer; may be nullptr 527 @param paint graphics state for layer; may be nullptr 528 @return depth of saved stack 529 */ 530 int saveLayer(const SkRect* bounds, const SkPaint* paint); 531 532 /** Saves SkMatrix and clip, and allocates a SkBitmap for subsequent drawing. 533 Calling restore() discards changes to SkMatrix and clip, and draws the SkBitmap. 534 535 SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(), 536 setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), 537 clipPath(), clipRegion(). 538 539 SkRect bounds suggests but does not define the layer size. To clip drawing to 540 a specific rectangle, use clipRect(). 541 542 Optional SkPaint paint applies alpha, SkColorFilter, SkImageFilter, and 543 SkBlendMode when restore() is called. 544 545 Call restoreToCount() with returned value to restore this and subsequent saves. 546 547 @param bounds hint to limit the size of layer; may be nullptr 548 @param paint graphics state for layer; may be nullptr 549 @return depth of saved stack 550 */ saveLayer(const SkRect & bounds,const SkPaint * paint)551 int saveLayer(const SkRect& bounds, const SkPaint* paint) { 552 return this->saveLayer(&bounds, paint); 553 } 554 555 /** Saves SkMatrix and clip, and allocates SkBitmap for subsequent drawing. 556 557 Calling restore() discards changes to SkMatrix and clip, 558 and blends layer with alpha opacity onto prior layer. 559 560 SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(), 561 setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), 562 clipPath(), clipRegion(). 563 564 SkRect bounds suggests but does not define layer size. To clip drawing to 565 a specific rectangle, use clipRect(). 566 567 alpha of zero is fully transparent, 255 is fully opaque. 568 569 Call restoreToCount() with returned value to restore this and subsequent saves. 570 571 @param bounds hint to limit the size of layer; may be nullptr 572 @param alpha opacity of layer 573 @return depth of saved stack 574 */ 575 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha); 576 577 /** \enum SkCanvas::SaveLayerFlagsSet 578 SaveLayerFlags provides options that may be used in any combination in SaveLayerRec, 579 defining how layer allocated by saveLayer() operates. It may be set to zero, 580 kPreserveLCDText_SaveLayerFlag, kInitWithPrevious_SaveLayerFlag, or both flags. 581 */ 582 enum SaveLayerFlagsSet { 583 // kPreserveLCDText_SaveLayerFlag = 1 << 1, (no longer used) 584 kInitWithPrevious_SaveLayerFlag = 1 << 2, //!< initializes with previous contents 585 kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag = 586 1 << 3, //!< experimental: do not use 587 588 #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG 589 kDontClipToLayer_Legacy_SaveLayerFlag = 590 kDontClipToLayer_PrivateSaveLayerFlag, //!< deprecated 591 #endif 592 }; 593 594 typedef uint32_t SaveLayerFlags; 595 596 /** \struct SkCanvas::SaveLayerRec 597 SaveLayerRec contains the state used to create the layer. 598 */ 599 struct SaveLayerRec { 600 601 /** Sets fBounds, fPaint, and fBackdrop to nullptr. Clears fSaveLayerFlags. 602 603 @return empty SaveLayerRec 604 */ SaveLayerRecSaveLayerRec605 SaveLayerRec() {} 606 607 /** Sets fBounds, fPaint, and fSaveLayerFlags; sets fBackdrop to nullptr. 608 609 @param bounds layer dimensions; may be nullptr 610 @param paint applied to layer when overlaying prior layer; may be nullptr 611 @param saveLayerFlags SaveLayerRec options to modify layer 612 @return SaveLayerRec with empty fBackdrop 613 */ 614 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0) fBoundsSaveLayerRec615 : fBounds(bounds) 616 , fPaint(paint) 617 , fSaveLayerFlags(saveLayerFlags) 618 {} 619 620 /** Sets fBounds, fPaint, fBackdrop, and fSaveLayerFlags. 621 622 @param bounds layer dimensions; may be nullptr 623 @param paint applied to layer when overlaying prior layer; 624 may be nullptr 625 @param backdrop prior layer copied with SkImageFilter; may be nullptr 626 @param saveLayerFlags SaveLayerRec options to modify layer 627 @return SaveLayerRec fully specified 628 */ SaveLayerRecSaveLayerRec629 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop, 630 SaveLayerFlags saveLayerFlags) 631 : fBounds(bounds) 632 , fPaint(paint) 633 , fBackdrop(backdrop) 634 , fSaveLayerFlags(saveLayerFlags) 635 {} 636 637 /** Experimental. Not ready for general use. 638 Sets fBounds, fPaint, fBackdrop, fClipMask, fClipMatrix, and fSaveLayerFlags. 639 clipMatrix uses alpha channel of image, transformed by clipMatrix, to clip 640 layer when drawn to SkCanvas. 641 642 Implementation is not complete; has no effect if SkBaseDevice is GPU-backed. 643 644 @param bounds layer dimensions; may be nullptr 645 @param paint graphics state applied to layer when overlaying prior 646 layer; may be nullptr 647 @param backdrop prior layer copied with SkImageFilter; 648 may be nullptr 649 @param clipMask clip applied to layer; may be nullptr 650 @param clipMatrix matrix applied to clipMask; may be nullptr to use 651 identity matrix 652 @param saveLayerFlags SaveLayerRec options to modify layer 653 @return SaveLayerRec fully specified 654 */ SaveLayerRecSaveLayerRec655 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop, 656 const SkImage* clipMask, const SkMatrix* clipMatrix, 657 SaveLayerFlags saveLayerFlags) 658 : fBounds(bounds) 659 , fPaint(paint) 660 , fBackdrop(backdrop) 661 , fClipMask(clipMask) 662 , fClipMatrix(clipMatrix) 663 , fSaveLayerFlags(saveLayerFlags) 664 {} 665 666 /** hints at layer size limit */ 667 const SkRect* fBounds = nullptr; 668 669 /** modifies overlay */ 670 const SkPaint* fPaint = nullptr; 671 672 /** applies SkImageFilter to prior layer */ 673 const SkImageFilter* fBackdrop = nullptr; 674 675 /** clips layer with mask alpha */ 676 const SkImage* fClipMask = nullptr; 677 678 /** transforms mask alpha used to clip */ 679 const SkMatrix* fClipMatrix = nullptr; 680 681 /** preserves LCD text, creates with prior layer contents */ 682 SaveLayerFlags fSaveLayerFlags = 0; 683 }; 684 685 /** Saves SkMatrix and clip, and allocates SkBitmap for subsequent drawing. 686 687 Calling restore() discards changes to SkMatrix and clip, 688 and blends SkBitmap with alpha opacity onto the prior layer. 689 690 SkMatrix may be changed by translate(), scale(), rotate(), skew(), concat(), 691 setMatrix(), and resetMatrix(). Clip may be changed by clipRect(), clipRRect(), 692 clipPath(), clipRegion(). 693 694 SaveLayerRec contains the state used to create the layer. 695 696 Call restoreToCount() with returned value to restore this and subsequent saves. 697 698 @param layerRec layer state 699 @return depth of save state stack before this call was made. 700 */ 701 int saveLayer(const SaveLayerRec& layerRec); 702 703 /** Removes changes to SkMatrix and clip since SkCanvas state was 704 last saved. The state is removed from the stack. 705 706 Does nothing if the stack is empty. 707 */ 708 void restore(); 709 710 /** Returns the number of saved states, each containing: SkMatrix and clip. 711 Equals the number of save() calls less the number of restore() calls plus one. 712 The save count of a new canvas is one. 713 714 @return depth of save state stack 715 */ 716 int getSaveCount() const; 717 718 /** Restores state to SkMatrix and clip values when save(), saveLayer(), 719 saveLayerPreserveLCDTextRequests(), or saveLayerAlpha() returned saveCount. 720 721 Does nothing if saveCount is greater than state stack count. 722 Restores state to initial values if saveCount is less than or equal to one. 723 724 @param saveCount depth of state stack to restore 725 */ 726 void restoreToCount(int saveCount); 727 728 /** Translates SkMatrix by dx along the x-axis and dy along the y-axis. 729 730 Mathematically, replaces SkMatrix with a translation matrix 731 premultiplied with SkMatrix. 732 733 This has the effect of moving the drawing by (dx, dy) before transforming 734 the result with SkMatrix. 735 736 @param dx distance to translate on x-axis 737 @param dy distance to translate on y-axis 738 */ 739 void translate(SkScalar dx, SkScalar dy); 740 741 /** Scales SkMatrix by sx on the x-axis and sy on the y-axis. 742 743 Mathematically, replaces SkMatrix with a scale matrix 744 premultiplied with SkMatrix. 745 746 This has the effect of scaling the drawing by (sx, sy) before transforming 747 the result with SkMatrix. 748 749 @param sx amount to scale on x-axis 750 @param sy amount to scale on y-axis 751 */ 752 void scale(SkScalar sx, SkScalar sy); 753 754 /** Rotates SkMatrix by degrees. Positive degrees rotates clockwise. 755 756 Mathematically, replaces SkMatrix with a rotation matrix 757 premultiplied with SkMatrix. 758 759 This has the effect of rotating the drawing by degrees before transforming 760 the result with SkMatrix. 761 762 @param degrees amount to rotate, in degrees 763 */ 764 void rotate(SkScalar degrees); 765 766 /** Rotates SkMatrix by degrees about a point at (px, py). Positive degrees rotates 767 clockwise. 768 769 Mathematically, constructs a rotation matrix; premultiplies the rotation matrix by 770 a translation matrix; then replaces SkMatrix with the resulting matrix 771 premultiplied with SkMatrix. 772 773 This has the effect of rotating the drawing about a given point before 774 transforming the result with SkMatrix. 775 776 @param degrees amount to rotate, in degrees 777 @param px x-axis value of the point to rotate about 778 @param py y-axis value of the point to rotate about 779 */ 780 void rotate(SkScalar degrees, SkScalar px, SkScalar py); 781 782 /** Skews SkMatrix by sx on the x-axis and sy on the y-axis. A positive value of sx 783 skews the drawing right as y-axis values increase; a positive value of sy skews 784 the drawing down as x-axis values increase. 785 786 Mathematically, replaces SkMatrix with a skew matrix premultiplied with SkMatrix. 787 788 This has the effect of skewing the drawing by (sx, sy) before transforming 789 the result with SkMatrix. 790 791 @param sx amount to skew on x-axis 792 @param sy amount to skew on y-axis 793 */ 794 void skew(SkScalar sx, SkScalar sy); 795 796 /** Replaces SkMatrix with matrix premultiplied with existing SkMatrix. 797 798 This has the effect of transforming the drawn geometry by matrix, before 799 transforming the result with existing SkMatrix. 800 801 @param matrix matrix to premultiply with existing SkMatrix 802 */ 803 void concat(const SkMatrix& matrix); 804 805 /** Replaces SkMatrix with matrix. 806 Unlike concat(), any prior matrix state is overwritten. 807 808 @param matrix matrix to copy, replacing existing SkMatrix 809 */ 810 void setMatrix(const SkMatrix& matrix); 811 812 /** Sets SkMatrix to the identity matrix. 813 Any prior matrix state is overwritten. 814 */ 815 void resetMatrix(); 816 817 /** Replaces clip with the intersection or difference of clip and rect, 818 with an aliased or anti-aliased clip edge. rect is transformed by SkMatrix 819 before it is combined with clip. 820 821 @param rect SkRect to combine with clip 822 @param op SkClipOp to apply to clip 823 @param doAntiAlias true if clip is to be anti-aliased 824 */ 825 void clipRect(const SkRect& rect, SkClipOp op, bool doAntiAlias); 826 827 /** Replaces clip with the intersection or difference of clip and rect. 828 Resulting clip is aliased; pixels are fully contained by the clip. 829 rect is transformed by SkMatrix before it is combined with clip. 830 831 @param rect SkRect to combine with clip 832 @param op SkClipOp to apply to clip 833 */ clipRect(const SkRect & rect,SkClipOp op)834 void clipRect(const SkRect& rect, SkClipOp op) { 835 this->clipRect(rect, op, false); 836 } 837 838 /** Replaces clip with the intersection of clip and rect. 839 Resulting clip is aliased; pixels are fully contained by the clip. 840 rect is transformed by SkMatrix 841 before it is combined with clip. 842 843 @param rect SkRect to combine with clip 844 @param doAntiAlias true if clip is to be anti-aliased 845 */ 846 void clipRect(const SkRect& rect, bool doAntiAlias = false) { 847 this->clipRect(rect, SkClipOp::kIntersect, doAntiAlias); 848 } 849 850 /** Sets the maximum clip rectangle, which can be set by clipRect(), clipRRect() and 851 clipPath() and intersect the current clip with the specified rect. 852 The maximum clip affects only future clipping operations; it is not retroactive. 853 The clip restriction is not recorded in pictures. 854 855 Pass an empty rect to disable maximum clip. 856 This private API is for use by Android framework only. 857 858 @param rect maximum allowed clip in device coordinates 859 */ 860 void androidFramework_setDeviceClipRestriction(const SkIRect& rect); 861 862 /** Replaces clip with the intersection or difference of clip and rrect, 863 with an aliased or anti-aliased clip edge. 864 rrect is transformed by SkMatrix 865 before it is combined with clip. 866 867 @param rrect SkRRect to combine with clip 868 @param op SkClipOp to apply to clip 869 @param doAntiAlias true if clip is to be anti-aliased 870 */ 871 void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias); 872 873 /** Replaces clip with the intersection or difference of clip and rrect. 874 Resulting clip is aliased; pixels are fully contained by the clip. 875 rrect is transformed by SkMatrix before it is combined with clip. 876 877 @param rrect SkRRect to combine with clip 878 @param op SkClipOp to apply to clip 879 */ clipRRect(const SkRRect & rrect,SkClipOp op)880 void clipRRect(const SkRRect& rrect, SkClipOp op) { 881 this->clipRRect(rrect, op, false); 882 } 883 884 /** Replaces clip with the intersection of clip and rrect, 885 with an aliased or anti-aliased clip edge. 886 rrect is transformed by SkMatrix before it is combined with clip. 887 888 @param rrect SkRRect to combine with clip 889 @param doAntiAlias true if clip is to be anti-aliased 890 */ 891 void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) { 892 this->clipRRect(rrect, SkClipOp::kIntersect, doAntiAlias); 893 } 894 895 /** Replaces clip with the intersection or difference of clip and path, 896 with an aliased or anti-aliased clip edge. SkPath::FillType determines if path 897 describes the area inside or outside its contours; and if path contour overlaps 898 itself or another path contour, whether the overlaps form part of the area. 899 path is transformed by SkMatrix before it is combined with clip. 900 901 @param path SkPath to combine with clip 902 @param op SkClipOp to apply to clip 903 @param doAntiAlias true if clip is to be anti-aliased 904 */ 905 void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias); 906 907 /** Replaces clip with the intersection or difference of clip and path. 908 Resulting clip is aliased; pixels are fully contained by the clip. 909 SkPath::FillType determines if path 910 describes the area inside or outside its contours; and if path contour overlaps 911 itself or another path contour, whether the overlaps form part of the area. 912 path is transformed by SkMatrix 913 before it is combined with clip. 914 915 @param path SkPath to combine with clip 916 @param op SkClipOp to apply to clip 917 */ clipPath(const SkPath & path,SkClipOp op)918 void clipPath(const SkPath& path, SkClipOp op) { 919 this->clipPath(path, op, false); 920 } 921 922 /** Replaces clip with the intersection of clip and path. 923 Resulting clip is aliased; pixels are fully contained by the clip. 924 SkPath::FillType determines if path 925 describes the area inside or outside its contours; and if path contour overlaps 926 itself or another path contour, whether the overlaps form part of the area. 927 path is transformed by SkMatrix before it is combined with clip. 928 929 @param path SkPath to combine with clip 930 @param doAntiAlias true if clip is to be anti-aliased 931 */ 932 void clipPath(const SkPath& path, bool doAntiAlias = false) { 933 this->clipPath(path, SkClipOp::kIntersect, doAntiAlias); 934 } 935 936 /** Experimental. For testing only. 937 Set to simplify clip stack using PathOps. 938 */ setAllowSimplifyClip(bool allow)939 void setAllowSimplifyClip(bool allow) { 940 fAllowSimplifyClip = allow; 941 } 942 943 /** Replaces clip with the intersection or difference of clip and SkRegion deviceRgn. 944 Resulting clip is aliased; pixels are fully contained by the clip. 945 deviceRgn is unaffected by SkMatrix. 946 947 @param deviceRgn SkRegion to combine with clip 948 @param op SkClipOp to apply to clip 949 */ 950 void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect); 951 952 /** Returns true if SkRect rect, transformed by SkMatrix, can be quickly determined to be 953 outside of clip. May return false even though rect is outside of clip. 954 955 Use to check if an area to be drawn is clipped out, to skip subsequent draw calls. 956 957 @param rect SkRect to compare with clip 958 @return true if rect, transformed by SkMatrix, does not intersect clip 959 */ 960 bool quickReject(const SkRect& rect) const; 961 962 /** Returns true if path, transformed by SkMatrix, can be quickly determined to be 963 outside of clip. May return false even though path is outside of clip. 964 965 Use to check if an area to be drawn is clipped out, to skip subsequent draw calls. 966 967 @param path SkPath to compare with clip 968 @return true if path, transformed by SkMatrix, does not intersect clip 969 */ 970 bool quickReject(const SkPath& path) const; 971 972 /** Returns bounds of clip, transformed by inverse of SkMatrix. If clip is empty, 973 return SkRect::MakeEmpty, where all SkRect sides equal zero. 974 975 SkRect returned is outset by one to account for partial pixel coverage if clip 976 is anti-aliased. 977 978 @return bounds of clip in local coordinates 979 */ 980 SkRect getLocalClipBounds() const; 981 982 /** Returns bounds of clip, transformed by inverse of SkMatrix. If clip is empty, 983 return false, and set bounds to SkRect::MakeEmpty, where all SkRect sides equal zero. 984 985 bounds is outset by one to account for partial pixel coverage if clip 986 is anti-aliased. 987 988 @param bounds SkRect of clip in local coordinates 989 @return true if clip bounds is not empty 990 */ getLocalClipBounds(SkRect * bounds)991 bool getLocalClipBounds(SkRect* bounds) const { 992 *bounds = this->getLocalClipBounds(); 993 return !bounds->isEmpty(); 994 } 995 996 /** Returns SkIRect bounds of clip, unaffected by SkMatrix. If clip is empty, 997 return SkRect::MakeEmpty, where all SkRect sides equal zero. 998 999 Unlike getLocalClipBounds(), returned SkIRect is not outset. 1000 1001 @return bounds of clip in SkBaseDevice coordinates 1002 */ 1003 SkIRect getDeviceClipBounds() const; 1004 1005 /** Returns SkIRect bounds of clip, unaffected by SkMatrix. If clip is empty, 1006 return false, and set bounds to SkRect::MakeEmpty, where all SkRect sides equal zero. 1007 1008 Unlike getLocalClipBounds(), bounds is not outset. 1009 1010 @param bounds SkRect of clip in device coordinates 1011 @return true if clip bounds is not empty 1012 */ getDeviceClipBounds(SkIRect * bounds)1013 bool getDeviceClipBounds(SkIRect* bounds) const { 1014 *bounds = this->getDeviceClipBounds(); 1015 return !bounds->isEmpty(); 1016 } 1017 1018 /** Fills clip with color color. 1019 mode determines how ARGB is combined with destination. 1020 1021 @param color unpremultiplied ARGB 1022 @param mode SkBlendMode used to combine source color and destination 1023 */ 1024 void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver); 1025 1026 /** Fills clip with color color using SkBlendMode::kSrc. 1027 This has the effect of replacing all pixels contained by clip with color. 1028 1029 @param color unpremultiplied ARGB 1030 */ clear(SkColor color)1031 void clear(SkColor color) { 1032 this->drawColor(color, SkBlendMode::kSrc); 1033 } 1034 1035 /** Makes SkCanvas contents undefined. Subsequent calls that read SkCanvas pixels, 1036 such as drawing with SkBlendMode, return undefined results. discard() does 1037 not change clip or SkMatrix. 1038 1039 discard() may do nothing, depending on the implementation of SkSurface or SkBaseDevice 1040 that created SkCanvas. 1041 1042 discard() allows optimized performance on subsequent draws by removing 1043 cached data associated with SkSurface or SkBaseDevice. 1044 It is not necessary to call discard() once done with SkCanvas; 1045 any cached data is deleted when owning SkSurface or SkBaseDevice is deleted. 1046 */ discard()1047 void discard() { this->onDiscard(); } 1048 1049 /** Fills clip with SkPaint paint. SkPaint components SkMaskFilter, SkShader, 1050 SkColorFilter, SkImageFilter, and SkBlendMode affect drawing; 1051 SkPathEffect in paint is ignored. 1052 1053 @param paint graphics state used to fill SkCanvas 1054 */ 1055 void drawPaint(const SkPaint& paint); 1056 1057 /** \enum SkCanvas::PointMode 1058 Selects if an array of points are drawn as discrete points, as lines, or as 1059 an open polygon. 1060 */ 1061 enum PointMode { 1062 kPoints_PointMode, //!< draw each point separately 1063 kLines_PointMode, //!< draw each pair of points as a line segment 1064 kPolygon_PointMode, //!< draw the array of points as a open polygon 1065 }; 1066 1067 /** Draws pts using clip, SkMatrix and SkPaint paint. 1068 count is the number of points; if count is less than one, has no effect. 1069 mode may be one of: kPoints_PointMode, kLines_PointMode, or kPolygon_PointMode. 1070 1071 If mode is kPoints_PointMode, the shape of point drawn depends on paint 1072 SkPaint::Cap. If paint is set to SkPaint::kRound_Cap, each point draws a 1073 circle of diameter SkPaint stroke width. If paint is set to SkPaint::kSquare_Cap 1074 or SkPaint::kButt_Cap, each point draws a square of width and height 1075 SkPaint stroke width. 1076 1077 If mode is kLines_PointMode, each pair of points draws a line segment. 1078 One line is drawn for every two points; each point is used once. If count is odd, 1079 the final point is ignored. 1080 1081 If mode is kPolygon_PointMode, each adjacent pair of points draws a line segment. 1082 count minus one lines are drawn; the first and last point are used once. 1083 1084 Each line segment respects paint SkPaint::Cap and SkPaint stroke width. 1085 SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style. 1086 1087 Always draws each element one at a time; is not affected by 1088 SkPaint::Join, and unlike drawPath(), does not create a mask from all points 1089 and lines before drawing. 1090 1091 @param mode whether pts draws points or lines 1092 @param count number of points in the array 1093 @param pts array of points to draw 1094 @param paint stroke, blend, color, and so on, used to draw 1095 */ 1096 void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint); 1097 1098 /** Draws point at (x, y) using clip, SkMatrix and SkPaint paint. 1099 1100 The shape of point drawn depends on paint SkPaint::Cap. 1101 If paint is set to SkPaint::kRound_Cap, draw a circle of diameter 1102 SkPaint stroke width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap, 1103 draw a square of width and height SkPaint stroke width. 1104 SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style. 1105 1106 @param x left edge of circle or square 1107 @param y top edge of circle or square 1108 @param paint stroke, blend, color, and so on, used to draw 1109 */ 1110 void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint); 1111 1112 /** Draws point p using clip, SkMatrix and SkPaint paint. 1113 1114 The shape of point drawn depends on paint SkPaint::Cap. 1115 If paint is set to SkPaint::kRound_Cap, draw a circle of diameter 1116 SkPaint stroke width. If paint is set to SkPaint::kSquare_Cap or SkPaint::kButt_Cap, 1117 draw a square of width and height SkPaint stroke width. 1118 SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style. 1119 1120 @param p top-left edge of circle or square 1121 @param paint stroke, blend, color, and so on, used to draw 1122 */ drawPoint(SkPoint p,const SkPaint & paint)1123 void drawPoint(SkPoint p, const SkPaint& paint) { 1124 this->drawPoint(p.x(), p.y(), paint); 1125 } 1126 1127 /** Draws line segment from (x0, y0) to (x1, y1) using clip, SkMatrix, and SkPaint paint. 1128 In paint: SkPaint stroke width describes the line thickness; 1129 SkPaint::Cap draws the end rounded or square; 1130 SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style. 1131 1132 @param x0 start of line segment on x-axis 1133 @param y0 start of line segment on y-axis 1134 @param x1 end of line segment on x-axis 1135 @param y1 end of line segment on y-axis 1136 @param paint stroke, blend, color, and so on, used to draw 1137 */ 1138 void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint); 1139 1140 /** Draws line segment from p0 to p1 using clip, SkMatrix, and SkPaint paint. 1141 In paint: SkPaint stroke width describes the line thickness; 1142 SkPaint::Cap draws the end rounded or square; 1143 SkPaint::Style is ignored, as if were set to SkPaint::kStroke_Style. 1144 1145 @param p0 start of line segment 1146 @param p1 end of line segment 1147 @param paint stroke, blend, color, and so on, used to draw 1148 */ drawLine(SkPoint p0,SkPoint p1,const SkPaint & paint)1149 void drawLine(SkPoint p0, SkPoint p1, const SkPaint& paint) { 1150 this->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint); 1151 } 1152 1153 /** Draws SkRect rect using clip, SkMatrix, and SkPaint paint. 1154 In paint: SkPaint::Style determines if rectangle is stroked or filled; 1155 if stroked, SkPaint stroke width describes the line thickness, and 1156 SkPaint::Join draws the corners rounded or square. 1157 1158 @param rect rectangle to draw 1159 @param paint stroke or fill, blend, color, and so on, used to draw 1160 */ 1161 void drawRect(const SkRect& rect, const SkPaint& paint); 1162 1163 /** Draws SkIRect rect using clip, SkMatrix, and SkPaint paint. 1164 In paint: SkPaint::Style determines if rectangle is stroked or filled; 1165 if stroked, SkPaint stroke width describes the line thickness, and 1166 SkPaint::Join draws the corners rounded or square. 1167 1168 @param rect rectangle to draw 1169 @param paint stroke or fill, blend, color, and so on, used to draw 1170 */ drawIRect(const SkIRect & rect,const SkPaint & paint)1171 void drawIRect(const SkIRect& rect, const SkPaint& paint) { 1172 SkRect r; 1173 r.set(rect); // promotes the ints to scalars 1174 this->drawRect(r, paint); 1175 } 1176 1177 /** Draws SkRegion region using clip, SkMatrix, and SkPaint paint. 1178 In paint: SkPaint::Style determines if rectangle is stroked or filled; 1179 if stroked, SkPaint stroke width describes the line thickness, and 1180 SkPaint::Join draws the corners rounded or square. 1181 1182 @param region region to draw 1183 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1184 */ 1185 void drawRegion(const SkRegion& region, const SkPaint& paint); 1186 1187 /** Draws oval oval using clip, SkMatrix, and SkPaint. 1188 In paint: SkPaint::Style determines if oval is stroked or filled; 1189 if stroked, SkPaint stroke width describes the line thickness. 1190 1191 @param oval SkRect bounds of oval 1192 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1193 */ 1194 void drawOval(const SkRect& oval, const SkPaint& paint); 1195 1196 /** Draws SkRRect rrect using clip, SkMatrix, and SkPaint paint. 1197 In paint: SkPaint::Style determines if rrect is stroked or filled; 1198 if stroked, SkPaint stroke width describes the line thickness. 1199 1200 rrect may represent a rectangle, circle, oval, uniformly rounded rectangle, or 1201 may have any combination of positive non-square radii for the four corners. 1202 1203 @param rrect SkRRect with up to eight corner radii to draw 1204 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1205 */ 1206 void drawRRect(const SkRRect& rrect, const SkPaint& paint); 1207 1208 /** Draws SkRRect outer and inner 1209 using clip, SkMatrix, and SkPaint paint. 1210 outer must contain inner or the drawing is undefined. 1211 In paint: SkPaint::Style determines if SkRRect is stroked or filled; 1212 if stroked, SkPaint stroke width describes the line thickness. 1213 If stroked and SkRRect corner has zero length radii, SkPaint::Join can 1214 draw corners rounded or square. 1215 1216 GPU-backed platforms optimize drawing when both outer and inner are 1217 concave and outer contains inner. These platforms may not be able to draw 1218 SkPath built with identical data as fast. 1219 1220 @param outer SkRRect outer bounds to draw 1221 @param inner SkRRect inner bounds to draw 1222 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1223 */ 1224 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint); 1225 1226 /** Draws circle at (cx, cy) with radius using clip, SkMatrix, and SkPaint paint. 1227 If radius is zero or less, nothing is drawn. 1228 In paint: SkPaint::Style determines if circle is stroked or filled; 1229 if stroked, SkPaint stroke width describes the line thickness. 1230 1231 @param cx circle center on the x-axis 1232 @param cy circle center on the y-axis 1233 @param radius half the diameter of circle 1234 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1235 */ 1236 void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint); 1237 1238 /** Draws circle at center with radius using clip, SkMatrix, and SkPaint paint. 1239 If radius is zero or less, nothing is drawn. 1240 In paint: SkPaint::Style determines if circle is stroked or filled; 1241 if stroked, SkPaint stroke width describes the line thickness. 1242 1243 @param center circle center 1244 @param radius half the diameter of circle 1245 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1246 */ drawCircle(SkPoint center,SkScalar radius,const SkPaint & paint)1247 void drawCircle(SkPoint center, SkScalar radius, const SkPaint& paint) { 1248 this->drawCircle(center.x(), center.y(), radius, paint); 1249 } 1250 1251 /** Draws arc using clip, SkMatrix, and SkPaint paint. 1252 1253 Arc is part of oval bounded by oval, sweeping from startAngle to startAngle plus 1254 sweepAngle. startAngle and sweepAngle are in degrees. 1255 1256 startAngle of zero places start point at the right middle edge of oval. 1257 A positive sweepAngle places arc end point clockwise from start point; 1258 a negative sweepAngle places arc end point counterclockwise from start point. 1259 sweepAngle may exceed 360 degrees, a full circle. 1260 If useCenter is true, draw a wedge that includes lines from oval 1261 center to arc end points. If useCenter is false, draw arc between end points. 1262 1263 If SkRect oval is empty or sweepAngle is zero, nothing is drawn. 1264 1265 @param oval SkRect bounds of oval containing arc to draw 1266 @param startAngle angle in degrees where arc begins 1267 @param sweepAngle sweep angle in degrees; positive is clockwise 1268 @param useCenter if true, include the center of the oval 1269 @param paint SkPaint stroke or fill, blend, color, and so on, used to draw 1270 */ 1271 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, 1272 bool useCenter, const SkPaint& paint); 1273 1274 /** Draws SkRRect bounded by SkRect rect, with corner radii (rx, ry) using clip, 1275 SkMatrix, and SkPaint paint. 1276 1277 In paint: SkPaint::Style determines if SkRRect is stroked or filled; 1278 if stroked, SkPaint stroke width describes the line thickness. 1279 If rx or ry are less than zero, they are treated as if they are zero. 1280 If rx plus ry exceeds rect width or rect height, radii are scaled down to fit. 1281 If rx and ry are zero, SkRRect is drawn as SkRect and if stroked is affected by 1282 SkPaint::Join. 1283 1284 @param rect SkRect bounds of SkRRect to draw 1285 @param rx axis length on x-axis of oval describing rounded corners 1286 @param ry axis length on y-axis of oval describing rounded corners 1287 @param paint stroke, blend, color, and so on, used to draw 1288 */ 1289 void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint); 1290 1291 /** Draws SkPath path using clip, SkMatrix, and SkPaint paint. 1292 SkPath contains an array of path contour, each of which may be open or closed. 1293 1294 In paint: SkPaint::Style determines if SkRRect is stroked or filled: 1295 if filled, SkPath::FillType determines whether path contour describes inside or 1296 outside of fill; if stroked, SkPaint stroke width describes the line thickness, 1297 SkPaint::Cap describes line ends, and SkPaint::Join describes how 1298 corners are drawn. 1299 1300 @param path SkPath to draw 1301 @param paint stroke, blend, color, and so on, used to draw 1302 */ 1303 void drawPath(const SkPath& path, const SkPaint& paint); 1304 1305 /** Draws SkImage image, with its top-left corner at (left, top), 1306 using clip, SkMatrix, and optional SkPaint paint. 1307 1308 If paint is supplied, apply SkColorFilter, alpha, SkImageFilter, SkBlendMode, 1309 and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1310 If paint contains SkMaskFilter, generate mask from image bounds. If generated 1311 mask extends beyond image bounds, replicate image edge colors, just as SkShader 1312 made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the 1313 image edge color when it samples outside of its bounds. 1314 1315 @param image uncompressed rectangular map of pixels 1316 @param left left side of image 1317 @param top top side of image 1318 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1319 and so on; or nullptr 1320 */ 1321 void drawImage(const SkImage* image, SkScalar left, SkScalar top, 1322 const SkPaint* paint = nullptr); 1323 1324 /** Draws SkImage image, with its top-left corner at (left, top), 1325 using clip, SkMatrix, and optional SkPaint paint. 1326 1327 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1328 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1329 If paint contains SkMaskFilter, generate mask from image bounds. If generated 1330 mask extends beyond image bounds, replicate image edge colors, just as SkShader 1331 made from SkImage::makeShader with SkShader::kClamp_TileMode set replicates the 1332 image edge color when it samples outside of its bounds. 1333 1334 @param image uncompressed rectangular map of pixels 1335 @param left left side of image 1336 @param top pop side of image 1337 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1338 and so on; or nullptr 1339 */ 1340 void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top, 1341 const SkPaint* paint = nullptr) { 1342 this->drawImage(image.get(), left, top, paint); 1343 } 1344 1345 /** \enum SkCanvas::SrcRectConstraint 1346 SrcRectConstraint controls the behavior at the edge of source SkRect, 1347 provided to drawImageRect(), trading off speed for precision. 1348 1349 SkFilterQuality in SkPaint may sample multiple pixels in the image. Source SkRect 1350 restricts the bounds of pixels that may be read. SkFilterQuality may slow down if 1351 it cannot read outside the bounds, when sampling near the edge of source SkRect. 1352 SrcRectConstraint specifies whether an SkImageFilter is allowed to read pixels 1353 outside source SkRect. 1354 */ 1355 enum SrcRectConstraint { 1356 kStrict_SrcRectConstraint, //!< sample only inside bounds; slower 1357 kFast_SrcRectConstraint, //!< sample outside bounds; faster 1358 }; 1359 1360 /** Draws SkRect src of SkImage image, scaled and translated to fill SkRect dst. 1361 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1362 1363 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1364 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1365 If paint contains SkMaskFilter, generate mask from image bounds. 1366 1367 If generated mask extends beyond image bounds, replicate image edge colors, just 1368 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1369 replicates the image edge color when it samples outside of its bounds. 1370 1371 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1372 sample within src; set to kFast_SrcRectConstraint allows sampling outside to 1373 improve performance. 1374 1375 @param image SkImage containing pixels, dimensions, and format 1376 @param src source SkRect of image to draw from 1377 @param dst destination SkRect of image to draw to 1378 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1379 and so on; or nullptr 1380 @param constraint filter strictly within src or draw faster 1381 */ 1382 void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst, 1383 const SkPaint* paint, 1384 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 1385 1386 /** Draws SkIRect isrc of SkImage image, scaled and translated to fill SkRect dst. 1387 Note that isrc is on integer pixel boundaries; dst may include fractional 1388 boundaries. Additionally transform draw using clip, SkMatrix, and optional SkPaint 1389 paint. 1390 1391 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1392 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1393 If paint contains SkMaskFilter, generate mask from image bounds. 1394 1395 If generated mask extends beyond image bounds, replicate image edge colors, just 1396 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1397 replicates the image edge color when it samples outside of its bounds. 1398 1399 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1400 sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to 1401 improve performance. 1402 1403 @param image SkImage containing pixels, dimensions, and format 1404 @param isrc source SkIRect of image to draw from 1405 @param dst destination SkRect of image to draw to 1406 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1407 and so on; or nullptr 1408 @param constraint filter strictly within isrc or draw faster 1409 */ 1410 void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst, 1411 const SkPaint* paint, 1412 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 1413 1414 /** Draws SkImage image, scaled and translated to fill SkRect dst, using clip, SkMatrix, 1415 and optional SkPaint paint. 1416 1417 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1418 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1419 If paint contains SkMaskFilter, generate mask from image bounds. 1420 1421 If generated mask extends beyond image bounds, replicate image edge colors, just 1422 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1423 replicates the image edge color when it samples outside of its bounds. 1424 1425 @param image SkImage containing pixels, dimensions, and format 1426 @param dst destination SkRect of image to draw to 1427 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1428 and so on; or nullptr 1429 */ 1430 void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint); 1431 1432 /** Draws SkRect src of SkImage image, scaled and translated to fill SkRect dst. 1433 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1434 1435 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1436 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1437 If paint contains SkMaskFilter, generate mask from image bounds. 1438 1439 If generated mask extends beyond image bounds, replicate image edge colors, just 1440 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1441 replicates the image edge color when it samples outside of its bounds. 1442 1443 @param image SkImage containing pixels, dimensions, and format 1444 @param src source SkRect of image to draw from 1445 @param dst destination SkRect of image to draw to 1446 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1447 and so on; or nullptr 1448 @param constraint filter strictly within src or draw faster 1449 */ 1450 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst, 1451 const SkPaint* paint, 1452 SrcRectConstraint constraint = kStrict_SrcRectConstraint) { 1453 this->drawImageRect(image.get(), src, dst, paint, constraint); 1454 } 1455 1456 /** Draws SkIRect isrc of SkImage image, scaled and translated to fill SkRect dst. 1457 isrc is on integer pixel boundaries; dst may include fractional boundaries. 1458 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1459 1460 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1461 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1462 If paint contains SkMaskFilter, generate mask from image bounds. 1463 1464 If generated mask extends beyond image bounds, replicate image edge colors, just 1465 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1466 replicates the image edge color when it samples outside of its bounds. 1467 1468 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1469 sample within image; set to kFast_SrcRectConstraint allows sampling outside to 1470 improve performance. 1471 1472 @param image SkImage containing pixels, dimensions, and format 1473 @param isrc source SkIRect of image to draw from 1474 @param dst destination SkRect of image to draw to 1475 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1476 and so on; or nullptr 1477 @param constraint filter strictly within image or draw faster 1478 */ 1479 void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst, 1480 const SkPaint* paint, 1481 SrcRectConstraint constraint = kStrict_SrcRectConstraint) { 1482 this->drawImageRect(image.get(), isrc, dst, paint, constraint); 1483 } 1484 1485 /** Draws SkImage image, scaled and translated to fill SkRect dst, 1486 using clip, SkMatrix, and optional SkPaint paint. 1487 1488 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1489 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1490 If paint contains SkMaskFilter, generate mask from image bounds. 1491 1492 If generated mask extends beyond image bounds, replicate image edge colors, just 1493 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1494 replicates the image edge color when it samples outside of its bounds. 1495 1496 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1497 sample within image; set to kFast_SrcRectConstraint allows sampling outside to 1498 improve performance. 1499 1500 @param image SkImage containing pixels, dimensions, and format 1501 @param dst destination SkRect of image to draw to 1502 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1503 and so on; or nullptr 1504 */ drawImageRect(const sk_sp<SkImage> & image,const SkRect & dst,const SkPaint * paint)1505 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint) { 1506 this->drawImageRect(image.get(), dst, paint); 1507 } 1508 1509 /** Draws SkImage image stretched proportionally to fit into SkRect dst. 1510 SkIRect center divides the image into nine sections: four sides, four corners, and 1511 the center. Corners are unmodified or scaled down proportionately if their sides 1512 are larger than dst; center and four sides are scaled to fit remaining space, if any. 1513 1514 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1515 1516 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1517 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1518 If paint contains SkMaskFilter, generate mask from image bounds. If paint 1519 SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all 1520 other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels. 1521 Any SkMaskFilter on paint is ignored as is paint anti-aliasing state. 1522 1523 If generated mask extends beyond image bounds, replicate image edge colors, just 1524 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1525 replicates the image edge color when it samples outside of its bounds. 1526 1527 @param image SkImage containing pixels, dimensions, and format 1528 @param center SkIRect edge of image corners and sides 1529 @param dst destination SkRect of image to draw to 1530 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1531 and so on; or nullptr 1532 */ 1533 void drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, 1534 const SkPaint* paint = nullptr); 1535 1536 /** Draws SkImage image stretched proportionally to fit into SkRect dst. 1537 SkIRect center divides the image into nine sections: four sides, four corners, and 1538 the center. Corners are not scaled, or scaled down proportionately if their sides 1539 are larger than dst; center and four sides are scaled to fit remaining space, if any. 1540 1541 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1542 1543 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1544 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1545 If paint contains SkMaskFilter, generate mask from image bounds. If paint 1546 SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all 1547 other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels. 1548 Any SkMaskFilter on paint is ignored as is paint anti-aliasing state. 1549 1550 If generated mask extends beyond image bounds, replicate image edge colors, just 1551 as SkShader made from SkImage::makeShader with SkShader::kClamp_TileMode set 1552 replicates the image edge color when it samples outside of its bounds. 1553 1554 @param image SkImage containing pixels, dimensions, and format 1555 @param center SkIRect edge of image corners and sides 1556 @param dst destination SkRect of image to draw to 1557 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1558 and so on; or nullptr 1559 */ 1560 void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst, 1561 const SkPaint* paint = nullptr) { 1562 this->drawImageNine(image.get(), center, dst, paint); 1563 } 1564 1565 /** Draws SkBitmap bitmap, with its top-left corner at (left, top), 1566 using clip, SkMatrix, and optional SkPaint paint. 1567 1568 If SkPaint paint is not nullptr, apply SkColorFilter, alpha, SkImageFilter, 1569 SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader. 1570 If paint contains SkMaskFilter, generate mask from bitmap bounds. 1571 1572 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1573 just as SkShader made from SkShader::MakeBitmapShader with 1574 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1575 outside of its bounds. 1576 1577 @param bitmap SkBitmap containing pixels, dimensions, and format 1578 @param left left side of bitmap 1579 @param top top side of bitmap 1580 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1581 and so on; or nullptr 1582 */ 1583 void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, 1584 const SkPaint* paint = nullptr); 1585 1586 /** Draws SkRect src of SkBitmap bitmap, scaled and translated to fill SkRect dst. 1587 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1588 1589 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1590 SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader. 1591 If paint contains SkMaskFilter, generate mask from bitmap bounds. 1592 1593 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1594 just as SkShader made from SkShader::MakeBitmapShader with 1595 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1596 outside of its bounds. 1597 1598 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1599 sample within src; set to kFast_SrcRectConstraint allows sampling outside to 1600 improve performance. 1601 1602 @param bitmap SkBitmap containing pixels, dimensions, and format 1603 @param src source SkRect of image to draw from 1604 @param dst destination SkRect of image to draw to 1605 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1606 and so on; or nullptr 1607 @param constraint filter strictly within src or draw faster 1608 */ 1609 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst, 1610 const SkPaint* paint, 1611 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 1612 1613 /** Draws SkIRect isrc of SkBitmap bitmap, scaled and translated to fill SkRect dst. 1614 isrc is on integer pixel boundaries; dst may include fractional boundaries. 1615 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1616 1617 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1618 SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader. 1619 If paint contains SkMaskFilter, generate mask from bitmap bounds. 1620 1621 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1622 just as SkShader made from SkShader::MakeBitmapShader with 1623 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1624 outside of its bounds. 1625 1626 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1627 sample within isrc; set to kFast_SrcRectConstraint allows sampling outside to 1628 improve performance. 1629 1630 @param bitmap SkBitmap containing pixels, dimensions, and format 1631 @param isrc source SkIRect of image to draw from 1632 @param dst destination SkRect of image to draw to 1633 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1634 and so on; or nullptr 1635 @param constraint sample strictly within isrc, or draw faster 1636 */ 1637 void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst, 1638 const SkPaint* paint, 1639 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 1640 1641 /** Draws SkBitmap bitmap, scaled and translated to fill SkRect dst. 1642 bitmap bounds is on integer pixel boundaries; dst may include fractional boundaries. 1643 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1644 1645 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1646 SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader. 1647 If paint contains SkMaskFilter, generate mask from bitmap bounds. 1648 1649 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1650 just as SkShader made from SkShader::MakeBitmapShader with 1651 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1652 outside of its bounds. 1653 1654 constraint set to kStrict_SrcRectConstraint limits SkPaint SkFilterQuality to 1655 sample within bitmap; set to kFast_SrcRectConstraint allows sampling outside to 1656 improve performance. 1657 1658 @param bitmap SkBitmap containing pixels, dimensions, and format 1659 @param dst destination SkRect of image to draw to 1660 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1661 and so on; or nullptr 1662 @param constraint filter strictly within bitmap or draw faster 1663 */ 1664 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint, 1665 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 1666 1667 /** Draws SkBitmap bitmap stretched proportionally to fit into SkRect dst. 1668 SkIRect center divides the bitmap into nine sections: four sides, four corners, 1669 and the center. Corners are not scaled, or scaled down proportionately if their 1670 sides are larger than dst; center and four sides are scaled to fit remaining 1671 space, if any. 1672 1673 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1674 1675 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1676 SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader. 1677 If paint contains SkMaskFilter, generate mask from bitmap bounds. If paint 1678 SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all 1679 other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels. 1680 Any SkMaskFilter on paint is ignored as is paint anti-aliasing state. 1681 1682 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1683 just as SkShader made from SkShader::MakeBitmapShader with 1684 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1685 outside of its bounds. 1686 1687 @param bitmap SkBitmap containing pixels, dimensions, and format 1688 @param center SkIRect edge of image corners and sides 1689 @param dst destination SkRect of image to draw to 1690 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1691 and so on; or nullptr 1692 */ 1693 void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, 1694 const SkPaint* paint = nullptr); 1695 1696 /** \struct SkCanvas::Lattice 1697 SkCanvas::Lattice divides SkBitmap or SkImage into a rectangular grid. 1698 Grid entries on even columns and even rows are fixed; these entries are 1699 always drawn at their original size if the destination is large enough. 1700 If the destination side is too small to hold the fixed entries, all fixed 1701 entries are proportionately scaled down to fit. 1702 The grid entries not on even columns and rows are scaled to fit the 1703 remaining space, if any. 1704 */ 1705 struct Lattice { 1706 1707 /** \enum SkCanvas::Lattice::RectType 1708 Optional setting per rectangular grid entry to make it transparent, 1709 or to fill the grid entry with a color. 1710 */ 1711 enum RectType : uint8_t { 1712 kDefault = 0, //!< draws SkBitmap into lattice rectangle 1713 kTransparent, //!< skips lattice rectangle by making it transparent 1714 kFixedColor, //!< draws one of fColors into lattice rectangle 1715 }; 1716 1717 const int* fXDivs; //!< x-axis values dividing bitmap 1718 const int* fYDivs; //!< y-axis values dividing bitmap 1719 const RectType* fRectTypes; //!< array of fill types 1720 int fXCount; //!< number of x-coordinates 1721 int fYCount; //!< number of y-coordinates 1722 const SkIRect* fBounds; //!< source bounds to draw from 1723 const SkColor* fColors; //!< array of colors 1724 }; 1725 1726 /** Draws SkBitmap bitmap stretched proportionally to fit into SkRect dst. 1727 1728 SkCanvas::Lattice lattice divides bitmap into a rectangular grid. 1729 Each intersection of an even-numbered row and column is fixed; like the corners 1730 of drawBitmapNine(), fixed lattice elements never scale larger than their initial 1731 size and shrink proportionately when all fixed elements exceed the bitmap 1732 dimension. All other grid elements scale to fill the available space, if any. 1733 1734 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1735 1736 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1737 SkBlendMode, and SkDrawLooper. If bitmap is kAlpha_8_SkColorType, apply SkShader. 1738 If paint contains SkMaskFilter, generate mask from bitmap bounds. If paint 1739 SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all 1740 other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels. 1741 Any SkMaskFilter on paint is ignored as is paint anti-aliasing state. 1742 1743 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1744 just as SkShader made from SkShader::MakeBitmapShader with 1745 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1746 outside of its bounds. 1747 1748 @param bitmap SkBitmap containing pixels, dimensions, and format 1749 @param lattice division of bitmap into fixed and variable rectangles 1750 @param dst destination SkRect of image to draw to 1751 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1752 and so on; or nullptr 1753 */ 1754 void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst, 1755 const SkPaint* paint = nullptr); 1756 1757 /** Draws SkImage image stretched proportionally to fit into SkRect dst. 1758 1759 SkCanvas::Lattice lattice divides image into a rectangular grid. 1760 Each intersection of an even-numbered row and column is fixed; like the corners 1761 of drawBitmapNine(), fixed lattice elements never scale larger than their initial 1762 size and shrink proportionately when all fixed elements exceed the bitmap 1763 dimension. All other grid elements scale to fill the available space, if any. 1764 1765 Additionally transform draw using clip, SkMatrix, and optional SkPaint paint. 1766 1767 If SkPaint paint is supplied, apply SkColorFilter, alpha, SkImageFilter, 1768 SkBlendMode, and SkDrawLooper. If image is kAlpha_8_SkColorType, apply SkShader. 1769 If paint contains SkMaskFilter, generate mask from image bounds. If paint 1770 SkFilterQuality set to kNone_SkFilterQuality, disable pixel filtering. For all 1771 other values of paint SkFilterQuality, use kLow_SkFilterQuality to filter pixels. 1772 Any SkMaskFilter on paint is ignored as is paint anti-aliasing state. 1773 1774 If generated mask extends beyond bitmap bounds, replicate bitmap edge colors, 1775 just as SkShader made from SkShader::MakeBitmapShader with 1776 SkShader::kClamp_TileMode set replicates the bitmap edge color when it samples 1777 outside of its bounds. 1778 1779 @param image SkImage containing pixels, dimensions, and format 1780 @param lattice division of bitmap into fixed and variable rectangles 1781 @param dst destination SkRect of image to draw to 1782 @param paint SkPaint containing SkBlendMode, SkColorFilter, SkImageFilter, 1783 and so on; or nullptr 1784 */ 1785 void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, 1786 const SkPaint* paint = nullptr); 1787 1788 /** 1789 * Experimental. Controls anti-aliasing of each edge of images in an image-set. 1790 */ 1791 enum QuadAAFlags : unsigned { 1792 kLeft_QuadAAFlag = 0b0001, 1793 kTop_QuadAAFlag = 0b0010, 1794 kRight_QuadAAFlag = 0b0100, 1795 kBottom_QuadAAFlag = 0b1000, 1796 1797 kNone_QuadAAFlags = 0b0000, 1798 kAll_QuadAAFlags = 0b1111, 1799 }; 1800 1801 /** This is used by the experimental API below. */ 1802 struct ImageSetEntry { 1803 sk_sp<const SkImage> fImage; 1804 SkRect fSrcRect; 1805 SkRect fDstRect; 1806 float fAlpha; 1807 unsigned fAAFlags; // QuadAAFlags 1808 }; 1809 1810 /** 1811 * This is an experimental API for the SkiaRenderer Chromium project. The signature will 1812 * surely evolve if this is not removed. It currently offers no performance advantage over 1813 * drawing images independently, though may in the future. The antialiasing flags are intended 1814 * to allow control over each edge's AA status, to allow perfect seaming for tile sets. The 1815 * current implementation only antialiases if all edges are flagged, however. 1816 * Results are undefined if an image's src rect is not within the image's bounds. 1817 */ 1818 void experimental_DrawImageSetV1(const ImageSetEntry imageSet[], int cnt, 1819 SkFilterQuality quality, SkBlendMode mode); 1820 1821 /** 1822 * This is an experimental API for the SkiaRenderer Chromium project. The signature will 1823 * surely evolve if this is not removed. The antialiasing flags are intended to allow control 1824 * over each edge's AA status, to allow perfect seaming for tile sets. 1825 * 1826 * When not fully supported, the implementation only antialiases if all edges are flagged. 1827 */ 1828 void experimental_DrawEdgeAARectV1(const SkRect& r, QuadAAFlags edgeAA, SkColor color, 1829 SkBlendMode mode); 1830 1831 /** Draws text, with origin at (x, y), using clip, SkMatrix, SkFont font, 1832 and SkPaint paint. 1833 1834 When encoding is SkTextEncoding::kUTF8, SkTextEncoding::kUTF16, or 1835 SkTextEncoding::kUTF32, this function uses the default 1836 character-to-glyph mapping from the SkTypeface in font. It does not 1837 perform typeface fallback for characters not found in the SkTypeface. 1838 It does not perform kerning or other complex shaping; glyphs are 1839 positioned based on their default advances. 1840 1841 Text meaning depends on SkTextEncoding. 1842 1843 Text size is affected by SkMatrix and SkFont text size. Default text 1844 size is 12 point. 1845 1846 All elements of paint: SkPathEffect, SkMaskFilter, SkShader, 1847 SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By 1848 default, draws filled black glyphs. 1849 1850 @param text character code points or glyphs drawn 1851 @param byteLength byte length of text array 1852 @param encoding text encoding used in the text array 1853 @param x start of text on x-axis 1854 @param y start of text on y-axis 1855 @param font typeface, text size and so, used to describe the text 1856 @param paint blend, color, and so on, used to draw 1857 */ 1858 void drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding, 1859 SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint); 1860 1861 /** Experimental. 1862 1863 Draws null terminated string, with origin at (x, y), using clip, SkMatrix, 1864 SkFont font, and SkPaint paint. 1865 1866 This function uses the default character-to-glyph mapping from the 1867 SkTypeface in font. It does not perform typeface fallback for 1868 characters not found in the SkTypeface. It does not perform kerning; 1869 glyphs are positioned based on their default advances. 1870 1871 String str is encoded as UTF-8. 1872 1873 Text size is affected by SkMatrix and font text size. Default text 1874 size is 12 point. 1875 1876 All elements of paint: SkPathEffect, SkMaskFilter, SkShader, 1877 SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By 1878 default, draws filled black glyphs. 1879 1880 @param str character code points drawn, 1881 ending with a char value of zero 1882 @param x start of string on x-axis 1883 @param y start of string on y-axis 1884 @param font typeface, text size and so, used to describe the text 1885 @param paint blend, color, and so on, used to draw 1886 */ drawString(const char str[],SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint)1887 void drawString(const char str[], SkScalar x, SkScalar y, const SkFont& font, 1888 const SkPaint& paint) { 1889 this->drawSimpleText(str, strlen(str), kUTF8_SkTextEncoding, x, y, font, paint); 1890 } 1891 1892 /** Experimental. 1893 1894 Draws SkString, with origin at (x, y), using clip, SkMatrix, SkFont font, 1895 and SkPaint paint. 1896 1897 This function uses the default character-to-glyph mapping from the 1898 SkTypeface in font. It does not perform typeface fallback for 1899 characters not found in the SkTypeface. It does not perform kerning; 1900 glyphs are positioned based on their default advances. 1901 1902 SkString str is encoded as UTF-8. 1903 1904 Text size is affected by SkMatrix and SkFont text size. Default text 1905 size is 12 point. 1906 1907 All elements of paint: SkPathEffect, SkMaskFilter, SkShader, 1908 SkColorFilter, SkImageFilter, and SkDrawLooper; apply to text. By 1909 default, draws filled black glyphs. 1910 1911 @param str character code points drawn, 1912 ending with a char value of zero 1913 @param x start of string on x-axis 1914 @param y start of string on y-axis 1915 @param font typeface, text size and so, used to describe the text 1916 @param paint blend, color, and so on, used to draw 1917 */ drawString(const SkString & str,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint)1918 void drawString(const SkString& str, SkScalar x, SkScalar y, const SkFont& font, 1919 const SkPaint& paint) { 1920 this->drawSimpleText(str.c_str(), str.size(), kUTF8_SkTextEncoding, x, y, font, paint); 1921 } 1922 1923 /** Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint. 1924 1925 blob contains glyphs, their positions, and paint attributes specific to text: 1926 SkTypeface, SkPaint text size, SkPaint text scale x, 1927 SkPaint text skew x, SkPaint::Align, SkPaint::Hinting, anti-alias, SkPaint fake bold, 1928 SkPaint font embedded bitmaps, SkPaint full hinting spacing, LCD text, SkPaint linear text, 1929 and SkPaint subpixel text. 1930 1931 SkTextEncoding must be set to kGlyphID_SkTextEncoding. 1932 1933 Elements of paint: anti-alias, SkBlendMode, color including alpha, 1934 SkColorFilter, SkPaint dither, SkDrawLooper, SkMaskFilter, SkPathEffect, SkShader, and 1935 SkPaint::Style; apply to blob. If SkPaint contains SkPaint::kStroke_Style: 1936 SkPaint miter limit, SkPaint::Cap, SkPaint::Join, and SkPaint stroke width; 1937 apply to SkPath created from blob. 1938 1939 @param blob glyphs, positions, and their paints' text size, typeface, and so on 1940 @param x horizontal offset applied to blob 1941 @param y vertical offset applied to blob 1942 @param paint blend, color, stroking, and so on, used to draw 1943 */ 1944 void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint); 1945 1946 /** Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint. 1947 1948 blob contains glyphs, their positions, and paint attributes specific to text: 1949 SkTypeface, SkPaint text size, SkPaint text scale x, 1950 SkPaint text skew x, SkPaint::Align, SkPaint::Hinting, anti-alias, SkPaint fake bold, 1951 SkPaint font embedded bitmaps, SkPaint full hinting spacing, LCD text, SkPaint linear text, 1952 and SkPaint subpixel text. 1953 1954 SkTextEncoding must be set to kGlyphID_SkTextEncoding. 1955 1956 Elements of paint: SkPathEffect, SkMaskFilter, SkShader, SkColorFilter, 1957 SkImageFilter, and SkDrawLooper; apply to blob. 1958 1959 @param blob glyphs, positions, and their paints' text size, typeface, and so on 1960 @param x horizontal offset applied to blob 1961 @param y vertical offset applied to blob 1962 @param paint blend, color, stroking, and so on, used to draw 1963 */ drawTextBlob(const sk_sp<SkTextBlob> & blob,SkScalar x,SkScalar y,const SkPaint & paint)1964 void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) { 1965 this->drawTextBlob(blob.get(), x, y, paint); 1966 } 1967 1968 /** Draws SkPicture picture, using clip and SkMatrix. 1969 Clip and SkMatrix are unchanged by picture contents, as if 1970 save() was called before and restore() was called after drawPicture(). 1971 1972 SkPicture records a series of draw commands for later playback. 1973 1974 @param picture recorded drawing commands to play 1975 */ drawPicture(const SkPicture * picture)1976 void drawPicture(const SkPicture* picture) { 1977 this->drawPicture(picture, nullptr, nullptr); 1978 } 1979 1980 /** Draws SkPicture picture, using clip and SkMatrix. 1981 Clip and SkMatrix are unchanged by picture contents, as if 1982 save() was called before and restore() was called after drawPicture(). 1983 1984 SkPicture records a series of draw commands for later playback. 1985 1986 @param picture recorded drawing commands to play 1987 */ drawPicture(const sk_sp<SkPicture> & picture)1988 void drawPicture(const sk_sp<SkPicture>& picture) { 1989 this->drawPicture(picture.get()); 1990 } 1991 1992 /** Draws SkPicture picture, using clip and SkMatrix; transforming picture with 1993 SkMatrix matrix, if provided; and use SkPaint paint alpha, SkColorFilter, 1994 SkImageFilter, and SkBlendMode, if provided. 1995 1996 matrix transformation is equivalent to: save(), concat(), drawPicture(), restore(). 1997 paint use is equivalent to: saveLayer(), drawPicture(), restore(). 1998 1999 @param picture recorded drawing commands to play 2000 @param matrix SkMatrix to rotate, scale, translate, and so on; may be nullptr 2001 @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr 2002 */ 2003 void drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint); 2004 2005 /** Draws SkPicture picture, using clip and SkMatrix; transforming picture with 2006 SkMatrix matrix, if provided; and use SkPaint paint alpha, SkColorFilter, 2007 SkImageFilter, and SkBlendMode, if provided. 2008 2009 matrix transformation is equivalent to: save(), concat(), drawPicture(), restore(). 2010 paint use is equivalent to: saveLayer(), drawPicture(), restore(). 2011 2012 @param picture recorded drawing commands to play 2013 @param matrix SkMatrix to rotate, scale, translate, and so on; may be nullptr 2014 @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr 2015 */ drawPicture(const sk_sp<SkPicture> & picture,const SkMatrix * matrix,const SkPaint * paint)2016 void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, 2017 const SkPaint* paint) { 2018 this->drawPicture(picture.get(), matrix, paint); 2019 } 2020 2021 /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix. 2022 If vertices texs and vertices colors are defined in vertices, and SkPaint paint 2023 contains SkShader, SkBlendMode mode combines vertices colors with SkShader. 2024 2025 @param vertices triangle mesh to draw 2026 @param mode combines vertices colors with SkShader, if both are present 2027 @param paint specifies the SkShader, used as SkVertices texture; may be nullptr 2028 */ 2029 void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint); 2030 2031 /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix. 2032 If vertices texs and vertices colors are defined in vertices, and SkPaint paint 2033 contains SkShader, SkBlendMode mode combines vertices colors with SkShader. 2034 2035 @param vertices triangle mesh to draw 2036 @param mode combines vertices colors with SkShader, if both are present 2037 @param paint specifies the SkShader, used as SkVertices texture, may be nullptr 2038 */ 2039 void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint); 2040 2041 /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix. Bone data is used to 2042 deform vertices with bone weights. 2043 If vertices texs and vertices colors are defined in vertices, and SkPaint paint 2044 contains SkShader, SkBlendMode mode combines vertices colors with SkShader. 2045 The first element of bones should be an object to world space transformation matrix that 2046 will be applied before performing mesh deformations. If no such transformation is needed, 2047 it should be the identity matrix. 2048 boneCount must be at most 80, and thus the size of bones should be at most 80. 2049 2050 @param vertices triangle mesh to draw 2051 @param bones bone matrix data 2052 @param boneCount number of bone matrices 2053 @param mode combines vertices colors with SkShader, if both are present 2054 @param paint specifies the SkShader, used as SkVertices texture, may be nullptr 2055 */ 2056 void drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[], int boneCount, 2057 SkBlendMode mode, const SkPaint& paint); 2058 2059 /** Draws SkVertices vertices, a triangle mesh, using clip and SkMatrix. Bone data is used to 2060 deform vertices with bone weights. 2061 If vertices texs and vertices colors are defined in vertices, and SkPaint paint 2062 contains SkShader, SkBlendMode mode combines vertices colors with SkShader. 2063 The first element of bones should be an object to world space transformation matrix that 2064 will be applied before performing mesh deformations. If no such transformation is needed, 2065 it should be the identity matrix. 2066 boneCount must be at most 80, and thus the size of bones should be at most 80. 2067 2068 @param vertices triangle mesh to draw 2069 @param bones bone matrix data 2070 @param boneCount number of bone matrices 2071 @param mode combines vertices colors with SkShader, if both are present 2072 @param paint specifies the SkShader, used as SkVertices texture, may be nullptr 2073 */ 2074 void drawVertices(const sk_sp<SkVertices>& vertices, const SkVertices::Bone bones[], 2075 int boneCount, SkBlendMode mode, const SkPaint& paint); 2076 2077 /** Draws a Coons patch: the interpolation of four cubics with shared corners, 2078 associating a color, and optionally a texture SkPoint, with each corner. 2079 2080 Coons patch uses clip and SkMatrix, paint SkShader, SkColorFilter, 2081 alpha, SkImageFilter, and SkBlendMode. If SkShader is provided it is treated 2082 as Coons patch texture; SkBlendMode mode combines color colors and SkShader if 2083 both are provided. 2084 2085 SkPoint array cubics specifies four SkPath cubic starting at the top-left corner, 2086 in clockwise order, sharing every fourth point. The last SkPath cubic ends at the 2087 first point. 2088 2089 Color array color associates colors with corners in top-left, top-right, 2090 bottom-right, bottom-left order. 2091 2092 If paint contains SkShader, SkPoint array texCoords maps SkShader as texture to 2093 corners in top-left, top-right, bottom-right, bottom-left order. 2094 2095 @param cubics SkPath cubic array, sharing common points 2096 @param colors color array, one for each corner 2097 @param texCoords SkPoint array of texture coordinates, mapping SkShader to corners; 2098 may be nullptr 2099 @param mode SkBlendMode for colors, and for SkShader if paint has one 2100 @param paint SkShader, SkColorFilter, SkBlendMode, used to draw 2101 */ 2102 void drawPatch(const SkPoint cubics[12], const SkColor colors[4], 2103 const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint); 2104 2105 /** Draws SkPath cubic Coons patch: the interpolation of four cubics with shared corners, 2106 associating a color, and optionally a texture SkPoint, with each corner. 2107 2108 Coons patch uses clip and SkMatrix, paint SkShader, SkColorFilter, 2109 alpha, SkImageFilter, and SkBlendMode. If SkShader is provided it is treated 2110 as Coons patch texture; SkBlendMode mode combines color colors and SkShader if 2111 both are provided. 2112 2113 SkPoint array cubics specifies four SkPath cubic starting at the top-left corner, 2114 in clockwise order, sharing every fourth point. The last SkPath cubic ends at the 2115 first point. 2116 2117 Color array color associates colors with corners in top-left, top-right, 2118 bottom-right, bottom-left order. 2119 2120 If paint contains SkShader, SkPoint array texCoords maps SkShader as texture to 2121 corners in top-left, top-right, bottom-right, bottom-left order. 2122 2123 @param cubics SkPath cubic array, sharing common points 2124 @param colors color array, one for each corner 2125 @param texCoords SkPoint array of texture coordinates, mapping SkShader to corners; 2126 may be nullptr 2127 @param paint SkShader, SkColorFilter, SkBlendMode, used to draw 2128 */ drawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],const SkPaint & paint)2129 void drawPatch(const SkPoint cubics[12], const SkColor colors[4], 2130 const SkPoint texCoords[4], const SkPaint& paint) { 2131 this->drawPatch(cubics, colors, texCoords, SkBlendMode::kModulate, paint); 2132 } 2133 2134 /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint. 2135 paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode 2136 to draw, if present. For each entry in the array, SkRect tex locates sprite in 2137 atlas, and SkRSXform xform transforms it into destination space. 2138 2139 xform, text, and colors if present, must contain count entries. 2140 Optional colors are applied for each sprite using SkBlendMode mode, treating 2141 sprite as source and colors as destination. 2142 Optional cullRect is a conservative bounds of all transformed sprites. 2143 If cullRect is outside of clip, canvas can skip drawing. 2144 2145 @param atlas SkImage containing sprites 2146 @param xform SkRSXform mappings for sprites in atlas 2147 @param tex SkRect locations of sprites in atlas 2148 @param colors one per sprite, blended with sprite using SkBlendMode; may be nullptr 2149 @param count number of sprites to draw 2150 @param mode SkBlendMode combining colors and sprites 2151 @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr 2152 @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr 2153 */ 2154 void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], 2155 const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect, 2156 const SkPaint* paint); 2157 2158 /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint. 2159 paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode 2160 to draw, if present. For each entry in the array, SkRect tex locates sprite in 2161 atlas, and SkRSXform xform transforms it into destination space. 2162 2163 xform, text, and colors if present, must contain count entries. 2164 Optional colors is applied for each sprite using SkBlendMode. 2165 Optional cullRect is a conservative bounds of all transformed sprites. 2166 If cullRect is outside of clip, canvas can skip drawing. 2167 2168 @param atlas SkImage containing sprites 2169 @param xform SkRSXform mappings for sprites in atlas 2170 @param tex SkRect locations of sprites in atlas 2171 @param colors one per sprite, blended with sprite using SkBlendMode; may be nullptr 2172 @param count number of sprites to draw 2173 @param mode SkBlendMode combining colors and sprites 2174 @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr 2175 @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr 2176 */ drawAtlas(const sk_sp<SkImage> & atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkRect * cullRect,const SkPaint * paint)2177 void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[], 2178 const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect, 2179 const SkPaint* paint) { 2180 this->drawAtlas(atlas.get(), xform, tex, colors, count, mode, cullRect, paint); 2181 } 2182 2183 /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint. 2184 paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode 2185 to draw, if present. For each entry in the array, SkRect tex locates sprite in 2186 atlas, and SkRSXform xform transforms it into destination space. 2187 2188 xform and text must contain count entries. 2189 Optional cullRect is a conservative bounds of all transformed sprites. 2190 If cullRect is outside of clip, canvas can skip drawing. 2191 2192 @param atlas SkImage containing sprites 2193 @param xform SkRSXform mappings for sprites in atlas 2194 @param tex SkRect locations of sprites in atlas 2195 @param count number of sprites to draw 2196 @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr 2197 @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr 2198 */ drawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],int count,const SkRect * cullRect,const SkPaint * paint)2199 void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count, 2200 const SkRect* cullRect, const SkPaint* paint) { 2201 this->drawAtlas(atlas, xform, tex, nullptr, count, SkBlendMode::kDst, cullRect, paint); 2202 } 2203 2204 /** Draws a set of sprites from atlas, using clip, SkMatrix, and optional SkPaint paint. 2205 paint uses anti-alias, alpha, SkColorFilter, SkImageFilter, and SkBlendMode 2206 to draw, if present. For each entry in the array, SkRect tex locates sprite in 2207 atlas, and SkRSXform xform transforms it into destination space. 2208 2209 xform and text must contain count entries. 2210 Optional cullRect is a conservative bounds of all transformed sprites. 2211 If cullRect is outside of clip, canvas can skip drawing. 2212 2213 @param atlas SkImage containing sprites 2214 @param xform SkRSXform mappings for sprites in atlas 2215 @param tex SkRect locations of sprites in atlas 2216 @param count number of sprites to draw 2217 @param cullRect bounds of transformed sprites for efficient clipping; may be nullptr 2218 @param paint SkColorFilter, SkImageFilter, SkBlendMode, and so on; may be nullptr 2219 */ drawAtlas(const sk_sp<SkImage> & atlas,const SkRSXform xform[],const SkRect tex[],int count,const SkRect * cullRect,const SkPaint * paint)2220 void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[], 2221 int count, const SkRect* cullRect, const SkPaint* paint) { 2222 this->drawAtlas(atlas.get(), xform, tex, nullptr, count, SkBlendMode::kDst, 2223 cullRect, paint); 2224 } 2225 2226 /** Draws SkDrawable drawable using clip and SkMatrix, concatenated with 2227 optional matrix. 2228 2229 If SkCanvas has an asynchronous implementation, as is the case 2230 when it is recording into SkPicture, then drawable will be referenced, 2231 so that SkDrawable::draw() can be called when the operation is finalized. To force 2232 immediate drawing, call SkDrawable::draw() instead. 2233 2234 @param drawable custom struct encapsulating drawing commands 2235 @param matrix transformation applied to drawing; may be nullptr 2236 */ 2237 void drawDrawable(SkDrawable* drawable, const SkMatrix* matrix = nullptr); 2238 2239 /** Draws SkDrawable drawable using clip and SkMatrix, offset by (x, y). 2240 2241 If SkCanvas has an asynchronous implementation, as is the case 2242 when it is recording into SkPicture, then drawable will be referenced, 2243 so that SkDrawable::draw() can be called when the operation is finalized. To force 2244 immediate drawing, call SkDrawable::draw() instead. 2245 2246 @param drawable custom struct encapsulating drawing commands 2247 @param x offset into SkCanvas writable pixels on x-axis 2248 @param y offset into SkCanvas writable pixels on y-axis 2249 */ 2250 void drawDrawable(SkDrawable* drawable, SkScalar x, SkScalar y); 2251 2252 /** Associates SkRect on SkCanvas with an annotation; a key-value pair, where the key is 2253 a null-terminated UTF-8 string, and optional value is stored as SkData. 2254 2255 Only some canvas implementations, such as recording to SkPicture, or drawing to 2256 document PDF, use annotations. 2257 2258 @param rect SkRect extent of canvas to annotate 2259 @param key string used for lookup 2260 @param value data holding value stored in annotation 2261 */ 2262 void drawAnnotation(const SkRect& rect, const char key[], SkData* value); 2263 2264 /** Associates SkRect on SkCanvas when an annotation; a key-value pair, where the key is 2265 a null-terminated UTF-8 string, and optional value is stored as SkData. 2266 2267 Only some canvas implementations, such as recording to SkPicture, or drawing to 2268 document PDF, use annotations. 2269 2270 @param rect SkRect extent of canvas to annotate 2271 @param key string used for lookup 2272 @param value data holding value stored in annotation 2273 */ drawAnnotation(const SkRect & rect,const char key[],const sk_sp<SkData> & value)2274 void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) { 2275 this->drawAnnotation(rect, key, value.get()); 2276 } 2277 2278 /** Returns true if clip is empty; that is, nothing will draw. 2279 2280 May do work when called; it should not be called 2281 more often than needed. However, once called, subsequent calls perform no 2282 work until clip changes. 2283 2284 @return true if clip is empty 2285 */ 2286 virtual bool isClipEmpty() const; 2287 2288 /** Returns true if clip is SkRect and not empty. 2289 Returns false if the clip is empty, or if it is not SkRect. 2290 2291 @return true if clip is SkRect and not empty 2292 */ 2293 virtual bool isClipRect() const; 2294 2295 /** Returns SkMatrix. 2296 This does not account for translation by SkBaseDevice or SkSurface. 2297 2298 @return SkMatrix in SkCanvas 2299 */ 2300 const SkMatrix& getTotalMatrix() const; 2301 2302 /////////////////////////////////////////////////////////////////////////// 2303 2304 // don't call 2305 virtual GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext(); internal_private_getTopLayerBounds()2306 SkIRect internal_private_getTopLayerBounds() const { return getTopLayerBounds(); } 2307 2308 // TEMP helpers until we switch virtual over to const& for src-rect 2309 void legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, 2310 const SkPaint* paint, 2311 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 2312 void legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, 2313 const SkPaint* paint, 2314 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 2315 2316 /** 2317 * Returns the global clip as a region. If the clip contains AA, then only the bounds 2318 * of the clip may be returned. 2319 */ 2320 void temporary_internal_getRgnClip(SkRegion* region); 2321 2322 void private_draw_shadow_rec(const SkPath&, const SkDrawShadowRec&); 2323 2324 2325 protected: 2326 // default impl defers to getDevice()->newSurface(info) 2327 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props); 2328 2329 // default impl defers to its device 2330 virtual bool onPeekPixels(SkPixmap* pixmap); 2331 virtual bool onAccessTopLayerPixels(SkPixmap* pixmap); 2332 virtual SkImageInfo onImageInfo() const; 2333 virtual bool onGetProps(SkSurfaceProps* props) const; 2334 virtual void onFlush(); 2335 2336 // Subclass save/restore notifiers. 2337 // Overriders should call the corresponding INHERITED method up the inheritance chain. 2338 // getSaveLayerStrategy()'s return value may suppress full layer allocation. 2339 enum SaveLayerStrategy { 2340 kFullLayer_SaveLayerStrategy, 2341 kNoLayer_SaveLayerStrategy, 2342 }; 2343 willSave()2344 virtual void willSave() {} 2345 // Overriders should call the corresponding INHERITED method up the inheritance chain. getSaveLayerStrategy(const SaveLayerRec &)2346 virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& ) { 2347 return kFullLayer_SaveLayerStrategy; 2348 } 2349 // returns true if we should actually perform the saveBehind, or false if we should just save. onDoSaveBehind(const SkRect *)2350 virtual bool onDoSaveBehind(const SkRect*) { return true; } willRestore()2351 virtual void willRestore() {} didRestore()2352 virtual void didRestore() {} didConcat(const SkMatrix &)2353 virtual void didConcat(const SkMatrix& ) {} didSetMatrix(const SkMatrix &)2354 virtual void didSetMatrix(const SkMatrix& ) {} didTranslate(SkScalar dx,SkScalar dy)2355 virtual void didTranslate(SkScalar dx, SkScalar dy) { 2356 this->didConcat(SkMatrix::MakeTrans(dx, dy)); 2357 } 2358 2359 // NOTE: If you are adding a new onDraw virtual to SkCanvas, PLEASE add an override to 2360 // SkCanvasVirtualEnforcer (in SkCanvasVirtualEnforcer.h). This ensures that subclasses using 2361 // that mechanism will be required to implement the new function. 2362 virtual void onDrawPaint(const SkPaint& paint); 2363 virtual void onDrawRect(const SkRect& rect, const SkPaint& paint); 2364 virtual void onDrawEdgeAARect(const SkRect& rect, QuadAAFlags edgeAA, SkColor color, 2365 SkBlendMode mode); 2366 virtual void onDrawRRect(const SkRRect& rrect, const SkPaint& paint); 2367 virtual void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint); 2368 virtual void onDrawOval(const SkRect& rect, const SkPaint& paint); 2369 virtual void onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle, 2370 bool useCenter, const SkPaint& paint); 2371 virtual void onDrawPath(const SkPath& path, const SkPaint& paint); 2372 virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint); 2373 2374 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 2375 const SkPaint& paint); 2376 2377 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 2378 const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint); 2379 virtual void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], 2380 const SkPaint& paint); 2381 2382 // TODO: Remove old signature onDrawVerticesObject(const SkVertices * vertices,SkBlendMode mode,const SkPaint & paint)2383 virtual void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode, 2384 const SkPaint& paint) { 2385 this->onDrawVerticesObject(vertices, nullptr, 0, mode, paint); 2386 } 2387 virtual void onDrawVerticesObject(const SkVertices* vertices, const SkVertices::Bone bones[], 2388 int boneCount, SkBlendMode mode, const SkPaint& paint); 2389 2390 virtual void onDrawImage(const SkImage* image, SkScalar dx, SkScalar dy, const SkPaint* paint); 2391 virtual void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, 2392 const SkPaint* paint, SrcRectConstraint constraint); 2393 virtual void onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, 2394 const SkPaint* paint); 2395 virtual void onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, 2396 const SkPaint* paint); 2397 2398 virtual void onDrawImageSet(const ImageSetEntry imageSet[], int count, SkFilterQuality, 2399 SkBlendMode); 2400 2401 virtual void onDrawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, 2402 const SkPaint* paint); 2403 virtual void onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, 2404 const SkPaint* paint, SrcRectConstraint constraint); 2405 virtual void onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, 2406 const SkPaint* paint); 2407 virtual void onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, 2408 const SkRect& dst, const SkPaint* paint); 2409 2410 virtual void onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect rect[], 2411 const SkColor colors[], int count, SkBlendMode mode, 2412 const SkRect* cull, const SkPaint* paint); 2413 2414 virtual void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value); 2415 virtual void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&); 2416 2417 virtual void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix); 2418 virtual void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, 2419 const SkPaint* paint); 2420 2421 enum ClipEdgeStyle { 2422 kHard_ClipEdgeStyle, 2423 kSoft_ClipEdgeStyle 2424 }; 2425 2426 virtual void onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle); 2427 virtual void onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle); 2428 virtual void onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle); 2429 virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp op); 2430 2431 virtual void onDiscard(); 2432 2433 // Clip rectangle bounds. Called internally by saveLayer. 2434 // returns false if the entire rectangle is entirely clipped out 2435 // If non-NULL, The imageFilter parameter will be used to expand the clip 2436 // and offscreen bounds for any margin required by the filter DAG. 2437 bool clipRectBounds(const SkRect* bounds, SaveLayerFlags flags, SkIRect* intersection, 2438 const SkImageFilter* imageFilter = nullptr); 2439 2440 SkBaseDevice* getTopDevice() const; 2441 2442 private: 2443 /** After calling saveLayer(), there can be any number of devices that make 2444 up the top-most drawing area. LayerIter can be used to iterate through 2445 those devices. Note that the iterator is only valid until the next API 2446 call made on the canvas. Ownership of all pointers in the iterator stays 2447 with the canvas, so none of them should be modified or deleted. 2448 */ 2449 class LayerIter /*: SkNoncopyable*/ { 2450 public: 2451 /** Initialize iterator with canvas, and set values for 1st device */ 2452 LayerIter(SkCanvas*); 2453 ~LayerIter(); 2454 2455 /** Return true if the iterator is done */ done()2456 bool done() const { return fDone; } 2457 /** Cycle to the next device */ 2458 void next(); 2459 2460 // These reflect the current device in the iterator 2461 2462 SkBaseDevice* device() const; 2463 const SkMatrix& matrix() const; 2464 SkIRect clipBounds() const; 2465 const SkPaint& paint() const; 2466 int x() const; 2467 int y() const; 2468 2469 private: 2470 // used to embed the SkDrawIter object directly in our instance, w/o 2471 // having to expose that class def to the public. There is an assert 2472 // in our constructor to ensure that fStorage is large enough 2473 // (though needs to be a compile-time-assert!). We use intptr_t to work 2474 // safely with 32 and 64 bit machines (to ensure the storage is enough) 2475 intptr_t fStorage[32]; 2476 class SkDrawIter* fImpl; // this points at fStorage 2477 SkPaint fDefaultPaint; 2478 bool fDone; 2479 }; 2480 2481 static bool BoundsAffectsClip(SaveLayerFlags); 2482 2483 static void DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter, 2484 SkBaseDevice* dst, const SkIPoint& dstOrigin, 2485 const SkMatrix& ctm); 2486 2487 enum ShaderOverrideOpacity { 2488 kNone_ShaderOverrideOpacity, //!< there is no overriding shader (bitmap or image) 2489 kOpaque_ShaderOverrideOpacity, //!< the overriding shader is opaque 2490 kNotOpaque_ShaderOverrideOpacity, //!< the overriding shader may not be opaque 2491 }; 2492 2493 // notify our surface (if we have one) that we are about to draw, so it 2494 // can perform copy-on-write or invalidate any cached images 2495 void predrawNotify(bool willOverwritesEntireSurface = false); 2496 void predrawNotify(const SkRect* rect, const SkPaint* paint, ShaderOverrideOpacity); predrawNotify(const SkRect * rect,const SkPaint * paint,bool shaderOverrideIsOpaque)2497 void predrawNotify(const SkRect* rect, const SkPaint* paint, bool shaderOverrideIsOpaque) { 2498 this->predrawNotify(rect, paint, shaderOverrideIsOpaque ? kOpaque_ShaderOverrideOpacity 2499 : kNotOpaque_ShaderOverrideOpacity); 2500 } 2501 2502 SkBaseDevice* getDevice() const; 2503 2504 class MCRec; 2505 2506 SkDeque fMCStack; 2507 // points to top of stack 2508 MCRec* fMCRec; 2509 2510 // the first N recs that can fit here mean we won't call malloc 2511 static constexpr int kMCRecSize = 128; // most recent measurement 2512 static constexpr int kMCRecCount = 32; // common depth for save/restores 2513 static constexpr int kDeviceCMSize = 224; // most recent measurement 2514 2515 intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)]; 2516 intptr_t fDeviceCMStorage[kDeviceCMSize / sizeof(intptr_t)]; 2517 2518 const SkSurfaceProps fProps; 2519 2520 int fSaveCount; // value returned by getSaveCount() 2521 2522 SkMetaData* fMetaData; 2523 std::unique_ptr<SkRasterHandleAllocator> fAllocator; 2524 2525 SkSurface_Base* fSurfaceBase; getSurfaceBase()2526 SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; } setSurfaceBase(SkSurface_Base * sb)2527 void setSurfaceBase(SkSurface_Base* sb) { 2528 fSurfaceBase = sb; 2529 } 2530 friend class SkSurface_Base; 2531 friend class SkSurface_Gpu; 2532 2533 SkIRect fClipRestrictionRect = SkIRect::MakeEmpty(); 2534 2535 void doSave(); 2536 void checkForDeferredSave(); 2537 void internalSetMatrix(const SkMatrix&); 2538 2539 friend class SkAndroidFrameworkUtils; 2540 friend class SkCanvasPriv; // needs kDontClipToLayer_PrivateSaveLayerFlag 2541 friend class SkDrawIter; // needs setupDrawForLayerDevice() 2542 friend class AutoDrawLooper; 2543 friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip 2544 friend class SkSurface_Raster; // needs getDevice() 2545 friend class SkNoDrawCanvas; // needs resetForNextPicture() 2546 friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>) 2547 friend class SkOverdrawCanvas; 2548 friend class SkRasterHandleAllocator; 2549 2550 protected: 2551 // For use by SkNoDrawCanvas (via SkCanvasVirtualEnforcer, which can't be a friend) 2552 SkCanvas(const SkIRect& bounds); 2553 private: 2554 SkCanvas(const SkBitmap&, std::unique_ptr<SkRasterHandleAllocator>, 2555 SkRasterHandleAllocator::Handle); 2556 2557 SkCanvas(SkCanvas&&) = delete; 2558 SkCanvas(const SkCanvas&) = delete; 2559 SkCanvas& operator=(SkCanvas&&) = delete; 2560 SkCanvas& operator=(const SkCanvas&) = delete; 2561 2562 /** Experimental 2563 * Saves the specified subset of the current pixels in the current layer, 2564 * and then clears those pixels to transparent black. 2565 * Restores the pixels on restore() by drawing them in SkBlendMode::kDstOver. 2566 * 2567 * @param subset conservative bounds of the area to be saved / restored. 2568 * @return depth of save state stack before this call was made. 2569 */ 2570 int only_axis_aligned_saveBehind(const SkRect* subset); 2571 2572 void resetForNextPicture(const SkIRect& bounds); 2573 2574 // needs gettotalclip() 2575 friend class SkCanvasStateUtils; 2576 2577 // call this each time we attach ourselves to a device 2578 // - constructor 2579 // - internalSaveLayer 2580 void setupDevice(SkBaseDevice*); 2581 2582 void init(sk_sp<SkBaseDevice>); 2583 2584 /** 2585 * Gets the bounds of the top level layer in global canvas coordinates. We don't want this 2586 * to be public because it exposes decisions about layer sizes that are internal to the canvas. 2587 */ 2588 SkIRect getTopLayerBounds() const; 2589 2590 void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, 2591 const SkRect& dst, const SkPaint* paint, 2592 SrcRectConstraint); 2593 void internalDrawPaint(const SkPaint& paint); 2594 void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy); 2595 void internalSaveBehind(const SkRect*); 2596 void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*, SkImage* clipImage, 2597 const SkMatrix& clipMatrix); 2598 2599 // shared by save() and saveLayer() 2600 void internalSave(); 2601 void internalRestore(); 2602 2603 /* 2604 * Returns true if drawing the specified rect (or all if it is null) with the specified 2605 * paint (or default if null) would overwrite the entire root device of the canvas 2606 * (i.e. the canvas' surface if it had one). 2607 */ 2608 bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const; 2609 2610 /** 2611 * Returns true if the paint's imagefilter can be invoked directly, without needed a layer. 2612 */ 2613 bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint&); 2614 2615 /** 2616 * Returns true if the clip (for any active layer) contains antialiasing. 2617 * If the clip is empty, this will return false. 2618 */ 2619 bool androidFramework_isClipAA() const; 2620 2621 /** 2622 * Keep track of the device clip bounds and if the matrix is scale-translate. This allows 2623 * us to do a fast quick reject in the common case. 2624 */ 2625 bool fIsScaleTranslate; 2626 SkRect fDeviceClipBounds; 2627 2628 bool fAllowSoftClip; 2629 bool fAllowSimplifyClip; 2630 2631 class AutoValidateClip { 2632 public: AutoValidateClip(SkCanvas * canvas)2633 explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) { 2634 fCanvas->validateClip(); 2635 } ~AutoValidateClip()2636 ~AutoValidateClip() { fCanvas->validateClip(); } 2637 2638 private: 2639 const SkCanvas* fCanvas; 2640 2641 AutoValidateClip(AutoValidateClip&&) = delete; 2642 AutoValidateClip(const AutoValidateClip&) = delete; 2643 AutoValidateClip& operator=(AutoValidateClip&&) = delete; 2644 AutoValidateClip& operator=(const AutoValidateClip&) = delete; 2645 }; 2646 2647 #ifdef SK_DEBUG 2648 void validateClip() const; 2649 #else validateClip()2650 void validateClip() const {} 2651 #endif 2652 2653 std::unique_ptr<SkGlyphRunBuilder> fScratchGlyphRunBuilder; 2654 2655 typedef SkRefCnt INHERITED; 2656 }; 2657 2658 /** \class SkAutoCanvasRestore 2659 Stack helper class calls SkCanvas::restoreToCount when SkAutoCanvasRestore 2660 goes out of scope. Use this to guarantee that the canvas is restored to a known 2661 state. 2662 */ 2663 class SkAutoCanvasRestore { 2664 public: 2665 2666 /** Preserves SkCanvas::save() count. Optionally saves SkCanvas clip and SkCanvas matrix. 2667 2668 @param canvas SkCanvas to guard 2669 @param doSave call SkCanvas::save() 2670 @return utility to restore SkCanvas state on destructor 2671 */ SkAutoCanvasRestore(SkCanvas * canvas,bool doSave)2672 SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) { 2673 if (fCanvas) { 2674 fSaveCount = canvas->getSaveCount(); 2675 if (doSave) { 2676 canvas->save(); 2677 } 2678 } 2679 } 2680 2681 /** Restores SkCanvas to saved state. Destructor is called when container goes out of 2682 scope. 2683 */ ~SkAutoCanvasRestore()2684 ~SkAutoCanvasRestore() { 2685 if (fCanvas) { 2686 fCanvas->restoreToCount(fSaveCount); 2687 } 2688 } 2689 2690 /** Restores SkCanvas to saved state immediately. Subsequent calls and 2691 ~SkAutoCanvasRestore() have no effect. 2692 */ restore()2693 void restore() { 2694 if (fCanvas) { 2695 fCanvas->restoreToCount(fSaveCount); 2696 fCanvas = nullptr; 2697 } 2698 } 2699 2700 private: 2701 SkCanvas* fCanvas; 2702 int fSaveCount; 2703 2704 SkAutoCanvasRestore(SkAutoCanvasRestore&&) = delete; 2705 SkAutoCanvasRestore(const SkAutoCanvasRestore&) = delete; 2706 SkAutoCanvasRestore& operator=(SkAutoCanvasRestore&&) = delete; 2707 SkAutoCanvasRestore& operator=(const SkAutoCanvasRestore&) = delete; 2708 }; 2709 2710 // Private 2711 #define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore) 2712 2713 #endif 2714