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