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