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