epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 11 | #include "SkBitmapDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkBounder.h" |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 13 | #include "SkDeviceImageFilterProxy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkDraw.h" |
| 15 | #include "SkDrawFilter.h" |
| 16 | #include "SkDrawLooper.h" |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 17 | #include "SkMetaData.h" |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 18 | #include "SkPathOps.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkPicture.h" |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 20 | #include "SkRasterClip.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 21 | #include "SkRRect.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 22 | #include "SkSurface_Base.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkTemplates.h" |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 24 | #include "SkTextFormatParams.h" |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 25 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 28 | #if SK_SUPPORT_GPU |
| 29 | #include "GrRenderTarget.h" |
| 30 | #endif |
| 31 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 32 | // experimental for faster tiled drawing... |
| 33 | //#define SK_ENABLE_CLIP_QUICKREJECT |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 34 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | //#define SK_TRACE_SAVERESTORE |
| 36 | |
| 37 | #ifdef SK_TRACE_SAVERESTORE |
| 38 | static int gLayerCounter; |
| 39 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 40 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 41 | |
| 42 | static int gRecCounter; |
| 43 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 44 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 45 | |
| 46 | static int gCanvasCounter; |
| 47 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 48 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 49 | #else |
| 50 | #define inc_layer() |
| 51 | #define dec_layer() |
| 52 | #define inc_rec() |
| 53 | #define dec_rec() |
| 54 | #define inc_canvas() |
| 55 | #define dec_canvas() |
| 56 | #endif |
| 57 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 58 | #ifdef SK_DEBUG |
| 59 | #include "SkPixelRef.h" |
| 60 | |
reed@google.com | f53d0a9 | 2013-01-30 13:17:32 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Some pixelref subclasses can support being "locked" from another thread |
| 63 | * during the lock-scope of skia calling them. In these instances, this balance |
| 64 | * check will fail, but may not be indicative of a problem, so we allow a build |
| 65 | * flag to disable this check. |
| 66 | * |
| 67 | * Potentially another fix would be to have a (debug-only) virtual or flag on |
| 68 | * pixelref, which could tell us at runtime if this check is valid. That would |
| 69 | * eliminate the need for this heavy-handed build check. |
| 70 | */ |
| 71 | #ifdef SK_DISABLE_PIXELREF_LOCKCOUNT_BALANCE_CHECK |
| 72 | class AutoCheckLockCountBalance { |
| 73 | public: |
| 74 | AutoCheckLockCountBalance(const SkBitmap&) { /* do nothing */ } |
| 75 | }; |
| 76 | #else |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 77 | class AutoCheckLockCountBalance { |
| 78 | public: |
| 79 | AutoCheckLockCountBalance(const SkBitmap& bm) : fPixelRef(bm.pixelRef()) { |
| 80 | fLockCount = fPixelRef ? fPixelRef->getLockCount() : 0; |
| 81 | } |
| 82 | ~AutoCheckLockCountBalance() { |
| 83 | const int count = fPixelRef ? fPixelRef->getLockCount() : 0; |
| 84 | SkASSERT(count == fLockCount); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | const SkPixelRef* fPixelRef; |
| 89 | int fLockCount; |
| 90 | }; |
reed@google.com | f53d0a9 | 2013-01-30 13:17:32 +0000 | [diff] [blame] | 91 | #endif |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 92 | |
| 93 | class AutoCheckNoSetContext { |
| 94 | public: |
| 95 | AutoCheckNoSetContext(const SkPaint& paint) : fPaint(paint) { |
| 96 | this->assertNoSetContext(fPaint); |
| 97 | } |
| 98 | ~AutoCheckNoSetContext() { |
| 99 | this->assertNoSetContext(fPaint); |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | const SkPaint& fPaint; |
| 104 | |
| 105 | void assertNoSetContext(const SkPaint& paint) { |
| 106 | SkShader* s = paint.getShader(); |
| 107 | if (s) { |
| 108 | SkASSERT(!s->setContextHasBeenCalled()); |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | #define CHECK_LOCKCOUNT_BALANCE(bitmap) AutoCheckLockCountBalance clcb(bitmap) |
| 114 | #define CHECK_SHADER_NOSETCONTEXT(paint) AutoCheckNoSetContext cshsc(paint) |
| 115 | |
| 116 | #else |
| 117 | #define CHECK_LOCKCOUNT_BALANCE(bitmap) |
| 118 | #define CHECK_SHADER_NOSETCONTEXT(paint) |
| 119 | #endif |
| 120 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 121 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 122 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 123 | void SkCanvas::predrawNotify() { |
| 124 | if (fSurfaceBase) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 125 | fSurfaceBase->aboutToDraw(SkSurface::kRetain_ContentChangeMode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 131 | /* This is the record we keep for each SkBaseDevice that the user installs. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 133 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 134 | these are used (assuming we're not on a layer) we rebuild these cache |
| 135 | values: they reflect the top of the save stack, but translated and clipped |
| 136 | by the device's XY offset and bitmap-bounds. |
| 137 | */ |
| 138 | struct DeviceCM { |
| 139 | DeviceCM* fNext; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 140 | SkBaseDevice* fDevice; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 141 | SkRasterClip fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | const SkMatrix* fMatrix; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 143 | SkPaint* fPaint; // may be null (in the future) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 145 | DeviceCM(SkBaseDevice* device, int x, int y, const SkPaint* paint, SkCanvas* canvas) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | : fNext(NULL) { |
| 147 | if (NULL != device) { |
| 148 | device->ref(); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 149 | device->onAttachToCanvas(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 151 | fDevice = device; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | fPaint = paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 153 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 155 | ~DeviceCM() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | if (NULL != fDevice) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 157 | fDevice->onDetachFromCanvas(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | fDevice->unref(); |
| 159 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 160 | SkDELETE(fPaint); |
| 161 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 162 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 163 | void updateMC(const SkMatrix& totalMatrix, const SkRasterClip& totalClip, |
| 164 | const SkClipStack& clipStack, SkRasterClip* updateClip) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 165 | int x = fDevice->getOrigin().x(); |
| 166 | int y = fDevice->getOrigin().y(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | int width = fDevice->width(); |
| 168 | int height = fDevice->height(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 169 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | if ((x | y) == 0) { |
| 171 | fMatrix = &totalMatrix; |
| 172 | fClip = totalClip; |
| 173 | } else { |
| 174 | fMatrixStorage = totalMatrix; |
| 175 | fMatrixStorage.postTranslate(SkIntToScalar(-x), |
| 176 | SkIntToScalar(-y)); |
| 177 | fMatrix = &fMatrixStorage; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 178 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | totalClip.translate(-x, -y, &fClip); |
| 180 | } |
| 181 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 182 | fClip.op(SkIRect::MakeWH(width, height), SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | |
| 184 | // intersect clip, but don't translate it (yet) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 185 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | if (updateClip) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 187 | updateClip->op(SkIRect::MakeXYWH(x, y, width, height), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | SkRegion::kDifference_Op); |
| 189 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 190 | |
robertphillips@google.com | 3fffb2e | 2012-10-09 22:30:18 +0000 | [diff] [blame] | 191 | fDevice->setMatrixClip(*fMatrix, fClip.forceGetBW(), clipStack); |
| 192 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | #ifdef SK_DEBUG |
| 194 | if (!fClip.isEmpty()) { |
| 195 | SkIRect deviceR; |
| 196 | deviceR.set(0, 0, width, height); |
| 197 | SkASSERT(deviceR.contains(fClip.getBounds())); |
| 198 | } |
| 199 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 200 | } |
| 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | private: |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 203 | SkMatrix fMatrixStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | /* This is the record we keep for each save/restore level in the stack. |
| 207 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 208 | for these fields. If the value is copied for this level, the copy is |
| 209 | stored in the ...Storage field, and the pointer points to that. If the |
| 210 | value is not copied for this level, we ignore ...Storage, and just point |
| 211 | at the corresponding value in the previous level in the stack. |
| 212 | */ |
| 213 | class SkCanvas::MCRec { |
| 214 | public: |
| 215 | MCRec* fNext; |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 216 | int fFlags; |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 217 | SkMatrix* fMatrix; // points to either fMatrixStorage or prev MCRec |
| 218 | SkRasterClip* fRasterClip; // points to either fRegionStorage or prev MCRec |
| 219 | SkDrawFilter* fFilter; // the current filter (or null) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | DeviceCM* fLayer; |
| 222 | /* If there are any layers in the stack, this points to the top-most |
| 223 | one that is at or below this level in the stack (so we know what |
| 224 | bitmap/device to draw into from this level. This value is NOT |
| 225 | reference counted, since the real owner is either our fLayer field, |
| 226 | or a previous one in a lower level.) |
| 227 | */ |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 228 | DeviceCM* fTopLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 230 | MCRec(const MCRec* prev, int flags) : fFlags(flags) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | if (NULL != prev) { |
| 232 | if (flags & SkCanvas::kMatrix_SaveFlag) { |
| 233 | fMatrixStorage = *prev->fMatrix; |
| 234 | fMatrix = &fMatrixStorage; |
| 235 | } else { |
| 236 | fMatrix = prev->fMatrix; |
| 237 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | if (flags & SkCanvas::kClip_SaveFlag) { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 240 | fRasterClipStorage = *prev->fRasterClip; |
| 241 | fRasterClip = &fRasterClipStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | } else { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 243 | fRasterClip = prev->fRasterClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | fFilter = prev->fFilter; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 247 | SkSafeRef(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | |
| 249 | fTopLayer = prev->fTopLayer; |
| 250 | } else { // no prev |
| 251 | fMatrixStorage.reset(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 252 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 253 | fMatrix = &fMatrixStorage; |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 254 | fRasterClip = &fRasterClipStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | fFilter = NULL; |
| 256 | fTopLayer = NULL; |
| 257 | } |
| 258 | fLayer = NULL; |
| 259 | |
| 260 | // don't bother initializing fNext |
| 261 | inc_rec(); |
| 262 | } |
| 263 | ~MCRec() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 264 | SkSafeUnref(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 265 | SkDELETE(fLayer); |
| 266 | dec_rec(); |
| 267 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 268 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | private: |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 270 | SkMatrix fMatrixStorage; |
| 271 | SkRasterClip fRasterClipStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | class SkDrawIter : public SkDraw { |
| 275 | public: |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 276 | SkDrawIter(SkCanvas* canvas, bool skipEmptyClips = true) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 277 | canvas = canvas->canvasForDrawIter(); |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 278 | fCanvas = canvas; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | canvas->updateDeviceCMCache(); |
| 280 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 281 | fClipStack = &canvas->fClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | fBounder = canvas->getBounder(); |
| 283 | fCurrLayer = canvas->fMCRec->fTopLayer; |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 284 | fSkipEmptyClips = skipEmptyClips; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 285 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 286 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 287 | bool next() { |
| 288 | // skip over recs with empty clips |
| 289 | if (fSkipEmptyClips) { |
| 290 | while (fCurrLayer && fCurrLayer->fClip.isEmpty()) { |
| 291 | fCurrLayer = fCurrLayer->fNext; |
| 292 | } |
| 293 | } |
| 294 | |
reed@google.com | f68c5e2 | 2012-02-24 16:38:58 +0000 | [diff] [blame] | 295 | const DeviceCM* rec = fCurrLayer; |
| 296 | if (rec && rec->fDevice) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | |
| 298 | fMatrix = rec->fMatrix; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 299 | fClip = &((SkRasterClip*)&rec->fClip)->forceGetBW(); |
| 300 | fRC = &rec->fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 301 | fDevice = rec->fDevice; |
| 302 | fBitmap = &fDevice->accessBitmap(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | fPaint = rec->fPaint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 304 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | |
| 306 | fCurrLayer = rec->fNext; |
| 307 | if (fBounder) { |
| 308 | fBounder->setClip(fClip); |
| 309 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 310 | // fCurrLayer may be NULL now |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 311 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 316 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 317 | SkBaseDevice* getDevice() const { return fDevice; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 318 | int getX() const { return fDevice->getOrigin().x(); } |
| 319 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | const SkMatrix& getMatrix() const { return *fMatrix; } |
| 321 | const SkRegion& getClip() const { return *fClip; } |
| 322 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 323 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | private: |
| 325 | SkCanvas* fCanvas; |
| 326 | const DeviceCM* fCurrLayer; |
| 327 | const SkPaint* fPaint; // May be null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | SkBool8 fSkipEmptyClips; |
| 329 | |
| 330 | typedef SkDraw INHERITED; |
| 331 | }; |
| 332 | |
| 333 | ///////////////////////////////////////////////////////////////////////////// |
| 334 | |
| 335 | class AutoDrawLooper { |
| 336 | public: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 337 | AutoDrawLooper(SkCanvas* canvas, const SkPaint& paint, |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 338 | bool skipLayerForImageFilter = false, |
| 339 | const SkRect* bounds = NULL) : fOrigPaint(paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 340 | fCanvas = canvas; |
| 341 | fLooper = paint.getLooper(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | fFilter = canvas->getDrawFilter(); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 343 | fPaint = NULL; |
| 344 | fSaveCount = canvas->getSaveCount(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 345 | fDoClearImageFilter = false; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 346 | fDone = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 348 | if (!skipLayerForImageFilter && fOrigPaint.getImageFilter()) { |
| 349 | SkPaint tmp; |
| 350 | tmp.setImageFilter(fOrigPaint.getImageFilter()); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 351 | (void)canvas->internalSaveLayer(bounds, &tmp, |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 352 | SkCanvas::kARGB_ClipLayer_SaveFlag, true); |
| 353 | // we'll clear the imageFilter for the actual draws in next(), so |
| 354 | // it will only be applied during the restore(). |
| 355 | fDoClearImageFilter = true; |
| 356 | } |
| 357 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 358 | if (fLooper) { |
| 359 | fLooper->init(canvas); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 360 | fIsSimple = false; |
| 361 | } else { |
| 362 | // can we be marked as simple? |
| 363 | fIsSimple = !fFilter && !fDoClearImageFilter; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 366 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | ~AutoDrawLooper() { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 368 | if (fDoClearImageFilter) { |
| 369 | fCanvas->internalRestore(); |
| 370 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 371 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 373 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 374 | const SkPaint& paint() const { |
| 375 | SkASSERT(fPaint); |
| 376 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 378 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 379 | bool next(SkDrawFilter::Type drawType) { |
| 380 | if (fDone) { |
| 381 | return false; |
| 382 | } else if (fIsSimple) { |
| 383 | fDone = true; |
| 384 | fPaint = &fOrigPaint; |
| 385 | return !fPaint->nothingToDraw(); |
| 386 | } else { |
| 387 | return this->doNext(drawType); |
| 388 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 389 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 390 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 391 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 392 | SkLazyPaint fLazyPaint; |
| 393 | SkCanvas* fCanvas; |
| 394 | const SkPaint& fOrigPaint; |
| 395 | SkDrawLooper* fLooper; |
| 396 | SkDrawFilter* fFilter; |
| 397 | const SkPaint* fPaint; |
| 398 | int fSaveCount; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 399 | bool fDoClearImageFilter; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 400 | bool fDone; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 401 | bool fIsSimple; |
| 402 | |
| 403 | bool doNext(SkDrawFilter::Type drawType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 406 | bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) { |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 407 | fPaint = NULL; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 408 | SkASSERT(!fIsSimple); |
| 409 | SkASSERT(fLooper || fFilter || fDoClearImageFilter); |
| 410 | |
| 411 | SkPaint* paint = fLazyPaint.set(fOrigPaint); |
| 412 | |
| 413 | if (fDoClearImageFilter) { |
| 414 | paint->setImageFilter(NULL); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 415 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 416 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 417 | if (fLooper && !fLooper->next(fCanvas, paint)) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 418 | fDone = true; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | if (fFilter) { |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 422 | if (!fFilter->filter(paint, drawType)) { |
| 423 | fDone = true; |
| 424 | return false; |
| 425 | } |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 426 | if (NULL == fLooper) { |
| 427 | // no looper means we only draw once |
| 428 | fDone = true; |
| 429 | } |
| 430 | } |
| 431 | fPaint = paint; |
| 432 | |
| 433 | // if we only came in here for the imagefilter, mark us as done |
| 434 | if (!fLooper && !fFilter) { |
| 435 | fDone = true; |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // call this after any possible paint modifiers |
| 439 | if (fPaint->nothingToDraw()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 440 | fPaint = NULL; |
| 441 | return false; |
| 442 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | /* Stack helper for managing a SkBounder. In the destructor, if we were |
| 447 | given a bounder, we call its commit() method, signifying that we are |
| 448 | done accumulating bounds for that draw. |
| 449 | */ |
| 450 | class SkAutoBounderCommit { |
| 451 | public: |
| 452 | SkAutoBounderCommit(SkBounder* bounder) : fBounder(bounder) {} |
| 453 | ~SkAutoBounderCommit() { |
| 454 | if (NULL != fBounder) { |
| 455 | fBounder->commit(); |
| 456 | } |
| 457 | } |
| 458 | private: |
| 459 | SkBounder* fBounder; |
| 460 | }; |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 461 | #define SkAutoBounderCommit(...) SK_REQUIRE_LOCAL_VAR(SkAutoBounderCommit) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | |
| 463 | #include "SkColorPriv.h" |
| 464 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | ////////// macros to place around the internal draw calls ////////////////// |
| 466 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 467 | #define LOOPER_BEGIN_DRAWDEVICE(paint, type) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 468 | this->predrawNotify(); \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 469 | AutoDrawLooper looper(this, paint, true); \ |
| 470 | while (looper.next(type)) { \ |
| 471 | SkAutoBounderCommit ac(fBounder); \ |
| 472 | SkDrawIter iter(this); |
| 473 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 474 | #define LOOPER_BEGIN(paint, type, bounds) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 475 | this->predrawNotify(); \ |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 476 | AutoDrawLooper looper(this, paint, false, bounds); \ |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 477 | while (looper.next(type)) { \ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 478 | SkAutoBounderCommit ac(fBounder); \ |
| 479 | SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 480 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 481 | #define LOOPER_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | |
| 483 | //////////////////////////////////////////////////////////////////////////// |
| 484 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 485 | SkBaseDevice* SkCanvas::init(SkBaseDevice* device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 486 | fBounder = NULL; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 487 | fCachedLocalClipBounds.setEmpty(); |
| 488 | fCachedLocalClipBoundsDirty = true; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 489 | fAllowSoftClip = true; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 490 | fAllowSimplifyClip = false; |
agl@chromium.org | 447bcfa | 2009-12-12 00:06:17 +0000 | [diff] [blame] | 491 | fDeviceCMDirty = false; |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 492 | fSaveLayerCount = 0; |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 493 | fMetaData = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | |
| 495 | fMCRec = (MCRec*)fMCStack.push_back(); |
| 496 | new (fMCRec) MCRec(NULL, 0); |
| 497 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 498 | fMCRec->fLayer = SkNEW_ARGS(DeviceCM, (NULL, 0, 0, NULL, NULL)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | fMCRec->fTopLayer = fMCRec->fLayer; |
| 500 | fMCRec->fNext = NULL; |
| 501 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 502 | fSurfaceBase = NULL; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 503 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 504 | return this->setDevice(device); |
| 505 | } |
| 506 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 507 | SkCanvas::SkCanvas() |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 508 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 509 | { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 510 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 511 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 512 | this->init(NULL); |
| 513 | } |
| 514 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 515 | SkCanvas::SkCanvas(int width, int height) |
| 516 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 517 | { |
| 518 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 519 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 520 | SkBitmap bitmap; |
| 521 | bitmap.setConfig(SkBitmap::kNo_Config, width, height); |
| 522 | this->init(SkNEW_ARGS(SkBitmapDevice, (bitmap)))->unref(); |
| 523 | } |
| 524 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 525 | SkCanvas::SkCanvas(SkBaseDevice* device) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 526 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 527 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | inc_canvas(); |
| 529 | |
| 530 | this->init(device); |
| 531 | } |
| 532 | |
| 533 | SkCanvas::SkCanvas(const SkBitmap& bitmap) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 534 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 535 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | inc_canvas(); |
| 537 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 538 | this->init(SkNEW_ARGS(SkBitmapDevice, (bitmap)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | SkCanvas::~SkCanvas() { |
| 542 | // free up the contents of our deque |
| 543 | this->restoreToCount(1); // restore everything but the last |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 544 | SkASSERT(0 == fSaveLayerCount); |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 545 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 546 | this->internalRestore(); // restore the last, since we're going away |
| 547 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 548 | SkSafeUnref(fBounder); |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 549 | SkDELETE(fMetaData); |
vandebo@chromium.org | b70ae31 | 2010-10-15 18:58:19 +0000 | [diff] [blame] | 550 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | dec_canvas(); |
| 552 | } |
| 553 | |
| 554 | SkBounder* SkCanvas::setBounder(SkBounder* bounder) { |
| 555 | SkRefCnt_SafeAssign(fBounder, bounder); |
| 556 | return bounder; |
| 557 | } |
| 558 | |
| 559 | SkDrawFilter* SkCanvas::getDrawFilter() const { |
| 560 | return fMCRec->fFilter; |
| 561 | } |
| 562 | |
| 563 | SkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) { |
| 564 | SkRefCnt_SafeAssign(fMCRec->fFilter, filter); |
| 565 | return filter; |
| 566 | } |
| 567 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 568 | SkMetaData& SkCanvas::getMetaData() { |
| 569 | // metadata users are rare, so we lazily allocate it. If that changes we |
| 570 | // can decide to just make it a field in the device (rather than a ptr) |
| 571 | if (NULL == fMetaData) { |
| 572 | fMetaData = new SkMetaData; |
| 573 | } |
| 574 | return *fMetaData; |
| 575 | } |
| 576 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 577 | /////////////////////////////////////////////////////////////////////////////// |
| 578 | |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 579 | void SkCanvas::flush() { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 580 | SkBaseDevice* device = this->getDevice(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 581 | if (device) { |
| 582 | device->flush(); |
| 583 | } |
| 584 | } |
| 585 | |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 586 | SkISize SkCanvas::getDeviceSize() const { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 587 | SkBaseDevice* d = this->getDevice(); |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 588 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 589 | } |
| 590 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 591 | SkBaseDevice* SkCanvas::getDevice() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 592 | // return root device |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 593 | MCRec* rec = (MCRec*) fMCStack.front(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 594 | SkASSERT(rec && rec->fLayer); |
| 595 | return rec->fLayer->fDevice; |
| 596 | } |
| 597 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 598 | SkBaseDevice* SkCanvas::getTopDevice(bool updateMatrixClip) const { |
reed@google.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 599 | if (updateMatrixClip) { |
| 600 | const_cast<SkCanvas*>(this)->updateDeviceCMCache(); |
| 601 | } |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 602 | return fMCRec->fTopLayer->fDevice; |
| 603 | } |
| 604 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 605 | SkBaseDevice* SkCanvas::setDevice(SkBaseDevice* device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | // return root device |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 607 | SkDeque::F2BIter iter(fMCStack); |
| 608 | MCRec* rec = (MCRec*)iter.next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 609 | SkASSERT(rec && rec->fLayer); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 610 | SkBaseDevice* rootDevice = rec->fLayer->fDevice; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 611 | |
| 612 | if (rootDevice == device) { |
| 613 | return device; |
| 614 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 615 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 616 | if (device) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 617 | device->onAttachToCanvas(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 618 | } |
| 619 | if (rootDevice) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 620 | rootDevice->onDetachFromCanvas(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | SkRefCnt_SafeAssign(rec->fLayer->fDevice, device); |
| 624 | rootDevice = device; |
| 625 | |
| 626 | fDeviceCMDirty = true; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 627 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 628 | /* Now we update our initial region to have the bounds of the new device, |
| 629 | and then intersect all of the clips in our stack with these bounds, |
| 630 | to ensure that we can't draw outside of the device's bounds (and trash |
| 631 | memory). |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 632 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 633 | NOTE: this is only a partial-fix, since if the new device is larger than |
| 634 | the previous one, we don't know how to "enlarge" the clips in our stack, |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 635 | so drawing may be artificially restricted. Without keeping a history of |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 636 | all calls to canvas->clipRect() and canvas->clipPath(), we can't exactly |
| 637 | reconstruct the correct clips, so this approximation will have to do. |
| 638 | The caller really needs to restore() back to the base if they want to |
| 639 | accurately take advantage of the new device bounds. |
| 640 | */ |
| 641 | |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 642 | SkIRect bounds; |
| 643 | if (device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 644 | bounds.set(0, 0, device->width(), device->height()); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 645 | } else { |
| 646 | bounds.setEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 647 | } |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 648 | // now jam our 1st clip to be bounds, and intersect the rest with that |
| 649 | rec->fRasterClip->setRect(bounds); |
| 650 | while ((rec = (MCRec*)iter.next()) != NULL) { |
| 651 | (void)rec->fRasterClip->op(bounds, SkRegion::kIntersect_Op); |
| 652 | } |
| 653 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 654 | return device; |
| 655 | } |
| 656 | |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 657 | bool SkCanvas::readPixels(SkBitmap* bitmap, |
| 658 | int x, int y, |
| 659 | Config8888 config8888) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 660 | SkBaseDevice* device = this->getDevice(); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 661 | if (!device) { |
| 662 | return false; |
| 663 | } |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 664 | return device->readPixels(bitmap, x, y, config8888); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 665 | } |
| 666 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 667 | bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 668 | SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 669 | if (!device) { |
| 670 | return false; |
| 671 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 672 | |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 673 | SkIRect bounds; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 674 | bounds.set(0, 0, device->width(), device->height()); |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 675 | if (!bounds.intersect(srcRect)) { |
| 676 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | SkBitmap tmp; |
| 680 | tmp.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), |
| 681 | bounds.height()); |
| 682 | if (this->readPixels(&tmp, bounds.fLeft, bounds.fTop)) { |
| 683 | bitmap->swap(tmp); |
| 684 | return true; |
| 685 | } else { |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 686 | return false; |
| 687 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 688 | } |
| 689 | |
bsalomon@google.com | d58a1cd | 2011-11-10 20:57:43 +0000 | [diff] [blame] | 690 | void SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y, |
| 691 | Config8888 config8888) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 692 | SkBaseDevice* device = this->getDevice(); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 693 | if (device) { |
bsalomon@google.com | 405d0f4 | 2012-08-29 21:26:13 +0000 | [diff] [blame] | 694 | if (SkIRect::Intersects(SkIRect::MakeSize(this->getDeviceSize()), |
| 695 | SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()))) { |
| 696 | device->accessBitmap(true); |
| 697 | device->writePixels(bitmap, x, y, config8888); |
| 698 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 702 | SkCanvas* SkCanvas::canvasForDrawIter() { |
| 703 | return this; |
| 704 | } |
| 705 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 706 | ////////////////////////////////////////////////////////////////////////////// |
| 707 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 708 | void SkCanvas::updateDeviceCMCache() { |
| 709 | if (fDeviceCMDirty) { |
| 710 | const SkMatrix& totalMatrix = this->getTotalMatrix(); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 711 | const SkRasterClip& totalClip = *fMCRec->fRasterClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 712 | DeviceCM* layer = fMCRec->fTopLayer; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 713 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 714 | if (NULL == layer->fNext) { // only one layer |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 715 | layer->updateMC(totalMatrix, totalClip, fClipStack, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 716 | } else { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 717 | SkRasterClip clip(totalClip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 718 | do { |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 719 | layer->updateMC(totalMatrix, clip, fClipStack, &clip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 720 | } while ((layer = layer->fNext) != NULL); |
| 721 | } |
| 722 | fDeviceCMDirty = false; |
| 723 | } |
| 724 | } |
| 725 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 726 | /////////////////////////////////////////////////////////////////////////////// |
| 727 | |
| 728 | int SkCanvas::internalSave(SaveFlags flags) { |
| 729 | int saveCount = this->getSaveCount(); // record this before the actual save |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 730 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
| 732 | new (newTop) MCRec(fMCRec, flags); // balanced in restore() |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 733 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 734 | newTop->fNext = fMCRec; |
| 735 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 736 | |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 737 | if (SkCanvas::kClip_SaveFlag & flags) { |
| 738 | fClipStack.save(); |
| 739 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 740 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 741 | return saveCount; |
| 742 | } |
| 743 | |
| 744 | int SkCanvas::save(SaveFlags flags) { |
| 745 | // call shared impl |
| 746 | return this->internalSave(flags); |
| 747 | } |
| 748 | |
| 749 | #define C32MASK (1 << SkBitmap::kARGB_8888_Config) |
| 750 | #define C16MASK (1 << SkBitmap::kRGB_565_Config) |
| 751 | #define C8MASK (1 << SkBitmap::kA8_Config) |
| 752 | |
| 753 | static SkBitmap::Config resolve_config(SkCanvas* canvas, |
| 754 | const SkIRect& bounds, |
| 755 | SkCanvas::SaveFlags flags, |
| 756 | bool* isOpaque) { |
| 757 | *isOpaque = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) == 0; |
| 758 | |
| 759 | #if 0 |
| 760 | // loop through and union all the configs we may draw into |
| 761 | uint32_t configMask = 0; |
| 762 | for (int i = canvas->countLayerDevices() - 1; i >= 0; --i) |
| 763 | { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 764 | SkBaseDevice* device = canvas->getLayerDevice(i); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 765 | if (device->intersects(bounds)) |
| 766 | configMask |= 1 << device->config(); |
| 767 | } |
| 768 | |
| 769 | // if the caller wants alpha or fullcolor, we can't return 565 |
| 770 | if (flags & (SkCanvas::kFullColorLayer_SaveFlag | |
| 771 | SkCanvas::kHasAlphaLayer_SaveFlag)) |
| 772 | configMask &= ~C16MASK; |
| 773 | |
| 774 | switch (configMask) { |
| 775 | case C8MASK: // if we only have A8, return that |
| 776 | return SkBitmap::kA8_Config; |
| 777 | |
| 778 | case C16MASK: // if we only have 565, return that |
| 779 | return SkBitmap::kRGB_565_Config; |
| 780 | |
| 781 | default: |
| 782 | return SkBitmap::kARGB_8888_Config; // default answer |
| 783 | } |
| 784 | #else |
| 785 | return SkBitmap::kARGB_8888_Config; // default answer |
| 786 | #endif |
| 787 | } |
| 788 | |
| 789 | static bool bounds_affects_clip(SkCanvas::SaveFlags flags) { |
| 790 | return (flags & SkCanvas::kClipToLayer_SaveFlag) != 0; |
| 791 | } |
| 792 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 793 | bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveFlags flags, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 794 | SkIRect* intersection, const SkImageFilter* imageFilter) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 795 | SkIRect clipBounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 796 | SkRegion::Op op = SkRegion::kIntersect_Op; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 797 | if (!this->getClipDeviceBounds(&clipBounds)) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 798 | return false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 799 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 800 | |
| 801 | if (imageFilter) { |
| 802 | imageFilter->filterBounds(clipBounds, *fMCRec->fMatrix, &clipBounds); |
| 803 | // Filters may grow the bounds beyond the device bounds. |
| 804 | op = SkRegion::kReplace_Op; |
| 805 | } |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 806 | SkIRect ir; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 807 | if (NULL != bounds) { |
| 808 | SkRect r; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 809 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 810 | this->getTotalMatrix().mapRect(&r, *bounds); |
| 811 | r.roundOut(&ir); |
| 812 | // early exit if the layer's bounds are clipped out |
| 813 | if (!ir.intersect(clipBounds)) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 814 | if (bounds_affects_clip(flags)) { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 815 | fMCRec->fRasterClip->setEmpty(); |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 816 | } |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 817 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | } |
| 819 | } else { // no user bounds, so just use the clip |
| 820 | ir = clipBounds; |
| 821 | } |
| 822 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 823 | fClipStack.clipDevRect(ir, op); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 824 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 825 | // early exit if the clip is now empty |
| 826 | if (bounds_affects_clip(flags) && |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 827 | !fMCRec->fRasterClip->op(ir, op)) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | |
| 831 | if (intersection) { |
| 832 | *intersection = ir; |
| 833 | } |
| 834 | return true; |
| 835 | } |
| 836 | |
| 837 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 838 | SaveFlags flags) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 839 | return this->internalSaveLayer(bounds, paint, flags, false); |
| 840 | } |
| 841 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 842 | static SkBaseDevice* createCompatibleDevice(SkCanvas* canvas, |
| 843 | SkBitmap::Config config, |
| 844 | int width, int height, |
| 845 | bool isOpaque) { |
| 846 | SkBaseDevice* device = canvas->getDevice(); |
| 847 | if (device) { |
| 848 | return device->createCompatibleDevice(config, width, height, isOpaque); |
| 849 | } else { |
| 850 | return NULL; |
| 851 | } |
| 852 | } |
| 853 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 854 | int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 855 | SaveFlags flags, bool justForImageFilter) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 856 | // do this before we create the layer. We don't call the public save() since |
| 857 | // that would invoke a possibly overridden virtual |
| 858 | int count = this->internalSave(flags); |
| 859 | |
| 860 | fDeviceCMDirty = true; |
| 861 | |
| 862 | SkIRect ir; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 863 | if (!this->clipRectBounds(bounds, flags, &ir, paint ? paint->getImageFilter() : NULL)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 864 | return count; |
| 865 | } |
| 866 | |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 867 | // Kill the imagefilter if our device doesn't allow it |
| 868 | SkLazyPaint lazyP; |
| 869 | if (paint && paint->getImageFilter()) { |
| 870 | if (!this->getTopDevice()->allowImageFilter(paint->getImageFilter())) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 871 | if (justForImageFilter) { |
| 872 | // early exit if the layer was just for the imageFilter |
| 873 | return count; |
| 874 | } |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 875 | SkPaint* p = lazyP.set(*paint); |
| 876 | p->setImageFilter(NULL); |
| 877 | paint = p; |
| 878 | } |
| 879 | } |
| 880 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 881 | bool isOpaque; |
| 882 | SkBitmap::Config config = resolve_config(this, ir, flags, &isOpaque); |
| 883 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 884 | SkBaseDevice* device; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 885 | if (paint && paint->getImageFilter()) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 886 | device = createCompatibleDevice(this, config, ir.width(), ir.height(), |
| 887 | isOpaque); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 888 | } else { |
| 889 | device = this->createLayerDevice(config, ir.width(), ir.height(), |
| 890 | isOpaque); |
| 891 | } |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 892 | if (NULL == device) { |
| 893 | SkDebugf("Unable to create device for layer."); |
| 894 | return count; |
| 895 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 896 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 897 | device->setOrigin(ir.fLeft, ir.fTop); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 898 | DeviceCM* layer = SkNEW_ARGS(DeviceCM, (device, ir.fLeft, ir.fTop, paint, this)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 899 | device->unref(); |
| 900 | |
| 901 | layer->fNext = fMCRec->fTopLayer; |
| 902 | fMCRec->fLayer = layer; |
| 903 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
| 904 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 905 | fSaveLayerCount += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 906 | return count; |
| 907 | } |
| 908 | |
| 909 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha, |
| 910 | SaveFlags flags) { |
| 911 | if (0xFF == alpha) { |
| 912 | return this->saveLayer(bounds, NULL, flags); |
| 913 | } else { |
| 914 | SkPaint tmpPaint; |
| 915 | tmpPaint.setAlpha(alpha); |
| 916 | return this->saveLayer(bounds, &tmpPaint, flags); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | void SkCanvas::restore() { |
| 921 | // check for underflow |
| 922 | if (fMCStack.count() > 1) { |
| 923 | this->internalRestore(); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | void SkCanvas::internalRestore() { |
| 928 | SkASSERT(fMCStack.count() != 0); |
| 929 | |
| 930 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 931 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 932 | |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 933 | if (SkCanvas::kClip_SaveFlag & fMCRec->fFlags) { |
| 934 | fClipStack.restore(); |
| 935 | } |
| 936 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 937 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 938 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 939 | // now detach it from fMCRec so we can pop(). Gets freed after its drawn |
| 940 | fMCRec->fLayer = NULL; |
| 941 | |
| 942 | // now do the normal restore() |
| 943 | fMCRec->~MCRec(); // balanced in save() |
| 944 | fMCStack.pop_back(); |
| 945 | fMCRec = (MCRec*)fMCStack.back(); |
| 946 | |
| 947 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 948 | since if we're being recorded, we don't want to record this (the |
| 949 | recorder will have already recorded the restore). |
| 950 | */ |
| 951 | if (NULL != layer) { |
| 952 | if (layer->fNext) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 953 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 954 | this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(), |
| 955 | layer->fPaint); |
| 956 | // reset this, since internalDrawDevice will have set it to true |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 957 | fDeviceCMDirty = true; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 958 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 959 | SkASSERT(fSaveLayerCount > 0); |
| 960 | fSaveLayerCount -= 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 961 | } |
| 962 | SkDELETE(layer); |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 963 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | int SkCanvas::getSaveCount() const { |
| 967 | return fMCStack.count(); |
| 968 | } |
| 969 | |
| 970 | void SkCanvas::restoreToCount(int count) { |
| 971 | // sanity check |
| 972 | if (count < 1) { |
| 973 | count = 1; |
| 974 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 975 | |
reed@google.com | b9d1c6a | 2011-11-28 16:09:24 +0000 | [diff] [blame] | 976 | int n = this->getSaveCount() - count; |
| 977 | for (int i = 0; i < n; ++i) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 978 | this->restore(); |
| 979 | } |
| 980 | } |
| 981 | |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 982 | bool SkCanvas::isDrawingToLayer() const { |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 983 | return fSaveLayerCount > 0; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 984 | } |
| 985 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 986 | SkSurface* SkCanvas::newSurface(const SkImageInfo& info) { |
| 987 | return this->onNewSurface(info); |
| 988 | } |
| 989 | |
| 990 | SkSurface* SkCanvas::onNewSurface(const SkImageInfo& info) { |
| 991 | SkBaseDevice* dev = this->getDevice(); |
| 992 | return dev ? dev->newSurface(info) : NULL; |
| 993 | } |
| 994 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 995 | ///////////////////////////////////////////////////////////////////////////// |
| 996 | |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 997 | void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 998 | const SkMatrix& matrix, const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame^] | 999 | if (bitmap.drawsNothing()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1003 | SkLazyPaint lazy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1004 | if (NULL == paint) { |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1005 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1006 | } |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1007 | |
| 1008 | SkDEBUGCODE(bitmap.validate();) |
| 1009 | CHECK_LOCKCOUNT_BALANCE(bitmap); |
| 1010 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1011 | SkRect storage; |
| 1012 | const SkRect* bounds = NULL; |
| 1013 | if (paint && paint->canComputeFastBounds()) { |
| 1014 | bitmap.getBounds(&storage); |
| 1015 | matrix.mapRect(&storage); |
| 1016 | bounds = &paint->computeFastBounds(storage, &storage); |
| 1017 | } |
| 1018 | |
| 1019 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1020 | |
| 1021 | while (iter.next()) { |
| 1022 | iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint()); |
| 1023 | } |
| 1024 | |
| 1025 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1028 | void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1029 | const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1030 | SkPaint tmp; |
| 1031 | if (NULL == paint) { |
| 1032 | tmp.setDither(true); |
| 1033 | paint = &tmp; |
| 1034 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1035 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1036 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1037 | while (iter.next()) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1038 | SkBaseDevice* dstDev = iter.fDevice; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1039 | paint = &looper.paint(); |
| 1040 | SkImageFilter* filter = paint->getImageFilter(); |
| 1041 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1042 | if (filter && !dstDev->canHandleImageFilter(filter)) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1043 | SkDeviceImageFilterProxy proxy(dstDev); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1044 | SkBitmap dst; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1045 | SkIPoint offset = SkIPoint::Make(0, 0); |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1046 | const SkBitmap& src = srcDev->accessBitmap(false); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1047 | SkMatrix matrix = *iter.fMatrix; |
| 1048 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1049 | if (filter->filterImage(&proxy, src, matrix, &dst, &offset)) { |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1050 | SkPaint tmpUnfiltered(*paint); |
| 1051 | tmpUnfiltered.setImageFilter(NULL); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1052 | dstDev->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), |
| 1053 | tmpUnfiltered); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } else { |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1056 | dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1057 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1058 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1059 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1062 | void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, |
| 1063 | const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame^] | 1064 | if (bitmap.drawsNothing()) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame^] | 1067 | SkDEBUGCODE(bitmap.validate();) |
| 1068 | CHECK_LOCKCOUNT_BALANCE(bitmap); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1069 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1070 | SkPaint tmp; |
| 1071 | if (NULL == paint) { |
| 1072 | paint = &tmp; |
| 1073 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1074 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1075 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1076 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1077 | while (iter.next()) { |
| 1078 | paint = &looper.paint(); |
| 1079 | SkImageFilter* filter = paint->getImageFilter(); |
| 1080 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
| 1081 | if (filter && !iter.fDevice->canHandleImageFilter(filter)) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1082 | SkDeviceImageFilterProxy proxy(iter.fDevice); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1083 | SkBitmap dst; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1084 | SkIPoint offset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1085 | SkMatrix matrix = *iter.fMatrix; |
| 1086 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1087 | if (filter->filterImage(&proxy, bitmap, matrix, &dst, &offset)) { |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1088 | SkPaint tmpUnfiltered(*paint); |
| 1089 | tmpUnfiltered.setImageFilter(NULL); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1090 | iter.fDevice->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1091 | tmpUnfiltered); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } else { |
| 1094 | iter.fDevice->drawSprite(iter, bitmap, pos.x(), pos.y(), *paint); |
| 1095 | } |
| 1096 | } |
| 1097 | LOOPER_END |
| 1098 | } |
| 1099 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1100 | ///////////////////////////////////////////////////////////////////////////// |
| 1101 | |
| 1102 | bool SkCanvas::translate(SkScalar dx, SkScalar dy) { |
| 1103 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1104 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1105 | return fMCRec->fMatrix->preTranslate(dx, dy); |
| 1106 | } |
| 1107 | |
| 1108 | bool SkCanvas::scale(SkScalar sx, SkScalar sy) { |
| 1109 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1110 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1111 | return fMCRec->fMatrix->preScale(sx, sy); |
| 1112 | } |
| 1113 | |
| 1114 | bool SkCanvas::rotate(SkScalar degrees) { |
| 1115 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1116 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1117 | return fMCRec->fMatrix->preRotate(degrees); |
| 1118 | } |
| 1119 | |
| 1120 | bool SkCanvas::skew(SkScalar sx, SkScalar sy) { |
| 1121 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1122 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1123 | return fMCRec->fMatrix->preSkew(sx, sy); |
| 1124 | } |
| 1125 | |
| 1126 | bool SkCanvas::concat(const SkMatrix& matrix) { |
| 1127 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1128 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1129 | return fMCRec->fMatrix->preConcat(matrix); |
| 1130 | } |
| 1131 | |
| 1132 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 1133 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1134 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1135 | *fMCRec->fMatrix = matrix; |
| 1136 | } |
| 1137 | |
| 1138 | // this is not virtual, so it must call a virtual method so that subclasses |
| 1139 | // will see its action |
| 1140 | void SkCanvas::resetMatrix() { |
| 1141 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1143 | matrix.reset(); |
| 1144 | this->setMatrix(matrix); |
| 1145 | } |
| 1146 | |
| 1147 | ////////////////////////////////////////////////////////////////////////////// |
| 1148 | |
reed@google.com | c42d35d | 2011-10-12 11:57:42 +0000 | [diff] [blame] | 1149 | bool SkCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1150 | #ifdef SK_ENABLE_CLIP_QUICKREJECT |
| 1151 | if (SkRegion::kIntersect_Op == op) { |
| 1152 | if (fMCRec->fRasterClip->isEmpty()) { |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1156 | if (this->quickReject(rect)) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1157 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1158 | fCachedLocalClipBoundsDirty = true; |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1159 | |
| 1160 | fClipStack.clipEmpty(); |
| 1161 | return fMCRec->fRasterClip->setEmpty(); |
| 1162 | } |
| 1163 | } |
| 1164 | #endif |
| 1165 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1166 | AutoValidateClip avc(this); |
| 1167 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1168 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1169 | fCachedLocalClipBoundsDirty = true; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 1170 | doAA &= fAllowSoftClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1171 | |
| 1172 | if (fMCRec->fMatrix->rectStaysRect()) { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1173 | // for these simpler matrices, we can stay a rect even after applying |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 1174 | // the matrix. This means we don't have to a) make a path, and b) tell |
| 1175 | // the region code to scan-convert the path, only to discover that it |
| 1176 | // is really just a rect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1177 | SkRect r; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1178 | |
| 1179 | fMCRec->fMatrix->mapRect(&r, rect); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1180 | fClipStack.clipDevRect(r, op, doAA); |
| 1181 | return fMCRec->fRasterClip->op(r, op, doAA); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1182 | } else { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1183 | // since we're rotated or some such thing, we convert the rect to a path |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 1184 | // and clip against that, since it can handle any matrix. However, to |
| 1185 | // avoid recursion in the case where we are subclassed (e.g. Pictures) |
| 1186 | // we explicitly call "our" version of clipPath. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1187 | SkPath path; |
| 1188 | |
| 1189 | path.addRect(rect); |
reed@google.com | c42d35d | 2011-10-12 11:57:42 +0000 | [diff] [blame] | 1190 | return this->SkCanvas::clipPath(path, op, doAA); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1194 | static bool clipPathHelper(const SkCanvas* canvas, SkRasterClip* currClip, |
reed@google.com | c42d35d | 2011-10-12 11:57:42 +0000 | [diff] [blame] | 1195 | const SkPath& devPath, SkRegion::Op op, bool doAA) { |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1196 | // base is used to limit the size (and therefore memory allocation) of the |
| 1197 | // region that results from scan converting devPath. |
| 1198 | SkRegion base; |
| 1199 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1200 | if (SkRegion::kIntersect_Op == op) { |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1201 | // since we are intersect, we can do better (tighter) with currRgn's |
| 1202 | // bounds, than just using the device. However, if currRgn is complex, |
| 1203 | // our region blitter may hork, so we do that case in two steps. |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1204 | if (currClip->isRect()) { |
commit-bot@chromium.org | b446fc7 | 2013-07-10 20:55:39 +0000 | [diff] [blame] | 1205 | // FIXME: we should also be able to do this when currClip->isBW(), |
| 1206 | // but relaxing the test above triggers GM asserts in |
| 1207 | // SkRgnBuilder::blitH(). We need to investigate what's going on. |
| 1208 | return currClip->setPath(devPath, currClip->bwRgn(), doAA); |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1209 | } else { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1210 | base.setRect(currClip->getBounds()); |
| 1211 | SkRasterClip clip; |
| 1212 | clip.setPath(devPath, base, doAA); |
| 1213 | return currClip->op(clip, op); |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1214 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1215 | } else { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1216 | const SkBaseDevice* device = canvas->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1217 | if (!device) { |
| 1218 | return currClip->setEmpty(); |
| 1219 | } |
| 1220 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1221 | base.setRect(0, 0, device->width(), device->height()); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (SkRegion::kReplace_Op == op) { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1224 | return currClip->setPath(devPath, base, doAA); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1225 | } else { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1226 | SkRasterClip clip; |
| 1227 | clip.setPath(devPath, base, doAA); |
| 1228 | return currClip->op(clip, op); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1233 | bool SkCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| 1234 | if (rrect.isRect()) { |
| 1235 | // call the non-virtual version |
| 1236 | return this->SkCanvas::clipRect(rrect.getBounds(), op, doAA); |
| 1237 | } else { |
| 1238 | SkPath path; |
| 1239 | path.addRRect(rrect); |
| 1240 | // call the non-virtual version |
| 1241 | return this->SkCanvas::clipPath(path, op, doAA); |
| 1242 | } |
| 1243 | } |
| 1244 | |
reed@google.com | c42d35d | 2011-10-12 11:57:42 +0000 | [diff] [blame] | 1245 | bool SkCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1246 | #ifdef SK_ENABLE_CLIP_QUICKREJECT |
| 1247 | if (SkRegion::kIntersect_Op == op && !path.isInverseFillType()) { |
| 1248 | if (fMCRec->fRasterClip->isEmpty()) { |
| 1249 | return false; |
| 1250 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1251 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1252 | if (this->quickReject(path.getBounds())) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1253 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1254 | fCachedLocalClipBoundsDirty = true; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1255 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1256 | fClipStack.clipEmpty(); |
| 1257 | return fMCRec->fRasterClip->setEmpty(); |
| 1258 | } |
| 1259 | } |
| 1260 | #endif |
| 1261 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1262 | AutoValidateClip avc(this); |
| 1263 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1264 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1265 | fCachedLocalClipBoundsDirty = true; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 1266 | doAA &= fAllowSoftClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1267 | |
| 1268 | SkPath devPath; |
| 1269 | path.transform(*fMCRec->fMatrix, &devPath); |
| 1270 | |
reed@google.com | fe70112 | 2011-11-08 19:41:23 +0000 | [diff] [blame] | 1271 | // Check if the transfomation, or the original path itself |
| 1272 | // made us empty. Note this can also happen if we contained NaN |
| 1273 | // values. computing the bounds detects this, and will set our |
| 1274 | // bounds to empty if that is the case. (see SkRect::set(pts, count)) |
| 1275 | if (devPath.getBounds().isEmpty()) { |
| 1276 | // resetting the path will remove any NaN or other wanky values |
| 1277 | // that might upset our scan converter. |
| 1278 | devPath.reset(); |
| 1279 | } |
| 1280 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1281 | // if we called path.swap() we could avoid a deep copy of this path |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1282 | fClipStack.clipDevPath(devPath, op, doAA); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1283 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1284 | if (fAllowSimplifyClip) { |
| 1285 | devPath.reset(); |
| 1286 | devPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1287 | const SkClipStack* clipStack = getClipStack(); |
| 1288 | SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 1289 | const SkClipStack::Element* element; |
| 1290 | while ((element = iter.next())) { |
| 1291 | SkClipStack::Element::Type type = element->getType(); |
| 1292 | if (type == SkClipStack::Element::kEmpty_Type) { |
| 1293 | continue; |
| 1294 | } |
| 1295 | SkPath operand; |
| 1296 | if (type == SkClipStack::Element::kRect_Type) { |
| 1297 | operand.addRect(element->getRect()); |
| 1298 | } else if (type == SkClipStack::Element::kPath_Type) { |
| 1299 | operand = element->getPath(); |
| 1300 | } else { |
| 1301 | SkDEBUGFAIL("Unexpected type."); |
| 1302 | } |
| 1303 | SkRegion::Op elementOp = element->getOp(); |
| 1304 | if (elementOp == SkRegion::kReplace_Op) { |
| 1305 | devPath = operand; |
| 1306 | } else { |
| 1307 | Op(devPath, operand, (SkPathOp) elementOp, &devPath); |
| 1308 | } |
caryclark@google.com | 96fd344 | 2013-05-07 19:48:31 +0000 | [diff] [blame] | 1309 | // if the prev and curr clips disagree about aa -vs- not, favor the aa request. |
| 1310 | // perhaps we need an API change to avoid this sort of mixed-signals about |
| 1311 | // clipping. |
| 1312 | doAA |= element->isAA(); |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1313 | } |
| 1314 | op = SkRegion::kReplace_Op; |
| 1315 | } |
| 1316 | |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1317 | return clipPathHelper(this, fMCRec->fRasterClip, devPath, op, doAA); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1320 | bool SkCanvas::updateClipConservativelyUsingBounds(const SkRect& bounds, SkRegion::Op op, |
| 1321 | bool inverseFilled) { |
| 1322 | // This is for updating the clip conservatively using only bounds |
| 1323 | // information. |
| 1324 | // Contract: |
| 1325 | // The current clip must contain the true clip. The true |
| 1326 | // clip is the clip that would have normally been computed |
| 1327 | // by calls to clipPath and clipRRect |
| 1328 | // Objective: |
| 1329 | // Keep the current clip as small as possible without |
| 1330 | // breaking the contract, using only clip bounding rectangles |
| 1331 | // (for performance). |
| 1332 | |
| 1333 | // N.B.: This *never* calls back through a virtual on canvas, so subclasses |
| 1334 | // don't have to worry about getting caught in a loop. Thus anywhere |
| 1335 | // we call a virtual method, we explicitly prefix it with |
| 1336 | // SkCanvas:: to be sure to call the base-class. |
skia.committer@gmail.com | a5d3e77 | 2013-05-30 07:01:29 +0000 | [diff] [blame] | 1337 | |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1338 | if (inverseFilled) { |
| 1339 | switch (op) { |
| 1340 | case SkRegion::kIntersect_Op: |
| 1341 | case SkRegion::kDifference_Op: |
| 1342 | // These ops can only shrink the current clip. So leaving |
| 1343 | // the clip unchanges conservatively respects the contract. |
| 1344 | return this->getClipDeviceBounds(NULL); |
| 1345 | case SkRegion::kUnion_Op: |
| 1346 | case SkRegion::kReplace_Op: |
| 1347 | case SkRegion::kReverseDifference_Op: |
| 1348 | case SkRegion::kXOR_Op: |
| 1349 | { |
| 1350 | // These ops can grow the current clip up to the extents of |
| 1351 | // the input clip, which is inverse filled, so we just set |
| 1352 | // the current clip to the device bounds. |
| 1353 | SkRect deviceBounds; |
| 1354 | SkIRect deviceIBounds; |
| 1355 | this->getDevice()->getGlobalBounds(&deviceIBounds); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1356 | deviceBounds = SkRect::Make(deviceIBounds); |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1357 | this->SkCanvas::save(SkCanvas::kMatrix_SaveFlag); |
| 1358 | // set the clip in device space |
| 1359 | this->SkCanvas::setMatrix(SkMatrix::I()); |
| 1360 | bool result = this->SkCanvas::clipRect(deviceBounds, |
| 1361 | SkRegion::kReplace_Op, false); |
| 1362 | this->SkCanvas::restore(); //pop the matrix, but keep the clip |
| 1363 | return result; |
| 1364 | } |
| 1365 | default: |
| 1366 | SkASSERT(0); // unhandled op? |
| 1367 | } |
| 1368 | } else { |
| 1369 | // Not inverse filled |
| 1370 | switch (op) { |
| 1371 | case SkRegion::kIntersect_Op: |
| 1372 | case SkRegion::kUnion_Op: |
| 1373 | case SkRegion::kReplace_Op: |
| 1374 | return this->SkCanvas::clipRect(bounds, op, false); |
| 1375 | case SkRegion::kDifference_Op: |
| 1376 | // Difference can only shrink the current clip. |
| 1377 | // Leaving clip unchanged conservatively fullfills the contract. |
| 1378 | return this->getClipDeviceBounds(NULL); |
| 1379 | case SkRegion::kReverseDifference_Op: |
| 1380 | // To reverse, we swap in the bounds with a replace op. |
| 1381 | // As with difference, leave it unchanged. |
| 1382 | return this->SkCanvas::clipRect(bounds, SkRegion::kReplace_Op, false); |
| 1383 | case SkRegion::kXOR_Op: |
| 1384 | // Be conservative, based on (A XOR B) always included in (A union B), |
| 1385 | // which is always included in (bounds(A) union bounds(B)) |
| 1386 | return this->SkCanvas::clipRect(bounds, SkRegion::kUnion_Op, false); |
| 1387 | default: |
| 1388 | SkASSERT(0); // unhandled op? |
| 1389 | } |
| 1390 | } |
| 1391 | return true; |
| 1392 | } |
| 1393 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1394 | bool SkCanvas::clipRegion(const SkRegion& rgn, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1395 | AutoValidateClip avc(this); |
| 1396 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1397 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1398 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1399 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1400 | // todo: signal fClipStack that we have a region, and therefore (I guess) |
| 1401 | // we have to ignore it, and use the region directly? |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 1402 | fClipStack.clipDevRect(rgn.getBounds(), op); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1403 | |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1404 | return fMCRec->fRasterClip->op(rgn, op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1407 | #ifdef SK_DEBUG |
| 1408 | void SkCanvas::validateClip() const { |
| 1409 | // construct clipRgn from the clipstack |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1410 | const SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1411 | if (!device) { |
| 1412 | SkASSERT(this->getTotalClip().isEmpty()); |
| 1413 | return; |
| 1414 | } |
| 1415 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1416 | SkIRect ir; |
| 1417 | ir.set(0, 0, device->width(), device->height()); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1418 | SkRasterClip tmpClip(ir); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1419 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1420 | SkClipStack::B2TIter iter(fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1421 | const SkClipStack::Element* element; |
| 1422 | while ((element = iter.next()) != NULL) { |
| 1423 | switch (element->getType()) { |
| 1424 | case SkClipStack::Element::kPath_Type: |
| 1425 | clipPathHelper(this, |
| 1426 | &tmpClip, |
| 1427 | element->getPath(), |
| 1428 | element->getOp(), |
| 1429 | element->isAA()); |
| 1430 | break; |
| 1431 | case SkClipStack::Element::kRect_Type: |
| 1432 | element->getRect().round(&ir); |
| 1433 | tmpClip.op(ir, element->getOp()); |
| 1434 | break; |
| 1435 | case SkClipStack::Element::kEmpty_Type: |
| 1436 | tmpClip.setEmpty(); |
| 1437 | break; |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 1441 | #if 0 // enable this locally for testing |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1442 | // now compare against the current rgn |
| 1443 | const SkRegion& rgn = this->getTotalClip(); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1444 | SkASSERT(rgn == tmpClip); |
reed@google.com | 02878b8 | 2011-02-24 13:04:42 +0000 | [diff] [blame] | 1445 | #endif |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1446 | } |
| 1447 | #endif |
| 1448 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1449 | void SkCanvas::replayClips(ClipVisitor* visitor) const { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1450 | SkClipStack::B2TIter iter(fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1451 | const SkClipStack::Element* element; |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1452 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1453 | static const SkRect kEmpty = { 0, 0, 0, 0 }; |
| 1454 | while ((element = iter.next()) != NULL) { |
| 1455 | switch (element->getType()) { |
| 1456 | case SkClipStack::Element::kPath_Type: |
| 1457 | visitor->clipPath(element->getPath(), element->getOp(), element->isAA()); |
| 1458 | break; |
| 1459 | case SkClipStack::Element::kRect_Type: |
| 1460 | visitor->clipRect(element->getRect(), element->getOp(), element->isAA()); |
| 1461 | break; |
| 1462 | case SkClipStack::Element::kEmpty_Type: |
| 1463 | visitor->clipRect(kEmpty, SkRegion::kIntersect_Op, false); |
| 1464 | break; |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1469 | /////////////////////////////////////////////////////////////////////////////// |
| 1470 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1471 | bool SkCanvas::quickReject(const SkRect& rect) const { |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1472 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 1473 | if (!rect.isFinite()) |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1474 | return true; |
| 1475 | |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1476 | if (fMCRec->fRasterClip->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1477 | return true; |
| 1478 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1479 | |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 1480 | if (fMCRec->fMatrix->hasPerspective()) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1481 | SkRect dst; |
| 1482 | fMCRec->fMatrix->mapRect(&dst, rect); |
| 1483 | SkIRect idst; |
| 1484 | dst.roundOut(&idst); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1485 | return !SkIRect::Intersects(idst, fMCRec->fRasterClip->getBounds()); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1486 | } else { |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1487 | const SkRect& clipR = this->getLocalClipBounds(); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1488 | |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1489 | // for speed, do the most likely reject compares first |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1490 | // TODO: should we use | instead, or compare all 4 at once? |
| 1491 | if (rect.fTop >= clipR.fBottom || rect.fBottom <= clipR.fTop) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1492 | return true; |
| 1493 | } |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1494 | if (rect.fLeft >= clipR.fRight || rect.fRight <= clipR.fLeft) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1495 | return true; |
| 1496 | } |
| 1497 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1498 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1501 | bool SkCanvas::quickReject(const SkPath& path) const { |
| 1502 | return path.isEmpty() || this->quickReject(path.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1505 | bool SkCanvas::getClipBounds(SkRect* bounds) const { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1506 | SkIRect ibounds; |
| 1507 | if (!getClipDeviceBounds(&ibounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1508 | return false; |
| 1509 | } |
| 1510 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1511 | SkMatrix inverse; |
| 1512 | // if we can't invert the CTM, we can't return local clip bounds |
| 1513 | if (!fMCRec->fMatrix->invert(&inverse)) { |
reed@android.com | 72dcd3a | 2009-05-18 15:46:29 +0000 | [diff] [blame] | 1514 | if (bounds) { |
| 1515 | bounds->setEmpty(); |
| 1516 | } |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1517 | return false; |
| 1518 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1519 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1520 | if (NULL != bounds) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1521 | SkRect r; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1522 | // adjust it outwards in case we are antialiasing |
| 1523 | const int inset = 1; |
reed@google.com | fa4d5bd | 2012-01-30 17:36:27 +0000 | [diff] [blame] | 1524 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 1525 | r.iset(ibounds.fLeft - inset, ibounds.fTop - inset, |
| 1526 | ibounds.fRight + inset, ibounds.fBottom + inset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1527 | inverse.mapRect(bounds, r); |
| 1528 | } |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1532 | bool SkCanvas::getClipDeviceBounds(SkIRect* bounds) const { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1533 | const SkRasterClip& clip = *fMCRec->fRasterClip; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1534 | if (clip.isEmpty()) { |
| 1535 | if (bounds) { |
| 1536 | bounds->setEmpty(); |
| 1537 | } |
| 1538 | return false; |
| 1539 | } |
| 1540 | |
| 1541 | if (NULL != bounds) { |
| 1542 | *bounds = clip.getBounds(); |
| 1543 | } |
| 1544 | return true; |
| 1545 | } |
| 1546 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1547 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
| 1548 | return *fMCRec->fMatrix; |
| 1549 | } |
| 1550 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1551 | SkCanvas::ClipType SkCanvas::getClipType() const { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1552 | if (fMCRec->fRasterClip->isEmpty()) return kEmpty_ClipType; |
| 1553 | if (fMCRec->fRasterClip->isRect()) return kRect_ClipType; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1554 | return kComplex_ClipType; |
| 1555 | } |
| 1556 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1557 | const SkRegion& SkCanvas::getTotalClip() const { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1558 | return fMCRec->fRasterClip->forceGetBW(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1561 | SkBaseDevice* SkCanvas::createLayerDevice(SkBitmap::Config config, |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame^] | 1562 | int width, int height, |
| 1563 | bool isOpaque) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1564 | SkBaseDevice* device = this->getTopDevice(); |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 1565 | if (device) { |
| 1566 | return device->createCompatibleDeviceForSaveLayer(config, width, height, |
| 1567 | isOpaque); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1568 | } else { |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 1569 | return NULL; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1570 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1573 | GrContext* SkCanvas::getGrContext() { |
| 1574 | #if SK_SUPPORT_GPU |
| 1575 | SkBaseDevice* device = this->getTopDevice(); |
| 1576 | if (NULL != device) { |
| 1577 | GrRenderTarget* renderTarget = device->accessRenderTarget(); |
| 1578 | if (NULL != renderTarget) { |
| 1579 | return renderTarget->getContext(); |
| 1580 | } |
| 1581 | } |
| 1582 | #endif |
| 1583 | |
| 1584 | return NULL; |
| 1585 | |
| 1586 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1587 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1588 | ////////////////////////////////////////////////////////////////////////////// |
| 1589 | // These are the virtual drawing methods |
| 1590 | ////////////////////////////////////////////////////////////////////////////// |
| 1591 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1592 | void SkCanvas::clear(SkColor color) { |
| 1593 | SkDrawIter iter(this); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 1594 | this->predrawNotify(); |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1595 | while (iter.next()) { |
| 1596 | iter.fDevice->clear(color); |
| 1597 | } |
| 1598 | } |
| 1599 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1600 | void SkCanvas::drawPaint(const SkPaint& paint) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1601 | this->internalDrawPaint(paint); |
| 1602 | } |
| 1603 | |
| 1604 | void SkCanvas::internalDrawPaint(const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1605 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 1606 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1607 | LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1608 | |
| 1609 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1610 | iter.fDevice->drawPaint(iter, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1613 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 1617 | const SkPaint& paint) { |
| 1618 | if ((long)count <= 0) { |
| 1619 | return; |
| 1620 | } |
| 1621 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1622 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 1623 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1624 | SkRect r, storage; |
| 1625 | const SkRect* bounds = NULL; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1626 | if (paint.canComputeFastBounds()) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1627 | // special-case 2 points (common for drawing a single line) |
| 1628 | if (2 == count) { |
| 1629 | r.set(pts[0], pts[1]); |
| 1630 | } else { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 1631 | r.set(pts, SkToInt(count)); |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1632 | } |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1633 | bounds = &paint.computeFastStrokeBounds(r, &storage); |
| 1634 | if (this->quickReject(*bounds)) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1635 | return; |
| 1636 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1637 | } |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1638 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1639 | SkASSERT(pts != NULL); |
| 1640 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1641 | LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1642 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1643 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1644 | iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1645 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1646 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1647 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 1650 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1651 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 1652 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1653 | SkRect storage; |
| 1654 | const SkRect* bounds = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1655 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1656 | bounds = &paint.computeFastBounds(r, &storage); |
| 1657 | if (this->quickReject(*bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | } |
| 1660 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1661 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1662 | LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1663 | |
| 1664 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1665 | iter.fDevice->drawRect(iter, r, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1668 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1671 | void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1672 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 1673 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1674 | SkRect storage; |
| 1675 | const SkRect* bounds = NULL; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1676 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1677 | bounds = &paint.computeFastBounds(oval, &storage); |
| 1678 | if (this->quickReject(*bounds)) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1679 | return; |
| 1680 | } |
| 1681 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 1682 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1683 | LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds) |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1684 | |
| 1685 | while (iter.next()) { |
| 1686 | iter.fDevice->drawOval(iter, oval, looper.paint()); |
| 1687 | } |
| 1688 | |
| 1689 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1693 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 1694 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1695 | SkRect storage; |
| 1696 | const SkRect* bounds = NULL; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1697 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1698 | bounds = &paint.computeFastBounds(rrect.getBounds(), &storage); |
| 1699 | if (this->quickReject(*bounds)) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1700 | return; |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | if (rrect.isRect()) { |
| 1705 | // call the non-virtual version |
| 1706 | this->SkCanvas::drawRect(rrect.getBounds(), paint); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1707 | return; |
| 1708 | } else if (rrect.isOval()) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1709 | // call the non-virtual version |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1710 | this->SkCanvas::drawOval(rrect.getBounds(), paint); |
| 1711 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1712 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1713 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1714 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1715 | |
| 1716 | while (iter.next()) { |
| 1717 | iter.fDevice->drawRRect(iter, rrect, looper.paint()); |
| 1718 | } |
| 1719 | |
| 1720 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 1724 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1725 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 1726 | |
reed@google.com | 9364511 | 2012-07-26 16:11:47 +0000 | [diff] [blame] | 1727 | if (!path.isFinite()) { |
| 1728 | return; |
| 1729 | } |
| 1730 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1731 | SkRect storage; |
| 1732 | const SkRect* bounds = NULL; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1733 | if (!path.isInverseFillType() && paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1734 | const SkRect& pathBounds = path.getBounds(); |
| 1735 | bounds = &paint.computeFastBounds(pathBounds, &storage); |
| 1736 | if (this->quickReject(*bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1737 | return; |
| 1738 | } |
| 1739 | } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1740 | if (path.isEmpty()) { |
| 1741 | if (path.isInverseFillType()) { |
| 1742 | this->internalDrawPaint(paint); |
| 1743 | } |
| 1744 | return; |
| 1745 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1746 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1747 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1748 | |
| 1749 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1750 | iter.fDevice->drawPath(iter, path, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1753 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 1757 | const SkPaint* paint) { |
| 1758 | SkDEBUGCODE(bitmap.validate();) |
| 1759 | |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1760 | if (NULL == paint || paint->canComputeFastBounds()) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 1761 | SkRect bounds = { |
| 1762 | x, y, |
| 1763 | x + SkIntToScalar(bitmap.width()), |
| 1764 | y + SkIntToScalar(bitmap.height()) |
| 1765 | }; |
| 1766 | if (paint) { |
| 1767 | (void)paint->computeFastBounds(bounds, &bounds); |
| 1768 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1769 | if (this->quickReject(bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1770 | return; |
| 1771 | } |
| 1772 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1773 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1774 | SkMatrix matrix; |
| 1775 | matrix.setTranslate(x, y); |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1776 | this->internalDrawBitmap(bitmap, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1779 | // this one is non-virtual, so it can be called safely by other canvas apis |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1780 | void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1781 | const SkRect& dst, const SkPaint* paint, |
| 1782 | DrawBitmapRectFlags flags) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame^] | 1783 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1784 | return; |
| 1785 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1786 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 1787 | CHECK_LOCKCOUNT_BALANCE(bitmap); |
| 1788 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1789 | SkRect storage; |
| 1790 | const SkRect* bounds = &dst; |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1791 | if (NULL == paint || paint->canComputeFastBounds()) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 1792 | if (paint) { |
| 1793 | bounds = &paint->computeFastBounds(dst, &storage); |
| 1794 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1795 | if (this->quickReject(*bounds)) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1796 | return; |
| 1797 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1798 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1799 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1800 | SkLazyPaint lazy; |
| 1801 | if (NULL == paint) { |
| 1802 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1803 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1804 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1805 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1806 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1807 | while (iter.next()) { |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1808 | iter.fDevice->drawBitmapRect(iter, bitmap, src, dst, looper.paint(), flags); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1809 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1810 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1811 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1814 | void SkCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1815 | const SkRect& dst, const SkPaint* paint, |
| 1816 | DrawBitmapRectFlags flags) { |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1817 | SkDEBUGCODE(bitmap.validate();) |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1818 | this->internalDrawBitmapRect(bitmap, src, dst, paint, flags); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1821 | void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, |
| 1822 | const SkPaint* paint) { |
| 1823 | SkDEBUGCODE(bitmap.validate();) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1824 | this->internalDrawBitmap(bitmap, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1827 | void SkCanvas::internalDrawBitmapNine(const SkBitmap& bitmap, |
| 1828 | const SkIRect& center, const SkRect& dst, |
| 1829 | const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame^] | 1830 | if (bitmap.drawsNothing()) { |
| 1831 | return; |
| 1832 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1833 | if (NULL == paint || paint->canComputeFastBounds()) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 1834 | SkRect storage; |
| 1835 | const SkRect* bounds = &dst; |
| 1836 | if (paint) { |
| 1837 | bounds = &paint->computeFastBounds(dst, &storage); |
| 1838 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1839 | if (this->quickReject(*bounds)) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1840 | return; |
| 1841 | } |
| 1842 | } |
| 1843 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1844 | const int32_t w = bitmap.width(); |
| 1845 | const int32_t h = bitmap.height(); |
| 1846 | |
| 1847 | SkIRect c = center; |
| 1848 | // pin center to the bounds of the bitmap |
| 1849 | c.fLeft = SkMax32(0, center.fLeft); |
| 1850 | c.fTop = SkMax32(0, center.fTop); |
| 1851 | c.fRight = SkPin32(center.fRight, c.fLeft, w); |
| 1852 | c.fBottom = SkPin32(center.fBottom, c.fTop, h); |
| 1853 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1854 | const SkScalar srcX[4] = { |
rmistry@google.com | 7d474f8 | 2013-01-02 22:03:54 +0000 | [diff] [blame] | 1855 | 0, SkIntToScalar(c.fLeft), SkIntToScalar(c.fRight), SkIntToScalar(w) |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1856 | }; |
| 1857 | const SkScalar srcY[4] = { |
rmistry@google.com | 7d474f8 | 2013-01-02 22:03:54 +0000 | [diff] [blame] | 1858 | 0, SkIntToScalar(c.fTop), SkIntToScalar(c.fBottom), SkIntToScalar(h) |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1859 | }; |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1860 | SkScalar dstX[4] = { |
| 1861 | dst.fLeft, dst.fLeft + SkIntToScalar(c.fLeft), |
| 1862 | dst.fRight - SkIntToScalar(w - c.fRight), dst.fRight |
| 1863 | }; |
| 1864 | SkScalar dstY[4] = { |
| 1865 | dst.fTop, dst.fTop + SkIntToScalar(c.fTop), |
| 1866 | dst.fBottom - SkIntToScalar(h - c.fBottom), dst.fBottom |
| 1867 | }; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1868 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1869 | if (dstX[1] > dstX[2]) { |
| 1870 | dstX[1] = dstX[0] + (dstX[3] - dstX[0]) * c.fLeft / (w - c.width()); |
| 1871 | dstX[2] = dstX[1]; |
| 1872 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1873 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1874 | if (dstY[1] > dstY[2]) { |
| 1875 | dstY[1] = dstY[0] + (dstY[3] - dstY[0]) * c.fTop / (h - c.height()); |
| 1876 | dstY[2] = dstY[1]; |
| 1877 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1878 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1879 | for (int y = 0; y < 3; y++) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1880 | SkRect s, d; |
| 1881 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1882 | s.fTop = srcY[y]; |
| 1883 | s.fBottom = srcY[y+1]; |
| 1884 | d.fTop = dstY[y]; |
| 1885 | d.fBottom = dstY[y+1]; |
| 1886 | for (int x = 0; x < 3; x++) { |
| 1887 | s.fLeft = srcX[x]; |
| 1888 | s.fRight = srcX[x+1]; |
| 1889 | d.fLeft = dstX[x]; |
| 1890 | d.fRight = dstX[x+1]; |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1891 | this->internalDrawBitmapRect(bitmap, &s, d, paint, |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 1892 | kNone_DrawBitmapRectFlag); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 1898 | const SkRect& dst, const SkPaint* paint) { |
| 1899 | SkDEBUGCODE(bitmap.validate();) |
| 1900 | |
| 1901 | // Need a device entry-point, so gpu can use a mesh |
| 1902 | this->internalDrawBitmapNine(bitmap, center, dst, paint); |
| 1903 | } |
| 1904 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1905 | class SkDeviceFilteredPaint { |
| 1906 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1907 | SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) { |
| 1908 | SkBaseDevice::TextFlags flags; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1909 | if (device->filterTextFlags(paint, &flags)) { |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 1910 | SkPaint* newPaint = fLazy.set(paint); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1911 | newPaint->setFlags(flags.fFlags); |
| 1912 | newPaint->setHinting(flags.fHinting); |
| 1913 | fPaint = newPaint; |
| 1914 | } else { |
| 1915 | fPaint = &paint; |
| 1916 | } |
| 1917 | } |
| 1918 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1919 | const SkPaint& paint() const { return *fPaint; } |
| 1920 | |
| 1921 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1922 | const SkPaint* fPaint; |
| 1923 | SkLazyPaint fLazy; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1924 | }; |
| 1925 | |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 1926 | void SkCanvas::DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 1927 | const SkRect& r, SkScalar textSize) { |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 1928 | if (paint.getStyle() == SkPaint::kFill_Style) { |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 1929 | draw.fDevice->drawRect(draw, r, paint); |
| 1930 | } else { |
| 1931 | SkPaint p(paint); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 1932 | p.setStrokeWidth(SkScalarMul(textSize, paint.getStrokeWidth())); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 1933 | draw.fDevice->drawRect(draw, r, p); |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | void SkCanvas::DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 1938 | const char text[], size_t byteLength, |
| 1939 | SkScalar x, SkScalar y) { |
| 1940 | SkASSERT(byteLength == 0 || text != NULL); |
| 1941 | |
| 1942 | // nothing to draw |
| 1943 | if (text == NULL || byteLength == 0 || |
| 1944 | draw.fClip->isEmpty() || |
| 1945 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | SkScalar width = 0; |
| 1950 | SkPoint start; |
| 1951 | |
| 1952 | start.set(0, 0); // to avoid warning |
| 1953 | if (paint.getFlags() & (SkPaint::kUnderlineText_Flag | |
| 1954 | SkPaint::kStrikeThruText_Flag)) { |
| 1955 | width = paint.measureText(text, byteLength); |
| 1956 | |
| 1957 | SkScalar offsetX = 0; |
| 1958 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 1959 | offsetX = SkScalarHalf(width); |
| 1960 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
| 1961 | offsetX = width; |
| 1962 | } |
| 1963 | start.set(x - offsetX, y); |
| 1964 | } |
| 1965 | |
| 1966 | if (0 == width) { |
| 1967 | return; |
| 1968 | } |
| 1969 | |
| 1970 | uint32_t flags = paint.getFlags(); |
| 1971 | |
| 1972 | if (flags & (SkPaint::kUnderlineText_Flag | |
| 1973 | SkPaint::kStrikeThruText_Flag)) { |
| 1974 | SkScalar textSize = paint.getTextSize(); |
| 1975 | SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness); |
| 1976 | SkRect r; |
| 1977 | |
| 1978 | r.fLeft = start.fX; |
| 1979 | r.fRight = start.fX + width; |
| 1980 | |
| 1981 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 1982 | SkScalar offset = SkScalarMulAdd(textSize, kStdUnderline_Offset, |
| 1983 | start.fY); |
| 1984 | r.fTop = offset; |
| 1985 | r.fBottom = offset + height; |
| 1986 | DrawRect(draw, paint, r, textSize); |
| 1987 | } |
| 1988 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 1989 | SkScalar offset = SkScalarMulAdd(textSize, kStdStrikeThru_Offset, |
| 1990 | start.fY); |
| 1991 | r.fTop = offset; |
| 1992 | r.fBottom = offset + height; |
| 1993 | DrawRect(draw, paint, r, textSize); |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1998 | void SkCanvas::drawText(const void* text, size_t byteLength, |
| 1999 | SkScalar x, SkScalar y, const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 2000 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 2001 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2002 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2003 | |
| 2004 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2005 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2006 | iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint()); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2007 | DrawTextDecorations(iter, dfp.paint(), |
| 2008 | static_cast<const char*>(text), byteLength, x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2011 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | void SkCanvas::drawPosText(const void* text, size_t byteLength, |
| 2015 | const SkPoint pos[], const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 2016 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 2017 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2018 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2019 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2020 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2021 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2022 | iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2023 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2024 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2025 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2026 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | void SkCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 2030 | const SkScalar xpos[], SkScalar constY, |
| 2031 | const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 2032 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 2033 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2034 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2035 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2036 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2037 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2038 | iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2039 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2040 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2041 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2042 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 2046 | const SkPath& path, const SkMatrix* matrix, |
| 2047 | const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 2048 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 2049 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2050 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2051 | |
| 2052 | while (iter.next()) { |
| 2053 | iter.fDevice->drawTextOnPath(iter, text, byteLength, path, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2054 | matrix, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2057 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 2061 | const SkPoint verts[], const SkPoint texs[], |
| 2062 | const SkColor colors[], SkXfermode* xmode, |
| 2063 | const uint16_t indices[], int indexCount, |
| 2064 | const SkPaint& paint) { |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 2065 | CHECK_SHADER_NOSETCONTEXT(paint); |
| 2066 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2067 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2068 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2069 | while (iter.next()) { |
| 2070 | iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2071 | colors, xmode, indices, indexCount, |
| 2072 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2073 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2074 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2075 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | ////////////////////////////////////////////////////////////////////////////// |
| 2079 | // These methods are NOT virtual, and therefore must call back into virtual |
| 2080 | // methods, rather than actually drawing themselves. |
| 2081 | ////////////////////////////////////////////////////////////////////////////// |
| 2082 | |
| 2083 | void SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2084 | SkXfermode::Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2085 | SkPaint paint; |
| 2086 | |
| 2087 | paint.setARGB(a, r, g, b); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2088 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2089 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2090 | } |
| 2091 | this->drawPaint(paint); |
| 2092 | } |
| 2093 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2094 | void SkCanvas::drawColor(SkColor c, SkXfermode::Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2095 | SkPaint paint; |
| 2096 | |
| 2097 | paint.setColor(c); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2098 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2099 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2100 | } |
| 2101 | this->drawPaint(paint); |
| 2102 | } |
| 2103 | |
| 2104 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
| 2105 | SkPoint pt; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2107 | pt.set(x, y); |
| 2108 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2109 | } |
| 2110 | |
| 2111 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) { |
| 2112 | SkPoint pt; |
| 2113 | SkPaint paint; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2115 | pt.set(x, y); |
| 2116 | paint.setColor(color); |
| 2117 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2118 | } |
| 2119 | |
| 2120 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 2121 | const SkPaint& paint) { |
| 2122 | SkPoint pts[2]; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2124 | pts[0].set(x0, y0); |
| 2125 | pts[1].set(x1, y1); |
| 2126 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 2127 | } |
| 2128 | |
| 2129 | void SkCanvas::drawRectCoords(SkScalar left, SkScalar top, |
| 2130 | SkScalar right, SkScalar bottom, |
| 2131 | const SkPaint& paint) { |
| 2132 | SkRect r; |
| 2133 | |
| 2134 | r.set(left, top, right, bottom); |
| 2135 | this->drawRect(r, paint); |
| 2136 | } |
| 2137 | |
| 2138 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 2139 | const SkPaint& paint) { |
| 2140 | if (radius < 0) { |
| 2141 | radius = 0; |
| 2142 | } |
| 2143 | |
| 2144 | SkRect r; |
| 2145 | r.set(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2146 | this->drawOval(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 2150 | const SkPaint& paint) { |
| 2151 | if (rx > 0 && ry > 0) { |
| 2152 | if (paint.canComputeFastBounds()) { |
| 2153 | SkRect storage; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 2154 | if (this->quickReject(paint.computeFastBounds(r, &storage))) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2155 | return; |
| 2156 | } |
| 2157 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2158 | SkRRect rrect; |
| 2159 | rrect.setRectXY(r, rx, ry); |
| 2160 | this->drawRRect(rrect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2161 | } else { |
| 2162 | this->drawRect(r, paint); |
| 2163 | } |
| 2164 | } |
| 2165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2166 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 2167 | SkScalar sweepAngle, bool useCenter, |
| 2168 | const SkPaint& paint) { |
| 2169 | if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) { |
| 2170 | this->drawOval(oval, paint); |
| 2171 | } else { |
| 2172 | SkPath path; |
| 2173 | if (useCenter) { |
| 2174 | path.moveTo(oval.centerX(), oval.centerY()); |
| 2175 | } |
| 2176 | path.arcTo(oval, startAngle, sweepAngle, !useCenter); |
| 2177 | if (useCenter) { |
| 2178 | path.close(); |
| 2179 | } |
| 2180 | this->drawPath(path, paint); |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | void SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength, |
| 2185 | const SkPath& path, SkScalar hOffset, |
| 2186 | SkScalar vOffset, const SkPaint& paint) { |
| 2187 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2188 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2189 | matrix.setTranslate(hOffset, vOffset); |
| 2190 | this->drawTextOnPath(text, byteLength, path, &matrix, paint); |
| 2191 | } |
| 2192 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 2193 | /////////////////////////////////////////////////////////////////////////////// |
| 2194 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2195 | void SkCanvas::drawPicture(SkPicture& picture) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2196 | picture.draw(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2199 | /////////////////////////////////////////////////////////////////////////////// |
| 2200 | /////////////////////////////////////////////////////////////////////////////// |
| 2201 | |
| 2202 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) { |
reed@google.com | d642329 | 2010-12-20 20:53:13 +0000 | [diff] [blame] | 2203 | SK_COMPILE_ASSERT(sizeof(fStorage) >= sizeof(SkDrawIter), fStorage_too_small); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2204 | |
| 2205 | SkASSERT(canvas); |
| 2206 | |
| 2207 | fImpl = new (fStorage) SkDrawIter(canvas, skipEmptyClips); |
| 2208 | fDone = !fImpl->next(); |
| 2209 | } |
| 2210 | |
| 2211 | SkCanvas::LayerIter::~LayerIter() { |
| 2212 | fImpl->~SkDrawIter(); |
| 2213 | } |
| 2214 | |
| 2215 | void SkCanvas::LayerIter::next() { |
| 2216 | fDone = !fImpl->next(); |
| 2217 | } |
| 2218 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 2219 | SkBaseDevice* SkCanvas::LayerIter::device() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2220 | return fImpl->getDevice(); |
| 2221 | } |
| 2222 | |
| 2223 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
| 2224 | return fImpl->getMatrix(); |
| 2225 | } |
| 2226 | |
| 2227 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 2228 | const SkPaint* paint = fImpl->getPaint(); |
| 2229 | if (NULL == paint) { |
| 2230 | paint = &fDefaultPaint; |
| 2231 | } |
| 2232 | return *paint; |
| 2233 | } |
| 2234 | |
| 2235 | const SkRegion& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); } |
| 2236 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 2237 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 2238 | |
| 2239 | /////////////////////////////////////////////////////////////////////////////// |
| 2240 | |
| 2241 | SkCanvas::ClipVisitor::~ClipVisitor() { } |