blob: 7a4ddd935f491f726b24b9cf2008c0fc30095508 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkCanvas_DEFINED
9#define SkCanvas_DEFINED
10
11#include "SkTypes.h"
12#include "SkBitmap.h"
13#include "SkDeque.h"
reed@google.com5c3d1472011-02-22 19:12:23 +000014#include "SkClipStack.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPaint.h"
16#include "SkRefCnt.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkPath.h"
18#include "SkRegion.h"
reed@android.com845fdac2009-06-23 03:01:32 +000019#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020
reed@google.come0d9ce82014-04-23 04:00:17 +000021#ifdef SK_SUPPORT_LEGACY_DRAWTEXT_VIRTUAL
22 #define SK_LEGACY_DRAWTEXT_VIRTUAL virtual
23#else
24 #define SK_LEGACY_DRAWTEXT_VIRTUAL
25#endif
26
fmalitac3b589a2014-06-05 12:40:07 -070027class SkCanvasClipVisitor;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000028class SkBaseDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029class SkDraw;
30class SkDrawFilter;
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +000031class SkMetaData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000032class SkPicture;
reed@google.com4ed0fb72012-12-12 20:48:18 +000033class SkRRect;
reed@google.com76f10a32014-02-05 15:32:21 +000034class SkSurface;
reed@google.com97af1a62012-08-28 12:19:02 +000035class SkSurface_Base;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000036class GrContext;
reed@google.com9c135db2014-03-12 18:28:35 +000037class GrRenderTarget;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038
39/** \class SkCanvas
40
41 A Canvas encapsulates all of the state about drawing into a device (bitmap).
42 This includes a reference to the device itself, and a stack of matrix/clip
43 values. For any given draw call (e.g. drawRect), the geometry of the object
44 being drawn is transformed by the concatenation of all the matrices in the
45 stack. The transformed geometry is clipped by the intersection of all of
46 the clips in the stack.
47
48 While the Canvas holds the state of the drawing device, the state (style)
49 of the object being drawn is held by the Paint, which is provided as a
50 parameter to each of the draw() methods. The Paint holds attributes such as
51 color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns),
52 etc.
53*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000054class SK_API SkCanvas : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000055public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000056 SK_DECLARE_INST_COUNT(SkCanvas)
57
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000058 /**
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +000059 * Attempt to allocate an offscreen raster canvas, matching the ImageInfo.
60 * On success, return a new canvas that will draw into that offscreen.
61 *
62 * The caller can access the pixels after drawing into this canvas by
63 * calling readPixels() or peekPixels().
64 *
65 * If the requested ImageInfo is opaque (either the colortype is
66 * intrinsically opaque like RGB_565, or the info's alphatype is kOpaque)
67 * then the pixel memory may be uninitialized. Otherwise, the pixel memory
68 * will be initialized to 0, which is interpreted as transparent.
69 *
70 * On failure, return NULL. This can fail for several reasons:
71 * 1. the memory allocation failed (e.g. request is too large)
72 * 2. invalid ImageInfo (e.g. negative dimensions)
73 * 3. unsupported ImageInfo for a canvas
74 * - kUnknown_SkColorType, kIndex_8_SkColorType
75 * - kIgnore_SkAlphaType
76 * - this list is not complete, so others may also be unsupported
77 *
78 * Note: it is valid to request a supported ImageInfo, but with zero
79 * dimensions.
80 */
81 static SkCanvas* NewRaster(const SkImageInfo&);
82
83 static SkCanvas* NewRasterN32(int width, int height) {
84 return NewRaster(SkImageInfo::MakeN32Premul(width, height));
85 }
86
87 /**
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000088 * Attempt to allocate raster canvas, matching the ImageInfo, that will draw directly into the
89 * specified pixels. To access the pixels after drawing to them, the caller should call
90 * flush() or call peekPixels(...).
91 *
92 * On failure, return NULL. This can fail for several reasons:
93 * 1. invalid ImageInfo (e.g. negative dimensions)
94 * 2. unsupported ImageInfo for a canvas
95 * - kUnknown_SkColorType, kIndex_8_SkColorType
96 * - kIgnore_SkAlphaType
97 * - this list is not complete, so others may also be unsupported
98 *
99 * Note: it is valid to request a supported ImageInfo, but with zero
100 * dimensions.
101 */
102 static SkCanvas* NewRasterDirect(const SkImageInfo&, void*, size_t);
103
104 static SkCanvas* NewRasterDirectN32(int width, int height, SkPMColor* pixels, size_t rowBytes) {
105 return NewRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
106 }
107
108 /**
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000109 * Creates an empty canvas with no backing device/pixels, and zero
110 * dimensions.
111 */
reed@google.comcde92112011-07-06 20:00:52 +0000112 SkCanvas();
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000113
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000114 /**
115 * Creates a canvas of the specified dimensions, but explicitly not backed
116 * by any device/pixels. Typically this use used by subclasses who handle
117 * the draw calls in some other way.
118 */
119 SkCanvas(int width, int height);
120
reed@google.com6dc74552011-07-21 18:00:46 +0000121 /** Construct a canvas with the specified device to draw into.
bsalomon@google.come97f0852011-06-17 13:10:25 +0000122
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000123 @param device Specifies a device for the canvas to draw into.
124 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000125 explicit SkCanvas(SkBaseDevice* device);
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000126
reed@google.com44699382013-10-31 17:28:30 +0000127 /** Construct a canvas with the specified bitmap to draw into.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 @param bitmap Specifies a bitmap for the canvas to draw into. Its
129 structure are copied to the canvas.
130 */
131 explicit SkCanvas(const SkBitmap& bitmap);
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
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 ///////////////////////////////////////////////////////////////////////////
143
reed@google.com210ce002011-11-01 14:24:23 +0000144 /**
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000145 * Trigger the immediate execution of all pending draw operations.
146 */
147 void flush();
148
149 /**
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000150 * Gets the size of the base or root layer in global canvas coordinates. The
151 * origin of the base layer is always (0,0). The current drawable area may be
152 * smaller (due to clipping or saveLayer).
reed@google.com210ce002011-11-01 14:24:23 +0000153 */
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000154 SkISize getBaseLayerSize() const;
155
156 /**
157 * DEPRECATED: call getBaseLayerSize
158 */
159 SkISize getDeviceSize() const { return this->getBaseLayerSize(); }
reed@google.com210ce002011-11-01 14:24:23 +0000160
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000161 /**
162 * DEPRECATED.
163 * Return the canvas' device object, which may be null. The device holds
164 * the bitmap of the pixels that the canvas draws into. The reference count
165 * of the returned device is not changed by this call.
166 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000167 SkBaseDevice* getDevice() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168
reed@google.com9266fed2011-03-30 00:18:03 +0000169 /**
170 * saveLayer() can create another device (which is later drawn onto
171 * the previous device). getTopDevice() returns the top-most device current
172 * installed. Note that this can change on other calls like save/restore,
173 * so do not access this device after subsequent canvas calls.
174 * The reference count of the device is not changed.
reed@google.com0b53d592012-03-19 18:26:34 +0000175 *
176 * @param updateMatrixClip If this is true, then before the device is
177 * returned, we ensure that its has been notified about the current
178 * matrix and clip. Note: this happens automatically when the device
179 * is drawn to, but is optional here, as there is a small perf hit
180 * sometimes.
reed@google.com9266fed2011-03-30 00:18:03 +0000181 */
reed@google.com9c135db2014-03-12 18:28:35 +0000182#ifndef SK_SUPPORT_LEGACY_GETTOPDEVICE
183private:
184#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000185 SkBaseDevice* getTopDevice(bool updateMatrixClip = false) const;
reed@google.com9c135db2014-03-12 18:28:35 +0000186public:
reed@google.com9266fed2011-03-30 00:18:03 +0000187
reed@google.com76f10a32014-02-05 15:32:21 +0000188 /**
189 * Create a new surface matching the specified info, one that attempts to
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000190 * be maximally compatible when used with this canvas. If there is no matching Surface type,
191 * NULL is returned.
reed@google.com76f10a32014-02-05 15:32:21 +0000192 */
193 SkSurface* newSurface(const SkImageInfo&);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000194
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000195 /**
196 * Return the GPU context of the device that is associated with the canvas.
197 * For a canvas with non-GPU device, NULL is returned.
198 */
199 GrContext* getGrContext();
200
reed@google.com4b226022011-01-11 18:32:13 +0000201 ///////////////////////////////////////////////////////////////////////////
202
bsalomon@google.comdaba14b2011-11-02 20:10:48 +0000203 /**
reed@google.com9c135db2014-03-12 18:28:35 +0000204 * If the canvas has writable pixels in its top layer (and is not recording to a picture
205 * or other non-raster target) and has direct access to its pixels (i.e. they are in
206 * local RAM) return the address of those pixels, and if not null,
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000207 * return the ImageInfo, rowBytes and origin. The returned address is only valid
reed@google.com9c135db2014-03-12 18:28:35 +0000208 * while the canvas object is in scope and unchanged. Any API calls made on
209 * canvas (or its parent surface if any) will invalidate the
210 * returned address (and associated information).
211 *
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000212 * On failure, returns NULL and the info, rowBytes, and origin parameters are ignored.
reed@google.com9c135db2014-03-12 18:28:35 +0000213 */
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000214 void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
reed@google.com9c135db2014-03-12 18:28:35 +0000215
216 /**
217 * If the canvas has readable pixels in its base layer (and is not recording to a picture
218 * or other non-raster target) and has direct access to its pixels (i.e. they are in
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000219 * local RAM) return the const-address of those pixels, and if not null,
220 * return the ImageInfo and rowBytes. The returned address is only valid
221 * while the canvas object is in scope and unchanged. Any API calls made on
222 * canvas (or its parent surface if any) will invalidate the
223 * returned address (and associated information).
224 *
225 * On failure, returns NULL and the info and rowBytes parameters are
226 * ignored.
227 */
228 const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
229
reed@google.com4b226022011-01-11 18:32:13 +0000230 /**
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000231 * Copy the pixels from the base-layer into the specified buffer (pixels + rowBytes),
232 * converting them into the requested format (SkImageInfo). The base-layer pixels are read
233 * starting at the specified (x,y) location in the coordinate system of the base-layer.
234 *
235 * The specified ImageInfo and (x,y) offset specifies a source rectangle
236 *
237 * srcR(x, y, info.width(), info.height());
238 *
239 * SrcR is intersected with the bounds of the base-layer. If this intersection is not empty,
240 * then we have two sets of pixels (of equal size), the "src" specified by base-layer at (x,y)
241 * and the "dst" by info+pixels+rowBytes. Replace the dst pixels with the corresponding src
242 * pixels, performing any colortype/alphatype transformations needed (in the case where the
243 * src and dst have different colortypes or alphatypes).
244 *
245 * This call can fail, returning false, for several reasons:
246 * - If the requested colortype/alphatype cannot be converted from the base-layer's types.
247 * - If this canvas is not backed by pixels (e.g. picture or PDF)
248 */
249 bool readPixels(const SkImageInfo&, void* pixels, size_t rowBytes, int x, int y);
250
251 /**
252 * Helper for calling readPixels(info, ...). This call will check if bitmap has been allocated.
253 * If not, it will attempt to call allocPixels(). If this fails, it will return false. If not,
254 * it calls through to readPixels(info, ...) and returns its result.
255 */
256 bool readPixels(SkBitmap* bitmap, int x, int y);
257
258 /**
259 * Helper for allocating pixels and then calling readPixels(info, ...). The bitmap is resized
260 * to the intersection of srcRect and the base-layer bounds. On success, pixels will be
261 * allocated in bitmap and true returned. On failure, false is returned and bitmap will be
262 * set to empty.
reed@google.com51df9e32010-12-23 19:29:18 +0000263 */
264 bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
reed@google.com51df9e32010-12-23 19:29:18 +0000265
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000266 /**
267 * This method affects the pixels in the base-layer, and operates in pixel coordinates,
268 * ignoring the matrix and clip.
269 *
270 * The specified ImageInfo and (x,y) offset specifies a rectangle: target.
271 *
272 * target.setXYWH(x, y, info.width(), info.height());
273 *
274 * Target is intersected with the bounds of the base-layer. If this intersection is not empty,
275 * then we have two sets of pixels (of equal size), the "src" specified by info+pixels+rowBytes
276 * and the "dst" by the canvas' backend. Replace the dst pixels with the corresponding src
277 * pixels, performing any colortype/alphatype transformations needed (in the case where the
278 * src and dst have different colortypes or alphatypes).
279 *
280 * This call can fail, returning false, for several reasons:
281 * - If the src colortype/alphatype cannot be converted to the canvas' types
282 * - If this canvas is not backed by pixels (e.g. picture or PDF)
283 */
284 bool writePixels(const SkImageInfo&, const void* pixels, size_t rowBytes, int x, int y);
285
286 /**
287 * Helper for calling writePixels(info, ...) by passing its pixels and rowbytes. If the bitmap
288 * is just wrapping a texture, returns false and does nothing.
289 */
290 bool writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com4b226022011-01-11 18:32:13 +0000291
reed@android.com8a1c16f2008-12-17 15:59:43 +0000292 ///////////////////////////////////////////////////////////////////////////
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000293
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294 enum SaveFlags {
295 /** save the matrix state, restoring it on restore() */
Florin Malita5f6102d2014-06-30 10:13:28 -0400296 // [deprecated] kMatrix_SaveFlag = 0x01,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297 /** save the clip state, restoring it on restore() */
Florin Malita5f6102d2014-06-30 10:13:28 -0400298 // [deprecated] kClip_SaveFlag = 0x02,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 /** the layer needs to support per-pixel alpha */
300 kHasAlphaLayer_SaveFlag = 0x04,
301 /** the layer needs to support 8-bits per color component */
302 kFullColorLayer_SaveFlag = 0x08,
reed@google.comb93ba452014-03-10 19:47:58 +0000303 /**
304 * the layer should clip against the bounds argument
305 *
306 * if SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG is undefined, this is treated as always on.
307 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308 kClipToLayer_SaveFlag = 0x10,
309
310 // helper masks for common choices
Florin Malita5f6102d2014-06-30 10:13:28 -0400311 // [deprecated] kMatrixClip_SaveFlag = 0x03,
reed@google.comb93ba452014-03-10 19:47:58 +0000312#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000313 kARGB_NoClipLayer_SaveFlag = 0x0F,
reed@google.comb93ba452014-03-10 19:47:58 +0000314#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000315 kARGB_ClipLayer_SaveFlag = 0x1F
316 };
317
reed@android.comdc3381f2010-02-11 16:05:15 +0000318 /** This call saves the current matrix, clip, and drawFilter, and pushes a
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 copy onto a private stack. Subsequent calls to translate, scale,
reed@android.comdc3381f2010-02-11 16:05:15 +0000320 rotate, skew, concat or clipRect, clipPath, and setDrawFilter all
321 operate on this copy.
322 When the balancing call to restore() is made, the previous matrix, clip,
323 and drawFilter are restored.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000324
325 @return The value to pass to restoreToCount() to balance this save()
326 */
327 int save();
328
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 /** This behaves the same as save(), but in addition it allocates an
330 offscreen bitmap. All drawing calls are directed there, and only when
331 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000332 the canvas (or the previous layer).
reed@android.comad164b22010-07-02 17:20:51 +0000333 @param bounds (may be null) This rect, if non-null, is used as a hint to
334 limit the size of the offscreen, and thus drawing may be
335 clipped to it, though that clipping is not guaranteed to
336 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 @param paint (may be null) This is copied, and is applied to the
338 offscreen when restore() is called
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000339 @return The value to pass to restoreToCount() to balance this save()
340 */
341 int saveLayer(const SkRect* bounds, const SkPaint* paint);
342
343 /** DEPRECATED - use saveLayer(const SkRect*, const SkPaint*) instead.
344
345 This behaves the same as saveLayer(const SkRect*, const SkPaint*),
346 but it allows fine-grained control of which state bits to be saved
347 (and subsequently restored).
348
349 @param bounds (may be null) This rect, if non-null, is used as a hint to
350 limit the size of the offscreen, and thus drawing may be
351 clipped to it, though that clipping is not guaranteed to
352 happen. If exact clipping is desired, use clipRect().
353 @param paint (may be null) This is copied, and is applied to the
354 offscreen when restore() is called
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 @param flags LayerFlags
356 @return The value to pass to restoreToCount() to balance this save()
357 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000358 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
359 int saveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360
361 /** This behaves the same as save(), but in addition it allocates an
362 offscreen bitmap. All drawing calls are directed there, and only when
363 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000364 the canvas (or the previous layer).
reed@android.com40408612010-07-02 17:24:23 +0000365 @param bounds (may be null) This rect, if non-null, is used as a hint to
366 limit the size of the offscreen, and thus drawing may be
367 clipped to it, though that clipping is not guaranteed to
368 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 @param alpha This is applied to the offscreen when restore() is called.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000370 @return The value to pass to restoreToCount() to balance this save()
371 */
372 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
373
374 /** DEPRECATED - use saveLayerAlpha(const SkRect*, U8CPU) instead.
375
376 This behaves the same as saveLayerAlpha(const SkRect*, U8CPU),
377 but it allows fine-grained control of which state bits to be saved
378 (and subsequently restored).
379
380 @param bounds (may be null) This rect, if non-null, is used as a hint to
381 limit the size of the offscreen, and thus drawing may be
382 clipped to it, though that clipping is not guaranteed to
383 happen. If exact clipping is desired, use clipRect().
384 @param alpha This is applied to the offscreen when restore() is called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 @param flags LayerFlags
386 @return The value to pass to restoreToCount() to balance this save()
387 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000388 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
389 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000390
391 /** This call balances a previous call to save(), and is used to remove all
reed@android.comdc3381f2010-02-11 16:05:15 +0000392 modifications to the matrix/clip/drawFilter state since the last save
393 call.
394 It is an error to call restore() more times than save() was called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000395 */
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000396 void restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000397
398 /** Returns the number of matrix/clip states on the SkCanvas' private stack.
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000399 This will equal # save() calls - # restore() calls + 1. The save count on
400 a new canvas is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 */
junov@chromium.orga907ac32012-02-24 21:54:07 +0000402 int getSaveCount() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000403
404 /** Efficient way to pop any calls to save() that happened after the save
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000405 count reached saveCount. It is an error for saveCount to be greater than
406 getSaveCount(). To pop all the way back to the initial matrix/clip context
407 pass saveCount == 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000408 @param saveCount The number of save() levels to restore from
409 */
410 void restoreToCount(int saveCount);
411
reed@google.com7c202932011-12-14 18:48:05 +0000412 /** Returns true if drawing is currently going to a layer (from saveLayer)
413 * rather than to the root device.
414 */
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000415 virtual bool isDrawingToLayer() const;
reed@google.com7c202932011-12-14 18:48:05 +0000416
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 /** Preconcat the current matrix with the specified translation
418 @param dx The distance to translate in X
419 @param dy The distance to translate in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000421 void translate(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422
423 /** Preconcat the current matrix with the specified scale.
424 @param sx The amount to scale in X
425 @param sy The amount to scale in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000427 void scale(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428
429 /** Preconcat the current matrix with the specified rotation.
430 @param degrees The amount to rotate, in degrees
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000432 void rotate(SkScalar degrees);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433
434 /** Preconcat the current matrix with the specified skew.
435 @param sx The amount to skew in X
436 @param sy The amount to skew in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000438 void skew(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000439
440 /** Preconcat the current matrix with the specified matrix.
441 @param matrix The matrix to preconcatenate with the current matrix
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000443 void concat(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000444
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 /** Replace the current matrix with a copy of the specified matrix.
446 @param matrix The matrix that will be copied into the current matrix.
447 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000448 void setMatrix(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000449
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 /** Helper for setMatrix(identity). Sets the current matrix to identity.
451 */
452 void resetMatrix();
453
reed@google.com4ed0fb72012-12-12 20:48:18 +0000454 /**
455 * Modify the current clip with the specified rectangle.
456 * @param rect The rect to combine with the current clip
457 * @param op The region op to apply to the current clip
458 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000459 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000460 void clipRect(const SkRect& rect,
461 SkRegion::Op op = SkRegion::kIntersect_Op,
462 bool doAntiAlias = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000463
reed@google.com4ed0fb72012-12-12 20:48:18 +0000464 /**
465 * Modify the current clip with the specified SkRRect.
466 * @param rrect The rrect to combine with the current clip
467 * @param op The region op to apply to the current clip
468 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000469 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000470 void clipRRect(const SkRRect& rrect,
471 SkRegion::Op op = SkRegion::kIntersect_Op,
472 bool doAntiAlias = false);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000473
474 /**
475 * Modify the current clip with the specified path.
476 * @param path The path to combine with the current clip
477 * @param op The region op to apply to the current clip
478 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000479 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000480 void clipPath(const SkPath& path,
481 SkRegion::Op op = SkRegion::kIntersect_Op,
482 bool doAntiAlias = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483
caryclark@google.com8f0a7b82012-11-07 14:54:49 +0000484 /** EXPERIMENTAL -- only used for testing
485 Set to false to force clips to be hard, even if doAntiAlias=true is
486 passed to clipRect or clipPath.
487 */
488 void setAllowSoftClip(bool allow) {
489 fAllowSoftClip = allow;
490 }
491
caryclark@google.com45a75fb2013-04-25 13:34:40 +0000492 /** EXPERIMENTAL -- only used for testing
493 Set to simplify clip stack using path ops.
494 */
495 void setAllowSimplifyClip(bool allow) {
496 fAllowSimplifyClip = allow;
497 }
498
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499 /** Modify the current clip with the specified region. Note that unlike
500 clipRect() and clipPath() which transform their arguments by the current
501 matrix, clipRegion() assumes its argument is already in device
502 coordinates, and so no transformation is performed.
503 @param deviceRgn The region to apply to the current clip
504 @param op The region op to apply to the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000505 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000506 void clipRegion(const SkRegion& deviceRgn,
507 SkRegion::Op op = SkRegion::kIntersect_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508
509 /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the
510 specified region. This does not intersect or in any other way account
511 for the existing clip region.
512 @param deviceRgn The region to copy into the current clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000514 void setClipRegion(const SkRegion& deviceRgn) {
515 this->clipRegion(deviceRgn, SkRegion::kReplace_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516 }
517
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 /** Return true if the specified rectangle, after being transformed by the
519 current matrix, would lie completely outside of the current clip. Call
520 this to check if an area you intend to draw into is clipped out (and
521 therefore you can skip making the draw calls).
522 @param rect the rect to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 @return true if the rect (transformed by the canvas' matrix) does not
524 intersect with the canvas' clip
525 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000526 bool quickReject(const SkRect& rect) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527
528 /** Return true if the specified path, after being transformed by the
529 current matrix, would lie completely outside of the current clip. Call
530 this to check if an area you intend to draw into is clipped out (and
531 therefore you can skip making the draw calls). Note, for speed it may
532 return false even if the path itself might not intersect the clip
533 (i.e. the bounds of the path intersects, but the path does not).
534 @param path The path to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535 @return true if the path (transformed by the canvas' matrix) does not
536 intersect with the canvas' clip
537 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000538 bool quickReject(const SkPath& path) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539
540 /** Return true if the horizontal band specified by top and bottom is
541 completely clipped out. This is a conservative calculation, meaning
542 that it is possible that if the method returns false, the band may still
543 in fact be clipped out, but the converse is not true. If this method
544 returns true, then the band is guaranteed to be clipped out.
545 @param top The top of the horizontal band to compare with the clip
546 @param bottom The bottom of the horizontal and to compare with the clip
547 @return true if the horizontal band is completely clipped out (i.e. does
548 not intersect the current clip)
549 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000550 bool quickRejectY(SkScalar top, SkScalar bottom) const {
reed@google.comc0784db2013-12-13 21:16:12 +0000551 SkASSERT(top <= bottom);
commit-bot@chromium.org9836bc32014-02-14 19:52:18 +0000552
553#ifndef SK_WILL_NEVER_DRAW_PERSPECTIVE_TEXT
554 // TODO: add a hasPerspective method similar to getLocalClipBounds. This
555 // would cache the SkMatrix::hasPerspective result. Alternatively, have
556 // the MC stack just set a hasPerspective boolean as it is updated.
557 if (this->getTotalMatrix().hasPerspective()) {
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +0000558 // TODO: consider implementing some half-plane test between the
commit-bot@chromium.org9836bc32014-02-14 19:52:18 +0000559 // two Y planes and the device-bounds (i.e., project the top and
560 // bottom Y planes and then determine if the clip bounds is completely
561 // outside either one).
562 return false;
563 }
564#endif
565
reed@google.comc0784db2013-12-13 21:16:12 +0000566 const SkRect& clipR = this->getLocalClipBounds();
djsollen@google.com92d2a292012-02-27 16:17:59 +0000567 // In the case where the clip is empty and we are provided with a
568 // negative top and positive bottom parameter then this test will return
569 // false even though it will be clipped. We have chosen to exclude that
570 // check as it is rare and would result double the comparisons.
reed@google.comc0784db2013-12-13 21:16:12 +0000571 return top >= clipR.fBottom || bottom <= clipR.fTop;
djsollen@google.com92d2a292012-02-27 16:17:59 +0000572 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573
574 /** Return the bounds of the current clip (in local coordinates) in the
575 bounds parameter, and return true if it is non-empty. This can be useful
576 in a way similar to quickReject, in that it tells you that drawing
577 outside of these bounds will be clipped out.
578 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000579 virtual bool getClipBounds(SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000580
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000581 /** Return the bounds of the current clip, in device coordinates; returns
582 true if non-empty. Maybe faster than getting the clip explicitly and
583 then taking its bounds.
584 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000585 virtual bool getClipDeviceBounds(SkIRect* bounds) const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000586
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000587
reed@android.com8a1c16f2008-12-17 15:59:43 +0000588 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000589 specified ARGB color, using the specified mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000590 @param a the alpha component (0..255) of the color to fill the canvas
591 @param r the red component (0..255) of the color to fill the canvas
592 @param g the green component (0..255) of the color to fill the canvas
593 @param b the blue component (0..255) of the color to fill the canvas
594 @param mode the mode to apply the color in (defaults to SrcOver)
595 */
596 void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
reed@android.com845fdac2009-06-23 03:01:32 +0000597 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598
599 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000600 specified color and mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000601 @param color the color to draw with
602 @param mode the mode to apply the color in (defaults to SrcOver)
603 */
604 void drawColor(SkColor color,
reed@android.com845fdac2009-06-23 03:01:32 +0000605 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606
reed@google.com2a981812011-04-14 18:59:28 +0000607 /**
608 * This erases the entire drawing surface to the specified color,
609 * irrespective of the clip. It does not blend with the previous pixels,
610 * but always overwrites them.
611 *
612 * It is roughly equivalent to the following:
613 * canvas.save();
614 * canvas.clipRect(hugeRect, kReplace_Op);
615 * paint.setColor(color);
616 * paint.setXfermodeMode(kSrc_Mode);
617 * canvas.drawPaint(paint);
618 * canvas.restore();
619 * though it is almost always much more efficient.
620 */
621 virtual void clear(SkColor);
622
623 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000624 * This makes the contents of the canvas undefined. Subsequent calls that
625 * require reading the canvas contents will produce undefined results. Examples
626 * include blending and readPixels. The actual implementation is backend-
627 * dependent and one legal implementation is to do nothing. Like clear(), this
628 * ignores the clip.
629 *
630 * This function should only be called if the caller intends to subsequently
631 * draw to the canvas. The canvas may do real work at discard() time in order
632 * to optimize performance on subsequent draws. Thus, if you call this and then
633 * never draw to the canvas subsequently you may pay a perfomance penalty.
634 */
635 void discard() { this->onDiscard(); }
636
637 /**
reed@google.com2a981812011-04-14 18:59:28 +0000638 * Fill the entire canvas' bitmap (restricted to the current clip) with the
639 * specified paint.
640 * @param paint The paint used to fill the canvas
641 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642 virtual void drawPaint(const SkPaint& paint);
643
644 enum PointMode {
645 /** drawPoints draws each point separately */
646 kPoints_PointMode,
647 /** drawPoints draws each pair of points as a line segment */
648 kLines_PointMode,
649 /** drawPoints draws the array of points as a polygon */
650 kPolygon_PointMode
651 };
652
653 /** Draw a series of points, interpreted based on the PointMode mode. For
654 all modes, the count parameter is interpreted as the total number of
655 points. For kLine mode, count/2 line segments are drawn.
656 For kPoint mode, each point is drawn centered at its coordinate, and its
657 size is specified by the paint's stroke-width. It draws as a square,
658 unless the paint's cap-type is round, in which the points are drawn as
659 circles.
660 For kLine mode, each pair of points is drawn as a line segment,
661 respecting the paint's settings for cap/join/width.
662 For kPolygon mode, the entire array is drawn as a series of connected
663 line segments.
664 Note that, while similar, kLine and kPolygon modes draw slightly
665 differently than the equivalent path built with a series of moveto,
666 lineto calls, in that the path will draw all of its contours at once,
667 with no interactions if contours intersect each other (think XOR
668 xfermode). drawPoints always draws each element one at a time.
669 @param mode PointMode specifying how to draw the array of points.
670 @param count The number of points in the array
671 @param pts Array of points to draw
672 @param paint The paint used to draw the points
673 */
674 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
675 const SkPaint& paint);
676
677 /** Helper method for drawing a single point. See drawPoints() for a more
678 details.
679 */
680 void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 /** Draws a single pixel in the specified color.
683 @param x The X coordinate of which pixel to draw
684 @param y The Y coordiante of which pixel to draw
685 @param color The color to draw
686 */
687 void drawPoint(SkScalar x, SkScalar y, SkColor color);
688
689 /** Draw a line segment with the specified start and stop x,y coordinates,
690 using the specified paint. NOTE: since a line is always "framed", the
691 paint's Style is ignored.
692 @param x0 The x-coordinate of the start point of the line
693 @param y0 The y-coordinate of the start point of the line
694 @param x1 The x-coordinate of the end point of the line
695 @param y1 The y-coordinate of the end point of the line
696 @param paint The paint used to draw the line
697 */
698 void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
699 const SkPaint& paint);
700
701 /** Draw the specified rectangle using the specified paint. The rectangle
702 will be filled or stroked based on the Style in the paint.
703 @param rect The rect to be drawn
704 @param paint The paint used to draw the rect
705 */
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000706 virtual void drawRect(const SkRect& rect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000707
708 /** Draw the specified rectangle using the specified paint. The rectangle
709 will be filled or framed based on the Style in the paint.
710 @param rect The rect to be drawn
711 @param paint The paint used to draw the rect
712 */
reed@google.com87001ed2014-02-17 16:28:05 +0000713 void drawIRect(const SkIRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000714 SkRect r;
715 r.set(rect); // promotes the ints to scalars
716 this->drawRect(r, paint);
717 }
reed@google.com4b226022011-01-11 18:32:13 +0000718
reed@android.com8a1c16f2008-12-17 15:59:43 +0000719 /** Draw the specified rectangle using the specified paint. The rectangle
720 will be filled or framed based on the Style in the paint.
721 @param left The left side of the rectangle to be drawn
722 @param top The top side of the rectangle to be drawn
723 @param right The right side of the rectangle to be drawn
724 @param bottom The bottom side of the rectangle to be drawn
725 @param paint The paint used to draw the rect
726 */
727 void drawRectCoords(SkScalar left, SkScalar top, SkScalar right,
728 SkScalar bottom, const SkPaint& paint);
729
730 /** Draw the specified oval using the specified paint. The oval will be
731 filled or framed based on the Style in the paint.
732 @param oval The rectangle bounds of the oval to be drawn
733 @param paint The paint used to draw the oval
734 */
reed@google.com4ed0fb72012-12-12 20:48:18 +0000735 virtual void drawOval(const SkRect& oval, const SkPaint&);
736
737 /**
738 * Draw the specified RRect using the specified paint The rrect will be filled or stroked
739 * based on the Style in the paint.
740 *
741 * @param rrect The round-rect to draw
742 * @param paint The paint used to draw the round-rect
743 */
744 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000745
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000746 /**
747 * Draw the annulus formed by the outer and inner rrects. The results
748 * are undefined if the outer does not contain the inner.
749 */
750 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
751
reed@android.com8a1c16f2008-12-17 15:59:43 +0000752 /** Draw the specified circle using the specified paint. If radius is <= 0,
753 then nothing will be drawn. The circle will be filled
754 or framed based on the Style in the paint.
755 @param cx The x-coordinate of the center of the cirle to be drawn
756 @param cy The y-coordinate of the center of the cirle to be drawn
757 @param radius The radius of the cirle to be drawn
758 @param paint The paint used to draw the circle
759 */
760 void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
761 const SkPaint& paint);
762
763 /** Draw the specified arc, which will be scaled to fit inside the
764 specified oval. If the sweep angle is >= 360, then the oval is drawn
765 completely. Note that this differs slightly from SkPath::arcTo, which
766 treats the sweep angle mod 360.
767 @param oval The bounds of oval used to define the shape of the arc
768 @param startAngle Starting angle (in degrees) where the arc begins
769 @param sweepAngle Sweep angle (in degrees) measured clockwise
770 @param useCenter true means include the center of the oval. For filling
771 this will draw a wedge. False means just use the arc.
772 @param paint The paint used to draw the arc
773 */
774 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
775 bool useCenter, const SkPaint& paint);
776
777 /** Draw the specified round-rect using the specified paint. The round-rect
778 will be filled or framed based on the Style in the paint.
779 @param rect The rectangular bounds of the roundRect to be drawn
780 @param rx The x-radius of the oval used to round the corners
781 @param ry The y-radius of the oval used to round the corners
782 @param paint The paint used to draw the roundRect
783 */
784 void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry,
785 const SkPaint& paint);
786
787 /** Draw the specified path using the specified paint. The path will be
788 filled or framed based on the Style in the paint.
789 @param path The path to be drawn
790 @param paint The paint used to draw the path
791 */
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000792 virtual void drawPath(const SkPath& path, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000793
794 /** Draw the specified bitmap, with its top/left corner at (x,y), using the
795 specified paint, transformed by the current matrix. Note: if the paint
796 contains a maskfilter that generates a mask which extends beyond the
797 bitmap's original width/height, then the bitmap will be drawn as if it
798 were in a Shader with CLAMP mode. Thus the color outside of the original
799 width/height will be the edge color replicated.
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000800
801 If a shader is present on the paint it will be ignored, except in the
reed@google.comf20fc242014-03-26 13:44:58 +0000802 case where the bitmap is kAlpha_8_SkColorType. In that case, the color is
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000803 generated by the shader.
804
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 @param bitmap The bitmap to be drawn
806 @param left The position of the left side of the bitmap being drawn
807 @param top The position of the top side of the bitmap being drawn
808 @param paint The paint used to draw the bitmap, or NULL
809 */
810 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
811 const SkPaint* paint = NULL);
812
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000813 enum DrawBitmapRectFlags {
robertphillips@google.com31acc112013-08-20 12:13:48 +0000814 kNone_DrawBitmapRectFlag = 0x0,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000815 /**
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000816 * When filtering is enabled, allow the color samples outside of
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000817 * the src rect (but still in the src bitmap) to bleed into the
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000818 * drawn portion
819 */
robertphillips@google.com31acc112013-08-20 12:13:48 +0000820 kBleed_DrawBitmapRectFlag = 0x1,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000821 };
822
reed@android.com8a1c16f2008-12-17 15:59:43 +0000823 /** Draw the specified bitmap, with the specified matrix applied (before the
824 canvas' matrix is applied).
825 @param bitmap The bitmap to be drawn
826 @param src Optional: specify the subset of the bitmap to be drawn
827 @param dst The destination rectangle where the scaled/translated
828 image will be drawn
829 @param paint The paint used to draw the bitmap, or NULL
830 */
reed@google.com71121732012-09-18 15:14:33 +0000831 virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
832 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000833 const SkPaint* paint = NULL,
robertphillips@google.com31acc112013-08-20 12:13:48 +0000834 DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000835
reed@google.com71121732012-09-18 15:14:33 +0000836 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000837 const SkPaint* paint = NULL) {
robertphillips@google.com31acc112013-08-20 12:13:48 +0000838 this->drawBitmapRectToRect(bitmap, NULL, dst, paint, kNone_DrawBitmapRectFlag);
reed@google.com71121732012-09-18 15:14:33 +0000839 }
840
841 void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* isrc,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000842 const SkRect& dst, const SkPaint* paint = NULL,
robertphillips@google.com31acc112013-08-20 12:13:48 +0000843 DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) {
reed@google.com71121732012-09-18 15:14:33 +0000844 SkRect realSrcStorage;
845 SkRect* realSrcPtr = NULL;
846 if (isrc) {
847 realSrcStorage.set(*isrc);
848 realSrcPtr = &realSrcStorage;
849 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000850 this->drawBitmapRectToRect(bitmap, realSrcPtr, dst, paint, flags);
reed@google.com71121732012-09-18 15:14:33 +0000851 }
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000852
reed@android.com8a1c16f2008-12-17 15:59:43 +0000853 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
854 const SkPaint* paint = NULL);
reed@google.com4b226022011-01-11 18:32:13 +0000855
reed@google.comf0b5e112011-09-07 11:57:34 +0000856 /**
857 * Draw the bitmap stretched differentially to fit into dst.
858 * center is a rect within the bitmap, and logically divides the bitmap
859 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
860 * bitmap is the "center", then the center-rect should be [2, 2, 3, 3].
861 *
862 * If the dst is >= the bitmap size, then...
robertphillips@google.com9bf380c2013-07-25 12:10:42 +0000863 * - The 4 corners are not stretched at all.
864 * - The sides are stretched in only one axis.
865 * - The center is stretched in both axes.
reed@google.comf0b5e112011-09-07 11:57:34 +0000866 * Else, for each axis where dst < bitmap,
867 * - The corners shrink proportionally
868 * - The sides (along the shrink axis) and center are not drawn
869 */
870 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
871 const SkRect& dst, const SkPaint* paint = NULL);
872
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873 /** Draw the specified bitmap, with its top/left corner at (x,y),
874 NOT transformed by the current matrix. Note: if the paint
875 contains a maskfilter that generates a mask which extends beyond the
876 bitmap's original width/height, then the bitmap will be drawn as if it
877 were in a Shader with CLAMP mode. Thus the color outside of the original
878 width/height will be the edge color replicated.
879 @param bitmap The bitmap to be drawn
880 @param left The position of the left side of the bitmap being drawn
881 @param top The position of the top side of the bitmap being drawn
882 @param paint The paint used to draw the bitmap, or NULL
883 */
884 virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
885 const SkPaint* paint = NULL);
886
887 /** Draw the text, with origin at (x,y), using the specified paint.
888 The origin is interpreted based on the Align setting in the paint.
889 @param text The text to be drawn
890 @param byteLength The number of bytes to read from the text parameter
891 @param x The x-coordinate of the origin of the text being drawn
892 @param y The y-coordinate of the origin of the text being drawn
893 @param paint The paint used for the text (e.g. color, size, style)
894 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000895 SK_LEGACY_DRAWTEXT_VIRTUAL void drawText(const void* text, size_t byteLength, SkScalar x,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000896 SkScalar y, const SkPaint& paint);
897
898 /** Draw the text, with each character/glyph origin specified by the pos[]
reed@google.com4b226022011-01-11 18:32:13 +0000899 array. The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000900 @param text The text to be drawn
901 @param byteLength The number of bytes to read from the text parameter
902 @param pos Array of positions, used to position each character
903 @param paint The paint used for the text (e.g. color, size, style)
904 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000905 SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosText(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000906 const SkPoint pos[], const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000907
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908 /** Draw the text, with each character/glyph origin specified by the x
909 coordinate taken from the xpos[] array, and the y from the constY param.
reed@google.com4b226022011-01-11 18:32:13 +0000910 The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000911 @param text The text to be drawn
912 @param byteLength The number of bytes to read from the text parameter
913 @param xpos Array of x-positions, used to position each character
914 @param constY The shared Y coordinate for all of the positions
915 @param paint The paint used for the text (e.g. color, size, style)
916 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000917 SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosTextH(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000918 const SkScalar xpos[], SkScalar constY,
919 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000920
reed@android.com8a1c16f2008-12-17 15:59:43 +0000921 /** Draw the text, with origin at (x,y), using the specified paint, along
922 the specified path. The paint's Align setting determins where along the
923 path to start the text.
924 @param text The text to be drawn
925 @param byteLength The number of bytes to read from the text parameter
926 @param path The path the text should follow for its baseline
927 @param hOffset The distance along the path to add to the text's
928 starting position
929 @param vOffset The distance above(-) or below(+) the path to
930 position the text
931 @param paint The paint used for the text
932 */
933 void drawTextOnPathHV(const void* text, size_t byteLength,
934 const SkPath& path, SkScalar hOffset,
935 SkScalar vOffset, const SkPaint& paint);
936
937 /** Draw the text, with origin at (x,y), using the specified paint, along
938 the specified path. The paint's Align setting determins where along the
939 path to start the text.
940 @param text The text to be drawn
941 @param byteLength The number of bytes to read from the text parameter
942 @param path The path the text should follow for its baseline
943 @param matrix (may be null) Applied to the text before it is
944 mapped onto the path
945 @param paint The paint used for the text
946 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000947 SK_LEGACY_DRAWTEXT_VIRTUAL void drawTextOnPath(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000948 const SkPath& path, const SkMatrix* matrix,
949 const SkPaint& paint);
950
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000951 /** PRIVATE / EXPERIMENTAL -- do not call
952 Perform back-end analysis/optimization of a picture. This may attach
953 optimization data to the picture which can be used by a later
954 drawPicture call.
955 @param picture The recorded drawing commands to analyze/optimize
956 */
robertphillips9b14f262014-06-04 05:40:44 -0700957 void EXPERIMENTAL_optimize(const SkPicture* picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000958
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +0000959 /** PRIVATE / EXPERIMENTAL -- do not call
960 Purge all the discardable optimization information associated with
961 'picture'. If NULL is passed in, purge all discardable information.
962 */
robertphillips9b14f262014-06-04 05:40:44 -0700963 void EXPERIMENTAL_purge(const SkPicture* picture);
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +0000964
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965 /** Draw the picture into this canvas. This method effective brackets the
966 playback of the picture's draw calls with save/restore, so the state
djsollen@google.coma44de962013-01-02 16:59:19 +0000967 of this canvas will be unchanged after this call.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000968 @param picture The recorded drawing commands to playback into this
969 canvas.
970 */
robertphillips9b14f262014-06-04 05:40:44 -0700971 void drawPicture(const SkPicture* picture);
972
reed@android.com8a1c16f2008-12-17 15:59:43 +0000973 enum VertexMode {
974 kTriangles_VertexMode,
975 kTriangleStrip_VertexMode,
976 kTriangleFan_VertexMode
977 };
reed@google.com4b226022011-01-11 18:32:13 +0000978
reed@android.com8a1c16f2008-12-17 15:59:43 +0000979 /** Draw the array of vertices, interpreted as triangles (based on mode).
commit-bot@chromium.org559a8832014-05-30 10:08:22 +0000980
981 If both textures and vertex-colors are NULL, it strokes hairlines with
982 the paint's color. This behavior is a useful debugging mode to visualize
983 the mesh.
984
reed@android.com8a1c16f2008-12-17 15:59:43 +0000985 @param vmode How to interpret the array of vertices
986 @param vertexCount The number of points in the vertices array (and
987 corresponding texs and colors arrays if non-null)
988 @param vertices Array of vertices for the mesh
989 @param texs May be null. If not null, specifies the coordinate
robertphillips@google.com631a59b2013-07-31 14:57:53 +0000990 in _texture_ space (not uv space) for each vertex.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000991 @param colors May be null. If not null, specifies a color for each
992 vertex, to be interpolated across the triangle.
993 @param xmode Used if both texs and colors are present. In this
994 case the colors are combined with the texture using mode,
995 before being drawn using the paint. If mode is null, then
reed@google.com8d3cd7a2013-01-30 21:36:11 +0000996 kModulate_Mode is used.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000997 @param indices If not null, array of indices to reference into the
998 vertex (texs, colors) array.
999 @param indexCount number of entries in the indices array (if not null)
reed@google.com4b226022011-01-11 18:32:13 +00001000 @param paint Specifies the shader/texture if present.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001 */
1002 virtual void drawVertices(VertexMode vmode, int vertexCount,
1003 const SkPoint vertices[], const SkPoint texs[],
1004 const SkColor colors[], SkXfermode* xmode,
1005 const uint16_t indices[], int indexCount,
1006 const SkPaint& paint);
1007
reed@android.comcb608442009-12-04 21:32:27 +00001008 /** Send a blob of data to the canvas.
1009 For canvases that draw, this call is effectively a no-op, as the data
1010 is not parsed, but just ignored. However, this call exists for
1011 subclasses like SkPicture's recording canvas, that can store the data
1012 and then play it back later (via another call to drawData).
1013 */
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001014 virtual void drawData(const void* data, size_t length) {
1015 // do nothing. Subclasses may do something with the data
1016 }
1017
skia.committer@gmail.coma5d3e772013-05-30 07:01:29 +00001018 /** Add comments. beginCommentGroup/endCommentGroup open/close a new group.
1019 Each comment added via addComment is notionally attached to its
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001020 enclosing group. Top-level comments simply belong to no group.
1021 */
1022 virtual void beginCommentGroup(const char* description) {
1023 // do nothing. Subclasses may do something
1024 }
1025 virtual void addComment(const char* kywd, const char* value) {
1026 // do nothing. Subclasses may do something
1027 }
1028 virtual void endCommentGroup() {
1029 // do nothing. Subclasses may do something
1030 }
1031
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001032 /**
1033 * With this call the client asserts that subsequent draw operations (up to the
1034 * matching popCull()) are fully contained within the given bounding box. The assertion
1035 * is not enforced, but the information might be used to quick-reject command blocks,
1036 * so an incorrect bounding box may result in incomplete rendering.
1037 */
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001038 void pushCull(const SkRect& cullRect);
reed@android.comcb608442009-12-04 21:32:27 +00001039
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001040 /**
1041 * Terminates the current culling block, and restores the previous one (if any).
1042 */
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001043 void popCull();
1044
reed@android.com8a1c16f2008-12-17 15:59:43 +00001045 //////////////////////////////////////////////////////////////////////////
reed@google.com4b226022011-01-11 18:32:13 +00001046
reed@android.com8a1c16f2008-12-17 15:59:43 +00001047 /** Get the current filter object. The filter's reference count is not
reed@android.comdc3381f2010-02-11 16:05:15 +00001048 affected. The filter is saved/restored, just like the matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001049 @return the canvas' filter (or NULL).
1050 */
1051 SkDrawFilter* getDrawFilter() const;
reed@google.com4b226022011-01-11 18:32:13 +00001052
reed@android.com8a1c16f2008-12-17 15:59:43 +00001053 /** Set the new filter (or NULL). Pass NULL to clear any existing filter.
1054 As a convenience, the parameter is returned. If an existing filter
1055 exists, its refcnt is decrement. If the new filter is not null, its
reed@android.comdc3381f2010-02-11 16:05:15 +00001056 refcnt is incremented. The filter is saved/restored, just like the
1057 matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001058 @param filter the new filter (or NULL)
1059 @return the new filter
1060 */
1061 virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
1062
1063 //////////////////////////////////////////////////////////////////////////
1064
reed@google.com754de5f2014-02-24 19:38:20 +00001065 /**
1066 * Return true if the current clip is empty (i.e. nothing will draw).
1067 * Note: this is not always a free call, so it should not be used
1068 * more often than necessary. However, once the canvas has computed this
1069 * result, subsequent calls will be cheap (until the clip state changes,
1070 * which can happen on any clip..() or restore() call.
1071 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001072 virtual bool isClipEmpty() const;
reed@google.com754de5f2014-02-24 19:38:20 +00001073
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001074 /**
1075 * Returns true if the current clip is just a (non-empty) rectangle.
1076 * Returns false if the clip is empty, or if it is complex.
1077 */
1078 virtual bool isClipRect() const;
1079
reed@android.com8a1c16f2008-12-17 15:59:43 +00001080 /** Return the current matrix on the canvas.
1081 This does not account for the translate in any of the devices.
1082 @return The current matrix on the canvas.
1083 */
junov@chromium.orga907ac32012-02-24 21:54:07 +00001084 const SkMatrix& getTotalMatrix() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001085
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001086#ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001087 enum ClipType {
1088 kEmpty_ClipType = 0,
1089 kRect_ClipType,
1090 kComplex_ClipType
1091 };
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001092 /** Returns a description of the total clip; may be cheaper than
1093 getting the clip and querying it directly.
1094 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001095 virtual ClipType getClipType() const;
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001096#endif
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001097
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001098 /** Return the clip stack. The clip stack stores all the individual
1099 * clips organized by the save/restore frame in which they were
1100 * added.
1101 * @return the current clip stack ("list" of individual clip elements)
1102 */
1103 const SkClipStack* getClipStack() const {
1104 return &fClipStack;
1105 }
1106
fmalitac3b589a2014-06-05 12:40:07 -07001107 typedef SkCanvasClipVisitor ClipVisitor;
reed@google.com90c07ea2012-04-13 13:50:27 +00001108 /**
1109 * Replays the clip operations, back to front, that have been applied to
1110 * the canvas, calling the appropriate method on the visitor for each
1111 * clip. All clips have already been transformed into device space.
1112 */
1113 void replayClips(ClipVisitor*) const;
1114
reed@android.com8a1c16f2008-12-17 15:59:43 +00001115 ///////////////////////////////////////////////////////////////////////////
1116
1117 /** After calling saveLayer(), there can be any number of devices that make
1118 up the top-most drawing area. LayerIter can be used to iterate through
1119 those devices. Note that the iterator is only valid until the next API
1120 call made on the canvas. Ownership of all pointers in the iterator stays
1121 with the canvas, so none of them should be modified or deleted.
1122 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +00001123 class SK_API LayerIter /*: SkNoncopyable*/ {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001124 public:
1125 /** Initialize iterator with canvas, and set values for 1st device */
1126 LayerIter(SkCanvas*, bool skipEmptyClips);
1127 ~LayerIter();
reed@google.com4b226022011-01-11 18:32:13 +00001128
reed@android.com8a1c16f2008-12-17 15:59:43 +00001129 /** Return true if the iterator is done */
1130 bool done() const { return fDone; }
1131 /** Cycle to the next device */
1132 void next();
reed@google.com4b226022011-01-11 18:32:13 +00001133
reed@android.com8a1c16f2008-12-17 15:59:43 +00001134 // These reflect the current device in the iterator
1135
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001136 SkBaseDevice* device() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137 const SkMatrix& matrix() const;
1138 const SkRegion& clip() const;
1139 const SkPaint& paint() const;
1140 int x() const;
1141 int y() const;
reed@google.com4b226022011-01-11 18:32:13 +00001142
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143 private:
1144 // used to embed the SkDrawIter object directly in our instance, w/o
1145 // having to expose that class def to the public. There is an assert
1146 // in our constructor to ensure that fStorage is large enough
1147 // (though needs to be a compile-time-assert!). We use intptr_t to work
1148 // safely with 32 and 64 bit machines (to ensure the storage is enough)
reed@android.comf2b98d62010-12-20 18:26:13 +00001149 intptr_t fStorage[32];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 class SkDrawIter* fImpl; // this points at fStorage
1151 SkPaint fDefaultPaint;
1152 bool fDone;
1153 };
1154
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001155 // don't call
1156 const SkRegion& internal_private_getTotalClip() const;
1157 // don't call
1158 void internal_private_getTotalClipAsPath(SkPath*) const;
reed@google.com9c135db2014-03-12 18:28:35 +00001159 // don't call
1160 GrRenderTarget* internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001161
reed@android.com8a1c16f2008-12-17 15:59:43 +00001162protected:
reed@google.com76f10a32014-02-05 15:32:21 +00001163 // default impl defers to getDevice()->newSurface(info)
1164 virtual SkSurface* onNewSurface(const SkImageInfo&);
1165
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001166 // default impl defers to its device
1167 virtual const void* onPeekPixels(SkImageInfo*, size_t* rowBytes);
reed@google.com9c135db2014-03-12 18:28:35 +00001168 virtual void* onAccessTopLayerPixels(SkImageInfo*, size_t* rowBytes);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001169
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001170 // Subclass save/restore notifiers.
1171 // Overriders should call the corresponding INHERITED method up the inheritance chain.
1172 // willSaveLayer()'s return value may suppress full layer allocation.
1173 enum SaveLayerStrategy {
1174 kFullLayer_SaveLayerStrategy,
1175 kNoLayer_SaveLayerStrategy
1176 };
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001177
fmalita6ca763f2014-06-17 13:52:18 -07001178 virtual void willSave() {}
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001179 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) {
1180 return kFullLayer_SaveLayerStrategy;
1181 }
1182 virtual void willRestore() {}
1183 virtual void didConcat(const SkMatrix&) {}
1184 virtual void didSetMatrix(const SkMatrix&) {}
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +00001185
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +00001186 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
1187
reed@google.come0d9ce82014-04-23 04:00:17 +00001188 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1189 SkScalar y, const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001190
reed@google.come0d9ce82014-04-23 04:00:17 +00001191 virtual void onDrawPosText(const void* text, size_t byteLength,
1192 const SkPoint pos[], const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001193
reed@google.come0d9ce82014-04-23 04:00:17 +00001194 virtual void onDrawPosTextH(const void* text, size_t byteLength,
1195 const SkScalar xpos[], SkScalar constY,
1196 const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001197
reed@google.come0d9ce82014-04-23 04:00:17 +00001198 virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1199 const SkPath& path, const SkMatrix* matrix,
1200 const SkPaint& paint);
1201
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001202 enum ClipEdgeStyle {
1203 kHard_ClipEdgeStyle,
1204 kSoft_ClipEdgeStyle
1205 };
1206
1207 virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1208 virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1209 virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1210 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op);
1211
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001212 virtual void onDiscard();
1213
robertphillips9b14f262014-06-04 05:40:44 -07001214 virtual void onDrawPicture(const SkPicture* picture);
1215
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001216 // Returns the canvas to be used by DrawIter. Default implementation
junov@google.com4370aed2012-01-18 16:21:08 +00001217 // returns this. Subclasses that encapsulate an indirect canvas may
1218 // need to overload this method. The impl must keep track of this, as it
1219 // is not released or deleted by the caller.
1220 virtual SkCanvas* canvasForDrawIter();
1221
junov@chromium.orga907ac32012-02-24 21:54:07 +00001222 // Clip rectangle bounds. Called internally by saveLayer.
1223 // returns false if the entire rectangle is entirely clipped out
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001224 // If non-NULL, The imageFilter parameter will be used to expand the clip
1225 // and offscreen bounds for any margin required by the filter DAG.
junov@chromium.orga907ac32012-02-24 21:54:07 +00001226 bool clipRectBounds(const SkRect* bounds, SaveFlags flags,
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001227 SkIRect* intersection,
1228 const SkImageFilter* imageFilter = NULL);
junov@chromium.orga907ac32012-02-24 21:54:07 +00001229
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001230 // Called by child classes that override clipPath and clipRRect to only
1231 // track fast conservative clip bounds, rather than exact clips.
commit-bot@chromium.org759cf482014-03-06 13:18:07 +00001232 void updateClipConservativelyUsingBounds(const SkRect&, SkRegion::Op,
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001233 bool inverseFilled);
1234
reed@google.com97af1a62012-08-28 12:19:02 +00001235 // notify our surface (if we have one) that we are about to draw, so it
1236 // can perform copy-on-write or invalidate any cached images
1237 void predrawNotify();
1238
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001239 virtual void onPushCull(const SkRect& cullRect);
1240 virtual void onPopCull();
1241
reed@android.com8a1c16f2008-12-17 15:59:43 +00001242private:
1243 class MCRec;
1244
reed@google.com5c3d1472011-02-22 19:12:23 +00001245 SkClipStack fClipStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001246 SkDeque fMCStack;
1247 // points to top of stack
1248 MCRec* fMCRec;
1249 // the first N recs that can fit here mean we won't call malloc
1250 uint32_t fMCRecStorage[32];
1251
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +00001252 int fSaveLayerCount; // number of successful saveLayer calls
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001253 int fCullCount; // number of active culls
reed@android.com8a1c16f2008-12-17 15:59:43 +00001254
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +00001255 SkMetaData* fMetaData;
1256
reed@google.com97af1a62012-08-28 12:19:02 +00001257 SkSurface_Base* fSurfaceBase;
1258 SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1259 void setSurfaceBase(SkSurface_Base* sb) {
1260 fSurfaceBase = sb;
1261 }
1262 friend class SkSurface_Base;
junov@chromium.org45c3db82013-04-11 17:52:05 +00001263 friend class SkSurface_Gpu;
skia.committer@gmail.comfc843592012-10-11 02:01:14 +00001264
reed@android.com8a1c16f2008-12-17 15:59:43 +00001265 bool fDeviceCMDirty; // cleared by updateDeviceCMCache()
1266 void updateDeviceCMCache();
1267
reed@google.com9c135db2014-03-12 18:28:35 +00001268 friend class SkDrawIter; // needs setupDrawForLayerDevice()
reed@google.com8926b162012-03-23 15:36:36 +00001269 friend class AutoDrawLooper;
reed@google.com9c135db2014-03-12 18:28:35 +00001270 friend class SkLua; // needs top layer size and offset
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +00001271 friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
reed@google.com9c135db2014-03-12 18:28:35 +00001272 friend class SkDeferredDevice; // needs getTopDevice()
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001274 SkBaseDevice* createLayerDevice(const SkImageInfo&);
bsalomon@google.come97f0852011-06-17 13:10:25 +00001275
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001276 SkBaseDevice* init(SkBaseDevice*);
reed@google.comf0b5e112011-09-07 11:57:34 +00001277
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001278 /**
1279 * DEPRECATED
1280 *
1281 * Specify a device for this canvas to draw into. If it is not null, its
1282 * reference count is incremented. If the canvas was already holding a
1283 * device, its reference count is decremented. The new device is returned.
1284 */
1285 SkBaseDevice* setRootDevice(SkBaseDevice* device);
skia.committer@gmail.com31acdea2014-02-18 03:01:51 +00001286
bsalomon@google.com4ebe3822014-02-26 20:22:32 +00001287 /**
1288 * Gets the size/origin of the top level layer in global canvas coordinates. We don't want this
1289 * to be public because it exposes decisions about layer sizes that are internal to the canvas.
1290 */
1291 SkISize getTopLayerSize() const;
1292 SkIPoint getTopLayerOrigin() const;
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001293
reed@google.comf0b5e112011-09-07 11:57:34 +00001294 // internal methods are not virtual, so they can safely be called by other
1295 // canvas apis, without confusing subclasses (like SkPictureRecording)
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001296 void internalDrawBitmap(const SkBitmap&, const SkMatrix& m, const SkPaint* paint);
reed@google.com71121732012-09-18 15:14:33 +00001297 void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001298 const SkRect& dst, const SkPaint* paint,
1299 DrawBitmapRectFlags flags);
reed@google.comf0b5e112011-09-07 11:57:34 +00001300 void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
1301 const SkRect& dst, const SkPaint* paint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001302 void internalDrawPaint(const SkPaint& paint);
reed@google.com8926b162012-03-23 15:36:36 +00001303 int internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001304 SaveFlags, bool justForImageFilter, SaveLayerStrategy strategy);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001305 void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001306
reed@android.com8a1c16f2008-12-17 15:59:43 +00001307 // shared by save() and saveLayer()
Florin Malita5f6102d2014-06-30 10:13:28 -04001308 int internalSave();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001309 void internalRestore();
bungeman@google.com52c748b2011-08-22 21:30:43 +00001310 static void DrawRect(const SkDraw& draw, const SkPaint& paint,
1311 const SkRect& r, SkScalar textSize);
1312 static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
1313 const char text[], size_t byteLength,
1314 SkScalar x, SkScalar y);
reed@google.com4b226022011-01-11 18:32:13 +00001315
reed@android.com8a1c16f2008-12-17 15:59:43 +00001316 /* These maintain a cache of the clip bounds in local coordinates,
1317 (converted to 2s-compliment if floats are slow).
1318 */
reed@google.comc0784db2013-12-13 21:16:12 +00001319 mutable SkRect fCachedLocalClipBounds;
1320 mutable bool fCachedLocalClipBoundsDirty;
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001321 bool fAllowSoftClip;
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001322 bool fAllowSimplifyClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001323
reed@google.comc0784db2013-12-13 21:16:12 +00001324 const SkRect& getLocalClipBounds() const {
1325 if (fCachedLocalClipBoundsDirty) {
1326 if (!this->getClipBounds(&fCachedLocalClipBounds)) {
1327 fCachedLocalClipBounds.setEmpty();
1328 }
1329 fCachedLocalClipBoundsDirty = false;
reed@android.comba09de42010-02-05 20:46:05 +00001330 }
reed@google.comc0784db2013-12-13 21:16:12 +00001331 return fCachedLocalClipBounds;
reed@android.comba09de42010-02-05 20:46:05 +00001332 }
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001333
reed@google.com5c3d1472011-02-22 19:12:23 +00001334 class AutoValidateClip : ::SkNoncopyable {
1335 public:
1336 explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1337 fCanvas->validateClip();
1338 }
1339 ~AutoValidateClip() { fCanvas->validateClip(); }
1340
1341 private:
1342 const SkCanvas* fCanvas;
1343 };
1344
1345#ifdef SK_DEBUG
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001346 // The cull stack rects are in device-space
1347 SkTDArray<SkIRect> fCullStack;
1348 void validateCull(const SkIRect&);
reed@google.com5c3d1472011-02-22 19:12:23 +00001349 void validateClip() const;
1350#else
1351 void validateClip() const {}
1352#endif
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +00001353
1354 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001355};
1356
1357/** Stack helper class to automatically call restoreToCount() on the canvas
1358 when this object goes out of scope. Use this to guarantee that the canvas
1359 is restored to a known state.
1360*/
1361class SkAutoCanvasRestore : SkNoncopyable {
1362public:
commit-bot@chromium.org28871192013-10-14 15:28:01 +00001363 SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1364 if (fCanvas) {
1365 fSaveCount = canvas->getSaveCount();
1366 if (doSave) {
1367 canvas->save();
1368 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001369 }
1370 }
1371 ~SkAutoCanvasRestore() {
reed@google.comf6c9a5b2012-11-20 15:12:21 +00001372 if (fCanvas) {
1373 fCanvas->restoreToCount(fSaveCount);
1374 }
1375 }
1376
1377 /**
1378 * Perform the restore now, instead of waiting for the destructor. Will
1379 * only do this once.
1380 */
1381 void restore() {
1382 if (fCanvas) {
1383 fCanvas->restoreToCount(fSaveCount);
1384 fCanvas = NULL;
1385 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386 }
1387
1388private:
1389 SkCanvas* fCanvas;
1390 int fSaveCount;
1391};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001392#define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001393
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001394/** Stack helper class to automatically open and close a comment block
1395 */
1396class SkAutoCommentBlock : SkNoncopyable {
1397public:
1398 SkAutoCommentBlock(SkCanvas* canvas, const char* description) {
1399 fCanvas = canvas;
1400 if (NULL != fCanvas) {
1401 fCanvas->beginCommentGroup(description);
1402 }
1403 }
1404
1405 ~SkAutoCommentBlock() {
1406 if (NULL != fCanvas) {
1407 fCanvas->endCommentGroup();
1408 }
1409 }
1410
1411private:
1412 SkCanvas* fCanvas;
1413};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001414#define SkAutoCommentBlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoCommentBlock)
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001415
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001416/**
1417 * If the caller wants read-only access to the pixels in a canvas, it can just
1418 * call canvas->peekPixels(), since that is the fastest way to "peek" at the
1419 * pixels on a raster-backed canvas.
1420 *
1421 * If the canvas has pixels, but they are not readily available to the CPU
1422 * (e.g. gpu-backed), then peekPixels() will fail, but readPixels() will
1423 * succeed (though be slower, since it will return a copy of the pixels).
1424 *
1425 * SkAutoROCanvasPixels encapsulates these two techniques, trying first to call
1426 * peekPixels() (for performance), but if that fails, calling readPixels() and
1427 * storing the copy locally.
1428 *
1429 * The caller must respect the restrictions associated with peekPixels(), since
1430 * that may have been called: The returned information is invalidated if...
1431 * - any API is called on the canvas (or its parent surface if present)
1432 * - the canvas goes out of scope
1433 */
1434class SkAutoROCanvasPixels : SkNoncopyable {
1435public:
1436 SkAutoROCanvasPixels(SkCanvas* canvas);
1437
1438 // returns NULL on failure
1439 const void* addr() const { return fAddr; }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +00001440
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001441 // undefined if addr() == NULL
1442 size_t rowBytes() const { return fRowBytes; }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +00001443
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001444 // undefined if addr() == NULL
1445 const SkImageInfo& info() const { return fInfo; }
1446
1447 // helper that, if returns true, installs the pixels into the bitmap. Note
1448 // that the bitmap may reference the address returned by peekPixels(), so
1449 // the caller must respect the restrictions associated with peekPixels().
1450 bool asROBitmap(SkBitmap*) const;
1451
1452private:
1453 SkBitmap fBitmap; // used if peekPixels() fails
1454 const void* fAddr; // NULL on failure
1455 SkImageInfo fInfo;
1456 size_t fRowBytes;
1457};
1458
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001459static inline SkCanvas::SaveFlags operator|(const SkCanvas::SaveFlags lhs,
1460 const SkCanvas::SaveFlags rhs) {
commit-bot@chromium.org92a89162014-05-30 21:07:05 +00001461 return static_cast<SkCanvas::SaveFlags>(static_cast<int>(lhs) | static_cast<int>(rhs));
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001462}
1463
1464static inline SkCanvas::SaveFlags& operator|=(SkCanvas::SaveFlags& lhs,
1465 const SkCanvas::SaveFlags rhs) {
1466 lhs = lhs | rhs;
1467 return lhs;
1468}
1469
fmalitac3b589a2014-06-05 12:40:07 -07001470class SkCanvasClipVisitor {
1471public:
1472 virtual ~SkCanvasClipVisitor();
1473 virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0;
1474 virtual void clipRRect(const SkRRect&, SkRegion::Op, bool antialias) = 0;
1475 virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0;
1476};
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001477
reed@android.com8a1c16f2008-12-17 15:59:43 +00001478#endif