blob: 9d87ed29098e576355917a1dd789de0937fba03d [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkCanvas_DEFINED
9#define SkCanvas_DEFINED
10
11#include "SkTypes.h"
reed374772b2016-10-05 17:33:02 -070012#include "SkBlendMode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkBitmap.h"
reed73603f32016-09-20 08:42:38 -070014#include "SkClipOp.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkDeque.h"
msarette8597a42016-03-24 10:41:47 -070016#include "SkImage.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkPaint.h"
18#include "SkRefCnt.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkRegion.h"
reed4a8126e2014-09-22 07:29:03 -070020#include "SkSurfaceProps.h"
reed@android.com845fdac2009-06-23 03:01:32 +000021#include "SkXfermode.h"
vjiaoblack95302da2016-07-21 10:25:54 -070022#include "SkLights.h"
vjiaoblacke6f5d562016-08-25 06:30:23 -070023#include "../private/SkShadowParams.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000024
bungemand3ebb482015-08-05 13:57:49 -070025class GrContext;
robertphillips175dd9b2016-04-28 14:32:04 -070026class GrDrawContext;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000027class SkBaseDevice;
reed6a070dc2014-11-11 19:36:09 -080028class SkCanvasClipVisitor;
bungemand3ebb482015-08-05 13:57:49 -070029class SkClipStack;
reedf70b5312016-03-04 16:36:20 -080030class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031class SkDraw;
reed3cb38402015-02-06 08:36:15 -080032class SkDrawable;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkDrawFilter;
bungemand3ebb482015-08-05 13:57:49 -070034class SkImageFilter;
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +000035class SkMetaData;
bungemand3ebb482015-08-05 13:57:49 -070036class SkPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +000037class SkPicture;
bungemand3ebb482015-08-05 13:57:49 -070038class SkPixmap;
reed1e7f5e72016-04-27 07:49:17 -070039class SkRasterClip;
reed@google.com4ed0fb72012-12-12 20:48:18 +000040class SkRRect;
reed71c3c762015-06-24 10:29:17 -070041struct SkRSXform;
reed@google.com76f10a32014-02-05 15:32:21 +000042class SkSurface;
reed@google.com97af1a62012-08-28 12:19:02 +000043class SkSurface_Base;
fmalita00d5c2c2014-08-21 08:53:26 -070044class SkTextBlob;
reedfa35f8e2014-09-04 12:14:36 -070045
reed73603f32016-09-20 08:42:38 -070046//#define SK_SUPPORT_LEGACY_CLIP_REGIONOPS
47
reed@android.com8a1c16f2008-12-17 15:59:43 +000048/** \class SkCanvas
49
50 A Canvas encapsulates all of the state about drawing into a device (bitmap).
51 This includes a reference to the device itself, and a stack of matrix/clip
52 values. For any given draw call (e.g. drawRect), the geometry of the object
53 being drawn is transformed by the concatenation of all the matrices in the
54 stack. The transformed geometry is clipped by the intersection of all of
55 the clips in the stack.
56
57 While the Canvas holds the state of the drawing device, the state (style)
58 of the object being drawn is held by the Paint, which is provided as a
59 parameter to each of the draw() methods. The Paint holds attributes such as
60 color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns),
61 etc.
62*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000063class SK_API SkCanvas : public SkRefCnt {
reedbada1882015-12-21 13:09:44 -080064 enum PrivateSaveLayerFlags {
caryclark952538e2016-02-26 05:01:42 -080065 kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31,
reedbada1882015-12-21 13:09:44 -080066 };
reed73603f32016-09-20 08:42:38 -070067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068public:
reed73603f32016-09-20 08:42:38 -070069#ifdef SK_SUPPORT_LEGACY_CLIP_REGIONOPS
70 typedef SkRegion::Op ClipOp;
71
72 static const ClipOp kDifference_Op = SkRegion::kDifference_Op;
73 static const ClipOp kIntersect_Op = SkRegion::kIntersect_Op;
74 static const ClipOp kUnion_Op = SkRegion::kUnion_Op;
75 static const ClipOp kXOR_Op = SkRegion::kXOR_Op;
76 static const ClipOp kReverseDifference_Op = SkRegion::kReverseDifference_Op;
77 static const ClipOp kReplace_Op = SkRegion::kReplace_Op;
78#else
79 typedef SkClipOp ClipOp;
80
81 static const ClipOp kDifference_Op = kDifference_SkClipOp;
82 static const ClipOp kIntersect_Op = kIntersect_SkClipOp;
83 static const ClipOp kUnion_Op = kUnion_SkClipOp;
84 static const ClipOp kXOR_Op = kXOR_SkClipOp;
85 static const ClipOp kReverseDifference_Op = kReverseDifference_SkClipOp;
86 static const ClipOp kReplace_Op = kReplace_SkClipOp;
87#endif
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000088 /**
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000089 * Attempt to allocate raster canvas, matching the ImageInfo, that will draw directly into the
90 * specified pixels. To access the pixels after drawing to them, the caller should call
91 * flush() or call peekPixels(...).
92 *
93 * On failure, return NULL. This can fail for several reasons:
94 * 1. invalid ImageInfo (e.g. negative dimensions)
95 * 2. unsupported ImageInfo for a canvas
96 * - kUnknown_SkColorType, kIndex_8_SkColorType
reed44977482015-02-27 10:23:00 -080097 * - kUnknown_SkAlphaType
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000098 * - this list is not complete, so others may also be unsupported
99 *
100 * Note: it is valid to request a supported ImageInfo, but with zero
101 * dimensions.
102 */
103 static SkCanvas* NewRasterDirect(const SkImageInfo&, void*, size_t);
104
105 static SkCanvas* NewRasterDirectN32(int width, int height, SkPMColor* pixels, size_t rowBytes) {
106 return NewRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
107 }
108
109 /**
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000110 * Creates an empty canvas with no backing device/pixels, and zero
111 * dimensions.
112 */
reed@google.comcde92112011-07-06 20:00:52 +0000113 SkCanvas();
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000114
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000115 /**
116 * Creates a canvas of the specified dimensions, but explicitly not backed
117 * by any device/pixels. Typically this use used by subclasses who handle
118 * the draw calls in some other way.
119 */
reed96a857e2015-01-25 10:33:58 -0800120 SkCanvas(int width, int height, const SkSurfaceProps* = NULL);
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000121
reed@google.com6dc74552011-07-21 18:00:46 +0000122 /** Construct a canvas with the specified device to draw into.
bsalomon@google.come97f0852011-06-17 13:10:25 +0000123
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000124 @param device Specifies a device for the canvas to draw into.
125 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000126 explicit SkCanvas(SkBaseDevice* device);
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000127
reed@google.com44699382013-10-31 17:28:30 +0000128 /** Construct a canvas with the specified bitmap to draw into.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 @param bitmap Specifies a bitmap for the canvas to draw into. Its
130 structure are copied to the canvas.
131 */
132 explicit SkCanvas(const SkBitmap& bitmap);
fmalita3d91aad2015-02-02 05:25:04 -0800133
134 /** Construct a canvas with the specified bitmap to draw into.
135 @param bitmap Specifies a bitmap for the canvas to draw into. Its
136 structure are copied to the canvas.
137 @param props New canvas surface properties.
138 */
139 SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141 virtual ~SkCanvas();
142
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +0000143 SkMetaData& getMetaData();
144
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000145 /**
146 * Return ImageInfo for this canvas. If the canvas is not backed by pixels
147 * (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
148 */
149 SkImageInfo imageInfo() const;
150
brianosman898235c2016-04-06 07:38:23 -0700151 /**
152 * If the canvas is backed by pixels (cpu or gpu), this writes a copy of the SurfaceProps
153 * for the canvas to the location supplied by the caller, and returns true. Otherwise,
154 * return false and leave the supplied props unchanged.
155 */
156 bool getProps(SkSurfaceProps*) const;
157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 ///////////////////////////////////////////////////////////////////////////
159
reed@google.com210ce002011-11-01 14:24:23 +0000160 /**
bsalomone63ffef2016-02-05 07:17:34 -0800161 * Trigger the immediate execution of all pending draw operations. For the GPU
162 * backend this will resolve all rendering to the GPU surface backing the
163 * SkSurface that owns this canvas.
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000164 */
165 void flush();
166
167 /**
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000168 * Gets the size of the base or root layer in global canvas coordinates. The
169 * origin of the base layer is always (0,0). The current drawable area may be
170 * smaller (due to clipping or saveLayer).
reed@google.com210ce002011-11-01 14:24:23 +0000171 */
tomhudson68260fa2015-03-23 07:22:40 -0700172 virtual SkISize getBaseLayerSize() const;
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000173
174 /**
175 * DEPRECATED: call getBaseLayerSize
176 */
177 SkISize getDeviceSize() const { return this->getBaseLayerSize(); }
reed@google.com210ce002011-11-01 14:24:23 +0000178
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000179 /**
180 * DEPRECATED.
181 * Return the canvas' device object, which may be null. The device holds
182 * the bitmap of the pixels that the canvas draws into. The reference count
183 * of the returned device is not changed by this call.
184 */
reed52d9ac62014-06-30 09:05:34 -0700185#ifndef SK_SUPPORT_LEGACY_GETDEVICE
186protected: // Can we make this private?
187#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000188 SkBaseDevice* getDevice() const;
reed52d9ac62014-06-30 09:05:34 -0700189public:
reed41e010c2015-06-09 12:16:53 -0700190 SkBaseDevice* getDevice_just_for_deprecated_compatibility_testing() const {
191 return this->getDevice();
192 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193
reed@google.com9266fed2011-03-30 00:18:03 +0000194 /**
195 * saveLayer() can create another device (which is later drawn onto
196 * the previous device). getTopDevice() returns the top-most device current
197 * installed. Note that this can change on other calls like save/restore,
198 * so do not access this device after subsequent canvas calls.
199 * The reference count of the device is not changed.
reed@google.com0b53d592012-03-19 18:26:34 +0000200 *
201 * @param updateMatrixClip If this is true, then before the device is
202 * returned, we ensure that its has been notified about the current
203 * matrix and clip. Note: this happens automatically when the device
204 * is drawn to, but is optional here, as there is a small perf hit
205 * sometimes.
reed@google.com9266fed2011-03-30 00:18:03 +0000206 */
reed@google.com9c135db2014-03-12 18:28:35 +0000207#ifndef SK_SUPPORT_LEGACY_GETTOPDEVICE
208private:
209#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000210 SkBaseDevice* getTopDevice(bool updateMatrixClip = false) const;
reed@google.com9c135db2014-03-12 18:28:35 +0000211public:
reed@google.com9266fed2011-03-30 00:18:03 +0000212
reed@google.com76f10a32014-02-05 15:32:21 +0000213 /**
214 * Create a new surface matching the specified info, one that attempts to
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000215 * be maximally compatible when used with this canvas. If there is no matching Surface type,
216 * NULL is returned.
reed4a8126e2014-09-22 07:29:03 -0700217 *
218 * If surfaceprops is specified, those are passed to the new surface, otherwise the new surface
219 * inherits the properties of the surface that owns this canvas. If this canvas has no parent
220 * surface, then the new surface is created with default properties.
reed@google.com76f10a32014-02-05 15:32:21 +0000221 */
reede8f30622016-03-23 18:59:25 -0700222 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps* = nullptr);
223#ifdef SK_SUPPORT_LEGACY_NEW_SURFACE_API
224 SkSurface* newSurface(const SkImageInfo& info, const SkSurfaceProps* props = NULL);
225#endif
bsalomon@google.come97f0852011-06-17 13:10:25 +0000226
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000227 /**
228 * Return the GPU context of the device that is associated with the canvas.
229 * For a canvas with non-GPU device, NULL is returned.
230 */
231 GrContext* getGrContext();
232
reed@google.com4b226022011-01-11 18:32:13 +0000233 ///////////////////////////////////////////////////////////////////////////
234
bsalomon@google.comdaba14b2011-11-02 20:10:48 +0000235 /**
reed@google.com9c135db2014-03-12 18:28:35 +0000236 * If the canvas has writable pixels in its top layer (and is not recording to a picture
237 * or other non-raster target) and has direct access to its pixels (i.e. they are in
238 * local RAM) return the address of those pixels, and if not null,
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000239 * return the ImageInfo, rowBytes and origin. The returned address is only valid
reed@google.com9c135db2014-03-12 18:28:35 +0000240 * while the canvas object is in scope and unchanged. Any API calls made on
241 * canvas (or its parent surface if any) will invalidate the
242 * returned address (and associated information).
243 *
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000244 * On failure, returns NULL and the info, rowBytes, and origin parameters are ignored.
reed@google.com9c135db2014-03-12 18:28:35 +0000245 */
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000246 void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
reed@google.com9c135db2014-03-12 18:28:35 +0000247
248 /**
249 * If the canvas has readable pixels in its base layer (and is not recording to a picture
250 * or other non-raster target) and has direct access to its pixels (i.e. they are in
reed6ceeebd2016-03-09 14:26:26 -0800251 * local RAM) return true, and if not null, return in the pixmap parameter information about
252 * the pixels. The pixmap's pixel address is only valid
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000253 * while the canvas object is in scope and unchanged. Any API calls made on
reed6ceeebd2016-03-09 14:26:26 -0800254 * canvas (or its parent surface if any) will invalidate the pixel address
255 * (and associated information).
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000256 *
reed6ceeebd2016-03-09 14:26:26 -0800257 * On failure, returns false and the pixmap parameter will be ignored.
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000258 */
reed6ceeebd2016-03-09 14:26:26 -0800259 bool peekPixels(SkPixmap*);
260
261#ifdef SK_SUPPORT_LEGACY_PEEKPIXELS_PARMS
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000262 const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
reed6ceeebd2016-03-09 14:26:26 -0800263#endif
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000264
reed@google.com4b226022011-01-11 18:32:13 +0000265 /**
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000266 * Copy the pixels from the base-layer into the specified buffer (pixels + rowBytes),
267 * converting them into the requested format (SkImageInfo). The base-layer pixels are read
reedb184f7f2014-07-13 04:32:32 -0700268 * starting at the specified (srcX,srcY) location in the coordinate system of the base-layer.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000269 *
reedb184f7f2014-07-13 04:32:32 -0700270 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000271 *
reedb184f7f2014-07-13 04:32:32 -0700272 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000273 *
reedb184f7f2014-07-13 04:32:32 -0700274 * srcR is intersected with the bounds of the base-layer. If this intersection is not empty,
275 * then we have two sets of pixels (of equal size). Replace the dst pixels with the
276 * corresponding src pixels, performing any colortype/alphatype transformations needed
277 * (in the case where the src and dst have different colortypes or alphatypes).
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000278 *
279 * This call can fail, returning false, for several reasons:
reedb184f7f2014-07-13 04:32:32 -0700280 * - If srcR does not intersect the base-layer bounds.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000281 * - If the requested colortype/alphatype cannot be converted from the base-layer's types.
282 * - If this canvas is not backed by pixels (e.g. picture or PDF)
283 */
reedb184f7f2014-07-13 04:32:32 -0700284 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
285 int srcX, int srcY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000286
287 /**
288 * Helper for calling readPixels(info, ...). This call will check if bitmap has been allocated.
289 * If not, it will attempt to call allocPixels(). If this fails, it will return false. If not,
290 * it calls through to readPixels(info, ...) and returns its result.
291 */
reedb184f7f2014-07-13 04:32:32 -0700292 bool readPixels(SkBitmap* bitmap, int srcX, int srcY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000293
294 /**
295 * Helper for allocating pixels and then calling readPixels(info, ...). The bitmap is resized
296 * to the intersection of srcRect and the base-layer bounds. On success, pixels will be
297 * allocated in bitmap and true returned. On failure, false is returned and bitmap will be
298 * set to empty.
reed@google.com51df9e32010-12-23 19:29:18 +0000299 */
300 bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
reed@google.com51df9e32010-12-23 19:29:18 +0000301
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000302 /**
303 * This method affects the pixels in the base-layer, and operates in pixel coordinates,
304 * ignoring the matrix and clip.
305 *
306 * The specified ImageInfo and (x,y) offset specifies a rectangle: target.
307 *
308 * target.setXYWH(x, y, info.width(), info.height());
309 *
310 * Target is intersected with the bounds of the base-layer. If this intersection is not empty,
311 * then we have two sets of pixels (of equal size), the "src" specified by info+pixels+rowBytes
312 * and the "dst" by the canvas' backend. Replace the dst pixels with the corresponding src
313 * pixels, performing any colortype/alphatype transformations needed (in the case where the
314 * src and dst have different colortypes or alphatypes).
315 *
316 * This call can fail, returning false, for several reasons:
317 * - If the src colortype/alphatype cannot be converted to the canvas' types
318 * - If this canvas is not backed by pixels (e.g. picture or PDF)
319 */
320 bool writePixels(const SkImageInfo&, const void* pixels, size_t rowBytes, int x, int y);
321
322 /**
323 * Helper for calling writePixels(info, ...) by passing its pixels and rowbytes. If the bitmap
324 * is just wrapping a texture, returns false and does nothing.
325 */
326 bool writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com4b226022011-01-11 18:32:13 +0000327
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 ///////////////////////////////////////////////////////////////////////////
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000329
reed@android.comdc3381f2010-02-11 16:05:15 +0000330 /** This call saves the current matrix, clip, and drawFilter, and pushes a
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 copy onto a private stack. Subsequent calls to translate, scale,
reed@android.comdc3381f2010-02-11 16:05:15 +0000332 rotate, skew, concat or clipRect, clipPath, and setDrawFilter all
333 operate on this copy.
334 When the balancing call to restore() is made, the previous matrix, clip,
335 and drawFilter are restored.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000336
337 @return The value to pass to restoreToCount() to balance this save()
338 */
339 int save();
340
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 /** This behaves the same as save(), but in addition it allocates an
342 offscreen bitmap. All drawing calls are directed there, and only when
343 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000344 the canvas (or the previous layer).
reed@android.comad164b22010-07-02 17:20:51 +0000345 @param bounds (may be null) This rect, if non-null, is used as a hint to
346 limit the size of the offscreen, and thus drawing may be
347 clipped to it, though that clipping is not guaranteed to
348 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 @param paint (may be null) This is copied, and is applied to the
350 offscreen when restore() is called
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000351 @return The value to pass to restoreToCount() to balance this save()
352 */
353 int saveLayer(const SkRect* bounds, const SkPaint* paint);
reed021f6312015-08-09 19:41:13 -0700354 int saveLayer(const SkRect& bounds, const SkPaint* paint) {
355 return this->saveLayer(&bounds, paint);
356 }
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000357
reed70ee31b2015-12-10 13:44:45 -0800358 /**
359 * Temporary name.
360 * Will allow any requests for LCD text to be respected, so the caller must be careful to
361 * only draw on top of opaque sections of the layer to get good results.
362 */
363 int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint);
364
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 /** This behaves the same as save(), but in addition it allocates an
366 offscreen bitmap. All drawing calls are directed there, and only when
367 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000368 the canvas (or the previous layer).
reed@android.com40408612010-07-02 17:24:23 +0000369 @param bounds (may be null) This rect, if non-null, is used as a hint to
370 limit the size of the offscreen, and thus drawing may be
371 clipped to it, though that clipping is not guaranteed to
372 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 @param alpha This is applied to the offscreen when restore() is called.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000374 @return The value to pass to restoreToCount() to balance this save()
375 */
376 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
377
reed4960eee2015-12-18 07:09:18 -0800378 enum {
379 kIsOpaque_SaveLayerFlag = 1 << 0,
380 kPreserveLCDText_SaveLayerFlag = 1 << 1,
reedbada1882015-12-21 13:09:44 -0800381
382#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
383 kDontClipToLayer_Legacy_SaveLayerFlag = kDontClipToLayer_PrivateSaveLayerFlag,
384#endif
reed4960eee2015-12-18 07:09:18 -0800385 };
386 typedef uint32_t SaveLayerFlags;
387
388 struct SaveLayerRec {
reedbfd5f172016-01-07 11:28:08 -0800389 SaveLayerRec()
390 : fBounds(nullptr), fPaint(nullptr), fBackdrop(nullptr), fSaveLayerFlags(0)
391 {}
reed4960eee2015-12-18 07:09:18 -0800392 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
393 : fBounds(bounds)
394 , fPaint(paint)
reedbfd5f172016-01-07 11:28:08 -0800395 , fBackdrop(nullptr)
396 , fSaveLayerFlags(saveLayerFlags)
397 {}
398 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
399 SaveLayerFlags saveLayerFlags)
400 : fBounds(bounds)
401 , fPaint(paint)
402 , fBackdrop(backdrop)
reed4960eee2015-12-18 07:09:18 -0800403 , fSaveLayerFlags(saveLayerFlags)
404 {}
405
reedbfd5f172016-01-07 11:28:08 -0800406 const SkRect* fBounds; // optional
407 const SkPaint* fPaint; // optional
408 const SkImageFilter* fBackdrop; // optional
409 SaveLayerFlags fSaveLayerFlags;
reed4960eee2015-12-18 07:09:18 -0800410 };
411
412 int saveLayer(const SaveLayerRec&);
413
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 /** This call balances a previous call to save(), and is used to remove all
reed@android.comdc3381f2010-02-11 16:05:15 +0000415 modifications to the matrix/clip/drawFilter state since the last save
416 call.
417 It is an error to call restore() more times than save() was called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 */
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000419 void restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420
421 /** Returns the number of matrix/clip states on the SkCanvas' private stack.
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000422 This will equal # save() calls - # restore() calls + 1. The save count on
423 a new canvas is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 */
junov@chromium.orga907ac32012-02-24 21:54:07 +0000425 int getSaveCount() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426
427 /** Efficient way to pop any calls to save() that happened after the save
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000428 count reached saveCount. It is an error for saveCount to be greater than
429 getSaveCount(). To pop all the way back to the initial matrix/clip context
430 pass saveCount == 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431 @param saveCount The number of save() levels to restore from
432 */
433 void restoreToCount(int saveCount);
434
435 /** Preconcat the current matrix with the specified translation
436 @param dx The distance to translate in X
437 @param dy The distance to translate in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000439 void translate(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440
441 /** Preconcat the current matrix with the specified scale.
442 @param sx The amount to scale in X
443 @param sy The amount to scale in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000445 void scale(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446
bungeman7438bfc2016-07-12 15:01:19 -0700447 /** Preconcat the current matrix with the specified rotation about the origin.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 @param degrees The amount to rotate, in degrees
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000450 void rotate(SkScalar degrees);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451
bungeman7438bfc2016-07-12 15:01:19 -0700452 /** Preconcat the current matrix with the specified rotation about a given point.
453 @param degrees The amount to rotate, in degrees
454 @param px The x coordinate of the point to rotate about.
455 @param py The y coordinate of the point to rotate about.
456 */
457 void rotate(SkScalar degrees, SkScalar px, SkScalar py);
458
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459 /** Preconcat the current matrix with the specified skew.
460 @param sx The amount to skew in X
461 @param sy The amount to skew in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000463 void skew(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464
465 /** Preconcat the current matrix with the specified matrix.
466 @param matrix The matrix to preconcatenate with the current matrix
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000468 void concat(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000469
reed@android.com8a1c16f2008-12-17 15:59:43 +0000470 /** Replace the current matrix with a copy of the specified matrix.
471 @param matrix The matrix that will be copied into the current matrix.
472 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000473 void setMatrix(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000474
reed@android.com8a1c16f2008-12-17 15:59:43 +0000475 /** Helper for setMatrix(identity). Sets the current matrix to identity.
476 */
477 void resetMatrix();
478
vjiaoblack95302da2016-07-21 10:25:54 -0700479#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700480 /** Add the specified translation to the current draw depth of the canvas.
481 @param z The distance to translate in Z.
482 Negative into screen, positive out of screen.
483 Without translation, the draw depth defaults to 0.
484 */
485 void translateZ(SkScalar z);
486
vjiaoblack95302da2016-07-21 10:25:54 -0700487 /** Set the current set of lights in the canvas.
488 @param lights The lights that we want the canvas to have.
489 */
490 void setLights(sk_sp<SkLights> lights);
491
492 /** Returns the current set of lights the canvas uses
493 */
494 sk_sp<SkLights> getLights() const;
495#endif
496
reed@google.com4ed0fb72012-12-12 20:48:18 +0000497 /**
498 * Modify the current clip with the specified rectangle.
499 * @param rect The rect to combine with the current clip
500 * @param op The region op to apply to the current clip
501 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000502 */
reed66998382016-09-21 11:15:07 -0700503 void clipRect(const SkRect& rect, ClipOp, bool doAntiAlias);
504 void clipRect(const SkRect& rect, ClipOp op) {
505 this->clipRect(rect, op, false);
506 }
507 void clipRect(const SkRect& rect, bool doAntiAlias = false) {
508 this->clipRect(rect, kIntersect_Op, doAntiAlias);
509 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510
reed@google.com4ed0fb72012-12-12 20:48:18 +0000511 /**
512 * Modify the current clip with the specified SkRRect.
513 * @param rrect The rrect to combine with the current clip
514 * @param op The region op to apply to the current clip
515 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000516 */
reed66998382016-09-21 11:15:07 -0700517 void clipRRect(const SkRRect& rrect, ClipOp op, bool doAntiAlias);
518 void clipRRect(const SkRRect& rrect, ClipOp op) {
519 this->clipRRect(rrect, op, false);
520 }
521 void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) {
522 this->clipRRect(rrect, kIntersect_Op, doAntiAlias);
523 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000524
525 /**
526 * Modify the current clip with the specified path.
527 * @param path The path to combine with the current clip
528 * @param op The region op to apply to the current clip
529 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000530 */
reed66998382016-09-21 11:15:07 -0700531 void clipPath(const SkPath& path, ClipOp op, bool doAntiAlias);
532 void clipPath(const SkPath& path, ClipOp op) {
533 this->clipPath(path, op, false);
534 }
535 void clipPath(const SkPath& path, bool doAntiAlias = false) {
536 this->clipPath(path, kIntersect_Op, doAntiAlias);
537 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538
caryclark@google.com8f0a7b82012-11-07 14:54:49 +0000539 /** EXPERIMENTAL -- only used for testing
caryclark@google.com45a75fb2013-04-25 13:34:40 +0000540 Set to simplify clip stack using path ops.
541 */
542 void setAllowSimplifyClip(bool allow) {
543 fAllowSimplifyClip = allow;
544 }
545
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546 /** Modify the current clip with the specified region. Note that unlike
547 clipRect() and clipPath() which transform their arguments by the current
548 matrix, clipRegion() assumes its argument is already in device
549 coordinates, and so no transformation is performed.
550 @param deviceRgn The region to apply to the current clip
mtklein6e998e62016-07-13 09:03:48 -0700551 @param op The region op to apply to the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 */
reed73603f32016-09-20 08:42:38 -0700553 void clipRegion(const SkRegion& deviceRgn, ClipOp op = kIntersect_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000554
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 /** Return true if the specified rectangle, after being transformed by the
556 current matrix, would lie completely outside of the current clip. Call
557 this to check if an area you intend to draw into is clipped out (and
558 therefore you can skip making the draw calls).
559 @param rect the rect to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560 @return true if the rect (transformed by the canvas' matrix) does not
561 intersect with the canvas' clip
562 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000563 bool quickReject(const SkRect& rect) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564
565 /** Return true if the specified path, after being transformed by the
566 current matrix, would lie completely outside of the current clip. Call
567 this to check if an area you intend to draw into is clipped out (and
568 therefore you can skip making the draw calls). Note, for speed it may
569 return false even if the path itself might not intersect the clip
570 (i.e. the bounds of the path intersects, but the path does not).
571 @param path The path to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000572 @return true if the path (transformed by the canvas' matrix) does not
573 intersect with the canvas' clip
574 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000575 bool quickReject(const SkPath& path) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577 /** Return the bounds of the current clip (in local coordinates) in the
578 bounds parameter, and return true if it is non-empty. This can be useful
579 in a way similar to quickReject, in that it tells you that drawing
580 outside of these bounds will be clipped out.
581 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000582 virtual bool getClipBounds(SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000584 /** Return the bounds of the current clip, in device coordinates; returns
585 true if non-empty. Maybe faster than getting the clip explicitly and
586 then taking its bounds.
587 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000588 virtual bool getClipDeviceBounds(SkIRect* bounds) const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000589
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000590
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000592 specified ARGB color, using the specified mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593 @param a the alpha component (0..255) of the color to fill the canvas
594 @param r the red component (0..255) of the color to fill the canvas
595 @param g the green component (0..255) of the color to fill the canvas
596 @param b the blue component (0..255) of the color to fill the canvas
597 @param mode the mode to apply the color in (defaults to SrcOver)
598 */
reed374772b2016-10-05 17:33:02 -0700599 void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, SkBlendMode mode = SkBlendMode::kSrcOver);
600#ifdef SK_SUPPORT_LEGACY_XFERMODE_OBJECT
601 void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, SkXfermode::Mode mode) {
602 this->drawARGB(a, r, g, b, (SkBlendMode)mode);
603 }
604#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000605
606 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000607 specified color and mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608 @param color the color to draw with
609 @param mode the mode to apply the color in (defaults to SrcOver)
610 */
reed374772b2016-10-05 17:33:02 -0700611 void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver);
612#ifdef SK_SUPPORT_LEGACY_XFERMODE_OBJECT
613 void drawColor(SkColor color, SkXfermode::Mode mode) {
614 this->drawColor(color, (SkBlendMode)mode);
615 }
616#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000617
reedf4e0d9e2014-12-05 20:49:03 -0800618 /**
619 * Helper method for drawing a color in SRC mode, completely replacing all the pixels
620 * in the current clip with this color.
621 */
622 void clear(SkColor color) {
reed374772b2016-10-05 17:33:02 -0700623 this->drawColor(color, SkBlendMode::kSrc);
reed8eddfb52014-12-04 07:50:14 -0800624 }
reed@google.com2a981812011-04-14 18:59:28 +0000625
626 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000627 * This makes the contents of the canvas undefined. Subsequent calls that
628 * require reading the canvas contents will produce undefined results. Examples
629 * include blending and readPixels. The actual implementation is backend-
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800630 * dependent and one legal implementation is to do nothing. This method
631 * ignores the current clip.
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000632 *
633 * This function should only be called if the caller intends to subsequently
634 * draw to the canvas. The canvas may do real work at discard() time in order
635 * to optimize performance on subsequent draws. Thus, if you call this and then
636 * never draw to the canvas subsequently you may pay a perfomance penalty.
637 */
638 void discard() { this->onDiscard(); }
639
640 /**
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800641 * Fill the entire canvas (restricted to the current clip) with the
reed@google.com2a981812011-04-14 18:59:28 +0000642 * specified paint.
643 * @param paint The paint used to fill the canvas
644 */
reed0846f1b2015-01-09 14:17:40 -0800645 void drawPaint(const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646
647 enum PointMode {
648 /** drawPoints draws each point separately */
649 kPoints_PointMode,
650 /** drawPoints draws each pair of points as a line segment */
651 kLines_PointMode,
652 /** drawPoints draws the array of points as a polygon */
653 kPolygon_PointMode
654 };
655
656 /** Draw a series of points, interpreted based on the PointMode mode. For
657 all modes, the count parameter is interpreted as the total number of
658 points. For kLine mode, count/2 line segments are drawn.
659 For kPoint mode, each point is drawn centered at its coordinate, and its
660 size is specified by the paint's stroke-width. It draws as a square,
661 unless the paint's cap-type is round, in which the points are drawn as
662 circles.
663 For kLine mode, each pair of points is drawn as a line segment,
664 respecting the paint's settings for cap/join/width.
665 For kPolygon mode, the entire array is drawn as a series of connected
666 line segments.
667 Note that, while similar, kLine and kPolygon modes draw slightly
668 differently than the equivalent path built with a series of moveto,
669 lineto calls, in that the path will draw all of its contours at once,
670 with no interactions if contours intersect each other (think XOR
671 xfermode). drawPoints always draws each element one at a time.
672 @param mode PointMode specifying how to draw the array of points.
673 @param count The number of points in the array
674 @param pts Array of points to draw
675 @param paint The paint used to draw the points
676 */
reed0846f1b2015-01-09 14:17:40 -0800677 void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678
679 /** Helper method for drawing a single point. See drawPoints() for a more
680 details.
681 */
682 void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000683
reed@android.com8a1c16f2008-12-17 15:59:43 +0000684 /** Draws a single pixel in the specified color.
685 @param x The X coordinate of which pixel to draw
686 @param y The Y coordiante of which pixel to draw
687 @param color The color to draw
688 */
689 void drawPoint(SkScalar x, SkScalar y, SkColor color);
690
691 /** Draw a line segment with the specified start and stop x,y coordinates,
692 using the specified paint. NOTE: since a line is always "framed", the
693 paint's Style is ignored.
694 @param x0 The x-coordinate of the start point of the line
695 @param y0 The y-coordinate of the start point of the line
696 @param x1 The x-coordinate of the end point of the line
697 @param y1 The y-coordinate of the end point of the line
698 @param paint The paint used to draw the line
699 */
700 void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
701 const SkPaint& paint);
702
703 /** Draw the specified rectangle using the specified paint. The rectangle
704 will be filled or stroked based on the Style in the paint.
705 @param rect The rect to be drawn
706 @param paint The paint used to draw the rect
707 */
reed0846f1b2015-01-09 14:17:40 -0800708 void drawRect(const SkRect& rect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000709
710 /** Draw the specified rectangle using the specified paint. The rectangle
711 will be filled or framed based on the Style in the paint.
712 @param rect The rect to be drawn
713 @param paint The paint used to draw the rect
714 */
reed@google.com87001ed2014-02-17 16:28:05 +0000715 void drawIRect(const SkIRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 SkRect r;
717 r.set(rect); // promotes the ints to scalars
718 this->drawRect(r, paint);
719 }
reed@google.com4b226022011-01-11 18:32:13 +0000720
reed@android.com8a1c16f2008-12-17 15:59:43 +0000721 /** Draw the specified rectangle using the specified paint. The rectangle
722 will be filled or framed based on the Style in the paint.
723 @param left The left side of the rectangle to be drawn
724 @param top The top side of the rectangle to be drawn
725 @param right The right side of the rectangle to be drawn
726 @param bottom The bottom side of the rectangle to be drawn
727 @param paint The paint used to draw the rect
728 */
729 void drawRectCoords(SkScalar left, SkScalar top, SkScalar right,
730 SkScalar bottom, const SkPaint& paint);
731
msarett44df6512016-08-25 13:54:30 -0700732 /** Draw the outline of the specified region using the specified paint.
733 @param region The region to be drawn
734 @param paint The paint used to draw the region
735 */
msarettdca352e2016-08-26 06:37:45 -0700736 void drawRegion(const SkRegion& region, const SkPaint& paint);
msarett44df6512016-08-25 13:54:30 -0700737
reed@android.com8a1c16f2008-12-17 15:59:43 +0000738 /** Draw the specified oval using the specified paint. The oval will be
739 filled or framed based on the Style in the paint.
740 @param oval The rectangle bounds of the oval to be drawn
741 @param paint The paint used to draw the oval
742 */
reed0846f1b2015-01-09 14:17:40 -0800743 void drawOval(const SkRect& oval, const SkPaint&);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000744
745 /**
746 * Draw the specified RRect using the specified paint The rrect will be filled or stroked
747 * based on the Style in the paint.
748 *
749 * @param rrect The round-rect to draw
750 * @param paint The paint used to draw the round-rect
751 */
reed0846f1b2015-01-09 14:17:40 -0800752 void drawRRect(const SkRRect& rrect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000753
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000754 /**
755 * Draw the annulus formed by the outer and inner rrects. The results
756 * are undefined if the outer does not contain the inner.
757 */
758 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
759
reed@android.com8a1c16f2008-12-17 15:59:43 +0000760 /** Draw the specified circle using the specified paint. If radius is <= 0,
761 then nothing will be drawn. The circle will be filled
762 or framed based on the Style in the paint.
763 @param cx The x-coordinate of the center of the cirle to be drawn
764 @param cy The y-coordinate of the center of the cirle to be drawn
765 @param radius The radius of the cirle to be drawn
766 @param paint The paint used to draw the circle
767 */
768 void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
769 const SkPaint& paint);
770
771 /** Draw the specified arc, which will be scaled to fit inside the
bsalomon21af9ca2016-08-25 12:29:23 -0700772 specified oval. Sweep angles are not treated as modulo 360 and thus can
773 exceed a full sweep of the oval. Note that this differs slightly from
774 SkPath::arcTo, which treats the sweep angle mod 360. If the oval is empty
775 or the sweep angle is zero nothing is drawn. If useCenter is true the oval
776 center is inserted into the implied path before the arc and the path is
777 closed back to the, center forming a wedge. Otherwise, the implied path
778 contains just the arc and is not closed.
bsalomonac3aa242016-08-19 11:25:19 -0700779 @param oval The bounds of oval used to define the shape of the arc.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000780 @param startAngle Starting angle (in degrees) where the arc begins
bsalomonac3aa242016-08-19 11:25:19 -0700781 @param sweepAngle Sweep angle (in degrees) measured clockwise.
bsalomon21af9ca2016-08-25 12:29:23 -0700782 @param useCenter true means include the center of the oval.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000783 @param paint The paint used to draw the arc
784 */
785 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
786 bool useCenter, const SkPaint& paint);
787
788 /** Draw the specified round-rect using the specified paint. The round-rect
789 will be filled or framed based on the Style in the paint.
790 @param rect The rectangular bounds of the roundRect to be drawn
791 @param rx The x-radius of the oval used to round the corners
792 @param ry The y-radius of the oval used to round the corners
793 @param paint The paint used to draw the roundRect
794 */
795 void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry,
796 const SkPaint& paint);
797
798 /** Draw the specified path using the specified paint. The path will be
799 filled or framed based on the Style in the paint.
800 @param path The path to be drawn
801 @param paint The paint used to draw the path
802 */
reed0846f1b2015-01-09 14:17:40 -0800803 void drawPath(const SkPath& path, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000804
piotaixrd52893c2014-09-25 14:39:40 -0700805 /** Draw the specified image, with its top/left corner at (x,y), using the
806 specified paint, transformed by the current matrix.
807
808 @param image The image to be drawn
809 @param left The position of the left side of the image being drawn
810 @param top The position of the top side of the image being drawn
811 @param paint The paint used to draw the image, or NULL
812 */
reed0846f1b2015-01-09 14:17:40 -0800813 void drawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint = NULL);
reedf8053da2016-03-17 08:14:57 -0700814 void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top,
815 const SkPaint* paint = NULL) {
816 this->drawImage(image.get(), left, top, paint);
817 }
piotaixrb5fae932014-09-24 13:03:30 -0700818
reeda5517e22015-07-14 10:54:12 -0700819 /**
820 * Controls the behavior at the edge of the src-rect, when specified in drawImageRect,
821 * trading off speed for exactness.
822 *
823 * When filtering is enabled (in the Paint), skia may need to sample in a neighborhood around
824 * the pixels in the image. If there is a src-rect specified, it is intended to restrict the
825 * pixels that will be read. However, for performance reasons, some implementations may slow
826 * down if they cannot read 1-pixel past the src-rect boundary at times.
827 *
828 * This enum allows the caller to specify if such a 1-pixel "slop" will be visually acceptable.
829 * If it is, the caller should pass kFast, and it may result in a faster draw. If the src-rect
830 * must be strictly respected, the caller should pass kStrict.
831 */
832 enum SrcRectConstraint {
833 /**
834 * If kStrict is specified, the implementation must respect the src-rect
835 * (if specified) strictly, and will never sample outside of those bounds during sampling
836 * even when filtering. This may be slower than kFast.
837 */
838 kStrict_SrcRectConstraint,
839
840 /**
841 * If kFast is specified, the implementation may sample outside of the src-rect
bsalomon19e82e32015-10-23 09:27:42 -0700842 * (if specified) by half the width of filter. This allows greater flexibility
reeda5517e22015-07-14 10:54:12 -0700843 * to the implementation and can make the draw much faster.
844 */
845 kFast_SrcRectConstraint,
846 };
847
848 /** Draw the specified image, scaling and translating so that it fills the specified
849 * dst rect. If the src rect is non-null, only that subset of the image is transformed
850 * and drawn.
851 *
852 * @param image The image to be drawn
853 * @param src Optional: specify the subset of the image to be drawn
854 * @param dst The destination rectangle where the scaled/translated
855 * image will be drawn
856 * @param paint The paint used to draw the image, or NULL
857 * @param constraint Control the tradeoff between speed and exactness w.r.t. the src-rect.
858 */
reede47829b2015-08-06 10:02:53 -0700859 void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst,
860 const SkPaint* paint,
861 SrcRectConstraint constraint = kStrict_SrcRectConstraint);
862 // variant that takes src SkIRect
863 void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst,
864 const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
865 // variant that assumes src == image-bounds
reede47829b2015-08-06 10:02:53 -0700866 void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint,
867 SrcRectConstraint = kStrict_SrcRectConstraint);
reed84984ef2015-07-17 07:09:43 -0700868
reedf8053da2016-03-17 08:14:57 -0700869 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
870 const SkPaint* paint,
871 SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
872 this->drawImageRect(image.get(), src, dst, paint, constraint);
873 }
874 void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst,
875 const SkPaint* paint, SrcRectConstraint cons = kStrict_SrcRectConstraint) {
876 this->drawImageRect(image.get(), isrc, dst, paint, cons);
877 }
878 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint,
879 SrcRectConstraint cons = kStrict_SrcRectConstraint) {
880 this->drawImageRect(image.get(), dst, paint, cons);
881 }
882
reed4c21dc52015-06-25 12:32:03 -0700883 /**
884 * Draw the image stretched differentially to fit into dst.
885 * center is a rect within the image, and logically divides the image
886 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
887 * image is the "center", then the center-rect should be [2, 2, 3, 3].
888 *
889 * If the dst is >= the image size, then...
890 * - The 4 corners are not stretched at all.
891 * - The sides are stretched in only one axis.
892 * - The center is stretched in both axes.
893 * Else, for each axis where dst < image,
894 * - The corners shrink proportionally
895 * - The sides (along the shrink axis) and center are not drawn
896 */
897 void drawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
reedf8053da2016-03-17 08:14:57 -0700898 const SkPaint* paint = nullptr);
899 void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst,
900 const SkPaint* paint = nullptr) {
901 this->drawImageNine(image.get(), center, dst, paint);
902 }
reed4c21dc52015-06-25 12:32:03 -0700903
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904 /** Draw the specified bitmap, with its top/left corner at (x,y), using the
905 specified paint, transformed by the current matrix. Note: if the paint
906 contains a maskfilter that generates a mask which extends beyond the
907 bitmap's original width/height, then the bitmap will be drawn as if it
908 were in a Shader with CLAMP mode. Thus the color outside of the original
909 width/height will be the edge color replicated.
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000910
911 If a shader is present on the paint it will be ignored, except in the
reed@google.comf20fc242014-03-26 13:44:58 +0000912 case where the bitmap is kAlpha_8_SkColorType. In that case, the color is
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000913 generated by the shader.
914
reed@android.com8a1c16f2008-12-17 15:59:43 +0000915 @param bitmap The bitmap to be drawn
916 @param left The position of the left side of the bitmap being drawn
917 @param top The position of the top side of the bitmap being drawn
918 @param paint The paint used to draw the bitmap, or NULL
919 */
reed0846f1b2015-01-09 14:17:40 -0800920 void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
921 const SkPaint* paint = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922
reeda5517e22015-07-14 10:54:12 -0700923 /** Draw the specified bitmap, scaling and translating so that it fills the specified
924 * dst rect. If the src rect is non-null, only that subset of the bitmap is transformed
925 * and drawn.
926 *
927 * @param bitmap The bitmap to be drawn
928 * @param src Optional: specify the subset of the bitmap to be drawn
929 * @param dst The destination rectangle where the scaled/translated
930 * bitmap will be drawn
931 * @param paint The paint used to draw the bitmap, or NULL
932 * @param constraint Control the tradeoff between speed and exactness w.r.t. the src-rect.
933 */
reede47829b2015-08-06 10:02:53 -0700934 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst,
reed84984ef2015-07-17 07:09:43 -0700935 const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700936 // variant where src is SkIRect
reed84984ef2015-07-17 07:09:43 -0700937 void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst,
938 const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700939 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint,
bsalomon19e82e32015-10-23 09:27:42 -0700940 SrcRectConstraint = kStrict_SrcRectConstraint);
reed84984ef2015-07-17 07:09:43 -0700941
reed@google.comf0b5e112011-09-07 11:57:34 +0000942 /**
msarettc573a402016-08-02 08:05:56 -0700943 * Draw the bitmap stretched or shrunk differentially to fit into dst.
reed@google.comf0b5e112011-09-07 11:57:34 +0000944 * center is a rect within the bitmap, and logically divides the bitmap
945 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
946 * bitmap is the "center", then the center-rect should be [2, 2, 3, 3].
947 *
948 * If the dst is >= the bitmap size, then...
robertphillips@google.com9bf380c2013-07-25 12:10:42 +0000949 * - The 4 corners are not stretched at all.
950 * - The sides are stretched in only one axis.
951 * - The center is stretched in both axes.
reed@google.comf0b5e112011-09-07 11:57:34 +0000952 * Else, for each axis where dst < bitmap,
953 * - The corners shrink proportionally
954 * - The sides (along the shrink axis) and center are not drawn
955 */
reed0846f1b2015-01-09 14:17:40 -0800956 void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
957 const SkPaint* paint = NULL);
reed@google.comf0b5e112011-09-07 11:57:34 +0000958
msarettc573a402016-08-02 08:05:56 -0700959 /**
960 * Specifies coordinates to divide a bitmap into (xCount*yCount) rects.
msarett71df2d72016-09-30 12:41:42 -0700961 *
962 * If the lattice divs or bounds are invalid, the entire lattice
963 * struct will be ignored on the draw call.
msarettc573a402016-08-02 08:05:56 -0700964 */
965 struct Lattice {
msarett0764efe2016-09-02 11:24:30 -0700966 enum Flags : uint8_t {
967 // If set, indicates that we should not draw corresponding rect.
968 kTransparent_Flags = 1 << 0,
969 };
msarettc573a402016-08-02 08:05:56 -0700970
msarett0764efe2016-09-02 11:24:30 -0700971 // An array of x-coordinates that divide the bitmap vertically.
msarett71df2d72016-09-30 12:41:42 -0700972 // These must be unique, increasing, and in the set [fBounds.fLeft, fBounds.fRight).
msarett0764efe2016-09-02 11:24:30 -0700973 // Does not have ownership.
msarett71df2d72016-09-30 12:41:42 -0700974 const int* fXDivs;
msarettc573a402016-08-02 08:05:56 -0700975
976 // An array of y-coordinates that divide the bitmap horizontally.
msarett71df2d72016-09-30 12:41:42 -0700977 // These must be unique, increasing, and in the set [fBounds.fTop, fBounds.fBottom).
msarettc573a402016-08-02 08:05:56 -0700978 // Does not have ownership.
msarett71df2d72016-09-30 12:41:42 -0700979 const int* fYDivs;
msarett0764efe2016-09-02 11:24:30 -0700980
981 // If non-null, the length of this array must be equal to
982 // (fXCount + 1) * (fYCount + 1). Note that we allow the first rect
msarett71df2d72016-09-30 12:41:42 -0700983 // in each direction to be empty (ex: fXDivs[0] = fBounds.fLeft).
984 // In this case, the caller still must specify a flag (as a placeholder)
985 // for these empty rects.
msarett0764efe2016-09-02 11:24:30 -0700986 // The flags correspond to the rects in the lattice, first moving
987 // left to right and then top to bottom.
msarett71df2d72016-09-30 12:41:42 -0700988 const Flags* fFlags;
msarett0764efe2016-09-02 11:24:30 -0700989
990 // The number of fXDivs.
msarett71df2d72016-09-30 12:41:42 -0700991 int fXCount;
msarettc573a402016-08-02 08:05:56 -0700992
993 // The number of fYDivs.
msarett71df2d72016-09-30 12:41:42 -0700994 int fYCount;
995
996 // The bound to draw from. Must be contained by the src that is being drawn,
997 // non-empty, and non-inverted.
998 // If nullptr, the bounds are the entire src.
999 const SkIRect* fBounds;
msarettc573a402016-08-02 08:05:56 -07001000 };
1001
1002 /**
1003 * Draw the bitmap stretched or shrunk differentially to fit into dst.
1004 *
1005 * Moving horizontally across the bitmap, alternating rects will be "scalable"
1006 * (in the x-dimension) to fit into dst or must be left "fixed". The first rect
1007 * is treated as "fixed", but it's possible to specify an empty first rect by
1008 * making lattice.fXDivs[0] = 0.
1009 *
1010 * The scale factor for all "scalable" rects will be the same, and may be greater
1011 * than or less than 1 (meaning we can stretch or shrink). If the number of
1012 * "fixed" pixels is greater than the width of the dst, we will collapse all of
1013 * the "scalable" regions and appropriately downscale the "fixed" regions.
1014 *
1015 * The same interpretation also applies to the y-dimension.
1016 */
1017 void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst,
1018 const SkPaint* paint = nullptr);
1019 void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
1020 const SkPaint* paint = nullptr);
1021
reed@android.com8a1c16f2008-12-17 15:59:43 +00001022 /** Draw the text, with origin at (x,y), using the specified paint.
1023 The origin is interpreted based on the Align setting in the paint.
1024 @param text The text to be drawn
1025 @param byteLength The number of bytes to read from the text parameter
1026 @param x The x-coordinate of the origin of the text being drawn
1027 @param y The y-coordinate of the origin of the text being drawn
1028 @param paint The paint used for the text (e.g. color, size, style)
1029 */
reedf7430cc2014-12-21 11:38:35 -08001030 void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
1031 const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001032
1033 /** Draw the text, with each character/glyph origin specified by the pos[]
reed@google.com4b226022011-01-11 18:32:13 +00001034 array. The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001035 @param text The text to be drawn
1036 @param byteLength The number of bytes to read from the text parameter
1037 @param pos Array of positions, used to position each character
1038 @param paint The paint used for the text (e.g. color, size, style)
1039 */
reedf7430cc2014-12-21 11:38:35 -08001040 void drawPosText(const void* text, size_t byteLength, const SkPoint pos[],
1041 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +00001042
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 /** Draw the text, with each character/glyph origin specified by the x
1044 coordinate taken from the xpos[] array, and the y from the constY param.
reed@google.com4b226022011-01-11 18:32:13 +00001045 The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001046 @param text The text to be drawn
1047 @param byteLength The number of bytes to read from the text parameter
1048 @param xpos Array of x-positions, used to position each character
1049 @param constY The shared Y coordinate for all of the positions
1050 @param paint The paint used for the text (e.g. color, size, style)
1051 */
reedf7430cc2014-12-21 11:38:35 -08001052 void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY,
1053 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +00001054
reed@android.com8a1c16f2008-12-17 15:59:43 +00001055 /** Draw the text, with origin at (x,y), using the specified paint, along
1056 the specified path. The paint's Align setting determins where along the
1057 path to start the text.
1058 @param text The text to be drawn
1059 @param byteLength The number of bytes to read from the text parameter
1060 @param path The path the text should follow for its baseline
1061 @param hOffset The distance along the path to add to the text's
1062 starting position
1063 @param vOffset The distance above(-) or below(+) the path to
1064 position the text
1065 @param paint The paint used for the text
1066 */
reedf7430cc2014-12-21 11:38:35 -08001067 void drawTextOnPathHV(const void* text, size_t byteLength, const SkPath& path, SkScalar hOffset,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001068 SkScalar vOffset, const SkPaint& paint);
1069
1070 /** Draw the text, with origin at (x,y), using the specified paint, along
1071 the specified path. The paint's Align setting determins where along the
1072 path to start the text.
1073 @param text The text to be drawn
1074 @param byteLength The number of bytes to read from the text parameter
1075 @param path The path the text should follow for its baseline
1076 @param matrix (may be null) Applied to the text before it is
1077 mapped onto the path
1078 @param paint The paint used for the text
1079 */
reedf7430cc2014-12-21 11:38:35 -08001080 void drawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
1081 const SkMatrix* matrix, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001082
reed45561a02016-07-07 12:47:17 -07001083 /**
1084 * Draw the text with each character/glyph individually transformed by its xform.
1085 * If cullRect is not null, it is a conservative bounds of what will be drawn
reed63f30d92016-07-07 16:32:51 -07001086 * taking into account the xforms and the paint, and will be used to accelerate culling.
reed45561a02016-07-07 12:47:17 -07001087 */
1088 void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
1089 const SkRect* cullRect, const SkPaint& paint);
1090
fmalita00d5c2c2014-08-21 08:53:26 -07001091 /** Draw the text blob, offset by (x,y), using the specified paint.
1092 @param blob The text blob to be drawn
1093 @param x The x-offset of the text being drawn
1094 @param y The y-offset of the text being drawn
1095 @param paint The paint used for the text (e.g. color, size, style)
1096 */
1097 void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
reed2ab90572016-08-10 14:16:41 -07001098 void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
1099 this->drawTextBlob(blob.get(), x, y, paint);
1100 }
fmalita00d5c2c2014-08-21 08:53:26 -07001101
reed@android.com8a1c16f2008-12-17 15:59:43 +00001102 /** Draw the picture into this canvas. This method effective brackets the
1103 playback of the picture's draw calls with save/restore, so the state
djsollen@google.coma44de962013-01-02 16:59:19 +00001104 of this canvas will be unchanged after this call.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 @param picture The recorded drawing commands to playback into this
1106 canvas.
1107 */
reed1c2c4412015-04-30 13:09:24 -07001108 void drawPicture(const SkPicture* picture) {
1109 this->drawPicture(picture, NULL, NULL);
1110 }
reedca2622b2016-03-18 07:25:55 -07001111 void drawPicture(const sk_sp<SkPicture>& picture) {
reedf8053da2016-03-17 08:14:57 -07001112 this->drawPicture(picture.get());
1113 }
robertphillips9b14f262014-06-04 05:40:44 -07001114
reedd5fa1a42014-08-09 11:08:05 -07001115 /**
1116 * Draw the picture into this canvas.
1117 *
1118 * If matrix is non-null, apply that matrix to the CTM when drawing this picture. This is
1119 * logically equivalent to
1120 * save/concat/drawPicture/restore
1121 *
1122 * If paint is non-null, draw the picture into a temporary buffer, and then apply the paint's
1123 * alpha/colorfilter/imagefilter/xfermode to that buffer as it is drawn to the canvas.
1124 * This is logically equivalent to
1125 * saveLayer(paint)/drawPicture/restore
1126 */
1127 void drawPicture(const SkPicture*, const SkMatrix* matrix, const SkPaint* paint);
reedca2622b2016-03-18 07:25:55 -07001128 void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, const SkPaint* paint) {
reedf8053da2016-03-17 08:14:57 -07001129 this->drawPicture(picture.get(), matrix, paint);
1130 }
reedd5fa1a42014-08-09 11:08:05 -07001131
vjiaoblack95302da2016-07-21 10:25:54 -07001132#ifdef SK_EXPERIMENTAL_SHADOWING
1133 /**
vjiaoblacke6f5d562016-08-25 06:30:23 -07001134 * Draw the picture into this canvas, with shadows!
vjiaoblack95302da2016-07-21 10:25:54 -07001135 *
1136 * We will use the canvas's lights along with the picture information (draw depths of
1137 * objects, etc) to first create a set of shadowmaps for the light-picture pairs, and
1138 * then use that set of shadowmaps to render the scene with shadows.
1139 *
1140 * If matrix is non-null, apply that matrix to the CTM when drawing this picture. This is
1141 * logically equivalent to
1142 * save/concat/drawPicture/restore
1143 *
1144 * If paint is non-null, draw the picture into a temporary buffer, and then apply the paint's
1145 * alpha/colorfilter/imagefilter/xfermode to that buffer as it is drawn to the canvas.
1146 * This is logically equivalent to
1147 * saveLayer(paint)/drawPicture/restore
1148 *
vjiaoblacke6f5d562016-08-25 06:30:23 -07001149 * We also support using variance shadow maps for blurred shadows; the user can specify
1150 * what shadow mapping algorithm to use with params.
1151 * - Variance Shadow Mapping works by storing both the depth and depth^2 in the shadow map.
1152 * - Then, the shadow map can be blurred, and when reading from it, the fragment shader
1153 * can calculate the variance of the depth at a position by doing E(x^2) - E(x)^2.
1154 * - We can then use the depth variance and depth at a fragment to arrive at an upper bound
1155 * of the probability that the current surface is shadowed by using Chebyshev's
1156 * inequality, and then use that to shade the fragment.
1157 *
1158 * - There are a few problems with VSM.
1159 * * Light Bleeding | Areas with high variance, such as near the edges of high up rects,
1160 * will cause their shadow penumbras to overwrite otherwise solid
1161 * shadows.
1162 * * Shape Distortion | We can combat Light Bleeding by biasing the shadow (setting
1163 * mostly shaded fragments to completely shaded) and increasing
1164 * the minimum allowed variance. However, this warps and rounds
1165 * out the shape of the shadow.
vjiaoblack95302da2016-07-21 10:25:54 -07001166 */
1167 void drawShadowedPicture(const SkPicture*,
1168 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -07001169 const SkPaint* paint,
1170 const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -07001171 void drawShadowedPicture(const sk_sp<SkPicture>& picture,
1172 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -07001173 const SkPaint* paint,
1174 const SkShadowParams& params) {
1175 this->drawShadowedPicture(picture.get(), matrix, paint, params);
vjiaoblack95302da2016-07-21 10:25:54 -07001176 }
1177#endif
1178
reed@android.com8a1c16f2008-12-17 15:59:43 +00001179 enum VertexMode {
1180 kTriangles_VertexMode,
1181 kTriangleStrip_VertexMode,
1182 kTriangleFan_VertexMode
1183 };
reed@google.com4b226022011-01-11 18:32:13 +00001184
reed@android.com8a1c16f2008-12-17 15:59:43 +00001185 /** Draw the array of vertices, interpreted as triangles (based on mode).
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001186
1187 If both textures and vertex-colors are NULL, it strokes hairlines with
1188 the paint's color. This behavior is a useful debugging mode to visualize
1189 the mesh.
1190
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 @param vmode How to interpret the array of vertices
1192 @param vertexCount The number of points in the vertices array (and
1193 corresponding texs and colors arrays if non-null)
1194 @param vertices Array of vertices for the mesh
1195 @param texs May be null. If not null, specifies the coordinate
robertphillips@google.com631a59b2013-07-31 14:57:53 +00001196 in _texture_ space (not uv space) for each vertex.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001197 @param colors May be null. If not null, specifies a color for each
1198 vertex, to be interpolated across the triangle.
1199 @param xmode Used if both texs and colors are present. In this
1200 case the colors are combined with the texture using mode,
1201 before being drawn using the paint. If mode is null, then
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001202 kModulate_Mode is used.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001203 @param indices If not null, array of indices to reference into the
1204 vertex (texs, colors) array.
1205 @param indexCount number of entries in the indices array (if not null)
reed@google.com4b226022011-01-11 18:32:13 +00001206 @param paint Specifies the shader/texture if present.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001207 */
reed0846f1b2015-01-09 14:17:40 -08001208 void drawVertices(VertexMode vmode, int vertexCount,
1209 const SkPoint vertices[], const SkPoint texs[],
1210 const SkColor colors[], SkXfermode* xmode,
1211 const uint16_t indices[], int indexCount,
1212 const SkPaint& paint);
reedcfb6bdf2016-03-29 11:32:50 -07001213 void drawVertices(VertexMode vmode, int vertexCount,
1214 const SkPoint vertices[], const SkPoint texs[],
mtklein92d48832016-04-01 04:55:47 -07001215 const SkColor colors[], const sk_sp<SkXfermode>& xmode,
reedcfb6bdf2016-03-29 11:32:50 -07001216 const uint16_t indices[], int indexCount,
1217 const SkPaint& paint) {
1218 this->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode.get(),
1219 indices, indexCount, paint);
1220 }
mtklein6cfa73a2014-08-13 13:33:49 -07001221
dandovb3c9d1c2014-08-12 08:34:29 -07001222 /**
1223 Draw a cubic coons patch
mtklein6cfa73a2014-08-13 13:33:49 -07001224
dandovb3c9d1c2014-08-12 08:34:29 -07001225 @param cubic specifies the 4 bounding cubic bezier curves of a patch with clockwise order
1226 starting at the top left corner.
1227 @param colors specifies the colors for the corners which will be bilerp across the patch,
1228 their order is clockwise starting at the top left corner.
mtklein6cfa73a2014-08-13 13:33:49 -07001229 @param texCoords specifies the texture coordinates that will be bilerp across the patch,
dandovb3c9d1c2014-08-12 08:34:29 -07001230 their order is the same as the colors.
mtklein6cfa73a2014-08-13 13:33:49 -07001231 @param xmode specifies how are the colors and the textures combined if both of them are
dandovb3c9d1c2014-08-12 08:34:29 -07001232 present.
dandov963137b2014-08-07 07:49:53 -07001233 @param paint Specifies the shader/texture if present.
1234 */
dandovb3c9d1c2014-08-12 08:34:29 -07001235 void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
1236 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
reedcfb6bdf2016-03-29 11:32:50 -07001237 void drawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4],
1238 const sk_sp<SkXfermode>& xmode, const SkPaint& paint) {
1239 this->drawPatch(cubics, colors, texCoords, xmode.get(), paint);
1240 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001241
reed3cb38402015-02-06 08:36:15 -08001242 /**
reed71c3c762015-06-24 10:29:17 -07001243 * Draw a set of sprites from the atlas. Each is specified by a tex rectangle in the
1244 * coordinate space of the atlas, and a corresponding xform which transforms the tex rectangle
1245 * into a quad.
1246 *
1247 * xform maps [0, 0, tex.width, tex.height] -> quad
1248 *
1249 * The color array is optional. When specified, each color modulates the pixels in its
1250 * corresponding quad (via the specified SkXfermode::Mode).
1251 *
1252 * The cullRect is optional. When specified, it must be a conservative bounds of all of the
1253 * resulting transformed quads, allowing the canvas to skip drawing if the cullRect does not
1254 * intersect the current clip.
1255 *
1256 * The paint is optional. If specified, its antialiasing, alpha, color-filter, image-filter
1257 * and xfermode are used to affect each of the quads.
1258 */
1259 void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
1260 const SkColor colors[], int count, SkXfermode::Mode, const SkRect* cullRect,
1261 const SkPaint* paint);
1262
1263 void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count,
1264 const SkRect* cullRect, const SkPaint* paint) {
1265 this->drawAtlas(atlas, xform, tex, NULL, count, SkXfermode::kDst_Mode, cullRect, paint);
1266 }
1267
reedf8053da2016-03-17 08:14:57 -07001268 void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1269 const SkColor colors[], int count, SkXfermode::Mode mode, const SkRect* cull,
1270 const SkPaint* paint) {
1271 this->drawAtlas(atlas.get(), xform, tex, colors, count, mode, cull, paint);
1272 }
1273 void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1274 int count, const SkRect* cullRect, const SkPaint* paint) {
1275 this->drawAtlas(atlas.get(), xform, tex, nullptr, count, SkXfermode::kDst_Mode,
1276 cullRect, paint);
1277 }
1278
reed71c3c762015-06-24 10:29:17 -07001279 /**
reed3cb38402015-02-06 08:36:15 -08001280 * Draw the contents of this drawable into the canvas. If the canvas is async
1281 * (e.g. it is recording into a picture) then the drawable will be referenced instead,
1282 * to have its draw() method called when the picture is finalized.
1283 *
1284 * If the intent is to force the contents of the drawable into this canvas immediately,
1285 * then drawable->draw(canvas) may be called.
1286 */
reeda8db7282015-07-07 10:22:31 -07001287 void drawDrawable(SkDrawable* drawable, const SkMatrix* = NULL);
1288 void drawDrawable(SkDrawable*, SkScalar x, SkScalar y);
reed6a070dc2014-11-11 19:36:09 -08001289
reedf70b5312016-03-04 16:36:20 -08001290 /**
1291 * Send an "annotation" to the canvas. The annotation is a key/value pair, where the key is
1292 * a null-terminated utf8 string, and the value is a blob of data stored in an SkData
1293 * (which may be null). The annotation is associated with the specified rectangle.
1294 *
1295 * The caller still retains its ownership of the data (if any).
1296 *
1297 * Note: on may canvas types, this information is ignored, but some canvases (e.g. recording
1298 * a picture or drawing to a PDF document) will pass on this information.
1299 */
1300 void drawAnnotation(const SkRect&, const char key[], SkData* value);
mtklein0fba3b92016-04-01 04:55:51 -07001301 void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
1302 this->drawAnnotation(rect, key, value.get());
1303 }
reedf70b5312016-03-04 16:36:20 -08001304
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 //////////////////////////////////////////////////////////////////////////
fmalita77650002016-01-21 18:47:11 -08001306#ifdef SK_INTERNAL
fmalita53d9f1c2016-01-25 06:23:54 -08001307#ifndef SK_SUPPORT_LEGACY_DRAWFILTER
1308 #define SK_SUPPORT_LEGACY_DRAWFILTER
fmalita77650002016-01-21 18:47:11 -08001309#endif
1310#endif
reed@google.com4b226022011-01-11 18:32:13 +00001311
fmalita53d9f1c2016-01-25 06:23:54 -08001312#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
reed@android.com8a1c16f2008-12-17 15:59:43 +00001313 /** Get the current filter object. The filter's reference count is not
reed@android.comdc3381f2010-02-11 16:05:15 +00001314 affected. The filter is saved/restored, just like the matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001315 @return the canvas' filter (or NULL).
1316 */
fmalita77650002016-01-21 18:47:11 -08001317 SK_ATTR_EXTERNALLY_DEPRECATED("getDrawFilter use is deprecated")
reed@android.com8a1c16f2008-12-17 15:59:43 +00001318 SkDrawFilter* getDrawFilter() const;
reed@google.com4b226022011-01-11 18:32:13 +00001319
reed@android.com8a1c16f2008-12-17 15:59:43 +00001320 /** Set the new filter (or NULL). Pass NULL to clear any existing filter.
1321 As a convenience, the parameter is returned. If an existing filter
1322 exists, its refcnt is decrement. If the new filter is not null, its
reed@android.comdc3381f2010-02-11 16:05:15 +00001323 refcnt is incremented. The filter is saved/restored, just like the
1324 matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001325 @param filter the new filter (or NULL)
1326 @return the new filter
1327 */
fmalita77650002016-01-21 18:47:11 -08001328 SK_ATTR_EXTERNALLY_DEPRECATED("setDrawFilter use is deprecated")
reed@android.com8a1c16f2008-12-17 15:59:43 +00001329 virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
fmalita77650002016-01-21 18:47:11 -08001330#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001331 //////////////////////////////////////////////////////////////////////////
1332
reed@google.com754de5f2014-02-24 19:38:20 +00001333 /**
1334 * Return true if the current clip is empty (i.e. nothing will draw).
1335 * Note: this is not always a free call, so it should not be used
1336 * more often than necessary. However, once the canvas has computed this
1337 * result, subsequent calls will be cheap (until the clip state changes,
1338 * which can happen on any clip..() or restore() call.
1339 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001340 virtual bool isClipEmpty() const;
reed@google.com754de5f2014-02-24 19:38:20 +00001341
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001342 /**
1343 * Returns true if the current clip is just a (non-empty) rectangle.
1344 * Returns false if the clip is empty, or if it is complex.
1345 */
1346 virtual bool isClipRect() const;
1347
reed@android.com8a1c16f2008-12-17 15:59:43 +00001348 /** Return the current matrix on the canvas.
1349 This does not account for the translate in any of the devices.
1350 @return The current matrix on the canvas.
1351 */
junov@chromium.orga907ac32012-02-24 21:54:07 +00001352 const SkMatrix& getTotalMatrix() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001353
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001354 /** Return the clip stack. The clip stack stores all the individual
1355 * clips organized by the save/restore frame in which they were
1356 * added.
1357 * @return the current clip stack ("list" of individual clip elements)
1358 */
1359 const SkClipStack* getClipStack() const {
reed687fa1c2015-04-07 08:00:56 -07001360 return fClipStack;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001361 }
1362
fmalitac3b589a2014-06-05 12:40:07 -07001363 typedef SkCanvasClipVisitor ClipVisitor;
reed@google.com90c07ea2012-04-13 13:50:27 +00001364 /**
1365 * Replays the clip operations, back to front, that have been applied to
1366 * the canvas, calling the appropriate method on the visitor for each
1367 * clip. All clips have already been transformed into device space.
1368 */
1369 void replayClips(ClipVisitor*) const;
1370
reed@android.com8a1c16f2008-12-17 15:59:43 +00001371 ///////////////////////////////////////////////////////////////////////////
1372
robertphillips36736a22016-04-23 08:26:43 -07001373 // don't call
robertphillips175dd9b2016-04-28 14:32:04 -07001374 GrDrawContext* internal_private_accessTopLayerDrawContext();
robertphillips36736a22016-04-23 08:26:43 -07001375
1376 // don't call
1377 static void Internal_Private_SetIgnoreSaveLayerBounds(bool);
1378 static bool Internal_Private_GetIgnoreSaveLayerBounds();
1379 static void Internal_Private_SetTreatSpriteAsBitmap(bool);
1380 static bool Internal_Private_GetTreatSpriteAsBitmap();
1381
1382 // TEMP helpers until we switch virtual over to const& for src-rect
1383 void legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1384 const SkPaint* paint,
1385 SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1386 void legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
1387 const SkPaint* paint,
1388 SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1389
tomhudsoncb3bd182016-05-18 07:24:16 -07001390 // expose minimum amount of information necessary for transitional refactoring
1391 /**
1392 * Returns CTM and clip bounds, translated from canvas coordinates to top layer coordinates.
1393 */
1394 void temporary_internal_describeTopLayer(SkMatrix* matrix, SkIRect* clip_bounds);
1395
robertphillipsda2cd8b2016-04-21 11:05:32 -07001396protected:
vjiaoblack95302da2016-07-21 10:25:54 -07001397#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -07001398 /** Returns the current (cumulative) draw depth of the canvas.
1399 */
1400 SkScalar getZ() const;
1401
vjiaoblack95302da2016-07-21 10:25:54 -07001402 sk_sp<SkLights> fLights;
1403#endif
1404
reed@google.com76f10a32014-02-05 15:32:21 +00001405 // default impl defers to getDevice()->newSurface(info)
reede8f30622016-03-23 18:59:25 -07001406 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&);
reed@google.com76f10a32014-02-05 15:32:21 +00001407
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001408 // default impl defers to its device
reed884e97c2015-05-26 11:31:54 -07001409 virtual bool onPeekPixels(SkPixmap*);
1410 virtual bool onAccessTopLayerPixels(SkPixmap*);
reedea5a6512016-07-07 16:44:27 -07001411 virtual SkImageInfo onImageInfo() const;
1412 virtual bool onGetProps(SkSurfaceProps*) const;
1413 virtual void onFlush();
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001414
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001415 // Subclass save/restore notifiers.
1416 // Overriders should call the corresponding INHERITED method up the inheritance chain.
reed4960eee2015-12-18 07:09:18 -08001417 // getSaveLayerStrategy()'s return value may suppress full layer allocation.
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001418 enum SaveLayerStrategy {
1419 kFullLayer_SaveLayerStrategy,
reed4960eee2015-12-18 07:09:18 -08001420 kNoLayer_SaveLayerStrategy,
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001421 };
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001422
fmalita6ca763f2014-06-17 13:52:18 -07001423 virtual void willSave() {}
reed4960eee2015-12-18 07:09:18 -08001424 // Overriders should call the corresponding INHERITED method up the inheritance chain.
1425 virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) {
1426 return kFullLayer_SaveLayerStrategy;
1427 }
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001428 virtual void willRestore() {}
mtklein6cfa73a2014-08-13 13:33:49 -07001429 virtual void didRestore() {}
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001430 virtual void didConcat(const SkMatrix&) {}
1431 virtual void didSetMatrix(const SkMatrix&) {}
mtkleincbdf0072016-08-19 09:05:27 -07001432 virtual void didTranslate(SkScalar dx, SkScalar dy) {
1433 this->didConcat(SkMatrix::MakeTrans(dx, dy));
1434 }
vjiaoblack95302da2016-07-21 10:25:54 -07001435
1436#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -07001437 virtual void didTranslateZ(SkScalar) {}
vjiaoblack95302da2016-07-21 10:25:54 -07001438#endif
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +00001439
reedf70b5312016-03-04 16:36:20 -08001440 virtual void onDrawAnnotation(const SkRect&, const char key[], SkData* value);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +00001441 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
1442
reed@google.come0d9ce82014-04-23 04:00:17 +00001443 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1444 SkScalar y, const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001445
reed@google.come0d9ce82014-04-23 04:00:17 +00001446 virtual void onDrawPosText(const void* text, size_t byteLength,
1447 const SkPoint pos[], const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001448
reed@google.come0d9ce82014-04-23 04:00:17 +00001449 virtual void onDrawPosTextH(const void* text, size_t byteLength,
1450 const SkScalar xpos[], SkScalar constY,
1451 const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001452
reed@google.come0d9ce82014-04-23 04:00:17 +00001453 virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1454 const SkPath& path, const SkMatrix* matrix,
1455 const SkPaint& paint);
reed45561a02016-07-07 12:47:17 -07001456 virtual void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
1457 const SkRect* cullRect, const SkPaint& paint);
mtklein6cfa73a2014-08-13 13:33:49 -07001458
fmalita00d5c2c2014-08-21 08:53:26 -07001459 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1460 const SkPaint& paint);
1461
dandovb3c9d1c2014-08-12 08:34:29 -07001462 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1463 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
reed@google.come0d9ce82014-04-23 04:00:17 +00001464
reeda8db7282015-07-07 10:22:31 -07001465 virtual void onDrawDrawable(SkDrawable*, const SkMatrix*);
reed6a070dc2014-11-11 19:36:09 -08001466
reed41af9662015-01-05 07:49:08 -08001467 virtual void onDrawPaint(const SkPaint&);
1468 virtual void onDrawRect(const SkRect&, const SkPaint&);
msarett44df6512016-08-25 13:54:30 -07001469 virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint);
reed41af9662015-01-05 07:49:08 -08001470 virtual void onDrawOval(const SkRect&, const SkPaint&);
bsalomonac3aa242016-08-19 11:25:19 -07001471 virtual void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
1472 const SkPaint&);
reed41af9662015-01-05 07:49:08 -08001473 virtual void onDrawRRect(const SkRRect&, const SkPaint&);
1474 virtual void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&);
1475 virtual void onDrawVertices(VertexMode, int vertexCount, const SkPoint vertices[],
1476 const SkPoint texs[], const SkColor colors[], SkXfermode*,
1477 const uint16_t indices[], int indexCount, const SkPaint&);
mtklein2766c002015-06-26 11:45:03 -07001478
reed71c3c762015-06-24 10:29:17 -07001479 virtual void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
1480 int count, SkXfermode::Mode, const SkRect* cull, const SkPaint*);
reed41af9662015-01-05 07:49:08 -08001481 virtual void onDrawPath(const SkPath&, const SkPaint&);
1482 virtual void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*);
reed562fe472015-07-28 07:35:14 -07001483 virtual void onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkPaint*,
1484 SrcRectConstraint);
reed4c21dc52015-06-25 12:32:03 -07001485 virtual void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
1486 const SkPaint*);
msarett16882062016-08-16 09:31:08 -07001487 virtual void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
1488 const SkPaint*);
reed4c21dc52015-06-25 12:32:03 -07001489
reed41af9662015-01-05 07:49:08 -08001490 virtual void onDrawBitmap(const SkBitmap&, SkScalar dx, SkScalar dy, const SkPaint*);
1491 virtual void onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, const SkPaint*,
reed562fe472015-07-28 07:35:14 -07001492 SrcRectConstraint);
reed41af9662015-01-05 07:49:08 -08001493 virtual void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
1494 const SkPaint*);
msarett16882062016-08-16 09:31:08 -07001495 virtual void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
1496 const SkPaint*);
reed41af9662015-01-05 07:49:08 -08001497
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001498 enum ClipEdgeStyle {
1499 kHard_ClipEdgeStyle,
1500 kSoft_ClipEdgeStyle
1501 };
1502
reed73603f32016-09-20 08:42:38 -07001503 virtual void onClipRect(const SkRect& rect, ClipOp, ClipEdgeStyle);
1504 virtual void onClipRRect(const SkRRect& rrect, ClipOp, ClipEdgeStyle);
1505 virtual void onClipPath(const SkPath& path, ClipOp, ClipEdgeStyle);
1506 virtual void onClipRegion(const SkRegion& deviceRgn, ClipOp);
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001507
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001508 virtual void onDiscard();
1509
reedd5fa1a42014-08-09 11:08:05 -07001510 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*);
robertphillips9b14f262014-06-04 05:40:44 -07001511
vjiaoblack95302da2016-07-21 10:25:54 -07001512#ifdef SK_EXPERIMENTAL_SHADOWING
1513 virtual void onDrawShadowedPicture(const SkPicture*,
1514 const SkMatrix*,
vjiaoblacke6f5d562016-08-25 06:30:23 -07001515 const SkPaint*,
1516 const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -07001517#endif
Herb Derby13569782016-10-06 14:33:43 -04001518
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001519 // Returns the canvas to be used by DrawIter. Default implementation
junov@google.com4370aed2012-01-18 16:21:08 +00001520 // returns this. Subclasses that encapsulate an indirect canvas may
1521 // need to overload this method. The impl must keep track of this, as it
1522 // is not released or deleted by the caller.
1523 virtual SkCanvas* canvasForDrawIter();
1524
junov@chromium.orga907ac32012-02-24 21:54:07 +00001525 // Clip rectangle bounds. Called internally by saveLayer.
1526 // returns false if the entire rectangle is entirely clipped out
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001527 // If non-NULL, The imageFilter parameter will be used to expand the clip
1528 // and offscreen bounds for any margin required by the filter DAG.
reed4960eee2015-12-18 07:09:18 -08001529 bool clipRectBounds(const SkRect* bounds, SaveLayerFlags, SkIRect* intersection,
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001530 const SkImageFilter* imageFilter = NULL);
junov@chromium.orga907ac32012-02-24 21:54:07 +00001531
reedc83a2972015-07-16 07:40:45 -07001532private:
reed3aafe112016-08-18 12:45:34 -07001533 /** After calling saveLayer(), there can be any number of devices that make
1534 up the top-most drawing area. LayerIter can be used to iterate through
1535 those devices. Note that the iterator is only valid until the next API
1536 call made on the canvas. Ownership of all pointers in the iterator stays
1537 with the canvas, so none of them should be modified or deleted.
1538 */
1539 class LayerIter /*: SkNoncopyable*/ {
1540 public:
1541 /** Initialize iterator with canvas, and set values for 1st device */
1542 LayerIter(SkCanvas*);
1543 ~LayerIter();
1544
1545 /** Return true if the iterator is done */
1546 bool done() const { return fDone; }
1547 /** Cycle to the next device */
1548 void next();
1549
1550 // These reflect the current device in the iterator
1551
1552 SkBaseDevice* device() const;
1553 const SkMatrix& matrix() const;
1554 const SkRasterClip& clip() const;
1555 const SkPaint& paint() const;
1556 int x() const;
1557 int y() const;
1558
1559 private:
1560 // used to embed the SkDrawIter object directly in our instance, w/o
1561 // having to expose that class def to the public. There is an assert
1562 // in our constructor to ensure that fStorage is large enough
1563 // (though needs to be a compile-time-assert!). We use intptr_t to work
1564 // safely with 32 and 64 bit machines (to ensure the storage is enough)
1565 intptr_t fStorage[32];
1566 class SkDrawIter* fImpl; // this points at fStorage
1567 SkPaint fDefaultPaint;
1568 bool fDone;
1569 };
Herb Derby13569782016-10-06 14:33:43 -04001570
reed4960eee2015-12-18 07:09:18 -08001571 static bool BoundsAffectsClip(SaveLayerFlags);
reedbada1882015-12-21 13:09:44 -08001572 static SaveLayerFlags LegacySaveFlagsToSaveLayerFlags(uint32_t legacySaveFlags);
reed4960eee2015-12-18 07:09:18 -08001573
reeda2217ef2016-07-20 06:04:34 -07001574 static void DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter,
1575 SkBaseDevice* dst, const SkMatrix& ctm,
1576 const SkClipStack* clipStack);
1577
reedc83a2972015-07-16 07:40:45 -07001578 enum ShaderOverrideOpacity {
1579 kNone_ShaderOverrideOpacity, //!< there is no overriding shader (bitmap or image)
1580 kOpaque_ShaderOverrideOpacity, //!< the overriding shader is opaque
1581 kNotOpaque_ShaderOverrideOpacity, //!< the overriding shader may not be opaque
1582 };
1583
reed@google.com97af1a62012-08-28 12:19:02 +00001584 // notify our surface (if we have one) that we are about to draw, so it
1585 // can perform copy-on-write or invalidate any cached images
reedc83a2972015-07-16 07:40:45 -07001586 void predrawNotify(bool willOverwritesEntireSurface = false);
1587 void predrawNotify(const SkRect* rect, const SkPaint* paint, ShaderOverrideOpacity);
1588 void predrawNotify(const SkRect* rect, const SkPaint* paint, bool shaderOverrideIsOpaque) {
1589 this->predrawNotify(rect, paint, shaderOverrideIsOpaque ? kOpaque_ShaderOverrideOpacity
1590 : kNotOpaque_ShaderOverrideOpacity);
1591 }
reed@google.com97af1a62012-08-28 12:19:02 +00001592
reed@android.com8a1c16f2008-12-17 15:59:43 +00001593 class MCRec;
1594
reed687fa1c2015-04-07 08:00:56 -07001595 SkAutoTUnref<SkClipStack> fClipStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001596 SkDeque fMCStack;
1597 // points to top of stack
1598 MCRec* fMCRec;
1599 // the first N recs that can fit here mean we won't call malloc
reedb679ca82015-04-07 04:40:48 -07001600 enum {
reeda499f902015-05-01 09:34:31 -07001601 kMCRecSize = 128, // most recent measurement
reed31b80a92015-11-16 13:22:24 -08001602 kMCRecCount = 32, // common depth for save/restores
reed8c30a812016-04-20 16:36:51 -07001603 kDeviceCMSize = 176, // most recent measurement
reedb679ca82015-04-07 04:40:48 -07001604 };
1605 intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
reeda499f902015-05-01 09:34:31 -07001606 intptr_t fDeviceCMStorage[kDeviceCMSize / sizeof(intptr_t)];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001607
reed4a8126e2014-09-22 07:29:03 -07001608 const SkSurfaceProps fProps;
1609
reed2ff1fce2014-12-11 07:07:37 -08001610 int fSaveCount; // value returned by getSaveCount()
reed@android.com8a1c16f2008-12-17 15:59:43 +00001611
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +00001612 SkMetaData* fMetaData;
1613
reed@google.com97af1a62012-08-28 12:19:02 +00001614 SkSurface_Base* fSurfaceBase;
1615 SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1616 void setSurfaceBase(SkSurface_Base* sb) {
1617 fSurfaceBase = sb;
1618 }
1619 friend class SkSurface_Base;
junov@chromium.org45c3db82013-04-11 17:52:05 +00001620 friend class SkSurface_Gpu;
skia.committer@gmail.comfc843592012-10-11 02:01:14 +00001621
reed@android.com8a1c16f2008-12-17 15:59:43 +00001622 bool fDeviceCMDirty; // cleared by updateDeviceCMCache()
1623 void updateDeviceCMCache();
1624
reed2ff1fce2014-12-11 07:07:37 -08001625 void doSave();
1626 void checkForDeferredSave();
reed8c30a812016-04-20 16:36:51 -07001627 void internalSetMatrix(const SkMatrix&);
reed2ff1fce2014-12-11 07:07:37 -08001628
reed@google.com9c135db2014-03-12 18:28:35 +00001629 friend class SkDrawIter; // needs setupDrawForLayerDevice()
reed@google.com8926b162012-03-23 15:36:36 +00001630 friend class AutoDrawLooper;
reed@google.com9c135db2014-03-12 18:28:35 +00001631 friend class SkLua; // needs top layer size and offset
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +00001632 friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
reed52d9ac62014-06-30 09:05:34 -07001633 friend class SkSurface_Raster; // needs getDevice()
reedd9544982014-09-09 18:46:22 -07001634 friend class SkRecorder; // InitFlags
mtklein9c5052f2016-08-06 12:51:51 -07001635 friend class SkLiteRecorder; // InitFlags
reedd9544982014-09-09 18:46:22 -07001636 friend class SkNoSaveLayerCanvas; // InitFlags
fmalita2d97bc12014-11-20 10:44:58 -08001637 friend class SkPictureImageFilter; // SkCanvas(SkBaseDevice*, SkSurfaceProps*, InitFlags)
reedc83a2972015-07-16 07:40:45 -07001638 friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
reed4960eee2015-12-18 07:09:18 -08001639 friend class SkPicturePlayback; // SaveFlagsToSaveLayerFlags
Herb Derby13569782016-10-06 14:33:43 -04001640 friend class SkDeferredCanvas; // For use of resetForNextPicture
piotaixrb5fae932014-09-24 13:03:30 -07001641
reedd9544982014-09-09 18:46:22 -07001642 enum InitFlags {
1643 kDefault_InitFlags = 0,
1644 kConservativeRasterClip_InitFlag = 1 << 0,
1645 };
reed78e27682014-11-19 08:04:34 -08001646 SkCanvas(const SkIRect& bounds, InitFlags);
robertphillipsfcf78292015-06-19 11:49:52 -07001647 SkCanvas(SkBaseDevice* device, InitFlags);
reedd9544982014-09-09 18:46:22 -07001648
mtkleinfeaadee2015-04-08 11:25:48 -07001649 void resetForNextPicture(const SkIRect& bounds);
1650
reed8f2e7912014-09-04 12:45:18 -07001651 // needs gettotalclip()
tfarinaa5414c42014-10-10 06:19:09 -07001652 friend class SkCanvasStateUtils;
piotaixrb5fae932014-09-24 13:03:30 -07001653
reed4a8126e2014-09-22 07:29:03 -07001654 // call this each time we attach ourselves to a device
1655 // - constructor
1656 // - internalSaveLayer
1657 void setupDevice(SkBaseDevice*);
1658
reedd9544982014-09-09 18:46:22 -07001659 SkBaseDevice* init(SkBaseDevice*, InitFlags);
reed@google.comf0b5e112011-09-07 11:57:34 +00001660
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001661 /**
senorblancoafc7cce2016-02-02 18:44:15 -08001662 * Gets the bounds of the top level layer in global canvas coordinates. We don't want this
bsalomon@google.com4ebe3822014-02-26 20:22:32 +00001663 * to be public because it exposes decisions about layer sizes that are internal to the canvas.
1664 */
senorblancoafc7cce2016-02-02 18:44:15 -08001665 SkIRect getTopLayerBounds() const;
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001666
reed@google.com71121732012-09-18 15:14:33 +00001667 void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001668 const SkRect& dst, const SkPaint* paint,
reeda5517e22015-07-14 10:54:12 -07001669 SrcRectConstraint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001670 void internalDrawPaint(const SkPaint& paint);
reed4960eee2015-12-18 07:09:18 -08001671 void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
reed7503d602016-07-15 14:23:29 -07001672 void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001673
reed@android.com8a1c16f2008-12-17 15:59:43 +00001674 // shared by save() and saveLayer()
reed2ff1fce2014-12-11 07:07:37 -08001675 void internalSave();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001676 void internalRestore();
bungeman@google.com52c748b2011-08-22 21:30:43 +00001677 static void DrawRect(const SkDraw& draw, const SkPaint& paint,
1678 const SkRect& r, SkScalar textSize);
1679 static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
1680 const char text[], size_t byteLength,
1681 SkScalar x, SkScalar y);
reed@google.com4b226022011-01-11 18:32:13 +00001682
reedfa35f8e2014-09-04 12:14:36 -07001683 // only for canvasutils
1684 const SkRegion& internal_private_getTotalClip() const;
1685
reedc83a2972015-07-16 07:40:45 -07001686 /*
1687 * Returns true if drawing the specified rect (or all if it is null) with the specified
1688 * paint (or default if null) would overwrite the entire root device of the canvas
1689 * (i.e. the canvas' surface if it had one).
1690 */
1691 bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
1692
reed262a71b2015-12-05 13:07:27 -08001693 /**
1694 * Returns true if the paint's imagefilter can be invoked directly, without needed a layer.
1695 */
1696 bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint&);
reedc83a2972015-07-16 07:40:45 -07001697
msarettfbfa2582016-08-12 08:29:08 -07001698
1699 /**
1700 * Keep track of the device clip bounds and if the matrix is scale-translate. This allows
1701 * us to do a fast quick reject in the common case.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001702 */
msarett9637ea92016-08-18 14:03:30 -07001703 bool fIsScaleTranslate;
msarettfbfa2582016-08-12 08:29:08 -07001704 SkRect fDeviceClipBounds;
1705
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001706 bool fAllowSoftClip;
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001707 bool fAllowSimplifyClip;
reed42b73eb2015-11-20 13:42:42 -08001708 const bool fConservativeRasterClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001709
reed@google.com5c3d1472011-02-22 19:12:23 +00001710 class AutoValidateClip : ::SkNoncopyable {
1711 public:
1712 explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1713 fCanvas->validateClip();
1714 }
1715 ~AutoValidateClip() { fCanvas->validateClip(); }
1716
1717 private:
1718 const SkCanvas* fCanvas;
1719 };
1720
1721#ifdef SK_DEBUG
1722 void validateClip() const;
1723#else
1724 void validateClip() const {}
1725#endif
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +00001726
1727 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001728};
1729
1730/** Stack helper class to automatically call restoreToCount() on the canvas
1731 when this object goes out of scope. Use this to guarantee that the canvas
1732 is restored to a known state.
1733*/
1734class SkAutoCanvasRestore : SkNoncopyable {
1735public:
commit-bot@chromium.org28871192013-10-14 15:28:01 +00001736 SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1737 if (fCanvas) {
1738 fSaveCount = canvas->getSaveCount();
1739 if (doSave) {
1740 canvas->save();
1741 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001742 }
1743 }
1744 ~SkAutoCanvasRestore() {
reed@google.comf6c9a5b2012-11-20 15:12:21 +00001745 if (fCanvas) {
1746 fCanvas->restoreToCount(fSaveCount);
1747 }
1748 }
1749
1750 /**
1751 * Perform the restore now, instead of waiting for the destructor. Will
1752 * only do this once.
1753 */
1754 void restore() {
1755 if (fCanvas) {
1756 fCanvas->restoreToCount(fSaveCount);
1757 fCanvas = NULL;
1758 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001759 }
1760
1761private:
1762 SkCanvas* fCanvas;
1763 int fSaveCount;
1764};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001765#define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001766
fmalitac3b589a2014-06-05 12:40:07 -07001767class SkCanvasClipVisitor {
1768public:
1769 virtual ~SkCanvasClipVisitor();
reed73603f32016-09-20 08:42:38 -07001770 virtual void clipRect(const SkRect&, SkCanvas::ClipOp, bool antialias) = 0;
1771 virtual void clipRRect(const SkRRect&, SkCanvas::ClipOp, bool antialias) = 0;
1772 virtual void clipPath(const SkPath&, SkCanvas::ClipOp, bool antialias) = 0;
fmalitac3b589a2014-06-05 12:40:07 -07001773};
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001774
reed@android.com8a1c16f2008-12-17 15:59:43 +00001775#endif