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