epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkCanvas_DEFINED |
| 11 | #define SkCanvas_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | #include "SkBitmap.h" |
| 15 | #include "SkDeque.h" |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 16 | #include "SkClipStack.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkPaint.h" |
| 18 | #include "SkRefCnt.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkPath.h" |
| 20 | #include "SkRegion.h" |
| 21 | #include "SkScalarCompare.h" |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 22 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | |
| 24 | class SkBounder; |
| 25 | class SkDevice; |
| 26 | class SkDraw; |
| 27 | class SkDrawFilter; |
| 28 | class SkPicture; |
| 29 | |
| 30 | /** \class SkCanvas |
| 31 | |
| 32 | A Canvas encapsulates all of the state about drawing into a device (bitmap). |
| 33 | This includes a reference to the device itself, and a stack of matrix/clip |
| 34 | values. For any given draw call (e.g. drawRect), the geometry of the object |
| 35 | being drawn is transformed by the concatenation of all the matrices in the |
| 36 | stack. The transformed geometry is clipped by the intersection of all of |
| 37 | the clips in the stack. |
| 38 | |
| 39 | While the Canvas holds the state of the drawing device, the state (style) |
| 40 | of the object being drawn is held by the Paint, which is provided as a |
| 41 | parameter to each of the draw() methods. The Paint holds attributes such as |
| 42 | color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns), |
| 43 | etc. |
| 44 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 45 | class SK_API SkCanvas : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | public: |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 47 | SkCanvas(); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 48 | |
reed@google.com | 6dc7455 | 2011-07-21 18:00:46 +0000 | [diff] [blame] | 49 | /** Construct a canvas with the specified device to draw into. |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 50 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 51 | @param device Specifies a device for the canvas to draw into. |
| 52 | */ |
| 53 | explicit SkCanvas(SkDevice* device); |
| 54 | |
| 55 | /** Deprecated - Construct a canvas with the specified bitmap to draw into. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | @param bitmap Specifies a bitmap for the canvas to draw into. Its |
| 57 | structure are copied to the canvas. |
| 58 | */ |
| 59 | explicit SkCanvas(const SkBitmap& bitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | virtual ~SkCanvas(); |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////// |
| 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | /** Return the canvas' device object, which may be null. The device holds |
| 65 | the bitmap of the pixels that the canvas draws into. The reference count |
| 66 | of the returned device is not changed by this call. |
| 67 | */ |
| 68 | SkDevice* getDevice() const; |
| 69 | |
| 70 | /** Specify a device for this canvas to draw into. If it is not null, its |
| 71 | reference count is incremented. If the canvas was already holding a |
| 72 | device, its reference count is decremented. The new device is returned. |
| 73 | */ |
| 74 | SkDevice* setDevice(SkDevice* device); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 75 | |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 76 | /** |
| 77 | * saveLayer() can create another device (which is later drawn onto |
| 78 | * the previous device). getTopDevice() returns the top-most device current |
| 79 | * installed. Note that this can change on other calls like save/restore, |
| 80 | * so do not access this device after subsequent canvas calls. |
| 81 | * The reference count of the device is not changed. |
| 82 | */ |
| 83 | SkDevice* getTopDevice() const; |
| 84 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Create a new raster device and make it current. This also returns |
| 87 | * the new device. |
| 88 | */ |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 89 | SkDevice* setBitmapDevice(const SkBitmap& bitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 91 | /** |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 92 | * Shortcut for getDevice()->createCompatibleDevice(...). |
| 93 | * If getDevice() == NULL, this method does nothing, and returns NULL. |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 94 | */ |
| 95 | SkDevice* createCompatibleDevice(SkBitmap::Config config, |
| 96 | int width, int height, |
| 97 | bool isOpaque); |
| 98 | |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 99 | /////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | /** |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 102 | * Copy the pixels from the device into bitmap. Returns true on success. |
| 103 | * If false is returned, then the bitmap parameter is left unchanged. |
reed@google.com | 3dd42b3 | 2011-02-07 22:44:43 +0000 | [diff] [blame] | 104 | * The bitmap parameter is treated as output-only, and will be completely |
| 105 | * overwritten (if the method returns true). |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 106 | */ |
| 107 | bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); |
| 108 | bool readPixels(SkBitmap* bitmap); |
| 109 | |
| 110 | /** |
| 111 | * Similar to draw sprite, this method will copy the pixels in bitmap onto |
| 112 | * the device, with the top/left corner specified by (x, y). The pixel |
| 113 | * values in the device are completely replaced: there is no blending. |
epoger@google.com | 4f1151a | 2011-07-25 15:47:33 +0000 | [diff] [blame] | 114 | * |
| 115 | * Note: If you are recording drawing commands on this canvas to |
| 116 | * SkPicture, writePixels() is ignored! |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 117 | */ |
| 118 | void writePixels(const SkBitmap& bitmap, int x, int y); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | /////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 121 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | enum SaveFlags { |
| 123 | /** save the matrix state, restoring it on restore() */ |
| 124 | kMatrix_SaveFlag = 0x01, |
| 125 | /** save the clip state, restoring it on restore() */ |
| 126 | kClip_SaveFlag = 0x02, |
| 127 | /** the layer needs to support per-pixel alpha */ |
| 128 | kHasAlphaLayer_SaveFlag = 0x04, |
| 129 | /** the layer needs to support 8-bits per color component */ |
| 130 | kFullColorLayer_SaveFlag = 0x08, |
| 131 | /** the layer should clip against the bounds argument */ |
| 132 | kClipToLayer_SaveFlag = 0x10, |
| 133 | |
| 134 | // helper masks for common choices |
| 135 | kMatrixClip_SaveFlag = 0x03, |
| 136 | kARGB_NoClipLayer_SaveFlag = 0x0F, |
| 137 | kARGB_ClipLayer_SaveFlag = 0x1F |
| 138 | }; |
| 139 | |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 140 | /** 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] | 141 | copy onto a private stack. Subsequent calls to translate, scale, |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 142 | rotate, skew, concat or clipRect, clipPath, and setDrawFilter all |
| 143 | operate on this copy. |
| 144 | When the balancing call to restore() is made, the previous matrix, clip, |
| 145 | and drawFilter are restored. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | @return The value to pass to restoreToCount() to balance this save() |
| 147 | */ |
| 148 | virtual int save(SaveFlags flags = kMatrixClip_SaveFlag); |
| 149 | |
| 150 | /** This behaves the same as save(), but in addition it allocates an |
| 151 | offscreen bitmap. All drawing calls are directed there, and only when |
| 152 | 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] | 153 | the canvas (or the previous layer). |
reed@android.com | ad164b2 | 2010-07-02 17:20:51 +0000 | [diff] [blame] | 154 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 155 | limit the size of the offscreen, and thus drawing may be |
| 156 | clipped to it, though that clipping is not guaranteed to |
| 157 | happen. If exact clipping is desired, use clipRect(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | @param paint (may be null) This is copied, and is applied to the |
| 159 | offscreen when restore() is called |
| 160 | @param flags LayerFlags |
| 161 | @return The value to pass to restoreToCount() to balance this save() |
| 162 | */ |
| 163 | virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 164 | SaveFlags flags = kARGB_ClipLayer_SaveFlag); |
| 165 | |
| 166 | /** This behaves the same as save(), but in addition it allocates an |
| 167 | offscreen bitmap. All drawing calls are directed there, and only when |
| 168 | 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] | 169 | the canvas (or the previous layer). |
reed@android.com | 4040861 | 2010-07-02 17:24:23 +0000 | [diff] [blame] | 170 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 171 | limit the size of the offscreen, and thus drawing may be |
| 172 | clipped to it, though that clipping is not guaranteed to |
| 173 | happen. If exact clipping is desired, use clipRect(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | @param alpha This is applied to the offscreen when restore() is called. |
| 175 | @param flags LayerFlags |
| 176 | @return The value to pass to restoreToCount() to balance this save() |
| 177 | */ |
| 178 | int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, |
| 179 | SaveFlags flags = kARGB_ClipLayer_SaveFlag); |
| 180 | |
| 181 | /** 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] | 182 | modifications to the matrix/clip/drawFilter state since the last save |
| 183 | call. |
| 184 | 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] | 185 | */ |
| 186 | virtual void restore(); |
| 187 | |
| 188 | /** Returns the number of matrix/clip states on the SkCanvas' private stack. |
| 189 | This will equal # save() calls - # restore() calls. |
| 190 | */ |
| 191 | int getSaveCount() const; |
| 192 | |
| 193 | /** Efficient way to pop any calls to save() that happened after the save |
| 194 | count reached saveCount. It is an error for saveCount to be less than |
| 195 | getSaveCount() |
| 196 | @param saveCount The number of save() levels to restore from |
| 197 | */ |
| 198 | void restoreToCount(int saveCount); |
| 199 | |
| 200 | /** Preconcat the current matrix with the specified translation |
| 201 | @param dx The distance to translate in X |
| 202 | @param dy The distance to translate in Y |
| 203 | returns true if the operation succeeded (e.g. did not overflow) |
| 204 | */ |
| 205 | virtual bool translate(SkScalar dx, SkScalar dy); |
| 206 | |
| 207 | /** Preconcat the current matrix with the specified scale. |
| 208 | @param sx The amount to scale in X |
| 209 | @param sy The amount to scale in Y |
| 210 | returns true if the operation succeeded (e.g. did not overflow) |
| 211 | */ |
| 212 | virtual bool scale(SkScalar sx, SkScalar sy); |
| 213 | |
| 214 | /** Preconcat the current matrix with the specified rotation. |
| 215 | @param degrees The amount to rotate, in degrees |
| 216 | returns true if the operation succeeded (e.g. did not overflow) |
| 217 | */ |
| 218 | virtual bool rotate(SkScalar degrees); |
| 219 | |
| 220 | /** Preconcat the current matrix with the specified skew. |
| 221 | @param sx The amount to skew in X |
| 222 | @param sy The amount to skew in Y |
| 223 | returns true if the operation succeeded (e.g. did not overflow) |
| 224 | */ |
| 225 | virtual bool skew(SkScalar sx, SkScalar sy); |
| 226 | |
| 227 | /** Preconcat the current matrix with the specified matrix. |
| 228 | @param matrix The matrix to preconcatenate with the current matrix |
| 229 | @return true if the operation succeeded (e.g. did not overflow) |
| 230 | */ |
| 231 | virtual bool concat(const SkMatrix& matrix); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 232 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | /** Replace the current matrix with a copy of the specified matrix. |
| 234 | @param matrix The matrix that will be copied into the current matrix. |
| 235 | */ |
| 236 | virtual void setMatrix(const SkMatrix& matrix); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | /** Helper for setMatrix(identity). Sets the current matrix to identity. |
| 239 | */ |
| 240 | void resetMatrix(); |
| 241 | |
| 242 | /** Modify the current clip with the specified rectangle. |
| 243 | @param rect The rect to intersect with the current clip |
| 244 | @param op The region op to apply to the current clip |
| 245 | @return true if the canvas' clip is non-empty |
| 246 | */ |
| 247 | virtual bool clipRect(const SkRect& rect, |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame^] | 248 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 249 | bool doAntiAlias = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | |
| 251 | /** Modify the current clip with the specified path. |
| 252 | @param path The path to apply to the current clip |
| 253 | @param op The region op to apply to the current clip |
| 254 | @return true if the canvas' new clip is non-empty |
| 255 | */ |
| 256 | virtual bool clipPath(const SkPath& path, |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame^] | 257 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 258 | bool doAntiAlias = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | |
| 260 | /** Modify the current clip with the specified region. Note that unlike |
| 261 | clipRect() and clipPath() which transform their arguments by the current |
| 262 | matrix, clipRegion() assumes its argument is already in device |
| 263 | coordinates, and so no transformation is performed. |
| 264 | @param deviceRgn The region to apply to the current clip |
| 265 | @param op The region op to apply to the current clip |
| 266 | @return true if the canvas' new clip is non-empty |
| 267 | */ |
| 268 | virtual bool clipRegion(const SkRegion& deviceRgn, |
| 269 | SkRegion::Op op = SkRegion::kIntersect_Op); |
| 270 | |
| 271 | /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the |
| 272 | specified region. This does not intersect or in any other way account |
| 273 | for the existing clip region. |
| 274 | @param deviceRgn The region to copy into the current clip. |
| 275 | @return true if the new clip region is non-empty |
| 276 | */ |
| 277 | bool setClipRegion(const SkRegion& deviceRgn) { |
| 278 | return this->clipRegion(deviceRgn, SkRegion::kReplace_Op); |
| 279 | } |
| 280 | |
| 281 | /** Enum describing how to treat edges when performing quick-reject tests |
| 282 | of a geometry against the current clip. Treating them as antialiased |
| 283 | (kAA_EdgeType) will take into account the extra pixels that may be drawn |
| 284 | if the edge does not lie exactly on a device pixel boundary (after being |
| 285 | transformed by the current matrix). |
| 286 | */ |
| 287 | enum EdgeType { |
| 288 | /** Treat the edges as B&W (not antialiased) for the purposes of testing |
| 289 | against the current clip |
| 290 | */ |
| 291 | kBW_EdgeType, |
| 292 | /** Treat the edges as antialiased for the purposes of testing |
| 293 | against the current clip |
| 294 | */ |
| 295 | kAA_EdgeType |
| 296 | }; |
| 297 | |
| 298 | /** Return true if the specified rectangle, after being transformed by the |
| 299 | current matrix, would lie completely outside of the current clip. Call |
| 300 | this to check if an area you intend to draw into is clipped out (and |
| 301 | therefore you can skip making the draw calls). |
| 302 | @param rect the rect to compare with the current clip |
| 303 | @param et specifies how to treat the edges (see EdgeType) |
| 304 | @return true if the rect (transformed by the canvas' matrix) does not |
| 305 | intersect with the canvas' clip |
| 306 | */ |
| 307 | bool quickReject(const SkRect& rect, EdgeType et) const; |
| 308 | |
| 309 | /** Return true if the specified path, after being transformed by the |
| 310 | current matrix, would lie completely outside of the current clip. Call |
| 311 | this to check if an area you intend to draw into is clipped out (and |
| 312 | therefore you can skip making the draw calls). Note, for speed it may |
| 313 | return false even if the path itself might not intersect the clip |
| 314 | (i.e. the bounds of the path intersects, but the path does not). |
| 315 | @param path The path to compare with the current clip |
| 316 | @param et specifies how to treat the edges (see EdgeType) |
| 317 | @return true if the path (transformed by the canvas' matrix) does not |
| 318 | intersect with the canvas' clip |
| 319 | */ |
| 320 | bool quickReject(const SkPath& path, EdgeType et) const; |
| 321 | |
| 322 | /** Return true if the horizontal band specified by top and bottom is |
| 323 | completely clipped out. This is a conservative calculation, meaning |
| 324 | that it is possible that if the method returns false, the band may still |
| 325 | in fact be clipped out, but the converse is not true. If this method |
| 326 | returns true, then the band is guaranteed to be clipped out. |
| 327 | @param top The top of the horizontal band to compare with the clip |
| 328 | @param bottom The bottom of the horizontal and to compare with the clip |
| 329 | @return true if the horizontal band is completely clipped out (i.e. does |
| 330 | not intersect the current clip) |
| 331 | */ |
| 332 | bool quickRejectY(SkScalar top, SkScalar bottom, EdgeType et) const; |
| 333 | |
| 334 | /** Return the bounds of the current clip (in local coordinates) in the |
| 335 | bounds parameter, and return true if it is non-empty. This can be useful |
| 336 | in a way similar to quickReject, in that it tells you that drawing |
| 337 | outside of these bounds will be clipped out. |
| 338 | */ |
| 339 | bool getClipBounds(SkRect* bounds, EdgeType et = kAA_EdgeType) const; |
| 340 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 341 | /** Return the bounds of the current clip, in device coordinates; returns |
| 342 | true if non-empty. Maybe faster than getting the clip explicitly and |
| 343 | then taking its bounds. |
| 344 | */ |
| 345 | bool getClipDeviceBounds(SkIRect* bounds) const; |
| 346 | |
| 347 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | /** 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] | 349 | specified ARGB color, using the specified mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | @param a the alpha component (0..255) of the color to fill the canvas |
| 351 | @param r the red component (0..255) of the color to fill the canvas |
| 352 | @param g the green component (0..255) of the color to fill the canvas |
| 353 | @param b the blue component (0..255) of the color to fill the canvas |
| 354 | @param mode the mode to apply the color in (defaults to SrcOver) |
| 355 | */ |
| 356 | void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 357 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | |
| 359 | /** 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] | 360 | specified color and mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 361 | @param color the color to draw with |
| 362 | @param mode the mode to apply the color in (defaults to SrcOver) |
| 363 | */ |
| 364 | void drawColor(SkColor color, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 365 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 367 | /** |
| 368 | * This erases the entire drawing surface to the specified color, |
| 369 | * irrespective of the clip. It does not blend with the previous pixels, |
| 370 | * but always overwrites them. |
| 371 | * |
| 372 | * It is roughly equivalent to the following: |
| 373 | * canvas.save(); |
| 374 | * canvas.clipRect(hugeRect, kReplace_Op); |
| 375 | * paint.setColor(color); |
| 376 | * paint.setXfermodeMode(kSrc_Mode); |
| 377 | * canvas.drawPaint(paint); |
| 378 | * canvas.restore(); |
| 379 | * though it is almost always much more efficient. |
| 380 | */ |
| 381 | virtual void clear(SkColor); |
| 382 | |
| 383 | /** |
| 384 | * Fill the entire canvas' bitmap (restricted to the current clip) with the |
| 385 | * specified paint. |
| 386 | * @param paint The paint used to fill the canvas |
| 387 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | virtual void drawPaint(const SkPaint& paint); |
| 389 | |
| 390 | enum PointMode { |
| 391 | /** drawPoints draws each point separately */ |
| 392 | kPoints_PointMode, |
| 393 | /** drawPoints draws each pair of points as a line segment */ |
| 394 | kLines_PointMode, |
| 395 | /** drawPoints draws the array of points as a polygon */ |
| 396 | kPolygon_PointMode |
| 397 | }; |
| 398 | |
| 399 | /** Draw a series of points, interpreted based on the PointMode mode. For |
| 400 | all modes, the count parameter is interpreted as the total number of |
| 401 | points. For kLine mode, count/2 line segments are drawn. |
| 402 | For kPoint mode, each point is drawn centered at its coordinate, and its |
| 403 | size is specified by the paint's stroke-width. It draws as a square, |
| 404 | unless the paint's cap-type is round, in which the points are drawn as |
| 405 | circles. |
| 406 | For kLine mode, each pair of points is drawn as a line segment, |
| 407 | respecting the paint's settings for cap/join/width. |
| 408 | For kPolygon mode, the entire array is drawn as a series of connected |
| 409 | line segments. |
| 410 | Note that, while similar, kLine and kPolygon modes draw slightly |
| 411 | differently than the equivalent path built with a series of moveto, |
| 412 | lineto calls, in that the path will draw all of its contours at once, |
| 413 | with no interactions if contours intersect each other (think XOR |
| 414 | xfermode). drawPoints always draws each element one at a time. |
| 415 | @param mode PointMode specifying how to draw the array of points. |
| 416 | @param count The number of points in the array |
| 417 | @param pts Array of points to draw |
| 418 | @param paint The paint used to draw the points |
| 419 | */ |
| 420 | virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 421 | const SkPaint& paint); |
| 422 | |
| 423 | /** Helper method for drawing a single point. See drawPoints() for a more |
| 424 | details. |
| 425 | */ |
| 426 | void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 427 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | /** Draws a single pixel in the specified color. |
| 429 | @param x The X coordinate of which pixel to draw |
| 430 | @param y The Y coordiante of which pixel to draw |
| 431 | @param color The color to draw |
| 432 | */ |
| 433 | void drawPoint(SkScalar x, SkScalar y, SkColor color); |
| 434 | |
| 435 | /** Draw a line segment with the specified start and stop x,y coordinates, |
| 436 | using the specified paint. NOTE: since a line is always "framed", the |
| 437 | paint's Style is ignored. |
| 438 | @param x0 The x-coordinate of the start point of the line |
| 439 | @param y0 The y-coordinate of the start point of the line |
| 440 | @param x1 The x-coordinate of the end point of the line |
| 441 | @param y1 The y-coordinate of the end point of the line |
| 442 | @param paint The paint used to draw the line |
| 443 | */ |
| 444 | void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 445 | const SkPaint& paint); |
| 446 | |
| 447 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 448 | will be filled or stroked based on the Style in the paint. |
| 449 | @param rect The rect to be drawn |
| 450 | @param paint The paint used to draw the rect |
| 451 | */ |
| 452 | virtual void drawRect(const SkRect& rect, const SkPaint& paint); |
| 453 | |
| 454 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 455 | will be filled or framed based on the Style in the paint. |
| 456 | @param rect The rect to be drawn |
| 457 | @param paint The paint used to draw the rect |
| 458 | */ |
| 459 | void drawIRect(const SkIRect& rect, const SkPaint& paint) |
| 460 | { |
| 461 | SkRect r; |
| 462 | r.set(rect); // promotes the ints to scalars |
| 463 | this->drawRect(r, paint); |
| 464 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 465 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 467 | will be filled or framed based on the Style in the paint. |
| 468 | @param left The left side of the rectangle to be drawn |
| 469 | @param top The top side of the rectangle to be drawn |
| 470 | @param right The right side of the rectangle to be drawn |
| 471 | @param bottom The bottom side of the rectangle to be drawn |
| 472 | @param paint The paint used to draw the rect |
| 473 | */ |
| 474 | void drawRectCoords(SkScalar left, SkScalar top, SkScalar right, |
| 475 | SkScalar bottom, const SkPaint& paint); |
| 476 | |
| 477 | /** Draw the specified oval using the specified paint. The oval will be |
| 478 | filled or framed based on the Style in the paint. |
| 479 | @param oval The rectangle bounds of the oval to be drawn |
| 480 | @param paint The paint used to draw the oval |
| 481 | */ |
| 482 | void drawOval(const SkRect& oval, const SkPaint&); |
| 483 | |
| 484 | /** Draw the specified circle using the specified paint. If radius is <= 0, |
| 485 | then nothing will be drawn. The circle will be filled |
| 486 | or framed based on the Style in the paint. |
| 487 | @param cx The x-coordinate of the center of the cirle to be drawn |
| 488 | @param cy The y-coordinate of the center of the cirle to be drawn |
| 489 | @param radius The radius of the cirle to be drawn |
| 490 | @param paint The paint used to draw the circle |
| 491 | */ |
| 492 | void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 493 | const SkPaint& paint); |
| 494 | |
| 495 | /** Draw the specified arc, which will be scaled to fit inside the |
| 496 | specified oval. If the sweep angle is >= 360, then the oval is drawn |
| 497 | completely. Note that this differs slightly from SkPath::arcTo, which |
| 498 | treats the sweep angle mod 360. |
| 499 | @param oval The bounds of oval used to define the shape of the arc |
| 500 | @param startAngle Starting angle (in degrees) where the arc begins |
| 501 | @param sweepAngle Sweep angle (in degrees) measured clockwise |
| 502 | @param useCenter true means include the center of the oval. For filling |
| 503 | this will draw a wedge. False means just use the arc. |
| 504 | @param paint The paint used to draw the arc |
| 505 | */ |
| 506 | void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 507 | bool useCenter, const SkPaint& paint); |
| 508 | |
| 509 | /** Draw the specified round-rect using the specified paint. The round-rect |
| 510 | will be filled or framed based on the Style in the paint. |
| 511 | @param rect The rectangular bounds of the roundRect to be drawn |
| 512 | @param rx The x-radius of the oval used to round the corners |
| 513 | @param ry The y-radius of the oval used to round the corners |
| 514 | @param paint The paint used to draw the roundRect |
| 515 | */ |
| 516 | void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, |
| 517 | const SkPaint& paint); |
| 518 | |
| 519 | /** Draw the specified path using the specified paint. The path will be |
| 520 | filled or framed based on the Style in the paint. |
| 521 | @param path The path to be drawn |
| 522 | @param paint The paint used to draw the path |
| 523 | */ |
| 524 | virtual void drawPath(const SkPath& path, const SkPaint& paint); |
| 525 | |
| 526 | /** Draw the specified bitmap, with its top/left corner at (x,y), using the |
| 527 | specified paint, transformed by the current matrix. Note: if the paint |
| 528 | contains a maskfilter that generates a mask which extends beyond the |
| 529 | bitmap's original width/height, then the bitmap will be drawn as if it |
| 530 | were in a Shader with CLAMP mode. Thus the color outside of the original |
| 531 | width/height will be the edge color replicated. |
| 532 | @param bitmap The bitmap to be drawn |
| 533 | @param left The position of the left side of the bitmap being drawn |
| 534 | @param top The position of the top side of the bitmap being drawn |
| 535 | @param paint The paint used to draw the bitmap, or NULL |
| 536 | */ |
| 537 | virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 538 | const SkPaint* paint = NULL); |
| 539 | |
| 540 | /** Draw the specified bitmap, with the specified matrix applied (before the |
| 541 | canvas' matrix is applied). |
| 542 | @param bitmap The bitmap to be drawn |
| 543 | @param src Optional: specify the subset of the bitmap to be drawn |
| 544 | @param dst The destination rectangle where the scaled/translated |
| 545 | image will be drawn |
| 546 | @param paint The paint used to draw the bitmap, or NULL |
| 547 | */ |
| 548 | virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, |
| 549 | const SkRect& dst, const SkPaint* paint = NULL); |
| 550 | |
| 551 | virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
| 552 | const SkPaint* paint = NULL); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 553 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 554 | /** |
| 555 | * Draw the bitmap stretched differentially to fit into dst. |
| 556 | * center is a rect within the bitmap, and logically divides the bitmap |
| 557 | * into 9 sections (3x3). For example, if the middle pixel of a [5x5] |
| 558 | * bitmap is the "center", then the center-rect should be [2, 2, 3, 3]. |
| 559 | * |
| 560 | * If the dst is >= the bitmap size, then... |
| 561 | * - The 4 corners are not stretch at all. |
| 562 | * - The sides are stretch in only one axis. |
| 563 | * - The center is stretch in both axes. |
| 564 | * Else, for each axis where dst < bitmap, |
| 565 | * - The corners shrink proportionally |
| 566 | * - The sides (along the shrink axis) and center are not drawn |
| 567 | */ |
| 568 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 569 | const SkRect& dst, const SkPaint* paint = NULL); |
| 570 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 571 | /** Draw the specified bitmap, with its top/left corner at (x,y), |
| 572 | NOT transformed by the current matrix. Note: if the paint |
| 573 | contains a maskfilter that generates a mask which extends beyond the |
| 574 | bitmap's original width/height, then the bitmap will be drawn as if it |
| 575 | were in a Shader with CLAMP mode. Thus the color outside of the original |
| 576 | width/height will be the edge color replicated. |
| 577 | @param bitmap The bitmap to be drawn |
| 578 | @param left The position of the left side of the bitmap being drawn |
| 579 | @param top The position of the top side of the bitmap being drawn |
| 580 | @param paint The paint used to draw the bitmap, or NULL |
| 581 | */ |
| 582 | virtual void drawSprite(const SkBitmap& bitmap, int left, int top, |
| 583 | const SkPaint* paint = NULL); |
| 584 | |
| 585 | /** Draw the text, with origin at (x,y), using the specified paint. |
| 586 | The origin is interpreted based on the Align setting in the paint. |
| 587 | @param text The text to be drawn |
| 588 | @param byteLength The number of bytes to read from the text parameter |
| 589 | @param x The x-coordinate of the origin of the text being drawn |
| 590 | @param y The y-coordinate of the origin of the text being drawn |
| 591 | @param paint The paint used for the text (e.g. color, size, style) |
| 592 | */ |
| 593 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
| 594 | SkScalar y, const SkPaint& paint); |
| 595 | |
| 596 | /** 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] | 597 | 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] | 598 | @param text The text to be drawn |
| 599 | @param byteLength The number of bytes to read from the text parameter |
| 600 | @param pos Array of positions, used to position each character |
| 601 | @param paint The paint used for the text (e.g. color, size, style) |
| 602 | */ |
| 603 | virtual void drawPosText(const void* text, size_t byteLength, |
| 604 | const SkPoint pos[], const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 605 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | /** Draw the text, with each character/glyph origin specified by the x |
| 607 | 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] | 608 | The origin is interpreted by the Align setting in the paint. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 609 | @param text The text to be drawn |
| 610 | @param byteLength The number of bytes to read from the text parameter |
| 611 | @param xpos Array of x-positions, used to position each character |
| 612 | @param constY The shared Y coordinate for all of the positions |
| 613 | @param paint The paint used for the text (e.g. color, size, style) |
| 614 | */ |
| 615 | virtual void drawPosTextH(const void* text, size_t byteLength, |
| 616 | const SkScalar xpos[], SkScalar constY, |
| 617 | const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 618 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 619 | /** Draw the text, with origin at (x,y), using the specified paint, along |
| 620 | the specified path. The paint's Align setting determins where along the |
| 621 | path to start the text. |
| 622 | @param text The text to be drawn |
| 623 | @param byteLength The number of bytes to read from the text parameter |
| 624 | @param path The path the text should follow for its baseline |
| 625 | @param hOffset The distance along the path to add to the text's |
| 626 | starting position |
| 627 | @param vOffset The distance above(-) or below(+) the path to |
| 628 | position the text |
| 629 | @param paint The paint used for the text |
| 630 | */ |
| 631 | void drawTextOnPathHV(const void* text, size_t byteLength, |
| 632 | const SkPath& path, SkScalar hOffset, |
| 633 | SkScalar vOffset, const SkPaint& paint); |
| 634 | |
| 635 | /** Draw the text, with origin at (x,y), using the specified paint, along |
| 636 | the specified path. The paint's Align setting determins where along the |
| 637 | path to start the text. |
| 638 | @param text The text to be drawn |
| 639 | @param byteLength The number of bytes to read from the text parameter |
| 640 | @param path The path the text should follow for its baseline |
| 641 | @param matrix (may be null) Applied to the text before it is |
| 642 | mapped onto the path |
| 643 | @param paint The paint used for the text |
| 644 | */ |
| 645 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 646 | const SkPath& path, const SkMatrix* matrix, |
| 647 | const SkPaint& paint); |
| 648 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 649 | #ifdef ANDROID |
| 650 | /** Draw the text on path, with each character/glyph origin specified by the pos[] |
| 651 | array. The origin is interpreted by the Align setting in the paint. |
| 652 | @param text The text to be drawn |
| 653 | @param byteLength The number of bytes to read from the text parameter |
| 654 | @param pos Array of positions, used to position each character |
| 655 | @param paint The paint used for the text (e.g. color, size, style) |
| 656 | @param path The path to draw on |
| 657 | @param matrix The canvas matrix |
| 658 | */ |
| 659 | void drawPosTextOnPath(const void* text, size_t byteLength, |
| 660 | const SkPoint pos[], const SkPaint& paint, |
| 661 | const SkPath& path, const SkMatrix* matrix); |
| 662 | #endif |
| 663 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 664 | /** Draw the picture into this canvas. This method effective brackets the |
| 665 | playback of the picture's draw calls with save/restore, so the state |
| 666 | of this canvas will be unchanged after this call. This contrasts with |
| 667 | the more immediate method SkPicture::draw(), which does not bracket |
| 668 | the canvas with save/restore, thus the canvas may be left in a changed |
| 669 | state after the call. |
| 670 | @param picture The recorded drawing commands to playback into this |
| 671 | canvas. |
| 672 | */ |
| 673 | virtual void drawPicture(SkPicture& picture); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 674 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 675 | enum VertexMode { |
| 676 | kTriangles_VertexMode, |
| 677 | kTriangleStrip_VertexMode, |
| 678 | kTriangleFan_VertexMode |
| 679 | }; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 680 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 681 | /** Draw the array of vertices, interpreted as triangles (based on mode). |
| 682 | @param vmode How to interpret the array of vertices |
| 683 | @param vertexCount The number of points in the vertices array (and |
| 684 | corresponding texs and colors arrays if non-null) |
| 685 | @param vertices Array of vertices for the mesh |
| 686 | @param texs May be null. If not null, specifies the coordinate |
| 687 | in texture space for each vertex. |
| 688 | @param colors May be null. If not null, specifies a color for each |
| 689 | vertex, to be interpolated across the triangle. |
| 690 | @param xmode Used if both texs and colors are present. In this |
| 691 | case the colors are combined with the texture using mode, |
| 692 | before being drawn using the paint. If mode is null, then |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 693 | kMultiply_Mode is used. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 694 | @param indices If not null, array of indices to reference into the |
| 695 | vertex (texs, colors) array. |
| 696 | @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] | 697 | @param paint Specifies the shader/texture if present. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | */ |
| 699 | virtual void drawVertices(VertexMode vmode, int vertexCount, |
| 700 | const SkPoint vertices[], const SkPoint texs[], |
| 701 | const SkColor colors[], SkXfermode* xmode, |
| 702 | const uint16_t indices[], int indexCount, |
| 703 | const SkPaint& paint); |
| 704 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 705 | /** Send a blob of data to the canvas. |
| 706 | For canvases that draw, this call is effectively a no-op, as the data |
| 707 | is not parsed, but just ignored. However, this call exists for |
| 708 | subclasses like SkPicture's recording canvas, that can store the data |
| 709 | and then play it back later (via another call to drawData). |
| 710 | */ |
| 711 | virtual void drawData(const void* data, size_t length); |
| 712 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 713 | ////////////////////////////////////////////////////////////////////////// |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 714 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 715 | /** Get the current bounder object. |
| 716 | The bounder's reference count is unchaged. |
| 717 | @return the canva's bounder (or NULL). |
| 718 | */ |
| 719 | SkBounder* getBounder() const { return fBounder; } |
| 720 | |
| 721 | /** Set a new bounder (or NULL). |
| 722 | Pass NULL to clear any previous bounder. |
| 723 | As a convenience, the parameter passed is also returned. |
| 724 | If a previous bounder exists, its reference count is decremented. |
| 725 | If bounder is not NULL, its reference count is incremented. |
| 726 | @param bounder the new bounder (or NULL) to be installed in the canvas |
| 727 | @return the set bounder object |
| 728 | */ |
| 729 | virtual SkBounder* setBounder(SkBounder* bounder); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 730 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | /** 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] | 732 | 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] | 733 | @return the canvas' filter (or NULL). |
| 734 | */ |
| 735 | SkDrawFilter* getDrawFilter() const; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 736 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 737 | /** Set the new filter (or NULL). Pass NULL to clear any existing filter. |
| 738 | As a convenience, the parameter is returned. If an existing filter |
| 739 | 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] | 740 | refcnt is incremented. The filter is saved/restored, just like the |
| 741 | matrix and clip. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 742 | @param filter the new filter (or NULL) |
| 743 | @return the new filter |
| 744 | */ |
| 745 | virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter); |
| 746 | |
| 747 | ////////////////////////////////////////////////////////////////////////// |
| 748 | |
| 749 | /** Return the current matrix on the canvas. |
| 750 | This does not account for the translate in any of the devices. |
| 751 | @return The current matrix on the canvas. |
| 752 | */ |
| 753 | const SkMatrix& getTotalMatrix() const; |
| 754 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 755 | enum ClipType { |
| 756 | kEmpty_ClipType = 0, |
| 757 | kRect_ClipType, |
| 758 | kComplex_ClipType |
| 759 | }; |
| 760 | |
| 761 | /** Returns a description of the total clip; may be cheaper than |
| 762 | getting the clip and querying it directly. |
| 763 | */ |
| 764 | ClipType getClipType() const; |
| 765 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 766 | /** Return the current device clip (concatenation of all clip calls). |
| 767 | This does not account for the translate in any of the devices. |
| 768 | @return the current device clip (concatenation of all clip calls). |
| 769 | */ |
| 770 | const SkRegion& getTotalClip() const; |
| 771 | |
reed@google.com | 7d7ca79 | 2011-02-23 22:39:18 +0000 | [diff] [blame] | 772 | /** |
reed@google.com | 5e2457e | 2011-10-10 21:24:37 +0000 | [diff] [blame] | 773 | * Return true if the current clip is non-empty. |
| 774 | * |
| 775 | * If bounds is not NULL, set it to the bounds of the current clip |
| 776 | * in global coordinates. |
| 777 | */ |
| 778 | bool getTotalClipBounds(SkIRect* bounds) const; |
| 779 | |
| 780 | /** |
reed@google.com | 7d7ca79 | 2011-02-23 22:39:18 +0000 | [diff] [blame] | 781 | * Return the current clipstack. This mirrors the result in getTotalClip() |
| 782 | * but is represented as a stack of geometric clips + region-ops. |
| 783 | */ |
| 784 | const SkClipStack& getTotalClipStack() const; |
| 785 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 786 | void setExternalMatrix(const SkMatrix* = NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 787 | |
| 788 | /////////////////////////////////////////////////////////////////////////// |
| 789 | |
| 790 | /** After calling saveLayer(), there can be any number of devices that make |
| 791 | up the top-most drawing area. LayerIter can be used to iterate through |
| 792 | those devices. Note that the iterator is only valid until the next API |
| 793 | call made on the canvas. Ownership of all pointers in the iterator stays |
| 794 | with the canvas, so none of them should be modified or deleted. |
| 795 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 796 | class SK_API LayerIter /*: SkNoncopyable*/ { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 797 | public: |
| 798 | /** Initialize iterator with canvas, and set values for 1st device */ |
| 799 | LayerIter(SkCanvas*, bool skipEmptyClips); |
| 800 | ~LayerIter(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 801 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 802 | /** Return true if the iterator is done */ |
| 803 | bool done() const { return fDone; } |
| 804 | /** Cycle to the next device */ |
| 805 | void next(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 806 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 807 | // These reflect the current device in the iterator |
| 808 | |
| 809 | SkDevice* device() const; |
| 810 | const SkMatrix& matrix() const; |
| 811 | const SkRegion& clip() const; |
| 812 | const SkPaint& paint() const; |
| 813 | int x() const; |
| 814 | int y() const; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 815 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | private: |
| 817 | // used to embed the SkDrawIter object directly in our instance, w/o |
| 818 | // having to expose that class def to the public. There is an assert |
| 819 | // in our constructor to ensure that fStorage is large enough |
| 820 | // (though needs to be a compile-time-assert!). We use intptr_t to work |
| 821 | // 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] | 822 | intptr_t fStorage[32]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 823 | class SkDrawIter* fImpl; // this points at fStorage |
| 824 | SkPaint fDefaultPaint; |
| 825 | bool fDone; |
| 826 | }; |
| 827 | |
| 828 | protected: |
| 829 | // all of the drawBitmap variants call this guy |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 830 | virtual void commonDrawBitmap(const SkBitmap&, const SkIRect*, |
| 831 | const SkMatrix&, const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 832 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 833 | private: |
| 834 | class MCRec; |
| 835 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 836 | SkClipStack fClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 837 | SkDeque fMCStack; |
| 838 | // points to top of stack |
| 839 | MCRec* fMCRec; |
| 840 | // the first N recs that can fit here mean we won't call malloc |
| 841 | uint32_t fMCRecStorage[32]; |
| 842 | |
| 843 | SkBounder* fBounder; |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 844 | SkDevice* fLastDeviceToGainFocus; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 845 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 846 | void prepareForDeviceDraw(SkDevice*, const SkMatrix&, const SkRegion&, |
| 847 | const SkClipStack& clipStack); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 848 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 849 | bool fDeviceCMDirty; // cleared by updateDeviceCMCache() |
| 850 | void updateDeviceCMCache(); |
| 851 | |
| 852 | friend class SkDrawIter; // needs setupDrawForLayerDevice() |
| 853 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 854 | SkDevice* createLayerDevice(SkBitmap::Config, int width, int height, |
| 855 | bool isOpaque); |
| 856 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 857 | SkDevice* init(SkDevice*); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 858 | |
| 859 | // internal methods are not virtual, so they can safely be called by other |
| 860 | // canvas apis, without confusing subclasses (like SkPictureRecording) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 861 | void internalDrawBitmap(const SkBitmap&, const SkIRect*, const SkMatrix& m, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 862 | const SkPaint* paint); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 863 | void internalDrawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, |
| 864 | const SkRect& dst, const SkPaint* paint); |
| 865 | void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 866 | const SkRect& dst, const SkPaint* paint); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 867 | void internalDrawPaint(const SkPaint& paint); |
| 868 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 869 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 870 | void drawDevice(SkDevice*, int x, int y, const SkPaint*); |
| 871 | // shared by save() and saveLayer() |
| 872 | int internalSave(SaveFlags flags); |
| 873 | void internalRestore(); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 874 | static void DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 875 | const SkRect& r, SkScalar textSize); |
| 876 | static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 877 | const char text[], size_t byteLength, |
| 878 | SkScalar x, SkScalar y); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 879 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 880 | /* These maintain a cache of the clip bounds in local coordinates, |
| 881 | (converted to 2s-compliment if floats are slow). |
| 882 | */ |
| 883 | mutable SkRectCompareType fLocalBoundsCompareType; |
| 884 | mutable bool fLocalBoundsCompareTypeDirty; |
| 885 | |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 886 | mutable SkRectCompareType fLocalBoundsCompareTypeBW; |
| 887 | mutable bool fLocalBoundsCompareTypeDirtyBW; |
| 888 | |
| 889 | /* Get the local clip bounds with an anti-aliased edge. |
| 890 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 891 | const SkRectCompareType& getLocalClipBoundsCompareType() const { |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 892 | return getLocalClipBoundsCompareType(kAA_EdgeType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 893 | } |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 894 | |
| 895 | const SkRectCompareType& getLocalClipBoundsCompareType(EdgeType et) const { |
| 896 | if (et == kAA_EdgeType) { |
| 897 | if (fLocalBoundsCompareTypeDirty) { |
| 898 | this->computeLocalClipBoundsCompareType(et); |
| 899 | fLocalBoundsCompareTypeDirty = false; |
| 900 | } |
| 901 | return fLocalBoundsCompareType; |
| 902 | } else { |
| 903 | if (fLocalBoundsCompareTypeDirtyBW) { |
| 904 | this->computeLocalClipBoundsCompareType(et); |
| 905 | fLocalBoundsCompareTypeDirtyBW = false; |
| 906 | } |
| 907 | return fLocalBoundsCompareTypeBW; |
| 908 | } |
| 909 | } |
| 910 | void computeLocalClipBoundsCompareType(EdgeType et) const; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 911 | |
| 912 | SkMatrix fExternalMatrix, fExternalInverse; |
| 913 | bool fUseExternalMatrix; |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 914 | |
| 915 | class AutoValidateClip : ::SkNoncopyable { |
| 916 | public: |
| 917 | explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) { |
| 918 | fCanvas->validateClip(); |
| 919 | } |
| 920 | ~AutoValidateClip() { fCanvas->validateClip(); } |
| 921 | |
| 922 | private: |
| 923 | const SkCanvas* fCanvas; |
| 924 | }; |
| 925 | |
| 926 | #ifdef SK_DEBUG |
| 927 | void validateClip() const; |
| 928 | #else |
| 929 | void validateClip() const {} |
| 930 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | /** Stack helper class to automatically call restoreToCount() on the canvas |
| 934 | when this object goes out of scope. Use this to guarantee that the canvas |
| 935 | is restored to a known state. |
| 936 | */ |
| 937 | class SkAutoCanvasRestore : SkNoncopyable { |
| 938 | public: |
| 939 | SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas) { |
| 940 | SkASSERT(canvas); |
| 941 | fSaveCount = canvas->getSaveCount(); |
| 942 | if (doSave) { |
| 943 | canvas->save(); |
| 944 | } |
| 945 | } |
| 946 | ~SkAutoCanvasRestore() { |
| 947 | fCanvas->restoreToCount(fSaveCount); |
| 948 | } |
| 949 | |
| 950 | private: |
| 951 | SkCanvas* fCanvas; |
| 952 | int fSaveCount; |
| 953 | }; |
| 954 | |
| 955 | #endif |