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