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