blob: 77038c3067b2803bda471e5d8e6d9daafbf907c4 [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"
reed3716fd02014-09-21 09:39:55 -070019#include "SkSurfaceProps.h"
reed@android.com845fdac2009-06-23 03:01:32 +000020#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
reed@google.come0d9ce82014-04-23 04:00:17 +000022#ifdef SK_SUPPORT_LEGACY_DRAWTEXT_VIRTUAL
23 #define SK_LEGACY_DRAWTEXT_VIRTUAL virtual
24#else
25 #define SK_LEGACY_DRAWTEXT_VIRTUAL
26#endif
27
fmalitac3b589a2014-06-05 12:40:07 -070028class SkCanvasClipVisitor;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000029class SkBaseDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030class SkDraw;
31class SkDrawFilter;
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +000032class SkMetaData;
reed@android.com8a1c16f2008-12-17 15:59:43 +000033class SkPicture;
reed@google.com4ed0fb72012-12-12 20:48:18 +000034class SkRRect;
reed@google.com76f10a32014-02-05 15:32:21 +000035class SkSurface;
reed@google.com97af1a62012-08-28 12:19:02 +000036class SkSurface_Base;
fmalita00d5c2c2014-08-21 08:53:26 -070037class SkTextBlob;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038class GrContext;
reed@google.com9c135db2014-03-12 18:28:35 +000039class GrRenderTarget;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
reedfa35f8e2014-09-04 12:14:36 -070041class SkCanvasState;
42
43namespace SkCanvasStateUtils {
reed8f2e7912014-09-04 12:45:18 -070044 SK_API SkCanvasState* CaptureCanvasState(SkCanvas*);
reedfa35f8e2014-09-04 12:14:36 -070045}
46
reed@android.com8a1c16f2008-12-17 15:59:43 +000047/** \class SkCanvas
48
49 A Canvas encapsulates all of the state about drawing into a device (bitmap).
50 This includes a reference to the device itself, and a stack of matrix/clip
51 values. For any given draw call (e.g. drawRect), the geometry of the object
52 being drawn is transformed by the concatenation of all the matrices in the
53 stack. The transformed geometry is clipped by the intersection of all of
54 the clips in the stack.
55
56 While the Canvas holds the state of the drawing device, the state (style)
57 of the object being drawn is held by the Paint, which is provided as a
58 parameter to each of the draw() methods. The Paint holds attributes such as
59 color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns),
60 etc.
61*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000062class SK_API SkCanvas : public SkRefCnt {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000064 SK_DECLARE_INST_COUNT(SkCanvas)
65
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000066 /**
commit-bot@chromium.org3107b6a2014-02-27 20:32:51 +000067 * Attempt to allocate an offscreen raster canvas, matching the ImageInfo.
68 * On success, return a new canvas that will draw into that offscreen.
69 *
70 * The caller can access the pixels after drawing into this canvas by
71 * calling readPixels() or peekPixels().
72 *
73 * If the requested ImageInfo is opaque (either the colortype is
74 * intrinsically opaque like RGB_565, or the info's alphatype is kOpaque)
75 * then the pixel memory may be uninitialized. Otherwise, the pixel memory
76 * will be initialized to 0, which is interpreted as transparent.
77 *
78 * On failure, return NULL. This can fail for several reasons:
79 * 1. the memory allocation failed (e.g. request is too large)
80 * 2. invalid ImageInfo (e.g. negative dimensions)
81 * 3. unsupported ImageInfo for a canvas
82 * - kUnknown_SkColorType, kIndex_8_SkColorType
83 * - kIgnore_SkAlphaType
84 * - this list is not complete, so others may also be unsupported
85 *
86 * Note: it is valid to request a supported ImageInfo, but with zero
87 * dimensions.
88 */
89 static SkCanvas* NewRaster(const SkImageInfo&);
90
91 static SkCanvas* NewRasterN32(int width, int height) {
92 return NewRaster(SkImageInfo::MakeN32Premul(width, height));
93 }
94
95 /**
commit-bot@chromium.org42b08932014-03-17 02:13:07 +000096 * Attempt to allocate raster canvas, matching the ImageInfo, that will draw directly into the
97 * specified pixels. To access the pixels after drawing to them, the caller should call
98 * flush() or call peekPixels(...).
99 *
100 * On failure, return NULL. This can fail for several reasons:
101 * 1. invalid ImageInfo (e.g. negative dimensions)
102 * 2. unsupported ImageInfo for a canvas
103 * - kUnknown_SkColorType, kIndex_8_SkColorType
104 * - kIgnore_SkAlphaType
105 * - this list is not complete, so others may also be unsupported
106 *
107 * Note: it is valid to request a supported ImageInfo, but with zero
108 * dimensions.
109 */
110 static SkCanvas* NewRasterDirect(const SkImageInfo&, void*, size_t);
111
112 static SkCanvas* NewRasterDirectN32(int width, int height, SkPMColor* pixels, size_t rowBytes) {
113 return NewRasterDirect(SkImageInfo::MakeN32Premul(width, height), pixels, rowBytes);
114 }
115
116 /**
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000117 * Creates an empty canvas with no backing device/pixels, and zero
118 * dimensions.
119 */
reed@google.comcde92112011-07-06 20:00:52 +0000120 SkCanvas();
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000121
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000122 /**
123 * Creates a canvas of the specified dimensions, but explicitly not backed
124 * by any device/pixels. Typically this use used by subclasses who handle
125 * the draw calls in some other way.
126 */
127 SkCanvas(int width, int height);
128
reed@google.com6dc74552011-07-21 18:00:46 +0000129 /** Construct a canvas with the specified device to draw into.
bsalomon@google.come97f0852011-06-17 13:10:25 +0000130
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000131 @param device Specifies a device for the canvas to draw into.
132 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000133 explicit SkCanvas(SkBaseDevice* device);
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000134
reed@google.com44699382013-10-31 17:28:30 +0000135 /** Construct a canvas with the specified bitmap to draw into.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 @param bitmap Specifies a bitmap for the canvas to draw into. Its
137 structure are copied to the canvas.
138 */
139 explicit SkCanvas(const SkBitmap& bitmap);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 virtual ~SkCanvas();
141
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +0000142 SkMetaData& getMetaData();
143
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000144 /**
145 * Return ImageInfo for this canvas. If the canvas is not backed by pixels
146 * (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
147 */
148 SkImageInfo imageInfo() const;
149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 ///////////////////////////////////////////////////////////////////////////
151
reed@google.com210ce002011-11-01 14:24:23 +0000152 /**
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000153 * Trigger the immediate execution of all pending draw operations.
154 */
155 void flush();
156
157 /**
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000158 * Gets the size of the base or root layer in global canvas coordinates. The
159 * origin of the base layer is always (0,0). The current drawable area may be
160 * smaller (due to clipping or saveLayer).
reed@google.com210ce002011-11-01 14:24:23 +0000161 */
bsalomon@google.com4ebe3822014-02-26 20:22:32 +0000162 SkISize getBaseLayerSize() const;
163
164 /**
165 * DEPRECATED: call getBaseLayerSize
166 */
167 SkISize getDeviceSize() const { return this->getBaseLayerSize(); }
reed@google.com210ce002011-11-01 14:24:23 +0000168
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +0000169 /**
170 * DEPRECATED.
171 * Return the canvas' device object, which may be null. The device holds
172 * the bitmap of the pixels that the canvas draws into. The reference count
173 * of the returned device is not changed by this call.
174 */
reed52d9ac62014-06-30 09:05:34 -0700175#ifndef SK_SUPPORT_LEGACY_GETDEVICE
176protected: // Can we make this private?
177#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000178 SkBaseDevice* getDevice() const;
reed52d9ac62014-06-30 09:05:34 -0700179public:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180
reed@google.com9266fed2011-03-30 00:18:03 +0000181 /**
182 * saveLayer() can create another device (which is later drawn onto
183 * the previous device). getTopDevice() returns the top-most device current
184 * installed. Note that this can change on other calls like save/restore,
185 * so do not access this device after subsequent canvas calls.
186 * The reference count of the device is not changed.
reed@google.com0b53d592012-03-19 18:26:34 +0000187 *
188 * @param updateMatrixClip If this is true, then before the device is
189 * returned, we ensure that its has been notified about the current
190 * matrix and clip. Note: this happens automatically when the device
191 * is drawn to, but is optional here, as there is a small perf hit
192 * sometimes.
reed@google.com9266fed2011-03-30 00:18:03 +0000193 */
reed@google.com9c135db2014-03-12 18:28:35 +0000194#ifndef SK_SUPPORT_LEGACY_GETTOPDEVICE
195private:
196#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000197 SkBaseDevice* getTopDevice(bool updateMatrixClip = false) const;
reed@google.com9c135db2014-03-12 18:28:35 +0000198public:
reed@google.com9266fed2011-03-30 00:18:03 +0000199
reed@google.com76f10a32014-02-05 15:32:21 +0000200 /**
201 * Create a new surface matching the specified info, one that attempts to
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000202 * be maximally compatible when used with this canvas. If there is no matching Surface type,
203 * NULL is returned.
reed3716fd02014-09-21 09:39:55 -0700204 *
205 * If surfaceprops is specified, those are passed to the new surface, otherwise the new surface
206 * inherits the properties of the surface that owns this canvas. If this canvas has no parent
207 * surface, then the new surface is created with default properties.
reed@google.com76f10a32014-02-05 15:32:21 +0000208 */
reed3716fd02014-09-21 09:39:55 -0700209 SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps* = NULL);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000210
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000211 /**
212 * Return the GPU context of the device that is associated with the canvas.
213 * For a canvas with non-GPU device, NULL is returned.
214 */
215 GrContext* getGrContext();
216
reed@google.com4b226022011-01-11 18:32:13 +0000217 ///////////////////////////////////////////////////////////////////////////
218
bsalomon@google.comdaba14b2011-11-02 20:10:48 +0000219 /**
reed@google.com9c135db2014-03-12 18:28:35 +0000220 * If the canvas has writable pixels in its top layer (and is not recording to a picture
221 * or other non-raster target) and has direct access to its pixels (i.e. they are in
222 * local RAM) return the address of those pixels, and if not null,
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000223 * return the ImageInfo, rowBytes and origin. The returned address is only valid
reed@google.com9c135db2014-03-12 18:28:35 +0000224 * while the canvas object is in scope and unchanged. Any API calls made on
225 * canvas (or its parent surface if any) will invalidate the
226 * returned address (and associated information).
227 *
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000228 * On failure, returns NULL and the info, rowBytes, and origin parameters are ignored.
reed@google.com9c135db2014-03-12 18:28:35 +0000229 */
commit-bot@chromium.org6b4aaa72014-04-21 21:09:38 +0000230 void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL);
reed@google.com9c135db2014-03-12 18:28:35 +0000231
232 /**
233 * If the canvas has readable pixels in its base layer (and is not recording to a picture
234 * 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 +0000235 * local RAM) return the const-address of those pixels, and if not null,
236 * return the ImageInfo and rowBytes. The returned address is only valid
237 * while the canvas object is in scope and unchanged. Any API calls made on
238 * canvas (or its parent surface if any) will invalidate the
239 * returned address (and associated information).
240 *
241 * On failure, returns NULL and the info and rowBytes parameters are
242 * ignored.
243 */
244 const void* peekPixels(SkImageInfo* info, size_t* rowBytes);
245
reed@google.com4b226022011-01-11 18:32:13 +0000246 /**
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000247 * Copy the pixels from the base-layer into the specified buffer (pixels + rowBytes),
248 * converting them into the requested format (SkImageInfo). The base-layer pixels are read
reedb184f7f2014-07-13 04:32:32 -0700249 * starting at the specified (srcX,srcY) location in the coordinate system of the base-layer.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000250 *
reedb184f7f2014-07-13 04:32:32 -0700251 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000252 *
reedb184f7f2014-07-13 04:32:32 -0700253 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000254 *
reedb184f7f2014-07-13 04:32:32 -0700255 * srcR is intersected with the bounds of the base-layer. If this intersection is not empty,
256 * then we have two sets of pixels (of equal size). Replace the dst pixels with the
257 * corresponding src pixels, performing any colortype/alphatype transformations needed
258 * (in the case where the src and dst have different colortypes or alphatypes).
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000259 *
260 * This call can fail, returning false, for several reasons:
reedb184f7f2014-07-13 04:32:32 -0700261 * - If srcR does not intersect the base-layer bounds.
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000262 * - If the requested colortype/alphatype cannot be converted from the base-layer's types.
263 * - If this canvas is not backed by pixels (e.g. picture or PDF)
264 */
reedb184f7f2014-07-13 04:32:32 -0700265 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
266 int srcX, int srcY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000267
268 /**
269 * Helper for calling readPixels(info, ...). This call will check if bitmap has been allocated.
270 * If not, it will attempt to call allocPixels(). If this fails, it will return false. If not,
271 * it calls through to readPixels(info, ...) and returns its result.
272 */
reedb184f7f2014-07-13 04:32:32 -0700273 bool readPixels(SkBitmap* bitmap, int srcX, int srcY);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000274
275 /**
276 * Helper for allocating pixels and then calling readPixels(info, ...). The bitmap is resized
277 * to the intersection of srcRect and the base-layer bounds. On success, pixels will be
278 * allocated in bitmap and true returned. On failure, false is returned and bitmap will be
279 * set to empty.
reed@google.com51df9e32010-12-23 19:29:18 +0000280 */
281 bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap);
reed@google.com51df9e32010-12-23 19:29:18 +0000282
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000283 /**
284 * This method affects the pixels in the base-layer, and operates in pixel coordinates,
285 * ignoring the matrix and clip.
286 *
287 * The specified ImageInfo and (x,y) offset specifies a rectangle: target.
288 *
289 * target.setXYWH(x, y, info.width(), info.height());
290 *
291 * Target is intersected with the bounds of the base-layer. If this intersection is not empty,
292 * then we have two sets of pixels (of equal size), the "src" specified by info+pixels+rowBytes
293 * and the "dst" by the canvas' backend. Replace the dst pixels with the corresponding src
294 * pixels, performing any colortype/alphatype transformations needed (in the case where the
295 * src and dst have different colortypes or alphatypes).
296 *
297 * This call can fail, returning false, for several reasons:
298 * - If the src colortype/alphatype cannot be converted to the canvas' types
299 * - If this canvas is not backed by pixels (e.g. picture or PDF)
300 */
301 bool writePixels(const SkImageInfo&, const void* pixels, size_t rowBytes, int x, int y);
302
303 /**
304 * Helper for calling writePixels(info, ...) by passing its pixels and rowbytes. If the bitmap
305 * is just wrapping a texture, returns false and does nothing.
306 */
307 bool writePixels(const SkBitmap& bitmap, int x, int y);
reed@google.com4b226022011-01-11 18:32:13 +0000308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 ///////////////////////////////////////////////////////////////////////////
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000310
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 enum SaveFlags {
312 /** save the matrix state, restoring it on restore() */
Florin Malita5f6102d2014-06-30 10:13:28 -0400313 // [deprecated] kMatrix_SaveFlag = 0x01,
fmalita696d3612014-07-01 11:55:41 -0700314 kMatrix_SaveFlag = 0x01,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000315 /** save the clip state, restoring it on restore() */
Florin Malita5f6102d2014-06-30 10:13:28 -0400316 // [deprecated] kClip_SaveFlag = 0x02,
fmalita696d3612014-07-01 11:55:41 -0700317 kClip_SaveFlag = 0x02,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 /** the layer needs to support per-pixel alpha */
319 kHasAlphaLayer_SaveFlag = 0x04,
320 /** the layer needs to support 8-bits per color component */
321 kFullColorLayer_SaveFlag = 0x08,
reed@google.comb93ba452014-03-10 19:47:58 +0000322 /**
323 * the layer should clip against the bounds argument
324 *
325 * if SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG is undefined, this is treated as always on.
326 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327 kClipToLayer_SaveFlag = 0x10,
328
329 // helper masks for common choices
Florin Malita5f6102d2014-06-30 10:13:28 -0400330 // [deprecated] kMatrixClip_SaveFlag = 0x03,
fmalita696d3612014-07-01 11:55:41 -0700331 kMatrixClip_SaveFlag = 0x03,
reed@google.comb93ba452014-03-10 19:47:58 +0000332#ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 kARGB_NoClipLayer_SaveFlag = 0x0F,
reed@google.comb93ba452014-03-10 19:47:58 +0000334#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 kARGB_ClipLayer_SaveFlag = 0x1F
336 };
337
reed@android.comdc3381f2010-02-11 16:05:15 +0000338 /** This call saves the current matrix, clip, and drawFilter, and pushes a
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 copy onto a private stack. Subsequent calls to translate, scale,
reed@android.comdc3381f2010-02-11 16:05:15 +0000340 rotate, skew, concat or clipRect, clipPath, and setDrawFilter all
341 operate on this copy.
342 When the balancing call to restore() is made, the previous matrix, clip,
343 and drawFilter are restored.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000344
345 @return The value to pass to restoreToCount() to balance this save()
346 */
347 int save();
348
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 /** This behaves the same as save(), but in addition it allocates an
350 offscreen bitmap. All drawing calls are directed there, and only when
351 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000352 the canvas (or the previous layer).
reed@android.comad164b22010-07-02 17:20:51 +0000353 @param bounds (may be null) This rect, if non-null, is used as a hint to
354 limit the size of the offscreen, and thus drawing may be
355 clipped to it, though that clipping is not guaranteed to
356 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 @param paint (may be null) This is copied, and is applied to the
358 offscreen when restore() is called
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000359 @return The value to pass to restoreToCount() to balance this save()
360 */
361 int saveLayer(const SkRect* bounds, const SkPaint* paint);
362
363 /** DEPRECATED - use saveLayer(const SkRect*, const SkPaint*) instead.
364
365 This behaves the same as saveLayer(const SkRect*, const SkPaint*),
366 but it allows fine-grained control of which state bits to be saved
367 (and subsequently restored).
368
369 @param bounds (may be null) This rect, if non-null, is used as a hint to
370 limit the size of the offscreen, and thus drawing may be
371 clipped to it, though that clipping is not guaranteed to
372 happen. If exact clipping is desired, use clipRect().
373 @param paint (may be null) This is copied, and is applied to the
374 offscreen when restore() is called
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 @param flags LayerFlags
376 @return The value to pass to restoreToCount() to balance this save()
377 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000378 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
379 int saveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000380
381 /** This behaves the same as save(), but in addition it allocates an
382 offscreen bitmap. All drawing calls are directed there, and only when
383 the balancing call to restore() is made is that offscreen transfered to
reed@android.comdc3381f2010-02-11 16:05:15 +0000384 the canvas (or the previous layer).
reed@android.com40408612010-07-02 17:24:23 +0000385 @param bounds (may be null) This rect, if non-null, is used as a hint to
386 limit the size of the offscreen, and thus drawing may be
387 clipped to it, though that clipping is not guaranteed to
388 happen. If exact clipping is desired, use clipRect().
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 @param alpha This is applied to the offscreen when restore() is called.
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000390 @return The value to pass to restoreToCount() to balance this save()
391 */
392 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha);
393
394 /** DEPRECATED - use saveLayerAlpha(const SkRect*, U8CPU) instead.
395
396 This behaves the same as saveLayerAlpha(const SkRect*, U8CPU),
397 but it allows fine-grained control of which state bits to be saved
398 (and subsequently restored).
399
400 @param bounds (may be null) This rect, if non-null, is used as a hint to
401 limit the size of the offscreen, and thus drawing may be
402 clipped to it, though that clipping is not guaranteed to
403 happen. If exact clipping is desired, use clipRect().
404 @param alpha This is applied to the offscreen when restore() is called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405 @param flags LayerFlags
406 @return The value to pass to restoreToCount() to balance this save()
407 */
commit-bot@chromium.orgd70fa202014-04-24 21:51:58 +0000408 SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated")
409 int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, SaveFlags flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410
411 /** This call balances a previous call to save(), and is used to remove all
reed@android.comdc3381f2010-02-11 16:05:15 +0000412 modifications to the matrix/clip/drawFilter state since the last save
413 call.
414 It is an error to call restore() more times than save() was called.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 */
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000416 void restore();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000417
418 /** Returns the number of matrix/clip states on the SkCanvas' private stack.
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000419 This will equal # save() calls - # restore() calls + 1. The save count on
420 a new canvas is 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000421 */
junov@chromium.orga907ac32012-02-24 21:54:07 +0000422 int getSaveCount() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423
424 /** Efficient way to pop any calls to save() that happened after the save
commit-bot@chromium.orgea7d08e2014-02-13 16:00:51 +0000425 count reached saveCount. It is an error for saveCount to be greater than
426 getSaveCount(). To pop all the way back to the initial matrix/clip context
427 pass saveCount == 1.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 @param saveCount The number of save() levels to restore from
429 */
430 void restoreToCount(int saveCount);
431
reed@google.com7c202932011-12-14 18:48:05 +0000432 /** Returns true if drawing is currently going to a layer (from saveLayer)
433 * rather than to the root device.
434 */
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000435 virtual bool isDrawingToLayer() const;
reed@google.com7c202932011-12-14 18:48:05 +0000436
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 /** Preconcat the current matrix with the specified translation
438 @param dx The distance to translate in X
439 @param dy The distance to translate in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000441 void translate(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000442
443 /** Preconcat the current matrix with the specified scale.
444 @param sx The amount to scale in X
445 @param sy The amount to scale in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000447 void scale(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448
449 /** Preconcat the current matrix with the specified rotation.
450 @param degrees The amount to rotate, in degrees
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000452 void rotate(SkScalar degrees);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453
454 /** Preconcat the current matrix with the specified skew.
455 @param sx The amount to skew in X
456 @param sy The amount to skew in Y
reed@android.com8a1c16f2008-12-17 15:59:43 +0000457 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000458 void skew(SkScalar sx, SkScalar sy);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459
460 /** Preconcat the current matrix with the specified matrix.
461 @param matrix The matrix to preconcatenate with the current matrix
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462 */
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000463 void concat(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000464
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465 /** Replace the current matrix with a copy of the specified matrix.
466 @param matrix The matrix that will be copied into the current matrix.
467 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000468 void setMatrix(const SkMatrix& matrix);
reed@google.com4b226022011-01-11 18:32:13 +0000469
reed@android.com8a1c16f2008-12-17 15:59:43 +0000470 /** Helper for setMatrix(identity). Sets the current matrix to identity.
471 */
472 void resetMatrix();
473
reed@google.com4ed0fb72012-12-12 20:48:18 +0000474 /**
475 * Modify the current clip with the specified rectangle.
476 * @param rect The rect 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 clipRect(const SkRect& rect,
481 SkRegion::Op op = SkRegion::kIntersect_Op,
482 bool doAntiAlias = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483
reed@google.com4ed0fb72012-12-12 20:48:18 +0000484 /**
485 * Modify the current clip with the specified SkRRect.
486 * @param rrect The rrect to combine with the current clip
487 * @param op The region op to apply to the current clip
488 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000489 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000490 void clipRRect(const SkRRect& rrect,
491 SkRegion::Op op = SkRegion::kIntersect_Op,
492 bool doAntiAlias = false);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000493
494 /**
495 * Modify the current clip with the specified path.
496 * @param path The path to combine with the current clip
497 * @param op The region op to apply to the current clip
498 * @param doAntiAlias true if the clip should be antialiased
reed@google.com4ed0fb72012-12-12 20:48:18 +0000499 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000500 void clipPath(const SkPath& path,
501 SkRegion::Op op = SkRegion::kIntersect_Op,
502 bool doAntiAlias = false);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503
caryclark@google.com8f0a7b82012-11-07 14:54:49 +0000504 /** EXPERIMENTAL -- only used for testing
505 Set to false to force clips to be hard, even if doAntiAlias=true is
506 passed to clipRect or clipPath.
507 */
508 void setAllowSoftClip(bool allow) {
509 fAllowSoftClip = allow;
510 }
511
caryclark@google.com45a75fb2013-04-25 13:34:40 +0000512 /** EXPERIMENTAL -- only used for testing
513 Set to simplify clip stack using path ops.
514 */
515 void setAllowSimplifyClip(bool allow) {
516 fAllowSimplifyClip = allow;
517 }
518
reed@android.com8a1c16f2008-12-17 15:59:43 +0000519 /** Modify the current clip with the specified region. Note that unlike
520 clipRect() and clipPath() which transform their arguments by the current
521 matrix, clipRegion() assumes its argument is already in device
522 coordinates, and so no transformation is performed.
523 @param deviceRgn The region to apply to the current clip
524 @param op The region op to apply to the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525 */
commit-bot@chromium.org759cf482014-03-06 13:18:07 +0000526 void clipRegion(const SkRegion& deviceRgn,
527 SkRegion::Op op = SkRegion::kIntersect_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528
529 /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the
530 specified region. This does not intersect or in any other way account
531 for the existing clip region.
532 @param deviceRgn The region to copy into the current clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000534 void setClipRegion(const SkRegion& deviceRgn) {
535 this->clipRegion(deviceRgn, SkRegion::kReplace_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 }
537
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538 /** Return true if the specified rectangle, after being transformed by the
539 current matrix, would lie completely outside of the current clip. Call
540 this to check if an area you intend to draw into is clipped out (and
541 therefore you can skip making the draw calls).
542 @param rect the rect to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 @return true if the rect (transformed by the canvas' matrix) does not
544 intersect with the canvas' clip
545 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000546 bool quickReject(const SkRect& rect) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000547
548 /** Return true if the specified path, after being transformed by the
549 current matrix, would lie completely outside of the current clip. Call
550 this to check if an area you intend to draw into is clipped out (and
551 therefore you can skip making the draw calls). Note, for speed it may
552 return false even if the path itself might not intersect the clip
553 (i.e. the bounds of the path intersects, but the path does not).
554 @param path The path to compare with the current clip
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 @return true if the path (transformed by the canvas' matrix) does not
556 intersect with the canvas' clip
557 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000558 bool quickReject(const SkPath& path) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559
560 /** Return true if the horizontal band specified by top and bottom is
561 completely clipped out. This is a conservative calculation, meaning
562 that it is possible that if the method returns false, the band may still
563 in fact be clipped out, but the converse is not true. If this method
564 returns true, then the band is guaranteed to be clipped out.
565 @param top The top of the horizontal band to compare with the clip
566 @param bottom The bottom of the horizontal and to compare with the clip
567 @return true if the horizontal band is completely clipped out (i.e. does
568 not intersect the current clip)
569 */
reed@google.com3b3e8952012-08-16 20:53:31 +0000570 bool quickRejectY(SkScalar top, SkScalar bottom) const {
reed@google.comc0784db2013-12-13 21:16:12 +0000571 SkASSERT(top <= bottom);
commit-bot@chromium.org9836bc32014-02-14 19:52:18 +0000572
573#ifndef SK_WILL_NEVER_DRAW_PERSPECTIVE_TEXT
574 // TODO: add a hasPerspective method similar to getLocalClipBounds. This
575 // would cache the SkMatrix::hasPerspective result. Alternatively, have
576 // the MC stack just set a hasPerspective boolean as it is updated.
577 if (this->getTotalMatrix().hasPerspective()) {
skia.committer@gmail.coma3b53272014-02-15 03:02:15 +0000578 // TODO: consider implementing some half-plane test between the
commit-bot@chromium.org9836bc32014-02-14 19:52:18 +0000579 // two Y planes and the device-bounds (i.e., project the top and
580 // bottom Y planes and then determine if the clip bounds is completely
581 // outside either one).
582 return false;
583 }
584#endif
585
reed@google.comc0784db2013-12-13 21:16:12 +0000586 const SkRect& clipR = this->getLocalClipBounds();
djsollen@google.com92d2a292012-02-27 16:17:59 +0000587 // In the case where the clip is empty and we are provided with a
588 // negative top and positive bottom parameter then this test will return
589 // false even though it will be clipped. We have chosen to exclude that
590 // check as it is rare and would result double the comparisons.
reed@google.comc0784db2013-12-13 21:16:12 +0000591 return top >= clipR.fBottom || bottom <= clipR.fTop;
djsollen@google.com92d2a292012-02-27 16:17:59 +0000592 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000593
594 /** Return the bounds of the current clip (in local coordinates) in the
595 bounds parameter, and return true if it is non-empty. This can be useful
596 in a way similar to quickReject, in that it tells you that drawing
597 outside of these bounds will be clipped out.
598 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000599 virtual bool getClipBounds(SkRect* bounds) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000600
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000601 /** Return the bounds of the current clip, in device coordinates; returns
602 true if non-empty. Maybe faster than getting the clip explicitly and
603 then taking its bounds.
604 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000605 virtual bool getClipDeviceBounds(SkIRect* bounds) const;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000606
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000607
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000609 specified ARGB color, using the specified mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000610 @param a the alpha component (0..255) of the color to fill the canvas
611 @param r the red component (0..255) of the color to fill the canvas
612 @param g the green component (0..255) of the color to fill the canvas
613 @param b the blue component (0..255) of the color to fill the canvas
614 @param mode the mode to apply the color in (defaults to SrcOver)
615 */
616 void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
reed@android.com845fdac2009-06-23 03:01:32 +0000617 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618
619 /** Fill the entire canvas' bitmap (restricted to the current clip) with the
reed@android.com845fdac2009-06-23 03:01:32 +0000620 specified color and mode.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621 @param color the color to draw with
622 @param mode the mode to apply the color in (defaults to SrcOver)
623 */
624 void drawColor(SkColor color,
reed@android.com845fdac2009-06-23 03:01:32 +0000625 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000626
reed@google.com2a981812011-04-14 18:59:28 +0000627 /**
628 * This erases the entire drawing surface to the specified color,
629 * irrespective of the clip. It does not blend with the previous pixels,
630 * but always overwrites them.
631 *
632 * It is roughly equivalent to the following:
633 * canvas.save();
634 * canvas.clipRect(hugeRect, kReplace_Op);
635 * paint.setColor(color);
636 * paint.setXfermodeMode(kSrc_Mode);
637 * canvas.drawPaint(paint);
638 * canvas.restore();
639 * though it is almost always much more efficient.
640 */
641 virtual void clear(SkColor);
642
643 /**
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000644 * This makes the contents of the canvas undefined. Subsequent calls that
645 * require reading the canvas contents will produce undefined results. Examples
646 * include blending and readPixels. The actual implementation is backend-
647 * dependent and one legal implementation is to do nothing. Like clear(), this
648 * ignores the clip.
649 *
650 * This function should only be called if the caller intends to subsequently
651 * draw to the canvas. The canvas may do real work at discard() time in order
652 * to optimize performance on subsequent draws. Thus, if you call this and then
653 * never draw to the canvas subsequently you may pay a perfomance penalty.
654 */
655 void discard() { this->onDiscard(); }
656
657 /**
reed@google.com2a981812011-04-14 18:59:28 +0000658 * Fill the entire canvas' bitmap (restricted to the current clip) with the
659 * specified paint.
660 * @param paint The paint used to fill the canvas
661 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662 virtual void drawPaint(const SkPaint& paint);
663
664 enum PointMode {
665 /** drawPoints draws each point separately */
666 kPoints_PointMode,
667 /** drawPoints draws each pair of points as a line segment */
668 kLines_PointMode,
669 /** drawPoints draws the array of points as a polygon */
670 kPolygon_PointMode
671 };
672
673 /** Draw a series of points, interpreted based on the PointMode mode. For
674 all modes, the count parameter is interpreted as the total number of
675 points. For kLine mode, count/2 line segments are drawn.
676 For kPoint mode, each point is drawn centered at its coordinate, and its
677 size is specified by the paint's stroke-width. It draws as a square,
678 unless the paint's cap-type is round, in which the points are drawn as
679 circles.
680 For kLine mode, each pair of points is drawn as a line segment,
681 respecting the paint's settings for cap/join/width.
682 For kPolygon mode, the entire array is drawn as a series of connected
683 line segments.
684 Note that, while similar, kLine and kPolygon modes draw slightly
685 differently than the equivalent path built with a series of moveto,
686 lineto calls, in that the path will draw all of its contours at once,
687 with no interactions if contours intersect each other (think XOR
688 xfermode). drawPoints always draws each element one at a time.
689 @param mode PointMode specifying how to draw the array of points.
690 @param count The number of points in the array
691 @param pts Array of points to draw
692 @param paint The paint used to draw the points
693 */
694 virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
695 const SkPaint& paint);
696
697 /** Helper method for drawing a single point. See drawPoints() for a more
698 details.
699 */
700 void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000701
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702 /** Draws a single pixel in the specified color.
703 @param x The X coordinate of which pixel to draw
704 @param y The Y coordiante of which pixel to draw
705 @param color The color to draw
706 */
707 void drawPoint(SkScalar x, SkScalar y, SkColor color);
708
709 /** Draw a line segment with the specified start and stop x,y coordinates,
710 using the specified paint. NOTE: since a line is always "framed", the
711 paint's Style is ignored.
712 @param x0 The x-coordinate of the start point of the line
713 @param y0 The y-coordinate of the start point of the line
714 @param x1 The x-coordinate of the end point of the line
715 @param y1 The y-coordinate of the end point of the line
716 @param paint The paint used to draw the line
717 */
718 void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
719 const SkPaint& paint);
720
721 /** Draw the specified rectangle using the specified paint. The rectangle
722 will be filled or stroked based on the Style in the paint.
723 @param rect The rect to be drawn
724 @param paint The paint used to draw the rect
725 */
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000726 virtual void drawRect(const SkRect& rect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000727
728 /** Draw the specified rectangle using the specified paint. The rectangle
729 will be filled or framed based on the Style in the paint.
730 @param rect The rect to be drawn
731 @param paint The paint used to draw the rect
732 */
reed@google.com87001ed2014-02-17 16:28:05 +0000733 void drawIRect(const SkIRect& rect, const SkPaint& paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000734 SkRect r;
735 r.set(rect); // promotes the ints to scalars
736 this->drawRect(r, paint);
737 }
reed@google.com4b226022011-01-11 18:32:13 +0000738
reed@android.com8a1c16f2008-12-17 15:59:43 +0000739 /** Draw the specified rectangle using the specified paint. The rectangle
740 will be filled or framed based on the Style in the paint.
741 @param left The left side of the rectangle to be drawn
742 @param top The top side of the rectangle to be drawn
743 @param right The right side of the rectangle to be drawn
744 @param bottom The bottom side of the rectangle to be drawn
745 @param paint The paint used to draw the rect
746 */
747 void drawRectCoords(SkScalar left, SkScalar top, SkScalar right,
748 SkScalar bottom, const SkPaint& paint);
749
750 /** Draw the specified oval using the specified paint. The oval will be
751 filled or framed based on the Style in the paint.
752 @param oval The rectangle bounds of the oval to be drawn
753 @param paint The paint used to draw the oval
754 */
reed@google.com4ed0fb72012-12-12 20:48:18 +0000755 virtual void drawOval(const SkRect& oval, const SkPaint&);
756
757 /**
758 * Draw the specified RRect using the specified paint The rrect will be filled or stroked
759 * based on the Style in the paint.
760 *
761 * @param rrect The round-rect to draw
762 * @param paint The paint used to draw the round-rect
763 */
764 virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +0000766 /**
767 * Draw the annulus formed by the outer and inner rrects. The results
768 * are undefined if the outer does not contain the inner.
769 */
770 void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&);
771
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772 /** Draw the specified circle using the specified paint. If radius is <= 0,
773 then nothing will be drawn. The circle will be filled
774 or framed based on the Style in the paint.
775 @param cx The x-coordinate of the center of the cirle to be drawn
776 @param cy The y-coordinate of the center of the cirle to be drawn
777 @param radius The radius of the cirle to be drawn
778 @param paint The paint used to draw the circle
779 */
780 void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
781 const SkPaint& paint);
782
783 /** Draw the specified arc, which will be scaled to fit inside the
784 specified oval. If the sweep angle is >= 360, then the oval is drawn
785 completely. Note that this differs slightly from SkPath::arcTo, which
786 treats the sweep angle mod 360.
787 @param oval The bounds of oval used to define the shape of the arc
788 @param startAngle Starting angle (in degrees) where the arc begins
789 @param sweepAngle Sweep angle (in degrees) measured clockwise
790 @param useCenter true means include the center of the oval. For filling
791 this will draw a wedge. False means just use the arc.
792 @param paint The paint used to draw the arc
793 */
794 void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
795 bool useCenter, const SkPaint& paint);
796
797 /** Draw the specified round-rect using the specified paint. The round-rect
798 will be filled or framed based on the Style in the paint.
799 @param rect The rectangular bounds of the roundRect to be drawn
800 @param rx The x-radius of the oval used to round the corners
801 @param ry The y-radius of the oval used to round the corners
802 @param paint The paint used to draw the roundRect
803 */
804 void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry,
805 const SkPaint& paint);
806
807 /** Draw the specified path using the specified paint. The path will be
808 filled or framed based on the Style in the paint.
809 @param path The path to be drawn
810 @param paint The paint used to draw the path
811 */
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000812 virtual void drawPath(const SkPath& path, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000813
814 /** Draw the specified bitmap, with its top/left corner at (x,y), using the
815 specified paint, transformed by the current matrix. Note: if the paint
816 contains a maskfilter that generates a mask which extends beyond the
817 bitmap's original width/height, then the bitmap will be drawn as if it
818 were in a Shader with CLAMP mode. Thus the color outside of the original
819 width/height will be the edge color replicated.
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000820
821 If a shader is present on the paint it will be ignored, except in the
reed@google.comf20fc242014-03-26 13:44:58 +0000822 case where the bitmap is kAlpha_8_SkColorType. In that case, the color is
commit-bot@chromium.org91246b92013-12-05 15:43:19 +0000823 generated by the shader.
824
reed@android.com8a1c16f2008-12-17 15:59:43 +0000825 @param bitmap The bitmap to be drawn
826 @param left The position of the left side of the bitmap being drawn
827 @param top The position of the top side of the bitmap being drawn
828 @param paint The paint used to draw the bitmap, or NULL
829 */
830 virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
831 const SkPaint* paint = NULL);
832
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000833 enum DrawBitmapRectFlags {
robertphillips@google.com31acc112013-08-20 12:13:48 +0000834 kNone_DrawBitmapRectFlag = 0x0,
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000835 /**
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000836 * When filtering is enabled, allow the color samples outside of
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000837 * the src rect (but still in the src bitmap) to bleed into the
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000838 * drawn portion
839 */
robertphillips@google.com31acc112013-08-20 12:13:48 +0000840 kBleed_DrawBitmapRectFlag = 0x1,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000841 };
842
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843 /** Draw the specified bitmap, with the specified matrix applied (before the
844 canvas' matrix is applied).
845 @param bitmap The bitmap to be drawn
846 @param src Optional: specify the subset of the bitmap to be drawn
847 @param dst The destination rectangle where the scaled/translated
848 image will be drawn
849 @param paint The paint used to draw the bitmap, or NULL
850 */
reed@google.com71121732012-09-18 15:14:33 +0000851 virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
852 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 DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000855
reed@google.com71121732012-09-18 15:14:33 +0000856 void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000857 const SkPaint* paint = NULL) {
robertphillips@google.com31acc112013-08-20 12:13:48 +0000858 this->drawBitmapRectToRect(bitmap, NULL, dst, paint, kNone_DrawBitmapRectFlag);
reed@google.com71121732012-09-18 15:14:33 +0000859 }
860
861 void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* isrc,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000862 const SkRect& dst, const SkPaint* paint = NULL,
robertphillips@google.com31acc112013-08-20 12:13:48 +0000863 DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) {
reed@google.com71121732012-09-18 15:14:33 +0000864 SkRect realSrcStorage;
865 SkRect* realSrcPtr = NULL;
866 if (isrc) {
867 realSrcStorage.set(*isrc);
868 realSrcPtr = &realSrcStorage;
869 }
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000870 this->drawBitmapRectToRect(bitmap, realSrcPtr, dst, paint, flags);
reed@google.com71121732012-09-18 15:14:33 +0000871 }
skia.committer@gmail.comc1ad0222012-09-19 02:01:47 +0000872
reed@android.com8a1c16f2008-12-17 15:59:43 +0000873 virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
874 const SkPaint* paint = NULL);
reed@google.com4b226022011-01-11 18:32:13 +0000875
reed@google.comf0b5e112011-09-07 11:57:34 +0000876 /**
877 * Draw the bitmap stretched differentially to fit into dst.
878 * center is a rect within the bitmap, and logically divides the bitmap
879 * into 9 sections (3x3). For example, if the middle pixel of a [5x5]
880 * bitmap is the "center", then the center-rect should be [2, 2, 3, 3].
881 *
882 * If the dst is >= the bitmap size, then...
robertphillips@google.com9bf380c2013-07-25 12:10:42 +0000883 * - The 4 corners are not stretched at all.
884 * - The sides are stretched in only one axis.
885 * - The center is stretched in both axes.
reed@google.comf0b5e112011-09-07 11:57:34 +0000886 * Else, for each axis where dst < bitmap,
887 * - The corners shrink proportionally
888 * - The sides (along the shrink axis) and center are not drawn
889 */
890 virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
891 const SkRect& dst, const SkPaint* paint = NULL);
892
reed@android.com8a1c16f2008-12-17 15:59:43 +0000893 /** Draw the specified bitmap, with its top/left corner at (x,y),
894 NOT transformed by the current matrix. Note: if the paint
895 contains a maskfilter that generates a mask which extends beyond the
896 bitmap's original width/height, then the bitmap will be drawn as if it
897 were in a Shader with CLAMP mode. Thus the color outside of the original
898 width/height will be the edge color replicated.
899 @param bitmap The bitmap to be drawn
900 @param left The position of the left side of the bitmap being drawn
901 @param top The position of the top side of the bitmap being drawn
902 @param paint The paint used to draw the bitmap, or NULL
903 */
904 virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
905 const SkPaint* paint = NULL);
906
907 /** Draw the text, with origin at (x,y), using the specified paint.
908 The origin is interpreted based on the Align setting in the paint.
909 @param text The text to be drawn
910 @param byteLength The number of bytes to read from the text parameter
911 @param x The x-coordinate of the origin of the text being drawn
912 @param y The y-coordinate of the origin of the text being drawn
913 @param paint The paint used for the text (e.g. color, size, style)
914 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000915 SK_LEGACY_DRAWTEXT_VIRTUAL void drawText(const void* text, size_t byteLength, SkScalar x,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000916 SkScalar y, const SkPaint& paint);
917
918 /** Draw the text, with each character/glyph origin specified by the pos[]
reed@google.com4b226022011-01-11 18:32:13 +0000919 array. The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920 @param text The text to be drawn
921 @param byteLength The number of bytes to read from the text parameter
922 @param pos Array of positions, used to position each character
923 @param paint The paint used for the text (e.g. color, size, style)
924 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000925 SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosText(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000926 const SkPoint pos[], const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000927
reed@android.com8a1c16f2008-12-17 15:59:43 +0000928 /** Draw the text, with each character/glyph origin specified by the x
929 coordinate taken from the xpos[] array, and the y from the constY param.
reed@google.com4b226022011-01-11 18:32:13 +0000930 The origin is interpreted by the Align setting in the paint.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000931 @param text The text to be drawn
932 @param byteLength The number of bytes to read from the text parameter
933 @param xpos Array of x-positions, used to position each character
934 @param constY The shared Y coordinate for all of the positions
935 @param paint The paint used for the text (e.g. color, size, style)
936 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000937 SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosTextH(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000938 const SkScalar xpos[], SkScalar constY,
939 const SkPaint& paint);
reed@google.com4b226022011-01-11 18:32:13 +0000940
reed@android.com8a1c16f2008-12-17 15:59:43 +0000941 /** Draw the text, with origin at (x,y), using the specified paint, along
942 the specified path. The paint's Align setting determins where along the
943 path to start the text.
944 @param text The text to be drawn
945 @param byteLength The number of bytes to read from the text parameter
946 @param path The path the text should follow for its baseline
947 @param hOffset The distance along the path to add to the text's
948 starting position
949 @param vOffset The distance above(-) or below(+) the path to
950 position the text
951 @param paint The paint used for the text
952 */
953 void drawTextOnPathHV(const void* text, size_t byteLength,
954 const SkPath& path, SkScalar hOffset,
955 SkScalar vOffset, const SkPaint& paint);
956
957 /** Draw the text, with origin at (x,y), using the specified paint, along
958 the specified path. The paint's Align setting determins where along the
959 path to start the text.
960 @param text The text to be drawn
961 @param byteLength The number of bytes to read from the text parameter
962 @param path The path the text should follow for its baseline
963 @param matrix (may be null) Applied to the text before it is
964 mapped onto the path
965 @param paint The paint used for the text
966 */
reed@google.come0d9ce82014-04-23 04:00:17 +0000967 SK_LEGACY_DRAWTEXT_VIRTUAL void drawTextOnPath(const void* text, size_t byteLength,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000968 const SkPath& path, const SkMatrix* matrix,
969 const SkPaint& paint);
970
fmalita00d5c2c2014-08-21 08:53:26 -0700971 /** Draw the text blob, offset by (x,y), using the specified paint.
972 @param blob The text blob to be drawn
973 @param x The x-offset of the text being drawn
974 @param y The y-offset of the text being drawn
975 @param paint The paint used for the text (e.g. color, size, style)
976 */
977 void drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint);
978
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000979 /** PRIVATE / EXPERIMENTAL -- do not call
980 Perform back-end analysis/optimization of a picture. This may attach
981 optimization data to the picture which can be used by a later
982 drawPicture call.
983 @param picture The recorded drawing commands to analyze/optimize
984 */
robertphillips9b14f262014-06-04 05:40:44 -0700985 void EXPERIMENTAL_optimize(const SkPicture* picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +0000986
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987 /** Draw the picture into this canvas. This method effective brackets the
988 playback of the picture's draw calls with save/restore, so the state
djsollen@google.coma44de962013-01-02 16:59:19 +0000989 of this canvas will be unchanged after this call.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000990 @param picture The recorded drawing commands to playback into this
991 canvas.
992 */
robertphillips9b14f262014-06-04 05:40:44 -0700993 void drawPicture(const SkPicture* picture);
994
reedd5fa1a42014-08-09 11:08:05 -0700995 /**
996 * Draw the picture into this canvas.
997 *
998 * If matrix is non-null, apply that matrix to the CTM when drawing this picture. This is
999 * logically equivalent to
1000 * save/concat/drawPicture/restore
1001 *
1002 * If paint is non-null, draw the picture into a temporary buffer, and then apply the paint's
1003 * alpha/colorfilter/imagefilter/xfermode to that buffer as it is drawn to the canvas.
1004 * This is logically equivalent to
1005 * saveLayer(paint)/drawPicture/restore
1006 */
1007 void drawPicture(const SkPicture*, const SkMatrix* matrix, const SkPaint* paint);
1008
reed@android.com8a1c16f2008-12-17 15:59:43 +00001009 enum VertexMode {
1010 kTriangles_VertexMode,
1011 kTriangleStrip_VertexMode,
1012 kTriangleFan_VertexMode
1013 };
reed@google.com4b226022011-01-11 18:32:13 +00001014
reed@android.com8a1c16f2008-12-17 15:59:43 +00001015 /** Draw the array of vertices, interpreted as triangles (based on mode).
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001016
1017 If both textures and vertex-colors are NULL, it strokes hairlines with
1018 the paint's color. This behavior is a useful debugging mode to visualize
1019 the mesh.
1020
reed@android.com8a1c16f2008-12-17 15:59:43 +00001021 @param vmode How to interpret the array of vertices
1022 @param vertexCount The number of points in the vertices array (and
1023 corresponding texs and colors arrays if non-null)
1024 @param vertices Array of vertices for the mesh
1025 @param texs May be null. If not null, specifies the coordinate
robertphillips@google.com631a59b2013-07-31 14:57:53 +00001026 in _texture_ space (not uv space) for each vertex.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001027 @param colors May be null. If not null, specifies a color for each
1028 vertex, to be interpolated across the triangle.
1029 @param xmode Used if both texs and colors are present. In this
1030 case the colors are combined with the texture using mode,
1031 before being drawn using the paint. If mode is null, then
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001032 kModulate_Mode is used.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001033 @param indices If not null, array of indices to reference into the
1034 vertex (texs, colors) array.
1035 @param indexCount number of entries in the indices array (if not null)
reed@google.com4b226022011-01-11 18:32:13 +00001036 @param paint Specifies the shader/texture if present.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001037 */
1038 virtual void drawVertices(VertexMode vmode, int vertexCount,
1039 const SkPoint vertices[], const SkPoint texs[],
1040 const SkColor colors[], SkXfermode* xmode,
1041 const uint16_t indices[], int indexCount,
1042 const SkPaint& paint);
mtklein6cfa73a2014-08-13 13:33:49 -07001043
dandovb3c9d1c2014-08-12 08:34:29 -07001044 /**
1045 Draw a cubic coons patch
mtklein6cfa73a2014-08-13 13:33:49 -07001046
dandovb3c9d1c2014-08-12 08:34:29 -07001047 @param cubic specifies the 4 bounding cubic bezier curves of a patch with clockwise order
1048 starting at the top left corner.
1049 @param colors specifies the colors for the corners which will be bilerp across the patch,
1050 their order is clockwise starting at the top left corner.
mtklein6cfa73a2014-08-13 13:33:49 -07001051 @param texCoords specifies the texture coordinates that will be bilerp across the patch,
dandovb3c9d1c2014-08-12 08:34:29 -07001052 their order is the same as the colors.
mtklein6cfa73a2014-08-13 13:33:49 -07001053 @param xmode specifies how are the colors and the textures combined if both of them are
dandovb3c9d1c2014-08-12 08:34:29 -07001054 present.
dandov963137b2014-08-07 07:49:53 -07001055 @param paint Specifies the shader/texture if present.
1056 */
dandovb3c9d1c2014-08-12 08:34:29 -07001057 void drawPatch(const SkPoint cubics[12], const SkColor colors[4],
1058 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001059
reed@android.comcb608442009-12-04 21:32:27 +00001060 /** Send a blob of data to the canvas.
1061 For canvases that draw, this call is effectively a no-op, as the data
1062 is not parsed, but just ignored. However, this call exists for
1063 subclasses like SkPicture's recording canvas, that can store the data
1064 and then play it back later (via another call to drawData).
1065 */
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001066 virtual void drawData(const void* data, size_t length) {
1067 // do nothing. Subclasses may do something with the data
1068 }
1069
skia.committer@gmail.coma5d3e772013-05-30 07:01:29 +00001070 /** Add comments. beginCommentGroup/endCommentGroup open/close a new group.
1071 Each comment added via addComment is notionally attached to its
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001072 enclosing group. Top-level comments simply belong to no group.
1073 */
1074 virtual void beginCommentGroup(const char* description) {
1075 // do nothing. Subclasses may do something
1076 }
1077 virtual void addComment(const char* kywd, const char* value) {
1078 // do nothing. Subclasses may do something
1079 }
1080 virtual void endCommentGroup() {
1081 // do nothing. Subclasses may do something
1082 }
1083
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001084 /**
1085 * With this call the client asserts that subsequent draw operations (up to the
1086 * matching popCull()) are fully contained within the given bounding box. The assertion
1087 * is not enforced, but the information might be used to quick-reject command blocks,
1088 * so an incorrect bounding box may result in incomplete rendering.
1089 */
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001090 void pushCull(const SkRect& cullRect);
reed@android.comcb608442009-12-04 21:32:27 +00001091
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001092 /**
1093 * Terminates the current culling block, and restores the previous one (if any).
1094 */
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001095 void popCull();
1096
reed@android.com8a1c16f2008-12-17 15:59:43 +00001097 //////////////////////////////////////////////////////////////////////////
reed@google.com4b226022011-01-11 18:32:13 +00001098
reed@android.com8a1c16f2008-12-17 15:59:43 +00001099 /** Get the current filter object. The filter's reference count is not
reed@android.comdc3381f2010-02-11 16:05:15 +00001100 affected. The filter is saved/restored, just like the matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001101 @return the canvas' filter (or NULL).
1102 */
1103 SkDrawFilter* getDrawFilter() const;
reed@google.com4b226022011-01-11 18:32:13 +00001104
reed@android.com8a1c16f2008-12-17 15:59:43 +00001105 /** Set the new filter (or NULL). Pass NULL to clear any existing filter.
1106 As a convenience, the parameter is returned. If an existing filter
1107 exists, its refcnt is decrement. If the new filter is not null, its
reed@android.comdc3381f2010-02-11 16:05:15 +00001108 refcnt is incremented. The filter is saved/restored, just like the
1109 matrix and clip.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001110 @param filter the new filter (or NULL)
1111 @return the new filter
1112 */
1113 virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter);
1114
1115 //////////////////////////////////////////////////////////////////////////
1116
reed@google.com754de5f2014-02-24 19:38:20 +00001117 /**
1118 * Return true if the current clip is empty (i.e. nothing will draw).
1119 * Note: this is not always a free call, so it should not be used
1120 * more often than necessary. However, once the canvas has computed this
1121 * result, subsequent calls will be cheap (until the clip state changes,
1122 * which can happen on any clip..() or restore() call.
1123 */
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001124 virtual bool isClipEmpty() const;
reed@google.com754de5f2014-02-24 19:38:20 +00001125
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001126 /**
1127 * Returns true if the current clip is just a (non-empty) rectangle.
1128 * Returns false if the clip is empty, or if it is complex.
1129 */
1130 virtual bool isClipRect() const;
1131
reed@android.com8a1c16f2008-12-17 15:59:43 +00001132 /** Return the current matrix on the canvas.
1133 This does not account for the translate in any of the devices.
1134 @return The current matrix on the canvas.
1135 */
junov@chromium.orga907ac32012-02-24 21:54:07 +00001136 const SkMatrix& getTotalMatrix() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001138 /** Return the clip stack. The clip stack stores all the individual
1139 * clips organized by the save/restore frame in which they were
1140 * added.
1141 * @return the current clip stack ("list" of individual clip elements)
1142 */
1143 const SkClipStack* getClipStack() const {
1144 return &fClipStack;
1145 }
1146
fmalitac3b589a2014-06-05 12:40:07 -07001147 typedef SkCanvasClipVisitor ClipVisitor;
reed@google.com90c07ea2012-04-13 13:50:27 +00001148 /**
1149 * Replays the clip operations, back to front, that have been applied to
1150 * the canvas, calling the appropriate method on the visitor for each
1151 * clip. All clips have already been transformed into device space.
1152 */
1153 void replayClips(ClipVisitor*) const;
1154
reed@android.com8a1c16f2008-12-17 15:59:43 +00001155 ///////////////////////////////////////////////////////////////////////////
1156
1157 /** After calling saveLayer(), there can be any number of devices that make
1158 up the top-most drawing area. LayerIter can be used to iterate through
1159 those devices. Note that the iterator is only valid until the next API
1160 call made on the canvas. Ownership of all pointers in the iterator stays
1161 with the canvas, so none of them should be modified or deleted.
1162 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +00001163 class SK_API LayerIter /*: SkNoncopyable*/ {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001164 public:
1165 /** Initialize iterator with canvas, and set values for 1st device */
1166 LayerIter(SkCanvas*, bool skipEmptyClips);
1167 ~LayerIter();
reed@google.com4b226022011-01-11 18:32:13 +00001168
reed@android.com8a1c16f2008-12-17 15:59:43 +00001169 /** Return true if the iterator is done */
1170 bool done() const { return fDone; }
1171 /** Cycle to the next device */
1172 void next();
reed@google.com4b226022011-01-11 18:32:13 +00001173
reed@android.com8a1c16f2008-12-17 15:59:43 +00001174 // These reflect the current device in the iterator
1175
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001176 SkBaseDevice* device() const;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001177 const SkMatrix& matrix() const;
1178 const SkRegion& clip() const;
1179 const SkPaint& paint() const;
1180 int x() const;
1181 int y() const;
reed@google.com4b226022011-01-11 18:32:13 +00001182
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183 private:
1184 // used to embed the SkDrawIter object directly in our instance, w/o
1185 // having to expose that class def to the public. There is an assert
1186 // in our constructor to ensure that fStorage is large enough
1187 // (though needs to be a compile-time-assert!). We use intptr_t to work
1188 // safely with 32 and 64 bit machines (to ensure the storage is enough)
reed@android.comf2b98d62010-12-20 18:26:13 +00001189 intptr_t fStorage[32];
reed@android.com8a1c16f2008-12-17 15:59:43 +00001190 class SkDrawIter* fImpl; // this points at fStorage
1191 SkPaint fDefaultPaint;
1192 bool fDone;
1193 };
1194
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001195 // don't call
reed@google.com9c135db2014-03-12 18:28:35 +00001196 GrRenderTarget* internal_private_accessTopLayerRenderTarget();
commit-bot@chromium.org5c70cdc2014-03-08 03:57:19 +00001197
reed@android.com8a1c16f2008-12-17 15:59:43 +00001198protected:
reed@google.com76f10a32014-02-05 15:32:21 +00001199 // default impl defers to getDevice()->newSurface(info)
reed3716fd02014-09-21 09:39:55 -07001200 virtual SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&);
reed@google.com76f10a32014-02-05 15:32:21 +00001201
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001202 // default impl defers to its device
1203 virtual const void* onPeekPixels(SkImageInfo*, size_t* rowBytes);
reed@google.com9c135db2014-03-12 18:28:35 +00001204 virtual void* onAccessTopLayerPixels(SkImageInfo*, size_t* rowBytes);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001205
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001206 // Subclass save/restore notifiers.
1207 // Overriders should call the corresponding INHERITED method up the inheritance chain.
1208 // willSaveLayer()'s return value may suppress full layer allocation.
1209 enum SaveLayerStrategy {
1210 kFullLayer_SaveLayerStrategy,
1211 kNoLayer_SaveLayerStrategy
1212 };
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001213
fmalita6ca763f2014-06-17 13:52:18 -07001214 virtual void willSave() {}
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001215 virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) {
1216 return kFullLayer_SaveLayerStrategy;
1217 }
1218 virtual void willRestore() {}
mtklein6cfa73a2014-08-13 13:33:49 -07001219 virtual void didRestore() {}
commit-bot@chromium.orgfc6dfba2014-05-14 13:13:44 +00001220 virtual void didConcat(const SkMatrix&) {}
1221 virtual void didSetMatrix(const SkMatrix&) {}
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +00001222
commit-bot@chromium.orged9806f2014-02-21 02:32:36 +00001223 virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
1224
reed@google.come0d9ce82014-04-23 04:00:17 +00001225 virtual void onDrawText(const void* text, size_t byteLength, SkScalar x,
1226 SkScalar y, const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001227
reed@google.come0d9ce82014-04-23 04:00:17 +00001228 virtual void onDrawPosText(const void* text, size_t byteLength,
1229 const SkPoint pos[], const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001230
reed@google.come0d9ce82014-04-23 04:00:17 +00001231 virtual void onDrawPosTextH(const void* text, size_t byteLength,
1232 const SkScalar xpos[], SkScalar constY,
1233 const SkPaint& paint);
skia.committer@gmail.comb0430d02014-04-24 03:05:07 +00001234
reed@google.come0d9ce82014-04-23 04:00:17 +00001235 virtual void onDrawTextOnPath(const void* text, size_t byteLength,
1236 const SkPath& path, const SkMatrix* matrix,
1237 const SkPaint& paint);
mtklein6cfa73a2014-08-13 13:33:49 -07001238
fmalita00d5c2c2014-08-21 08:53:26 -07001239 virtual void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1240 const SkPaint& paint);
1241
dandovb3c9d1c2014-08-12 08:34:29 -07001242 virtual void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1243 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
reed@google.come0d9ce82014-04-23 04:00:17 +00001244
robertphillips@google.com8f90a892014-02-28 18:19:39 +00001245 enum ClipEdgeStyle {
1246 kHard_ClipEdgeStyle,
1247 kSoft_ClipEdgeStyle
1248 };
1249
1250 virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1251 virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1252 virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle);
1253 virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op);
1254
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001255 virtual void onDiscard();
1256
reedd5fa1a42014-08-09 11:08:05 -07001257 virtual void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*);
robertphillips9b14f262014-06-04 05:40:44 -07001258
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001259 // Returns the canvas to be used by DrawIter. Default implementation
junov@google.com4370aed2012-01-18 16:21:08 +00001260 // returns this. Subclasses that encapsulate an indirect canvas may
1261 // need to overload this method. The impl must keep track of this, as it
1262 // is not released or deleted by the caller.
1263 virtual SkCanvas* canvasForDrawIter();
1264
junov@chromium.orga907ac32012-02-24 21:54:07 +00001265 // Clip rectangle bounds. Called internally by saveLayer.
1266 // returns false if the entire rectangle is entirely clipped out
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001267 // If non-NULL, The imageFilter parameter will be used to expand the clip
1268 // and offscreen bounds for any margin required by the filter DAG.
junov@chromium.orga907ac32012-02-24 21:54:07 +00001269 bool clipRectBounds(const SkRect* bounds, SaveFlags flags,
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +00001270 SkIRect* intersection,
1271 const SkImageFilter* imageFilter = NULL);
junov@chromium.orga907ac32012-02-24 21:54:07 +00001272
reed@google.com97af1a62012-08-28 12:19:02 +00001273 // notify our surface (if we have one) that we are about to draw, so it
1274 // can perform copy-on-write or invalidate any cached images
1275 void predrawNotify();
1276
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001277 virtual void onPushCull(const SkRect& cullRect);
1278 virtual void onPopCull();
1279
reed@android.com8a1c16f2008-12-17 15:59:43 +00001280private:
1281 class MCRec;
1282
reed@google.com5c3d1472011-02-22 19:12:23 +00001283 SkClipStack fClipStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001284 SkDeque fMCStack;
1285 // points to top of stack
1286 MCRec* fMCRec;
1287 // the first N recs that can fit here mean we won't call malloc
1288 uint32_t fMCRecStorage[32];
1289
reed3716fd02014-09-21 09:39:55 -07001290 const SkSurfaceProps fProps;
1291
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +00001292 int fSaveLayerCount; // number of successful saveLayer calls
commit-bot@chromium.org210ae2a2014-02-27 17:40:13 +00001293 int fCullCount; // number of active culls
reed@android.com8a1c16f2008-12-17 15:59:43 +00001294
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +00001295 SkMetaData* fMetaData;
1296
reed@google.com97af1a62012-08-28 12:19:02 +00001297 SkSurface_Base* fSurfaceBase;
1298 SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; }
1299 void setSurfaceBase(SkSurface_Base* sb) {
1300 fSurfaceBase = sb;
1301 }
1302 friend class SkSurface_Base;
junov@chromium.org45c3db82013-04-11 17:52:05 +00001303 friend class SkSurface_Gpu;
skia.committer@gmail.comfc843592012-10-11 02:01:14 +00001304
reed@android.com8a1c16f2008-12-17 15:59:43 +00001305 bool fDeviceCMDirty; // cleared by updateDeviceCMCache()
1306 void updateDeviceCMCache();
1307
reed@google.com9c135db2014-03-12 18:28:35 +00001308 friend class SkDrawIter; // needs setupDrawForLayerDevice()
reed@google.com8926b162012-03-23 15:36:36 +00001309 friend class AutoDrawLooper;
reed@google.com9c135db2014-03-12 18:28:35 +00001310 friend class SkLua; // needs top layer size and offset
commit-bot@chromium.org2a67e122014-05-19 13:53:10 +00001311 friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip
reed@google.com9c135db2014-03-12 18:28:35 +00001312 friend class SkDeferredDevice; // needs getTopDevice()
reed52d9ac62014-06-30 09:05:34 -07001313 friend class SkSurface_Raster; // needs getDevice()
reedd9544982014-09-09 18:46:22 -07001314 friend class SkRecorder; // InitFlags
1315 friend class SkNoSaveLayerCanvas; // InitFlags
reedfa35f8e2014-09-04 12:14:36 -07001316
reedd9544982014-09-09 18:46:22 -07001317 enum InitFlags {
1318 kDefault_InitFlags = 0,
1319 kConservativeRasterClip_InitFlag = 1 << 0,
1320 };
reed3716fd02014-09-21 09:39:55 -07001321 SkCanvas(int width, int height, InitFlags);
1322 SkCanvas(SkBaseDevice*, const SkSurfaceProps*, InitFlags);
1323 SkCanvas(const SkBitmap&, const SkSurfaceProps&);
reedd9544982014-09-09 18:46:22 -07001324
reed8f2e7912014-09-04 12:45:18 -07001325 // needs gettotalclip()
1326 friend SkCanvasState* SkCanvasStateUtils::CaptureCanvasState(SkCanvas*);
reedfa35f8e2014-09-04 12:14:36 -07001327
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001328 SkBaseDevice* createLayerDevice(const SkImageInfo&);
bsalomon@google.come97f0852011-06-17 13:10:25 +00001329
reed3716fd02014-09-21 09:39:55 -07001330 // call this each time we attach ourselves to a device
1331 // - constructor
1332 // - internalSaveLayer
1333 void setupDevice(SkBaseDevice*);
1334
reedd9544982014-09-09 18:46:22 -07001335 SkBaseDevice* init(SkBaseDevice*, InitFlags);
reed@google.comf0b5e112011-09-07 11:57:34 +00001336
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001337 /**
1338 * DEPRECATED
1339 *
1340 * Specify a device for this canvas to draw into. If it is not null, its
1341 * reference count is incremented. If the canvas was already holding a
1342 * device, its reference count is decremented. The new device is returned.
1343 */
1344 SkBaseDevice* setRootDevice(SkBaseDevice* device);
skia.committer@gmail.com31acdea2014-02-18 03:01:51 +00001345
bsalomon@google.com4ebe3822014-02-26 20:22:32 +00001346 /**
1347 * Gets the size/origin of the top level layer in global canvas coordinates. We don't want this
1348 * to be public because it exposes decisions about layer sizes that are internal to the canvas.
1349 */
1350 SkISize getTopLayerSize() const;
1351 SkIPoint getTopLayerOrigin() const;
commit-bot@chromium.org403f8d72014-02-17 15:24:26 +00001352
reed@google.comf0b5e112011-09-07 11:57:34 +00001353 // internal methods are not virtual, so they can safely be called by other
1354 // canvas apis, without confusing subclasses (like SkPictureRecording)
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001355 void internalDrawBitmap(const SkBitmap&, const SkMatrix& m, const SkPaint* paint);
reed@google.com71121732012-09-18 15:14:33 +00001356 void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001357 const SkRect& dst, const SkPaint* paint,
1358 DrawBitmapRectFlags flags);
reed@google.comf0b5e112011-09-07 11:57:34 +00001359 void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
1360 const SkRect& dst, const SkPaint* paint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001361 void internalDrawPaint(const SkPaint& paint);
reed@google.com8926b162012-03-23 15:36:36 +00001362 int internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +00001363 SaveFlags, bool justForImageFilter, SaveLayerStrategy strategy);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001364 void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001365
reed@android.com8a1c16f2008-12-17 15:59:43 +00001366 // shared by save() and saveLayer()
Florin Malita5f6102d2014-06-30 10:13:28 -04001367 int internalSave();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001368 void internalRestore();
bungeman@google.com52c748b2011-08-22 21:30:43 +00001369 static void DrawRect(const SkDraw& draw, const SkPaint& paint,
1370 const SkRect& r, SkScalar textSize);
1371 static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
1372 const char text[], size_t byteLength,
1373 SkScalar x, SkScalar y);
reed@google.com4b226022011-01-11 18:32:13 +00001374
reedfa35f8e2014-09-04 12:14:36 -07001375 // only for canvasutils
1376 const SkRegion& internal_private_getTotalClip() const;
1377
reed@android.com8a1c16f2008-12-17 15:59:43 +00001378 /* These maintain a cache of the clip bounds in local coordinates,
1379 (converted to 2s-compliment if floats are slow).
1380 */
reed@google.comc0784db2013-12-13 21:16:12 +00001381 mutable SkRect fCachedLocalClipBounds;
1382 mutable bool fCachedLocalClipBoundsDirty;
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001383 bool fAllowSoftClip;
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001384 bool fAllowSimplifyClip;
reedd9544982014-09-09 18:46:22 -07001385 bool fConservativeRasterClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001386
reed@google.comc0784db2013-12-13 21:16:12 +00001387 const SkRect& getLocalClipBounds() const {
1388 if (fCachedLocalClipBoundsDirty) {
1389 if (!this->getClipBounds(&fCachedLocalClipBounds)) {
1390 fCachedLocalClipBounds.setEmpty();
1391 }
1392 fCachedLocalClipBoundsDirty = false;
reed@android.comba09de42010-02-05 20:46:05 +00001393 }
reed@google.comc0784db2013-12-13 21:16:12 +00001394 return fCachedLocalClipBounds;
reed@android.comba09de42010-02-05 20:46:05 +00001395 }
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001396
reed@google.com5c3d1472011-02-22 19:12:23 +00001397 class AutoValidateClip : ::SkNoncopyable {
1398 public:
1399 explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) {
1400 fCanvas->validateClip();
1401 }
1402 ~AutoValidateClip() { fCanvas->validateClip(); }
1403
1404 private:
1405 const SkCanvas* fCanvas;
1406 };
1407
1408#ifdef SK_DEBUG
commit-bot@chromium.org520cf8b2014-03-20 20:25:14 +00001409 // The cull stack rects are in device-space
1410 SkTDArray<SkIRect> fCullStack;
1411 void validateCull(const SkIRect&);
reed@google.com5c3d1472011-02-22 19:12:23 +00001412 void validateClip() const;
1413#else
1414 void validateClip() const {}
1415#endif
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +00001416
1417 typedef SkRefCnt INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001418};
1419
1420/** Stack helper class to automatically call restoreToCount() on the canvas
1421 when this object goes out of scope. Use this to guarantee that the canvas
1422 is restored to a known state.
1423*/
1424class SkAutoCanvasRestore : SkNoncopyable {
1425public:
commit-bot@chromium.org28871192013-10-14 15:28:01 +00001426 SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) {
1427 if (fCanvas) {
1428 fSaveCount = canvas->getSaveCount();
1429 if (doSave) {
1430 canvas->save();
1431 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001432 }
1433 }
1434 ~SkAutoCanvasRestore() {
reed@google.comf6c9a5b2012-11-20 15:12:21 +00001435 if (fCanvas) {
1436 fCanvas->restoreToCount(fSaveCount);
1437 }
1438 }
1439
1440 /**
1441 * Perform the restore now, instead of waiting for the destructor. Will
1442 * only do this once.
1443 */
1444 void restore() {
1445 if (fCanvas) {
1446 fCanvas->restoreToCount(fSaveCount);
1447 fCanvas = NULL;
1448 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001449 }
1450
1451private:
1452 SkCanvas* fCanvas;
1453 int fSaveCount;
1454};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001455#define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001456
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001457/** Stack helper class to automatically open and close a comment block
1458 */
1459class SkAutoCommentBlock : SkNoncopyable {
1460public:
1461 SkAutoCommentBlock(SkCanvas* canvas, const char* description) {
1462 fCanvas = canvas;
bsalomon49f085d2014-09-05 13:34:00 -07001463 if (fCanvas) {
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001464 fCanvas->beginCommentGroup(description);
1465 }
1466 }
1467
1468 ~SkAutoCommentBlock() {
bsalomon49f085d2014-09-05 13:34:00 -07001469 if (fCanvas) {
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001470 fCanvas->endCommentGroup();
1471 }
1472 }
1473
1474private:
1475 SkCanvas* fCanvas;
1476};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +00001477#define SkAutoCommentBlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoCommentBlock)
robertphillips@google.com0a4805e2013-05-29 13:24:23 +00001478
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001479/**
1480 * If the caller wants read-only access to the pixels in a canvas, it can just
1481 * call canvas->peekPixels(), since that is the fastest way to "peek" at the
1482 * pixels on a raster-backed canvas.
1483 *
1484 * If the canvas has pixels, but they are not readily available to the CPU
1485 * (e.g. gpu-backed), then peekPixels() will fail, but readPixels() will
1486 * succeed (though be slower, since it will return a copy of the pixels).
1487 *
1488 * SkAutoROCanvasPixels encapsulates these two techniques, trying first to call
1489 * peekPixels() (for performance), but if that fails, calling readPixels() and
1490 * storing the copy locally.
1491 *
1492 * The caller must respect the restrictions associated with peekPixels(), since
1493 * that may have been called: The returned information is invalidated if...
1494 * - any API is called on the canvas (or its parent surface if present)
1495 * - the canvas goes out of scope
1496 */
1497class SkAutoROCanvasPixels : SkNoncopyable {
1498public:
1499 SkAutoROCanvasPixels(SkCanvas* canvas);
1500
1501 // returns NULL on failure
1502 const void* addr() const { return fAddr; }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +00001503
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001504 // undefined if addr() == NULL
1505 size_t rowBytes() const { return fRowBytes; }
skia.committer@gmail.com02d6f542014-02-14 03:02:05 +00001506
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +00001507 // undefined if addr() == NULL
1508 const SkImageInfo& info() const { return fInfo; }
1509
1510 // helper that, if returns true, installs the pixels into the bitmap. Note
1511 // that the bitmap may reference the address returned by peekPixels(), so
1512 // the caller must respect the restrictions associated with peekPixels().
1513 bool asROBitmap(SkBitmap*) const;
1514
1515private:
1516 SkBitmap fBitmap; // used if peekPixels() fails
1517 const void* fAddr; // NULL on failure
1518 SkImageInfo fInfo;
1519 size_t fRowBytes;
1520};
1521
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001522static inline SkCanvas::SaveFlags operator|(const SkCanvas::SaveFlags lhs,
1523 const SkCanvas::SaveFlags rhs) {
commit-bot@chromium.org92a89162014-05-30 21:07:05 +00001524 return static_cast<SkCanvas::SaveFlags>(static_cast<int>(lhs) | static_cast<int>(rhs));
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001525}
1526
1527static inline SkCanvas::SaveFlags& operator|=(SkCanvas::SaveFlags& lhs,
1528 const SkCanvas::SaveFlags rhs) {
1529 lhs = lhs | rhs;
1530 return lhs;
1531}
1532
fmalitac3b589a2014-06-05 12:40:07 -07001533class SkCanvasClipVisitor {
1534public:
1535 virtual ~SkCanvasClipVisitor();
1536 virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0;
1537 virtual void clipRRect(const SkRRect&, SkRegion::Op, bool antialias) = 0;
1538 virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0;
1539};
commit-bot@chromium.org2a5cd602014-05-30 20:41:20 +00001540
reed@android.com8a1c16f2008-12-17 15:59:43 +00001541#endif