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