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; |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 28 | class SkMetaData; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | class SkPicture; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 30 | class SkRRect; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 31 | class SkSurface_Base; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | |
| 33 | /** \class SkCanvas |
| 34 | |
| 35 | A Canvas encapsulates all of the state about drawing into a device (bitmap). |
| 36 | This includes a reference to the device itself, and a stack of matrix/clip |
| 37 | values. For any given draw call (e.g. drawRect), the geometry of the object |
| 38 | being drawn is transformed by the concatenation of all the matrices in the |
| 39 | stack. The transformed geometry is clipped by the intersection of all of |
| 40 | the clips in the stack. |
| 41 | |
| 42 | While the Canvas holds the state of the drawing device, the state (style) |
| 43 | of the object being drawn is held by the Paint, which is provided as a |
| 44 | parameter to each of the draw() methods. The Paint holds attributes such as |
| 45 | color, typeface, textSize, strokeWidth, shader (e.g. gradients, patterns), |
| 46 | etc. |
| 47 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 48 | class SK_API SkCanvas : public SkRefCnt { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 50 | SK_DECLARE_INST_COUNT(SkCanvas) |
| 51 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 52 | SkCanvas(); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 53 | |
reed@google.com | 6dc7455 | 2011-07-21 18:00:46 +0000 | [diff] [blame] | 54 | /** Construct a canvas with the specified device to draw into. |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 55 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 56 | @param device Specifies a device for the canvas to draw into. |
| 57 | */ |
| 58 | explicit SkCanvas(SkDevice* device); |
| 59 | |
| 60 | /** Deprecated - Construct a canvas with the specified bitmap to draw into. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | @param bitmap Specifies a bitmap for the canvas to draw into. Its |
| 62 | structure are copied to the canvas. |
| 63 | */ |
| 64 | explicit SkCanvas(const SkBitmap& bitmap); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | virtual ~SkCanvas(); |
| 66 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 67 | SkMetaData& getMetaData(); |
| 68 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | /////////////////////////////////////////////////////////////////////////// |
| 70 | |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 71 | /** |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 72 | * Trigger the immediate execution of all pending draw operations. |
| 73 | */ |
| 74 | void flush(); |
| 75 | |
| 76 | /** |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 77 | * Return the width/height of the underlying device. The current drawable |
| 78 | * area may be small (due to clipping or saveLayer). For a canvas with |
| 79 | * no device, 0,0 will be returned. |
| 80 | */ |
| 81 | SkISize getDeviceSize() const; |
| 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | /** Return the canvas' device object, which may be null. The device holds |
| 84 | the bitmap of the pixels that the canvas draws into. The reference count |
| 85 | of the returned device is not changed by this call. |
| 86 | */ |
| 87 | SkDevice* getDevice() const; |
| 88 | |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 89 | /** |
| 90 | * saveLayer() can create another device (which is later drawn onto |
| 91 | * the previous device). getTopDevice() returns the top-most device current |
| 92 | * installed. Note that this can change on other calls like save/restore, |
| 93 | * so do not access this device after subsequent canvas calls. |
| 94 | * The reference count of the device is not changed. |
reed@google.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 95 | * |
| 96 | * @param updateMatrixClip If this is true, then before the device is |
| 97 | * returned, we ensure that its has been notified about the current |
| 98 | * matrix and clip. Note: this happens automatically when the device |
| 99 | * is drawn to, but is optional here, as there is a small perf hit |
| 100 | * sometimes. |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 101 | */ |
reed@google.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 102 | SkDevice* getTopDevice(bool updateMatrixClip = false) const; |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 103 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 104 | /** |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 105 | * Shortcut for getDevice()->createCompatibleDevice(...). |
| 106 | * If getDevice() == NULL, this method does nothing, and returns NULL. |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 107 | */ |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 108 | SkDevice* createCompatibleDevice(SkBitmap::Config config, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 109 | int width, int height, |
| 110 | bool isOpaque); |
| 111 | |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 112 | /////////////////////////////////////////////////////////////////////////// |
| 113 | |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 114 | /** |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 115 | * This enum can be used with read/writePixels to perform a pixel ops to or |
| 116 | * from an 8888 config other than Skia's native config (SkPMColor). There |
| 117 | * are three byte orders supported: native, BGRA, and RGBA. Each has a |
| 118 | * premultiplied and unpremultiplied variant. |
| 119 | * |
| 120 | * Components of a 8888 pixel can be packed/unpacked from a 32bit word using |
| 121 | * either byte offsets or shift values. Byte offsets are endian-invariant |
| 122 | * while shifts are not. BGRA and RGBA configs are defined by byte |
| 123 | * orderings. The native config is defined by shift values (SK_A32_SHIFT, |
| 124 | * ..., SK_B32_SHIFT). |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 125 | */ |
| 126 | enum Config8888 { |
| 127 | /** |
| 128 | * Skia's native order specified by: |
| 129 | * SK_A32_SHIFT, SK_R32_SHIFT, SK_G32_SHIFT, and SK_B32_SHIFT |
| 130 | * |
| 131 | * kNative_Premul_Config8888 is equivalent to SkPMColor |
| 132 | * kNative_Unpremul_Config8888 has the same component order as SkPMColor |
| 133 | * but is not premultiplied. |
| 134 | */ |
| 135 | kNative_Premul_Config8888, |
| 136 | kNative_Unpremul_Config8888, |
| 137 | /** |
| 138 | * low byte to high byte: B, G, R, A. |
| 139 | */ |
| 140 | kBGRA_Premul_Config8888, |
| 141 | kBGRA_Unpremul_Config8888, |
| 142 | /** |
| 143 | * low byte to high byte: R, G, B, A. |
| 144 | */ |
| 145 | kRGBA_Premul_Config8888, |
tomhudson@google.com | 1f90287 | 2012-06-01 13:15:47 +0000 | [diff] [blame] | 146 | kRGBA_Unpremul_Config8888 |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /** |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 150 | * On success (returns true), copy the canvas pixels into the bitmap. |
| 151 | * On failure, the bitmap parameter is left unchanged and false is |
| 152 | * returned. |
| 153 | * |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 154 | * The canvas' pixels are converted to the bitmap's config. The only |
| 155 | * supported config is kARGB_8888_Config, though this is likely to be |
| 156 | * relaxed in the future. The meaning of config kARGB_8888_Config is |
| 157 | * modified by the enum param config8888. The default value interprets |
| 158 | * kARGB_8888_Config as SkPMColor |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 159 | * |
| 160 | * If the bitmap has pixels already allocated, the canvas pixels will be |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 161 | * written there. If not, bitmap->allocPixels() will be called |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 162 | * automatically. If the bitmap is backed by a texture readPixels will |
| 163 | * fail. |
| 164 | * |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 165 | * The actual pixels written is the intersection of the canvas' bounds, and |
| 166 | * the rectangle formed by the bitmap's width,height and the specified x,y. |
| 167 | * If bitmap pixels extend outside of that intersection, they will not be |
| 168 | * modified. |
| 169 | * |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 170 | * Other failure conditions: |
| 171 | * * If the canvas is backed by a non-raster device (e.g. PDF) then |
| 172 | * readPixels will fail. |
| 173 | * * If bitmap is texture-backed then readPixels will fail. (This may be |
| 174 | * relaxed in the future.) |
| 175 | * |
| 176 | * Example that reads the entire canvas into a bitmap using the native |
| 177 | * SkPMColor: |
| 178 | * SkISize size = canvas->getDeviceSize(); |
| 179 | * bitmap->setConfig(SkBitmap::kARGB_8888_Config, size.fWidth, |
| 180 | * size.fHeight); |
| 181 | * if (canvas->readPixels(bitmap, 0, 0)) { |
| 182 | * // use the pixels |
| 183 | * } |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 184 | */ |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 185 | bool readPixels(SkBitmap* bitmap, |
| 186 | int x, int y, |
| 187 | Config8888 config8888 = kNative_Premul_Config8888); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 188 | |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 189 | /** |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 190 | * DEPRECATED: This will be removed as soon as webkit is no longer relying |
| 191 | * on it. The bitmap is resized to the intersection of srcRect and the |
| 192 | * canvas bounds. New pixels are always allocated on success. Bitmap is |
| 193 | * unmodified on failure. |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 194 | */ |
| 195 | bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Similar to draw sprite, this method will copy the pixels in bitmap onto |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 199 | * the canvas, with the top/left corner specified by (x, y). The canvas' |
| 200 | * pixel values are completely replaced: there is no blending. |
| 201 | * |
| 202 | * Currently if bitmap is backed by a texture this is a no-op. This may be |
| 203 | * relaxed in the future. |
| 204 | * |
| 205 | * If the bitmap has config kARGB_8888_Config then the config8888 param |
| 206 | * will determines how the pixel valuess are intepreted. If the bitmap is |
| 207 | * not kARGB_8888_Config then this parameter is ignored. |
epoger@google.com | 4f1151a | 2011-07-25 15:47:33 +0000 | [diff] [blame] | 208 | * |
| 209 | * Note: If you are recording drawing commands on this canvas to |
| 210 | * SkPicture, writePixels() is ignored! |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 211 | */ |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 212 | void writePixels(const SkBitmap& bitmap, |
| 213 | int x, int y, |
| 214 | Config8888 config8888 = kNative_Premul_Config8888); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | /////////////////////////////////////////////////////////////////////////// |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 217 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | enum SaveFlags { |
| 219 | /** save the matrix state, restoring it on restore() */ |
| 220 | kMatrix_SaveFlag = 0x01, |
| 221 | /** save the clip state, restoring it on restore() */ |
| 222 | kClip_SaveFlag = 0x02, |
| 223 | /** the layer needs to support per-pixel alpha */ |
| 224 | kHasAlphaLayer_SaveFlag = 0x04, |
| 225 | /** the layer needs to support 8-bits per color component */ |
| 226 | kFullColorLayer_SaveFlag = 0x08, |
| 227 | /** the layer should clip against the bounds argument */ |
| 228 | kClipToLayer_SaveFlag = 0x10, |
| 229 | |
| 230 | // helper masks for common choices |
| 231 | kMatrixClip_SaveFlag = 0x03, |
| 232 | kARGB_NoClipLayer_SaveFlag = 0x0F, |
| 233 | kARGB_ClipLayer_SaveFlag = 0x1F |
| 234 | }; |
| 235 | |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 236 | /** 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] | 237 | copy onto a private stack. Subsequent calls to translate, scale, |
reed@android.com | dc3381f | 2010-02-11 16:05:15 +0000 | [diff] [blame] | 238 | rotate, skew, concat or clipRect, clipPath, and setDrawFilter all |
| 239 | operate on this copy. |
| 240 | When the balancing call to restore() is made, the previous matrix, clip, |
| 241 | and drawFilter are restored. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | @return The value to pass to restoreToCount() to balance this save() |
| 243 | */ |
| 244 | virtual int save(SaveFlags flags = kMatrixClip_SaveFlag); |
| 245 | |
| 246 | /** This behaves the same as save(), but in addition it allocates an |
| 247 | offscreen bitmap. All drawing calls are directed there, and only when |
| 248 | 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] | 249 | the canvas (or the previous layer). |
reed@android.com | ad164b2 | 2010-07-02 17:20:51 +0000 | [diff] [blame] | 250 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 251 | limit the size of the offscreen, and thus drawing may be |
| 252 | clipped to it, though that clipping is not guaranteed to |
| 253 | happen. If exact clipping is desired, use clipRect(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | @param paint (may be null) This is copied, and is applied to the |
| 255 | offscreen when restore() is called |
| 256 | @param flags LayerFlags |
| 257 | @return The value to pass to restoreToCount() to balance this save() |
| 258 | */ |
| 259 | virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 260 | SaveFlags flags = kARGB_ClipLayer_SaveFlag); |
| 261 | |
| 262 | /** This behaves the same as save(), but in addition it allocates an |
| 263 | offscreen bitmap. All drawing calls are directed there, and only when |
| 264 | 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] | 265 | the canvas (or the previous layer). |
reed@android.com | 4040861 | 2010-07-02 17:24:23 +0000 | [diff] [blame] | 266 | @param bounds (may be null) This rect, if non-null, is used as a hint to |
| 267 | limit the size of the offscreen, and thus drawing may be |
| 268 | clipped to it, though that clipping is not guaranteed to |
| 269 | happen. If exact clipping is desired, use clipRect(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 270 | @param alpha This is applied to the offscreen when restore() is called. |
| 271 | @param flags LayerFlags |
| 272 | @return The value to pass to restoreToCount() to balance this save() |
| 273 | */ |
| 274 | int saveLayerAlpha(const SkRect* bounds, U8CPU alpha, |
| 275 | SaveFlags flags = kARGB_ClipLayer_SaveFlag); |
| 276 | |
| 277 | /** 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] | 278 | modifications to the matrix/clip/drawFilter state since the last save |
| 279 | call. |
| 280 | 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] | 281 | */ |
| 282 | virtual void restore(); |
| 283 | |
| 284 | /** Returns the number of matrix/clip states on the SkCanvas' private stack. |
| 285 | This will equal # save() calls - # restore() calls. |
| 286 | */ |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 287 | int getSaveCount() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | |
| 289 | /** Efficient way to pop any calls to save() that happened after the save |
| 290 | count reached saveCount. It is an error for saveCount to be less than |
| 291 | getSaveCount() |
| 292 | @param saveCount The number of save() levels to restore from |
| 293 | */ |
| 294 | void restoreToCount(int saveCount); |
| 295 | |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 296 | /** Returns true if drawing is currently going to a layer (from saveLayer) |
| 297 | * rather than to the root device. |
| 298 | */ |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 299 | virtual bool isDrawingToLayer() const; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 300 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 301 | /** Preconcat the current matrix with the specified translation |
| 302 | @param dx The distance to translate in X |
| 303 | @param dy The distance to translate in Y |
| 304 | returns true if the operation succeeded (e.g. did not overflow) |
| 305 | */ |
| 306 | virtual bool translate(SkScalar dx, SkScalar dy); |
| 307 | |
| 308 | /** Preconcat the current matrix with the specified scale. |
| 309 | @param sx The amount to scale in X |
| 310 | @param sy The amount to scale in Y |
| 311 | returns true if the operation succeeded (e.g. did not overflow) |
| 312 | */ |
| 313 | virtual bool scale(SkScalar sx, SkScalar sy); |
| 314 | |
| 315 | /** Preconcat the current matrix with the specified rotation. |
| 316 | @param degrees The amount to rotate, in degrees |
| 317 | returns true if the operation succeeded (e.g. did not overflow) |
| 318 | */ |
| 319 | virtual bool rotate(SkScalar degrees); |
| 320 | |
| 321 | /** Preconcat the current matrix with the specified skew. |
| 322 | @param sx The amount to skew in X |
| 323 | @param sy The amount to skew in Y |
| 324 | returns true if the operation succeeded (e.g. did not overflow) |
| 325 | */ |
| 326 | virtual bool skew(SkScalar sx, SkScalar sy); |
| 327 | |
| 328 | /** Preconcat the current matrix with the specified matrix. |
| 329 | @param matrix The matrix to preconcatenate with the current matrix |
| 330 | @return true if the operation succeeded (e.g. did not overflow) |
| 331 | */ |
| 332 | virtual bool concat(const SkMatrix& matrix); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 333 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | /** Replace the current matrix with a copy of the specified matrix. |
| 335 | @param matrix The matrix that will be copied into the current matrix. |
| 336 | */ |
| 337 | virtual void setMatrix(const SkMatrix& matrix); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 338 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | /** Helper for setMatrix(identity). Sets the current matrix to identity. |
| 340 | */ |
| 341 | void resetMatrix(); |
| 342 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 343 | /** |
| 344 | * Modify the current clip with the specified rectangle. |
| 345 | * @param rect The rect to combine with the current clip |
| 346 | * @param op The region op to apply to the current clip |
| 347 | * @param doAntiAlias true if the clip should be antialiased |
| 348 | * @return true if the canvas' clip is non-empty |
| 349 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | virtual bool clipRect(const SkRect& rect, |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 351 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 352 | bool doAntiAlias = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 354 | /** |
| 355 | * Modify the current clip with the specified SkRRect. |
| 356 | * @param rrect The rrect to combine with the current clip |
| 357 | * @param op The region op to apply to the current clip |
| 358 | * @param doAntiAlias true if the clip should be antialiased |
| 359 | * @return true if the canvas' clip is non-empty |
| 360 | */ |
| 361 | virtual bool clipRRect(const SkRRect& rrect, |
| 362 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 363 | bool doAntiAlias = false); |
| 364 | |
| 365 | /** |
| 366 | * Modify the current clip with the specified path. |
| 367 | * @param path The path to combine with the current clip |
| 368 | * @param op The region op to apply to the current clip |
| 369 | * @param doAntiAlias true if the clip should be antialiased |
| 370 | * @return true if the canvas' new clip is non-empty |
| 371 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | virtual bool clipPath(const SkPath& path, |
reed@google.com | 071eef9 | 2011-10-12 11:52:53 +0000 | [diff] [blame] | 373 | SkRegion::Op op = SkRegion::kIntersect_Op, |
| 374 | bool doAntiAlias = false); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 376 | /** EXPERIMENTAL -- only used for testing |
| 377 | Set to false to force clips to be hard, even if doAntiAlias=true is |
| 378 | passed to clipRect or clipPath. |
| 379 | */ |
| 380 | void setAllowSoftClip(bool allow) { |
| 381 | fAllowSoftClip = allow; |
| 382 | } |
| 383 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 384 | /** EXPERIMENTAL -- only used for testing |
| 385 | Set to simplify clip stack using path ops. |
| 386 | */ |
| 387 | void setAllowSimplifyClip(bool allow) { |
| 388 | fAllowSimplifyClip = allow; |
| 389 | } |
| 390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | /** Modify the current clip with the specified region. Note that unlike |
| 392 | clipRect() and clipPath() which transform their arguments by the current |
| 393 | matrix, clipRegion() assumes its argument is already in device |
| 394 | coordinates, and so no transformation is performed. |
| 395 | @param deviceRgn The region to apply to the current clip |
| 396 | @param op The region op to apply to the current clip |
| 397 | @return true if the canvas' new clip is non-empty |
| 398 | */ |
| 399 | virtual bool clipRegion(const SkRegion& deviceRgn, |
| 400 | SkRegion::Op op = SkRegion::kIntersect_Op); |
| 401 | |
| 402 | /** Helper for clipRegion(rgn, kReplace_Op). Sets the current clip to the |
| 403 | specified region. This does not intersect or in any other way account |
| 404 | for the existing clip region. |
| 405 | @param deviceRgn The region to copy into the current clip. |
| 406 | @return true if the new clip region is non-empty |
| 407 | */ |
| 408 | bool setClipRegion(const SkRegion& deviceRgn) { |
| 409 | return this->clipRegion(deviceRgn, SkRegion::kReplace_Op); |
| 410 | } |
| 411 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 412 | /** Return true if the specified rectangle, after being transformed by the |
| 413 | current matrix, would lie completely outside of the current clip. Call |
| 414 | this to check if an area you intend to draw into is clipped out (and |
| 415 | therefore you can skip making the draw calls). |
| 416 | @param rect the rect to compare with the current clip |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 417 | @return true if the rect (transformed by the canvas' matrix) does not |
| 418 | intersect with the canvas' clip |
| 419 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 420 | bool quickReject(const SkRect& rect) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | |
| 422 | /** Return true if the specified path, after being transformed by the |
| 423 | current matrix, would lie completely outside of the current clip. Call |
| 424 | this to check if an area you intend to draw into is clipped out (and |
| 425 | therefore you can skip making the draw calls). Note, for speed it may |
| 426 | return false even if the path itself might not intersect the clip |
| 427 | (i.e. the bounds of the path intersects, but the path does not). |
| 428 | @param path The path to compare with the current clip |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | @return true if the path (transformed by the canvas' matrix) does not |
| 430 | intersect with the canvas' clip |
| 431 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 432 | bool quickReject(const SkPath& path) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | |
| 434 | /** Return true if the horizontal band specified by top and bottom is |
| 435 | completely clipped out. This is a conservative calculation, meaning |
| 436 | that it is possible that if the method returns false, the band may still |
| 437 | in fact be clipped out, but the converse is not true. If this method |
| 438 | returns true, then the band is guaranteed to be clipped out. |
| 439 | @param top The top of the horizontal band to compare with the clip |
| 440 | @param bottom The bottom of the horizontal and to compare with the clip |
| 441 | @return true if the horizontal band is completely clipped out (i.e. does |
| 442 | not intersect the current clip) |
| 443 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 444 | bool quickRejectY(SkScalar top, SkScalar bottom) const { |
djsollen@google.com | 92d2a29 | 2012-02-27 16:17:59 +0000 | [diff] [blame] | 445 | SkASSERT(SkScalarToCompareType(top) <= SkScalarToCompareType(bottom)); |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 446 | const SkRectCompareType& clipR = this->getLocalClipBoundsCompareType(); |
djsollen@google.com | 92d2a29 | 2012-02-27 16:17:59 +0000 | [diff] [blame] | 447 | // In the case where the clip is empty and we are provided with a |
| 448 | // negative top and positive bottom parameter then this test will return |
| 449 | // false even though it will be clipped. We have chosen to exclude that |
| 450 | // check as it is rare and would result double the comparisons. |
| 451 | return SkScalarToCompareType(top) >= clipR.fBottom |
| 452 | || SkScalarToCompareType(bottom) <= clipR.fTop; |
| 453 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 454 | |
| 455 | /** Return the bounds of the current clip (in local coordinates) in the |
| 456 | bounds parameter, and return true if it is non-empty. This can be useful |
| 457 | in a way similar to quickReject, in that it tells you that drawing |
| 458 | outside of these bounds will be clipped out. |
| 459 | */ |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 460 | bool getClipBounds(SkRect* bounds) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 461 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 462 | /** Return the bounds of the current clip, in device coordinates; returns |
| 463 | true if non-empty. Maybe faster than getting the clip explicitly and |
| 464 | then taking its bounds. |
| 465 | */ |
| 466 | bool getClipDeviceBounds(SkIRect* bounds) const; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 467 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 468 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 469 | /** 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] | 470 | specified ARGB color, using the specified mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 471 | @param a the alpha component (0..255) of the color to fill the canvas |
| 472 | @param r the red component (0..255) of the color to fill the canvas |
| 473 | @param g the green component (0..255) of the color to fill the canvas |
| 474 | @param b the blue component (0..255) of the color to fill the canvas |
| 475 | @param mode the mode to apply the color in (defaults to SrcOver) |
| 476 | */ |
| 477 | void drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 478 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 479 | |
| 480 | /** 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] | 481 | specified color and mode. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | @param color the color to draw with |
| 483 | @param mode the mode to apply the color in (defaults to SrcOver) |
| 484 | */ |
| 485 | void drawColor(SkColor color, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 486 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 487 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 488 | /** |
| 489 | * This erases the entire drawing surface to the specified color, |
| 490 | * irrespective of the clip. It does not blend with the previous pixels, |
| 491 | * but always overwrites them. |
| 492 | * |
| 493 | * It is roughly equivalent to the following: |
| 494 | * canvas.save(); |
| 495 | * canvas.clipRect(hugeRect, kReplace_Op); |
| 496 | * paint.setColor(color); |
| 497 | * paint.setXfermodeMode(kSrc_Mode); |
| 498 | * canvas.drawPaint(paint); |
| 499 | * canvas.restore(); |
| 500 | * though it is almost always much more efficient. |
| 501 | */ |
| 502 | virtual void clear(SkColor); |
| 503 | |
| 504 | /** |
| 505 | * Fill the entire canvas' bitmap (restricted to the current clip) with the |
| 506 | * specified paint. |
| 507 | * @param paint The paint used to fill the canvas |
| 508 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 509 | virtual void drawPaint(const SkPaint& paint); |
| 510 | |
| 511 | enum PointMode { |
| 512 | /** drawPoints draws each point separately */ |
| 513 | kPoints_PointMode, |
| 514 | /** drawPoints draws each pair of points as a line segment */ |
| 515 | kLines_PointMode, |
| 516 | /** drawPoints draws the array of points as a polygon */ |
| 517 | kPolygon_PointMode |
| 518 | }; |
| 519 | |
| 520 | /** Draw a series of points, interpreted based on the PointMode mode. For |
| 521 | all modes, the count parameter is interpreted as the total number of |
| 522 | points. For kLine mode, count/2 line segments are drawn. |
| 523 | For kPoint mode, each point is drawn centered at its coordinate, and its |
| 524 | size is specified by the paint's stroke-width. It draws as a square, |
| 525 | unless the paint's cap-type is round, in which the points are drawn as |
| 526 | circles. |
| 527 | For kLine mode, each pair of points is drawn as a line segment, |
| 528 | respecting the paint's settings for cap/join/width. |
| 529 | For kPolygon mode, the entire array is drawn as a series of connected |
| 530 | line segments. |
| 531 | Note that, while similar, kLine and kPolygon modes draw slightly |
| 532 | differently than the equivalent path built with a series of moveto, |
| 533 | lineto calls, in that the path will draw all of its contours at once, |
| 534 | with no interactions if contours intersect each other (think XOR |
| 535 | xfermode). drawPoints always draws each element one at a time. |
| 536 | @param mode PointMode specifying how to draw the array of points. |
| 537 | @param count The number of points in the array |
| 538 | @param pts Array of points to draw |
| 539 | @param paint The paint used to draw the points |
| 540 | */ |
| 541 | virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 542 | const SkPaint& paint); |
| 543 | |
| 544 | /** Helper method for drawing a single point. See drawPoints() for a more |
| 545 | details. |
| 546 | */ |
| 547 | void drawPoint(SkScalar x, SkScalar y, const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 548 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 549 | /** Draws a single pixel in the specified color. |
| 550 | @param x The X coordinate of which pixel to draw |
| 551 | @param y The Y coordiante of which pixel to draw |
| 552 | @param color The color to draw |
| 553 | */ |
| 554 | void drawPoint(SkScalar x, SkScalar y, SkColor color); |
| 555 | |
| 556 | /** Draw a line segment with the specified start and stop x,y coordinates, |
| 557 | using the specified paint. NOTE: since a line is always "framed", the |
| 558 | paint's Style is ignored. |
| 559 | @param x0 The x-coordinate of the start point of the line |
| 560 | @param y0 The y-coordinate of the start point of the line |
| 561 | @param x1 The x-coordinate of the end point of the line |
| 562 | @param y1 The y-coordinate of the end point of the line |
| 563 | @param paint The paint used to draw the line |
| 564 | */ |
| 565 | void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 566 | const SkPaint& paint); |
| 567 | |
| 568 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 569 | will be filled or stroked based on the Style in the paint. |
| 570 | @param rect The rect to be drawn |
| 571 | @param paint The paint used to draw the rect |
| 572 | */ |
| 573 | virtual void drawRect(const SkRect& rect, const SkPaint& paint); |
| 574 | |
| 575 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 576 | will be filled or framed based on the Style in the paint. |
| 577 | @param rect The rect to be drawn |
| 578 | @param paint The paint used to draw the rect |
| 579 | */ |
| 580 | void drawIRect(const SkIRect& rect, const SkPaint& paint) |
| 581 | { |
| 582 | SkRect r; |
| 583 | r.set(rect); // promotes the ints to scalars |
| 584 | this->drawRect(r, paint); |
| 585 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 586 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 587 | /** Draw the specified rectangle using the specified paint. The rectangle |
| 588 | will be filled or framed based on the Style in the paint. |
| 589 | @param left The left side of the rectangle to be drawn |
| 590 | @param top The top side of the rectangle to be drawn |
| 591 | @param right The right side of the rectangle to be drawn |
| 592 | @param bottom The bottom side of the rectangle to be drawn |
| 593 | @param paint The paint used to draw the rect |
| 594 | */ |
| 595 | void drawRectCoords(SkScalar left, SkScalar top, SkScalar right, |
| 596 | SkScalar bottom, const SkPaint& paint); |
| 597 | |
| 598 | /** Draw the specified oval using the specified paint. The oval will be |
| 599 | filled or framed based on the Style in the paint. |
| 600 | @param oval The rectangle bounds of the oval to be drawn |
| 601 | @param paint The paint used to draw the oval |
| 602 | */ |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 603 | virtual void drawOval(const SkRect& oval, const SkPaint&); |
| 604 | |
| 605 | /** |
| 606 | * Draw the specified RRect using the specified paint The rrect will be filled or stroked |
| 607 | * based on the Style in the paint. |
| 608 | * |
| 609 | * @param rrect The round-rect to draw |
| 610 | * @param paint The paint used to draw the round-rect |
| 611 | */ |
| 612 | virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 613 | |
| 614 | /** Draw the specified circle using the specified paint. If radius is <= 0, |
| 615 | then nothing will be drawn. The circle will be filled |
| 616 | or framed based on the Style in the paint. |
| 617 | @param cx The x-coordinate of the center of the cirle to be drawn |
| 618 | @param cy The y-coordinate of the center of the cirle to be drawn |
| 619 | @param radius The radius of the cirle to be drawn |
| 620 | @param paint The paint used to draw the circle |
| 621 | */ |
| 622 | void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 623 | const SkPaint& paint); |
| 624 | |
| 625 | /** Draw the specified arc, which will be scaled to fit inside the |
| 626 | specified oval. If the sweep angle is >= 360, then the oval is drawn |
| 627 | completely. Note that this differs slightly from SkPath::arcTo, which |
| 628 | treats the sweep angle mod 360. |
| 629 | @param oval The bounds of oval used to define the shape of the arc |
| 630 | @param startAngle Starting angle (in degrees) where the arc begins |
| 631 | @param sweepAngle Sweep angle (in degrees) measured clockwise |
| 632 | @param useCenter true means include the center of the oval. For filling |
| 633 | this will draw a wedge. False means just use the arc. |
| 634 | @param paint The paint used to draw the arc |
| 635 | */ |
| 636 | void drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, |
| 637 | bool useCenter, const SkPaint& paint); |
| 638 | |
| 639 | /** Draw the specified round-rect using the specified paint. The round-rect |
| 640 | will be filled or framed based on the Style in the paint. |
| 641 | @param rect The rectangular bounds of the roundRect to be drawn |
| 642 | @param rx The x-radius of the oval used to round the corners |
| 643 | @param ry The y-radius of the oval used to round the corners |
| 644 | @param paint The paint used to draw the roundRect |
| 645 | */ |
| 646 | void drawRoundRect(const SkRect& rect, SkScalar rx, SkScalar ry, |
| 647 | const SkPaint& paint); |
| 648 | |
| 649 | /** Draw the specified path using the specified paint. The path will be |
| 650 | filled or framed based on the Style in the paint. |
| 651 | @param path The path to be drawn |
| 652 | @param paint The paint used to draw the path |
| 653 | */ |
| 654 | virtual void drawPath(const SkPath& path, const SkPaint& paint); |
| 655 | |
| 656 | /** Draw the specified bitmap, with its top/left corner at (x,y), using the |
| 657 | specified paint, transformed by the current matrix. Note: if the paint |
| 658 | contains a maskfilter that generates a mask which extends beyond the |
| 659 | bitmap's original width/height, then the bitmap will be drawn as if it |
| 660 | were in a Shader with CLAMP mode. Thus the color outside of the original |
| 661 | width/height will be the edge color replicated. |
| 662 | @param bitmap The bitmap to be drawn |
| 663 | @param left The position of the left side of the bitmap being drawn |
| 664 | @param top The position of the top side of the bitmap being drawn |
| 665 | @param paint The paint used to draw the bitmap, or NULL |
| 666 | */ |
| 667 | virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 668 | const SkPaint* paint = NULL); |
| 669 | |
| 670 | /** Draw the specified bitmap, with the specified matrix applied (before the |
| 671 | canvas' matrix is applied). |
| 672 | @param bitmap The bitmap to be drawn |
| 673 | @param src Optional: specify the subset of the bitmap to be drawn |
| 674 | @param dst The destination rectangle where the scaled/translated |
| 675 | image will be drawn |
| 676 | @param paint The paint used to draw the bitmap, or NULL |
| 677 | */ |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 678 | virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
| 679 | const SkRect& dst, |
| 680 | const SkPaint* paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 681 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 682 | void drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, |
| 683 | const SkPaint* paint) { |
| 684 | this->drawBitmapRectToRect(bitmap, NULL, dst, paint); |
| 685 | } |
| 686 | |
| 687 | void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* isrc, |
| 688 | const SkRect& dst, const SkPaint* paint = NULL) { |
| 689 | SkRect realSrcStorage; |
| 690 | SkRect* realSrcPtr = NULL; |
| 691 | if (isrc) { |
| 692 | realSrcStorage.set(*isrc); |
| 693 | realSrcPtr = &realSrcStorage; |
| 694 | } |
| 695 | this->drawBitmapRectToRect(bitmap, realSrcPtr, dst, paint); |
| 696 | } |
skia.committer@gmail.com | c1ad022 | 2012-09-19 02:01:47 +0000 | [diff] [blame] | 697 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
| 699 | const SkPaint* paint = NULL); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 700 | |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 701 | /** |
| 702 | * Draw the bitmap stretched differentially to fit into dst. |
| 703 | * center is a rect within the bitmap, and logically divides the bitmap |
| 704 | * into 9 sections (3x3). For example, if the middle pixel of a [5x5] |
| 705 | * bitmap is the "center", then the center-rect should be [2, 2, 3, 3]. |
| 706 | * |
| 707 | * If the dst is >= the bitmap size, then... |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame^] | 708 | * - The 4 corners are not stretched at all. |
| 709 | * - The sides are stretched in only one axis. |
| 710 | * - The center is stretched in both axes. |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 711 | * Else, for each axis where dst < bitmap, |
| 712 | * - The corners shrink proportionally |
| 713 | * - The sides (along the shrink axis) and center are not drawn |
| 714 | */ |
| 715 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 716 | const SkRect& dst, const SkPaint* paint = NULL); |
| 717 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 718 | /** Draw the specified bitmap, with its top/left corner at (x,y), |
| 719 | NOT transformed by the current matrix. Note: if the paint |
| 720 | contains a maskfilter that generates a mask which extends beyond the |
| 721 | bitmap's original width/height, then the bitmap will be drawn as if it |
| 722 | were in a Shader with CLAMP mode. Thus the color outside of the original |
| 723 | width/height will be the edge color replicated. |
| 724 | @param bitmap The bitmap to be drawn |
| 725 | @param left The position of the left side of the bitmap being drawn |
| 726 | @param top The position of the top side of the bitmap being drawn |
| 727 | @param paint The paint used to draw the bitmap, or NULL |
| 728 | */ |
| 729 | virtual void drawSprite(const SkBitmap& bitmap, int left, int top, |
| 730 | const SkPaint* paint = NULL); |
| 731 | |
| 732 | /** Draw the text, with origin at (x,y), using the specified paint. |
| 733 | The origin is interpreted based on the Align setting in the paint. |
| 734 | @param text The text to be drawn |
| 735 | @param byteLength The number of bytes to read from the text parameter |
| 736 | @param x The x-coordinate of the origin of the text being drawn |
| 737 | @param y The y-coordinate of the origin of the text being drawn |
| 738 | @param paint The paint used for the text (e.g. color, size, style) |
| 739 | */ |
| 740 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
| 741 | SkScalar y, const SkPaint& paint); |
| 742 | |
| 743 | /** 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] | 744 | 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] | 745 | @param text The text to be drawn |
| 746 | @param byteLength The number of bytes to read from the text parameter |
| 747 | @param pos Array of positions, used to position each character |
| 748 | @param paint The paint used for the text (e.g. color, size, style) |
| 749 | */ |
| 750 | virtual void drawPosText(const void* text, size_t byteLength, |
| 751 | const SkPoint pos[], const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 752 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 753 | /** Draw the text, with each character/glyph origin specified by the x |
| 754 | 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] | 755 | The origin is interpreted by the Align setting in the paint. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 756 | @param text The text to be drawn |
| 757 | @param byteLength The number of bytes to read from the text parameter |
| 758 | @param xpos Array of x-positions, used to position each character |
| 759 | @param constY The shared Y coordinate for all of the positions |
| 760 | @param paint The paint used for the text (e.g. color, size, style) |
| 761 | */ |
| 762 | virtual void drawPosTextH(const void* text, size_t byteLength, |
| 763 | const SkScalar xpos[], SkScalar constY, |
| 764 | const SkPaint& paint); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 765 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 766 | /** Draw the text, with origin at (x,y), using the specified paint, along |
| 767 | the specified path. The paint's Align setting determins where along the |
| 768 | path to start the text. |
| 769 | @param text The text to be drawn |
| 770 | @param byteLength The number of bytes to read from the text parameter |
| 771 | @param path The path the text should follow for its baseline |
| 772 | @param hOffset The distance along the path to add to the text's |
| 773 | starting position |
| 774 | @param vOffset The distance above(-) or below(+) the path to |
| 775 | position the text |
| 776 | @param paint The paint used for the text |
| 777 | */ |
| 778 | void drawTextOnPathHV(const void* text, size_t byteLength, |
| 779 | const SkPath& path, SkScalar hOffset, |
| 780 | SkScalar vOffset, const SkPaint& paint); |
| 781 | |
| 782 | /** Draw the text, with origin at (x,y), using the specified paint, along |
| 783 | the specified path. The paint's Align setting determins where along the |
| 784 | path to start the text. |
| 785 | @param text The text to be drawn |
| 786 | @param byteLength The number of bytes to read from the text parameter |
| 787 | @param path The path the text should follow for its baseline |
| 788 | @param matrix (may be null) Applied to the text before it is |
| 789 | mapped onto the path |
| 790 | @param paint The paint used for the text |
| 791 | */ |
| 792 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 793 | const SkPath& path, const SkMatrix* matrix, |
| 794 | const SkPaint& paint); |
| 795 | |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 796 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 797 | /** Draw the text on path, with each character/glyph origin specified by the pos[] |
| 798 | array. The origin is interpreted by the Align setting in the paint. |
| 799 | @param text The text to be drawn |
| 800 | @param byteLength The number of bytes to read from the text parameter |
| 801 | @param pos Array of positions, used to position each character |
| 802 | @param paint The paint used for the text (e.g. color, size, style) |
| 803 | @param path The path to draw on |
| 804 | @param matrix The canvas matrix |
| 805 | */ |
| 806 | void drawPosTextOnPath(const void* text, size_t byteLength, |
| 807 | const SkPoint pos[], const SkPaint& paint, |
| 808 | const SkPath& path, const SkMatrix* matrix); |
| 809 | #endif |
| 810 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 811 | /** Draw the picture into this canvas. This method effective brackets the |
| 812 | 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] | 813 | of this canvas will be unchanged after this call. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 814 | @param picture The recorded drawing commands to playback into this |
| 815 | canvas. |
| 816 | */ |
| 817 | virtual void drawPicture(SkPicture& picture); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 818 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 819 | enum VertexMode { |
| 820 | kTriangles_VertexMode, |
| 821 | kTriangleStrip_VertexMode, |
| 822 | kTriangleFan_VertexMode |
| 823 | }; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 824 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 825 | /** Draw the array of vertices, interpreted as triangles (based on mode). |
| 826 | @param vmode How to interpret the array of vertices |
| 827 | @param vertexCount The number of points in the vertices array (and |
| 828 | corresponding texs and colors arrays if non-null) |
| 829 | @param vertices Array of vertices for the mesh |
| 830 | @param texs May be null. If not null, specifies the coordinate |
| 831 | in texture space for each vertex. |
| 832 | @param colors May be null. If not null, specifies a color for each |
| 833 | vertex, to be interpolated across the triangle. |
| 834 | @param xmode Used if both texs and colors are present. In this |
| 835 | case the colors are combined with the texture using mode, |
| 836 | before being drawn using the paint. If mode is null, then |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 837 | kModulate_Mode is used. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 838 | @param indices If not null, array of indices to reference into the |
| 839 | vertex (texs, colors) array. |
| 840 | @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] | 841 | @param paint Specifies the shader/texture if present. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 842 | */ |
| 843 | virtual void drawVertices(VertexMode vmode, int vertexCount, |
| 844 | const SkPoint vertices[], const SkPoint texs[], |
| 845 | const SkColor colors[], SkXfermode* xmode, |
| 846 | const uint16_t indices[], int indexCount, |
| 847 | const SkPaint& paint); |
| 848 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 849 | /** Send a blob of data to the canvas. |
| 850 | For canvases that draw, this call is effectively a no-op, as the data |
| 851 | is not parsed, but just ignored. However, this call exists for |
| 852 | subclasses like SkPicture's recording canvas, that can store the data |
| 853 | and then play it back later (via another call to drawData). |
| 854 | */ |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 855 | virtual void drawData(const void* data, size_t length) { |
| 856 | // do nothing. Subclasses may do something with the data |
| 857 | } |
| 858 | |
skia.committer@gmail.com | a5d3e77 | 2013-05-30 07:01:29 +0000 | [diff] [blame] | 859 | /** Add comments. beginCommentGroup/endCommentGroup open/close a new group. |
| 860 | Each comment added via addComment is notionally attached to its |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 861 | enclosing group. Top-level comments simply belong to no group. |
| 862 | */ |
| 863 | virtual void beginCommentGroup(const char* description) { |
| 864 | // do nothing. Subclasses may do something |
| 865 | } |
| 866 | virtual void addComment(const char* kywd, const char* value) { |
| 867 | // do nothing. Subclasses may do something |
| 868 | } |
| 869 | virtual void endCommentGroup() { |
| 870 | // do nothing. Subclasses may do something |
| 871 | } |
| 872 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 873 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 874 | ////////////////////////////////////////////////////////////////////////// |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 875 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 876 | /** Get the current bounder object. |
| 877 | The bounder's reference count is unchaged. |
| 878 | @return the canva's bounder (or NULL). |
| 879 | */ |
| 880 | SkBounder* getBounder() const { return fBounder; } |
| 881 | |
| 882 | /** Set a new bounder (or NULL). |
| 883 | Pass NULL to clear any previous bounder. |
| 884 | As a convenience, the parameter passed is also returned. |
| 885 | If a previous bounder exists, its reference count is decremented. |
| 886 | If bounder is not NULL, its reference count is incremented. |
| 887 | @param bounder the new bounder (or NULL) to be installed in the canvas |
| 888 | @return the set bounder object |
| 889 | */ |
| 890 | virtual SkBounder* setBounder(SkBounder* bounder); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 891 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 892 | /** 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] | 893 | 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] | 894 | @return the canvas' filter (or NULL). |
| 895 | */ |
| 896 | SkDrawFilter* getDrawFilter() const; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 897 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 898 | /** Set the new filter (or NULL). Pass NULL to clear any existing filter. |
| 899 | As a convenience, the parameter is returned. If an existing filter |
| 900 | 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] | 901 | refcnt is incremented. The filter is saved/restored, just like the |
| 902 | matrix and clip. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 903 | @param filter the new filter (or NULL) |
| 904 | @return the new filter |
| 905 | */ |
| 906 | virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter); |
| 907 | |
| 908 | ////////////////////////////////////////////////////////////////////////// |
| 909 | |
| 910 | /** Return the current matrix on the canvas. |
| 911 | This does not account for the translate in any of the devices. |
| 912 | @return The current matrix on the canvas. |
| 913 | */ |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 914 | const SkMatrix& getTotalMatrix() const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 915 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 916 | enum ClipType { |
| 917 | kEmpty_ClipType = 0, |
| 918 | kRect_ClipType, |
| 919 | kComplex_ClipType |
| 920 | }; |
| 921 | |
| 922 | /** Returns a description of the total clip; may be cheaper than |
| 923 | getting the clip and querying it directly. |
| 924 | */ |
| 925 | ClipType getClipType() const; |
| 926 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 927 | /** Return the current device clip (concatenation of all clip calls). |
reed@google.com | a707f60 | 2012-04-12 16:12:16 +0000 | [diff] [blame] | 928 | * This does not account for the translate in any of the devices. |
| 929 | * @return the current device clip (concatenation of all clip calls). |
reed@google.com | 5e2457e | 2011-10-10 21:24:37 +0000 | [diff] [blame] | 930 | * |
reed@google.com | a707f60 | 2012-04-12 16:12:16 +0000 | [diff] [blame] | 931 | * DEPRECATED -- call getClipDeviceBounds() instead. |
reed@google.com | 5e2457e | 2011-10-10 21:24:37 +0000 | [diff] [blame] | 932 | */ |
reed@google.com | a707f60 | 2012-04-12 16:12:16 +0000 | [diff] [blame] | 933 | const SkRegion& getTotalClip() const; |
reed@google.com | 5e2457e | 2011-10-10 21:24:37 +0000 | [diff] [blame] | 934 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 935 | /** Return the clip stack. The clip stack stores all the individual |
| 936 | * clips organized by the save/restore frame in which they were |
| 937 | * added. |
| 938 | * @return the current clip stack ("list" of individual clip elements) |
| 939 | */ |
| 940 | const SkClipStack* getClipStack() const { |
| 941 | return &fClipStack; |
| 942 | } |
| 943 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 944 | class ClipVisitor { |
| 945 | public: |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 946 | virtual ~ClipVisitor(); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 947 | virtual void clipRect(const SkRect&, SkRegion::Op, bool antialias) = 0; |
| 948 | virtual void clipPath(const SkPath&, SkRegion::Op, bool antialias) = 0; |
| 949 | }; |
| 950 | |
| 951 | /** |
| 952 | * Replays the clip operations, back to front, that have been applied to |
| 953 | * the canvas, calling the appropriate method on the visitor for each |
| 954 | * clip. All clips have already been transformed into device space. |
| 955 | */ |
| 956 | void replayClips(ClipVisitor*) const; |
| 957 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 958 | /////////////////////////////////////////////////////////////////////////// |
| 959 | |
| 960 | /** After calling saveLayer(), there can be any number of devices that make |
| 961 | up the top-most drawing area. LayerIter can be used to iterate through |
| 962 | those devices. Note that the iterator is only valid until the next API |
| 963 | call made on the canvas. Ownership of all pointers in the iterator stays |
| 964 | with the canvas, so none of them should be modified or deleted. |
| 965 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 966 | class SK_API LayerIter /*: SkNoncopyable*/ { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 967 | public: |
| 968 | /** Initialize iterator with canvas, and set values for 1st device */ |
| 969 | LayerIter(SkCanvas*, bool skipEmptyClips); |
| 970 | ~LayerIter(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 971 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 972 | /** Return true if the iterator is done */ |
| 973 | bool done() const { return fDone; } |
| 974 | /** Cycle to the next device */ |
| 975 | void next(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 976 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 977 | // These reflect the current device in the iterator |
| 978 | |
| 979 | SkDevice* device() const; |
| 980 | const SkMatrix& matrix() const; |
| 981 | const SkRegion& clip() const; |
| 982 | const SkPaint& paint() const; |
| 983 | int x() const; |
| 984 | int y() const; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 985 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 986 | private: |
| 987 | // used to embed the SkDrawIter object directly in our instance, w/o |
| 988 | // having to expose that class def to the public. There is an assert |
| 989 | // in our constructor to ensure that fStorage is large enough |
| 990 | // (though needs to be a compile-time-assert!). We use intptr_t to work |
| 991 | // 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] | 992 | intptr_t fStorage[32]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 993 | class SkDrawIter* fImpl; // this points at fStorage |
| 994 | SkPaint fDefaultPaint; |
| 995 | bool fDone; |
| 996 | }; |
| 997 | |
| 998 | protected: |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 999 | // Returns the canvas to be used by DrawIter. Default implementation |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 1000 | // returns this. Subclasses that encapsulate an indirect canvas may |
| 1001 | // need to overload this method. The impl must keep track of this, as it |
| 1002 | // is not released or deleted by the caller. |
| 1003 | virtual SkCanvas* canvasForDrawIter(); |
| 1004 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1005 | // Clip rectangle bounds. Called internally by saveLayer. |
| 1006 | // returns false if the entire rectangle is entirely clipped out |
| 1007 | bool clipRectBounds(const SkRect* bounds, SaveFlags flags, |
reed@google.com | 49794c0 | 2012-03-15 12:10:47 +0000 | [diff] [blame] | 1008 | SkIRect* intersection); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1009 | |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1010 | // Called by child classes that override clipPath and clipRRect to only |
| 1011 | // track fast conservative clip bounds, rather than exact clips. |
| 1012 | bool updateClipConservativelyUsingBounds(const SkRect&, SkRegion::Op, |
| 1013 | bool inverseFilled); |
| 1014 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 1015 | // notify our surface (if we have one) that we are about to draw, so it |
| 1016 | // can perform copy-on-write or invalidate any cached images |
| 1017 | void predrawNotify(); |
| 1018 | |
reed@google.com | 260eae5 | 2012-11-14 19:07:24 +0000 | [diff] [blame] | 1019 | /** DEPRECATED -- use constructor(device) |
| 1020 | |
| 1021 | Marked as 'protected' to avoid new clients using this before we can |
| 1022 | completely remove it. |
| 1023 | |
| 1024 | Specify a device for this canvas to draw into. If it is not null, its |
| 1025 | reference count is incremented. If the canvas was already holding a |
| 1026 | device, its reference count is decremented. The new device is returned. |
| 1027 | */ |
| 1028 | virtual SkDevice* setDevice(SkDevice* device); |
| 1029 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1030 | private: |
| 1031 | class MCRec; |
| 1032 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1033 | SkClipStack fClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1034 | SkDeque fMCStack; |
| 1035 | // points to top of stack |
| 1036 | MCRec* fMCRec; |
| 1037 | // the first N recs that can fit here mean we won't call malloc |
| 1038 | uint32_t fMCRecStorage[32]; |
| 1039 | |
| 1040 | SkBounder* fBounder; |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 1041 | int fSaveLayerCount; // number of successful saveLayer calls |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1042 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 1043 | SkMetaData* fMetaData; |
| 1044 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 1045 | SkSurface_Base* fSurfaceBase; |
| 1046 | SkSurface_Base* getSurfaceBase() const { return fSurfaceBase; } |
| 1047 | void setSurfaceBase(SkSurface_Base* sb) { |
| 1048 | fSurfaceBase = sb; |
| 1049 | } |
| 1050 | friend class SkSurface_Base; |
junov@chromium.org | 45c3db8 | 2013-04-11 17:52:05 +0000 | [diff] [blame] | 1051 | friend class SkSurface_Gpu; |
skia.committer@gmail.com | fc84359 | 2012-10-11 02:01:14 +0000 | [diff] [blame] | 1052 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1053 | bool fDeviceCMDirty; // cleared by updateDeviceCMCache() |
| 1054 | void updateDeviceCMCache(); |
| 1055 | |
| 1056 | friend class SkDrawIter; // needs setupDrawForLayerDevice() |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1057 | friend class AutoDrawLooper; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1058 | |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1059 | SkDevice* createLayerDevice(SkBitmap::Config, int width, int height, |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1060 | bool isOpaque); |
| 1061 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1062 | SkDevice* init(SkDevice*); |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 1063 | |
| 1064 | // internal methods are not virtual, so they can safely be called by other |
| 1065 | // canvas apis, without confusing subclasses (like SkPictureRecording) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame^] | 1066 | void internalDrawBitmap(const SkBitmap&, const SkMatrix& m, const SkPaint* paint); |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1067 | void internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
reed@google.com | f0b5e11 | 2011-09-07 11:57:34 +0000 | [diff] [blame] | 1068 | const SkRect& dst, const SkPaint* paint); |
| 1069 | void internalDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 1070 | const SkRect& dst, const SkPaint* paint); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1071 | void internalDrawPaint(const SkPaint& paint); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1072 | int internalSaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 1073 | SaveFlags, bool justForImageFilter); |
| 1074 | void internalDrawDevice(SkDevice*, int x, int y, const SkPaint*); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1075 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1076 | // shared by save() and saveLayer() |
| 1077 | int internalSave(SaveFlags flags); |
| 1078 | void internalRestore(); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 1079 | static void DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 1080 | const SkRect& r, SkScalar textSize); |
| 1081 | static void DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 1082 | const char text[], size_t byteLength, |
| 1083 | SkScalar x, SkScalar y); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1084 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1085 | /* These maintain a cache of the clip bounds in local coordinates, |
| 1086 | (converted to 2s-compliment if floats are slow). |
| 1087 | */ |
| 1088 | mutable SkRectCompareType fLocalBoundsCompareType; |
| 1089 | mutable bool fLocalBoundsCompareTypeDirty; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 1090 | bool fAllowSoftClip; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1091 | bool fAllowSimplifyClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1092 | |
| 1093 | const SkRectCompareType& getLocalClipBoundsCompareType() const { |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1094 | if (fLocalBoundsCompareTypeDirty) { |
| 1095 | this->computeLocalClipBoundsCompareType(); |
| 1096 | fLocalBoundsCompareTypeDirty = false; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1097 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1098 | return fLocalBoundsCompareType; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1099 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1100 | void computeLocalClipBoundsCompareType() const; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1101 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1102 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1103 | class AutoValidateClip : ::SkNoncopyable { |
| 1104 | public: |
| 1105 | explicit AutoValidateClip(SkCanvas* canvas) : fCanvas(canvas) { |
| 1106 | fCanvas->validateClip(); |
| 1107 | } |
| 1108 | ~AutoValidateClip() { fCanvas->validateClip(); } |
| 1109 | |
| 1110 | private: |
| 1111 | const SkCanvas* fCanvas; |
| 1112 | }; |
| 1113 | |
| 1114 | #ifdef SK_DEBUG |
| 1115 | void validateClip() const; |
| 1116 | #else |
| 1117 | void validateClip() const {} |
| 1118 | #endif |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 1119 | |
| 1120 | typedef SkRefCnt INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1121 | }; |
| 1122 | |
| 1123 | /** Stack helper class to automatically call restoreToCount() on the canvas |
| 1124 | when this object goes out of scope. Use this to guarantee that the canvas |
| 1125 | is restored to a known state. |
| 1126 | */ |
| 1127 | class SkAutoCanvasRestore : SkNoncopyable { |
| 1128 | public: |
| 1129 | SkAutoCanvasRestore(SkCanvas* canvas, bool doSave) : fCanvas(canvas) { |
| 1130 | SkASSERT(canvas); |
| 1131 | fSaveCount = canvas->getSaveCount(); |
| 1132 | if (doSave) { |
| 1133 | canvas->save(); |
| 1134 | } |
| 1135 | } |
| 1136 | ~SkAutoCanvasRestore() { |
reed@google.com | f6c9a5b | 2012-11-20 15:12:21 +0000 | [diff] [blame] | 1137 | if (fCanvas) { |
| 1138 | fCanvas->restoreToCount(fSaveCount); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * Perform the restore now, instead of waiting for the destructor. Will |
| 1144 | * only do this once. |
| 1145 | */ |
| 1146 | void restore() { |
| 1147 | if (fCanvas) { |
| 1148 | fCanvas->restoreToCount(fSaveCount); |
| 1149 | fCanvas = NULL; |
| 1150 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | private: |
| 1154 | SkCanvas* fCanvas; |
| 1155 | int fSaveCount; |
| 1156 | }; |
| 1157 | |
robertphillips@google.com | 0a4805e | 2013-05-29 13:24:23 +0000 | [diff] [blame] | 1158 | /** Stack helper class to automatically open and close a comment block |
| 1159 | */ |
| 1160 | class SkAutoCommentBlock : SkNoncopyable { |
| 1161 | public: |
| 1162 | SkAutoCommentBlock(SkCanvas* canvas, const char* description) { |
| 1163 | fCanvas = canvas; |
| 1164 | if (NULL != fCanvas) { |
| 1165 | fCanvas->beginCommentGroup(description); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | ~SkAutoCommentBlock() { |
| 1170 | if (NULL != fCanvas) { |
| 1171 | fCanvas->endCommentGroup(); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | private: |
| 1176 | SkCanvas* fCanvas; |
| 1177 | }; |
| 1178 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1179 | #endif |