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