blob: 7b6ef7de68d065ceea337dbc76c4a771147594e3 [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
reed374772b2016-10-05 17:33:02 -070011#include "SkBlendMode.h"
reed73603f32016-09-20 08:42:38 -070012#include "SkClipOp.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkDeque.h"
14#include "SkPaint.h"
Mike Reed356f7c22017-01-10 11:58:39 -050015#include "SkRasterHandleAllocator.h"
reed4a8126e2014-09-22 07:29:03 -070016#include "SkSurfaceProps.h"
vjiaoblack95302da2016-07-21 10:25:54 -070017#include "SkLights.h"
vjiaoblacke6f5d562016-08-25 06:30:23 -070018#include "../private/SkShadowParams.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019
bungemand3ebb482015-08-05 13:57:49 -070020class GrContext;
Brian Osman11052242016-10-27 14:47:55 -040021class GrRenderTargetContext;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000022class SkBaseDevice;
Mike Reed267be7f2017-02-13 09:32:54 -050023class SkBitmap;
bungemand3ebb482015-08-05 13:57:49 -070024class SkClipStack;
reedf70b5312016-03-04 16:36:20 -080025class SkData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000026class SkDraw;
reed3cb38402015-02-06 08:36:15 -080027class SkDrawable;
reed@android.com8a1c16f2008-12-17 15:59:43 +000028class SkDrawFilter;
Mike Reed267be7f2017-02-13 09:32:54 -050029class SkImage;
bungemand3ebb482015-08-05 13:57:49 -070030class SkImageFilter;
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +000031class SkMetaData;
bungemand3ebb482015-08-05 13:57:49 -070032class SkPath;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkPicture;
bungemand3ebb482015-08-05 13:57:49 -070034class SkPixmap;
reed1e7f5e72016-04-27 07:49:17 -070035class SkRasterClip;
Mike Reed267be7f2017-02-13 09:32:54 -050036class SkRegion;
reed@google.com4ed0fb72012-12-12 20:48:18 +000037class SkRRect;
reed71c3c762015-06-24 10:29:17 -070038struct SkRSXform;
reed@google.com76f10a32014-02-05 15:32:21 +000039class SkSurface;
reed@google.com97af1a62012-08-28 12:19:02 +000040class SkSurface_Base;
fmalita00d5c2c2014-08-21 08:53:26 -070041class SkTextBlob;
Brian Salomon199fb872017-02-06 09:41:10 -050042class SkVertices;
reedfa35f8e2014-09-04 12:14:36 -070043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044/** \class SkCanvas
45
46 A Canvas encapsulates all of the state about drawing into a device (bitmap).
47 This includes a reference to the device itself, and a stack of matrix/clip
48 values. For any given draw call (e.g. drawRect), the geometry of the object
49 being drawn is transformed by the concatenation of all the matrices in the
50 stack. The transformed geometry is clipped by the intersection of all of
51 the clips in the stack.
52
53 While the Canvas holds the state of the drawing device, the state (style)
54 of the object being drawn is held by the Paint, which is provided as a
55 parameter to each of the draw() methods. The Paint holds attributes such as
56 color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns),
57 etc.
58*/
Mike Reed02b73492016-12-07 16:52:58 -050059class SK_API SkCanvas : SkNoncopyable {
reedbada1882015-12-21 13:09:44 -080060 enum PrivateSaveLayerFlags {
caryclark952538e2016-02-26 05:01:42 -080061 kDontClipToLayer_PrivateSaveLayerFlag = 1U << 31,
reedbada1882015-12-21 13:09:44 -080062 };
reed73603f32016-09-20 08:42:38 -070063
reed@android.com8a1c16f2008-12-17 15:59:43 +000064public:
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000065 /**
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000066 * Attempt to allocate raster canvas, matching the ImageInfo, that will draw directly into the
67 * specified pixels. To access the pixels after drawing to them, the caller should call
68 * flush() or call peekPixels(...).
69 *
70 * On failure, return NULL. This can fail for several reasons:
71 * 1. invalid ImageInfo (e.g. negative dimensions)
72 * 2. unsupported ImageInfo for a canvas
73 * - kUnknown_SkColorType, kIndex_8_SkColorType
reed44977482015-02-27 10:23:00 -080074 * - kUnknown_SkAlphaType
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000075 * - this list is not complete, so others may also be unsupported
76 *
77 * Note: it is valid to request a supported ImageInfo, but with zero
78 * dimensions.
79 */
Mike Reed5df49342016-11-12 08:06:55 -060080 static std::unique_ptr<SkCanvas> MakeRasterDirect(const SkImageInfo&, void*, size_t);
81
82 static std::unique_ptr<SkCanvas> MakeRasterDirectN32(int width, int height, SkPMColor* pixels,
83 size_t rowBytes) {
84 return MakeRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
85 }
86
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000087 /**
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000088 * Creates an empty canvas with no backing device/pixels, and zero
89 * dimensions.
90 */
reed@google.comcde92112011-07-06 20:00:52 +000091 SkCanvas();
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +000092
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000093 /**
94 * Creates a canvas of the specified dimensions, but explicitly not backed
95 * by any device/pixels. Typically this use used by subclasses who handle
96 * the draw calls in some other way.
97 */
reed96a857e2015-01-25 10:33:58 -080098 SkCanvas(int width, int height, const SkSurfaceProps* = NULL);
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000099
reed@google.com6dc74552011-07-21 18:00:46 +0000100 /** Construct a canvas with the specified device to draw into.
bsalomon@google.come97f0852011-06-17 13:10:25 +0000101
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000102 @param device Specifies a device for the canvas to draw into.
103 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000104 explicit SkCanvas(SkBaseDevice* device);
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000105
reed@google.com44699382013-10-31 17:28:30 +0000106 /** Construct a canvas with the specified bitmap to draw into.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 @param bitmap Specifies a bitmap for the canvas to draw into. Its
108 structure are copied to the canvas.
109 */
110 explicit SkCanvas(const SkBitmap& bitmap);
fmalita3d91aad2015-02-02 05:25:04 -0800111
Matt Sarett31f99ce2017-04-11 08:46:01 -0400112#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
113 enum class ColorBehavior {
114 kLegacy,
115 };
116
117 /**
118 * Android framework only constructor.
119 * Allows the creation of a legacy SkCanvas even though the |bitmap|
120 * and its pixel ref may have an SkColorSpace.
121 */
122 SkCanvas(const SkBitmap& bitmap, ColorBehavior);
123#endif
124
fmalita3d91aad2015-02-02 05:25:04 -0800125 /** Construct a canvas with the specified bitmap to draw into.
126 @param bitmap Specifies a bitmap for the canvas to draw into. Its
127 structure are copied to the canvas.
128 @param props New canvas surface properties.
129 */
130 SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
131
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 virtual ~SkCanvas();
133
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +0000134 SkMetaData& getMetaData();
135
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000136 /**
137 * Return ImageInfo for this canvas. If the canvas is not backed by pixels
138 * (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
139 */
140 SkImageInfo imageInfo() const;
141
brianosman898235c2016-04-06 07:38:23 -0700142 /**
143 * If the canvas is backed by pixels (cpu or gpu), this writes a copy of the SurfaceProps
144 * for the canvas to the location supplied by the caller, and returns true. Otherwise,
145 * return false and leave the supplied props unchanged.
146 */
147 bool getProps(SkSurfaceProps*) const;
148
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149 ///////////////////////////////////////////////////////////////////////////
150
reed@google.com210ce002011-11-01 14:24:23 +0000151 /**
bsalomone63ffef2016-02-05 07:17:34 -0800152 * Trigger the immediate execution of all pending draw operations. For the GPU
153 * backend this will resolve all rendering to the GPU surface backing the
154 * SkSurface that owns this canvas.
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000155 */
156 void flush();
157
158 /**
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000159 * Gets the size of the base or root layer in global canvas coordinates. The
160 * origin of the base layer is always (0,0). The current drawable area may be
161 * smaller (due to clipping or saveLayer).
reed@google.com210ce002011-11-01 14:24:23 +0000162 */
tomhudson68260fa2015-03-23 07:22:40 -0700163 virtual SkISize getBaseLayerSize() const;
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000164
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000165 /**
reed@google.com76f10a32014-02-05 15:32:21 +0000166 * Create a new surface matching the specified info, one that attempts to
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000167 * be maximally compatible when used with this canvas. If there is no matching Surface type,
168 * NULL is returned.
reed4a8126e2014-09-22 07:29:03 -0700169 *
170 * If surfaceprops is specified, those are passed to the new surface, otherwise the new surface
171 * inherits the properties of the surface that owns this canvas. If this canvas has no parent
172 * surface, then the new surface is created with default properties.
reed@google.com76f10a32014-02-05 15:32:21 +0000173 */
reede8f30622016-03-23 18:59:25 -0700174 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps* = nullptr);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000175
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000176 /**
177 * Return the GPU context of the device that is associated with the canvas.
178 * For a canvas with non-GPU device, NULL is returned.
179 */
180 GrContext* getGrContext();
181
reed@google.com4b226022011-01-11 18:32:13 +0000182 ///////////////////////////////////////////////////////////////////////////
183
bsalomon@google.comdaba14b2011-11-02 20:10:48 +0000184 /**
reed@google.com9c135db2014-03-12 18:28:35 +0000185 * If the canvas has writable pixels in its top layer (and is not recording to a picture
186 * or other non-raster target) and has direct access to its pixels (i.e. they are in
187 * local RAM) return the address of those pixels, and if not null,
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000188 * return the ImageInfo, rowBytes and origin. The returned address is only valid
reed@google.com9c135db2014-03-12 18:28:35 +0000189 * while the canvas object is in scope and unchanged. Any API calls made on
190 * canvas (or its parent surface if any) will invalidate the
191 * returned address (and associated information).
192 *
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000193 * On failure, returns NULL and the info, rowBytes, and origin parameters are ignored.
reed@google.com9c135db2014-03-12 18:28:35 +0000194 */
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000195 void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
reed@google.com9c135db2014-03-12 18:28:35 +0000196
Mike Reed356f7c22017-01-10 11:58:39 -0500197 SkRasterHandleAllocator::Handle accessTopRasterHandle() const;
198
reed@google.com9c135db2014-03-12 18:28:35 +0000199 /**
200 * If the canvas has readable pixels in its base layer (and is not recording to a picture
201 * or other non-raster target) and has direct access to its pixels (i.e. they are in
reed6ceeebd2016-03-09 14:26:26 -0800202 * local RAM) return true, and if not null, return in the pixmap parameter information about
203 * the pixels. The pixmap's pixel address is only valid
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000204 * while the canvas object is in scope and unchanged. Any API calls made on
reed6ceeebd2016-03-09 14:26:26 -0800205 * canvas (or its parent surface if any) will invalidate the pixel address
206 * (and associated information).
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000207 *
reed6ceeebd2016-03-09 14:26:26 -0800208 * On failure, returns false and the pixmap parameter will be ignored.
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000209 */
reed6ceeebd2016-03-09 14:26:26 -0800210 bool peekPixels(SkPixmap*);
211
reed@google.com4b226022011-01-11 18:32:13 +0000212 /**
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000213 * Copy the pixels from the base-layer into the specified buffer (pixels + rowBytes),
214 * converting them into the requested format (SkImageInfo). The base-layer pixels are read
reedb184f7f2014-07-13 04:32:32 -0700215 * starting at the specified (srcX,srcY) location in the coordinate system of the base-layer.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000216 *
reedb184f7f2014-07-13 04:32:32 -0700217 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000218 *
reedb184f7f2014-07-13 04:32:32 -0700219 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000220 *
reedb184f7f2014-07-13 04:32:32 -0700221 * srcR is intersected with the bounds of the base-layer. If this intersection is not empty,
222 * then we have two sets of pixels (of equal size). Replace the dst pixels with the
223 * corresponding src pixels, performing any colortype/alphatype transformations needed
224 * (in the case where the src and dst have different colortypes or alphatypes).
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000225 *
226 * This call can fail, returning false, for several reasons:
reedb184f7f2014-07-13 04:32:32 -0700227 * - If srcR does not intersect the base-layer bounds.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000228 * - If the requested colortype/alphatype cannot be converted from the base-layer's types.
229 * - If this canvas is not backed by pixels (e.g. picture or PDF)
230 */
reedb184f7f2014-07-13 04:32:32 -0700231 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
232 int srcX, int srcY);
Mike Reed12e946b2017-04-17 10:53:29 -0400233 bool readPixels(const SkPixmap&, int srcX, int srcY);
234 bool readPixels(const SkBitmap&, int srcX, int srcY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000235
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000236 /**
237 * This method affects the pixels in the base-layer, and operates in pixel coordinates,
238 * ignoring the matrix and clip.
239 *
240 * The specified ImageInfo and (x,y) offset specifies a rectangle: target.
241 *
242 * target.setXYWH(x, y, info.width(), info.height());
243 *
244 * Target is intersected with the bounds of the base-layer. If this intersection is not empty,
245 * then we have two sets of pixels (of equal size), the "src" specified by info+pixels+rowBytes
246 * and the "dst" by the canvas' backend. Replace the dst pixels with the corresponding src
247 * pixels, performing any colortype/alphatype transformations needed (in the case where the
248 * src and dst have different colortypes or alphatypes).
249 *
250 * This call can fail, returning false, for several reasons:
251 * - If the src colortype/alphatype cannot be converted to the canvas' types
252 * - If this canvas is not backed by pixels (e.g. picture or PDF)
253 */
254 bool writePixels(const SkImageInfo&, const void* pixels, size_t rowBytes, int x, int y);
255
256 /**
257 * Helper for calling writePixels(info, ...) by passing its pixels and rowbytes. If the bitmap
258 * is just wrapping a texture, returns false and does nothing.
259 */
260 bool writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com4b226022011-01-11 18:32:13 +0000261
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 ///////////////////////////////////////////////////////////////////////////
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000263
reed@android.comdc3381f2010-02-11 16:05:15 +0000264 /** This call saves the current matrix, clip, and drawFilter, and pushes a
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 copy onto a private stack. Subsequent calls to translate, scale,
reed@android.comdc3381f2010-02-11 16:05:15 +0000266 rotate, skew, concat or clipRect, clipPath, and setDrawFilter all
267 operate on this copy.
268 When the balancing call to restore() is made, the previous matrix, clip,
269 and drawFilter are restored.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000270
271 @return The value to pass to restoreToCount() to balance this save()
272 */
273 int save();
274
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 /** This behaves the same as save(), but in addition it allocates an
276 offscreen bitmap. All drawing calls are directed there, and only when
277 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000278 the canvas (or the previous layer).
reed@android.comad164b22010-07-02 17:20:51 +0000279 @param bounds (may be null) This rect, if non-null, is used as a hint to
280 limit the size of the offscreen, and thus drawing may be
281 clipped to it, though that clipping is not guaranteed to
282 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 @param paint (may be null) This is copied, and is applied to the
284 offscreen when restore() is called
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000285 @return The value to pass to restoreToCount() to balance this save()
286 */
287 int saveLayer(const SkRect* bounds, const SkPaint* paint);
reed021f6312015-08-09 19:41:13 -0700288 int saveLayer(const SkRect& bounds, const SkPaint* paint) {
289 return this->saveLayer(&bounds, paint);
290 }
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000291
reed70ee31b2015-12-10 13:44:45 -0800292 /**
293 * Temporary name.
294 * Will allow any requests for LCD text to be respected, so the caller must be careful to
295 * only draw on top of opaque sections of the layer to get good results.
296 */
297 int saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint);
298
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 /** This behaves the same as save(), but in addition it allocates an
300 offscreen bitmap. All drawing calls are directed there, and only when
301 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000302 the canvas (or the previous layer).
reed@android.com40408612010-07-02 17:24:23 +0000303 @param bounds (may be null) This rect, if non-null, is used as a hint to
304 limit the size of the offscreen, and thus drawing may be
305 clipped to it, though that clipping is not guaranteed to
306 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 @param alpha This is applied to the offscreen when restore() is called.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000308 @return The value to pass to restoreToCount() to balance this save()
309 */
310 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
311
reed4960eee2015-12-18 07:09:18 -0800312 enum {
313 kIsOpaque_SaveLayerFlag = 1 << 0,
314 kPreserveLCDText_SaveLayerFlag = 1 << 1,
reedbada1882015-12-21 13:09:44 -0800315
Mike Reedc61abee2017-02-28 17:45:27 -0500316 /** initialize the new layer with the contents of the previous layer */
317 kInitWithPrevious_SaveLayerFlag = 1 << 2,
318
reedbada1882015-12-21 13:09:44 -0800319#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
320 kDontClipToLayer_Legacy_SaveLayerFlag = kDontClipToLayer_PrivateSaveLayerFlag,
321#endif
reed4960eee2015-12-18 07:09:18 -0800322 };
323 typedef uint32_t SaveLayerFlags;
324
325 struct SaveLayerRec {
reedbfd5f172016-01-07 11:28:08 -0800326 SaveLayerRec()
327 : fBounds(nullptr), fPaint(nullptr), fBackdrop(nullptr), fSaveLayerFlags(0)
328 {}
reed4960eee2015-12-18 07:09:18 -0800329 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, SaveLayerFlags saveLayerFlags = 0)
330 : fBounds(bounds)
331 , fPaint(paint)
reedbfd5f172016-01-07 11:28:08 -0800332 , fBackdrop(nullptr)
333 , fSaveLayerFlags(saveLayerFlags)
334 {}
335 SaveLayerRec(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
336 SaveLayerFlags saveLayerFlags)
337 : fBounds(bounds)
338 , fPaint(paint)
339 , fBackdrop(backdrop)
reed4960eee2015-12-18 07:09:18 -0800340 , fSaveLayerFlags(saveLayerFlags)
341 {}
342
reedbfd5f172016-01-07 11:28:08 -0800343 const SkRect* fBounds; // optional
344 const SkPaint* fPaint; // optional
345 const SkImageFilter* fBackdrop; // optional
346 SaveLayerFlags fSaveLayerFlags;
reed4960eee2015-12-18 07:09:18 -0800347 };
348
349 int saveLayer(const SaveLayerRec&);
350
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 /** This call balances a previous call to save(), and is used to remove all
reed@android.comdc3381f2010-02-11 16:05:15 +0000352 modifications to the matrix/clip/drawFilter state since the last save
353 call.
354 It is an error to call restore() more times than save() was called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 */
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000356 void restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357
358 /** Returns the number of matrix/clip states on the SkCanvas' private stack.
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000359 This will equal # save() calls - # restore() calls + 1. The save count on
360 a new canvas is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 */
junov@chromium.orga907ac32012-02-24 21:54:07 +0000362 int getSaveCount() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363
364 /** Efficient way to pop any calls to save() that happened after the save
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000365 count reached saveCount. It is an error for saveCount to be greater than
366 getSaveCount(). To pop all the way back to the initial matrix/clip context
367 pass saveCount == 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000368 @param saveCount The number of save() levels to restore from
369 */
370 void restoreToCount(int saveCount);
371
372 /** Preconcat the current matrix with the specified translation
373 @param dx The distance to translate in X
374 @param dy The distance to translate in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000376 void translate(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377
378 /** Preconcat the current matrix with the specified scale.
379 @param sx The amount to scale in X
380 @param sy The amount to scale in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000381 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000382 void scale(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383
bungeman7438bfc2016-07-12 15:01:19 -0700384 /** Preconcat the current matrix with the specified rotation about the origin.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 @param degrees The amount to rotate, in degrees
reed@android.com8a1c16f2008-12-17 15:59:43 +0000386 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000387 void rotate(SkScalar degrees);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388
bungeman7438bfc2016-07-12 15:01:19 -0700389 /** Preconcat the current matrix with the specified rotation about a given point.
390 @param degrees The amount to rotate, in degrees
391 @param px The x coordinate of the point to rotate about.
392 @param py The y coordinate of the point to rotate about.
393 */
394 void rotate(SkScalar degrees, SkScalar px, SkScalar py);
395
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 /** Preconcat the current matrix with the specified skew.
397 @param sx The amount to skew in X
398 @param sy The amount to skew in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000399 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000400 void skew(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401
402 /** Preconcat the current matrix with the specified matrix.
403 @param matrix The matrix to preconcatenate with the current matrix
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000405 void concat(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000406
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 /** Replace the current matrix with a copy of the specified matrix.
408 @param matrix The matrix that will be copied into the current matrix.
409 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000410 void setMatrix(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000411
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 /** Helper for setMatrix(identity). Sets the current matrix to identity.
413 */
414 void resetMatrix();
415
vjiaoblack95302da2016-07-21 10:25:54 -0700416#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -0700417 /** Add the specified translation to the current draw depth of the canvas.
418 @param z The distance to translate in Z.
419 Negative into screen, positive out of screen.
420 Without translation, the draw depth defaults to 0.
421 */
422 void translateZ(SkScalar z);
423
vjiaoblack95302da2016-07-21 10:25:54 -0700424 /** Set the current set of lights in the canvas.
425 @param lights The lights that we want the canvas to have.
426 */
427 void setLights(sk_sp<SkLights> lights);
428
429 /** Returns the current set of lights the canvas uses
430 */
431 sk_sp<SkLights> getLights() const;
432#endif
433
reed@google.com4ed0fb72012-12-12 20:48:18 +0000434 /**
435 * Modify the current clip with the specified rectangle.
436 * @param rect The rect to combine with the current clip
437 * @param op The region op to apply to the current clip
438 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000439 */
Mike Reedc1f77742016-12-09 09:00:50 -0500440 void clipRect(const SkRect& rect, SkClipOp, bool doAntiAlias);
441 void clipRect(const SkRect& rect, SkClipOp op) {
reed66998382016-09-21 11:15:07 -0700442 this->clipRect(rect, op, false);
443 }
444 void clipRect(const SkRect& rect, bool doAntiAlias = false) {
Mike Reed2dc52372016-12-14 14:00:03 -0500445 this->clipRect(rect, SkClipOp::kIntersect, doAntiAlias);
reed66998382016-09-21 11:15:07 -0700446 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447
reed@google.com4ed0fb72012-12-12 20:48:18 +0000448 /**
Stan Iliev5f1bb0a2016-12-12 17:39:55 -0500449 * Sets the max clip rectangle, which can be set by clipRect, clipRRect and
450 * clipPath and intersect the current clip with the specified rect.
451 * The max clip affects only future ops (it is not retroactive).
452 * We DON'T record the clip restriction in pictures.
453 * This is private API to be used only by Android framework.
454 * @param rect The maximum allowed clip in device coordinates.
455 * Empty rect means max clip is not enforced.
456 */
457 void androidFramework_setDeviceClipRestriction(const SkIRect& rect);
458
459 /**
reed@google.com4ed0fb72012-12-12 20:48:18 +0000460 * Modify the current clip with the specified SkRRect.
461 * @param rrect The rrect to combine with the current clip
462 * @param op The region op to apply to the current clip
463 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000464 */
Mike Reedc1f77742016-12-09 09:00:50 -0500465 void clipRRect(const SkRRect& rrect, SkClipOp op, bool doAntiAlias);
466 void clipRRect(const SkRRect& rrect, SkClipOp op) {
reed66998382016-09-21 11:15:07 -0700467 this->clipRRect(rrect, op, false);
468 }
469 void clipRRect(const SkRRect& rrect, bool doAntiAlias = false) {
Mike Reed2dc52372016-12-14 14:00:03 -0500470 this->clipRRect(rrect, SkClipOp::kIntersect, doAntiAlias);
reed66998382016-09-21 11:15:07 -0700471 }
reed@google.com4ed0fb72012-12-12 20:48:18 +0000472
473 /**
474 * Modify the current clip with the specified path.
475 * @param path The path to combine with the current clip
476 * @param op The region op to apply to the current clip
477 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000478 */
Mike Reedc1f77742016-12-09 09:00:50 -0500479 void clipPath(const SkPath& path, SkClipOp op, bool doAntiAlias);
480 void clipPath(const SkPath& path, SkClipOp op) {
reed66998382016-09-21 11:15:07 -0700481 this->clipPath(path, op, false);
482 }
483 void clipPath(const SkPath& path, bool doAntiAlias = false) {
Mike Reed2dc52372016-12-14 14:00:03 -0500484 this->clipPath(path, SkClipOp::kIntersect, doAntiAlias);
reed66998382016-09-21 11:15:07 -0700485 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486
caryclark@google.com8f0a7b82012-11-07 14:54:49 +0000487 /** EXPERIMENTAL -- only used for testing
caryclark@google.com45a75fb2013-04-25 13:34:40 +0000488 Set to simplify clip stack using path ops.
489 */
490 void setAllowSimplifyClip(bool allow) {
491 fAllowSimplifyClip = allow;
492 }
493
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494 /** Modify the current clip with the specified region. Note that unlike
495 clipRect() and clipPath() which transform their arguments by the current
496 matrix, clipRegion() assumes its argument is already in device
497 coordinates, and so no transformation is performed.
498 @param deviceRgn The region to apply to the current clip
mtklein6e998e62016-07-13 09:03:48 -0700499 @param op The region op to apply to the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000500 */
Mike Reed2dc52372016-12-14 14:00:03 -0500501 void clipRegion(const SkRegion& deviceRgn, SkClipOp op = SkClipOp::kIntersect);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000502
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503 /** Return true if the specified rectangle, after being transformed by the
504 current matrix, would lie completely outside of the current clip. Call
505 this to check if an area you intend to draw into is clipped out (and
506 therefore you can skip making the draw calls).
507 @param rect the rect to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508 @return true if the rect (transformed by the canvas' matrix) does not
509 intersect with the canvas' clip
510 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000511 bool quickReject(const SkRect& rect) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000512
513 /** Return true if the specified path, after being transformed by the
514 current matrix, would lie completely outside of the current clip. Call
515 this to check if an area you intend to draw into is clipped out (and
516 therefore you can skip making the draw calls). Note, for speed it may
517 return false even if the path itself might not intersect the clip
518 (i.e. the bounds of the path intersects, but the path does not).
519 @param path The path to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 @return true if the path (transformed by the canvas' matrix) does not
521 intersect with the canvas' clip
522 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000523 bool quickReject(const SkPath& path) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524
Mike Reed918e1442017-01-23 11:39:45 -0500525 /**
526 * Return the bounds of the current clip in local coordinates. If the clip is empty,
527 * return { 0, 0, 0, 0 }.
528 */
Mike Reed42e8c532017-01-23 14:09:13 -0500529 SkRect getLocalClipBounds() const { return this->onGetLocalClipBounds(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000530
Mike Reed918e1442017-01-23 11:39:45 -0500531 /**
Mike Reed6f4a9b22017-01-24 09:13:40 -0500532 * Returns true if the clip bounds are non-empty.
533 */
534 bool getLocalClipBounds(SkRect* bounds) const {
535 *bounds = this->onGetLocalClipBounds();
536 return !bounds->isEmpty();
537 }
538
539 /**
Mike Reed918e1442017-01-23 11:39:45 -0500540 * Return the bounds of the current clip in device coordinates. If the clip is empty,
541 * return { 0, 0, 0, 0 }.
542 */
Mike Reed42e8c532017-01-23 14:09:13 -0500543 SkIRect getDeviceClipBounds() const { return this->onGetDeviceClipBounds(); }
544
Mike Reed6f4a9b22017-01-24 09:13:40 -0500545 /**
546 * Returns true if the clip bounds are non-empty.
547 */
548 bool getDeviceClipBounds(SkIRect* bounds) const {
549 *bounds = this->onGetDeviceClipBounds();
550 return !bounds->isEmpty();
551 }
552
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000554 specified color and mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 @param color the color to draw with
556 @param mode the mode to apply the color in (defaults to SrcOver)
557 */
reed374772b2016-10-05 17:33:02 -0700558 void drawColor(SkColor color, SkBlendMode mode = SkBlendMode::kSrcOver);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559
reedf4e0d9e2014-12-05 20:49:03 -0800560 /**
561 * Helper method for drawing a color in SRC mode, completely replacing all the pixels
562 * in the current clip with this color.
563 */
564 void clear(SkColor color) {
reed374772b2016-10-05 17:33:02 -0700565 this->drawColor(color, SkBlendMode::kSrc);
reed8eddfb52014-12-04 07:50:14 -0800566 }
reed@google.com2a981812011-04-14 18:59:28 +0000567
568 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000569 * This makes the contents of the canvas undefined. Subsequent calls that
570 * require reading the canvas contents will produce undefined results. Examples
571 * include blending and readPixels. The actual implementation is backend-
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800572 * dependent and one legal implementation is to do nothing. This method
573 * ignores the current clip.
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000574 *
575 * This function should only be called if the caller intends to subsequently
576 * draw to the canvas. The canvas may do real work at discard() time in order
577 * to optimize performance on subsequent draws. Thus, if you call this and then
578 * never draw to the canvas subsequently you may pay a perfomance penalty.
579 */
580 void discard() { this->onDiscard(); }
581
582 /**
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800583 * Fill the entire canvas (restricted to the current clip) with the
reed@google.com2a981812011-04-14 18:59:28 +0000584 * specified paint.
585 * @param paint The paint used to fill the canvas
586 */
reed0846f1b2015-01-09 14:17:40 -0800587 void drawPaint(const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588
589 enum PointMode {
590 /** drawPoints draws each point separately */
591 kPoints_PointMode,
592 /** drawPoints draws each pair of points as a line segment */
593 kLines_PointMode,
594 /** drawPoints draws the array of points as a polygon */
595 kPolygon_PointMode
596 };
597
598 /** Draw a series of points, interpreted based on the PointMode mode. For
599 all modes, the count parameter is interpreted as the total number of
600 points. For kLine mode, count/2 line segments are drawn.
601 For kPoint mode, each point is drawn centered at its coordinate, and its
602 size is specified by the paint's stroke-width. It draws as a square,
603 unless the paint's cap-type is round, in which the points are drawn as
604 circles.
605 For kLine mode, each pair of points is drawn as a line segment,
606 respecting the paint's settings for cap/join/width.
607 For kPolygon mode, the entire array is drawn as a series of connected
608 line segments.
609 Note that, while similar, kLine and kPolygon modes draw slightly
610 differently than the equivalent path built with a series of moveto,
611 lineto calls, in that the path will draw all of its contours at once,
612 with no interactions if contours intersect each other (think XOR
613 xfermode). drawPoints always draws each element one at a time.
614 @param mode PointMode specifying how to draw the array of points.
615 @param count The number of points in the array
616 @param pts Array of points to draw
617 @param paint The paint used to draw the points
618 */
reed0846f1b2015-01-09 14:17:40 -0800619 void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000620
Mike Reed3661bc92017-02-22 13:21:42 -0500621 /** Helper method for drawing a single point. See drawPoints() for more details.
622 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000623 void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000624
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 /** Draw a line segment with the specified start and stop x,y coordinates,
626 using the specified paint. NOTE: since a line is always "framed", the
627 paint's Style is ignored.
628 @param x0 The x-coordinate of the start point of the line
629 @param y0 The y-coordinate of the start point of the line
630 @param x1 The x-coordinate of the end point of the line
631 @param y1 The y-coordinate of the end point of the line
632 @param paint The paint used to draw the line
633 */
Mike Reed3661bc92017-02-22 13:21:42 -0500634 void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000635
636 /** Draw the specified rectangle using the specified paint. The rectangle
637 will be filled or stroked based on the Style in the paint.
638 @param rect The rect to be drawn
639 @param paint The paint used to draw the rect
640 */
reed0846f1b2015-01-09 14:17:40 -0800641 void drawRect(const SkRect& rect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642
643 /** Draw the specified rectangle using the specified paint. The rectangle
644 will be filled or framed based on the Style in the paint.
645 @param rect The rect to be drawn
646 @param paint The paint used to draw the rect
647 */
reed@google.com87001ed2014-02-17 16:28:05 +0000648 void drawIRect(const SkIRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649 SkRect r;
650 r.set(rect); // promotes the ints to scalars
651 this->drawRect(r, paint);
652 }
reed@google.com4b226022011-01-11 18:32:13 +0000653
msarett44df6512016-08-25 13:54:30 -0700654 /** Draw the outline of the specified region using the specified paint.
655 @param region The region to be drawn
656 @param paint The paint used to draw the region
657 */
msarettdca352e2016-08-26 06:37:45 -0700658 void drawRegion(const SkRegion& region, const SkPaint& paint);
msarett44df6512016-08-25 13:54:30 -0700659
reed@android.com8a1c16f2008-12-17 15:59:43 +0000660 /** Draw the specified oval using the specified paint. The oval will be
661 filled or framed based on the Style in the paint.
662 @param oval The rectangle bounds of the oval to be drawn
663 @param paint The paint used to draw the oval
664 */
reed0846f1b2015-01-09 14:17:40 -0800665 void drawOval(const SkRect& oval, const SkPaint&);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000666
667 /**
668 * Draw the specified RRect using the specified paint The rrect will be filled or stroked
669 * based on the Style in the paint.
670 *
671 * @param rrect The round-rect to draw
672 * @param paint The paint used to draw the round-rect
673 */
reed0846f1b2015-01-09 14:17:40 -0800674 void drawRRect(const SkRRect& rrect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000676 /**
677 * Draw the annulus formed by the outer and inner rrects. The results
678 * are undefined if the outer does not contain the inner.
679 */
680 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 /** Draw the specified circle using the specified paint. If radius is <= 0,
683 then nothing will be drawn. The circle will be filled
684 or framed based on the Style in the paint.
685 @param cx The x-coordinate of the center of the cirle to be drawn
686 @param cy The y-coordinate of the center of the cirle to be drawn
687 @param radius The radius of the cirle to be drawn
688 @param paint The paint used to draw the circle
689 */
Mike Reed3661bc92017-02-22 13:21:42 -0500690 void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000691
692 /** Draw the specified arc, which will be scaled to fit inside the
bsalomon21af9ca2016-08-25 12:29:23 -0700693 specified oval. Sweep angles are not treated as modulo 360 and thus can
694 exceed a full sweep of the oval. Note that this differs slightly from
695 SkPath::arcTo, which treats the sweep angle mod 360. If the oval is empty
696 or the sweep angle is zero nothing is drawn. If useCenter is true the oval
697 center is inserted into the implied path before the arc and the path is
698 closed back to the, center forming a wedge. Otherwise, the implied path
699 contains just the arc and is not closed.
bsalomonac3aa242016-08-19 11:25:19 -0700700 @param oval The bounds of oval used to define the shape of the arc.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701 @param startAngle Starting angle (in degrees) where the arc begins
bsalomonac3aa242016-08-19 11:25:19 -0700702 @param sweepAngle Sweep angle (in degrees) measured clockwise.
bsalomon21af9ca2016-08-25 12:29:23 -0700703 @param useCenter true means include the center of the oval.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000704 @param paint The paint used to draw the arc
705 */
706 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
707 bool useCenter, const SkPaint& paint);
708
709 /** Draw the specified round-rect using the specified paint. The round-rect
710 will be filled or framed based on the Style in the paint.
711 @param rect The rectangular bounds of the roundRect to be drawn
712 @param rx The x-radius of the oval used to round the corners
713 @param ry The y-radius of the oval used to round the corners
714 @param paint The paint used to draw the roundRect
715 */
Mike Reed3661bc92017-02-22 13:21:42 -0500716 void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000717
718 /** Draw the specified path using the specified paint. The path will be
719 filled or framed based on the Style in the paint.
720 @param path The path to be drawn
721 @param paint The paint used to draw the path
722 */
reed0846f1b2015-01-09 14:17:40 -0800723 void drawPath(const SkPath& path, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000724
piotaixrd52893c2014-09-25 14:39:40 -0700725 /** Draw the specified image, with its top/left corner at (x,y), using the
726 specified paint, transformed by the current matrix.
727
728 @param image The image to be drawn
729 @param left The position of the left side of the image being drawn
730 @param top The position of the top side of the image being drawn
731 @param paint The paint used to draw the image, or NULL
732 */
reed0846f1b2015-01-09 14:17:40 -0800733 void drawImage(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint = NULL);
reedf8053da2016-03-17 08:14:57 -0700734 void drawImage(const sk_sp<SkImage>& image, SkScalar left, SkScalar top,
735 const SkPaint* paint = NULL) {
736 this->drawImage(image.get(), left, top, paint);
737 }
piotaixrb5fae932014-09-24 13:03:30 -0700738
reeda5517e22015-07-14 10:54:12 -0700739 /**
740 * Controls the behavior at the edge of the src-rect, when specified in drawImageRect,
741 * trading off speed for exactness.
742 *
743 * When filtering is enabled (in the Paint), skia may need to sample in a neighborhood around
744 * the pixels in the image. If there is a src-rect specified, it is intended to restrict the
745 * pixels that will be read. However, for performance reasons, some implementations may slow
746 * down if they cannot read 1-pixel past the src-rect boundary at times.
747 *
748 * This enum allows the caller to specify if such a 1-pixel "slop" will be visually acceptable.
749 * If it is, the caller should pass kFast, and it may result in a faster draw. If the src-rect
750 * must be strictly respected, the caller should pass kStrict.
751 */
752 enum SrcRectConstraint {
753 /**
754 * If kStrict is specified, the implementation must respect the src-rect
755 * (if specified) strictly, and will never sample outside of those bounds during sampling
756 * even when filtering. This may be slower than kFast.
757 */
758 kStrict_SrcRectConstraint,
759
760 /**
761 * If kFast is specified, the implementation may sample outside of the src-rect
bsalomon19e82e32015-10-23 09:27:42 -0700762 * (if specified) by half the width of filter. This allows greater flexibility
reeda5517e22015-07-14 10:54:12 -0700763 * to the implementation and can make the draw much faster.
764 */
765 kFast_SrcRectConstraint,
766 };
767
768 /** Draw the specified image, scaling and translating so that it fills the specified
769 * dst rect. If the src rect is non-null, only that subset of the image is transformed
770 * and drawn.
771 *
772 * @param image The image to be drawn
773 * @param src Optional: specify the subset of the image to be drawn
774 * @param dst The destination rectangle where the scaled/translated
775 * image will be drawn
776 * @param paint The paint used to draw the image, or NULL
777 * @param constraint Control the tradeoff between speed and exactness w.r.t. the src-rect.
778 */
reede47829b2015-08-06 10:02:53 -0700779 void drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst,
780 const SkPaint* paint,
781 SrcRectConstraint constraint = kStrict_SrcRectConstraint);
782 // variant that takes src SkIRect
783 void drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst,
784 const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
785 // variant that assumes src == image-bounds
reede47829b2015-08-06 10:02:53 -0700786 void drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint,
787 SrcRectConstraint = kStrict_SrcRectConstraint);
reed84984ef2015-07-17 07:09:43 -0700788
reedf8053da2016-03-17 08:14:57 -0700789 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& src, const SkRect& dst,
790 const SkPaint* paint,
791 SrcRectConstraint constraint = kStrict_SrcRectConstraint) {
792 this->drawImageRect(image.get(), src, dst, paint, constraint);
793 }
794 void drawImageRect(const sk_sp<SkImage>& image, const SkIRect& isrc, const SkRect& dst,
795 const SkPaint* paint, SrcRectConstraint cons = kStrict_SrcRectConstraint) {
796 this->drawImageRect(image.get(), isrc, dst, paint, cons);
797 }
798 void drawImageRect(const sk_sp<SkImage>& image, const SkRect& dst, const SkPaint* paint,
799 SrcRectConstraint cons = kStrict_SrcRectConstraint) {
800 this->drawImageRect(image.get(), dst, paint, cons);
801 }
802
reed4c21dc52015-06-25 12:32:03 -0700803 /**
804 * Draw the image stretched differentially to fit into dst.
805 * center is a rect within the image, and logically divides the image
806 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
807 * image is the "center", then the center-rect should be [2, 2, 3, 3].
808 *
809 * If the dst is >= the image size, then...
810 * - The 4 corners are not stretched at all.
811 * - The sides are stretched in only one axis.
812 * - The center is stretched in both axes.
813 * Else, for each axis where dst < image,
814 * - The corners shrink proportionally
815 * - The sides (along the shrink axis) and center are not drawn
816 */
817 void drawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
reedf8053da2016-03-17 08:14:57 -0700818 const SkPaint* paint = nullptr);
819 void drawImageNine(const sk_sp<SkImage>& image, const SkIRect& center, const SkRect& dst,
820 const SkPaint* paint = nullptr) {
821 this->drawImageNine(image.get(), center, dst, paint);
822 }
reed4c21dc52015-06-25 12:32:03 -0700823
reed@android.com8a1c16f2008-12-17 15:59:43 +0000824 /** Draw the specified bitmap, with its top/left corner at (x,y), using the
825 specified paint, transformed by the current matrix. Note: if the paint
826 contains a maskfilter that generates a mask which extends beyond the
827 bitmap's original width/height, then the bitmap will be drawn as if it
828 were in a Shader with CLAMP mode. Thus the color outside of the original
829 width/height will be the edge color replicated.
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000830
831 If a shader is present on the paint it will be ignored, except in the
reed@google.comf20fc242014-03-26 13:44:58 +0000832 case where the bitmap is kAlpha_8_SkColorType. In that case, the color is
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000833 generated by the shader.
834
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835 @param bitmap The bitmap to be drawn
836 @param left The position of the left side of the bitmap being drawn
837 @param top The position of the top side of the bitmap being drawn
838 @param paint The paint used to draw the bitmap, or NULL
839 */
reed0846f1b2015-01-09 14:17:40 -0800840 void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
841 const SkPaint* paint = NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000842
reeda5517e22015-07-14 10:54:12 -0700843 /** Draw the specified bitmap, scaling and translating so that it fills the specified
844 * dst rect. If the src rect is non-null, only that subset of the bitmap is transformed
845 * and drawn.
846 *
847 * @param bitmap The bitmap to be drawn
848 * @param src Optional: specify the subset of the bitmap to be drawn
849 * @param dst The destination rectangle where the scaled/translated
850 * bitmap will be drawn
851 * @param paint The paint used to draw the bitmap, or NULL
852 * @param constraint Control the tradeoff between speed and exactness w.r.t. the src-rect.
853 */
reede47829b2015-08-06 10:02:53 -0700854 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst,
reed84984ef2015-07-17 07:09:43 -0700855 const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700856 // variant where src is SkIRect
reed84984ef2015-07-17 07:09:43 -0700857 void drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst,
858 const SkPaint* paint, SrcRectConstraint = kStrict_SrcRectConstraint);
reede47829b2015-08-06 10:02:53 -0700859 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint,
bsalomon19e82e32015-10-23 09:27:42 -0700860 SrcRectConstraint = kStrict_SrcRectConstraint);
reed84984ef2015-07-17 07:09:43 -0700861
reed@google.comf0b5e112011-09-07 11:57:34 +0000862 /**
msarettc573a402016-08-02 08:05:56 -0700863 * Draw the bitmap stretched or shrunk differentially to fit into dst.
reed@google.comf0b5e112011-09-07 11:57:34 +0000864 * center is a rect within the bitmap, and logically divides the bitmap
865 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
866 * bitmap is the "center", then the center-rect should be [2, 2, 3, 3].
867 *
868 * If the dst is >= the bitmap size, then...
robertphillips@google.com9bf380c2013-07-25 12:10:42 +0000869 * - The 4 corners are not stretched at all.
870 * - The sides are stretched in only one axis.
871 * - The center is stretched in both axes.
reed@google.comf0b5e112011-09-07 11:57:34 +0000872 * Else, for each axis where dst < bitmap,
873 * - The corners shrink proportionally
874 * - The sides (along the shrink axis) and center are not drawn
875 */
reed0846f1b2015-01-09 14:17:40 -0800876 void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
877 const SkPaint* paint = NULL);
reed@google.comf0b5e112011-09-07 11:57:34 +0000878
msarettc573a402016-08-02 08:05:56 -0700879 /**
880 * Specifies coordinates to divide a bitmap into (xCount*yCount) rects.
msarett71df2d72016-09-30 12:41:42 -0700881 *
882 * If the lattice divs or bounds are invalid, the entire lattice
883 * struct will be ignored on the draw call.
msarettc573a402016-08-02 08:05:56 -0700884 */
885 struct Lattice {
msarett0764efe2016-09-02 11:24:30 -0700886 enum Flags : uint8_t {
887 // If set, indicates that we should not draw corresponding rect.
888 kTransparent_Flags = 1 << 0,
889 };
msarettc573a402016-08-02 08:05:56 -0700890
msarett0764efe2016-09-02 11:24:30 -0700891 // An array of x-coordinates that divide the bitmap vertically.
msarett71df2d72016-09-30 12:41:42 -0700892 // These must be unique, increasing, and in the set [fBounds.fLeft, fBounds.fRight).
msarett0764efe2016-09-02 11:24:30 -0700893 // Does not have ownership.
msarett71df2d72016-09-30 12:41:42 -0700894 const int* fXDivs;
msarettc573a402016-08-02 08:05:56 -0700895
896 // An array of y-coordinates that divide the bitmap horizontally.
msarett71df2d72016-09-30 12:41:42 -0700897 // These must be unique, increasing, and in the set [fBounds.fTop, fBounds.fBottom).
msarettc573a402016-08-02 08:05:56 -0700898 // Does not have ownership.
msarett71df2d72016-09-30 12:41:42 -0700899 const int* fYDivs;
msarett0764efe2016-09-02 11:24:30 -0700900
901 // If non-null, the length of this array must be equal to
902 // (fXCount + 1) * (fYCount + 1). Note that we allow the first rect
msarett71df2d72016-09-30 12:41:42 -0700903 // in each direction to be empty (ex: fXDivs[0] = fBounds.fLeft).
904 // In this case, the caller still must specify a flag (as a placeholder)
905 // for these empty rects.
msarett0764efe2016-09-02 11:24:30 -0700906 // The flags correspond to the rects in the lattice, first moving
907 // left to right and then top to bottom.
msarett71df2d72016-09-30 12:41:42 -0700908 const Flags* fFlags;
msarett0764efe2016-09-02 11:24:30 -0700909
910 // The number of fXDivs.
msarett71df2d72016-09-30 12:41:42 -0700911 int fXCount;
msarettc573a402016-08-02 08:05:56 -0700912
913 // The number of fYDivs.
msarett71df2d72016-09-30 12:41:42 -0700914 int fYCount;
915
916 // The bound to draw from. Must be contained by the src that is being drawn,
917 // non-empty, and non-inverted.
918 // If nullptr, the bounds are the entire src.
919 const SkIRect* fBounds;
msarettc573a402016-08-02 08:05:56 -0700920 };
921
922 /**
923 * Draw the bitmap stretched or shrunk differentially to fit into dst.
924 *
925 * Moving horizontally across the bitmap, alternating rects will be "scalable"
926 * (in the x-dimension) to fit into dst or must be left "fixed". The first rect
927 * is treated as "fixed", but it's possible to specify an empty first rect by
928 * making lattice.fXDivs[0] = 0.
929 *
930 * The scale factor for all "scalable" rects will be the same, and may be greater
931 * than or less than 1 (meaning we can stretch or shrink). If the number of
932 * "fixed" pixels is greater than the width of the dst, we will collapse all of
933 * the "scalable" regions and appropriately downscale the "fixed" regions.
934 *
935 * The same interpretation also applies to the y-dimension.
936 */
937 void drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst,
938 const SkPaint* paint = nullptr);
939 void drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst,
940 const SkPaint* paint = nullptr);
941
reed@android.com8a1c16f2008-12-17 15:59:43 +0000942 /** Draw the text, with origin at (x,y), using the specified paint.
943 The origin is interpreted based on the Align setting in the paint.
944 @param text The text to be drawn
945 @param byteLength The number of bytes to read from the text parameter
946 @param x The x-coordinate of the origin of the text being drawn
947 @param y The y-coordinate of the origin of the text being drawn
948 @param paint The paint used for the text (e.g. color, size, style)
949 */
reedf7430cc2014-12-21 11:38:35 -0800950 void drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
951 const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000952
Cary Clark2a475ea2017-04-28 15:35:12 -0400953 /** Draw null-terminated UTF-8 string, with origin at (x,y), using the specified paint.
954 The origin is interpreted based on the Align setting in the paint.
955 @param string The null-terminated string to be drawn
956 @param x The x-coordinate of the origin of the string being drawn
957 @param y The y-coordinate of the origin of the string being drawn
958 @param paint The paint used for the string (e.g. color, size, style)
959 */
960 void drawString(const char* string, SkScalar x, SkScalar y, const SkPaint& paint) {
961 if (!string) {
962 return;
963 }
964 this->drawText(string, strlen(string), x, y, paint);
965 }
966
967 /** Draw string, with origin at (x,y), using the specified paint.
968 The origin is interpreted based on the Align setting in the paint.
969 @param string The string to be drawn
970 @param x The x-coordinate of the origin of the string being drawn
971 @param y The y-coordinate of the origin of the string being drawn
972 @param paint The paint used for the string (e.g. color, size, style)
973 */
974 void drawString(const SkString& string, SkScalar x, SkScalar y, const SkPaint& paint);
975
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 /** Draw the text, with each character/glyph origin specified by the pos[]
reed@google.com4b226022011-01-11 18:32:13 +0000977 array. The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978 @param text The text to be drawn
979 @param byteLength The number of bytes to read from the text parameter
980 @param pos Array of positions, used to position each character
981 @param paint The paint used for the text (e.g. color, size, style)
982 */
reedf7430cc2014-12-21 11:38:35 -0800983 void drawPosText(const void* text, size_t byteLength, const SkPoint pos[],
984 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000985
reed@android.com8a1c16f2008-12-17 15:59:43 +0000986 /** Draw the text, with each character/glyph origin specified by the x
987 coordinate taken from the xpos[] array, and the y from the constY param.
reed@google.com4b226022011-01-11 18:32:13 +0000988 The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000989 @param text The text to be drawn
990 @param byteLength The number of bytes to read from the text parameter
991 @param xpos Array of x-positions, used to position each character
992 @param constY The shared Y coordinate for all of the positions
993 @param paint The paint used for the text (e.g. color, size, style)
994 */
reedf7430cc2014-12-21 11:38:35 -0800995 void drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], SkScalar constY,
996 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000997
reed@android.com8a1c16f2008-12-17 15:59:43 +0000998 /** Draw the text, with origin at (x,y), using the specified paint, along
999 the specified path. The paint's Align setting determins where along the
1000 path to start the text.
1001 @param text The text to be drawn
1002 @param byteLength The number of bytes to read from the text parameter
1003 @param path The path the text should follow for its baseline
1004 @param hOffset The distance along the path to add to the text's
1005 starting position
1006 @param vOffset The distance above(-) or below(+) the path to
1007 position the text
1008 @param paint The paint used for the text
1009 */
reedf7430cc2014-12-21 11:38:35 -08001010 void drawTextOnPathHV(const void* text, size_t byteLength, const SkPath& path, SkScalar hOffset,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001011 SkScalar vOffset, const SkPaint& paint);
1012
1013 /** Draw the text, with origin at (x,y), using the specified paint, along
1014 the specified path. The paint's Align setting determins where along the
1015 path to start the text.
1016 @param text The text to be drawn
1017 @param byteLength The number of bytes to read from the text parameter
1018 @param path The path the text should follow for its baseline
1019 @param matrix (may be null) Applied to the text before it is
1020 mapped onto the path
1021 @param paint The paint used for the text
1022 */
reedf7430cc2014-12-21 11:38:35 -08001023 void drawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
1024 const SkMatrix* matrix, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001025
reed45561a02016-07-07 12:47:17 -07001026 /**
1027 * Draw the text with each character/glyph individually transformed by its xform.
1028 * If cullRect is not null, it is a conservative bounds of what will be drawn
reed63f30d92016-07-07 16:32:51 -07001029 * taking into account the xforms and the paint, and will be used to accelerate culling.
reed45561a02016-07-07 12:47:17 -07001030 */
1031 void drawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
1032 const SkRect* cullRect, const SkPaint& paint);
1033
fmalita00d5c2c2014-08-21 08:53:26 -07001034 /** Draw the text blob, offset by (x,y), using the specified paint.
1035 @param blob The text blob to be drawn
1036 @param x The x-offset of the text being drawn
1037 @param y The y-offset of the text being drawn
1038 @param paint The paint used for the text (e.g. color, size, style)
1039 */
1040 void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
reed2ab90572016-08-10 14:16:41 -07001041 void drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaint& paint) {
1042 this->drawTextBlob(blob.get(), x, y, paint);
1043 }
fmalita00d5c2c2014-08-21 08:53:26 -07001044
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 /** Draw the picture into this canvas. This method effective brackets the
1046 playback of the picture's draw calls with save/restore, so the state
djsollen@google.coma44de962013-01-02 16:59:19 +00001047 of this canvas will be unchanged after this call.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001048 @param picture The recorded drawing commands to playback into this
1049 canvas.
1050 */
reed1c2c4412015-04-30 13:09:24 -07001051 void drawPicture(const SkPicture* picture) {
1052 this->drawPicture(picture, NULL, NULL);
1053 }
reedca2622b2016-03-18 07:25:55 -07001054 void drawPicture(const sk_sp<SkPicture>& picture) {
reedf8053da2016-03-17 08:14:57 -07001055 this->drawPicture(picture.get());
1056 }
robertphillips9b14f262014-06-04 05:40:44 -07001057
reedd5fa1a42014-08-09 11:08:05 -07001058 /**
1059 * Draw the picture into this canvas.
1060 *
1061 * If matrix is non-null, apply that matrix to the CTM when drawing this picture. This is
1062 * logically equivalent to
1063 * save/concat/drawPicture/restore
1064 *
1065 * If paint is non-null, draw the picture into a temporary buffer, and then apply the paint's
1066 * alpha/colorfilter/imagefilter/xfermode to that buffer as it is drawn to the canvas.
1067 * This is logically equivalent to
1068 * saveLayer(paint)/drawPicture/restore
1069 */
1070 void drawPicture(const SkPicture*, const SkMatrix* matrix, const SkPaint* paint);
reedca2622b2016-03-18 07:25:55 -07001071 void drawPicture(const sk_sp<SkPicture>& picture, const SkMatrix* matrix, const SkPaint* paint) {
reedf8053da2016-03-17 08:14:57 -07001072 this->drawPicture(picture.get(), matrix, paint);
1073 }
reedd5fa1a42014-08-09 11:08:05 -07001074
vjiaoblack95302da2016-07-21 10:25:54 -07001075#ifdef SK_EXPERIMENTAL_SHADOWING
1076 /**
vjiaoblacke6f5d562016-08-25 06:30:23 -07001077 * Draw the picture into this canvas, with shadows!
vjiaoblack95302da2016-07-21 10:25:54 -07001078 *
1079 * We will use the canvas's lights along with the picture information (draw depths of
1080 * objects, etc) to first create a set of shadowmaps for the light-picture pairs, and
1081 * then use that set of shadowmaps to render the scene with shadows.
1082 *
1083 * If matrix is non-null, apply that matrix to the CTM when drawing this picture. This is
1084 * logically equivalent to
1085 * save/concat/drawPicture/restore
1086 *
1087 * If paint is non-null, draw the picture into a temporary buffer, and then apply the paint's
1088 * alpha/colorfilter/imagefilter/xfermode to that buffer as it is drawn to the canvas.
1089 * This is logically equivalent to
1090 * saveLayer(paint)/drawPicture/restore
1091 *
vjiaoblacke6f5d562016-08-25 06:30:23 -07001092 * We also support using variance shadow maps for blurred shadows; the user can specify
1093 * what shadow mapping algorithm to use with params.
1094 * - Variance Shadow Mapping works by storing both the depth and depth^2 in the shadow map.
1095 * - Then, the shadow map can be blurred, and when reading from it, the fragment shader
1096 * can calculate the variance of the depth at a position by doing E(x^2) - E(x)^2.
1097 * - We can then use the depth variance and depth at a fragment to arrive at an upper bound
1098 * of the probability that the current surface is shadowed by using Chebyshev's
1099 * inequality, and then use that to shade the fragment.
1100 *
1101 * - There are a few problems with VSM.
1102 * * Light Bleeding | Areas with high variance, such as near the edges of high up rects,
1103 * will cause their shadow penumbras to overwrite otherwise solid
1104 * shadows.
1105 * * Shape Distortion | We can combat Light Bleeding by biasing the shadow (setting
1106 * mostly shaded fragments to completely shaded) and increasing
1107 * the minimum allowed variance. However, this warps and rounds
1108 * out the shape of the shadow.
vjiaoblack95302da2016-07-21 10:25:54 -07001109 */
1110 void drawShadowedPicture(const SkPicture*,
1111 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -07001112 const SkPaint* paint,
1113 const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -07001114 void drawShadowedPicture(const sk_sp<SkPicture>& picture,
1115 const SkMatrix* matrix,
vjiaoblacke6f5d562016-08-25 06:30:23 -07001116 const SkPaint* paint,
1117 const SkShadowParams& params) {
1118 this->drawShadowedPicture(picture.get(), matrix, paint, params);
vjiaoblack95302da2016-07-21 10:25:54 -07001119 }
1120#endif
1121
Brian Salomon199fb872017-02-06 09:41:10 -05001122 /** Draw vertices from an immutable SkVertices object.
1123
1124 @param vertices The mesh to draw.
1125 @param mode Used if both texs and colors are present and paint has a
1126 shader. In this case the colors are combined with the texture
1127 using mode, before being drawn using the paint.
1128 @param paint Specifies the shader/texture if present.
Brian Salomon199fb872017-02-06 09:41:10 -05001129 */
Mike Reede88a1cb2017-03-17 09:50:46 -04001130 void drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint);
1131 void drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, const SkPaint& paint);
Mike Reed7d954ad2016-10-28 15:42:34 -04001132
dandovb3c9d1c2014-08-12 08:34:29 -07001133 /**
1134 Draw a cubic coons patch
mtklein6cfa73a2014-08-13 13:33:49 -07001135
dandovb3c9d1c2014-08-12 08:34:29 -07001136 @param cubic specifies the 4 bounding cubic bezier curves of a patch with clockwise order
1137 starting at the top left corner.
1138 @param colors specifies the colors for the corners which will be bilerp across the patch,
1139 their order is clockwise starting at the top left corner.
mtklein6cfa73a2014-08-13 13:33:49 -07001140 @param texCoords specifies the texture coordinates that will be bilerp across the patch,
dandovb3c9d1c2014-08-12 08:34:29 -07001141 their order is the same as the colors.
Mike Reed7d954ad2016-10-28 15:42:34 -04001142 @param mode specifies how are the colors and the textures combined if both of them are
dandovb3c9d1c2014-08-12 08:34:29 -07001143 present.
dandov963137b2014-08-07 07:49:53 -07001144 @param paint Specifies the shader/texture if present.
1145 */
dandovb3c9d1c2014-08-12 08:34:29 -07001146 void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reed7d954ad2016-10-28 15:42:34 -04001147 const SkPoint texCoords[4], SkBlendMode mode, const SkPaint& paint);
1148 void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
1149 const SkPoint texCoords[4], const SkPaint& paint) {
1150 this->drawPatch(cubics, colors, texCoords, SkBlendMode::kModulate, paint);
1151 }
1152
reed3cb38402015-02-06 08:36:15 -08001153 /**
reed71c3c762015-06-24 10:29:17 -07001154 * Draw a set of sprites from the atlas. Each is specified by a tex rectangle in the
1155 * coordinate space of the atlas, and a corresponding xform which transforms the tex rectangle
1156 * into a quad.
1157 *
1158 * xform maps [0, 0, tex.width, tex.height] -> quad
1159 *
1160 * The color array is optional. When specified, each color modulates the pixels in its
Mike Reed7d954ad2016-10-28 15:42:34 -04001161 * corresponding quad (via the specified SkBlendMode).
reed71c3c762015-06-24 10:29:17 -07001162 *
1163 * The cullRect is optional. When specified, it must be a conservative bounds of all of the
1164 * resulting transformed quads, allowing the canvas to skip drawing if the cullRect does not
1165 * intersect the current clip.
1166 *
1167 * The paint is optional. If specified, its antialiasing, alpha, color-filter, image-filter
Mike Reed7d954ad2016-10-28 15:42:34 -04001168 * and blendmode are used to affect each of the quads.
reed71c3c762015-06-24 10:29:17 -07001169 */
1170 void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[],
Mike Reed7d954ad2016-10-28 15:42:34 -04001171 const SkColor colors[], int count, SkBlendMode, const SkRect* cullRect,
reed71c3c762015-06-24 10:29:17 -07001172 const SkPaint* paint);
Mike Reed7d954ad2016-10-28 15:42:34 -04001173 void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1174 const SkColor colors[], int count, SkBlendMode mode, const SkRect* cullRect,
1175 const SkPaint* paint) {
1176 this->drawAtlas(atlas.get(), xform, tex, colors, count, mode, cullRect, paint);
1177 }
reed71c3c762015-06-24 10:29:17 -07001178 void drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], int count,
1179 const SkRect* cullRect, const SkPaint* paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -04001180 this->drawAtlas(atlas, xform, tex, nullptr, count, SkBlendMode::kDst, cullRect, paint);
1181 }
1182 void drawAtlas(const sk_sp<SkImage>& atlas, const SkRSXform xform[], const SkRect tex[],
1183 int count, const SkRect* cullRect, const SkPaint* paint) {
1184 this->drawAtlas(atlas.get(), xform, tex, nullptr, count, SkBlendMode::kDst,
1185 cullRect, paint);
1186 }
1187
reed71c3c762015-06-24 10:29:17 -07001188 /**
reed3cb38402015-02-06 08:36:15 -08001189 * Draw the contents of this drawable into the canvas. If the canvas is async
1190 * (e.g. it is recording into a picture) then the drawable will be referenced instead,
1191 * to have its draw() method called when the picture is finalized.
1192 *
1193 * If the intent is to force the contents of the drawable into this canvas immediately,
1194 * then drawable->draw(canvas) may be called.
1195 */
reeda8db7282015-07-07 10:22:31 -07001196 void drawDrawable(SkDrawable* drawable, const SkMatrix* = NULL);
1197 void drawDrawable(SkDrawable*, SkScalar x, SkScalar y);
reed6a070dc2014-11-11 19:36:09 -08001198
reedf70b5312016-03-04 16:36:20 -08001199 /**
1200 * Send an "annotation" to the canvas. The annotation is a key/value pair, where the key is
1201 * a null-terminated utf8 string, and the value is a blob of data stored in an SkData
1202 * (which may be null). The annotation is associated with the specified rectangle.
1203 *
1204 * The caller still retains its ownership of the data (if any).
1205 *
1206 * Note: on may canvas types, this information is ignored, but some canvases (e.g. recording
1207 * a picture or drawing to a PDF document) will pass on this information.
1208 */
1209 void drawAnnotation(const SkRect&, const char key[], SkData* value);
mtklein0fba3b92016-04-01 04:55:51 -07001210 void drawAnnotation(const SkRect& rect, const char key[], const sk_sp<SkData>& value) {
1211 this->drawAnnotation(rect, key, value.get());
1212 }
reedf70b5312016-03-04 16:36:20 -08001213
reed@android.com8a1c16f2008-12-17 15:59:43 +00001214 //////////////////////////////////////////////////////////////////////////
fmalita77650002016-01-21 18:47:11 -08001215#ifdef SK_INTERNAL
fmalita53d9f1c2016-01-25 06:23:54 -08001216#ifndef SK_SUPPORT_LEGACY_DRAWFILTER
1217 #define SK_SUPPORT_LEGACY_DRAWFILTER
fmalita77650002016-01-21 18:47:11 -08001218#endif
1219#endif
reed@google.com4b226022011-01-11 18:32:13 +00001220
fmalita53d9f1c2016-01-25 06:23:54 -08001221#ifdef SK_SUPPORT_LEGACY_DRAWFILTER
reed@android.com8a1c16f2008-12-17 15:59:43 +00001222 /** Get the current filter object. The filter's reference count is not
reed@android.comdc3381f2010-02-11 16:05:15 +00001223 affected. The filter is saved/restored, just like the matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001224 @return the canvas' filter (or NULL).
1225 */
fmalita77650002016-01-21 18:47:11 -08001226 SK_ATTR_EXTERNALLY_DEPRECATED("getDrawFilter use is deprecated")
reed@android.com8a1c16f2008-12-17 15:59:43 +00001227 SkDrawFilter* getDrawFilter() const;
reed@google.com4b226022011-01-11 18:32:13 +00001228
reed@android.com8a1c16f2008-12-17 15:59:43 +00001229 /** Set the new filter (or NULL). Pass NULL to clear any existing filter.
1230 As a convenience, the parameter is returned. If an existing filter
1231 exists, its refcnt is decrement. If the new filter is not null, its
reed@android.comdc3381f2010-02-11 16:05:15 +00001232 refcnt is incremented. The filter is saved/restored, just like the
1233 matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001234 @param filter the new filter (or NULL)
1235 @return the new filter
1236 */
fmalita77650002016-01-21 18:47:11 -08001237 SK_ATTR_EXTERNALLY_DEPRECATED("setDrawFilter use is deprecated")
reed@android.com8a1c16f2008-12-17 15:59:43 +00001238 virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
fmalita77650002016-01-21 18:47:11 -08001239#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +00001240 //////////////////////////////////////////////////////////////////////////
1241
reed@google.com754de5f2014-02-24 19:38:20 +00001242 /**
1243 * Return true if the current clip is empty (i.e. nothing will draw).
1244 * Note: this is not always a free call, so it should not be used
1245 * more often than necessary. However, once the canvas has computed this
1246 * result, subsequent calls will be cheap (until the clip state changes,
1247 * which can happen on any clip..() or restore() call.
1248 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001249 virtual bool isClipEmpty() const;
reed@google.com754de5f2014-02-24 19:38:20 +00001250
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001251 /**
1252 * Returns true if the current clip is just a (non-empty) rectangle.
1253 * Returns false if the clip is empty, or if it is complex.
1254 */
1255 virtual bool isClipRect() const;
1256
reed@android.com8a1c16f2008-12-17 15:59:43 +00001257 /** Return the current matrix on the canvas.
1258 This does not account for the translate in any of the devices.
1259 @return The current matrix on the canvas.
1260 */
junov@chromium.orga907ac32012-02-24 21:54:07 +00001261 const SkMatrix& getTotalMatrix() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001262
reed@android.com8a1c16f2008-12-17 15:59:43 +00001263 ///////////////////////////////////////////////////////////////////////////
1264
robertphillips36736a22016-04-23 08:26:43 -07001265 // don't call
Brian Osman11052242016-10-27 14:47:55 -04001266 GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext();
robertphillips36736a22016-04-23 08:26:43 -07001267
1268 // don't call
1269 static void Internal_Private_SetIgnoreSaveLayerBounds(bool);
1270 static bool Internal_Private_GetIgnoreSaveLayerBounds();
1271 static void Internal_Private_SetTreatSpriteAsBitmap(bool);
1272 static bool Internal_Private_GetTreatSpriteAsBitmap();
1273
1274 // TEMP helpers until we switch virtual over to const& for src-rect
1275 void legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
1276 const SkPaint* paint,
1277 SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1278 void legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
1279 const SkPaint* paint,
1280 SrcRectConstraint constraint = kStrict_SrcRectConstraint);
1281
tomhudsoncb3bd182016-05-18 07:24:16 -07001282 // expose minimum amount of information necessary for transitional refactoring
1283 /**
1284 * Returns CTM and clip bounds, translated from canvas coordinates to top layer coordinates.
1285 */
1286 void temporary_internal_describeTopLayer(SkMatrix* matrix, SkIRect* clip_bounds);
1287
Mike Reeda1361362017-03-07 09:37:29 -05001288 /**
1289 * Returns the global clip as a region. If the clip contains AA, then only the bounds
1290 * of the clip may be returned.
1291 */
Mike Reed3726a4a2017-01-19 11:36:41 -05001292 void temporary_internal_getRgnClip(SkRegion*);
1293
robertphillipsda2cd8b2016-04-21 11:05:32 -07001294protected:
vjiaoblack95302da2016-07-21 10:25:54 -07001295#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -07001296 /** Returns the current (cumulative) draw depth of the canvas.
1297 */
1298 SkScalar getZ() const;
1299
vjiaoblack95302da2016-07-21 10:25:54 -07001300 sk_sp<SkLights> fLights;
1301#endif
1302
reed@google.com76f10a32014-02-05 15:32:21 +00001303 // default impl defers to getDevice()->newSurface(info)
reede8f30622016-03-23 18:59:25 -07001304 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&);
reed@google.com76f10a32014-02-05 15:32:21 +00001305
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001306 // default impl defers to its device
reed884e97c2015-05-26 11:31:54 -07001307 virtual bool onPeekPixels(SkPixmap*);
1308 virtual bool onAccessTopLayerPixels(SkPixmap*);
reedea5a6512016-07-07 16:44:27 -07001309 virtual SkImageInfo onImageInfo() const;
1310 virtual bool onGetProps(SkSurfaceProps*) const;
1311 virtual void onFlush();
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001312
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001313 // Subclass save/restore notifiers.
1314 // Overriders should call the corresponding INHERITED method up the inheritance chain.
reed4960eee2015-12-18 07:09:18 -08001315 // getSaveLayerStrategy()'s return value may suppress full layer allocation.
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001316 enum SaveLayerStrategy {
1317 kFullLayer_SaveLayerStrategy,
reed4960eee2015-12-18 07:09:18 -08001318 kNoLayer_SaveLayerStrategy,
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001319 };
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001320
fmalita6ca763f2014-06-17 13:52:18 -07001321 virtual void willSave() {}
reed4960eee2015-12-18 07:09:18 -08001322 // Overriders should call the corresponding INHERITED method up the inheritance chain.
1323 virtual SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) {
1324 return kFullLayer_SaveLayerStrategy;
1325 }
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001326 virtual void willRestore() {}
mtklein6cfa73a2014-08-13 13:33:49 -07001327 virtual void didRestore() {}
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001328 virtual void didConcat(const SkMatrix&) {}
1329 virtual void didSetMatrix(const SkMatrix&) {}
mtkleincbdf0072016-08-19 09:05:27 -07001330 virtual void didTranslate(SkScalar dx, SkScalar dy) {
1331 this->didConcat(SkMatrix::MakeTrans(dx, dy));
1332 }
vjiaoblack95302da2016-07-21 10:25:54 -07001333
1334#ifdef SK_EXPERIMENTAL_SHADOWING
vjiaoblacke5de1302016-07-13 14:05:28 -07001335 virtual void didTranslateZ(SkScalar) {}
vjiaoblack95302da2016-07-21 10:25:54 -07001336#endif
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +00001337
Mike Reed42e8c532017-01-23 14:09:13 -05001338 virtual SkRect onGetLocalClipBounds() const;
1339 virtual SkIRect onGetDeviceClipBounds() const;
1340
1341
reedf70b5312016-03-04 16:36:20 -08001342 virtual void onDrawAnnotation(const SkRect&, const char key[], SkData* value);
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +00001343 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
1344
reed@google.come0d9ce82014-04-23 04:00:17 +00001345 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1346 SkScalar y, const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001347
reed@google.come0d9ce82014-04-23 04:00:17 +00001348 virtual void onDrawPosText(const void* text, size_t byteLength,
1349 const SkPoint pos[], const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001350
reed@google.come0d9ce82014-04-23 04:00:17 +00001351 virtual void onDrawPosTextH(const void* text, size_t byteLength,
1352 const SkScalar xpos[], SkScalar constY,
1353 const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001354
reed@google.come0d9ce82014-04-23 04:00:17 +00001355 virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1356 const SkPath& path, const SkMatrix* matrix,
1357 const SkPaint& paint);
reed45561a02016-07-07 12:47:17 -07001358 virtual void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform[],
1359 const SkRect* cullRect, const SkPaint& paint);
mtklein6cfa73a2014-08-13 13:33:49 -07001360
fmalita00d5c2c2014-08-21 08:53:26 -07001361 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1362 const SkPaint& paint);
1363
dandovb3c9d1c2014-08-12 08:34:29 -07001364 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedfaba3712016-11-03 14:45:31 -04001365 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint);
reed@google.come0d9ce82014-04-23 04:00:17 +00001366
reeda8db7282015-07-07 10:22:31 -07001367 virtual void onDrawDrawable(SkDrawable*, const SkMatrix*);
reed6a070dc2014-11-11 19:36:09 -08001368
reed41af9662015-01-05 07:49:08 -08001369 virtual void onDrawPaint(const SkPaint&);
1370 virtual void onDrawRect(const SkRect&, const SkPaint&);
msarett44df6512016-08-25 13:54:30 -07001371 virtual void onDrawRegion(const SkRegion& region, const SkPaint& paint);
reed41af9662015-01-05 07:49:08 -08001372 virtual void onDrawOval(const SkRect&, const SkPaint&);
bsalomonac3aa242016-08-19 11:25:19 -07001373 virtual void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
1374 const SkPaint&);
reed41af9662015-01-05 07:49:08 -08001375 virtual void onDrawRRect(const SkRRect&, const SkPaint&);
1376 virtual void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&);
Mike Reede88a1cb2017-03-17 09:50:46 -04001377 virtual void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&);
reed71c3c762015-06-24 10:29:17 -07001378 virtual void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
Mike Reedfaba3712016-11-03 14:45:31 -04001379 int count, SkBlendMode, const SkRect* cull, const SkPaint*);
reed41af9662015-01-05 07:49:08 -08001380 virtual void onDrawPath(const SkPath&, const SkPaint&);
1381 virtual void onDrawImage(const SkImage*, SkScalar dx, SkScalar dy, const SkPaint*);
reed562fe472015-07-28 07:35:14 -07001382 virtual void onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkPaint*,
1383 SrcRectConstraint);
reed4c21dc52015-06-25 12:32:03 -07001384 virtual void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
1385 const SkPaint*);
msarett16882062016-08-16 09:31:08 -07001386 virtual void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
1387 const SkPaint*);
reed4c21dc52015-06-25 12:32:03 -07001388
reed41af9662015-01-05 07:49:08 -08001389 virtual void onDrawBitmap(const SkBitmap&, SkScalar dx, SkScalar dy, const SkPaint*);
1390 virtual void onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, const SkPaint*,
reed562fe472015-07-28 07:35:14 -07001391 SrcRectConstraint);
reed41af9662015-01-05 07:49:08 -08001392 virtual void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
1393 const SkPaint*);
msarett16882062016-08-16 09:31:08 -07001394 virtual void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
1395 const SkPaint*);
reed41af9662015-01-05 07:49:08 -08001396
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001397 enum ClipEdgeStyle {
1398 kHard_ClipEdgeStyle,
1399 kSoft_ClipEdgeStyle
1400 };
1401
Mike Reedc1f77742016-12-09 09:00:50 -05001402 virtual void onClipRect(const SkRect& rect, SkClipOp, ClipEdgeStyle);
1403 virtual void onClipRRect(const SkRRect& rrect, SkClipOp, ClipEdgeStyle);
1404 virtual void onClipPath(const SkPath& path, SkClipOp, ClipEdgeStyle);
1405 virtual void onClipRegion(const SkRegion& deviceRgn, SkClipOp);
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001406
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001407 virtual void onDiscard();
1408
reedd5fa1a42014-08-09 11:08:05 -07001409 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*);
robertphillips9b14f262014-06-04 05:40:44 -07001410
vjiaoblack95302da2016-07-21 10:25:54 -07001411#ifdef SK_EXPERIMENTAL_SHADOWING
1412 virtual void onDrawShadowedPicture(const SkPicture*,
1413 const SkMatrix*,
vjiaoblacke6f5d562016-08-25 06:30:23 -07001414 const SkPaint*,
1415 const SkShadowParams& params);
vjiaoblack95302da2016-07-21 10:25:54 -07001416#endif
Herb Derby13569782016-10-06 14:33:43 -04001417
junov@chromium.orga907ac32012-02-24 21:54:07 +00001418 // Clip rectangle bounds. Called internally by saveLayer.
1419 // returns false if the entire rectangle is entirely clipped out
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001420 // If non-NULL, The imageFilter parameter will be used to expand the clip
1421 // and offscreen bounds for any margin required by the filter DAG.
reed4960eee2015-12-18 07:09:18 -08001422 bool clipRectBounds(const SkRect* bounds, SaveLayerFlags, SkIRect* intersection,
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001423 const SkImageFilter* imageFilter = NULL);
junov@chromium.orga907ac32012-02-24 21:54:07 +00001424
reedc83a2972015-07-16 07:40:45 -07001425private:
reed3aafe112016-08-18 12:45:34 -07001426 /** After calling saveLayer(), there can be any number of devices that make
1427 up the top-most drawing area. LayerIter can be used to iterate through
1428 those devices. Note that the iterator is only valid until the next API
1429 call made on the canvas. Ownership of all pointers in the iterator stays
1430 with the canvas, so none of them should be modified or deleted.
1431 */
1432 class LayerIter /*: SkNoncopyable*/ {
1433 public:
1434 /** Initialize iterator with canvas, and set values for 1st device */
1435 LayerIter(SkCanvas*);
1436 ~LayerIter();
1437
1438 /** Return true if the iterator is done */
1439 bool done() const { return fDone; }
1440 /** Cycle to the next device */
1441 void next();
1442
1443 // These reflect the current device in the iterator
1444
1445 SkBaseDevice* device() const;
1446 const SkMatrix& matrix() const;
Mike Reeda1361362017-03-07 09:37:29 -05001447 void clip(SkRegion*) const;
reed3aafe112016-08-18 12:45:34 -07001448 const SkPaint& paint() const;
1449 int x() const;
1450 int y() const;
1451
1452 private:
1453 // used to embed the SkDrawIter object directly in our instance, w/o
1454 // having to expose that class def to the public. There is an assert
1455 // in our constructor to ensure that fStorage is large enough
1456 // (though needs to be a compile-time-assert!). We use intptr_t to work
1457 // safely with 32 and 64 bit machines (to ensure the storage is enough)
1458 intptr_t fStorage[32];
1459 class SkDrawIter* fImpl; // this points at fStorage
1460 SkPaint fDefaultPaint;
1461 bool fDone;
1462 };
Herb Derby13569782016-10-06 14:33:43 -04001463
reed4960eee2015-12-18 07:09:18 -08001464 static bool BoundsAffectsClip(SaveLayerFlags);
reedbada1882015-12-21 13:09:44 -08001465 static SaveLayerFlags LegacySaveFlagsToSaveLayerFlags(uint32_t legacySaveFlags);
reed4960eee2015-12-18 07:09:18 -08001466
reeda2217ef2016-07-20 06:04:34 -07001467 static void DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter,
Mike Reedc42a1cd2017-02-14 14:25:14 -05001468 SkBaseDevice* dst, const SkIPoint& dstOrigin,
Mike Reeda1361362017-03-07 09:37:29 -05001469 const SkMatrix& ctm);
reeda2217ef2016-07-20 06:04:34 -07001470
reedc83a2972015-07-16 07:40:45 -07001471 enum ShaderOverrideOpacity {
1472 kNone_ShaderOverrideOpacity, //!< there is no overriding shader (bitmap or image)
1473 kOpaque_ShaderOverrideOpacity, //!< the overriding shader is opaque
1474 kNotOpaque_ShaderOverrideOpacity, //!< the overriding shader may not be opaque
1475 };
1476
reed@google.com97af1a62012-08-28 12:19:02 +00001477 // notify our surface (if we have one) that we are about to draw, so it
1478 // can perform copy-on-write or invalidate any cached images
reedc83a2972015-07-16 07:40:45 -07001479 void predrawNotify(bool willOverwritesEntireSurface = false);
1480 void predrawNotify(const SkRect* rect, const SkPaint* paint, ShaderOverrideOpacity);
1481 void predrawNotify(const SkRect* rect, const SkPaint* paint, bool shaderOverrideIsOpaque) {
1482 this->predrawNotify(rect, paint, shaderOverrideIsOpaque ? kOpaque_ShaderOverrideOpacity
1483 : kNotOpaque_ShaderOverrideOpacity);
1484 }
reed@google.com97af1a62012-08-28 12:19:02 +00001485
Florin Malita0ed3b642017-01-13 16:56:38 +00001486 SkBaseDevice* getDevice() const;
1487 SkBaseDevice* getTopDevice() const;
1488
reed@android.com8a1c16f2008-12-17 15:59:43 +00001489 class MCRec;
1490
1491 SkDeque fMCStack;
1492 // points to top of stack
1493 MCRec* fMCRec;
1494 // the first N recs that can fit here mean we won't call malloc
reedb679ca82015-04-07 04:40:48 -07001495 enum {
reeda499f902015-05-01 09:34:31 -07001496 kMCRecSize = 128, // most recent measurement
reed31b80a92015-11-16 13:22:24 -08001497 kMCRecCount = 32, // common depth for save/restores
Stan Iliev5f1bb0a2016-12-12 17:39:55 -05001498 kDeviceCMSize = 184, // most recent measurement
reedb679ca82015-04-07 04:40:48 -07001499 };
1500 intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
reeda499f902015-05-01 09:34:31 -07001501 intptr_t fDeviceCMStorage[kDeviceCMSize / sizeof(intptr_t)];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001502
reed4a8126e2014-09-22 07:29:03 -07001503 const SkSurfaceProps fProps;
1504
reed2ff1fce2014-12-11 07:07:37 -08001505 int fSaveCount; // value returned by getSaveCount()
reed@android.com8a1c16f2008-12-17 15:59:43 +00001506
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +00001507 SkMetaData* fMetaData;
Mike Reed356f7c22017-01-10 11:58:39 -05001508 std::unique_ptr<SkRasterHandleAllocator> fAllocator;
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +00001509
reed@google.com97af1a62012-08-28 12:19:02 +00001510 SkSurface_Base* fSurfaceBase;
1511 SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1512 void setSurfaceBase(SkSurface_Base* sb) {
1513 fSurfaceBase = sb;
1514 }
1515 friend class SkSurface_Base;
junov@chromium.org45c3db82013-04-11 17:52:05 +00001516 friend class SkSurface_Gpu;
skia.committer@gmail.comfc843592012-10-11 02:01:14 +00001517
Stan Iliev5f1bb0a2016-12-12 17:39:55 -05001518 SkIRect fClipRestrictionRect = SkIRect::MakeEmpty();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001519
reed2ff1fce2014-12-11 07:07:37 -08001520 void doSave();
1521 void checkForDeferredSave();
reed8c30a812016-04-20 16:36:51 -07001522 void internalSetMatrix(const SkMatrix&);
reed2ff1fce2014-12-11 07:07:37 -08001523
reed@google.com9c135db2014-03-12 18:28:35 +00001524 friend class SkDrawIter; // needs setupDrawForLayerDevice()
reed@google.com8926b162012-03-23 15:36:36 +00001525 friend class AutoDrawLooper;
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +00001526 friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
reed52d9ac62014-06-30 09:05:34 -07001527 friend class SkSurface_Raster; // needs getDevice()
Florin Malita439ace92016-12-02 12:05:41 -05001528 friend class SkRecorder; // resetForNextPicture
1529 friend class SkLiteRecorder; // resetForNextPicture
1530 friend class SkNoDrawCanvas; // InitFlags
fmalita2d97bc12014-11-20 10:44:58 -08001531 friend class SkPictureImageFilter; // SkCanvas(SkBaseDevice*, SkSurfaceProps*, InitFlags)
reedc83a2972015-07-16 07:40:45 -07001532 friend class SkPictureRecord; // predrawNotify (why does it need it? <reed>)
reed4960eee2015-12-18 07:09:18 -08001533 friend class SkPicturePlayback; // SaveFlagsToSaveLayerFlags
Herb Derby13569782016-10-06 14:33:43 -04001534 friend class SkDeferredCanvas; // For use of resetForNextPicture
Matt Sarett22886c42016-11-22 11:31:41 -05001535 friend class SkOverdrawCanvas;
Mike Reed356f7c22017-01-10 11:58:39 -05001536 friend class SkRasterHandleAllocator;
piotaixrb5fae932014-09-24 13:03:30 -07001537
reedd9544982014-09-09 18:46:22 -07001538 enum InitFlags {
1539 kDefault_InitFlags = 0,
1540 kConservativeRasterClip_InitFlag = 1 << 0,
1541 };
reed78e27682014-11-19 08:04:34 -08001542 SkCanvas(const SkIRect& bounds, InitFlags);
robertphillipsfcf78292015-06-19 11:49:52 -07001543 SkCanvas(SkBaseDevice* device, InitFlags);
Mike Reed356f7c22017-01-10 11:58:39 -05001544 SkCanvas(const SkBitmap&, std::unique_ptr<SkRasterHandleAllocator>,
1545 SkRasterHandleAllocator::Handle);
reedd9544982014-09-09 18:46:22 -07001546
mtkleinfeaadee2015-04-08 11:25:48 -07001547 void resetForNextPicture(const SkIRect& bounds);
1548
reed8f2e7912014-09-04 12:45:18 -07001549 // needs gettotalclip()
tfarinaa5414c42014-10-10 06:19:09 -07001550 friend class SkCanvasStateUtils;
piotaixrb5fae932014-09-24 13:03:30 -07001551
reed4a8126e2014-09-22 07:29:03 -07001552 // call this each time we attach ourselves to a device
1553 // - constructor
1554 // - internalSaveLayer
1555 void setupDevice(SkBaseDevice*);
1556
reedd9544982014-09-09 18:46:22 -07001557 SkBaseDevice* init(SkBaseDevice*, InitFlags);
reed@google.comf0b5e112011-09-07 11:57:34 +00001558
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001559 /**
senorblancoafc7cce2016-02-02 18:44:15 -08001560 * 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 +00001561 * to be public because it exposes decisions about layer sizes that are internal to the canvas.
1562 */
senorblancoafc7cce2016-02-02 18:44:15 -08001563 SkIRect getTopLayerBounds() const;
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001564
reed@google.com71121732012-09-18 15:14:33 +00001565 void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001566 const SkRect& dst, const SkPaint* paint,
reeda5517e22015-07-14 10:54:12 -07001567 SrcRectConstraint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001568 void internalDrawPaint(const SkPaint& paint);
reed4960eee2015-12-18 07:09:18 -08001569 void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
reed7503d602016-07-15 14:23:29 -07001570 void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001571
reed@android.com8a1c16f2008-12-17 15:59:43 +00001572 // shared by save() and saveLayer()
reed2ff1fce2014-12-11 07:07:37 -08001573 void internalSave();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001574 void internalRestore();
reed@google.com4b226022011-01-11 18:32:13 +00001575
reedc83a2972015-07-16 07:40:45 -07001576 /*
1577 * Returns true if drawing the specified rect (or all if it is null) with the specified
1578 * paint (or default if null) would overwrite the entire root device of the canvas
1579 * (i.e. the canvas' surface if it had one).
1580 */
1581 bool wouldOverwriteEntireSurface(const SkRect*, const SkPaint*, ShaderOverrideOpacity) const;
1582
reed262a71b2015-12-05 13:07:27 -08001583 /**
1584 * Returns true if the paint's imagefilter can be invoked directly, without needed a layer.
1585 */
1586 bool canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint&);
reedc83a2972015-07-16 07:40:45 -07001587
Mike Reeda1361362017-03-07 09:37:29 -05001588 /**
1589 * Returns true if the clip (for any active layer) contains antialiasing.
1590 * If the clip is empty, this will return false.
Mike Reed46784be2017-01-15 20:02:32 -05001591 */
Mike Reeda1361362017-03-07 09:37:29 -05001592 bool androidFramework_isClipAA() const;
msarettfbfa2582016-08-12 08:29:08 -07001593
1594 /**
1595 * Keep track of the device clip bounds and if the matrix is scale-translate. This allows
1596 * us to do a fast quick reject in the common case.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001597 */
msarett9637ea92016-08-18 14:03:30 -07001598 bool fIsScaleTranslate;
msarettfbfa2582016-08-12 08:29:08 -07001599 SkRect fDeviceClipBounds;
1600
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001601 bool fAllowSoftClip;
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001602 bool fAllowSimplifyClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001603
reed@google.com5c3d1472011-02-22 19:12:23 +00001604 class AutoValidateClip : ::SkNoncopyable {
1605 public:
1606 explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1607 fCanvas->validateClip();
1608 }
1609 ~AutoValidateClip() { fCanvas->validateClip(); }
1610
1611 private:
1612 const SkCanvas* fCanvas;
1613 };
1614
1615#ifdef SK_DEBUG
1616 void validateClip() const;
1617#else
1618 void validateClip() const {}
1619#endif
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +00001620
1621 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001622};
1623
1624/** Stack helper class to automatically call restoreToCount() on the canvas
1625 when this object goes out of scope. Use this to guarantee that the canvas
1626 is restored to a known state.
1627*/
1628class SkAutoCanvasRestore : SkNoncopyable {
1629public:
commit-bot@chromium.org28871192013-10-14 15:28:01 +00001630 SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1631 if (fCanvas) {
1632 fSaveCount = canvas->getSaveCount();
1633 if (doSave) {
1634 canvas->save();
1635 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001636 }
1637 }
1638 ~SkAutoCanvasRestore() {
reed@google.comf6c9a5b2012-11-20 15:12:21 +00001639 if (fCanvas) {
1640 fCanvas->restoreToCount(fSaveCount);
1641 }
1642 }
1643
1644 /**
1645 * Perform the restore now, instead of waiting for the destructor. Will
1646 * only do this once.
1647 */
1648 void restore() {
1649 if (fCanvas) {
1650 fCanvas->restoreToCount(fSaveCount);
1651 fCanvas = NULL;
1652 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001653 }
1654
1655private:
1656 SkCanvas* fCanvas;
1657 int fSaveCount;
1658};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001659#define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001660
1661#endif