epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkBounder.h" |
| 12 | #include "SkDevice.h" |
| 13 | #include "SkDraw.h" |
| 14 | #include "SkDrawFilter.h" |
| 15 | #include "SkDrawLooper.h" |
| 16 | #include "SkPicture.h" |
| 17 | #include "SkScalarCompare.h" |
| 18 | #include "SkTemplates.h" |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 19 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
| 22 | //#define SK_TRACE_SAVERESTORE |
| 23 | |
| 24 | #ifdef SK_TRACE_SAVERESTORE |
| 25 | static int gLayerCounter; |
| 26 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 27 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 28 | |
| 29 | static int gRecCounter; |
| 30 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 31 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 32 | |
| 33 | static int gCanvasCounter; |
| 34 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 35 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 36 | #else |
| 37 | #define inc_layer() |
| 38 | #define dec_layer() |
| 39 | #define inc_rec() |
| 40 | #define dec_rec() |
| 41 | #define inc_canvas() |
| 42 | #define dec_canvas() |
| 43 | #endif |
| 44 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 45 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | // Helpers for computing fast bounds for quickReject tests |
| 49 | |
| 50 | static SkCanvas::EdgeType paint2EdgeType(const SkPaint* paint) { |
| 51 | return paint != NULL && paint->isAntiAlias() ? |
| 52 | SkCanvas::kAA_EdgeType : SkCanvas::kBW_EdgeType; |
| 53 | } |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | /* This is the record we keep for each SkDevice that the user installs. |
| 58 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 59 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 60 | these are used (assuming we're not on a layer) we rebuild these cache |
| 61 | values: they reflect the top of the save stack, but translated and clipped |
| 62 | by the device's XY offset and bitmap-bounds. |
| 63 | */ |
| 64 | struct DeviceCM { |
| 65 | DeviceCM* fNext; |
| 66 | SkDevice* fDevice; |
| 67 | SkRegion fClip; |
| 68 | const SkMatrix* fMatrix; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 69 | SkPaint* fPaint; // may be null (in the future) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 70 | // optional, related to canvas' external matrix |
| 71 | const SkMatrix* fMVMatrix; |
| 72 | const SkMatrix* fExtMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 74 | DeviceCM(SkDevice* device, int x, int y, const SkPaint* paint) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | : fNext(NULL) { |
| 76 | if (NULL != device) { |
| 77 | device->ref(); |
| 78 | device->lockPixels(); |
| 79 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 80 | fDevice = device; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | fPaint = paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 82 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 84 | ~DeviceCM() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | if (NULL != fDevice) { |
| 86 | fDevice->unlockPixels(); |
| 87 | fDevice->unref(); |
| 88 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 89 | SkDELETE(fPaint); |
| 90 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 91 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | void updateMC(const SkMatrix& totalMatrix, const SkRegion& totalClip, |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 93 | const SkClipStack& clipStack, SkRegion* updateClip) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 94 | int x = fDevice->getOrigin().x(); |
| 95 | int y = fDevice->getOrigin().y(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | int width = fDevice->width(); |
| 97 | int height = fDevice->height(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | if ((x | y) == 0) { |
| 100 | fMatrix = &totalMatrix; |
| 101 | fClip = totalClip; |
| 102 | } else { |
| 103 | fMatrixStorage = totalMatrix; |
| 104 | fMatrixStorage.postTranslate(SkIntToScalar(-x), |
| 105 | SkIntToScalar(-y)); |
| 106 | fMatrix = &fMatrixStorage; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 107 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | totalClip.translate(-x, -y, &fClip); |
| 109 | } |
| 110 | |
| 111 | fClip.op(0, 0, width, height, SkRegion::kIntersect_Op); |
| 112 | |
| 113 | // intersect clip, but don't translate it (yet) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 114 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | if (updateClip) { |
| 116 | updateClip->op(x, y, x + width, y + height, |
| 117 | SkRegion::kDifference_Op); |
| 118 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 119 | |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 120 | fDevice->setMatrixClip(*fMatrix, fClip, clipStack); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
| 122 | #ifdef SK_DEBUG |
| 123 | if (!fClip.isEmpty()) { |
| 124 | SkIRect deviceR; |
| 125 | deviceR.set(0, 0, width, height); |
| 126 | SkASSERT(deviceR.contains(fClip.getBounds())); |
| 127 | } |
| 128 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 129 | // default is to assume no external matrix |
| 130 | fMVMatrix = NULL; |
| 131 | fExtMatrix = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 133 | |
| 134 | // can only be called after calling updateMC() |
| 135 | void updateExternalMatrix(const SkMatrix& extM, const SkMatrix& extI) { |
| 136 | fMVMatrixStorage.setConcat(extI, *fMatrix); |
| 137 | fMVMatrix = &fMVMatrixStorage; |
| 138 | fExtMatrix = &extM; // assumes extM has long life-time (owned by canvas) |
| 139 | } |
| 140 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | private: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 142 | SkMatrix fMatrixStorage, fMVMatrixStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /* This is the record we keep for each save/restore level in the stack. |
| 146 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 147 | for these fields. If the value is copied for this level, the copy is |
| 148 | stored in the ...Storage field, and the pointer points to that. If the |
| 149 | value is not copied for this level, we ignore ...Storage, and just point |
| 150 | at the corresponding value in the previous level in the stack. |
| 151 | */ |
| 152 | class SkCanvas::MCRec { |
| 153 | public: |
| 154 | MCRec* fNext; |
| 155 | SkMatrix* fMatrix; // points to either fMatrixStorage or prev MCRec |
| 156 | SkRegion* fRegion; // points to either fRegionStorage or prev MCRec |
| 157 | SkDrawFilter* fFilter; // the current filter (or null) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | DeviceCM* fLayer; |
| 160 | /* If there are any layers in the stack, this points to the top-most |
| 161 | one that is at or below this level in the stack (so we know what |
| 162 | bitmap/device to draw into from this level. This value is NOT |
| 163 | reference counted, since the real owner is either our fLayer field, |
| 164 | or a previous one in a lower level.) |
| 165 | */ |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 166 | DeviceCM* fTopLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | |
| 168 | MCRec(const MCRec* prev, int flags) { |
| 169 | if (NULL != prev) { |
| 170 | if (flags & SkCanvas::kMatrix_SaveFlag) { |
| 171 | fMatrixStorage = *prev->fMatrix; |
| 172 | fMatrix = &fMatrixStorage; |
| 173 | } else { |
| 174 | fMatrix = prev->fMatrix; |
| 175 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | if (flags & SkCanvas::kClip_SaveFlag) { |
| 178 | fRegionStorage = *prev->fRegion; |
| 179 | fRegion = &fRegionStorage; |
| 180 | } else { |
| 181 | fRegion = prev->fRegion; |
| 182 | } |
| 183 | |
| 184 | fFilter = prev->fFilter; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 185 | SkSafeRef(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | |
| 187 | fTopLayer = prev->fTopLayer; |
| 188 | } else { // no prev |
| 189 | fMatrixStorage.reset(); |
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 | fMatrix = &fMatrixStorage; |
| 192 | fRegion = &fRegionStorage; |
| 193 | fFilter = NULL; |
| 194 | fTopLayer = NULL; |
| 195 | } |
| 196 | fLayer = NULL; |
| 197 | |
| 198 | // don't bother initializing fNext |
| 199 | inc_rec(); |
| 200 | } |
| 201 | ~MCRec() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 202 | SkSafeUnref(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | SkDELETE(fLayer); |
| 204 | dec_rec(); |
| 205 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 206 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | private: |
| 208 | SkMatrix fMatrixStorage; |
| 209 | SkRegion fRegionStorage; |
| 210 | }; |
| 211 | |
| 212 | class SkDrawIter : public SkDraw { |
| 213 | public: |
| 214 | SkDrawIter(SkCanvas* canvas, bool skipEmptyClips = true) { |
| 215 | fCanvas = canvas; |
| 216 | canvas->updateDeviceCMCache(); |
| 217 | |
reed@google.com | 7d7ca79 | 2011-02-23 22:39:18 +0000 | [diff] [blame] | 218 | fClipStack = &canvas->getTotalClipStack(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | fBounder = canvas->getBounder(); |
| 220 | fCurrLayer = canvas->fMCRec->fTopLayer; |
| 221 | fSkipEmptyClips = skipEmptyClips; |
| 222 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 223 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | bool next() { |
| 225 | // skip over recs with empty clips |
| 226 | if (fSkipEmptyClips) { |
| 227 | while (fCurrLayer && fCurrLayer->fClip.isEmpty()) { |
| 228 | fCurrLayer = fCurrLayer->fNext; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (NULL != fCurrLayer) { |
| 233 | const DeviceCM* rec = fCurrLayer; |
| 234 | |
| 235 | fMatrix = rec->fMatrix; |
| 236 | fClip = &rec->fClip; |
| 237 | fDevice = rec->fDevice; |
| 238 | fBitmap = &fDevice->accessBitmap(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | fPaint = rec->fPaint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 240 | fMVMatrix = rec->fMVMatrix; |
| 241 | fExtMatrix = rec->fExtMatrix; |
| 242 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | |
| 244 | fCurrLayer = rec->fNext; |
| 245 | if (fBounder) { |
| 246 | fBounder->setClip(fClip); |
| 247 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | // fCurrLayer may be NULL now |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 249 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 250 | fCanvas->prepareForDeviceDraw(fDevice, *fMatrix, *fClip, *fClipStack); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | return false; |
| 254 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 255 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | SkDevice* getDevice() const { return fDevice; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 257 | int getX() const { return fDevice->getOrigin().x(); } |
| 258 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | const SkMatrix& getMatrix() const { return *fMatrix; } |
| 260 | const SkRegion& getClip() const { return *fClip; } |
| 261 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 262 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | private: |
| 264 | SkCanvas* fCanvas; |
| 265 | const DeviceCM* fCurrLayer; |
| 266 | const SkPaint* fPaint; // May be null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | SkBool8 fSkipEmptyClips; |
| 268 | |
| 269 | typedef SkDraw INHERITED; |
| 270 | }; |
| 271 | |
| 272 | ///////////////////////////////////////////////////////////////////////////// |
| 273 | |
| 274 | class AutoDrawLooper { |
| 275 | public: |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 276 | AutoDrawLooper(SkCanvas* canvas, const SkPaint& paint) : fOrigPaint(paint) { |
| 277 | fCanvas = canvas; |
| 278 | fLooper = paint.getLooper(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | fFilter = canvas->getDrawFilter(); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 280 | fPaint = NULL; |
| 281 | fSaveCount = canvas->getSaveCount(); |
| 282 | fDone = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 284 | if (fLooper) { |
| 285 | fLooper->init(canvas); |
| 286 | } |
| 287 | } |
| 288 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 289 | ~AutoDrawLooper() { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 290 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 292 | |
| 293 | const SkPaint& paint() const { |
| 294 | SkASSERT(fPaint); |
| 295 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 297 | |
| 298 | bool next(SkDrawFilter::Type drawType); |
| 299 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 301 | SkLazyPaint fLazyPaint; |
| 302 | SkCanvas* fCanvas; |
| 303 | const SkPaint& fOrigPaint; |
| 304 | SkDrawLooper* fLooper; |
| 305 | SkDrawFilter* fFilter; |
| 306 | const SkPaint* fPaint; |
| 307 | int fSaveCount; |
| 308 | bool fDone; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | }; |
| 310 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 311 | bool AutoDrawLooper::next(SkDrawFilter::Type drawType) { |
| 312 | if (fDone) { |
| 313 | fPaint = NULL; |
| 314 | return false; |
| 315 | } |
| 316 | if (!fLooper && !fFilter) { |
| 317 | fDone = true; |
| 318 | fPaint = &fOrigPaint; |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | SkPaint* paint = fLazyPaint.set(fOrigPaint); |
| 323 | if (fLooper && !fLooper->next(fCanvas, paint)) { |
| 324 | fDone = true; |
| 325 | fPaint = NULL; |
| 326 | return false; |
| 327 | } |
| 328 | if (fFilter) { |
| 329 | fFilter->filter(paint, drawType); |
mike@reedtribe.org | 53e3bed | 2011-04-08 00:37:03 +0000 | [diff] [blame] | 330 | if (NULL == fLooper) { |
| 331 | // no looper means we only draw once |
| 332 | fDone = true; |
| 333 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 334 | } |
| 335 | fPaint = paint; |
| 336 | return true; |
| 337 | } |
| 338 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | /* Stack helper for managing a SkBounder. In the destructor, if we were |
| 340 | given a bounder, we call its commit() method, signifying that we are |
| 341 | done accumulating bounds for that draw. |
| 342 | */ |
| 343 | class SkAutoBounderCommit { |
| 344 | public: |
| 345 | SkAutoBounderCommit(SkBounder* bounder) : fBounder(bounder) {} |
| 346 | ~SkAutoBounderCommit() { |
| 347 | if (NULL != fBounder) { |
| 348 | fBounder->commit(); |
| 349 | } |
| 350 | } |
| 351 | private: |
| 352 | SkBounder* fBounder; |
| 353 | }; |
| 354 | |
| 355 | #include "SkColorPriv.h" |
| 356 | |
| 357 | class AutoValidator { |
| 358 | public: |
| 359 | AutoValidator(SkDevice* device) : fDevice(device) {} |
| 360 | ~AutoValidator() { |
| 361 | #ifdef SK_DEBUG |
| 362 | const SkBitmap& bm = fDevice->accessBitmap(false); |
| 363 | if (bm.config() == SkBitmap::kARGB_4444_Config) { |
| 364 | for (int y = 0; y < bm.height(); y++) { |
| 365 | const SkPMColor16* p = bm.getAddr16(0, y); |
| 366 | for (int x = 0; x < bm.width(); x++) { |
| 367 | SkPMColor16 c = p[x]; |
| 368 | SkPMColor16Assert(c); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | #endif |
| 373 | } |
| 374 | private: |
| 375 | SkDevice* fDevice; |
| 376 | }; |
| 377 | |
| 378 | ////////// macros to place around the internal draw calls ////////////////// |
| 379 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 380 | #define LOOPER_BEGIN(paint, type) \ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | /* AutoValidator validator(fMCRec->fTopLayer->fDevice); */ \ |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 382 | AutoDrawLooper looper(this, paint); \ |
| 383 | while (looper.next(type)) { \ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 384 | SkAutoBounderCommit ac(fBounder); \ |
| 385 | SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 386 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 387 | #define LOOPER_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | |
| 389 | //////////////////////////////////////////////////////////////////////////// |
| 390 | |
| 391 | SkDevice* SkCanvas::init(SkDevice* device) { |
| 392 | fBounder = NULL; |
vandebo@chromium.org | 3c89818 | 2011-06-22 05:01:28 +0000 | [diff] [blame] | 393 | fLocalBoundsCompareType.setEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | fLocalBoundsCompareTypeDirty = true; |
vandebo@chromium.org | 3c89818 | 2011-06-22 05:01:28 +0000 | [diff] [blame] | 395 | fLocalBoundsCompareTypeBW.setEmpty(); |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 396 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 397 | fLastDeviceToGainFocus = NULL; |
agl@chromium.org | 447bcfa | 2009-12-12 00:06:17 +0000 | [diff] [blame] | 398 | fDeviceCMDirty = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 399 | |
| 400 | fMCRec = (MCRec*)fMCStack.push_back(); |
| 401 | new (fMCRec) MCRec(NULL, 0); |
| 402 | |
| 403 | fMCRec->fLayer = SkNEW_ARGS(DeviceCM, (NULL, 0, 0, NULL)); |
| 404 | fMCRec->fTopLayer = fMCRec->fLayer; |
| 405 | fMCRec->fNext = NULL; |
| 406 | |
vandebo@chromium.org | 3c89818 | 2011-06-22 05:01:28 +0000 | [diff] [blame] | 407 | fExternalMatrix.reset(); |
| 408 | fExternalInverse.reset(); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 409 | fUseExternalMatrix = false; |
| 410 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 411 | return this->setDevice(device); |
| 412 | } |
| 413 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 414 | SkCanvas::SkCanvas() |
| 415 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 416 | inc_canvas(); |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 417 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 418 | this->init(NULL); |
| 419 | } |
| 420 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 421 | SkCanvas::SkCanvas(SkDevice* device) |
mike@reedtribe.org | ea4ac97 | 2011-04-26 11:48:33 +0000 | [diff] [blame] | 422 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 423 | inc_canvas(); |
| 424 | |
| 425 | this->init(device); |
| 426 | } |
| 427 | |
| 428 | SkCanvas::SkCanvas(const SkBitmap& bitmap) |
| 429 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) { |
| 430 | inc_canvas(); |
| 431 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 432 | this->init(SkNEW_ARGS(SkDevice, (bitmap)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | SkCanvas::~SkCanvas() { |
| 436 | // free up the contents of our deque |
| 437 | this->restoreToCount(1); // restore everything but the last |
| 438 | this->internalRestore(); // restore the last, since we're going away |
| 439 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 440 | SkSafeUnref(fBounder); |
vandebo@chromium.org | b70ae31 | 2010-10-15 18:58:19 +0000 | [diff] [blame] | 441 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | dec_canvas(); |
| 443 | } |
| 444 | |
| 445 | SkBounder* SkCanvas::setBounder(SkBounder* bounder) { |
| 446 | SkRefCnt_SafeAssign(fBounder, bounder); |
| 447 | return bounder; |
| 448 | } |
| 449 | |
| 450 | SkDrawFilter* SkCanvas::getDrawFilter() const { |
| 451 | return fMCRec->fFilter; |
| 452 | } |
| 453 | |
| 454 | SkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) { |
| 455 | SkRefCnt_SafeAssign(fMCRec->fFilter, filter); |
| 456 | return filter; |
| 457 | } |
| 458 | |
| 459 | /////////////////////////////////////////////////////////////////////////////// |
| 460 | |
| 461 | SkDevice* SkCanvas::getDevice() const { |
| 462 | // return root device |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 463 | SkDeque::F2BIter iter(fMCStack); |
| 464 | MCRec* rec = (MCRec*)iter.next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 465 | SkASSERT(rec && rec->fLayer); |
| 466 | return rec->fLayer->fDevice; |
| 467 | } |
| 468 | |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 469 | SkDevice* SkCanvas::getTopDevice() const { |
| 470 | return fMCRec->fTopLayer->fDevice; |
| 471 | } |
| 472 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 473 | SkDevice* SkCanvas::setDevice(SkDevice* device) { |
| 474 | // return root device |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 475 | SkDeque::F2BIter iter(fMCStack); |
| 476 | MCRec* rec = (MCRec*)iter.next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | SkASSERT(rec && rec->fLayer); |
| 478 | SkDevice* rootDevice = rec->fLayer->fDevice; |
| 479 | |
| 480 | if (rootDevice == device) { |
| 481 | return device; |
| 482 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 483 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | /* Notify the devices that they are going in/out of scope, so they can do |
| 485 | things like lock/unlock their pixels, etc. |
| 486 | */ |
| 487 | if (device) { |
| 488 | device->lockPixels(); |
| 489 | } |
| 490 | if (rootDevice) { |
| 491 | rootDevice->unlockPixels(); |
| 492 | } |
| 493 | |
| 494 | SkRefCnt_SafeAssign(rec->fLayer->fDevice, device); |
| 495 | rootDevice = device; |
| 496 | |
| 497 | fDeviceCMDirty = true; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 498 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | /* Now we update our initial region to have the bounds of the new device, |
| 500 | and then intersect all of the clips in our stack with these bounds, |
| 501 | to ensure that we can't draw outside of the device's bounds (and trash |
| 502 | memory). |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 503 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 504 | NOTE: this is only a partial-fix, since if the new device is larger than |
| 505 | 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] | 506 | so drawing may be artificially restricted. Without keeping a history of |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 507 | all calls to canvas->clipRect() and canvas->clipPath(), we can't exactly |
| 508 | reconstruct the correct clips, so this approximation will have to do. |
| 509 | The caller really needs to restore() back to the base if they want to |
| 510 | accurately take advantage of the new device bounds. |
| 511 | */ |
| 512 | |
| 513 | if (NULL == device) { |
| 514 | rec->fRegion->setEmpty(); |
| 515 | while ((rec = (MCRec*)iter.next()) != NULL) { |
| 516 | (void)rec->fRegion->setEmpty(); |
| 517 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 518 | fClipStack.reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | } else { |
| 520 | // compute our total bounds for all devices |
| 521 | SkIRect bounds; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 522 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | bounds.set(0, 0, device->width(), device->height()); |
| 524 | |
| 525 | // now jam our 1st clip to be bounds, and intersect the rest with that |
| 526 | rec->fRegion->setRect(bounds); |
| 527 | while ((rec = (MCRec*)iter.next()) != NULL) { |
| 528 | (void)rec->fRegion->op(bounds, SkRegion::kIntersect_Op); |
| 529 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 530 | fClipStack.clipDevRect(bounds, SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 531 | } |
| 532 | return device; |
| 533 | } |
| 534 | |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 535 | SkDevice* SkCanvas::setBitmapDevice(const SkBitmap& bitmap) { |
| 536 | SkDevice* device = this->setDevice(SkNEW_ARGS(SkDevice, (bitmap))); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | device->unref(); |
| 538 | return device; |
| 539 | } |
| 540 | |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 541 | bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
| 542 | SkDevice* device = this->getDevice(); |
| 543 | if (!device) { |
| 544 | return false; |
| 545 | } |
| 546 | return device->readPixels(srcRect, bitmap); |
| 547 | } |
| 548 | |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 549 | ////////////////////////////////////////////////////////////////////////////// |
| 550 | |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 551 | bool SkCanvas::readPixels(SkBitmap* bitmap) { |
| 552 | SkDevice* device = this->getDevice(); |
| 553 | if (!device) { |
| 554 | return false; |
| 555 | } |
| 556 | SkIRect bounds; |
| 557 | bounds.set(0, 0, device->width(), device->height()); |
| 558 | return this->readPixels(bounds, bitmap); |
| 559 | } |
| 560 | |
| 561 | void SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) { |
| 562 | SkDevice* device = this->getDevice(); |
| 563 | if (device) { |
| 564 | device->writePixels(bitmap, x, y); |
| 565 | } |
| 566 | } |
| 567 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 568 | ////////////////////////////////////////////////////////////////////////////// |
| 569 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 570 | void SkCanvas::updateDeviceCMCache() { |
| 571 | if (fDeviceCMDirty) { |
| 572 | const SkMatrix& totalMatrix = this->getTotalMatrix(); |
| 573 | const SkRegion& totalClip = this->getTotalClip(); |
| 574 | DeviceCM* layer = fMCRec->fTopLayer; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 575 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 576 | if (NULL == layer->fNext) { // only one layer |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 577 | layer->updateMC(totalMatrix, totalClip, fClipStack, NULL); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 578 | if (fUseExternalMatrix) { |
| 579 | layer->updateExternalMatrix(fExternalMatrix, |
| 580 | fExternalInverse); |
| 581 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 582 | } else { |
| 583 | SkRegion clip; |
| 584 | clip = totalClip; // make a copy |
| 585 | do { |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 586 | layer->updateMC(totalMatrix, clip, fClipStack, &clip); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 587 | if (fUseExternalMatrix) { |
| 588 | layer->updateExternalMatrix(fExternalMatrix, |
| 589 | fExternalInverse); |
| 590 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 591 | } while ((layer = layer->fNext) != NULL); |
| 592 | } |
| 593 | fDeviceCMDirty = false; |
| 594 | } |
| 595 | } |
| 596 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 597 | void SkCanvas::prepareForDeviceDraw(SkDevice* device, const SkMatrix& matrix, |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 598 | const SkRegion& clip, |
| 599 | const SkClipStack& clipStack) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 600 | SkASSERT(device); |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 601 | if (fLastDeviceToGainFocus != device) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 602 | device->gainFocus(this, matrix, clip, clipStack); |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 603 | fLastDeviceToGainFocus = device; |
| 604 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /////////////////////////////////////////////////////////////////////////////// |
| 608 | |
| 609 | int SkCanvas::internalSave(SaveFlags flags) { |
| 610 | int saveCount = this->getSaveCount(); // record this before the actual save |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 611 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 612 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
| 613 | new (newTop) MCRec(fMCRec, flags); // balanced in restore() |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 614 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 615 | newTop->fNext = fMCRec; |
| 616 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 617 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 618 | fClipStack.save(); |
| 619 | SkASSERT(fClipStack.getSaveCount() == this->getSaveCount() - 1); |
| 620 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 621 | return saveCount; |
| 622 | } |
| 623 | |
| 624 | int SkCanvas::save(SaveFlags flags) { |
| 625 | // call shared impl |
| 626 | return this->internalSave(flags); |
| 627 | } |
| 628 | |
| 629 | #define C32MASK (1 << SkBitmap::kARGB_8888_Config) |
| 630 | #define C16MASK (1 << SkBitmap::kRGB_565_Config) |
| 631 | #define C8MASK (1 << SkBitmap::kA8_Config) |
| 632 | |
| 633 | static SkBitmap::Config resolve_config(SkCanvas* canvas, |
| 634 | const SkIRect& bounds, |
| 635 | SkCanvas::SaveFlags flags, |
| 636 | bool* isOpaque) { |
| 637 | *isOpaque = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) == 0; |
| 638 | |
| 639 | #if 0 |
| 640 | // loop through and union all the configs we may draw into |
| 641 | uint32_t configMask = 0; |
| 642 | for (int i = canvas->countLayerDevices() - 1; i >= 0; --i) |
| 643 | { |
| 644 | SkDevice* device = canvas->getLayerDevice(i); |
| 645 | if (device->intersects(bounds)) |
| 646 | configMask |= 1 << device->config(); |
| 647 | } |
| 648 | |
| 649 | // if the caller wants alpha or fullcolor, we can't return 565 |
| 650 | if (flags & (SkCanvas::kFullColorLayer_SaveFlag | |
| 651 | SkCanvas::kHasAlphaLayer_SaveFlag)) |
| 652 | configMask &= ~C16MASK; |
| 653 | |
| 654 | switch (configMask) { |
| 655 | case C8MASK: // if we only have A8, return that |
| 656 | return SkBitmap::kA8_Config; |
| 657 | |
| 658 | case C16MASK: // if we only have 565, return that |
| 659 | return SkBitmap::kRGB_565_Config; |
| 660 | |
| 661 | default: |
| 662 | return SkBitmap::kARGB_8888_Config; // default answer |
| 663 | } |
| 664 | #else |
| 665 | return SkBitmap::kARGB_8888_Config; // default answer |
| 666 | #endif |
| 667 | } |
| 668 | |
| 669 | static bool bounds_affects_clip(SkCanvas::SaveFlags flags) { |
| 670 | return (flags & SkCanvas::kClipToLayer_SaveFlag) != 0; |
| 671 | } |
| 672 | |
| 673 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 674 | SaveFlags flags) { |
| 675 | // do this before we create the layer. We don't call the public save() since |
| 676 | // that would invoke a possibly overridden virtual |
| 677 | int count = this->internalSave(flags); |
| 678 | |
| 679 | fDeviceCMDirty = true; |
| 680 | |
| 681 | SkIRect ir; |
| 682 | const SkIRect& clipBounds = this->getTotalClip().getBounds(); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 683 | if (clipBounds.isEmpty()) { |
| 684 | return count; |
| 685 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 686 | |
| 687 | if (NULL != bounds) { |
| 688 | SkRect r; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 689 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 690 | this->getTotalMatrix().mapRect(&r, *bounds); |
| 691 | r.roundOut(&ir); |
| 692 | // early exit if the layer's bounds are clipped out |
| 693 | if (!ir.intersect(clipBounds)) { |
| 694 | if (bounds_affects_clip(flags)) |
| 695 | fMCRec->fRegion->setEmpty(); |
| 696 | return count; |
| 697 | } |
| 698 | } else { // no user bounds, so just use the clip |
| 699 | ir = clipBounds; |
| 700 | } |
| 701 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 702 | fClipStack.clipDevRect(ir, SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 703 | // early exit if the clip is now empty |
| 704 | if (bounds_affects_clip(flags) && |
| 705 | !fMCRec->fRegion->op(ir, SkRegion::kIntersect_Op)) { |
| 706 | return count; |
| 707 | } |
| 708 | |
| 709 | bool isOpaque; |
| 710 | SkBitmap::Config config = resolve_config(this, ir, flags, &isOpaque); |
| 711 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 712 | SkDevice* device = this->createLayerDevice(config, ir.width(), ir.height(), |
| 713 | isOpaque); |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame^] | 714 | if (NULL == device) { |
| 715 | SkDebugf("Unable to create device for layer."); |
| 716 | return count; |
| 717 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 718 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 719 | device->setOrigin(ir.fLeft, ir.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 720 | DeviceCM* layer = SkNEW_ARGS(DeviceCM, (device, ir.fLeft, ir.fTop, paint)); |
| 721 | device->unref(); |
| 722 | |
| 723 | layer->fNext = fMCRec->fTopLayer; |
| 724 | fMCRec->fLayer = layer; |
| 725 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
| 726 | |
| 727 | return count; |
| 728 | } |
| 729 | |
| 730 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha, |
| 731 | SaveFlags flags) { |
| 732 | if (0xFF == alpha) { |
| 733 | return this->saveLayer(bounds, NULL, flags); |
| 734 | } else { |
| 735 | SkPaint tmpPaint; |
| 736 | tmpPaint.setAlpha(alpha); |
| 737 | return this->saveLayer(bounds, &tmpPaint, flags); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void SkCanvas::restore() { |
| 742 | // check for underflow |
| 743 | if (fMCStack.count() > 1) { |
| 744 | this->internalRestore(); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | void SkCanvas::internalRestore() { |
| 749 | SkASSERT(fMCStack.count() != 0); |
| 750 | |
| 751 | fDeviceCMDirty = true; |
| 752 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 753 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 754 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 755 | fClipStack.restore(); |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 756 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 757 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 758 | // now detach it from fMCRec so we can pop(). Gets freed after its drawn |
| 759 | fMCRec->fLayer = NULL; |
| 760 | |
| 761 | // now do the normal restore() |
| 762 | fMCRec->~MCRec(); // balanced in save() |
| 763 | fMCStack.pop_back(); |
| 764 | fMCRec = (MCRec*)fMCStack.back(); |
| 765 | |
| 766 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 767 | since if we're being recorded, we don't want to record this (the |
| 768 | recorder will have already recorded the restore). |
| 769 | */ |
| 770 | if (NULL != layer) { |
| 771 | if (layer->fNext) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 772 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
| 773 | this->drawDevice(layer->fDevice, origin.x(), origin.y(), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 774 | layer->fPaint); |
| 775 | // reset this, since drawDevice will have set it to true |
| 776 | fDeviceCMDirty = true; |
| 777 | } |
| 778 | SkDELETE(layer); |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 779 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 780 | |
| 781 | SkASSERT(fClipStack.getSaveCount() == this->getSaveCount() - 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | int SkCanvas::getSaveCount() const { |
| 785 | return fMCStack.count(); |
| 786 | } |
| 787 | |
| 788 | void SkCanvas::restoreToCount(int count) { |
| 789 | // sanity check |
| 790 | if (count < 1) { |
| 791 | count = 1; |
| 792 | } |
| 793 | while (fMCStack.count() > count) { |
| 794 | this->restore(); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | ///////////////////////////////////////////////////////////////////////////// |
| 799 | |
| 800 | // can't draw it if its empty, or its too big for a fixed-point width or height |
| 801 | static bool reject_bitmap(const SkBitmap& bitmap) { |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 802 | return bitmap.width() <= 0 || bitmap.height() <= 0 |
| 803 | #ifndef SK_ALLOW_OVER_32K_BITMAPS |
| 804 | || bitmap.width() > 32767 || bitmap.height() > 32767 |
| 805 | #endif |
| 806 | ; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 807 | } |
| 808 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 809 | void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, const SkIRect* srcRect, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 810 | const SkMatrix& matrix, const SkPaint* paint) { |
| 811 | if (reject_bitmap(bitmap)) { |
| 812 | return; |
| 813 | } |
| 814 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 815 | SkLazyPaint lazy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | if (NULL == paint) { |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 817 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | } |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 819 | this->commonDrawBitmap(bitmap, srcRect, matrix, *paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | void SkCanvas::drawDevice(SkDevice* device, int x, int y, |
| 823 | const SkPaint* paint) { |
| 824 | SkPaint tmp; |
| 825 | if (NULL == paint) { |
| 826 | tmp.setDither(true); |
| 827 | paint = &tmp; |
| 828 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 829 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 830 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 831 | while (iter.next()) { |
| 832 | iter.fDevice->drawDevice(iter, device, x - iter.getX(), y - iter.getY(), |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 833 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 834 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 835 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | ///////////////////////////////////////////////////////////////////////////// |
| 839 | |
| 840 | bool SkCanvas::translate(SkScalar dx, SkScalar dy) { |
| 841 | fDeviceCMDirty = true; |
| 842 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 843 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 844 | return fMCRec->fMatrix->preTranslate(dx, dy); |
| 845 | } |
| 846 | |
| 847 | bool SkCanvas::scale(SkScalar sx, SkScalar sy) { |
| 848 | fDeviceCMDirty = true; |
| 849 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 850 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 851 | return fMCRec->fMatrix->preScale(sx, sy); |
| 852 | } |
| 853 | |
| 854 | bool SkCanvas::rotate(SkScalar degrees) { |
| 855 | fDeviceCMDirty = true; |
| 856 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 857 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 858 | return fMCRec->fMatrix->preRotate(degrees); |
| 859 | } |
| 860 | |
| 861 | bool SkCanvas::skew(SkScalar sx, SkScalar sy) { |
| 862 | fDeviceCMDirty = true; |
| 863 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 864 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 865 | return fMCRec->fMatrix->preSkew(sx, sy); |
| 866 | } |
| 867 | |
| 868 | bool SkCanvas::concat(const SkMatrix& matrix) { |
| 869 | fDeviceCMDirty = true; |
| 870 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 871 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 872 | return fMCRec->fMatrix->preConcat(matrix); |
| 873 | } |
| 874 | |
| 875 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 876 | fDeviceCMDirty = true; |
| 877 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 878 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 879 | *fMCRec->fMatrix = matrix; |
| 880 | } |
| 881 | |
| 882 | // this is not virtual, so it must call a virtual method so that subclasses |
| 883 | // will see its action |
| 884 | void SkCanvas::resetMatrix() { |
| 885 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 886 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 887 | matrix.reset(); |
| 888 | this->setMatrix(matrix); |
| 889 | } |
| 890 | |
| 891 | ////////////////////////////////////////////////////////////////////////////// |
| 892 | |
| 893 | bool SkCanvas::clipRect(const SkRect& rect, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 894 | AutoValidateClip avc(this); |
| 895 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 896 | fDeviceCMDirty = true; |
| 897 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 898 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 899 | |
| 900 | if (fMCRec->fMatrix->rectStaysRect()) { |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 901 | // for these simpler matrices, we can stay a rect ever after applying |
| 902 | // the matrix. This means we don't have to a) make a path, and b) tell |
| 903 | // the region code to scan-convert the path, only to discover that it |
| 904 | // is really just a rect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 905 | SkRect r; |
| 906 | SkIRect ir; |
| 907 | |
| 908 | fMCRec->fMatrix->mapRect(&r, rect); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 909 | fClipStack.clipDevRect(r, op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 910 | r.round(&ir); |
| 911 | return fMCRec->fRegion->op(ir, op); |
| 912 | } else { |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 913 | // since we're rotate or some such thing, we convert the rect to a path |
| 914 | // and clip against that, since it can handle any matrix. However, to |
| 915 | // avoid recursion in the case where we are subclassed (e.g. Pictures) |
| 916 | // we explicitly call "our" version of clipPath. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 917 | SkPath path; |
| 918 | |
| 919 | path.addRect(rect); |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 920 | return this->SkCanvas::clipPath(path, op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 924 | static bool clipPathHelper(const SkCanvas* canvas, SkRegion* currRgn, |
| 925 | const SkPath& devPath, SkRegion::Op op) { |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 926 | // base is used to limit the size (and therefore memory allocation) of the |
| 927 | // region that results from scan converting devPath. |
| 928 | SkRegion base; |
| 929 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 930 | if (SkRegion::kIntersect_Op == op) { |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 931 | // since we are intersect, we can do better (tighter) with currRgn's |
| 932 | // bounds, than just using the device. However, if currRgn is complex, |
| 933 | // our region blitter may hork, so we do that case in two steps. |
| 934 | if (currRgn->isRect()) { |
| 935 | return currRgn->setPath(devPath, *currRgn); |
| 936 | } else { |
| 937 | base.setRect(currRgn->getBounds()); |
| 938 | SkRegion rgn; |
| 939 | rgn.setPath(devPath, base); |
| 940 | return currRgn->op(rgn, op); |
| 941 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 942 | } else { |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 943 | const SkBitmap& bm = canvas->getDevice()->accessBitmap(false); |
| 944 | base.setRect(0, 0, bm.width(), bm.height()); |
| 945 | |
| 946 | if (SkRegion::kReplace_Op == op) { |
| 947 | return currRgn->setPath(devPath, base); |
| 948 | } else { |
| 949 | SkRegion rgn; |
| 950 | rgn.setPath(devPath, base); |
| 951 | return currRgn->op(rgn, op); |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 956 | bool SkCanvas::clipPath(const SkPath& path, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 957 | AutoValidateClip avc(this); |
| 958 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 959 | fDeviceCMDirty = true; |
| 960 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 961 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 962 | |
| 963 | SkPath devPath; |
| 964 | path.transform(*fMCRec->fMatrix, &devPath); |
| 965 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 966 | // if we called path.swap() we could avoid a deep copy of this path |
| 967 | fClipStack.clipDevPath(devPath, op); |
| 968 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 969 | return clipPathHelper(this, fMCRec->fRegion, devPath, op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | bool SkCanvas::clipRegion(const SkRegion& rgn, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 973 | AutoValidateClip avc(this); |
| 974 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 975 | fDeviceCMDirty = true; |
| 976 | fLocalBoundsCompareTypeDirty = true; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 977 | fLocalBoundsCompareTypeDirtyBW = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 978 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 979 | // todo: signal fClipStack that we have a region, and therefore (I guess) |
| 980 | // we have to ignore it, and use the region directly? |
| 981 | fClipStack.clipDevRect(rgn.getBounds()); |
| 982 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 983 | return fMCRec->fRegion->op(rgn, op); |
| 984 | } |
| 985 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 986 | #ifdef SK_DEBUG |
| 987 | void SkCanvas::validateClip() const { |
| 988 | // construct clipRgn from the clipstack |
| 989 | const SkDevice* device = this->getDevice(); |
| 990 | SkIRect ir; |
| 991 | ir.set(0, 0, device->width(), device->height()); |
| 992 | SkRegion clipRgn(ir); |
| 993 | |
| 994 | SkClipStack::B2FIter iter(fClipStack); |
| 995 | const SkClipStack::B2FIter::Clip* clip; |
| 996 | while ((clip = iter.next()) != NULL) { |
| 997 | if (clip->fPath) { |
| 998 | clipPathHelper(this, &clipRgn, *clip->fPath, clip->fOp); |
| 999 | } else if (clip->fRect) { |
| 1000 | clip->fRect->round(&ir); |
| 1001 | clipRgn.op(ir, clip->fOp); |
| 1002 | } else { |
reed@google.com | dca7acb | 2011-02-23 21:47:37 +0000 | [diff] [blame] | 1003 | clipRgn.setEmpty(); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 1007 | #if 0 // enable this locally for testing |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1008 | // now compare against the current rgn |
| 1009 | const SkRegion& rgn = this->getTotalClip(); |
| 1010 | SkASSERT(rgn == clipRgn); |
reed@google.com | 02878b8 | 2011-02-24 13:04:42 +0000 | [diff] [blame] | 1011 | #endif |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1012 | } |
| 1013 | #endif |
| 1014 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1015 | /////////////////////////////////////////////////////////////////////////////// |
| 1016 | |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1017 | void SkCanvas::computeLocalClipBoundsCompareType(EdgeType et) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1018 | SkRect r; |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1019 | SkRectCompareType& rCompare = et == kAA_EdgeType ? fLocalBoundsCompareType : |
| 1020 | fLocalBoundsCompareTypeBW; |
| 1021 | |
| 1022 | if (!this->getClipBounds(&r, et)) { |
| 1023 | rCompare.setEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1024 | } else { |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1025 | rCompare.set(SkScalarToCompareType(r.fLeft), |
| 1026 | SkScalarToCompareType(r.fTop), |
| 1027 | SkScalarToCompareType(r.fRight), |
| 1028 | SkScalarToCompareType(r.fBottom)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1032 | /* current impl ignores edgetype, and relies on |
| 1033 | getLocalClipBoundsCompareType(), which always returns a value assuming |
| 1034 | antialiasing (worst case) |
| 1035 | */ |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1036 | bool SkCanvas::quickReject(const SkRect& rect, EdgeType et) const { |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1037 | |
| 1038 | if (!rect.hasValidCoordinates()) |
| 1039 | return true; |
| 1040 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1041 | if (fMCRec->fRegion->isEmpty()) { |
| 1042 | return true; |
| 1043 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1044 | |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 1045 | if (fMCRec->fMatrix->hasPerspective()) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1046 | SkRect dst; |
| 1047 | fMCRec->fMatrix->mapRect(&dst, rect); |
| 1048 | SkIRect idst; |
| 1049 | dst.roundOut(&idst); |
| 1050 | return !SkIRect::Intersects(idst, fMCRec->fRegion->getBounds()); |
| 1051 | } else { |
reed@android.com | ba09de4 | 2010-02-05 20:46:05 +0000 | [diff] [blame] | 1052 | const SkRectCompareType& clipR = this->getLocalClipBoundsCompareType(et); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1053 | |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1054 | // for speed, do the most likely reject compares first |
| 1055 | SkScalarCompareType userT = SkScalarToCompareType(rect.fTop); |
| 1056 | SkScalarCompareType userB = SkScalarToCompareType(rect.fBottom); |
| 1057 | if (userT >= clipR.fBottom || userB <= clipR.fTop) { |
| 1058 | return true; |
| 1059 | } |
| 1060 | SkScalarCompareType userL = SkScalarToCompareType(rect.fLeft); |
| 1061 | SkScalarCompareType userR = SkScalarToCompareType(rect.fRight); |
| 1062 | if (userL >= clipR.fRight || userR <= clipR.fLeft) { |
| 1063 | return true; |
| 1064 | } |
| 1065 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1066 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | bool SkCanvas::quickReject(const SkPath& path, EdgeType et) const { |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1070 | return path.isEmpty() || this->quickReject(path.getBounds(), et); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | bool SkCanvas::quickRejectY(SkScalar top, SkScalar bottom, EdgeType et) const { |
| 1074 | /* current impl ignores edgetype, and relies on |
| 1075 | getLocalClipBoundsCompareType(), which always returns a value assuming |
| 1076 | antialiasing (worst case) |
| 1077 | */ |
| 1078 | |
| 1079 | if (fMCRec->fRegion->isEmpty()) { |
| 1080 | return true; |
| 1081 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1082 | |
reed@android.com | aefd2bc | 2009-03-30 21:02:14 +0000 | [diff] [blame] | 1083 | SkScalarCompareType userT = SkScalarToCompareType(top); |
| 1084 | SkScalarCompareType userB = SkScalarToCompareType(bottom); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1085 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1086 | // check for invalid user Y coordinates (i.e. empty) |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1087 | // reed: why do we need to do this check, since it slows us down? |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1088 | if (userT >= userB) { |
| 1089 | return true; |
| 1090 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1091 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1092 | // check if we are above or below the local clip bounds |
| 1093 | const SkRectCompareType& clipR = this->getLocalClipBoundsCompareType(); |
| 1094 | return userT >= clipR.fBottom || userB <= clipR.fTop; |
| 1095 | } |
| 1096 | |
| 1097 | bool SkCanvas::getClipBounds(SkRect* bounds, EdgeType et) const { |
| 1098 | const SkRegion& clip = *fMCRec->fRegion; |
| 1099 | if (clip.isEmpty()) { |
| 1100 | if (bounds) { |
| 1101 | bounds->setEmpty(); |
| 1102 | } |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1106 | SkMatrix inverse; |
| 1107 | // if we can't invert the CTM, we can't return local clip bounds |
| 1108 | if (!fMCRec->fMatrix->invert(&inverse)) { |
reed@android.com | 72dcd3a | 2009-05-18 15:46:29 +0000 | [diff] [blame] | 1109 | if (bounds) { |
| 1110 | bounds->setEmpty(); |
| 1111 | } |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1114 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1115 | if (NULL != bounds) { |
| 1116 | SkRect r; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1117 | // get the clip's bounds |
| 1118 | const SkIRect& ibounds = clip.getBounds(); |
| 1119 | // adjust it outwards if we are antialiasing |
| 1120 | int inset = (kAA_EdgeType == et); |
| 1121 | r.iset(ibounds.fLeft - inset, ibounds.fTop - inset, |
| 1122 | ibounds.fRight + inset, ibounds.fBottom + inset); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1124 | // invert into local coordinates |
| 1125 | inverse.mapRect(bounds, r); |
| 1126 | } |
| 1127 | return true; |
| 1128 | } |
| 1129 | |
| 1130 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
| 1131 | return *fMCRec->fMatrix; |
| 1132 | } |
| 1133 | |
| 1134 | const SkRegion& SkCanvas::getTotalClip() const { |
| 1135 | return *fMCRec->fRegion; |
| 1136 | } |
| 1137 | |
reed@google.com | 7d7ca79 | 2011-02-23 22:39:18 +0000 | [diff] [blame] | 1138 | const SkClipStack& SkCanvas::getTotalClipStack() const { |
| 1139 | return fClipStack; |
| 1140 | } |
| 1141 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1142 | void SkCanvas::setExternalMatrix(const SkMatrix* matrix) { |
| 1143 | if (NULL == matrix || matrix->isIdentity()) { |
| 1144 | if (fUseExternalMatrix) { |
| 1145 | fDeviceCMDirty = true; |
| 1146 | } |
| 1147 | fUseExternalMatrix = false; |
| 1148 | } else { |
| 1149 | fUseExternalMatrix = true; |
| 1150 | fDeviceCMDirty = true; // |= (fExternalMatrix != *matrix) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1151 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1152 | fExternalMatrix = *matrix; |
| 1153 | matrix->invert(&fExternalInverse); |
| 1154 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1155 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1156 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1157 | SkDevice* SkCanvas::createLayerDevice(SkBitmap::Config config, |
| 1158 | int width, int height, |
| 1159 | bool isOpaque) { |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1160 | SkDevice* device = this->getTopDevice(); |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 1161 | if (device) { |
| 1162 | return device->createCompatibleDeviceForSaveLayer(config, width, height, |
| 1163 | isOpaque); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1164 | } else { |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 1165 | return NULL; |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1166 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1169 | SkDevice* SkCanvas::createCompatibleDevice(SkBitmap::Config config, |
| 1170 | int width, int height, |
| 1171 | bool isOpaque) { |
| 1172 | SkDevice* device = this->getDevice(); |
| 1173 | if (device) { |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 1174 | return device->createCompatibleDevice(config, width, height, isOpaque); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1175 | } else { |
| 1176 | return NULL; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1181 | ////////////////////////////////////////////////////////////////////////////// |
| 1182 | // These are the virtual drawing methods |
| 1183 | ////////////////////////////////////////////////////////////////////////////// |
| 1184 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1185 | void SkCanvas::clear(SkColor color) { |
| 1186 | SkDrawIter iter(this); |
| 1187 | |
| 1188 | while (iter.next()) { |
| 1189 | iter.fDevice->clear(color); |
| 1190 | } |
| 1191 | } |
| 1192 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1193 | void SkCanvas::drawPaint(const SkPaint& paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1194 | LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1195 | |
| 1196 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1197 | iter.fDevice->drawPaint(iter, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1200 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 1204 | const SkPaint& paint) { |
| 1205 | if ((long)count <= 0) { |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | SkASSERT(pts != NULL); |
| 1210 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1211 | LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1212 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1213 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1214 | iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1215 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1216 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1217 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
| 1221 | if (paint.canComputeFastBounds()) { |
| 1222 | SkRect storage; |
| 1223 | if (this->quickReject(paint.computeFastBounds(r, &storage), |
| 1224 | paint2EdgeType(&paint))) { |
| 1225 | return; |
| 1226 | } |
| 1227 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1228 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1229 | LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1230 | |
| 1231 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1232 | iter.fDevice->drawRect(iter, r, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1235 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
| 1239 | if (paint.canComputeFastBounds()) { |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1240 | SkRect storage; |
| 1241 | const SkRect& bounds = path.getBounds(); |
| 1242 | if (this->quickReject(paint.computeFastBounds(bounds, &storage), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1243 | paint2EdgeType(&paint))) { |
| 1244 | return; |
| 1245 | } |
| 1246 | } |
| 1247 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1248 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1249 | |
| 1250 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1251 | iter.fDevice->drawPath(iter, path, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1254 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 1258 | const SkPaint* paint) { |
| 1259 | SkDEBUGCODE(bitmap.validate();) |
| 1260 | |
| 1261 | if (NULL == paint || (paint->getMaskFilter() == NULL)) { |
| 1262 | SkRect fastBounds; |
| 1263 | fastBounds.set(x, y, |
| 1264 | x + SkIntToScalar(bitmap.width()), |
| 1265 | y + SkIntToScalar(bitmap.height())); |
| 1266 | if (this->quickReject(fastBounds, paint2EdgeType(paint))) { |
| 1267 | return; |
| 1268 | } |
| 1269 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1270 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1271 | SkMatrix matrix; |
| 1272 | matrix.setTranslate(x, y); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1273 | this->internalDrawBitmap(bitmap, NULL, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, |
| 1277 | const SkRect& dst, const SkPaint* paint) { |
| 1278 | if (bitmap.width() == 0 || bitmap.height() == 0 || dst.isEmpty()) { |
| 1279 | return; |
| 1280 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1281 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1282 | // do this now, to avoid the cost of calling extract for RLE bitmaps |
| 1283 | if (this->quickReject(dst, paint2EdgeType(paint))) { |
| 1284 | return; |
| 1285 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1286 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1287 | const SkBitmap* bitmapPtr = &bitmap; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1288 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1289 | SkMatrix matrix; |
reed@android.com | 8789999 | 2009-10-16 14:48:38 +0000 | [diff] [blame] | 1290 | SkRect tmpSrc; |
| 1291 | if (src) { |
| 1292 | tmpSrc.set(*src); |
| 1293 | // if the extract process clipped off the top or left of the |
| 1294 | // original, we adjust for that here to get the position right. |
| 1295 | if (tmpSrc.fLeft > 0) { |
| 1296 | tmpSrc.fRight -= tmpSrc.fLeft; |
| 1297 | tmpSrc.fLeft = 0; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 1298 | } |
reed@android.com | 8789999 | 2009-10-16 14:48:38 +0000 | [diff] [blame] | 1299 | if (tmpSrc.fTop > 0) { |
| 1300 | tmpSrc.fBottom -= tmpSrc.fTop; |
| 1301 | tmpSrc.fTop = 0; |
| 1302 | } |
| 1303 | } else { |
| 1304 | tmpSrc.set(0, 0, SkIntToScalar(bitmap.width()), |
| 1305 | SkIntToScalar(bitmap.height())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1306 | } |
reed@android.com | 8789999 | 2009-10-16 14:48:38 +0000 | [diff] [blame] | 1307 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1308 | |
| 1309 | // ensure that src is "valid" before we pass it to our internal routines |
| 1310 | // and to SkDevice. i.e. sure it is contained inside the original bitmap. |
| 1311 | SkIRect tmpISrc; |
| 1312 | if (src) { |
| 1313 | tmpISrc.set(0, 0, bitmap.width(), bitmap.height()); |
reed@google.com | 2ade086 | 2011-03-17 17:48:04 +0000 | [diff] [blame] | 1314 | if (!tmpISrc.intersect(*src)) { |
| 1315 | return; |
| 1316 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1317 | src = &tmpISrc; |
| 1318 | } |
| 1319 | this->internalDrawBitmap(*bitmapPtr, src, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, |
| 1323 | const SkPaint* paint) { |
| 1324 | SkDEBUGCODE(bitmap.validate();) |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1325 | this->internalDrawBitmap(bitmap, NULL, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1328 | void SkCanvas::commonDrawBitmap(const SkBitmap& bitmap, const SkIRect* srcRect, |
| 1329 | const SkMatrix& matrix, const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1330 | SkDEBUGCODE(bitmap.validate();) |
reed@android.com | 9b03906 | 2009-02-11 15:09:58 +0000 | [diff] [blame] | 1331 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1332 | LOOPER_BEGIN(paint, SkDrawFilter::kBitmap_Type) |
reed@android.com | 9b03906 | 2009-02-11 15:09:58 +0000 | [diff] [blame] | 1333 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1334 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1335 | iter.fDevice->drawBitmap(iter, bitmap, srcRect, matrix, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1336 | } |
reed@android.com | 9b03906 | 2009-02-11 15:09:58 +0000 | [diff] [blame] | 1337 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1338 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, |
| 1342 | const SkPaint* paint) { |
| 1343 | SkDEBUGCODE(bitmap.validate();) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1344 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1345 | if (reject_bitmap(bitmap)) { |
| 1346 | return; |
| 1347 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1349 | SkPaint tmp; |
| 1350 | if (NULL == paint) { |
| 1351 | paint = &tmp; |
| 1352 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1353 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1354 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1355 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1356 | while (iter.next()) { |
| 1357 | iter.fDevice->drawSprite(iter, bitmap, x - iter.getX(), y - iter.getY(), |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1358 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1359 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1360 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1363 | class SkDeviceFilteredPaint { |
| 1364 | public: |
| 1365 | SkDeviceFilteredPaint(SkDevice* device, const SkPaint& paint) { |
| 1366 | SkDevice::TextFlags flags; |
| 1367 | if (device->filterTextFlags(paint, &flags)) { |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 1368 | SkPaint* newPaint = fLazy.set(paint); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1369 | newPaint->setFlags(flags.fFlags); |
| 1370 | newPaint->setHinting(flags.fHinting); |
| 1371 | fPaint = newPaint; |
| 1372 | } else { |
| 1373 | fPaint = &paint; |
| 1374 | } |
| 1375 | } |
| 1376 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1377 | const SkPaint& paint() const { return *fPaint; } |
| 1378 | |
| 1379 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1380 | const SkPaint* fPaint; |
| 1381 | SkLazyPaint fLazy; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1382 | }; |
| 1383 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1384 | void SkCanvas::drawText(const void* text, size_t byteLength, |
| 1385 | SkScalar x, SkScalar y, const SkPaint& paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1386 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1387 | |
| 1388 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1389 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1390 | iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1393 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | void SkCanvas::drawPosText(const void* text, size_t byteLength, |
| 1397 | const SkPoint pos[], const SkPaint& paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1398 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1399 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1400 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1401 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1402 | iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1403 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1404 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1405 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1406 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | void SkCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 1410 | const SkScalar xpos[], SkScalar constY, |
| 1411 | const SkPaint& paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1412 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1413 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1414 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1415 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1416 | iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 1417 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1418 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1419 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1420 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 1424 | const SkPath& path, const SkMatrix* matrix, |
| 1425 | const SkPaint& paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1426 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1427 | |
| 1428 | while (iter.next()) { |
| 1429 | iter.fDevice->drawTextOnPath(iter, text, byteLength, path, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1430 | matrix, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1433 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1436 | #ifdef ANDROID |
| 1437 | void SkCanvas::drawPosTextOnPath(const void* text, size_t byteLength, |
| 1438 | const SkPoint pos[], const SkPaint& paint, |
| 1439 | const SkPath& path, const SkMatrix* matrix) { |
| 1440 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1441 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type) |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1442 | |
| 1443 | while (iter.next()) { |
| 1444 | iter.fDevice->drawPosTextOnPath(iter, text, byteLength, pos, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1445 | looper.paint(), path, matrix); |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1448 | LOOPER_END |
djsollen@google.com | cd9d69b | 2011-03-14 20:30:14 +0000 | [diff] [blame] | 1449 | } |
| 1450 | #endif |
| 1451 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1452 | void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 1453 | const SkPoint verts[], const SkPoint texs[], |
| 1454 | const SkColor colors[], SkXfermode* xmode, |
| 1455 | const uint16_t indices[], int indexCount, |
| 1456 | const SkPaint& paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1457 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1458 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1459 | while (iter.next()) { |
| 1460 | iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1461 | colors, xmode, indices, indexCount, |
| 1462 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1463 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1464 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1465 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 1468 | void SkCanvas::drawData(const void* data, size_t length) { |
| 1469 | // do nothing. Subclasses may do something with the data |
| 1470 | } |
| 1471 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1472 | ////////////////////////////////////////////////////////////////////////////// |
| 1473 | // These methods are NOT virtual, and therefore must call back into virtual |
| 1474 | // methods, rather than actually drawing themselves. |
| 1475 | ////////////////////////////////////////////////////////////////////////////// |
| 1476 | |
| 1477 | void SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 1478 | SkXfermode::Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1479 | SkPaint paint; |
| 1480 | |
| 1481 | paint.setARGB(a, r, g, b); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 1482 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 1483 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1484 | } |
| 1485 | this->drawPaint(paint); |
| 1486 | } |
| 1487 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 1488 | void SkCanvas::drawColor(SkColor c, SkXfermode::Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1489 | SkPaint paint; |
| 1490 | |
| 1491 | paint.setColor(c); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 1492 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 1493 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1494 | } |
| 1495 | this->drawPaint(paint); |
| 1496 | } |
| 1497 | |
| 1498 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
| 1499 | SkPoint pt; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1500 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1501 | pt.set(x, y); |
| 1502 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 1503 | } |
| 1504 | |
| 1505 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) { |
| 1506 | SkPoint pt; |
| 1507 | SkPaint paint; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1508 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1509 | pt.set(x, y); |
| 1510 | paint.setColor(color); |
| 1511 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 1512 | } |
| 1513 | |
| 1514 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 1515 | const SkPaint& paint) { |
| 1516 | SkPoint pts[2]; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1517 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1518 | pts[0].set(x0, y0); |
| 1519 | pts[1].set(x1, y1); |
| 1520 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 1521 | } |
| 1522 | |
| 1523 | void SkCanvas::drawRectCoords(SkScalar left, SkScalar top, |
| 1524 | SkScalar right, SkScalar bottom, |
| 1525 | const SkPaint& paint) { |
| 1526 | SkRect r; |
| 1527 | |
| 1528 | r.set(left, top, right, bottom); |
| 1529 | this->drawRect(r, paint); |
| 1530 | } |
| 1531 | |
| 1532 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 1533 | const SkPaint& paint) { |
| 1534 | if (radius < 0) { |
| 1535 | radius = 0; |
| 1536 | } |
| 1537 | |
| 1538 | SkRect r; |
| 1539 | r.set(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1540 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1541 | if (paint.canComputeFastBounds()) { |
| 1542 | SkRect storage; |
| 1543 | if (this->quickReject(paint.computeFastBounds(r, &storage), |
| 1544 | paint2EdgeType(&paint))) { |
| 1545 | return; |
| 1546 | } |
| 1547 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1548 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1549 | SkPath path; |
| 1550 | path.addOval(r); |
| 1551 | this->drawPath(path, paint); |
| 1552 | } |
| 1553 | |
| 1554 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 1555 | const SkPaint& paint) { |
| 1556 | if (rx > 0 && ry > 0) { |
| 1557 | if (paint.canComputeFastBounds()) { |
| 1558 | SkRect storage; |
| 1559 | if (this->quickReject(paint.computeFastBounds(r, &storage), |
| 1560 | paint2EdgeType(&paint))) { |
| 1561 | return; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | SkPath path; |
| 1566 | path.addRoundRect(r, rx, ry, SkPath::kCW_Direction); |
| 1567 | this->drawPath(path, paint); |
| 1568 | } else { |
| 1569 | this->drawRect(r, paint); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 1574 | if (paint.canComputeFastBounds()) { |
| 1575 | SkRect storage; |
| 1576 | if (this->quickReject(paint.computeFastBounds(oval, &storage), |
| 1577 | paint2EdgeType(&paint))) { |
| 1578 | return; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | SkPath path; |
| 1583 | path.addOval(oval); |
| 1584 | this->drawPath(path, paint); |
| 1585 | } |
| 1586 | |
| 1587 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 1588 | SkScalar sweepAngle, bool useCenter, |
| 1589 | const SkPaint& paint) { |
| 1590 | if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) { |
| 1591 | this->drawOval(oval, paint); |
| 1592 | } else { |
| 1593 | SkPath path; |
| 1594 | if (useCenter) { |
| 1595 | path.moveTo(oval.centerX(), oval.centerY()); |
| 1596 | } |
| 1597 | path.arcTo(oval, startAngle, sweepAngle, !useCenter); |
| 1598 | if (useCenter) { |
| 1599 | path.close(); |
| 1600 | } |
| 1601 | this->drawPath(path, paint); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | void SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength, |
| 1606 | const SkPath& path, SkScalar hOffset, |
| 1607 | SkScalar vOffset, const SkPaint& paint) { |
| 1608 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1609 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1610 | matrix.setTranslate(hOffset, vOffset); |
| 1611 | this->drawTextOnPath(text, byteLength, path, &matrix, paint); |
| 1612 | } |
| 1613 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 1614 | /////////////////////////////////////////////////////////////////////////////// |
| 1615 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1616 | void SkCanvas::drawPicture(SkPicture& picture) { |
| 1617 | int saveCount = save(); |
| 1618 | picture.draw(this); |
| 1619 | restoreToCount(saveCount); |
| 1620 | } |
| 1621 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1622 | /////////////////////////////////////////////////////////////////////////////// |
| 1623 | /////////////////////////////////////////////////////////////////////////////// |
| 1624 | |
| 1625 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) { |
reed@google.com | d642329 | 2010-12-20 20:53:13 +0000 | [diff] [blame] | 1626 | SK_COMPILE_ASSERT(sizeof(fStorage) >= sizeof(SkDrawIter), fStorage_too_small); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1627 | |
| 1628 | SkASSERT(canvas); |
| 1629 | |
| 1630 | fImpl = new (fStorage) SkDrawIter(canvas, skipEmptyClips); |
| 1631 | fDone = !fImpl->next(); |
| 1632 | } |
| 1633 | |
| 1634 | SkCanvas::LayerIter::~LayerIter() { |
| 1635 | fImpl->~SkDrawIter(); |
| 1636 | } |
| 1637 | |
| 1638 | void SkCanvas::LayerIter::next() { |
| 1639 | fDone = !fImpl->next(); |
| 1640 | } |
| 1641 | |
| 1642 | SkDevice* SkCanvas::LayerIter::device() const { |
| 1643 | return fImpl->getDevice(); |
| 1644 | } |
| 1645 | |
| 1646 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
| 1647 | return fImpl->getMatrix(); |
| 1648 | } |
| 1649 | |
| 1650 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 1651 | const SkPaint* paint = fImpl->getPaint(); |
| 1652 | if (NULL == paint) { |
| 1653 | paint = &fDefaultPaint; |
| 1654 | } |
| 1655 | return *paint; |
| 1656 | } |
| 1657 | |
| 1658 | const SkRegion& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); } |
| 1659 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 1660 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |