blob: 8640d729b06965ce22ccac1a287554be767f226e [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() */
296 kMatrix_SaveFlag = 0x01,
297 /** save the clip state, restoring it on restore() */
298 kClip_SaveFlag = 0x02,
299 /** 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
311 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
329 /** DEPRECATED - use save() instead.
330
331 This behaves the same as save(), but it allows fine-grained control of
332 which state bits to be saved (and subsequently restored).
333
djsollen@google.comd4236572013-08-13 14:29:06 +0000334 @param flags The flags govern what portion of the Matrix/Clip/drawFilter
335 state the save (and matching restore) effect. For example,
336 if only kMatrix is specified, then only the matrix state
337 will be pushed and popped. Likewise for the clip if kClip
338 is specified. However, the drawFilter is always affected
339 by calls to save/restore.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 @return The value to pass to restoreToCount() to balance this save()
341 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000342 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
343 int save(SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344
345 /** This behaves the same as save(), but in addition it allocates an
346 offscreen bitmap. All drawing calls are directed there, and only when
347 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000348 the canvas (or the previous layer).
reed@android.comad164b22010-07-02 17:20:51 +0000349 @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().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 @param paint (may be null) This is copied, and is applied to the
354 offscreen when restore() is called
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000355 @return The value to pass to restoreToCount() to balance this save()
356 */
357 int saveLayer(const SkRect* bounds, const SkPaint* paint);
358
359 /** DEPRECATED - use saveLayer(const SkRect*, const SkPaint*) instead.
360
361 This behaves the same as saveLayer(const SkRect*, const SkPaint*),
362 but it allows fine-grained control of which state bits to be saved
363 (and subsequently restored).
364
365 @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().
369 @param paint (may be null) This is copied, and is applied to the
370 offscreen when restore() is called
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 @param flags LayerFlags
372 @return The value to pass to restoreToCount() to balance this save()
373 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000374 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
375 int saveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376
377 /** This behaves the same as save(), but in addition it allocates an
378 offscreen bitmap. All drawing calls are directed there, and only when
379 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000380 the canvas (or the previous layer).
reed@android.com40408612010-07-02 17:24:23 +0000381 @param bounds (may be null) This rect, if non-null, is used as a hint to
382 limit the size of the offscreen, and thus drawing may be
383 clipped to it, though that clipping is not guaranteed to
384 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 @param alpha This is applied to the offscreen when restore() is called.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000386 @return The value to pass to restoreToCount() to balance this save()
387 */
388 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
389
390 /** DEPRECATED - use saveLayerAlpha(const SkRect*, U8CPU) instead.
391
392 This behaves the same as saveLayerAlpha(const SkRect*, U8CPU),
393 but it allows fine-grained control of which state bits to be saved
394 (and subsequently restored).
395
396 @param bounds (may be null) This rect, if non-null, is used as a hint to
397 limit the size of the offscreen, and thus drawing may be
398 clipped to it, though that clipping is not guaranteed to
399 happen. If exact clipping is desired, use clipRect().
400 @param alpha This is applied to the offscreen when restore() is called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 @param flags LayerFlags
402 @return The value to pass to restoreToCount() to balance this save()
403 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000404 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
405 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000406
407 /** This call balances a previous call to save(), and is used to remove all
reed@android.comdc3381f2010-02-11 16:05:15 +0000408 modifications to the matrix/clip/drawFilter state since the last save
409 call.
410 It is an error to call restore() more times than save() was called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000411 */
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000412 void restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413
414 /** Returns the number of matrix/clip states on the SkCanvas' private stack.
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000415 This will equal # save() calls - # restore() calls + 1. The save count on
416 a new canvas is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417 */
junov@chromium.orga907ac32012-02-24 21:54:07 +0000418 int getSaveCount() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419
420 /** Efficient way to pop any calls to save() that happened after the save
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000421 count reached saveCount. It is an error for saveCount to be greater than
422 getSaveCount(). To pop all the way back to the initial matrix/clip context
423 pass saveCount == 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000424 @param saveCount The number of save() levels to restore from
425 */
426 void restoreToCount(int saveCount);
427
reed@google.com7c202932011-12-14 18:48:05 +0000428 /** Returns true if drawing is currently going to a layer (from saveLayer)
429 * rather than to the root device.
430 */
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000431 virtual bool isDrawingToLayer() const;
reed@google.com7c202932011-12-14 18:48:05 +0000432
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 /** Preconcat the current matrix with the specified translation
434 @param dx The distance to translate in X
435 @param dy The distance to translate in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000437 void translate(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438
439 /** Preconcat the current matrix with the specified scale.
440 @param sx The amount to scale in X
441 @param sy The amount to scale in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000443 void scale(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444
445 /** Preconcat the current matrix with the specified rotation.
446 @param degrees The amount to rotate, in degrees
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000448 void rotate(SkScalar degrees);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449
450 /** Preconcat the current matrix with the specified skew.
451 @param sx The amount to skew in X
452 @param sy The amount to skew in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000454 void skew(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455
456 /** Preconcat the current matrix with the specified matrix.
457 @param matrix The matrix to preconcatenate with the current matrix
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000459 void concat(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000460
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461 /** Replace the current matrix with a copy of the specified matrix.
462 @param matrix The matrix that will be copied into the current matrix.
463 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000464 void setMatrix(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000465
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 /** Helper for setMatrix(identity). Sets the current matrix to identity.
467 */
468 void resetMatrix();
469
reed@google.com4ed0fb72012-12-12 20:48:18 +0000470 /**
471 * Modify the current clip with the specified rectangle.
472 * @param rect The rect to combine with the current clip
473 * @param op The region op to apply to the current clip
474 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000475 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000476 void clipRect(const SkRect& rect,
477 SkRegion::Op op = SkRegion::kIntersect_Op,
478 bool doAntiAlias = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479
reed@google.com4ed0fb72012-12-12 20:48:18 +0000480 /**
481 * Modify the current clip with the specified SkRRect.
482 * @param rrect The rrect to combine with the current clip
483 * @param op The region op to apply to the current clip
484 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000485 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000486 void clipRRect(const SkRRect& rrect,
487 SkRegion::Op op = SkRegion::kIntersect_Op,
488 bool doAntiAlias = false);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000489
490 /**
491 * Modify the current clip with the specified path.
492 * @param path The path to combine with the current clip
493 * @param op The region op to apply to the current clip
494 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000495 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000496 void clipPath(const SkPath& path,
497 SkRegion::Op op = SkRegion::kIntersect_Op,
498 bool doAntiAlias = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499
caryclark@google.com8f0a7b82012-11-07 14:54:49 +0000500 /** EXPERIMENTAL -- only used for testing
501 Set to false to force clips to be hard, even if doAntiAlias=true is
502 passed to clipRect or clipPath.
503 */
504 void setAllowSoftClip(bool allow) {
505 fAllowSoftClip = allow;
506 }
507
caryclark@google.com45a75fb2013-04-25 13:34:40 +0000508 /** EXPERIMENTAL -- only used for testing
509 Set to simplify clip stack using path ops.
510 */
511 void setAllowSimplifyClip(bool allow) {
512 fAllowSimplifyClip = allow;
513 }
514
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515 /** Modify the current clip with the specified region. Note that unlike
516 clipRect() and clipPath() which transform their arguments by the current
517 matrix, clipRegion() assumes its argument is already in device
518 coordinates, and so no transformation is performed.
519 @param deviceRgn The region to apply to the current clip
520 @param op The region op to apply to the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000521 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000522 void clipRegion(const SkRegion& deviceRgn,
523 SkRegion::Op op = SkRegion::kIntersect_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524
525 /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the
526 specified region. This does not intersect or in any other way account
527 for the existing clip region.
528 @param deviceRgn The region to copy into the current clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000530 void setClipRegion(const SkRegion& deviceRgn) {
531 this->clipRegion(deviceRgn, SkRegion::kReplace_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532 }
533
reed@android.com8a1c16f2008-12-17 15:59:43 +0000534 /** Return true if the specified rectangle, after being transformed by the
535 current matrix, would lie completely outside of the current clip. Call
536 this to check if an area you intend to draw into is clipped out (and
537 therefore you can skip making the draw calls).
538 @param rect the rect to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 @return true if the rect (transformed by the canvas' matrix) does not
540 intersect with the canvas' clip
541 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000542 bool quickReject(const SkRect& rect) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543
544 /** Return true if the specified path, after being transformed by the
545 current matrix, would lie completely outside of the current clip. Call
546 this to check if an area you intend to draw into is clipped out (and
547 therefore you can skip making the draw calls). Note, for speed it may
548 return false even if the path itself might not intersect the clip
549 (i.e. the bounds of the path intersects, but the path does not).
550 @param path The path to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551 @return true if the path (transformed by the canvas' matrix) does not
552 intersect with the canvas' clip
553 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000554 bool quickReject(const SkPath& path) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555
556 /** Return true if the horizontal band specified by top and bottom is
557 completely clipped out. This is a conservative calculation, meaning
558 that it is possible that if the method returns false, the band may still
559 in fact be clipped out, but the converse is not true. If this method
560 returns true, then the band is guaranteed to be clipped out.
561 @param top The top of the horizontal band to compare with the clip
562 @param bottom The bottom of the horizontal and to compare with the clip
563 @return true if the horizontal band is completely clipped out (i.e. does
564 not intersect the current clip)
565 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000566 bool quickRejectY(SkScalar top, SkScalar bottom) const {
reed@google.comc0784db2013-12-13 21:16:12 +0000567 SkASSERT(top <= bottom);
commit-bot@chromium.org9836bc32014-02-14 19:52:18 +0000568
569#ifndef SK_WILL_NEVER_DRAW_PERSPECTIVE_TEXT
570 // TODO: add a hasPerspective method similar to getLocalClipBounds. This
571 // would cache the SkMatrix::hasPerspective result. Alternatively, have
572 // the MC stack just set a hasPerspective boolean as it is updated.
573 if (this->getTotalMatrix().hasPerspective()) {
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +0000574 // TODO: consider implementing some half-plane test between the
commit-bot@chromium.org9836bc32014-02-14 19:52:18 +0000575 // two Y planes and the device-bounds (i.e., project the top and
576 // bottom Y planes and then determine if the clip bounds is completely
577 // outside either one).
578 return false;
579 }
580#endif
581
reed@google.comc0784db2013-12-13 21:16:12 +0000582 const SkRect& clipR = this->getLocalClipBounds();
djsollen@google.com92d2a292012-02-27 16:17:59 +0000583 // In the case where the clip is empty and we are provided with a
584 // negative top and positive bottom parameter then this test will return
585 // false even though it will be clipped. We have chosen to exclude that
586 // check as it is rare and would result double the comparisons.
reed@google.comc0784db2013-12-13 21:16:12 +0000587 return top >= clipR.fBottom || bottom <= clipR.fTop;
djsollen@google.com92d2a292012-02-27 16:17:59 +0000588 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000589
590 /** Return the bounds of the current clip (in local coordinates) in the
591 bounds parameter, and return true if it is non-empty. This can be useful
592 in a way similar to quickReject, in that it tells you that drawing
593 outside of these bounds will be clipped out.
594 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000595 virtual bool getClipBounds(SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000596
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000597 /** Return the bounds of the current clip, in device coordinates; returns
598 true if non-empty. Maybe faster than getting the clip explicitly and
599 then taking its bounds.
600 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000601 virtual bool getClipDeviceBounds(SkIRect* bounds) const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000602
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000603
reed@android.com8a1c16f2008-12-17 15:59:43 +0000604 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000605 specified ARGB color, using the specified mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606 @param a the alpha component (0..255) of the color to fill the canvas
607 @param r the red component (0..255) of the color to fill the canvas
608 @param g the green component (0..255) of the color to fill the canvas
609 @param b the blue component (0..255) of the color to fill the canvas
610 @param mode the mode to apply the color in (defaults to SrcOver)
611 */
612 void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
reed@android.com845fdac2009-06-23 03:01:32 +0000613 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000614
615 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000616 specified color and mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000617 @param color the color to draw with
618 @param mode the mode to apply the color in (defaults to SrcOver)
619 */
620 void drawColor(SkColor color,
reed@android.com845fdac2009-06-23 03:01:32 +0000621 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000622
reed@google.com2a981812011-04-14 18:59:28 +0000623 /**
624 * This erases the entire drawing surface to the specified color,
625 * irrespective of the clip. It does not blend with the previous pixels,
626 * but always overwrites them.
627 *
628 * It is roughly equivalent to the following:
629 * canvas.save();
630 * canvas.clipRect(hugeRect, kReplace_Op);
631 * paint.setColor(color);
632 * paint.setXfermodeMode(kSrc_Mode);
633 * canvas.drawPaint(paint);
634 * canvas.restore();
635 * though it is almost always much more efficient.
636 */
637 virtual void clear(SkColor);
638
639 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000640 * This makes the contents of the canvas undefined. Subsequent calls that
641 * require reading the canvas contents will produce undefined results. Examples
642 * include blending and readPixels. The actual implementation is backend-
643 * dependent and one legal implementation is to do nothing. Like clear(), this
644 * ignores the clip.
645 *
646 * This function should only be called if the caller intends to subsequently
647 * draw to the canvas. The canvas may do real work at discard() time in order
648 * to optimize performance on subsequent draws. Thus, if you call this and then
649 * never draw to the canvas subsequently you may pay a perfomance penalty.
650 */
651 void discard() { this->onDiscard(); }
652
653 /**
reed@google.com2a981812011-04-14 18:59:28 +0000654 * Fill the entire canvas' bitmap (restricted to the current clip) with the
655 * specified paint.
656 * @param paint The paint used to fill the canvas
657 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000658 virtual void drawPaint(const SkPaint& paint);
659
660 enum PointMode {
661 /** drawPoints draws each point separately */
662 kPoints_PointMode,
663 /** drawPoints draws each pair of points as a line segment */
664 kLines_PointMode,
665 /** drawPoints draws the array of points as a polygon */
666 kPolygon_PointMode
667 };
668
669 /** Draw a series of points, interpreted based on the PointMode mode. For
670 all modes, the count parameter is interpreted as the total number of
671 points. For kLine mode, count/2 line segments are drawn.
672 For kPoint mode, each point is drawn centered at its coordinate, and its
673 size is specified by the paint's stroke-width. It draws as a square,
674 unless the paint's cap-type is round, in which the points are drawn as
675 circles.
676 For kLine mode, each pair of points is drawn as a line segment,
677 respecting the paint's settings for cap/join/width.
678 For kPolygon mode, the entire array is drawn as a series of connected
679 line segments.
680 Note that, while similar, kLine and kPolygon modes draw slightly
681 differently than the equivalent path built with a series of moveto,
682 lineto calls, in that the path will draw all of its contours at once,
683 with no interactions if contours intersect each other (think XOR
684 xfermode). drawPoints always draws each element one at a time.
685 @param mode PointMode specifying how to draw the array of points.
686 @param count The number of points in the array
687 @param pts Array of points to draw
688 @param paint The paint used to draw the points
689 */
690 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
691 const SkPaint& paint);
692
693 /** Helper method for drawing a single point. See drawPoints() for a more
694 details.
695 */
696 void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000697
reed@android.com8a1c16f2008-12-17 15:59:43 +0000698 /** Draws a single pixel in the specified color.
699 @param x The X coordinate of which pixel to draw
700 @param y The Y coordiante of which pixel to draw
701 @param color The color to draw
702 */
703 void drawPoint(SkScalar x, SkScalar y, SkColor color);
704
705 /** Draw a line segment with the specified start and stop x,y coordinates,
706 using the specified paint. NOTE: since a line is always "framed", the
707 paint's Style is ignored.
708 @param x0 The x-coordinate of the start point of the line
709 @param y0 The y-coordinate of the start point of the line
710 @param x1 The x-coordinate of the end point of the line
711 @param y1 The y-coordinate of the end point of the line
712 @param paint The paint used to draw the line
713 */
714 void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
715 const SkPaint& paint);
716
717 /** Draw the specified rectangle using the specified paint. The rectangle
718 will be filled or stroked based on the Style in the paint.
719 @param rect The rect to be drawn
720 @param paint The paint used to draw the rect
721 */
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000722 virtual void drawRect(const SkRect& rect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000723
724 /** Draw the specified rectangle using the specified paint. The rectangle
725 will be filled or framed based on the Style in the paint.
726 @param rect The rect to be drawn
727 @param paint The paint used to draw the rect
728 */
reed@google.com87001ed2014-02-17 16:28:05 +0000729 void drawIRect(const SkIRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000730 SkRect r;
731 r.set(rect); // promotes the ints to scalars
732 this->drawRect(r, paint);
733 }
reed@google.com4b226022011-01-11 18:32:13 +0000734
reed@android.com8a1c16f2008-12-17 15:59:43 +0000735 /** Draw the specified rectangle using the specified paint. The rectangle
736 will be filled or framed based on the Style in the paint.
737 @param left The left side of the rectangle to be drawn
738 @param top The top side of the rectangle to be drawn
739 @param right The right side of the rectangle to be drawn
740 @param bottom The bottom side of the rectangle to be drawn
741 @param paint The paint used to draw the rect
742 */
743 void drawRectCoords(SkScalar left, SkScalar top, SkScalar right,
744 SkScalar bottom, const SkPaint& paint);
745
746 /** Draw the specified oval using the specified paint. The oval will be
747 filled or framed based on the Style in the paint.
748 @param oval The rectangle bounds of the oval to be drawn
749 @param paint The paint used to draw the oval
750 */
reed@google.com4ed0fb72012-12-12 20:48:18 +0000751 virtual void drawOval(const SkRect& oval, const SkPaint&);
752
753 /**
754 * Draw the specified RRect using the specified paint The rrect will be filled or stroked
755 * based on the Style in the paint.
756 *
757 * @param rrect The round-rect to draw
758 * @param paint The paint used to draw the round-rect
759 */
760 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000762 /**
763 * Draw the annulus formed by the outer and inner rrects. The results
764 * are undefined if the outer does not contain the inner.
765 */
766 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
767
reed@android.com8a1c16f2008-12-17 15:59:43 +0000768 /** Draw the specified circle using the specified paint. If radius is <= 0,
769 then nothing will be drawn. The circle will be filled
770 or framed based on the Style in the paint.
771 @param cx The x-coordinate of the center of the cirle to be drawn
772 @param cy The y-coordinate of the center of the cirle to be drawn
773 @param radius The radius of the cirle to be drawn
774 @param paint The paint used to draw the circle
775 */
776 void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
777 const SkPaint& paint);
778
779 /** Draw the specified arc, which will be scaled to fit inside the
780 specified oval. If the sweep angle is >= 360, then the oval is drawn
781 completely. Note that this differs slightly from SkPath::arcTo, which
782 treats the sweep angle mod 360.
783 @param oval The bounds of oval used to define the shape of the arc
784 @param startAngle Starting angle (in degrees) where the arc begins
785 @param sweepAngle Sweep angle (in degrees) measured clockwise
786 @param useCenter true means include the center of the oval. For filling
787 this will draw a wedge. False means just use the arc.
788 @param paint The paint used to draw the arc
789 */
790 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
791 bool useCenter, const SkPaint& paint);
792
793 /** Draw the specified round-rect using the specified paint. The round-rect
794 will be filled or framed based on the Style in the paint.
795 @param rect The rectangular bounds of the roundRect to be drawn
796 @param rx The x-radius of the oval used to round the corners
797 @param ry The y-radius of the oval used to round the corners
798 @param paint The paint used to draw the roundRect
799 */
800 void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry,
801 const SkPaint& paint);
802
803 /** Draw the specified path using the specified paint. The path will be
804 filled or framed based on the Style in the paint.
805 @param path The path to be drawn
806 @param paint The paint used to draw the path
807 */
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000808 virtual void drawPath(const SkPath& path, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000809
810 /** Draw the specified bitmap, with its top/left corner at (x,y), using the
811 specified paint, transformed by the current matrix. Note: if the paint
812 contains a maskfilter that generates a mask which extends beyond the
813 bitmap's original width/height, then the bitmap will be drawn as if it
814 were in a Shader with CLAMP mode. Thus the color outside of the original
815 width/height will be the edge color replicated.
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000816
817 If a shader is present on the paint it will be ignored, except in the
reed@google.comf20fc242014-03-26 13:44:58 +0000818 case where the bitmap is kAlpha_8_SkColorType. In that case, the color is
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000819 generated by the shader.
820
reed@android.com8a1c16f2008-12-17 15:59:43 +0000821 @param bitmap The bitmap to be drawn
822 @param left The position of the left side of the bitmap being drawn
823 @param top The position of the top side of the bitmap being drawn
824 @param paint The paint used to draw the bitmap, or NULL
825 */
826 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
827 const SkPaint* paint = NULL);
828
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000829 enum DrawBitmapRectFlags {
robertphillips@google.com31acc112013-08-20 12:13:48 +0000830 kNone_DrawBitmapRectFlag = 0x0,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000831 /**
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000832 * When filtering is enabled, allow the color samples outside of
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000833 * the src rect (but still in the src bitmap) to bleed into the
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000834 * drawn portion
835 */
robertphillips@google.com31acc112013-08-20 12:13:48 +0000836 kBleed_DrawBitmapRectFlag = 0x1,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000837 };
838
reed@android.com8a1c16f2008-12-17 15:59:43 +0000839 /** Draw the specified bitmap, with the specified matrix applied (before the
840 canvas' matrix is applied).
841 @param bitmap The bitmap to be drawn
842 @param src Optional: specify the subset of the bitmap to be drawn
843 @param dst The destination rectangle where the scaled/translated
844 image will be drawn
845 @param paint The paint used to draw the bitmap, or NULL
846 */
reed@google.com71121732012-09-18 15:14:33 +0000847 virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
848 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000849 const SkPaint* paint = NULL,
robertphillips@google.com31acc112013-08-20 12:13:48 +0000850 DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000851
reed@google.com71121732012-09-18 15:14:33 +0000852 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000853 const SkPaint* paint = NULL) {
robertphillips@google.com31acc112013-08-20 12:13:48 +0000854 this->drawBitmapRectToRect(bitmap, NULL, dst, paint, kNone_DrawBitmapRectFlag);
reed@google.com71121732012-09-18 15:14:33 +0000855 }
856
857 void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* isrc,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000858 const SkRect& dst, const SkPaint* paint = NULL,
robertphillips@google.com31acc112013-08-20 12:13:48 +0000859 DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) {
reed@google.com71121732012-09-18 15:14:33 +0000860 SkRect realSrcStorage;
861 SkRect* realSrcPtr = NULL;
862 if (isrc) {
863 realSrcStorage.set(*isrc);
864 realSrcPtr = &realSrcStorage;
865 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000866 this->drawBitmapRectToRect(bitmap, realSrcPtr, dst, paint, flags);
reed@google.com71121732012-09-18 15:14:33 +0000867 }
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000868
reed@android.com8a1c16f2008-12-17 15:59:43 +0000869 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
870 const SkPaint* paint = NULL);
reed@google.com4b226022011-01-11 18:32:13 +0000871
reed@google.comf0b5e112011-09-07 11:57:34 +0000872 /**
873 * Draw the bitmap stretched differentially to fit into dst.
874 * center is a rect within the bitmap, and logically divides the bitmap
875 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
876 * bitmap is the "center", then the center-rect should be [2, 2, 3, 3].
877 *
878 * If the dst is >= the bitmap size, then...
robertphillips@google.com9bf380c2013-07-25 12:10:42 +0000879 * - The 4 corners are not stretched at all.
880 * - The sides are stretched in only one axis.
881 * - The center is stretched in both axes.
reed@google.comf0b5e112011-09-07 11:57:34 +0000882 * Else, for each axis where dst < bitmap,
883 * - The corners shrink proportionally
884 * - The sides (along the shrink axis) and center are not drawn
885 */
886 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
887 const SkRect& dst, const SkPaint* paint = NULL);
888
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889 /** Draw the specified bitmap, with its top/left corner at (x,y),
890 NOT transformed by the current matrix. Note: if the paint
891 contains a maskfilter that generates a mask which extends beyond the
892 bitmap's original width/height, then the bitmap will be drawn as if it
893 were in a Shader with CLAMP mode. Thus the color outside of the original
894 width/height will be the edge color replicated.
895 @param bitmap The bitmap to be drawn
896 @param left The position of the left side of the bitmap being drawn
897 @param top The position of the top side of the bitmap being drawn
898 @param paint The paint used to draw the bitmap, or NULL
899 */
900 virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
901 const SkPaint* paint = NULL);
902
903 /** Draw the text, with origin at (x,y), using the specified paint.
904 The origin is interpreted based on the Align setting in the paint.
905 @param text The text to be drawn
906 @param byteLength The number of bytes to read from the text parameter
907 @param x The x-coordinate of the origin of the text being drawn
908 @param y The y-coordinate of the origin of the text being drawn
909 @param paint The paint used for the text (e.g. color, size, style)
910 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000911 SK_LEGACY_DRAWTEXT_VIRTUAL void drawText(const void* text, size_t byteLength, SkScalar x,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000912 SkScalar y, const SkPaint& paint);
913
914 /** Draw the text, with each character/glyph origin specified by the pos[]
reed@google.com4b226022011-01-11 18:32:13 +0000915 array. The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000916 @param text The text to be drawn
917 @param byteLength The number of bytes to read from the text parameter
918 @param pos Array of positions, used to position each character
919 @param paint The paint used for the text (e.g. color, size, style)
920 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000921 SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosText(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000922 const SkPoint pos[], const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000923
reed@android.com8a1c16f2008-12-17 15:59:43 +0000924 /** Draw the text, with each character/glyph origin specified by the x
925 coordinate taken from the xpos[] array, and the y from the constY param.
reed@google.com4b226022011-01-11 18:32:13 +0000926 The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000927 @param text The text to be drawn
928 @param byteLength The number of bytes to read from the text parameter
929 @param xpos Array of x-positions, used to position each character
930 @param constY The shared Y coordinate for all of the positions
931 @param paint The paint used for the text (e.g. color, size, style)
932 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000933 SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosTextH(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000934 const SkScalar xpos[], SkScalar constY,
935 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000936
reed@android.com8a1c16f2008-12-17 15:59:43 +0000937 /** 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 hOffset The distance along the path to add to the text's
944 starting position
945 @param vOffset The distance above(-) or below(+) the path to
946 position the text
947 @param paint The paint used for the text
948 */
949 void drawTextOnPathHV(const void* text, size_t byteLength,
950 const SkPath& path, SkScalar hOffset,
951 SkScalar vOffset, const SkPaint& paint);
952
953 /** Draw the text, with origin at (x,y), using the specified paint, along
954 the specified path. The paint's Align setting determins where along the
955 path to start the text.
956 @param text The text to be drawn
957 @param byteLength The number of bytes to read from the text parameter
958 @param path The path the text should follow for its baseline
959 @param matrix (may be null) Applied to the text before it is
960 mapped onto the path
961 @param paint The paint used for the text
962 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000963 SK_LEGACY_DRAWTEXT_VIRTUAL void drawTextOnPath(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000964 const SkPath& path, const SkMatrix* matrix,
965 const SkPaint& paint);
966
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000967 /** PRIVATE / EXPERIMENTAL -- do not call
968 Perform back-end analysis/optimization of a picture. This may attach
969 optimization data to the picture which can be used by a later
970 drawPicture call.
971 @param picture The recorded drawing commands to analyze/optimize
972 */
robertphillips9b14f262014-06-04 05:40:44 -0700973 void EXPERIMENTAL_optimize(const SkPicture* picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000974
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +0000975 /** PRIVATE / EXPERIMENTAL -- do not call
976 Purge all the discardable optimization information associated with
977 'picture'. If NULL is passed in, purge all discardable information.
978 */
robertphillips9b14f262014-06-04 05:40:44 -0700979 void EXPERIMENTAL_purge(const SkPicture* picture);
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +0000980
reed@android.com8a1c16f2008-12-17 15:59:43 +0000981 /** Draw the picture into this canvas. This method effective brackets the
982 playback of the picture's draw calls with save/restore, so the state
djsollen@google.coma44de962013-01-02 16:59:19 +0000983 of this canvas will be unchanged after this call.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000984 @param picture The recorded drawing commands to playback into this
985 canvas.
986 */
robertphillips9b14f262014-06-04 05:40:44 -0700987 void drawPicture(const SkPicture* picture);
988
reed@android.com8a1c16f2008-12-17 15:59:43 +0000989 enum VertexMode {
990 kTriangles_VertexMode,
991 kTriangleStrip_VertexMode,
992 kTriangleFan_VertexMode
993 };
reed@google.com4b226022011-01-11 18:32:13 +0000994
reed@android.com8a1c16f2008-12-17 15:59:43 +0000995 /** Draw the array of vertices, interpreted as triangles (based on mode).
commit-bot@chromium.org559a8832014-05-30 10:08:22 +0000996
997 If both textures and vertex-colors are NULL, it strokes hairlines with
998 the paint's color. This behavior is a useful debugging mode to visualize
999 the mesh.
1000
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001 @param vmode How to interpret the array of vertices
1002 @param vertexCount The number of points in the vertices array (and
1003 corresponding texs and colors arrays if non-null)
1004 @param vertices Array of vertices for the mesh
1005 @param texs May be null. If not null, specifies the coordinate
robertphillips@google.com631a59b2013-07-31 14:57:53 +00001006 in _texture_ space (not uv space) for each vertex.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001007 @param colors May be null. If not null, specifies a color for each
1008 vertex, to be interpolated across the triangle.
1009 @param xmode Used if both texs and colors are present. In this
1010 case the colors are combined with the texture using mode,
1011 before being drawn using the paint. If mode is null, then
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001012 kModulate_Mode is used.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001013 @param indices If not null, array of indices to reference into the
1014 vertex (texs, colors) array.
1015 @param indexCount number of entries in the indices array (if not null)
reed@google.com4b226022011-01-11 18:32:13 +00001016 @param paint Specifies the shader/texture if present.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017 */
1018 virtual void drawVertices(VertexMode vmode, int vertexCount,
1019 const SkPoint vertices[], const SkPoint texs[],
1020 const SkColor colors[], SkXfermode* xmode,
1021 const uint16_t indices[], int indexCount,
1022 const SkPaint& paint);
1023
reed@android.comcb608442009-12-04 21:32:27 +00001024 /** Send a blob of data to the canvas.
1025 For canvases that draw, this call is effectively a no-op, as the data
1026 is not parsed, but just ignored. However, this call exists for
1027 subclasses like SkPicture's recording canvas, that can store the data
1028 and then play it back later (via another call to drawData).
1029 */
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001030 virtual void drawData(const void* data, size_t length) {
1031 // do nothing. Subclasses may do something with the data
1032 }
1033
skia.committer@gmail.coma5d3e772013-05-30 07:01:29 +00001034 /** Add comments. beginCommentGroup/endCommentGroup open/close a new group.
1035 Each comment added via addComment is notionally attached to its
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001036 enclosing group. Top-level comments simply belong to no group.
1037 */
1038 virtual void beginCommentGroup(const char* description) {
1039 // do nothing. Subclasses may do something
1040 }
1041 virtual void addComment(const char* kywd, const char* value) {
1042 // do nothing. Subclasses may do something
1043 }
1044 virtual void endCommentGroup() {
1045 // do nothing. Subclasses may do something
1046 }
1047
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001048 /**
1049 * With this call the client asserts that subsequent draw operations (up to the
1050 * matching popCull()) are fully contained within the given bounding box. The assertion
1051 * is not enforced, but the information might be used to quick-reject command blocks,
1052 * so an incorrect bounding box may result in incomplete rendering.
1053 */
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001054 void pushCull(const SkRect& cullRect);
reed@android.comcb608442009-12-04 21:32:27 +00001055
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001056 /**
1057 * Terminates the current culling block, and restores the previous one (if any).
1058 */
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001059 void popCull();
1060
reed@android.com8a1c16f2008-12-17 15:59:43 +00001061 //////////////////////////////////////////////////////////////////////////
reed@google.com4b226022011-01-11 18:32:13 +00001062
reed@android.com8a1c16f2008-12-17 15:59:43 +00001063 /** Get the current filter object. The filter's reference count is not
reed@android.comdc3381f2010-02-11 16:05:15 +00001064 affected. The filter is saved/restored, just like the matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001065 @return the canvas' filter (or NULL).
1066 */
1067 SkDrawFilter* getDrawFilter() const;
reed@google.com4b226022011-01-11 18:32:13 +00001068
reed@android.com8a1c16f2008-12-17 15:59:43 +00001069 /** Set the new filter (or NULL). Pass NULL to clear any existing filter.
1070 As a convenience, the parameter is returned. If an existing filter
1071 exists, its refcnt is decrement. If the new filter is not null, its
reed@android.comdc3381f2010-02-11 16:05:15 +00001072 refcnt is incremented. The filter is saved/restored, just like the
1073 matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001074 @param filter the new filter (or NULL)
1075 @return the new filter
1076 */
1077 virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
1078
1079 //////////////////////////////////////////////////////////////////////////
1080
reed@google.com754de5f2014-02-24 19:38:20 +00001081 /**
1082 * Return true if the current clip is empty (i.e. nothing will draw).
1083 * Note: this is not always a free call, so it should not be used
1084 * more often than necessary. However, once the canvas has computed this
1085 * result, subsequent calls will be cheap (until the clip state changes,
1086 * which can happen on any clip..() or restore() call.
1087 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001088 virtual bool isClipEmpty() const;
reed@google.com754de5f2014-02-24 19:38:20 +00001089
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001090 /**
1091 * Returns true if the current clip is just a (non-empty) rectangle.
1092 * Returns false if the clip is empty, or if it is complex.
1093 */
1094 virtual bool isClipRect() const;
1095
reed@android.com8a1c16f2008-12-17 15:59:43 +00001096 /** Return the current matrix on the canvas.
1097 This does not account for the translate in any of the devices.
1098 @return The current matrix on the canvas.
1099 */
junov@chromium.orga907ac32012-02-24 21:54:07 +00001100 const SkMatrix& getTotalMatrix() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001101
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001102#ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001103 enum ClipType {
1104 kEmpty_ClipType = 0,
1105 kRect_ClipType,
1106 kComplex_ClipType
1107 };
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001108 /** Returns a description of the total clip; may be cheaper than
1109 getting the clip and querying it directly.
1110 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001111 virtual ClipType getClipType() const;
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001112#endif
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001113
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001114 /** Return the clip stack. The clip stack stores all the individual
1115 * clips organized by the save/restore frame in which they were
1116 * added.
1117 * @return the current clip stack ("list" of individual clip elements)
1118 */
1119 const SkClipStack* getClipStack() const {
1120 return &fClipStack;
1121 }
1122
fmalitac3b589a2014-06-05 12:40:07 -07001123 typedef SkCanvasClipVisitor ClipVisitor;
reed@google.com90c07ea2012-04-13 13:50:27 +00001124 /**
1125 * Replays the clip operations, back to front, that have been applied to
1126 * the canvas, calling the appropriate method on the visitor for each
1127 * clip. All clips have already been transformed into device space.
1128 */
1129 void replayClips(ClipVisitor*) const;
1130
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131 ///////////////////////////////////////////////////////////////////////////
1132
1133 /** After calling saveLayer(), there can be any number of devices that make
1134 up the top-most drawing area. LayerIter can be used to iterate through
1135 those devices. Note that the iterator is only valid until the next API
1136 call made on the canvas. Ownership of all pointers in the iterator stays
1137 with the canvas, so none of them should be modified or deleted.
1138 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +00001139 class SK_API LayerIter /*: SkNoncopyable*/ {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001140 public:
1141 /** Initialize iterator with canvas, and set values for 1st device */
1142 LayerIter(SkCanvas*, bool skipEmptyClips);
1143 ~LayerIter();
reed@google.com4b226022011-01-11 18:32:13 +00001144
reed@android.com8a1c16f2008-12-17 15:59:43 +00001145 /** Return true if the iterator is done */
1146 bool done() const { return fDone; }
1147 /** Cycle to the next device */
1148 void next();
reed@google.com4b226022011-01-11 18:32:13 +00001149
reed@android.com8a1c16f2008-12-17 15:59:43 +00001150 // These reflect the current device in the iterator
1151
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001152 SkBaseDevice* device() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001153 const SkMatrix& matrix() const;
1154 const SkRegion& clip() const;
1155 const SkPaint& paint() const;
1156 int x() const;
1157 int y() const;
reed@google.com4b226022011-01-11 18:32:13 +00001158
reed@android.com8a1c16f2008-12-17 15:59:43 +00001159 private:
1160 // used to embed the SkDrawIter object directly in our instance, w/o
1161 // having to expose that class def to the public. There is an assert
1162 // in our constructor to ensure that fStorage is large enough
1163 // (though needs to be a compile-time-assert!). We use intptr_t to work
1164 // safely with 32 and 64 bit machines (to ensure the storage is enough)
reed@android.comf2b98d62010-12-20 18:26:13 +00001165 intptr_t fStorage[32];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001166 class SkDrawIter* fImpl; // this points at fStorage
1167 SkPaint fDefaultPaint;
1168 bool fDone;
1169 };
1170
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001171 // don't call
1172 const SkRegion& internal_private_getTotalClip() const;
1173 // don't call
1174 void internal_private_getTotalClipAsPath(SkPath*) const;
reed@google.com9c135db2014-03-12 18:28:35 +00001175 // don't call
1176 GrRenderTarget* internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001177
reed@android.com8a1c16f2008-12-17 15:59:43 +00001178protected:
reed@google.com76f10a32014-02-05 15:32:21 +00001179 // default impl defers to getDevice()->newSurface(info)
1180 virtual SkSurface* onNewSurface(const SkImageInfo&);
1181
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001182 // default impl defers to its device
1183 virtual const void* onPeekPixels(SkImageInfo*, size_t* rowBytes);
reed@google.com9c135db2014-03-12 18:28:35 +00001184 virtual void* onAccessTopLayerPixels(SkImageInfo*, size_t* rowBytes);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001185
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001186 // Subclass save/restore notifiers.
1187 // Overriders should call the corresponding INHERITED method up the inheritance chain.
1188 // willSaveLayer()'s return value may suppress full layer allocation.
1189 enum SaveLayerStrategy {
1190 kFullLayer_SaveLayerStrategy,
1191 kNoLayer_SaveLayerStrategy
1192 };
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001193
fmalita07fc6622014-06-16 12:13:39 -07001194 // Transitional, pending external clients cleanup.
fmalita6ca763f2014-06-17 13:52:18 -07001195 virtual void willSave(SaveFlags) { this->willSave(); }
fmalita07fc6622014-06-16 12:13:39 -07001196
fmalita6ca763f2014-06-17 13:52:18 -07001197 virtual void willSave() {}
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001198 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) {
1199 return kFullLayer_SaveLayerStrategy;
1200 }
1201 virtual void willRestore() {}
1202 virtual void didConcat(const SkMatrix&) {}
1203 virtual void didSetMatrix(const SkMatrix&) {}
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +00001204
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +00001205 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
1206
reed@google.come0d9ce82014-04-23 04:00:17 +00001207 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1208 SkScalar y, const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001209
reed@google.come0d9ce82014-04-23 04:00:17 +00001210 virtual void onDrawPosText(const void* text, size_t byteLength,
1211 const SkPoint pos[], const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001212
reed@google.come0d9ce82014-04-23 04:00:17 +00001213 virtual void onDrawPosTextH(const void* text, size_t byteLength,
1214 const SkScalar xpos[], SkScalar constY,
1215 const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001216
reed@google.come0d9ce82014-04-23 04:00:17 +00001217 virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1218 const SkPath& path, const SkMatrix* matrix,
1219 const SkPaint& paint);
1220
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001221 enum ClipEdgeStyle {
1222 kHard_ClipEdgeStyle,
1223 kSoft_ClipEdgeStyle
1224 };
1225
1226 virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1227 virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1228 virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1229 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op);
1230
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001231 virtual void onDiscard();
1232
robertphillips9b14f262014-06-04 05:40:44 -07001233 virtual void onDrawPicture(const SkPicture* picture);
1234
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001235 // Returns the canvas to be used by DrawIter. Default implementation
junov@google.com4370aed2012-01-18 16:21:08 +00001236 // returns this. Subclasses that encapsulate an indirect canvas may
1237 // need to overload this method. The impl must keep track of this, as it
1238 // is not released or deleted by the caller.
1239 virtual SkCanvas* canvasForDrawIter();
1240
junov@chromium.orga907ac32012-02-24 21:54:07 +00001241 // Clip rectangle bounds. Called internally by saveLayer.
1242 // returns false if the entire rectangle is entirely clipped out
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001243 // If non-NULL, The imageFilter parameter will be used to expand the clip
1244 // and offscreen bounds for any margin required by the filter DAG.
junov@chromium.orga907ac32012-02-24 21:54:07 +00001245 bool clipRectBounds(const SkRect* bounds, SaveFlags flags,
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001246 SkIRect* intersection,
1247 const SkImageFilter* imageFilter = NULL);
junov@chromium.orga907ac32012-02-24 21:54:07 +00001248
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001249 // Called by child classes that override clipPath and clipRRect to only
1250 // track fast conservative clip bounds, rather than exact clips.
commit-bot@chromium.org759cf482014-03-06 13:18:07 +00001251 void updateClipConservativelyUsingBounds(const SkRect&, SkRegion::Op,
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001252 bool inverseFilled);
1253
reed@google.com97af1a62012-08-28 12:19:02 +00001254 // notify our surface (if we have one) that we are about to draw, so it
1255 // can perform copy-on-write or invalidate any cached images
1256 void predrawNotify();
1257
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001258 virtual void onPushCull(const SkRect& cullRect);
1259 virtual void onPopCull();
1260
reed@android.com8a1c16f2008-12-17 15:59:43 +00001261private:
1262 class MCRec;
1263
reed@google.com5c3d1472011-02-22 19:12:23 +00001264 SkClipStack fClipStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001265 SkDeque fMCStack;
1266 // points to top of stack
1267 MCRec* fMCRec;
1268 // the first N recs that can fit here mean we won't call malloc
1269 uint32_t fMCRecStorage[32];
1270
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +00001271 int fSaveLayerCount; // number of successful saveLayer calls
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001272 int fCullCount; // number of active culls
reed@android.com8a1c16f2008-12-17 15:59:43 +00001273
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +00001274 SkMetaData* fMetaData;
1275
reed@google.com97af1a62012-08-28 12:19:02 +00001276 SkSurface_Base* fSurfaceBase;
1277 SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1278 void setSurfaceBase(SkSurface_Base* sb) {
1279 fSurfaceBase = sb;
1280 }
1281 friend class SkSurface_Base;
junov@chromium.org45c3db82013-04-11 17:52:05 +00001282 friend class SkSurface_Gpu;
skia.committer@gmail.comfc843592012-10-11 02:01:14 +00001283
reed@android.com8a1c16f2008-12-17 15:59:43 +00001284 bool fDeviceCMDirty; // cleared by updateDeviceCMCache()
1285 void updateDeviceCMCache();
1286
reed@google.com9c135db2014-03-12 18:28:35 +00001287 friend class SkDrawIter; // needs setupDrawForLayerDevice()
reed@google.com8926b162012-03-23 15:36:36 +00001288 friend class AutoDrawLooper;
reed@google.com9c135db2014-03-12 18:28:35 +00001289 friend class SkLua; // needs top layer size and offset
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +00001290 friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
reed@google.com9c135db2014-03-12 18:28:35 +00001291 friend class SkDeferredDevice; // needs getTopDevice()
reed@android.com8a1c16f2008-12-17 15:59:43 +00001292
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001293 SkBaseDevice* createLayerDevice(const SkImageInfo&);
bsalomon@google.come97f0852011-06-17 13:10:25 +00001294
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001295 SkBaseDevice* init(SkBaseDevice*);
reed@google.comf0b5e112011-09-07 11:57:34 +00001296
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001297 /**
1298 * DEPRECATED
1299 *
1300 * Specify a device for this canvas to draw into. If it is not null, its
1301 * reference count is incremented. If the canvas was already holding a
1302 * device, its reference count is decremented. The new device is returned.
1303 */
1304 SkBaseDevice* setRootDevice(SkBaseDevice* device);
skia.committer@gmail.com31acdea2014-02-18 03:01:51 +00001305
bsalomon@google.com4ebe3822014-02-26 20:22:32 +00001306 /**
1307 * Gets the size/origin of the top level layer in global canvas coordinates. We don't want this
1308 * to be public because it exposes decisions about layer sizes that are internal to the canvas.
1309 */
1310 SkISize getTopLayerSize() const;
1311 SkIPoint getTopLayerOrigin() const;
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001312
reed@google.comf0b5e112011-09-07 11:57:34 +00001313 // internal methods are not virtual, so they can safely be called by other
1314 // canvas apis, without confusing subclasses (like SkPictureRecording)
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001315 void internalDrawBitmap(const SkBitmap&, const SkMatrix& m, const SkPaint* paint);
reed@google.com71121732012-09-18 15:14:33 +00001316 void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001317 const SkRect& dst, const SkPaint* paint,
1318 DrawBitmapRectFlags flags);
reed@google.comf0b5e112011-09-07 11:57:34 +00001319 void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
1320 const SkRect& dst, const SkPaint* paint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001321 void internalDrawPaint(const SkPaint& paint);
reed@google.com8926b162012-03-23 15:36:36 +00001322 int internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001323 SaveFlags, bool justForImageFilter, SaveLayerStrategy strategy);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001324 void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001325
reed@android.com8a1c16f2008-12-17 15:59:43 +00001326 // shared by save() and saveLayer()
1327 int internalSave(SaveFlags flags);
1328 void internalRestore();
bungeman@google.com52c748b2011-08-22 21:30:43 +00001329 static void DrawRect(const SkDraw& draw, const SkPaint& paint,
1330 const SkRect& r, SkScalar textSize);
1331 static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
1332 const char text[], size_t byteLength,
1333 SkScalar x, SkScalar y);
reed@google.com4b226022011-01-11 18:32:13 +00001334
reed@android.com8a1c16f2008-12-17 15:59:43 +00001335 /* These maintain a cache of the clip bounds in local coordinates,
1336 (converted to 2s-compliment if floats are slow).
1337 */
reed@google.comc0784db2013-12-13 21:16:12 +00001338 mutable SkRect fCachedLocalClipBounds;
1339 mutable bool fCachedLocalClipBoundsDirty;
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001340 bool fAllowSoftClip;
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001341 bool fAllowSimplifyClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001342
reed@google.comc0784db2013-12-13 21:16:12 +00001343 const SkRect& getLocalClipBounds() const {
1344 if (fCachedLocalClipBoundsDirty) {
1345 if (!this->getClipBounds(&fCachedLocalClipBounds)) {
1346 fCachedLocalClipBounds.setEmpty();
1347 }
1348 fCachedLocalClipBoundsDirty = false;
reed@android.comba09de42010-02-05 20:46:05 +00001349 }
reed@google.comc0784db2013-12-13 21:16:12 +00001350 return fCachedLocalClipBounds;
reed@android.comba09de42010-02-05 20:46:05 +00001351 }
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001352
reed@google.com5c3d1472011-02-22 19:12:23 +00001353 class AutoValidateClip : ::SkNoncopyable {
1354 public:
1355 explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1356 fCanvas->validateClip();
1357 }
1358 ~AutoValidateClip() { fCanvas->validateClip(); }
1359
1360 private:
1361 const SkCanvas* fCanvas;
1362 };
1363
1364#ifdef SK_DEBUG
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001365 // The cull stack rects are in device-space
1366 SkTDArray<SkIRect> fCullStack;
1367 void validateCull(const SkIRect&);
reed@google.com5c3d1472011-02-22 19:12:23 +00001368 void validateClip() const;
1369#else
1370 void validateClip() const {}
1371#endif
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +00001372
1373 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001374};
1375
1376/** Stack helper class to automatically call restoreToCount() on the canvas
1377 when this object goes out of scope. Use this to guarantee that the canvas
1378 is restored to a known state.
1379*/
1380class SkAutoCanvasRestore : SkNoncopyable {
1381public:
commit-bot@chromium.org28871192013-10-14 15:28:01 +00001382 SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1383 if (fCanvas) {
1384 fSaveCount = canvas->getSaveCount();
1385 if (doSave) {
1386 canvas->save();
1387 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001388 }
1389 }
1390 ~SkAutoCanvasRestore() {
reed@google.comf6c9a5b2012-11-20 15:12:21 +00001391 if (fCanvas) {
1392 fCanvas->restoreToCount(fSaveCount);
1393 }
1394 }
1395
1396 /**
1397 * Perform the restore now, instead of waiting for the destructor. Will
1398 * only do this once.
1399 */
1400 void restore() {
1401 if (fCanvas) {
1402 fCanvas->restoreToCount(fSaveCount);
1403 fCanvas = NULL;
1404 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001405 }
1406
1407private:
1408 SkCanvas* fCanvas;
1409 int fSaveCount;
1410};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001411#define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001412
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001413/** Stack helper class to automatically open and close a comment block
1414 */
1415class SkAutoCommentBlock : SkNoncopyable {
1416public:
1417 SkAutoCommentBlock(SkCanvas* canvas, const char* description) {
1418 fCanvas = canvas;
1419 if (NULL != fCanvas) {
1420 fCanvas->beginCommentGroup(description);
1421 }
1422 }
1423
1424 ~SkAutoCommentBlock() {
1425 if (NULL != fCanvas) {
1426 fCanvas->endCommentGroup();
1427 }
1428 }
1429
1430private:
1431 SkCanvas* fCanvas;
1432};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001433#define SkAutoCommentBlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoCommentBlock)
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001434
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001435/**
1436 * If the caller wants read-only access to the pixels in a canvas, it can just
1437 * call canvas->peekPixels(), since that is the fastest way to "peek" at the
1438 * pixels on a raster-backed canvas.
1439 *
1440 * If the canvas has pixels, but they are not readily available to the CPU
1441 * (e.g. gpu-backed), then peekPixels() will fail, but readPixels() will
1442 * succeed (though be slower, since it will return a copy of the pixels).
1443 *
1444 * SkAutoROCanvasPixels encapsulates these two techniques, trying first to call
1445 * peekPixels() (for performance), but if that fails, calling readPixels() and
1446 * storing the copy locally.
1447 *
1448 * The caller must respect the restrictions associated with peekPixels(), since
1449 * that may have been called: The returned information is invalidated if...
1450 * - any API is called on the canvas (or its parent surface if present)
1451 * - the canvas goes out of scope
1452 */
1453class SkAutoROCanvasPixels : SkNoncopyable {
1454public:
1455 SkAutoROCanvasPixels(SkCanvas* canvas);
1456
1457 // returns NULL on failure
1458 const void* addr() const { return fAddr; }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +00001459
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001460 // undefined if addr() == NULL
1461 size_t rowBytes() const { return fRowBytes; }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +00001462
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001463 // undefined if addr() == NULL
1464 const SkImageInfo& info() const { return fInfo; }
1465
1466 // helper that, if returns true, installs the pixels into the bitmap. Note
1467 // that the bitmap may reference the address returned by peekPixels(), so
1468 // the caller must respect the restrictions associated with peekPixels().
1469 bool asROBitmap(SkBitmap*) const;
1470
1471private:
1472 SkBitmap fBitmap; // used if peekPixels() fails
1473 const void* fAddr; // NULL on failure
1474 SkImageInfo fInfo;
1475 size_t fRowBytes;
1476};
1477
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001478static inline SkCanvas::SaveFlags operator|(const SkCanvas::SaveFlags lhs,
1479 const SkCanvas::SaveFlags rhs) {
commit-bot@chromium.org92a89162014-05-30 21:07:05 +00001480 return static_cast<SkCanvas::SaveFlags>(static_cast<int>(lhs) | static_cast<int>(rhs));
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001481}
1482
1483static inline SkCanvas::SaveFlags& operator|=(SkCanvas::SaveFlags& lhs,
1484 const SkCanvas::SaveFlags rhs) {
1485 lhs = lhs | rhs;
1486 return lhs;
1487}
1488
fmalitac3b589a2014-06-05 12:40:07 -07001489class SkCanvasClipVisitor {
1490public:
1491 virtual ~SkCanvasClipVisitor();
1492 virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0;
1493 virtual void clipRRect(const SkRRect&, SkRegion::Op, bool antialias) = 0;
1494 virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0;
1495};
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001496
reed@android.com8a1c16f2008-12-17 15:59:43 +00001497#endif