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