reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkCanvas_DEFINED |
| 9 | #define SkCanvas_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
| 12 | #include "SkBitmap.h" |
| 13 | #include "SkDeque.h" |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 14 | #include "SkClipStack.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkPaint.h" |
| 16 | #include "SkRefCnt.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkPath.h" |
| 18 | #include "SkRegion.h" |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 19 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 21 | #ifdef SK_SUPPORT_LEGACY_DRAWTEXT_VIRTUAL |
| 22 | #define SK_LEGACY_DRAWTEXT_VIRTUAL virtual |
| 23 | #else |
| 24 | #define SK_LEGACY_DRAWTEXT_VIRTUAL |
| 25 | #endif |
| 26 | |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 27 | class SkCanvasClipVisitor; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 28 | class SkBaseDevice; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | class SkDraw; |
| 30 | class SkDrawFilter; |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 31 | class SkMetaData; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | class SkPicture; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 33 | class SkRRect; |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 34 | class SkSurface; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 35 | class SkSurface_Base; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 36 | class GrContext; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 37 | class GrRenderTarget; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
| 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.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 54 | class SK_API SkCanvas : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 56 | SK_DECLARE_INST_COUNT(SkCanvas) |
| 57 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 58 | /** |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 59 | * 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.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 88 | * 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.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 109 | * Creates an empty canvas with no backing device/pixels, and zero |
| 110 | * dimensions. |
| 111 | */ |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 112 | SkCanvas(); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 113 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 114 | /** |
| 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.com | 6dc7455 | 2011-07-21 18:00:46 +0000 | [diff] [blame] | 121 | /** Construct a canvas with the specified device to draw into. |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 122 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 123 | @param device Specifies a device for the canvas to draw into. |
| 124 | */ |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 125 | explicit SkCanvas(SkBaseDevice* device); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 126 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 127 | /** Construct a canvas with the specified bitmap to draw into. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | @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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | virtual ~SkCanvas(); |
| 133 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 134 | SkMetaData& getMetaData(); |
| 135 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 136 | /** |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | /////////////////////////////////////////////////////////////////////////// |
| 143 | |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 144 | /** |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 145 | * Trigger the immediate execution of all pending draw operations. |
| 146 | */ |
| 147 | void flush(); |
| 148 | |
| 149 | /** |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 150 | * 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.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 153 | */ |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 154 | SkISize getBaseLayerSize() const; |
| 155 | |
| 156 | /** |
| 157 | * DEPRECATED: call getBaseLayerSize |
| 158 | */ |
| 159 | SkISize getDeviceSize() const { return this->getBaseLayerSize(); } |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 160 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 161 | /** |
| 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.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 167 | SkBaseDevice* getDevice() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 169 | /** |
| 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.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 175 | * |
| 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.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 181 | */ |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 182 | #ifndef SK_SUPPORT_LEGACY_GETTOPDEVICE |
| 183 | private: |
| 184 | #endif |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 185 | SkBaseDevice* getTopDevice(bool updateMatrixClip = false) const; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 186 | public: |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 187 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 188 | /** |
| 189 | * Create a new surface matching the specified info, one that attempts to |
commit-bot@chromium.org | cae54f1 | 2014-04-11 18:34:35 +0000 | [diff] [blame] | 190 | * be maximally compatible when used with this canvas. If there is no matching Surface type, |
| 191 | * NULL is returned. |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 192 | */ |
| 193 | SkSurface* newSurface(const SkImageInfo&); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 194 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 195 | /** |
| 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.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 201 | /////////////////////////////////////////////////////////////////////////// |
| 202 | |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 203 | /** |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 204 | * 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.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 207 | * return the ImageInfo, rowBytes and origin. The returned address is only valid |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 208 | * 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.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 212 | * On failure, returns NULL and the info, rowBytes, and origin parameters are ignored. |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 213 | */ |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 214 | void* accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin = NULL); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 215 | |
| 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.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 219 | * 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.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 230 | /** |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 231 | * 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.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 263 | */ |
| 264 | bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 265 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 266 | /** |
| 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.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 291 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | /////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 293 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | enum SaveFlags { |
| 295 | /** save the matrix state, restoring it on restore() */ |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 296 | // [deprecated] kMatrix_SaveFlag = 0x01, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | /** save the clip state, restoring it on restore() */ |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 298 | // [deprecated] kClip_SaveFlag = 0x02, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 303 | /** |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | kClipToLayer_SaveFlag = 0x10, |
| 309 | |
| 310 | // helper masks for common choices |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 311 | // [deprecated] kMatrixClip_SaveFlag = 0x03, |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 312 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 313 | kARGB_NoClipLayer_SaveFlag = 0x0F, |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 314 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 315 | kARGB_ClipLayer_SaveFlag = 0x1F |
| 316 | }; |
| 317 | |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 318 | /** This call saves the current matrix, clip, and drawFilter, and pushes a |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | copy onto a private stack. Subsequent calls to translate, scale, |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 320 | 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.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 324 | |
| 325 | @return The value to pass to restoreToCount() to balance this save() |
| 326 | */ |
| 327 | int save(); |
| 328 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | /** This behaves the same as save(), but in addition it allocates an |
| 330 | offscreen bitmap. All drawing calls are directed there, and only when |
| 331 | the balancing call to restore() is made is that offscreen transfered to |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 332 | the canvas (or the previous layer). |
reed@android.com | ad164b2 | 2010-07-02 17:20:51 +0000 | [diff] [blame] | 333 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 334 | limit the size of the offscreen, and thus drawing may be |
| 335 | clipped to it, though that clipping is not guaranteed to |
| 336 | happen. If exact clipping is desired, use clipRect(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | @param paint (may be null) This is copied, and is applied to the |
| 338 | offscreen when restore() is called |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 339 | @return The value to pass to restoreToCount() to balance this save() |
| 340 | */ |
| 341 | int saveLayer(const SkRect* bounds, const SkPaint* paint); |
| 342 | |
| 343 | /** DEPRECATED - use saveLayer(const SkRect*, const SkPaint*) instead. |
| 344 | |
| 345 | This behaves the same as saveLayer(const SkRect*, const SkPaint*), |
| 346 | but it allows fine-grained control of which state bits to be saved |
| 347 | (and subsequently restored). |
| 348 | |
| 349 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 350 | limit the size of the offscreen, and thus drawing may be |
| 351 | clipped to it, though that clipping is not guaranteed to |
| 352 | happen. If exact clipping is desired, use clipRect(). |
| 353 | @param paint (may be null) This is copied, and is applied to the |
| 354 | offscreen when restore() is called |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | @param flags LayerFlags |
| 356 | @return The value to pass to restoreToCount() to balance this save() |
| 357 | */ |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 358 | SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated") |
| 359 | int saveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | |
| 361 | /** This behaves the same as save(), but in addition it allocates an |
| 362 | offscreen bitmap. All drawing calls are directed there, and only when |
| 363 | the balancing call to restore() is made is that offscreen transfered to |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 364 | the canvas (or the previous layer). |
reed@android.com | 4040861 | 2010-07-02 17:24:23 +0000 | [diff] [blame] | 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(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | @param alpha This is applied to the offscreen when restore() is called. |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 370 | @return The value to pass to restoreToCount() to balance this save() |
| 371 | */ |
| 372 | int saveLayerAlpha(const SkRect* bounds, U8CPU alpha); |
| 373 | |
| 374 | /** DEPRECATED - use saveLayerAlpha(const SkRect*, U8CPU) instead. |
| 375 | |
| 376 | This behaves the same as saveLayerAlpha(const SkRect*, U8CPU), |
| 377 | but it allows fine-grained control of which state bits to be saved |
| 378 | (and subsequently restored). |
| 379 | |
| 380 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 381 | limit the size of the offscreen, and thus drawing may be |
| 382 | clipped to it, though that clipping is not guaranteed to |
| 383 | happen. If exact clipping is desired, use clipRect(). |
| 384 | @param alpha This is applied to the offscreen when restore() is called. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | @param flags LayerFlags |
| 386 | @return The value to pass to restoreToCount() to balance this save() |
| 387 | */ |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 388 | SK_ATTR_EXTERNALLY_DEPRECATED("SaveFlags use is deprecated") |
| 389 | int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, SaveFlags flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | |
| 391 | /** This call balances a previous call to save(), and is used to remove all |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 392 | modifications to the matrix/clip/drawFilter state since the last save |
| 393 | call. |
| 394 | It is an error to call restore() more times than save() was called. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 395 | */ |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 396 | void restore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | |
| 398 | /** Returns the number of matrix/clip states on the SkCanvas' private stack. |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 399 | This will equal # save() calls - # restore() calls + 1. The save count on |
| 400 | a new canvas is 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | */ |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 402 | int getSaveCount() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | |
| 404 | /** Efficient way to pop any calls to save() that happened after the save |
commit-bot@chromium.org | ea7d08e | 2014-02-13 16:00:51 +0000 | [diff] [blame] | 405 | count reached saveCount. It is an error for saveCount to be greater than |
| 406 | getSaveCount(). To pop all the way back to the initial matrix/clip context |
| 407 | pass saveCount == 1. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 408 | @param saveCount The number of save() levels to restore from |
| 409 | */ |
| 410 | void restoreToCount(int saveCount); |
| 411 | |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 412 | /** Returns true if drawing is currently going to a layer (from saveLayer) |
| 413 | * rather than to the root device. |
| 414 | */ |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 415 | virtual bool isDrawingToLayer() const; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 416 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 417 | /** Preconcat the current matrix with the specified translation |
| 418 | @param dx The distance to translate in X |
| 419 | @param dy The distance to translate in Y |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | */ |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 421 | void translate(SkScalar dx, SkScalar dy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | |
| 423 | /** Preconcat the current matrix with the specified scale. |
| 424 | @param sx The amount to scale in X |
| 425 | @param sy The amount to scale in Y |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | */ |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 427 | void scale(SkScalar sx, SkScalar sy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | |
| 429 | /** Preconcat the current matrix with the specified rotation. |
| 430 | @param degrees The amount to rotate, in degrees |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 431 | */ |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 432 | void rotate(SkScalar degrees); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | |
| 434 | /** Preconcat the current matrix with the specified skew. |
| 435 | @param sx The amount to skew in X |
| 436 | @param sy The amount to skew in Y |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | */ |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 438 | void skew(SkScalar sx, SkScalar sy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 439 | |
| 440 | /** Preconcat the current matrix with the specified matrix. |
| 441 | @param matrix The matrix to preconcatenate with the current matrix |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | */ |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 443 | void concat(const SkMatrix& matrix); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 444 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 445 | /** Replace the current matrix with a copy of the specified matrix. |
| 446 | @param matrix The matrix that will be copied into the current matrix. |
| 447 | */ |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 448 | void setMatrix(const SkMatrix& matrix); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 449 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | /** Helper for setMatrix(identity). Sets the current matrix to identity. |
| 451 | */ |
| 452 | void resetMatrix(); |
| 453 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 454 | /** |
| 455 | * Modify the current clip with the specified rectangle. |
| 456 | * @param rect The rect to combine with the current clip |
| 457 | * @param op The region op to apply to the current clip |
| 458 | * @param doAntiAlias true if the clip should be antialiased |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 459 | */ |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 460 | void clipRect(const SkRect& rect, |
| 461 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 462 | bool doAntiAlias = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 463 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 464 | /** |
| 465 | * Modify the current clip with the specified SkRRect. |
| 466 | * @param rrect The rrect to combine with the current clip |
| 467 | * @param op The region op to apply to the current clip |
| 468 | * @param doAntiAlias true if the clip should be antialiased |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 469 | */ |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 470 | void clipRRect(const SkRRect& rrect, |
| 471 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 472 | bool doAntiAlias = false); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Modify the current clip with the specified path. |
| 476 | * @param path The path to combine with the current clip |
| 477 | * @param op The region op to apply to the current clip |
| 478 | * @param doAntiAlias true if the clip should be antialiased |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 479 | */ |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 480 | void clipPath(const SkPath& path, |
| 481 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 482 | bool doAntiAlias = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 483 | |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 484 | /** EXPERIMENTAL -- only used for testing |
| 485 | Set to false to force clips to be hard, even if doAntiAlias=true is |
| 486 | passed to clipRect or clipPath. |
| 487 | */ |
| 488 | void setAllowSoftClip(bool allow) { |
| 489 | fAllowSoftClip = allow; |
| 490 | } |
| 491 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 492 | /** EXPERIMENTAL -- only used for testing |
| 493 | Set to simplify clip stack using path ops. |
| 494 | */ |
| 495 | void setAllowSimplifyClip(bool allow) { |
| 496 | fAllowSimplifyClip = allow; |
| 497 | } |
| 498 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | /** Modify the current clip with the specified region. Note that unlike |
| 500 | clipRect() and clipPath() which transform their arguments by the current |
| 501 | matrix, clipRegion() assumes its argument is already in device |
| 502 | coordinates, and so no transformation is performed. |
| 503 | @param deviceRgn The region to apply to the current clip |
| 504 | @param op The region op to apply to the current clip |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 505 | */ |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 506 | void clipRegion(const SkRegion& deviceRgn, |
| 507 | SkRegion::Op op = SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | |
| 509 | /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the |
| 510 | specified region. This does not intersect or in any other way account |
| 511 | for the existing clip region. |
| 512 | @param deviceRgn The region to copy into the current clip. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 513 | */ |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 514 | void setClipRegion(const SkRegion& deviceRgn) { |
| 515 | this->clipRegion(deviceRgn, SkRegion::kReplace_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 516 | } |
| 517 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 518 | /** Return true if the specified rectangle, after being transformed by the |
| 519 | current matrix, would lie completely outside of the current clip. Call |
| 520 | this to check if an area you intend to draw into is clipped out (and |
| 521 | therefore you can skip making the draw calls). |
| 522 | @param rect the rect to compare with the current clip |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | @return true if the rect (transformed by the canvas' matrix) does not |
| 524 | intersect with the canvas' clip |
| 525 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 526 | bool quickReject(const SkRect& rect) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 527 | |
| 528 | /** Return true if the specified path, after being transformed by the |
| 529 | current matrix, would lie completely outside of the current clip. Call |
| 530 | this to check if an area you intend to draw into is clipped out (and |
| 531 | therefore you can skip making the draw calls). Note, for speed it may |
| 532 | return false even if the path itself might not intersect the clip |
| 533 | (i.e. the bounds of the path intersects, but the path does not). |
| 534 | @param path The path to compare with the current clip |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | @return true if the path (transformed by the canvas' matrix) does not |
| 536 | intersect with the canvas' clip |
| 537 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 538 | bool quickReject(const SkPath& path) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 539 | |
| 540 | /** Return true if the horizontal band specified by top and bottom is |
| 541 | completely clipped out. This is a conservative calculation, meaning |
| 542 | that it is possible that if the method returns false, the band may still |
| 543 | in fact be clipped out, but the converse is not true. If this method |
| 544 | returns true, then the band is guaranteed to be clipped out. |
| 545 | @param top The top of the horizontal band to compare with the clip |
| 546 | @param bottom The bottom of the horizontal and to compare with the clip |
| 547 | @return true if the horizontal band is completely clipped out (i.e. does |
| 548 | not intersect the current clip) |
| 549 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 550 | bool quickRejectY(SkScalar top, SkScalar bottom) const { |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 551 | SkASSERT(top <= bottom); |
commit-bot@chromium.org | 9836bc3 | 2014-02-14 19:52:18 +0000 | [diff] [blame] | 552 | |
| 553 | #ifndef SK_WILL_NEVER_DRAW_PERSPECTIVE_TEXT |
| 554 | // TODO: add a hasPerspective method similar to getLocalClipBounds. This |
| 555 | // would cache the SkMatrix::hasPerspective result. Alternatively, have |
| 556 | // the MC stack just set a hasPerspective boolean as it is updated. |
| 557 | if (this->getTotalMatrix().hasPerspective()) { |
skia.committer@gmail.com | a3b5327 | 2014-02-15 03:02:15 +0000 | [diff] [blame] | 558 | // TODO: consider implementing some half-plane test between the |
commit-bot@chromium.org | 9836bc3 | 2014-02-14 19:52:18 +0000 | [diff] [blame] | 559 | // two Y planes and the device-bounds (i.e., project the top and |
| 560 | // bottom Y planes and then determine if the clip bounds is completely |
| 561 | // outside either one). |
| 562 | return false; |
| 563 | } |
| 564 | #endif |
| 565 | |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 566 | const SkRect& clipR = this->getLocalClipBounds(); |
djsollen@google.com | 92d2a29 | 2012-02-27 16:17:59 +0000 | [diff] [blame] | 567 | // In the case where the clip is empty and we are provided with a |
| 568 | // negative top and positive bottom parameter then this test will return |
| 569 | // false even though it will be clipped. We have chosen to exclude that |
| 570 | // check as it is rare and would result double the comparisons. |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 571 | return top >= clipR.fBottom || bottom <= clipR.fTop; |
djsollen@google.com | 92d2a29 | 2012-02-27 16:17:59 +0000 | [diff] [blame] | 572 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 573 | |
| 574 | /** Return the bounds of the current clip (in local coordinates) in the |
| 575 | bounds parameter, and return true if it is non-empty. This can be useful |
| 576 | in a way similar to quickReject, in that it tells you that drawing |
| 577 | outside of these bounds will be clipped out. |
| 578 | */ |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 579 | virtual bool getClipBounds(SkRect* bounds) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 580 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 581 | /** Return the bounds of the current clip, in device coordinates; returns |
| 582 | true if non-empty. Maybe faster than getting the clip explicitly and |
| 583 | then taking its bounds. |
| 584 | */ |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 585 | virtual bool getClipDeviceBounds(SkIRect* bounds) const; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 586 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 587 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 588 | /** Fill the entire canvas' bitmap (restricted to the current clip) with the |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 589 | specified ARGB color, using the specified mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 590 | @param a the alpha component (0..255) of the color to fill the canvas |
| 591 | @param r the red component (0..255) of the color to fill the canvas |
| 592 | @param g the green component (0..255) of the color to fill the canvas |
| 593 | @param b the blue component (0..255) of the color to fill the canvas |
| 594 | @param mode the mode to apply the color in (defaults to SrcOver) |
| 595 | */ |
| 596 | void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 597 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 598 | |
| 599 | /** Fill the entire canvas' bitmap (restricted to the current clip) with the |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 600 | specified color and mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 601 | @param color the color to draw with |
| 602 | @param mode the mode to apply the color in (defaults to SrcOver) |
| 603 | */ |
| 604 | void drawColor(SkColor color, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 605 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 607 | /** |
| 608 | * This erases the entire drawing surface to the specified color, |
| 609 | * irrespective of the clip. It does not blend with the previous pixels, |
| 610 | * but always overwrites them. |
| 611 | * |
| 612 | * It is roughly equivalent to the following: |
| 613 | * canvas.save(); |
| 614 | * canvas.clipRect(hugeRect, kReplace_Op); |
| 615 | * paint.setColor(color); |
| 616 | * paint.setXfermodeMode(kSrc_Mode); |
| 617 | * canvas.drawPaint(paint); |
| 618 | * canvas.restore(); |
| 619 | * though it is almost always much more efficient. |
| 620 | */ |
| 621 | virtual void clear(SkColor); |
| 622 | |
| 623 | /** |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 624 | * This makes the contents of the canvas undefined. Subsequent calls that |
| 625 | * require reading the canvas contents will produce undefined results. Examples |
| 626 | * include blending and readPixels. The actual implementation is backend- |
| 627 | * dependent and one legal implementation is to do nothing. Like clear(), this |
| 628 | * ignores the clip. |
| 629 | * |
| 630 | * This function should only be called if the caller intends to subsequently |
| 631 | * draw to the canvas. The canvas may do real work at discard() time in order |
| 632 | * to optimize performance on subsequent draws. Thus, if you call this and then |
| 633 | * never draw to the canvas subsequently you may pay a perfomance penalty. |
| 634 | */ |
| 635 | void discard() { this->onDiscard(); } |
| 636 | |
| 637 | /** |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 638 | * Fill the entire canvas' bitmap (restricted to the current clip) with the |
| 639 | * specified paint. |
| 640 | * @param paint The paint used to fill the canvas |
| 641 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 642 | virtual void drawPaint(const SkPaint& paint); |
| 643 | |
| 644 | enum PointMode { |
| 645 | /** drawPoints draws each point separately */ |
| 646 | kPoints_PointMode, |
| 647 | /** drawPoints draws each pair of points as a line segment */ |
| 648 | kLines_PointMode, |
| 649 | /** drawPoints draws the array of points as a polygon */ |
| 650 | kPolygon_PointMode |
| 651 | }; |
| 652 | |
| 653 | /** Draw a series of points, interpreted based on the PointMode mode. For |
| 654 | all modes, the count parameter is interpreted as the total number of |
| 655 | points. For kLine mode, count/2 line segments are drawn. |
| 656 | For kPoint mode, each point is drawn centered at its coordinate, and its |
| 657 | size is specified by the paint's stroke-width. It draws as a square, |
| 658 | unless the paint's cap-type is round, in which the points are drawn as |
| 659 | circles. |
| 660 | For kLine mode, each pair of points is drawn as a line segment, |
| 661 | respecting the paint's settings for cap/join/width. |
| 662 | For kPolygon mode, the entire array is drawn as a series of connected |
| 663 | line segments. |
| 664 | Note that, while similar, kLine and kPolygon modes draw slightly |
| 665 | differently than the equivalent path built with a series of moveto, |
| 666 | lineto calls, in that the path will draw all of its contours at once, |
| 667 | with no interactions if contours intersect each other (think XOR |
| 668 | xfermode). drawPoints always draws each element one at a time. |
| 669 | @param mode PointMode specifying how to draw the array of points. |
| 670 | @param count The number of points in the array |
| 671 | @param pts Array of points to draw |
| 672 | @param paint The paint used to draw the points |
| 673 | */ |
| 674 | virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 675 | const SkPaint& paint); |
| 676 | |
| 677 | /** Helper method for drawing a single point. See drawPoints() for a more |
| 678 | details. |
| 679 | */ |
| 680 | void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 681 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 682 | /** Draws a single pixel in the specified color. |
| 683 | @param x The X coordinate of which pixel to draw |
| 684 | @param y The Y coordiante of which pixel to draw |
| 685 | @param color The color to draw |
| 686 | */ |
| 687 | void drawPoint(SkScalar x, SkScalar y, SkColor color); |
| 688 | |
| 689 | /** Draw a line segment with the specified start and stop x,y coordinates, |
| 690 | using the specified paint. NOTE: since a line is always "framed", the |
| 691 | paint's Style is ignored. |
| 692 | @param x0 The x-coordinate of the start point of the line |
| 693 | @param y0 The y-coordinate of the start point of the line |
| 694 | @param x1 The x-coordinate of the end point of the line |
| 695 | @param y1 The y-coordinate of the end point of the line |
| 696 | @param paint The paint used to draw the line |
| 697 | */ |
| 698 | void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 699 | const SkPaint& paint); |
| 700 | |
| 701 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 702 | will be filled or stroked based on the Style in the paint. |
| 703 | @param rect The rect to be drawn |
| 704 | @param paint The paint used to draw the rect |
| 705 | */ |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 706 | virtual void drawRect(const SkRect& rect, const SkPaint& paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 707 | |
| 708 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 709 | will be filled or framed based on the Style in the paint. |
| 710 | @param rect The rect to be drawn |
| 711 | @param paint The paint used to draw the rect |
| 712 | */ |
reed@google.com | 87001ed | 2014-02-17 16:28:05 +0000 | [diff] [blame] | 713 | void drawIRect(const SkIRect& rect, const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 714 | SkRect r; |
| 715 | r.set(rect); // promotes the ints to scalars |
| 716 | this->drawRect(r, paint); |
| 717 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 718 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 719 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 720 | will be filled or framed based on the Style in the paint. |
| 721 | @param left The left side of the rectangle to be drawn |
| 722 | @param top The top side of the rectangle to be drawn |
| 723 | @param right The right side of the rectangle to be drawn |
| 724 | @param bottom The bottom side of the rectangle to be drawn |
| 725 | @param paint The paint used to draw the rect |
| 726 | */ |
| 727 | void drawRectCoords(SkScalar left, SkScalar top, SkScalar right, |
| 728 | SkScalar bottom, const SkPaint& paint); |
| 729 | |
| 730 | /** Draw the specified oval using the specified paint. The oval will be |
| 731 | filled or framed based on the Style in the paint. |
| 732 | @param oval The rectangle bounds of the oval to be drawn |
| 733 | @param paint The paint used to draw the oval |
| 734 | */ |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 735 | virtual void drawOval(const SkRect& oval, const SkPaint&); |
| 736 | |
| 737 | /** |
| 738 | * Draw the specified RRect using the specified paint The rrect will be filled or stroked |
| 739 | * based on the Style in the paint. |
| 740 | * |
| 741 | * @param rrect The round-rect to draw |
| 742 | * @param paint The paint used to draw the round-rect |
| 743 | */ |
| 744 | virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 745 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 746 | /** |
| 747 | * Draw the annulus formed by the outer and inner rrects. The results |
| 748 | * are undefined if the outer does not contain the inner. |
| 749 | */ |
| 750 | void drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint&); |
| 751 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 752 | /** Draw the specified circle using the specified paint. If radius is <= 0, |
| 753 | then nothing will be drawn. The circle will be filled |
| 754 | or framed based on the Style in the paint. |
| 755 | @param cx The x-coordinate of the center of the cirle to be drawn |
| 756 | @param cy The y-coordinate of the center of the cirle to be drawn |
| 757 | @param radius The radius of the cirle to be drawn |
| 758 | @param paint The paint used to draw the circle |
| 759 | */ |
| 760 | void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 761 | const SkPaint& paint); |
| 762 | |
| 763 | /** Draw the specified arc, which will be scaled to fit inside the |
| 764 | specified oval. If the sweep angle is >= 360, then the oval is drawn |
| 765 | completely. Note that this differs slightly from SkPath::arcTo, which |
| 766 | treats the sweep angle mod 360. |
| 767 | @param oval The bounds of oval used to define the shape of the arc |
| 768 | @param startAngle Starting angle (in degrees) where the arc begins |
| 769 | @param sweepAngle Sweep angle (in degrees) measured clockwise |
| 770 | @param useCenter true means include the center of the oval. For filling |
| 771 | this will draw a wedge. False means just use the arc. |
| 772 | @param paint The paint used to draw the arc |
| 773 | */ |
| 774 | void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 775 | bool useCenter, const SkPaint& paint); |
| 776 | |
| 777 | /** Draw the specified round-rect using the specified paint. The round-rect |
| 778 | will be filled or framed based on the Style in the paint. |
| 779 | @param rect The rectangular bounds of the roundRect to be drawn |
| 780 | @param rx The x-radius of the oval used to round the corners |
| 781 | @param ry The y-radius of the oval used to round the corners |
| 782 | @param paint The paint used to draw the roundRect |
| 783 | */ |
| 784 | void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, |
| 785 | const SkPaint& paint); |
| 786 | |
| 787 | /** Draw the specified path using the specified paint. The path will be |
| 788 | filled or framed based on the Style in the paint. |
| 789 | @param path The path to be drawn |
| 790 | @param paint The paint used to draw the path |
| 791 | */ |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 792 | virtual void drawPath(const SkPath& path, const SkPaint& paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 793 | |
| 794 | /** Draw the specified bitmap, with its top/left corner at (x,y), using the |
| 795 | specified paint, transformed by the current matrix. Note: if the paint |
| 796 | contains a maskfilter that generates a mask which extends beyond the |
| 797 | bitmap's original width/height, then the bitmap will be drawn as if it |
| 798 | were in a Shader with CLAMP mode. Thus the color outside of the original |
| 799 | width/height will be the edge color replicated. |
commit-bot@chromium.org | 91246b9 | 2013-12-05 15:43:19 +0000 | [diff] [blame] | 800 | |
| 801 | If a shader is present on the paint it will be ignored, except in the |
reed@google.com | f20fc24 | 2014-03-26 13:44:58 +0000 | [diff] [blame] | 802 | case where the bitmap is kAlpha_8_SkColorType. In that case, the color is |
commit-bot@chromium.org | 91246b9 | 2013-12-05 15:43:19 +0000 | [diff] [blame] | 803 | generated by the shader. |
| 804 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 805 | @param bitmap The bitmap to be drawn |
| 806 | @param left The position of the left side of the bitmap being drawn |
| 807 | @param top The position of the top side of the bitmap being drawn |
| 808 | @param paint The paint used to draw the bitmap, or NULL |
| 809 | */ |
| 810 | virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 811 | const SkPaint* paint = NULL); |
| 812 | |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 813 | enum DrawBitmapRectFlags { |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 814 | kNone_DrawBitmapRectFlag = 0x0, |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 815 | /** |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 816 | * When filtering is enabled, allow the color samples outside of |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 817 | * the src rect (but still in the src bitmap) to bleed into the |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 818 | * drawn portion |
| 819 | */ |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 820 | kBleed_DrawBitmapRectFlag = 0x1, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 821 | }; |
| 822 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 823 | /** Draw the specified bitmap, with the specified matrix applied (before the |
| 824 | canvas' matrix is applied). |
| 825 | @param bitmap The bitmap to be drawn |
| 826 | @param src Optional: specify the subset of the bitmap to be drawn |
| 827 | @param dst The destination rectangle where the scaled/translated |
| 828 | image will be drawn |
| 829 | @param paint The paint used to draw the bitmap, or NULL |
| 830 | */ |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 831 | virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
| 832 | const SkRect& dst, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 833 | const SkPaint* paint = NULL, |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 834 | DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 835 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 836 | void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 837 | const SkPaint* paint = NULL) { |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 838 | this->drawBitmapRectToRect(bitmap, NULL, dst, paint, kNone_DrawBitmapRectFlag); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* isrc, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 842 | const SkRect& dst, const SkPaint* paint = NULL, |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 843 | DrawBitmapRectFlags flags = kNone_DrawBitmapRectFlag) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 844 | SkRect realSrcStorage; |
| 845 | SkRect* realSrcPtr = NULL; |
| 846 | if (isrc) { |
| 847 | realSrcStorage.set(*isrc); |
| 848 | realSrcPtr = &realSrcStorage; |
| 849 | } |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 850 | this->drawBitmapRectToRect(bitmap, realSrcPtr, dst, paint, flags); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 851 | } |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 852 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 853 | virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
| 854 | const SkPaint* paint = NULL); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 855 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 856 | /** |
| 857 | * Draw the bitmap stretched differentially to fit into dst. |
| 858 | * center is a rect within the bitmap, and logically divides the bitmap |
| 859 | * into 9 sections (3x3). For example, if the middle pixel of a [5x5] |
| 860 | * bitmap is the "center", then the center-rect should be [2, 2, 3, 3]. |
| 861 | * |
| 862 | * If the dst is >= the bitmap size, then... |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 863 | * - The 4 corners are not stretched at all. |
| 864 | * - The sides are stretched in only one axis. |
| 865 | * - The center is stretched in both axes. |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 866 | * Else, for each axis where dst < bitmap, |
| 867 | * - The corners shrink proportionally |
| 868 | * - The sides (along the shrink axis) and center are not drawn |
| 869 | */ |
| 870 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 871 | const SkRect& dst, const SkPaint* paint = NULL); |
| 872 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 873 | /** Draw the specified bitmap, with its top/left corner at (x,y), |
| 874 | NOT transformed by the current matrix. Note: if the paint |
| 875 | contains a maskfilter that generates a mask which extends beyond the |
| 876 | bitmap's original width/height, then the bitmap will be drawn as if it |
| 877 | were in a Shader with CLAMP mode. Thus the color outside of the original |
| 878 | width/height will be the edge color replicated. |
| 879 | @param bitmap The bitmap to be drawn |
| 880 | @param left The position of the left side of the bitmap being drawn |
| 881 | @param top The position of the top side of the bitmap being drawn |
| 882 | @param paint The paint used to draw the bitmap, or NULL |
| 883 | */ |
| 884 | virtual void drawSprite(const SkBitmap& bitmap, int left, int top, |
| 885 | const SkPaint* paint = NULL); |
| 886 | |
| 887 | /** Draw the text, with origin at (x,y), using the specified paint. |
| 888 | The origin is interpreted based on the Align setting in the paint. |
| 889 | @param text The text to be drawn |
| 890 | @param byteLength The number of bytes to read from the text parameter |
| 891 | @param x The x-coordinate of the origin of the text being drawn |
| 892 | @param y The y-coordinate of the origin of the text being drawn |
| 893 | @param paint The paint used for the text (e.g. color, size, style) |
| 894 | */ |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 895 | SK_LEGACY_DRAWTEXT_VIRTUAL void drawText(const void* text, size_t byteLength, SkScalar x, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 896 | SkScalar y, const SkPaint& paint); |
| 897 | |
| 898 | /** Draw the text, with each character/glyph origin specified by the pos[] |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 899 | array. The origin is interpreted by the Align setting in the paint. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 900 | @param text The text to be drawn |
| 901 | @param byteLength The number of bytes to read from the text parameter |
| 902 | @param pos Array of positions, used to position each character |
| 903 | @param paint The paint used for the text (e.g. color, size, style) |
| 904 | */ |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 905 | SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosText(const void* text, size_t byteLength, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 906 | const SkPoint pos[], const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 907 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 908 | /** Draw the text, with each character/glyph origin specified by the x |
| 909 | coordinate taken from the xpos[] array, and the y from the constY param. |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 910 | The origin is interpreted by the Align setting in the paint. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 911 | @param text The text to be drawn |
| 912 | @param byteLength The number of bytes to read from the text parameter |
| 913 | @param xpos Array of x-positions, used to position each character |
| 914 | @param constY The shared Y coordinate for all of the positions |
| 915 | @param paint The paint used for the text (e.g. color, size, style) |
| 916 | */ |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 917 | SK_LEGACY_DRAWTEXT_VIRTUAL void drawPosTextH(const void* text, size_t byteLength, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 918 | const SkScalar xpos[], SkScalar constY, |
| 919 | const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 920 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 921 | /** Draw the text, with origin at (x,y), using the specified paint, along |
| 922 | the specified path. The paint's Align setting determins where along the |
| 923 | path to start the text. |
| 924 | @param text The text to be drawn |
| 925 | @param byteLength The number of bytes to read from the text parameter |
| 926 | @param path The path the text should follow for its baseline |
| 927 | @param hOffset The distance along the path to add to the text's |
| 928 | starting position |
| 929 | @param vOffset The distance above(-) or below(+) the path to |
| 930 | position the text |
| 931 | @param paint The paint used for the text |
| 932 | */ |
| 933 | void drawTextOnPathHV(const void* text, size_t byteLength, |
| 934 | const SkPath& path, SkScalar hOffset, |
| 935 | SkScalar vOffset, const SkPaint& paint); |
| 936 | |
| 937 | /** Draw the text, with origin at (x,y), using the specified paint, along |
| 938 | the specified path. The paint's Align setting determins where along the |
| 939 | path to start the text. |
| 940 | @param text The text to be drawn |
| 941 | @param byteLength The number of bytes to read from the text parameter |
| 942 | @param path The path the text should follow for its baseline |
| 943 | @param matrix (may be null) Applied to the text before it is |
| 944 | mapped onto the path |
| 945 | @param paint The paint used for the text |
| 946 | */ |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 947 | SK_LEGACY_DRAWTEXT_VIRTUAL void drawTextOnPath(const void* text, size_t byteLength, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 948 | const SkPath& path, const SkMatrix* matrix, |
| 949 | const SkPaint& paint); |
| 950 | |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 951 | /** PRIVATE / EXPERIMENTAL -- do not call |
| 952 | Perform back-end analysis/optimization of a picture. This may attach |
| 953 | optimization data to the picture which can be used by a later |
| 954 | drawPicture call. |
| 955 | @param picture The recorded drawing commands to analyze/optimize |
| 956 | */ |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 957 | void EXPERIMENTAL_optimize(const SkPicture* picture); |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 958 | |
commit-bot@chromium.org | c873329 | 2014-04-11 15:54:14 +0000 | [diff] [blame] | 959 | /** PRIVATE / EXPERIMENTAL -- do not call |
| 960 | Purge all the discardable optimization information associated with |
| 961 | 'picture'. If NULL is passed in, purge all discardable information. |
| 962 | */ |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 963 | void EXPERIMENTAL_purge(const SkPicture* picture); |
commit-bot@chromium.org | c873329 | 2014-04-11 15:54:14 +0000 | [diff] [blame] | 964 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 965 | /** Draw the picture into this canvas. This method effective brackets the |
| 966 | playback of the picture's draw calls with save/restore, so the state |
djsollen@google.com | a44de96 | 2013-01-02 16:59:19 +0000 | [diff] [blame] | 967 | of this canvas will be unchanged after this call. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 968 | @param picture The recorded drawing commands to playback into this |
| 969 | canvas. |
| 970 | */ |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 971 | void drawPicture(const SkPicture* picture); |
| 972 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 973 | enum VertexMode { |
| 974 | kTriangles_VertexMode, |
| 975 | kTriangleStrip_VertexMode, |
| 976 | kTriangleFan_VertexMode |
| 977 | }; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 978 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 979 | /** Draw the array of vertices, interpreted as triangles (based on mode). |
commit-bot@chromium.org | 559a883 | 2014-05-30 10:08:22 +0000 | [diff] [blame] | 980 | |
| 981 | If both textures and vertex-colors are NULL, it strokes hairlines with |
| 982 | the paint's color. This behavior is a useful debugging mode to visualize |
| 983 | the mesh. |
| 984 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 985 | @param vmode How to interpret the array of vertices |
| 986 | @param vertexCount The number of points in the vertices array (and |
| 987 | corresponding texs and colors arrays if non-null) |
| 988 | @param vertices Array of vertices for the mesh |
| 989 | @param texs May be null. If not null, specifies the coordinate |
robertphillips@google.com | 631a59b | 2013-07-31 14:57:53 +0000 | [diff] [blame] | 990 | in _texture_ space (not uv space) for each vertex. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 991 | @param colors May be null. If not null, specifies a color for each |
| 992 | vertex, to be interpolated across the triangle. |
| 993 | @param xmode Used if both texs and colors are present. In this |
| 994 | case the colors are combined with the texture using mode, |
| 995 | before being drawn using the paint. If mode is null, then |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 996 | kModulate_Mode is used. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 997 | @param indices If not null, array of indices to reference into the |
| 998 | vertex (texs, colors) array. |
| 999 | @param indexCount number of entries in the indices array (if not null) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1000 | @param paint Specifies the shader/texture if present. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1001 | */ |
| 1002 | virtual void drawVertices(VertexMode vmode, int vertexCount, |
| 1003 | const SkPoint vertices[], const SkPoint texs[], |
| 1004 | const SkColor colors[], SkXfermode* xmode, |
| 1005 | const uint16_t indices[], int indexCount, |
| 1006 | const SkPaint& paint); |
| 1007 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 1008 | /** Send a blob of data to the canvas. |
| 1009 | For canvases that draw, this call is effectively a no-op, as the data |
| 1010 | is not parsed, but just ignored. However, this call exists for |
| 1011 | subclasses like SkPicture's recording canvas, that can store the data |
| 1012 | and then play it back later (via another call to drawData). |
| 1013 | */ |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 1014 | virtual void drawData(const void* data, size_t length) { |
| 1015 | // do nothing. Subclasses may do something with the data |
| 1016 | } |
| 1017 | |
skia.committer@gmail.com | a5d3e77 | 2013-05-30 07:01:29 +0000 | [diff] [blame] | 1018 | /** Add comments. beginCommentGroup/endCommentGroup open/close a new group. |
| 1019 | Each comment added via addComment is notionally attached to its |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 1020 | enclosing group. Top-level comments simply belong to no group. |
| 1021 | */ |
| 1022 | virtual void beginCommentGroup(const char* description) { |
| 1023 | // do nothing. Subclasses may do something |
| 1024 | } |
| 1025 | virtual void addComment(const char* kywd, const char* value) { |
| 1026 | // do nothing. Subclasses may do something |
| 1027 | } |
| 1028 | virtual void endCommentGroup() { |
| 1029 | // do nothing. Subclasses may do something |
| 1030 | } |
| 1031 | |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 1032 | /** |
| 1033 | * With this call the client asserts that subsequent draw operations (up to the |
| 1034 | * matching popCull()) are fully contained within the given bounding box. The assertion |
| 1035 | * is not enforced, but the information might be used to quick-reject command blocks, |
| 1036 | * so an incorrect bounding box may result in incomplete rendering. |
| 1037 | */ |
commit-bot@chromium.org | 520cf8b | 2014-03-20 20:25:14 +0000 | [diff] [blame] | 1038 | void pushCull(const SkRect& cullRect); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 1039 | |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 1040 | /** |
| 1041 | * Terminates the current culling block, and restores the previous one (if any). |
| 1042 | */ |
commit-bot@chromium.org | 520cf8b | 2014-03-20 20:25:14 +0000 | [diff] [blame] | 1043 | void popCull(); |
| 1044 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1045 | ////////////////////////////////////////////////////////////////////////// |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1046 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1047 | /** Get the current filter object. The filter's reference count is not |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 1048 | affected. The filter is saved/restored, just like the matrix and clip. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1049 | @return the canvas' filter (or NULL). |
| 1050 | */ |
| 1051 | SkDrawFilter* getDrawFilter() const; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1052 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1053 | /** Set the new filter (or NULL). Pass NULL to clear any existing filter. |
| 1054 | As a convenience, the parameter is returned. If an existing filter |
| 1055 | exists, its refcnt is decrement. If the new filter is not null, its |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 1056 | refcnt is incremented. The filter is saved/restored, just like the |
| 1057 | matrix and clip. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1058 | @param filter the new filter (or NULL) |
| 1059 | @return the new filter |
| 1060 | */ |
| 1061 | virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter); |
| 1062 | |
| 1063 | ////////////////////////////////////////////////////////////////////////// |
| 1064 | |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1065 | /** |
| 1066 | * Return true if the current clip is empty (i.e. nothing will draw). |
| 1067 | * Note: this is not always a free call, so it should not be used |
| 1068 | * more often than necessary. However, once the canvas has computed this |
| 1069 | * result, subsequent calls will be cheap (until the clip state changes, |
| 1070 | * which can happen on any clip..() or restore() call. |
| 1071 | */ |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1072 | virtual bool isClipEmpty() const; |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1073 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1074 | /** |
| 1075 | * Returns true if the current clip is just a (non-empty) rectangle. |
| 1076 | * Returns false if the clip is empty, or if it is complex. |
| 1077 | */ |
| 1078 | virtual bool isClipRect() const; |
| 1079 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1080 | /** Return the current matrix on the canvas. |
| 1081 | This does not account for the translate in any of the devices. |
| 1082 | @return The current matrix on the canvas. |
| 1083 | */ |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1084 | const SkMatrix& getTotalMatrix() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1085 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1086 | #ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1087 | enum ClipType { |
| 1088 | kEmpty_ClipType = 0, |
| 1089 | kRect_ClipType, |
| 1090 | kComplex_ClipType |
| 1091 | }; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1092 | /** Returns a description of the total clip; may be cheaper than |
| 1093 | getting the clip and querying it directly. |
| 1094 | */ |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1095 | virtual ClipType getClipType() const; |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1096 | #endif |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1097 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 1098 | /** Return the clip stack. The clip stack stores all the individual |
| 1099 | * clips organized by the save/restore frame in which they were |
| 1100 | * added. |
| 1101 | * @return the current clip stack ("list" of individual clip elements) |
| 1102 | */ |
| 1103 | const SkClipStack* getClipStack() const { |
| 1104 | return &fClipStack; |
| 1105 | } |
| 1106 | |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 1107 | typedef SkCanvasClipVisitor ClipVisitor; |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1108 | /** |
| 1109 | * Replays the clip operations, back to front, that have been applied to |
| 1110 | * the canvas, calling the appropriate method on the visitor for each |
| 1111 | * clip. All clips have already been transformed into device space. |
| 1112 | */ |
| 1113 | void replayClips(ClipVisitor*) const; |
| 1114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1115 | /////////////////////////////////////////////////////////////////////////// |
| 1116 | |
| 1117 | /** After calling saveLayer(), there can be any number of devices that make |
| 1118 | up the top-most drawing area. LayerIter can be used to iterate through |
| 1119 | those devices. Note that the iterator is only valid until the next API |
| 1120 | call made on the canvas. Ownership of all pointers in the iterator stays |
| 1121 | with the canvas, so none of them should be modified or deleted. |
| 1122 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 1123 | class SK_API LayerIter /*: SkNoncopyable*/ { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1124 | public: |
| 1125 | /** Initialize iterator with canvas, and set values for 1st device */ |
| 1126 | LayerIter(SkCanvas*, bool skipEmptyClips); |
| 1127 | ~LayerIter(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1129 | /** Return true if the iterator is done */ |
| 1130 | bool done() const { return fDone; } |
| 1131 | /** Cycle to the next device */ |
| 1132 | void next(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1134 | // These reflect the current device in the iterator |
| 1135 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1136 | SkBaseDevice* device() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1137 | const SkMatrix& matrix() const; |
| 1138 | const SkRegion& clip() const; |
| 1139 | const SkPaint& paint() const; |
| 1140 | int x() const; |
| 1141 | int y() const; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1143 | private: |
| 1144 | // used to embed the SkDrawIter object directly in our instance, w/o |
| 1145 | // having to expose that class def to the public. There is an assert |
| 1146 | // in our constructor to ensure that fStorage is large enough |
| 1147 | // (though needs to be a compile-time-assert!). We use intptr_t to work |
| 1148 | // safely with 32 and 64 bit machines (to ensure the storage is enough) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1149 | intptr_t fStorage[32]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1150 | class SkDrawIter* fImpl; // this points at fStorage |
| 1151 | SkPaint fDefaultPaint; |
| 1152 | bool fDone; |
| 1153 | }; |
| 1154 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1155 | // don't call |
| 1156 | const SkRegion& internal_private_getTotalClip() const; |
| 1157 | // don't call |
| 1158 | void internal_private_getTotalClipAsPath(SkPath*) const; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1159 | // don't call |
| 1160 | GrRenderTarget* internal_private_accessTopLayerRenderTarget(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1161 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1162 | protected: |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1163 | // default impl defers to getDevice()->newSurface(info) |
| 1164 | virtual SkSurface* onNewSurface(const SkImageInfo&); |
| 1165 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1166 | // default impl defers to its device |
| 1167 | virtual const void* onPeekPixels(SkImageInfo*, size_t* rowBytes); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1168 | virtual void* onAccessTopLayerPixels(SkImageInfo*, size_t* rowBytes); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1169 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1170 | // Subclass save/restore notifiers. |
| 1171 | // Overriders should call the corresponding INHERITED method up the inheritance chain. |
| 1172 | // willSaveLayer()'s return value may suppress full layer allocation. |
| 1173 | enum SaveLayerStrategy { |
| 1174 | kFullLayer_SaveLayerStrategy, |
| 1175 | kNoLayer_SaveLayerStrategy |
| 1176 | }; |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1177 | |
fmalita | 6ca763f | 2014-06-17 13:52:18 -0700 | [diff] [blame] | 1178 | virtual void willSave() {} |
commit-bot@chromium.org | fc6dfba | 2014-05-14 13:13:44 +0000 | [diff] [blame] | 1179 | virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) { |
| 1180 | return kFullLayer_SaveLayerStrategy; |
| 1181 | } |
| 1182 | virtual void willRestore() {} |
| 1183 | virtual void didConcat(const SkMatrix&) {} |
| 1184 | virtual void didSetMatrix(const SkMatrix&) {} |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1185 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1186 | virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&); |
| 1187 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 1188 | virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, |
| 1189 | SkScalar y, const SkPaint& paint); |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 1190 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 1191 | virtual void onDrawPosText(const void* text, size_t byteLength, |
| 1192 | const SkPoint pos[], const SkPaint& paint); |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 1193 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 1194 | virtual void onDrawPosTextH(const void* text, size_t byteLength, |
| 1195 | const SkScalar xpos[], SkScalar constY, |
| 1196 | const SkPaint& paint); |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 1197 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 1198 | virtual void onDrawTextOnPath(const void* text, size_t byteLength, |
| 1199 | const SkPath& path, const SkMatrix* matrix, |
| 1200 | const SkPaint& paint); |
| 1201 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1202 | enum ClipEdgeStyle { |
| 1203 | kHard_ClipEdgeStyle, |
| 1204 | kSoft_ClipEdgeStyle |
| 1205 | }; |
| 1206 | |
| 1207 | virtual void onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle); |
| 1208 | virtual void onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle); |
| 1209 | virtual void onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle); |
| 1210 | virtual void onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op); |
| 1211 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1212 | virtual void onDiscard(); |
| 1213 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 1214 | virtual void onDrawPicture(const SkPicture* picture); |
| 1215 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1216 | // Returns the canvas to be used by DrawIter. Default implementation |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 1217 | // returns this. Subclasses that encapsulate an indirect canvas may |
| 1218 | // need to overload this method. The impl must keep track of this, as it |
| 1219 | // is not released or deleted by the caller. |
| 1220 | virtual SkCanvas* canvasForDrawIter(); |
| 1221 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1222 | // Clip rectangle bounds. Called internally by saveLayer. |
| 1223 | // returns false if the entire rectangle is entirely clipped out |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 1224 | // If non-NULL, The imageFilter parameter will be used to expand the clip |
| 1225 | // and offscreen bounds for any margin required by the filter DAG. |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1226 | bool clipRectBounds(const SkRect* bounds, SaveFlags flags, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 1227 | SkIRect* intersection, |
| 1228 | const SkImageFilter* imageFilter = NULL); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1229 | |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1230 | // Called by child classes that override clipPath and clipRRect to only |
| 1231 | // track fast conservative clip bounds, rather than exact clips. |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1232 | void updateClipConservativelyUsingBounds(const SkRect&, SkRegion::Op, |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1233 | bool inverseFilled); |
| 1234 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 1235 | // notify our surface (if we have one) that we are about to draw, so it |
| 1236 | // can perform copy-on-write or invalidate any cached images |
| 1237 | void predrawNotify(); |
| 1238 | |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 1239 | virtual void onPushCull(const SkRect& cullRect); |
| 1240 | virtual void onPopCull(); |
| 1241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1242 | private: |
| 1243 | class MCRec; |
| 1244 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1245 | SkClipStack fClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1246 | SkDeque fMCStack; |
| 1247 | // points to top of stack |
| 1248 | MCRec* fMCRec; |
| 1249 | // the first N recs that can fit here mean we won't call malloc |
| 1250 | uint32_t fMCRecStorage[32]; |
| 1251 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 1252 | int fSaveLayerCount; // number of successful saveLayer calls |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 1253 | int fCullCount; // number of active culls |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1254 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 1255 | SkMetaData* fMetaData; |
| 1256 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 1257 | SkSurface_Base* fSurfaceBase; |
| 1258 | SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; } |
| 1259 | void setSurfaceBase(SkSurface_Base* sb) { |
| 1260 | fSurfaceBase = sb; |
| 1261 | } |
| 1262 | friend class SkSurface_Base; |
junov@chromium.org | 45c3db8 | 2013-04-11 17:52:05 +0000 | [diff] [blame] | 1263 | friend class SkSurface_Gpu; |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 1264 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1265 | bool fDeviceCMDirty; // cleared by updateDeviceCMCache() |
| 1266 | void updateDeviceCMCache(); |
| 1267 | |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1268 | friend class SkDrawIter; // needs setupDrawForLayerDevice() |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1269 | friend class AutoDrawLooper; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1270 | friend class SkLua; // needs top layer size and offset |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 1271 | friend class SkDebugCanvas; // needs experimental fAllowSimplifyClip |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1272 | friend class SkDeferredDevice; // needs getTopDevice() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1273 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1274 | SkBaseDevice* createLayerDevice(const SkImageInfo&); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1275 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1276 | SkBaseDevice* init(SkBaseDevice*); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 1277 | |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 1278 | /** |
| 1279 | * DEPRECATED |
| 1280 | * |
| 1281 | * Specify a device for this canvas to draw into. If it is not null, its |
| 1282 | * reference count is incremented. If the canvas was already holding a |
| 1283 | * device, its reference count is decremented. The new device is returned. |
| 1284 | */ |
| 1285 | SkBaseDevice* setRootDevice(SkBaseDevice* device); |
skia.committer@gmail.com | 31acdea | 2014-02-18 03:01:51 +0000 | [diff] [blame] | 1286 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 1287 | /** |
| 1288 | * Gets the size/origin of the top level layer in global canvas coordinates. We don't want this |
| 1289 | * to be public because it exposes decisions about layer sizes that are internal to the canvas. |
| 1290 | */ |
| 1291 | SkISize getTopLayerSize() const; |
| 1292 | SkIPoint getTopLayerOrigin() const; |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 1293 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 1294 | // internal methods are not virtual, so they can safely be called by other |
| 1295 | // canvas apis, without confusing subclasses (like SkPictureRecording) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1296 | void internalDrawBitmap(const SkBitmap&, const SkMatrix& m, const SkPaint* paint); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1297 | void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1298 | const SkRect& dst, const SkPaint* paint, |
| 1299 | DrawBitmapRectFlags flags); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 1300 | void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 1301 | const SkRect& dst, const SkPaint* paint); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1302 | void internalDrawPaint(const SkPaint& paint); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1303 | int internalSaveLayer(const SkRect* bounds, const SkPaint* paint, |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1304 | SaveFlags, bool justForImageFilter, SaveLayerStrategy strategy); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1305 | void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1306 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1307 | // shared by save() and saveLayer() |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame^] | 1308 | int internalSave(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1309 | void internalRestore(); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 1310 | static void DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 1311 | const SkRect& r, SkScalar textSize); |
| 1312 | static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 1313 | const char text[], size_t byteLength, |
| 1314 | SkScalar x, SkScalar y); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1316 | /* These maintain a cache of the clip bounds in local coordinates, |
| 1317 | (converted to 2s-compliment if floats are slow). |
| 1318 | */ |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1319 | mutable SkRect fCachedLocalClipBounds; |
| 1320 | mutable bool fCachedLocalClipBoundsDirty; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 1321 | bool fAllowSoftClip; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1322 | bool fAllowSimplifyClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1323 | |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1324 | const SkRect& getLocalClipBounds() const { |
| 1325 | if (fCachedLocalClipBoundsDirty) { |
| 1326 | if (!this->getClipBounds(&fCachedLocalClipBounds)) { |
| 1327 | fCachedLocalClipBounds.setEmpty(); |
| 1328 | } |
| 1329 | fCachedLocalClipBoundsDirty = false; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1330 | } |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1331 | return fCachedLocalClipBounds; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1332 | } |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1333 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1334 | class AutoValidateClip : ::SkNoncopyable { |
| 1335 | public: |
| 1336 | explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) { |
| 1337 | fCanvas->validateClip(); |
| 1338 | } |
| 1339 | ~AutoValidateClip() { fCanvas->validateClip(); } |
| 1340 | |
| 1341 | private: |
| 1342 | const SkCanvas* fCanvas; |
| 1343 | }; |
| 1344 | |
| 1345 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 520cf8b | 2014-03-20 20:25:14 +0000 | [diff] [blame] | 1346 | // The cull stack rects are in device-space |
| 1347 | SkTDArray<SkIRect> fCullStack; |
| 1348 | void validateCull(const SkIRect&); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1349 | void validateClip() const; |
| 1350 | #else |
| 1351 | void validateClip() const {} |
| 1352 | #endif |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 1353 | |
| 1354 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1355 | }; |
| 1356 | |
| 1357 | /** Stack helper class to automatically call restoreToCount() on the canvas |
| 1358 | when this object goes out of scope. Use this to guarantee that the canvas |
| 1359 | is restored to a known state. |
| 1360 | */ |
| 1361 | class SkAutoCanvasRestore : SkNoncopyable { |
| 1362 | public: |
commit-bot@chromium.org | 2887119 | 2013-10-14 15:28:01 +0000 | [diff] [blame] | 1363 | SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas), fSaveCount(0) { |
| 1364 | if (fCanvas) { |
| 1365 | fSaveCount = canvas->getSaveCount(); |
| 1366 | if (doSave) { |
| 1367 | canvas->save(); |
| 1368 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | ~SkAutoCanvasRestore() { |
reed@google.com | f6c9a5b | 2012-11-20 15:12:21 +0000 | [diff] [blame] | 1372 | if (fCanvas) { |
| 1373 | fCanvas->restoreToCount(fSaveCount); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | /** |
| 1378 | * Perform the restore now, instead of waiting for the destructor. Will |
| 1379 | * only do this once. |
| 1380 | */ |
| 1381 | void restore() { |
| 1382 | if (fCanvas) { |
| 1383 | fCanvas->restoreToCount(fSaveCount); |
| 1384 | fCanvas = NULL; |
| 1385 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | private: |
| 1389 | SkCanvas* fCanvas; |
| 1390 | int fSaveCount; |
| 1391 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 1392 | #define SkAutoCanvasRestore(...) SK_REQUIRE_LOCAL_VAR(SkAutoCanvasRestore) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1393 | |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 1394 | /** Stack helper class to automatically open and close a comment block |
| 1395 | */ |
| 1396 | class SkAutoCommentBlock : SkNoncopyable { |
| 1397 | public: |
| 1398 | SkAutoCommentBlock(SkCanvas* canvas, const char* description) { |
| 1399 | fCanvas = canvas; |
| 1400 | if (NULL != fCanvas) { |
| 1401 | fCanvas->beginCommentGroup(description); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | ~SkAutoCommentBlock() { |
| 1406 | if (NULL != fCanvas) { |
| 1407 | fCanvas->endCommentGroup(); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | private: |
| 1412 | SkCanvas* fCanvas; |
| 1413 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 1414 | #define SkAutoCommentBlock(...) SK_REQUIRE_LOCAL_VAR(SkAutoCommentBlock) |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 1415 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1416 | /** |
| 1417 | * If the caller wants read-only access to the pixels in a canvas, it can just |
| 1418 | * call canvas->peekPixels(), since that is the fastest way to "peek" at the |
| 1419 | * pixels on a raster-backed canvas. |
| 1420 | * |
| 1421 | * If the canvas has pixels, but they are not readily available to the CPU |
| 1422 | * (e.g. gpu-backed), then peekPixels() will fail, but readPixels() will |
| 1423 | * succeed (though be slower, since it will return a copy of the pixels). |
| 1424 | * |
| 1425 | * SkAutoROCanvasPixels encapsulates these two techniques, trying first to call |
| 1426 | * peekPixels() (for performance), but if that fails, calling readPixels() and |
| 1427 | * storing the copy locally. |
| 1428 | * |
| 1429 | * The caller must respect the restrictions associated with peekPixels(), since |
| 1430 | * that may have been called: The returned information is invalidated if... |
| 1431 | * - any API is called on the canvas (or its parent surface if present) |
| 1432 | * - the canvas goes out of scope |
| 1433 | */ |
| 1434 | class SkAutoROCanvasPixels : SkNoncopyable { |
| 1435 | public: |
| 1436 | SkAutoROCanvasPixels(SkCanvas* canvas); |
| 1437 | |
| 1438 | // returns NULL on failure |
| 1439 | const void* addr() const { return fAddr; } |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 1440 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1441 | // undefined if addr() == NULL |
| 1442 | size_t rowBytes() const { return fRowBytes; } |
skia.committer@gmail.com | 02d6f54 | 2014-02-14 03:02:05 +0000 | [diff] [blame] | 1443 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1444 | // undefined if addr() == NULL |
| 1445 | const SkImageInfo& info() const { return fInfo; } |
| 1446 | |
| 1447 | // helper that, if returns true, installs the pixels into the bitmap. Note |
| 1448 | // that the bitmap may reference the address returned by peekPixels(), so |
| 1449 | // the caller must respect the restrictions associated with peekPixels(). |
| 1450 | bool asROBitmap(SkBitmap*) const; |
| 1451 | |
| 1452 | private: |
| 1453 | SkBitmap fBitmap; // used if peekPixels() fails |
| 1454 | const void* fAddr; // NULL on failure |
| 1455 | SkImageInfo fInfo; |
| 1456 | size_t fRowBytes; |
| 1457 | }; |
| 1458 | |
commit-bot@chromium.org | 2a5cd60 | 2014-05-30 20:41:20 +0000 | [diff] [blame] | 1459 | static inline SkCanvas::SaveFlags operator|(const SkCanvas::SaveFlags lhs, |
| 1460 | const SkCanvas::SaveFlags rhs) { |
commit-bot@chromium.org | 92a8916 | 2014-05-30 21:07:05 +0000 | [diff] [blame] | 1461 | return static_cast<SkCanvas::SaveFlags>(static_cast<int>(lhs) | static_cast<int>(rhs)); |
commit-bot@chromium.org | 2a5cd60 | 2014-05-30 20:41:20 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | static inline SkCanvas::SaveFlags& operator|=(SkCanvas::SaveFlags& lhs, |
| 1465 | const SkCanvas::SaveFlags rhs) { |
| 1466 | lhs = lhs | rhs; |
| 1467 | return lhs; |
| 1468 | } |
| 1469 | |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 1470 | class SkCanvasClipVisitor { |
| 1471 | public: |
| 1472 | virtual ~SkCanvasClipVisitor(); |
| 1473 | virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0; |
| 1474 | virtual void clipRRect(const SkRRect&, SkRegion::Op, bool antialias) = 0; |
| 1475 | virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0; |
| 1476 | }; |
commit-bot@chromium.org | 2a5cd60 | 2014-05-30 20:41:20 +0000 | [diff] [blame] | 1477 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1478 | #endif |