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