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