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