reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed | 9f01471 | 2014-06-18 15:51:20 -0700 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 10 | #include "SkCanvasPriv.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 11 | #include "SkBitmapDevice.h" |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 12 | #include "SkDeviceImageFilterProxy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkDraw.h" |
| 14 | #include "SkDrawFilter.h" |
| 15 | #include "SkDrawLooper.h" |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 16 | #include "SkMetaData.h" |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 17 | #include "SkPathOps.h" |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame^] | 18 | #include "SkPatchUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkPicture.h" |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 20 | #include "SkRasterClip.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 21 | #include "SkRRect.h" |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 22 | #include "SkSmallAllocator.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 23 | #include "SkSurface_Base.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkTemplates.h" |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 25 | #include "SkTextFormatParams.h" |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 26 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 29 | #if SK_SUPPORT_GPU |
| 30 | #include "GrRenderTarget.h" |
| 31 | #endif |
| 32 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 33 | // experimental for faster tiled drawing... |
| 34 | //#define SK_ENABLE_CLIP_QUICKREJECT |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | //#define SK_TRACE_SAVERESTORE |
| 37 | |
| 38 | #ifdef SK_TRACE_SAVERESTORE |
| 39 | static int gLayerCounter; |
| 40 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 41 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 42 | |
| 43 | static int gRecCounter; |
| 44 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 45 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 46 | |
| 47 | static int gCanvasCounter; |
| 48 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 49 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 50 | #else |
| 51 | #define inc_layer() |
| 52 | #define dec_layer() |
| 53 | #define inc_rec() |
| 54 | #define dec_rec() |
| 55 | #define inc_canvas() |
| 56 | #define dec_canvas() |
| 57 | #endif |
| 58 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 59 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 60 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 61 | void SkCanvas::predrawNotify() { |
| 62 | if (fSurfaceBase) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 63 | fSurfaceBase->aboutToDraw(SkSurface::kRetain_ContentChangeMode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 69 | /* This is the record we keep for each SkBaseDevice that the user installs. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 71 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 72 | these are used (assuming we're not on a layer) we rebuild these cache |
| 73 | values: they reflect the top of the save stack, but translated and clipped |
| 74 | by the device's XY offset and bitmap-bounds. |
| 75 | */ |
| 76 | struct DeviceCM { |
| 77 | DeviceCM* fNext; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 78 | SkBaseDevice* fDevice; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 79 | SkRasterClip fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | const SkMatrix* fMatrix; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 81 | SkPaint* fPaint; // may be null (in the future) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 83 | DeviceCM(SkBaseDevice* device, int x, int y, const SkPaint* paint, SkCanvas* canvas) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | : fNext(NULL) { |
| 85 | if (NULL != device) { |
| 86 | device->ref(); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 87 | device->onAttachToCanvas(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 89 | fDevice = device; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | fPaint = paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 91 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 93 | ~DeviceCM() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | if (NULL != fDevice) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 95 | fDevice->onDetachFromCanvas(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | fDevice->unref(); |
| 97 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 98 | SkDELETE(fPaint); |
| 99 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 100 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 101 | void updateMC(const SkMatrix& totalMatrix, const SkRasterClip& totalClip, |
| 102 | const SkClipStack& clipStack, SkRasterClip* updateClip) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 103 | int x = fDevice->getOrigin().x(); |
| 104 | int y = fDevice->getOrigin().y(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | int width = fDevice->width(); |
| 106 | int height = fDevice->height(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 107 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | if ((x | y) == 0) { |
| 109 | fMatrix = &totalMatrix; |
| 110 | fClip = totalClip; |
| 111 | } else { |
| 112 | fMatrixStorage = totalMatrix; |
| 113 | fMatrixStorage.postTranslate(SkIntToScalar(-x), |
| 114 | SkIntToScalar(-y)); |
| 115 | fMatrix = &fMatrixStorage; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | totalClip.translate(-x, -y, &fClip); |
| 118 | } |
| 119 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 120 | fClip.op(SkIRect::MakeWH(width, height), SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | |
| 122 | // intersect clip, but don't translate it (yet) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | if (updateClip) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 125 | updateClip->op(SkIRect::MakeXYWH(x, y, width, height), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | SkRegion::kDifference_Op); |
| 127 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 128 | |
robertphillips@google.com | 3fffb2e | 2012-10-09 22:30:18 +0000 | [diff] [blame] | 129 | fDevice->setMatrixClip(*fMatrix, fClip.forceGetBW(), clipStack); |
| 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | #ifdef SK_DEBUG |
| 132 | if (!fClip.isEmpty()) { |
| 133 | SkIRect deviceR; |
| 134 | deviceR.set(0, 0, width, height); |
| 135 | SkASSERT(deviceR.contains(fClip.getBounds())); |
| 136 | } |
| 137 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | private: |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 141 | SkMatrix fMatrixStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | /* This is the record we keep for each save/restore level in the stack. |
| 145 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 146 | for these fields. If the value is copied for this level, the copy is |
| 147 | stored in the ...Storage field, and the pointer points to that. If the |
| 148 | value is not copied for this level, we ignore ...Storage, and just point |
| 149 | at the corresponding value in the previous level in the stack. |
| 150 | */ |
| 151 | class SkCanvas::MCRec { |
| 152 | public: |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 153 | SkMatrix fMatrix; |
| 154 | SkRasterClip fRasterClip; |
| 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 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 166 | MCRec(const MCRec* prev) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | if (NULL != prev) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 168 | fMatrix = prev->fMatrix; |
| 169 | fRasterClip = prev->fRasterClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | |
| 171 | fFilter = prev->fFilter; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 172 | SkSafeRef(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | |
| 174 | fTopLayer = prev->fTopLayer; |
| 175 | } else { // no prev |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 176 | fMatrix.reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | fFilter = NULL; |
| 178 | fTopLayer = NULL; |
| 179 | } |
| 180 | fLayer = NULL; |
| 181 | |
| 182 | // don't bother initializing fNext |
| 183 | inc_rec(); |
| 184 | } |
| 185 | ~MCRec() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 186 | SkSafeUnref(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | SkDELETE(fLayer); |
| 188 | dec_rec(); |
| 189 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | class SkDrawIter : public SkDraw { |
| 193 | public: |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 194 | SkDrawIter(SkCanvas* canvas, bool skipEmptyClips = true) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 195 | canvas = canvas->canvasForDrawIter(); |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 196 | fCanvas = canvas; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | canvas->updateDeviceCMCache(); |
| 198 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 199 | fClipStack = &canvas->fClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | fCurrLayer = canvas->fMCRec->fTopLayer; |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 201 | fSkipEmptyClips = skipEmptyClips; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | bool next() { |
| 205 | // skip over recs with empty clips |
| 206 | if (fSkipEmptyClips) { |
| 207 | while (fCurrLayer && fCurrLayer->fClip.isEmpty()) { |
| 208 | fCurrLayer = fCurrLayer->fNext; |
| 209 | } |
| 210 | } |
| 211 | |
reed@google.com | f68c5e2 | 2012-02-24 16:38:58 +0000 | [diff] [blame] | 212 | const DeviceCM* rec = fCurrLayer; |
| 213 | if (rec && rec->fDevice) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | |
| 215 | fMatrix = rec->fMatrix; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 216 | fClip = &((SkRasterClip*)&rec->fClip)->forceGetBW(); |
| 217 | fRC = &rec->fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | fDevice = rec->fDevice; |
| 219 | fBitmap = &fDevice->accessBitmap(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | fPaint = rec->fPaint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 221 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | |
| 223 | fCurrLayer = rec->fNext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 224 | // fCurrLayer may be NULL now |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 225 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | return false; |
| 229 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 230 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 231 | SkBaseDevice* getDevice() const { return fDevice; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 232 | int getX() const { return fDevice->getOrigin().x(); } |
| 233 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | const SkMatrix& getMatrix() const { return *fMatrix; } |
| 235 | const SkRegion& getClip() const { return *fClip; } |
| 236 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | private: |
| 239 | SkCanvas* fCanvas; |
| 240 | const DeviceCM* fCurrLayer; |
| 241 | const SkPaint* fPaint; // May be null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | SkBool8 fSkipEmptyClips; |
| 243 | |
| 244 | typedef SkDraw INHERITED; |
| 245 | }; |
| 246 | |
| 247 | ///////////////////////////////////////////////////////////////////////////// |
| 248 | |
| 249 | class AutoDrawLooper { |
| 250 | public: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 251 | AutoDrawLooper(SkCanvas* canvas, const SkPaint& paint, |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 252 | bool skipLayerForImageFilter = false, |
| 253 | const SkRect* bounds = NULL) : fOrigPaint(paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 254 | fCanvas = canvas; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | fFilter = canvas->getDrawFilter(); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 256 | fPaint = NULL; |
| 257 | fSaveCount = canvas->getSaveCount(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 258 | fDoClearImageFilter = false; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 259 | fDone = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 261 | if (!skipLayerForImageFilter && fOrigPaint.getImageFilter()) { |
| 262 | SkPaint tmp; |
| 263 | tmp.setImageFilter(fOrigPaint.getImageFilter()); |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 264 | (void)canvas->internalSaveLayer(bounds, &tmp, SkCanvas::kARGB_ClipLayer_SaveFlag, |
| 265 | true, SkCanvas::kFullLayer_SaveLayerStrategy); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 266 | // we'll clear the imageFilter for the actual draws in next(), so |
| 267 | // it will only be applied during the restore(). |
| 268 | fDoClearImageFilter = true; |
| 269 | } |
| 270 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 271 | if (SkDrawLooper* looper = paint.getLooper()) { |
| 272 | void* buffer = fLooperContextAllocator.reserveT<SkDrawLooper::Context>( |
| 273 | looper->contextSize()); |
| 274 | fLooperContext = looper->createContext(canvas, buffer); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 275 | fIsSimple = false; |
| 276 | } else { |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 277 | fLooperContext = NULL; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 278 | // can we be marked as simple? |
| 279 | fIsSimple = !fFilter && !fDoClearImageFilter; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 282 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | ~AutoDrawLooper() { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 284 | if (fDoClearImageFilter) { |
| 285 | fCanvas->internalRestore(); |
| 286 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 287 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 288 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 289 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 290 | const SkPaint& paint() const { |
| 291 | SkASSERT(fPaint); |
| 292 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 293 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 294 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 295 | bool next(SkDrawFilter::Type drawType) { |
| 296 | if (fDone) { |
| 297 | return false; |
| 298 | } else if (fIsSimple) { |
| 299 | fDone = true; |
| 300 | fPaint = &fOrigPaint; |
| 301 | return !fPaint->nothingToDraw(); |
| 302 | } else { |
| 303 | return this->doNext(drawType); |
| 304 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 305 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 306 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 307 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 308 | SkLazyPaint fLazyPaint; |
| 309 | SkCanvas* fCanvas; |
| 310 | const SkPaint& fOrigPaint; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 311 | SkDrawFilter* fFilter; |
| 312 | const SkPaint* fPaint; |
| 313 | int fSaveCount; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 314 | bool fDoClearImageFilter; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 315 | bool fDone; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 316 | bool fIsSimple; |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 317 | SkDrawLooper::Context* fLooperContext; |
| 318 | SkSmallAllocator<1, 32> fLooperContextAllocator; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 319 | |
| 320 | bool doNext(SkDrawFilter::Type drawType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | }; |
| 322 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 323 | bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) { |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 324 | fPaint = NULL; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 325 | SkASSERT(!fIsSimple); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 326 | SkASSERT(fLooperContext || fFilter || fDoClearImageFilter); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 327 | |
| 328 | SkPaint* paint = fLazyPaint.set(fOrigPaint); |
| 329 | |
| 330 | if (fDoClearImageFilter) { |
| 331 | paint->setImageFilter(NULL); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 332 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 333 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 334 | if (fLooperContext && !fLooperContext->next(fCanvas, paint)) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 335 | fDone = true; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | if (fFilter) { |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 339 | if (!fFilter->filter(paint, drawType)) { |
| 340 | fDone = true; |
| 341 | return false; |
| 342 | } |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 343 | if (NULL == fLooperContext) { |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 344 | // no looper means we only draw once |
| 345 | fDone = true; |
| 346 | } |
| 347 | } |
| 348 | fPaint = paint; |
| 349 | |
| 350 | // if we only came in here for the imagefilter, mark us as done |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 351 | if (!fLooperContext && !fFilter) { |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 352 | fDone = true; |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // call this after any possible paint modifiers |
| 356 | if (fPaint->nothingToDraw()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 357 | fPaint = NULL; |
| 358 | return false; |
| 359 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | #include "SkColorPriv.h" |
| 364 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 365 | ////////// macros to place around the internal draw calls ////////////////// |
| 366 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 367 | #define LOOPER_BEGIN_DRAWDEVICE(paint, type) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 368 | this->predrawNotify(); \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 369 | AutoDrawLooper looper(this, paint, true); \ |
| 370 | while (looper.next(type)) { \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 371 | SkDrawIter iter(this); |
| 372 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 373 | #define LOOPER_BEGIN(paint, type, bounds) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 374 | this->predrawNotify(); \ |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 375 | AutoDrawLooper looper(this, paint, false, bounds); \ |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 376 | while (looper.next(type)) { \ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 378 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 379 | #define LOOPER_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | |
| 381 | //////////////////////////////////////////////////////////////////////////// |
| 382 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 383 | SkBaseDevice* SkCanvas::init(SkBaseDevice* device) { |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 384 | fCachedLocalClipBounds.setEmpty(); |
| 385 | fCachedLocalClipBoundsDirty = true; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 386 | fAllowSoftClip = true; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 387 | fAllowSimplifyClip = false; |
agl@chromium.org | 447bcfa | 2009-12-12 00:06:17 +0000 | [diff] [blame] | 388 | fDeviceCMDirty = false; |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 389 | fSaveLayerCount = 0; |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 390 | fCullCount = 0; |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 391 | fMetaData = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | |
| 393 | fMCRec = (MCRec*)fMCStack.push_back(); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 394 | new (fMCRec) MCRec(NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 395 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 396 | fMCRec->fLayer = SkNEW_ARGS(DeviceCM, (NULL, 0, 0, NULL, NULL)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | fMCRec->fTopLayer = fMCRec->fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 399 | fSurfaceBase = NULL; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 400 | |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 401 | return this->setRootDevice(device); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 402 | } |
| 403 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 404 | SkCanvas::SkCanvas() |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 405 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 406 | { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 407 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 408 | |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 409 | this->init(NULL); |
| 410 | } |
| 411 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 412 | SkCanvas::SkCanvas(int width, int height) |
| 413 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 414 | { |
| 415 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 416 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 417 | SkBitmap bitmap; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 418 | bitmap.setInfo(SkImageInfo::MakeUnknown(width, height)); |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 419 | this->init(SkNEW_ARGS(SkBitmapDevice, (bitmap)))->unref(); |
| 420 | } |
| 421 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 422 | SkCanvas::SkCanvas(SkBaseDevice* device) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 423 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 424 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | inc_canvas(); |
| 426 | |
| 427 | this->init(device); |
| 428 | } |
| 429 | |
| 430 | SkCanvas::SkCanvas(const SkBitmap& bitmap) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 431 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 432 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | inc_canvas(); |
| 434 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 435 | this->init(SkNEW_ARGS(SkBitmapDevice, (bitmap)))->unref(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | SkCanvas::~SkCanvas() { |
| 439 | // free up the contents of our deque |
| 440 | this->restoreToCount(1); // restore everything but the last |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 441 | SkASSERT(0 == fSaveLayerCount); |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 442 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 443 | this->internalRestore(); // restore the last, since we're going away |
| 444 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 445 | SkDELETE(fMetaData); |
vandebo@chromium.org | b70ae31 | 2010-10-15 18:58:19 +0000 | [diff] [blame] | 446 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | dec_canvas(); |
| 448 | } |
| 449 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | SkDrawFilter* SkCanvas::getDrawFilter() const { |
| 451 | return fMCRec->fFilter; |
| 452 | } |
| 453 | |
| 454 | SkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) { |
| 455 | SkRefCnt_SafeAssign(fMCRec->fFilter, filter); |
| 456 | return filter; |
| 457 | } |
| 458 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 459 | SkMetaData& SkCanvas::getMetaData() { |
| 460 | // metadata users are rare, so we lazily allocate it. If that changes we |
| 461 | // can decide to just make it a field in the device (rather than a ptr) |
| 462 | if (NULL == fMetaData) { |
| 463 | fMetaData = new SkMetaData; |
| 464 | } |
| 465 | return *fMetaData; |
| 466 | } |
| 467 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 468 | /////////////////////////////////////////////////////////////////////////////// |
| 469 | |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 470 | void SkCanvas::flush() { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 471 | SkBaseDevice* device = this->getDevice(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 472 | if (device) { |
| 473 | device->flush(); |
| 474 | } |
| 475 | } |
| 476 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 477 | SkISize SkCanvas::getTopLayerSize() const { |
| 478 | SkBaseDevice* d = this->getTopDevice(); |
| 479 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 480 | } |
| 481 | |
| 482 | SkIPoint SkCanvas::getTopLayerOrigin() const { |
| 483 | SkBaseDevice* d = this->getTopDevice(); |
| 484 | return d ? d->getOrigin() : SkIPoint::Make(0, 0); |
| 485 | } |
| 486 | |
| 487 | SkISize SkCanvas::getBaseLayerSize() const { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 488 | SkBaseDevice* d = this->getDevice(); |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 489 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 490 | } |
| 491 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 492 | SkBaseDevice* SkCanvas::getDevice() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 493 | // return root device |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 494 | MCRec* rec = (MCRec*) fMCStack.front(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | SkASSERT(rec && rec->fLayer); |
| 496 | return rec->fLayer->fDevice; |
| 497 | } |
| 498 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 499 | SkBaseDevice* SkCanvas::getTopDevice(bool updateMatrixClip) const { |
reed@google.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 500 | if (updateMatrixClip) { |
| 501 | const_cast<SkCanvas*>(this)->updateDeviceCMCache(); |
| 502 | } |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 503 | return fMCRec->fTopLayer->fDevice; |
| 504 | } |
| 505 | |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 506 | SkBaseDevice* SkCanvas::setRootDevice(SkBaseDevice* device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 507 | // return root device |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 508 | SkDeque::F2BIter iter(fMCStack); |
| 509 | MCRec* rec = (MCRec*)iter.next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 510 | SkASSERT(rec && rec->fLayer); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 511 | SkBaseDevice* rootDevice = rec->fLayer->fDevice; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 512 | |
| 513 | if (rootDevice == device) { |
| 514 | return device; |
| 515 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 516 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 517 | if (device) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 518 | device->onAttachToCanvas(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | } |
| 520 | if (rootDevice) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 521 | rootDevice->onDetachFromCanvas(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | SkRefCnt_SafeAssign(rec->fLayer->fDevice, device); |
| 525 | rootDevice = device; |
| 526 | |
| 527 | fDeviceCMDirty = true; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 528 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | /* Now we update our initial region to have the bounds of the new device, |
| 530 | and then intersect all of the clips in our stack with these bounds, |
| 531 | to ensure that we can't draw outside of the device's bounds (and trash |
| 532 | memory). |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 533 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 534 | NOTE: this is only a partial-fix, since if the new device is larger than |
| 535 | 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] | 536 | so drawing may be artificially restricted. Without keeping a history of |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 537 | all calls to canvas->clipRect() and canvas->clipPath(), we can't exactly |
| 538 | reconstruct the correct clips, so this approximation will have to do. |
| 539 | The caller really needs to restore() back to the base if they want to |
| 540 | accurately take advantage of the new device bounds. |
| 541 | */ |
| 542 | |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 543 | SkIRect bounds; |
| 544 | if (device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 545 | bounds.set(0, 0, device->width(), device->height()); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 546 | } else { |
| 547 | bounds.setEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 548 | } |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 549 | // now jam our 1st clip to be bounds, and intersect the rest with that |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 550 | rec->fRasterClip.setRect(bounds); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 551 | while ((rec = (MCRec*)iter.next()) != NULL) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 552 | (void)rec->fRasterClip.op(bounds, SkRegion::kIntersect_Op); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 553 | } |
| 554 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 555 | return device; |
| 556 | } |
| 557 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 558 | bool SkCanvas::readPixels(SkBitmap* bitmap, int x, int y) { |
| 559 | if (kUnknown_SkColorType == bitmap->colorType() || bitmap->getTexture()) { |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | bool weAllocated = false; |
| 564 | if (NULL == bitmap->pixelRef()) { |
| 565 | if (!bitmap->allocPixels()) { |
| 566 | return false; |
| 567 | } |
| 568 | weAllocated = true; |
| 569 | } |
| 570 | |
| 571 | SkBitmap bm(*bitmap); |
| 572 | bm.lockPixels(); |
| 573 | if (bm.getPixels() && this->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), x, y)) { |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | if (weAllocated) { |
| 578 | bitmap->setPixelRef(NULL); |
| 579 | } |
| 580 | return false; |
| 581 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 582 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 583 | bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 584 | SkIRect r = srcRect; |
| 585 | const SkISize size = this->getBaseLayerSize(); |
| 586 | if (!r.intersect(0, 0, size.width(), size.height())) { |
| 587 | bitmap->reset(); |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | if (!bitmap->allocN32Pixels(r.width(), r.height())) { |
| 592 | // bitmap will already be reset. |
| 593 | return false; |
| 594 | } |
| 595 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), r.x(), r.y())) { |
| 596 | bitmap->reset(); |
| 597 | return false; |
| 598 | } |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | bool SkCanvas::readPixels(const SkImageInfo& origInfo, void* dstP, size_t rowBytes, int x, int y) { |
| 603 | switch (origInfo.colorType()) { |
| 604 | case kUnknown_SkColorType: |
| 605 | case kIndex_8_SkColorType: |
| 606 | return false; |
| 607 | default: |
| 608 | break; |
| 609 | } |
| 610 | if (NULL == dstP || rowBytes < origInfo.minRowBytes()) { |
| 611 | return false; |
| 612 | } |
| 613 | if (0 == origInfo.width() || 0 == origInfo.height()) { |
| 614 | return false; |
| 615 | } |
| 616 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 617 | SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 618 | if (!device) { |
| 619 | return false; |
| 620 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 621 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 622 | const SkISize size = this->getBaseLayerSize(); |
| 623 | SkIRect srcR = SkIRect::MakeXYWH(x, y, origInfo.width(), origInfo.height()); |
| 624 | if (!srcR.intersect(0, 0, size.width(), size.height())) { |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 625 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 626 | } |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 627 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 628 | SkImageInfo info = origInfo; |
| 629 | // the intersect may have shrunk info's logical size |
| 630 | info.fWidth = srcR.width(); |
| 631 | info.fHeight = srcR.height(); |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 632 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 633 | // if x or y are negative, then we have to adjust pixels |
| 634 | if (x > 0) { |
| 635 | x = 0; |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 636 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 637 | if (y > 0) { |
| 638 | y = 0; |
| 639 | } |
| 640 | // here x,y are either 0 or negative |
| 641 | dstP = ((char*)dstP - y * rowBytes - x * info.bytesPerPixel()); |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 642 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 643 | // The device can assert that the requested area is always contained in its bounds |
| 644 | return device->readPixels(info, dstP, rowBytes, srcR.x(), srcR.y()); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 645 | } |
| 646 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 647 | bool SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) { |
| 648 | if (bitmap.getTexture()) { |
| 649 | return false; |
| 650 | } |
| 651 | SkBitmap bm(bitmap); |
| 652 | bm.lockPixels(); |
| 653 | if (bm.getPixels()) { |
| 654 | return this->writePixels(bm.info(), bm.getPixels(), bm.rowBytes(), x, y); |
| 655 | } |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | bool SkCanvas::writePixels(const SkImageInfo& origInfo, const void* pixels, size_t rowBytes, |
| 660 | int x, int y) { |
| 661 | switch (origInfo.colorType()) { |
| 662 | case kUnknown_SkColorType: |
| 663 | case kIndex_8_SkColorType: |
| 664 | return false; |
| 665 | default: |
| 666 | break; |
| 667 | } |
| 668 | if (NULL == pixels || rowBytes < origInfo.minRowBytes()) { |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | const SkISize size = this->getBaseLayerSize(); |
| 673 | SkIRect target = SkIRect::MakeXYWH(x, y, origInfo.width(), origInfo.height()); |
| 674 | if (!target.intersect(0, 0, size.width(), size.height())) { |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | SkBaseDevice* device = this->getDevice(); |
| 679 | if (!device) { |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | SkImageInfo info = origInfo; |
| 684 | // the intersect may have shrunk info's logical size |
| 685 | info.fWidth = target.width(); |
| 686 | info.fHeight = target.height(); |
| 687 | |
| 688 | // if x or y are negative, then we have to adjust pixels |
| 689 | if (x > 0) { |
| 690 | x = 0; |
| 691 | } |
| 692 | if (y > 0) { |
| 693 | y = 0; |
| 694 | } |
| 695 | // here x,y are either 0 or negative |
| 696 | pixels = ((const char*)pixels - y * rowBytes - x * info.bytesPerPixel()); |
| 697 | |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 698 | // Tell our owning surface to bump its generation ID |
| 699 | this->predrawNotify(); |
| 700 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 701 | // The device can assert that the requested area is always contained in its bounds |
commit-bot@chromium.org | 4ef54f8 | 2014-03-17 17:03:18 +0000 | [diff] [blame] | 702 | return device->writePixels(info, pixels, rowBytes, target.x(), target.y()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 703 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 704 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 705 | SkCanvas* SkCanvas::canvasForDrawIter() { |
| 706 | return this; |
| 707 | } |
| 708 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 709 | ////////////////////////////////////////////////////////////////////////////// |
| 710 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 711 | void SkCanvas::updateDeviceCMCache() { |
| 712 | if (fDeviceCMDirty) { |
| 713 | const SkMatrix& totalMatrix = this->getTotalMatrix(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 714 | const SkRasterClip& totalClip = fMCRec->fRasterClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 715 | DeviceCM* layer = fMCRec->fTopLayer; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 716 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 717 | if (NULL == layer->fNext) { // only one layer |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 718 | layer->updateMC(totalMatrix, totalClip, fClipStack, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 719 | } else { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 720 | SkRasterClip clip(totalClip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 721 | do { |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 722 | layer->updateMC(totalMatrix, clip, fClipStack, &clip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 723 | } while ((layer = layer->fNext) != NULL); |
| 724 | } |
| 725 | fDeviceCMDirty = false; |
| 726 | } |
| 727 | } |
| 728 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 729 | /////////////////////////////////////////////////////////////////////////////// |
| 730 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 731 | int SkCanvas::internalSave() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 732 | int saveCount = this->getSaveCount(); // record this before the actual save |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 733 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 734 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 735 | new (newTop) MCRec(fMCRec); // balanced in restore() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 736 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 737 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 738 | fClipStack.save(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 739 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | return saveCount; |
| 741 | } |
| 742 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 743 | int SkCanvas::save() { |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 744 | this->willSave(); |
| 745 | return this->internalSave(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 746 | } |
| 747 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | static bool bounds_affects_clip(SkCanvas::SaveFlags flags) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 749 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 750 | return (flags & SkCanvas::kClipToLayer_SaveFlag) != 0; |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 751 | #else |
| 752 | return true; |
| 753 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 754 | } |
| 755 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 756 | bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveFlags flags, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 757 | SkIRect* intersection, const SkImageFilter* imageFilter) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 758 | SkIRect clipBounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 759 | SkRegion::Op op = SkRegion::kIntersect_Op; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 760 | if (!this->getClipDeviceBounds(&clipBounds)) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 761 | return false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 762 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 763 | |
| 764 | if (imageFilter) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 765 | imageFilter->filterBounds(clipBounds, fMCRec->fMatrix, &clipBounds); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 766 | // Filters may grow the bounds beyond the device bounds. |
| 767 | op = SkRegion::kReplace_Op; |
| 768 | } |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 769 | SkIRect ir; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 770 | if (NULL != bounds) { |
| 771 | SkRect r; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 772 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 773 | this->getTotalMatrix().mapRect(&r, *bounds); |
| 774 | r.roundOut(&ir); |
| 775 | // early exit if the layer's bounds are clipped out |
| 776 | if (!ir.intersect(clipBounds)) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 777 | if (bounds_affects_clip(flags)) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 778 | fMCRec->fRasterClip.setEmpty(); |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 779 | } |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 780 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 781 | } |
| 782 | } else { // no user bounds, so just use the clip |
| 783 | ir = clipBounds; |
| 784 | } |
| 785 | |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 786 | if (bounds_affects_clip(flags)) { |
| 787 | fClipStack.clipDevRect(ir, op); |
| 788 | // early exit if the clip is now empty |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 789 | if (!fMCRec->fRasterClip.op(ir, op)) { |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 790 | return false; |
| 791 | } |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if (intersection) { |
| 795 | *intersection = ir; |
| 796 | } |
| 797 | return true; |
| 798 | } |
| 799 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 800 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint) { |
| 801 | SaveLayerStrategy strategy = this->willSaveLayer(bounds, paint, kARGB_ClipLayer_SaveFlag); |
| 802 | return this->internalSaveLayer(bounds, paint, kARGB_ClipLayer_SaveFlag, false, strategy); |
| 803 | } |
| 804 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 805 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 806 | SaveFlags flags) { |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 807 | SaveLayerStrategy strategy = this->willSaveLayer(bounds, paint, flags); |
| 808 | return this->internalSaveLayer(bounds, paint, flags, false, strategy); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 809 | } |
| 810 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 811 | int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags, |
| 812 | bool justForImageFilter, SaveLayerStrategy strategy) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 813 | #ifndef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
commit-bot@chromium.org | 2a5cd60 | 2014-05-30 20:41:20 +0000 | [diff] [blame] | 814 | flags |= kClipToLayer_SaveFlag; |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 815 | #endif |
| 816 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 817 | // do this before we create the layer. We don't call the public save() since |
| 818 | // that would invoke a possibly overridden virtual |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 819 | int count = this->internalSave(); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 820 | |
| 821 | fDeviceCMDirty = true; |
| 822 | |
| 823 | SkIRect ir; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 824 | if (!this->clipRectBounds(bounds, flags, &ir, paint ? paint->getImageFilter() : NULL)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 825 | return count; |
| 826 | } |
| 827 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 828 | // FIXME: do willSaveLayer() overriders returning kNoLayer_SaveLayerStrategy really care about |
| 829 | // the clipRectBounds() call above? |
| 830 | if (kNoLayer_SaveLayerStrategy == strategy) { |
| 831 | return count; |
| 832 | } |
| 833 | |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 834 | // Kill the imagefilter if our device doesn't allow it |
| 835 | SkLazyPaint lazyP; |
| 836 | if (paint && paint->getImageFilter()) { |
| 837 | if (!this->getTopDevice()->allowImageFilter(paint->getImageFilter())) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 838 | if (justForImageFilter) { |
| 839 | // early exit if the layer was just for the imageFilter |
| 840 | return count; |
| 841 | } |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 842 | SkPaint* p = lazyP.set(*paint); |
| 843 | p->setImageFilter(NULL); |
| 844 | paint = p; |
| 845 | } |
| 846 | } |
| 847 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 848 | bool isOpaque = !SkToBool(flags & kHasAlphaLayer_SaveFlag); |
| 849 | SkImageInfo info = SkImageInfo::MakeN32(ir.width(), ir.height(), |
| 850 | isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 851 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 852 | SkBaseDevice* device; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 853 | if (paint && paint->getImageFilter()) { |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 854 | device = this->getDevice(); |
| 855 | if (device) { |
| 856 | device = device->createCompatibleDevice(info); |
| 857 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 858 | } else { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 859 | device = this->createLayerDevice(info); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 860 | } |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 861 | if (NULL == device) { |
| 862 | SkDebugf("Unable to create device for layer."); |
| 863 | return count; |
| 864 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 865 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 866 | device->setOrigin(ir.fLeft, ir.fTop); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 867 | 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] | 868 | device->unref(); |
| 869 | |
| 870 | layer->fNext = fMCRec->fTopLayer; |
| 871 | fMCRec->fLayer = layer; |
| 872 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
| 873 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 874 | fSaveLayerCount += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 875 | return count; |
| 876 | } |
| 877 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 878 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) { |
| 879 | return this->saveLayerAlpha(bounds, alpha, kARGB_ClipLayer_SaveFlag); |
| 880 | } |
| 881 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 882 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha, |
| 883 | SaveFlags flags) { |
| 884 | if (0xFF == alpha) { |
| 885 | return this->saveLayer(bounds, NULL, flags); |
| 886 | } else { |
| 887 | SkPaint tmpPaint; |
| 888 | tmpPaint.setAlpha(alpha); |
| 889 | return this->saveLayer(bounds, &tmpPaint, flags); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | void SkCanvas::restore() { |
| 894 | // check for underflow |
| 895 | if (fMCStack.count() > 1) { |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 896 | this->willRestore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 897 | this->internalRestore(); |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | void SkCanvas::internalRestore() { |
| 902 | SkASSERT(fMCStack.count() != 0); |
| 903 | |
| 904 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 905 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 906 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 907 | fClipStack.restore(); |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 908 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 909 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 910 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 911 | // now detach it from fMCRec so we can pop(). Gets freed after its drawn |
| 912 | fMCRec->fLayer = NULL; |
| 913 | |
| 914 | // now do the normal restore() |
| 915 | fMCRec->~MCRec(); // balanced in save() |
| 916 | fMCStack.pop_back(); |
| 917 | fMCRec = (MCRec*)fMCStack.back(); |
| 918 | |
| 919 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 920 | since if we're being recorded, we don't want to record this (the |
| 921 | recorder will have already recorded the restore). |
| 922 | */ |
| 923 | if (NULL != layer) { |
| 924 | if (layer->fNext) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 925 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 926 | this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(), |
| 927 | layer->fPaint); |
| 928 | // reset this, since internalDrawDevice will have set it to true |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 929 | fDeviceCMDirty = true; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 930 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 931 | SkASSERT(fSaveLayerCount > 0); |
| 932 | fSaveLayerCount -= 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 933 | } |
| 934 | SkDELETE(layer); |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 935 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | int SkCanvas::getSaveCount() const { |
| 939 | return fMCStack.count(); |
| 940 | } |
| 941 | |
| 942 | void SkCanvas::restoreToCount(int count) { |
| 943 | // sanity check |
| 944 | if (count < 1) { |
| 945 | count = 1; |
| 946 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 947 | |
reed@google.com | b9d1c6a | 2011-11-28 16:09:24 +0000 | [diff] [blame] | 948 | int n = this->getSaveCount() - count; |
| 949 | for (int i = 0; i < n; ++i) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 950 | this->restore(); |
| 951 | } |
| 952 | } |
| 953 | |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 954 | bool SkCanvas::isDrawingToLayer() const { |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 955 | return fSaveLayerCount > 0; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 956 | } |
| 957 | |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 958 | SkSurface* SkCanvas::newSurface(const SkImageInfo& info) { |
| 959 | return this->onNewSurface(info); |
| 960 | } |
| 961 | |
| 962 | SkSurface* SkCanvas::onNewSurface(const SkImageInfo& info) { |
| 963 | SkBaseDevice* dev = this->getDevice(); |
| 964 | return dev ? dev->newSurface(info) : NULL; |
| 965 | } |
| 966 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 967 | SkImageInfo SkCanvas::imageInfo() const { |
| 968 | SkBaseDevice* dev = this->getDevice(); |
| 969 | if (dev) { |
| 970 | return dev->imageInfo(); |
| 971 | } else { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 972 | return SkImageInfo::MakeUnknown(0, 0); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
| 976 | const void* SkCanvas::peekPixels(SkImageInfo* info, size_t* rowBytes) { |
| 977 | return this->onPeekPixels(info, rowBytes); |
| 978 | } |
| 979 | |
| 980 | const void* SkCanvas::onPeekPixels(SkImageInfo* info, size_t* rowBytes) { |
| 981 | SkBaseDevice* dev = this->getDevice(); |
| 982 | return dev ? dev->peekPixels(info, rowBytes) : NULL; |
| 983 | } |
| 984 | |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 985 | void* SkCanvas::accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin) { |
| 986 | void* pixels = this->onAccessTopLayerPixels(info, rowBytes); |
| 987 | if (pixels && origin) { |
| 988 | *origin = this->getTopDevice(false)->getOrigin(); |
| 989 | } |
| 990 | return pixels; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | void* SkCanvas::onAccessTopLayerPixels(SkImageInfo* info, size_t* rowBytes) { |
| 994 | SkBaseDevice* dev = this->getTopDevice(); |
| 995 | return dev ? dev->accessPixels(info, rowBytes) : NULL; |
| 996 | } |
| 997 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 998 | SkAutoROCanvasPixels::SkAutoROCanvasPixels(SkCanvas* canvas) { |
| 999 | fAddr = canvas->peekPixels(&fInfo, &fRowBytes); |
| 1000 | if (NULL == fAddr) { |
| 1001 | fInfo = canvas->imageInfo(); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 1002 | if (kUnknown_SkColorType == fInfo.colorType() || !fBitmap.allocPixels(fInfo)) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1003 | return; // failure, fAddr is NULL |
| 1004 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1005 | if (!canvas->readPixels(&fBitmap, 0, 0)) { |
| 1006 | return; // failure, fAddr is NULL |
| 1007 | } |
| 1008 | fAddr = fBitmap.getPixels(); |
| 1009 | fRowBytes = fBitmap.rowBytes(); |
| 1010 | } |
| 1011 | SkASSERT(fAddr); // success |
| 1012 | } |
| 1013 | |
| 1014 | bool SkAutoROCanvasPixels::asROBitmap(SkBitmap* bitmap) const { |
| 1015 | if (fAddr) { |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 1016 | return bitmap->installPixels(fInfo, const_cast<void*>(fAddr), fRowBytes); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1017 | } else { |
| 1018 | bitmap->reset(); |
| 1019 | return false; |
| 1020 | } |
| 1021 | } |
| 1022 | |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 1023 | void SkCanvas::onPushCull(const SkRect& cullRect) { |
| 1024 | // do nothing. Subclasses may do something |
| 1025 | } |
| 1026 | |
| 1027 | void SkCanvas::onPopCull() { |
| 1028 | // do nothing. Subclasses may do something |
| 1029 | } |
| 1030 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1031 | ///////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 520cf8b | 2014-03-20 20:25:14 +0000 | [diff] [blame] | 1032 | #ifdef SK_DEBUG |
| 1033 | // Ensure that cull rects are monotonically nested in device space. |
| 1034 | void SkCanvas::validateCull(const SkIRect& devCull) { |
| 1035 | if (fCullStack.isEmpty() |
| 1036 | || devCull.isEmpty() |
| 1037 | || fCullStack.top().contains(devCull)) { |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | SkDEBUGF(("Invalid cull: [%d %d %d %d] (previous cull: [%d %d %d %d])\n", |
| 1042 | devCull.x(), devCull.y(), devCull.right(), devCull.bottom(), |
| 1043 | fCullStack.top().x(), fCullStack.top().y(), |
| 1044 | fCullStack.top().right(), fCullStack.top().bottom())); |
| 1045 | |
| 1046 | #ifdef ASSERT_NESTED_CULLING |
| 1047 | SkDEBUGFAIL("Invalid cull."); |
| 1048 | #endif |
| 1049 | } |
| 1050 | #endif |
| 1051 | |
| 1052 | void SkCanvas::pushCull(const SkRect& cullRect) { |
| 1053 | ++fCullCount; |
| 1054 | this->onPushCull(cullRect); |
| 1055 | |
| 1056 | #ifdef SK_DEBUG |
| 1057 | // Map the cull rect into device space. |
| 1058 | SkRect mappedCull; |
| 1059 | this->getTotalMatrix().mapRect(&mappedCull, cullRect); |
| 1060 | |
| 1061 | // Take clipping into account. |
| 1062 | SkIRect devClip, devCull; |
| 1063 | mappedCull.roundOut(&devCull); |
| 1064 | this->getClipDeviceBounds(&devClip); |
| 1065 | if (!devCull.intersect(devClip)) { |
| 1066 | devCull.setEmpty(); |
| 1067 | } |
| 1068 | |
| 1069 | this->validateCull(devCull); |
| 1070 | fCullStack.push(devCull); // balanced in popCull |
| 1071 | #endif |
| 1072 | } |
| 1073 | |
| 1074 | void SkCanvas::popCull() { |
| 1075 | SkASSERT(fCullStack.count() == fCullCount); |
| 1076 | |
| 1077 | if (fCullCount > 0) { |
| 1078 | --fCullCount; |
| 1079 | this->onPopCull(); |
| 1080 | |
| 1081 | SkDEBUGCODE(fCullStack.pop()); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1086 | |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1087 | void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1088 | const SkMatrix& matrix, const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1089 | if (bitmap.drawsNothing()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1090 | return; |
| 1091 | } |
| 1092 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1093 | SkLazyPaint lazy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1094 | if (NULL == paint) { |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1095 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1096 | } |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1097 | |
| 1098 | SkDEBUGCODE(bitmap.validate();) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1099 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1100 | SkRect storage; |
| 1101 | const SkRect* bounds = NULL; |
| 1102 | if (paint && paint->canComputeFastBounds()) { |
| 1103 | bitmap.getBounds(&storage); |
| 1104 | matrix.mapRect(&storage); |
| 1105 | bounds = &paint->computeFastBounds(storage, &storage); |
| 1106 | } |
| 1107 | |
| 1108 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1109 | |
| 1110 | while (iter.next()) { |
| 1111 | iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint()); |
| 1112 | } |
| 1113 | |
| 1114 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1117 | void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1118 | const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1119 | SkPaint tmp; |
| 1120 | if (NULL == paint) { |
| 1121 | tmp.setDither(true); |
| 1122 | paint = &tmp; |
| 1123 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1124 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1125 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1126 | while (iter.next()) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1127 | SkBaseDevice* dstDev = iter.fDevice; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1128 | paint = &looper.paint(); |
| 1129 | SkImageFilter* filter = paint->getImageFilter(); |
| 1130 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1131 | if (filter && !dstDev->canHandleImageFilter(filter)) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1132 | SkDeviceImageFilterProxy proxy(dstDev); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1133 | SkBitmap dst; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1134 | SkIPoint offset = SkIPoint::Make(0, 0); |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1135 | const SkBitmap& src = srcDev->accessBitmap(false); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1136 | SkMatrix matrix = *iter.fMatrix; |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1137 | matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1138 | SkIRect clipBounds = SkIRect::MakeWH(srcDev->width(), srcDev->height()); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1139 | SkAutoTUnref<SkImageFilter::Cache> cache(dstDev->getImageFilterCache()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1140 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1141 | if (filter->filterImage(&proxy, src, ctx, &dst, &offset)) { |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1142 | SkPaint tmpUnfiltered(*paint); |
| 1143 | tmpUnfiltered.setImageFilter(NULL); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1144 | dstDev->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), |
| 1145 | tmpUnfiltered); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } else { |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1148 | dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1149 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1150 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1151 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1154 | void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, |
| 1155 | const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1156 | if (bitmap.drawsNothing()) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1157 | return; |
| 1158 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1159 | SkDEBUGCODE(bitmap.validate();) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1160 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1161 | SkPaint tmp; |
| 1162 | if (NULL == paint) { |
| 1163 | paint = &tmp; |
| 1164 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1165 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1166 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1167 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1168 | while (iter.next()) { |
| 1169 | paint = &looper.paint(); |
| 1170 | SkImageFilter* filter = paint->getImageFilter(); |
| 1171 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
| 1172 | if (filter && !iter.fDevice->canHandleImageFilter(filter)) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1173 | SkDeviceImageFilterProxy proxy(iter.fDevice); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1174 | SkBitmap dst; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1175 | SkIPoint offset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1176 | SkMatrix matrix = *iter.fMatrix; |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1177 | matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1178 | SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1179 | SkAutoTUnref<SkImageFilter::Cache> cache(iter.fDevice->getImageFilterCache()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1180 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1181 | if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) { |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1182 | SkPaint tmpUnfiltered(*paint); |
| 1183 | tmpUnfiltered.setImageFilter(NULL); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1184 | iter.fDevice->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1185 | tmpUnfiltered); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } else { |
| 1188 | iter.fDevice->drawSprite(iter, bitmap, pos.x(), pos.y(), *paint); |
| 1189 | } |
| 1190 | } |
| 1191 | LOOPER_END |
| 1192 | } |
| 1193 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1194 | ///////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1195 | void SkCanvas::translate(SkScalar dx, SkScalar dy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1196 | SkMatrix m; |
| 1197 | m.setTranslate(dx, dy); |
| 1198 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1201 | void SkCanvas::scale(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1202 | SkMatrix m; |
| 1203 | m.setScale(sx, sy); |
| 1204 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1207 | void SkCanvas::rotate(SkScalar degrees) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1208 | SkMatrix m; |
| 1209 | m.setRotate(degrees); |
| 1210 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1213 | void SkCanvas::skew(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1214 | SkMatrix m; |
| 1215 | m.setSkew(sx, sy); |
| 1216 | this->concat(m); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1219 | void SkCanvas::concat(const SkMatrix& matrix) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1220 | if (matrix.isIdentity()) { |
| 1221 | return; |
| 1222 | } |
| 1223 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1224 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1225 | fCachedLocalClipBoundsDirty = true; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1226 | fMCRec->fMatrix.preConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1227 | |
| 1228 | this->didConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1231 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 1232 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1233 | fCachedLocalClipBoundsDirty = true; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1234 | fMCRec->fMatrix = matrix; |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1235 | this->didSetMatrix(matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1238 | void SkCanvas::resetMatrix() { |
| 1239 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1240 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1241 | matrix.reset(); |
| 1242 | this->setMatrix(matrix); |
| 1243 | } |
| 1244 | |
| 1245 | ////////////////////////////////////////////////////////////////////////////// |
| 1246 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1247 | void SkCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1248 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1249 | this->onClipRect(rect, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | void SkCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1253 | #ifdef SK_ENABLE_CLIP_QUICKREJECT |
| 1254 | if (SkRegion::kIntersect_Op == op) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1255 | if (fMCRec->fRasterClip.isEmpty()) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
| 1258 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1259 | if (this->quickReject(rect)) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1260 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1261 | fCachedLocalClipBoundsDirty = true; |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1262 | |
| 1263 | fClipStack.clipEmpty(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1264 | return fMCRec->fRasterClip.setEmpty(); |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | #endif |
| 1268 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1269 | AutoValidateClip avc(this); |
| 1270 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1271 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1272 | fCachedLocalClipBoundsDirty = true; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1273 | if (!fAllowSoftClip) { |
| 1274 | edgeStyle = kHard_ClipEdgeStyle; |
| 1275 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1276 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1277 | if (fMCRec->fMatrix.rectStaysRect()) { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1278 | // for these simpler matrices, we can stay a rect even after applying |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 1279 | // the matrix. This means we don't have to a) make a path, and b) tell |
| 1280 | // the region code to scan-convert the path, only to discover that it |
| 1281 | // is really just a rect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1282 | SkRect r; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1283 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1284 | fMCRec->fMatrix.mapRect(&r, rect); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1285 | fClipStack.clipDevRect(r, op, kSoft_ClipEdgeStyle == edgeStyle); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1286 | fMCRec->fRasterClip.op(r, op, kSoft_ClipEdgeStyle == edgeStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1287 | } else { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1288 | // since we're rotated or some such thing, we convert the rect to a path |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 1289 | // and clip against that, since it can handle any matrix. However, to |
| 1290 | // avoid recursion in the case where we are subclassed (e.g. Pictures) |
| 1291 | // we explicitly call "our" version of clipPath. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1292 | SkPath path; |
| 1293 | |
| 1294 | path.addRect(rect); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1295 | this->SkCanvas::onClipPath(path, op, edgeStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1299 | static void clip_path_helper(const SkCanvas* canvas, SkRasterClip* currClip, |
| 1300 | const SkPath& devPath, SkRegion::Op op, bool doAA) { |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1301 | // base is used to limit the size (and therefore memory allocation) of the |
| 1302 | // region that results from scan converting devPath. |
| 1303 | SkRegion base; |
| 1304 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1305 | if (SkRegion::kIntersect_Op == op) { |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1306 | // since we are intersect, we can do better (tighter) with currRgn's |
| 1307 | // bounds, than just using the device. However, if currRgn is complex, |
| 1308 | // 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] | 1309 | if (currClip->isRect()) { |
commit-bot@chromium.org | b446fc7 | 2013-07-10 20:55:39 +0000 | [diff] [blame] | 1310 | // FIXME: we should also be able to do this when currClip->isBW(), |
| 1311 | // but relaxing the test above triggers GM asserts in |
| 1312 | // SkRgnBuilder::blitH(). We need to investigate what's going on. |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1313 | currClip->setPath(devPath, currClip->bwRgn(), doAA); |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1314 | } else { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1315 | base.setRect(currClip->getBounds()); |
| 1316 | SkRasterClip clip; |
| 1317 | clip.setPath(devPath, base, doAA); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1318 | currClip->op(clip, op); |
reed@google.com | 759876a | 2011-03-03 14:32:51 +0000 | [diff] [blame] | 1319 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1320 | } else { |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 1321 | const SkISize size = canvas->getBaseLayerSize(); |
| 1322 | base.setRect(0, 0, size.width(), size.height()); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (SkRegion::kReplace_Op == op) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1325 | currClip->setPath(devPath, base, doAA); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1326 | } else { |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1327 | SkRasterClip clip; |
| 1328 | clip.setPath(devPath, base, doAA); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1329 | currClip->op(clip, op); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1334 | void SkCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1335 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1336 | if (rrect.isRect()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1337 | this->onClipRect(rrect.getBounds(), op, edgeStyle); |
| 1338 | } else { |
| 1339 | this->onClipRRect(rrect, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1340 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1341 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1342 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1343 | void SkCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1344 | SkRRect transformedRRect; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1345 | if (rrect.transform(fMCRec->fMatrix, &transformedRRect)) { |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1346 | AutoValidateClip avc(this); |
| 1347 | |
| 1348 | fDeviceCMDirty = true; |
| 1349 | fCachedLocalClipBoundsDirty = true; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1350 | if (!fAllowSoftClip) { |
| 1351 | edgeStyle = kHard_ClipEdgeStyle; |
| 1352 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1353 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1354 | fClipStack.clipDevRRect(transformedRRect, op, kSoft_ClipEdgeStyle == edgeStyle); |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1355 | |
| 1356 | SkPath devPath; |
| 1357 | devPath.addRRect(transformedRRect); |
| 1358 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1359 | clip_path_helper(this, &fMCRec->fRasterClip, devPath, op, kSoft_ClipEdgeStyle == edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1360 | return; |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | SkPath path; |
| 1364 | path.addRRect(rrect); |
| 1365 | // call the non-virtual version |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1366 | this->SkCanvas::onClipPath(path, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1369 | void SkCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1370 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1371 | SkRect r; |
| 1372 | if (!path.isInverseFillType() && path.isRect(&r)) { |
| 1373 | this->onClipRect(r, op, edgeStyle); |
| 1374 | } else { |
| 1375 | this->onClipPath(path, op, edgeStyle); |
| 1376 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | void SkCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1380 | #ifdef SK_ENABLE_CLIP_QUICKREJECT |
| 1381 | if (SkRegion::kIntersect_Op == op && !path.isInverseFillType()) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1382 | if (fMCRec->fRasterClip.isEmpty()) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1383 | return false; |
| 1384 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1385 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1386 | if (this->quickReject(path.getBounds())) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1387 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1388 | fCachedLocalClipBoundsDirty = true; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1389 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1390 | fClipStack.clipEmpty(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1391 | return fMCRec->fRasterClip.setEmpty(); |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | #endif |
| 1395 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1396 | AutoValidateClip avc(this); |
| 1397 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1398 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1399 | fCachedLocalClipBoundsDirty = true; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1400 | if (!fAllowSoftClip) { |
| 1401 | edgeStyle = kHard_ClipEdgeStyle; |
| 1402 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1403 | |
| 1404 | SkPath devPath; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1405 | path.transform(fMCRec->fMatrix, &devPath); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1406 | |
reed@google.com | fe70112 | 2011-11-08 19:41:23 +0000 | [diff] [blame] | 1407 | // Check if the transfomation, or the original path itself |
| 1408 | // made us empty. Note this can also happen if we contained NaN |
| 1409 | // values. computing the bounds detects this, and will set our |
| 1410 | // bounds to empty if that is the case. (see SkRect::set(pts, count)) |
| 1411 | if (devPath.getBounds().isEmpty()) { |
| 1412 | // resetting the path will remove any NaN or other wanky values |
| 1413 | // that might upset our scan converter. |
| 1414 | devPath.reset(); |
| 1415 | } |
| 1416 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1417 | // if we called path.swap() we could avoid a deep copy of this path |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1418 | fClipStack.clipDevPath(devPath, op, kSoft_ClipEdgeStyle == edgeStyle); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1419 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1420 | if (fAllowSimplifyClip) { |
| 1421 | devPath.reset(); |
| 1422 | devPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1423 | const SkClipStack* clipStack = getClipStack(); |
| 1424 | SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 1425 | const SkClipStack::Element* element; |
| 1426 | while ((element = iter.next())) { |
| 1427 | SkClipStack::Element::Type type = element->getType(); |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1428 | SkPath operand; |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 1429 | if (type != SkClipStack::Element::kEmpty_Type) { |
| 1430 | element->asPath(&operand); |
| 1431 | } |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1432 | SkRegion::Op elementOp = element->getOp(); |
| 1433 | if (elementOp == SkRegion::kReplace_Op) { |
| 1434 | devPath = operand; |
| 1435 | } else { |
| 1436 | Op(devPath, operand, (SkPathOp) elementOp, &devPath); |
| 1437 | } |
caryclark@google.com | 96fd344 | 2013-05-07 19:48:31 +0000 | [diff] [blame] | 1438 | // if the prev and curr clips disagree about aa -vs- not, favor the aa request. |
| 1439 | // perhaps we need an API change to avoid this sort of mixed-signals about |
| 1440 | // clipping. |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1441 | if (element->isAA()) { |
| 1442 | edgeStyle = kSoft_ClipEdgeStyle; |
| 1443 | } |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1444 | } |
| 1445 | op = SkRegion::kReplace_Op; |
| 1446 | } |
| 1447 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1448 | clip_path_helper(this, &fMCRec->fRasterClip, devPath, op, edgeStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1451 | void SkCanvas::updateClipConservativelyUsingBounds(const SkRect& bounds, SkRegion::Op op, |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1452 | bool inverseFilled) { |
| 1453 | // This is for updating the clip conservatively using only bounds |
| 1454 | // information. |
| 1455 | // Contract: |
| 1456 | // The current clip must contain the true clip. The true |
| 1457 | // clip is the clip that would have normally been computed |
| 1458 | // by calls to clipPath and clipRRect |
| 1459 | // Objective: |
| 1460 | // Keep the current clip as small as possible without |
| 1461 | // breaking the contract, using only clip bounding rectangles |
| 1462 | // (for performance). |
| 1463 | |
| 1464 | // N.B.: This *never* calls back through a virtual on canvas, so subclasses |
| 1465 | // don't have to worry about getting caught in a loop. Thus anywhere |
| 1466 | // we call a virtual method, we explicitly prefix it with |
| 1467 | // SkCanvas:: to be sure to call the base-class. |
skia.committer@gmail.com | a5d3e77 | 2013-05-30 07:01:29 +0000 | [diff] [blame] | 1468 | |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1469 | if (inverseFilled) { |
| 1470 | switch (op) { |
| 1471 | case SkRegion::kIntersect_Op: |
| 1472 | case SkRegion::kDifference_Op: |
| 1473 | // These ops can only shrink the current clip. So leaving |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1474 | // the clip unchanged conservatively respects the contract. |
| 1475 | break; |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1476 | case SkRegion::kUnion_Op: |
| 1477 | case SkRegion::kReplace_Op: |
| 1478 | case SkRegion::kReverseDifference_Op: |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1479 | case SkRegion::kXOR_Op: { |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1480 | // These ops can grow the current clip up to the extents of |
| 1481 | // the input clip, which is inverse filled, so we just set |
| 1482 | // the current clip to the device bounds. |
| 1483 | SkRect deviceBounds; |
| 1484 | SkIRect deviceIBounds; |
| 1485 | this->getDevice()->getGlobalBounds(&deviceIBounds); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1486 | deviceBounds = SkRect::Make(deviceIBounds); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 1487 | |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1488 | // set the clip in device space |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 1489 | SkMatrix savedMatrix = this->getTotalMatrix(); |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1490 | this->SkCanvas::setMatrix(SkMatrix::I()); |
skia.committer@gmail.com | 370a899 | 2014-03-01 03:02:09 +0000 | [diff] [blame] | 1491 | this->SkCanvas::onClipRect(deviceBounds, SkRegion::kReplace_Op, |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1492 | kHard_ClipEdgeStyle); |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 1493 | this->setMatrix(savedMatrix); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | } |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1496 | default: |
| 1497 | SkASSERT(0); // unhandled op? |
| 1498 | } |
| 1499 | } else { |
| 1500 | // Not inverse filled |
| 1501 | switch (op) { |
| 1502 | case SkRegion::kIntersect_Op: |
| 1503 | case SkRegion::kUnion_Op: |
| 1504 | case SkRegion::kReplace_Op: |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1505 | this->SkCanvas::onClipRect(bounds, op, kHard_ClipEdgeStyle); |
| 1506 | break; |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1507 | case SkRegion::kDifference_Op: |
| 1508 | // Difference can only shrink the current clip. |
| 1509 | // Leaving clip unchanged conservatively fullfills the contract. |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1510 | break; |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1511 | case SkRegion::kReverseDifference_Op: |
| 1512 | // To reverse, we swap in the bounds with a replace op. |
| 1513 | // As with difference, leave it unchanged. |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1514 | this->SkCanvas::onClipRect(bounds, SkRegion::kReplace_Op, kHard_ClipEdgeStyle); |
| 1515 | break; |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1516 | case SkRegion::kXOR_Op: |
| 1517 | // Be conservative, based on (A XOR B) always included in (A union B), |
| 1518 | // which is always included in (bounds(A) union bounds(B)) |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1519 | this->SkCanvas::onClipRect(bounds, SkRegion::kUnion_Op, kHard_ClipEdgeStyle); |
| 1520 | break; |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1521 | default: |
| 1522 | SkASSERT(0); // unhandled op? |
| 1523 | } |
| 1524 | } |
junov@chromium.org | ed8d6bb | 2013-05-29 19:09:48 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1527 | void SkCanvas::clipRegion(const SkRegion& rgn, SkRegion::Op op) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1528 | this->onClipRegion(rgn, op); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | void SkCanvas::onClipRegion(const SkRegion& rgn, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1532 | AutoValidateClip avc(this); |
| 1533 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1534 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1535 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1536 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1537 | // todo: signal fClipStack that we have a region, and therefore (I guess) |
| 1538 | // we have to ignore it, and use the region directly? |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 1539 | fClipStack.clipDevRect(rgn.getBounds(), op); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1540 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1541 | fMCRec->fRasterClip.op(rgn, op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1544 | #ifdef SK_DEBUG |
| 1545 | void SkCanvas::validateClip() const { |
| 1546 | // construct clipRgn from the clipstack |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1547 | const SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1548 | if (!device) { |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1549 | SkASSERT(this->isClipEmpty()); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1550 | return; |
| 1551 | } |
| 1552 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1553 | SkIRect ir; |
| 1554 | ir.set(0, 0, device->width(), device->height()); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1555 | SkRasterClip tmpClip(ir); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1556 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1557 | SkClipStack::B2TIter iter(fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1558 | const SkClipStack::Element* element; |
| 1559 | while ((element = iter.next()) != NULL) { |
| 1560 | switch (element->getType()) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1561 | case SkClipStack::Element::kRect_Type: |
| 1562 | element->getRect().round(&ir); |
| 1563 | tmpClip.op(ir, element->getOp()); |
| 1564 | break; |
| 1565 | case SkClipStack::Element::kEmpty_Type: |
| 1566 | tmpClip.setEmpty(); |
| 1567 | break; |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 1568 | default: { |
| 1569 | SkPath path; |
| 1570 | element->asPath(&path); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1571 | clip_path_helper(this, &tmpClip, path, element->getOp(), element->isAA()); |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 1572 | break; |
| 1573 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1576 | } |
| 1577 | #endif |
| 1578 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1579 | void SkCanvas::replayClips(ClipVisitor* visitor) const { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1580 | SkClipStack::B2TIter iter(fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1581 | const SkClipStack::Element* element; |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1582 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1583 | while ((element = iter.next()) != NULL) { |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 1584 | element->replay(visitor); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1588 | /////////////////////////////////////////////////////////////////////////////// |
| 1589 | |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1590 | bool SkCanvas::isClipEmpty() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1591 | return fMCRec->fRasterClip.isEmpty(); |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1594 | bool SkCanvas::isClipRect() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1595 | return fMCRec->fRasterClip.isRect(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1598 | bool SkCanvas::quickReject(const SkRect& rect) const { |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1599 | |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 1600 | if (!rect.isFinite()) |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1601 | return true; |
| 1602 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1603 | if (fMCRec->fRasterClip.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1604 | return true; |
| 1605 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1606 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1607 | if (fMCRec->fMatrix.hasPerspective()) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1608 | SkRect dst; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1609 | fMCRec->fMatrix.mapRect(&dst, rect); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1610 | SkIRect idst; |
| 1611 | dst.roundOut(&idst); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1612 | return !SkIRect::Intersects(idst, fMCRec->fRasterClip.getBounds()); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1613 | } else { |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1614 | const SkRect& clipR = this->getLocalClipBounds(); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1615 | |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1616 | // for speed, do the most likely reject compares first |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1617 | // TODO: should we use | instead, or compare all 4 at once? |
| 1618 | if (rect.fTop >= clipR.fBottom || rect.fBottom <= clipR.fTop) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1619 | return true; |
| 1620 | } |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1621 | if (rect.fLeft >= clipR.fRight || rect.fRight <= clipR.fLeft) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1622 | return true; |
| 1623 | } |
| 1624 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1625 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1628 | bool SkCanvas::quickReject(const SkPath& path) const { |
| 1629 | return path.isEmpty() || this->quickReject(path.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1632 | bool SkCanvas::getClipBounds(SkRect* bounds) const { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1633 | SkIRect ibounds; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1634 | if (!this->getClipDeviceBounds(&ibounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1635 | return false; |
| 1636 | } |
| 1637 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1638 | SkMatrix inverse; |
| 1639 | // if we can't invert the CTM, we can't return local clip bounds |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1640 | if (!fMCRec->fMatrix.invert(&inverse)) { |
reed@android.com | 72dcd3a | 2009-05-18 15:46:29 +0000 | [diff] [blame] | 1641 | if (bounds) { |
| 1642 | bounds->setEmpty(); |
| 1643 | } |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1644 | return false; |
| 1645 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1646 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1647 | if (NULL != bounds) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1648 | SkRect r; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1649 | // adjust it outwards in case we are antialiasing |
| 1650 | const int inset = 1; |
reed@google.com | fa4d5bd | 2012-01-30 17:36:27 +0000 | [diff] [blame] | 1651 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 1652 | r.iset(ibounds.fLeft - inset, ibounds.fTop - inset, |
| 1653 | ibounds.fRight + inset, ibounds.fBottom + inset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1654 | inverse.mapRect(bounds, r); |
| 1655 | } |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1659 | bool SkCanvas::getClipDeviceBounds(SkIRect* bounds) const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1660 | const SkRasterClip& clip = fMCRec->fRasterClip; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1661 | if (clip.isEmpty()) { |
| 1662 | if (bounds) { |
| 1663 | bounds->setEmpty(); |
| 1664 | } |
| 1665 | return false; |
| 1666 | } |
| 1667 | |
| 1668 | if (NULL != bounds) { |
| 1669 | *bounds = clip.getBounds(); |
| 1670 | } |
| 1671 | return true; |
| 1672 | } |
| 1673 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1674 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1675 | return fMCRec->fMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1678 | #ifdef SK_SUPPORT_LEGACY_GETCLIPTYPE |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1679 | SkCanvas::ClipType SkCanvas::getClipType() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1680 | if (fMCRec->fRasterClip.isEmpty()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1681 | return kEmpty_ClipType; |
| 1682 | } |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1683 | if (fMCRec->fRasterClip.isRect()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1684 | return kRect_ClipType; |
| 1685 | } |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1686 | return kComplex_ClipType; |
| 1687 | } |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1688 | #endif |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1689 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1690 | const SkRegion& SkCanvas::internal_private_getTotalClip() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1691 | return fMCRec->fRasterClip.forceGetBW(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | void SkCanvas::internal_private_getTotalClipAsPath(SkPath* path) const { |
| 1695 | path->reset(); |
| 1696 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1697 | const SkRegion& rgn = fMCRec->fRasterClip.forceGetBW(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1698 | if (rgn.isEmpty()) { |
| 1699 | return; |
| 1700 | } |
| 1701 | (void)rgn.getBoundaryPath(path); |
| 1702 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1703 | |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1704 | GrRenderTarget* SkCanvas::internal_private_accessTopLayerRenderTarget() { |
| 1705 | SkBaseDevice* dev = this->getTopDevice(); |
| 1706 | return dev ? dev->accessRenderTarget() : NULL; |
| 1707 | } |
| 1708 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1709 | SkBaseDevice* SkCanvas::createLayerDevice(const SkImageInfo& info) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1710 | SkBaseDevice* device = this->getTopDevice(); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1711 | return device ? device->createCompatibleDeviceForSaveLayer(info) : NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1714 | GrContext* SkCanvas::getGrContext() { |
| 1715 | #if SK_SUPPORT_GPU |
| 1716 | SkBaseDevice* device = this->getTopDevice(); |
| 1717 | if (NULL != device) { |
| 1718 | GrRenderTarget* renderTarget = device->accessRenderTarget(); |
| 1719 | if (NULL != renderTarget) { |
| 1720 | return renderTarget->getContext(); |
| 1721 | } |
| 1722 | } |
| 1723 | #endif |
| 1724 | |
| 1725 | return NULL; |
| 1726 | |
| 1727 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1728 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1729 | void SkCanvas::drawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1730 | const SkPaint& paint) { |
| 1731 | if (outer.isEmpty()) { |
| 1732 | return; |
| 1733 | } |
| 1734 | if (inner.isEmpty()) { |
| 1735 | this->drawRRect(outer, paint); |
| 1736 | return; |
| 1737 | } |
| 1738 | |
| 1739 | // We don't have this method (yet), but technically this is what we should |
| 1740 | // be able to assert... |
| 1741 | // SkASSERT(outer.contains(inner)); |
| 1742 | // |
| 1743 | // For now at least check for containment of bounds |
| 1744 | SkASSERT(outer.getBounds().contains(inner.getBounds())); |
| 1745 | |
| 1746 | this->onDrawDRRect(outer, inner, paint); |
| 1747 | } |
| 1748 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1749 | ////////////////////////////////////////////////////////////////////////////// |
| 1750 | // These are the virtual drawing methods |
| 1751 | ////////////////////////////////////////////////////////////////////////////// |
| 1752 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1753 | void SkCanvas::clear(SkColor color) { |
| 1754 | SkDrawIter iter(this); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 1755 | this->predrawNotify(); |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1756 | while (iter.next()) { |
| 1757 | iter.fDevice->clear(color); |
| 1758 | } |
| 1759 | } |
| 1760 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1761 | void SkCanvas::onDiscard() { |
| 1762 | if (NULL != fSurfaceBase) { |
| 1763 | fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); |
| 1764 | } |
| 1765 | } |
| 1766 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1767 | void SkCanvas::drawPaint(const SkPaint& paint) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1768 | this->internalDrawPaint(paint); |
| 1769 | } |
| 1770 | |
| 1771 | void SkCanvas::internalDrawPaint(const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1772 | LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1773 | |
| 1774 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1775 | iter.fDevice->drawPaint(iter, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1778 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 1782 | const SkPaint& paint) { |
| 1783 | if ((long)count <= 0) { |
| 1784 | return; |
| 1785 | } |
| 1786 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1787 | SkRect r, storage; |
| 1788 | const SkRect* bounds = NULL; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1789 | if (paint.canComputeFastBounds()) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1790 | // special-case 2 points (common for drawing a single line) |
| 1791 | if (2 == count) { |
| 1792 | r.set(pts[0], pts[1]); |
| 1793 | } else { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 1794 | r.set(pts, SkToInt(count)); |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1795 | } |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1796 | bounds = &paint.computeFastStrokeBounds(r, &storage); |
| 1797 | if (this->quickReject(*bounds)) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1798 | return; |
| 1799 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1800 | } |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1801 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1802 | SkASSERT(pts != NULL); |
| 1803 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1804 | LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1805 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1806 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1807 | iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1808 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1809 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1810 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 1813 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1814 | SkRect storage; |
| 1815 | const SkRect* bounds = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1816 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1817 | bounds = &paint.computeFastBounds(r, &storage); |
| 1818 | if (this->quickReject(*bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1819 | return; |
| 1820 | } |
| 1821 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1822 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1823 | LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1824 | |
| 1825 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1826 | iter.fDevice->drawRect(iter, r, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1827 | } |
| 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 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1832 | void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1833 | SkRect storage; |
| 1834 | const SkRect* bounds = NULL; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1835 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1836 | bounds = &paint.computeFastBounds(oval, &storage); |
| 1837 | if (this->quickReject(*bounds)) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1838 | return; |
| 1839 | } |
| 1840 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 1841 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1842 | LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds) |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1843 | |
| 1844 | while (iter.next()) { |
| 1845 | iter.fDevice->drawOval(iter, oval, looper.paint()); |
| 1846 | } |
| 1847 | |
| 1848 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1852 | SkRect storage; |
| 1853 | const SkRect* bounds = NULL; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1854 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1855 | bounds = &paint.computeFastBounds(rrect.getBounds(), &storage); |
| 1856 | if (this->quickReject(*bounds)) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1857 | return; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | if (rrect.isRect()) { |
| 1862 | // call the non-virtual version |
| 1863 | this->SkCanvas::drawRect(rrect.getBounds(), paint); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1864 | return; |
| 1865 | } else if (rrect.isOval()) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1866 | // call the non-virtual version |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1867 | this->SkCanvas::drawOval(rrect.getBounds(), paint); |
| 1868 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1869 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1870 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1871 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1872 | |
| 1873 | while (iter.next()) { |
| 1874 | iter.fDevice->drawRRect(iter, rrect, looper.paint()); |
| 1875 | } |
| 1876 | |
| 1877 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1880 | void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1881 | const SkPaint& paint) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1882 | SkRect storage; |
| 1883 | const SkRect* bounds = NULL; |
| 1884 | if (paint.canComputeFastBounds()) { |
| 1885 | bounds = &paint.computeFastBounds(outer.getBounds(), &storage); |
| 1886 | if (this->quickReject(*bounds)) { |
| 1887 | return; |
| 1888 | } |
| 1889 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 1890 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1891 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 1892 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1893 | while (iter.next()) { |
| 1894 | iter.fDevice->drawDRRect(iter, outer, inner, looper.paint()); |
| 1895 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 1896 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1897 | LOOPER_END |
| 1898 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1899 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 1900 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | 9364511 | 2012-07-26 16:11:47 +0000 | [diff] [blame] | 1901 | if (!path.isFinite()) { |
| 1902 | return; |
| 1903 | } |
| 1904 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1905 | SkRect storage; |
| 1906 | const SkRect* bounds = NULL; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1907 | if (!path.isInverseFillType() && paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1908 | const SkRect& pathBounds = path.getBounds(); |
| 1909 | bounds = &paint.computeFastBounds(pathBounds, &storage); |
| 1910 | if (this->quickReject(*bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1911 | return; |
| 1912 | } |
| 1913 | } |
commit-bot@chromium.org | 0b45dc4 | 2014-02-21 05:42:57 +0000 | [diff] [blame] | 1914 | |
| 1915 | const SkRect& r = path.getBounds(); |
| 1916 | if (r.width() <= 0 && r.height() <= 0) { |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 1917 | if (path.isInverseFillType()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1918 | this->internalDrawPaint(paint); |
| 1919 | } |
| 1920 | return; |
| 1921 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1922 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1923 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1924 | |
| 1925 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1926 | iter.fDevice->drawPath(iter, path, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1929 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 1933 | const SkPaint* paint) { |
| 1934 | SkDEBUGCODE(bitmap.validate();) |
| 1935 | |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1936 | if (NULL == paint || paint->canComputeFastBounds()) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 1937 | SkRect bounds = { |
| 1938 | x, y, |
| 1939 | x + SkIntToScalar(bitmap.width()), |
| 1940 | y + SkIntToScalar(bitmap.height()) |
| 1941 | }; |
| 1942 | if (paint) { |
| 1943 | (void)paint->computeFastBounds(bounds, &bounds); |
| 1944 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1945 | if (this->quickReject(bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1949 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1950 | SkMatrix matrix; |
| 1951 | matrix.setTranslate(x, y); |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1952 | this->internalDrawBitmap(bitmap, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1955 | // 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] | 1956 | void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1957 | const SkRect& dst, const SkPaint* paint, |
| 1958 | DrawBitmapRectFlags flags) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1959 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1960 | return; |
| 1961 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1962 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1963 | SkRect storage; |
| 1964 | const SkRect* bounds = &dst; |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1965 | if (NULL == paint || paint->canComputeFastBounds()) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 1966 | if (paint) { |
| 1967 | bounds = &paint->computeFastBounds(dst, &storage); |
| 1968 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1969 | if (this->quickReject(*bounds)) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1970 | return; |
| 1971 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1972 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1973 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1974 | SkLazyPaint lazy; |
| 1975 | if (NULL == paint) { |
| 1976 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1977 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1978 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1979 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1980 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1981 | while (iter.next()) { |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1982 | iter.fDevice->drawBitmapRect(iter, bitmap, src, dst, looper.paint(), flags); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1983 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1984 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1985 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1988 | void SkCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1989 | const SkRect& dst, const SkPaint* paint, |
| 1990 | DrawBitmapRectFlags flags) { |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1991 | SkDEBUGCODE(bitmap.validate();) |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1992 | this->internalDrawBitmapRect(bitmap, src, dst, paint, flags); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1995 | void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, |
| 1996 | const SkPaint* paint) { |
| 1997 | SkDEBUGCODE(bitmap.validate();) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1998 | this->internalDrawBitmap(bitmap, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2001 | void SkCanvas::internalDrawBitmapNine(const SkBitmap& bitmap, |
| 2002 | const SkIRect& center, const SkRect& dst, |
| 2003 | const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 2004 | if (bitmap.drawsNothing()) { |
| 2005 | return; |
| 2006 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2007 | if (NULL == paint || paint->canComputeFastBounds()) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 2008 | SkRect storage; |
| 2009 | const SkRect* bounds = &dst; |
| 2010 | if (paint) { |
| 2011 | bounds = &paint->computeFastBounds(dst, &storage); |
| 2012 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 2013 | if (this->quickReject(*bounds)) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
| 2016 | } |
| 2017 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2018 | const int32_t w = bitmap.width(); |
| 2019 | const int32_t h = bitmap.height(); |
| 2020 | |
| 2021 | SkIRect c = center; |
| 2022 | // pin center to the bounds of the bitmap |
| 2023 | c.fLeft = SkMax32(0, center.fLeft); |
| 2024 | c.fTop = SkMax32(0, center.fTop); |
| 2025 | c.fRight = SkPin32(center.fRight, c.fLeft, w); |
| 2026 | c.fBottom = SkPin32(center.fBottom, c.fTop, h); |
| 2027 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2028 | const SkScalar srcX[4] = { |
rmistry@google.com | 7d474f8 | 2013-01-02 22:03:54 +0000 | [diff] [blame] | 2029 | 0, SkIntToScalar(c.fLeft), SkIntToScalar(c.fRight), SkIntToScalar(w) |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2030 | }; |
| 2031 | const SkScalar srcY[4] = { |
rmistry@google.com | 7d474f8 | 2013-01-02 22:03:54 +0000 | [diff] [blame] | 2032 | 0, SkIntToScalar(c.fTop), SkIntToScalar(c.fBottom), SkIntToScalar(h) |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2033 | }; |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2034 | SkScalar dstX[4] = { |
| 2035 | dst.fLeft, dst.fLeft + SkIntToScalar(c.fLeft), |
| 2036 | dst.fRight - SkIntToScalar(w - c.fRight), dst.fRight |
| 2037 | }; |
| 2038 | SkScalar dstY[4] = { |
| 2039 | dst.fTop, dst.fTop + SkIntToScalar(c.fTop), |
| 2040 | dst.fBottom - SkIntToScalar(h - c.fBottom), dst.fBottom |
| 2041 | }; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2042 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2043 | if (dstX[1] > dstX[2]) { |
| 2044 | dstX[1] = dstX[0] + (dstX[3] - dstX[0]) * c.fLeft / (w - c.width()); |
| 2045 | dstX[2] = dstX[1]; |
| 2046 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2047 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2048 | if (dstY[1] > dstY[2]) { |
| 2049 | dstY[1] = dstY[0] + (dstY[3] - dstY[0]) * c.fTop / (h - c.height()); |
| 2050 | dstY[2] = dstY[1]; |
| 2051 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2052 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2053 | for (int y = 0; y < 3; y++) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2054 | SkRect s, d; |
| 2055 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2056 | s.fTop = srcY[y]; |
| 2057 | s.fBottom = srcY[y+1]; |
| 2058 | d.fTop = dstY[y]; |
| 2059 | d.fBottom = dstY[y+1]; |
| 2060 | for (int x = 0; x < 3; x++) { |
| 2061 | s.fLeft = srcX[x]; |
| 2062 | s.fRight = srcX[x+1]; |
| 2063 | d.fLeft = dstX[x]; |
| 2064 | d.fRight = dstX[x+1]; |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 2065 | this->internalDrawBitmapRect(bitmap, &s, d, paint, |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 2066 | kNone_DrawBitmapRectFlag); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 2072 | const SkRect& dst, const SkPaint* paint) { |
| 2073 | SkDEBUGCODE(bitmap.validate();) |
| 2074 | |
| 2075 | // Need a device entry-point, so gpu can use a mesh |
| 2076 | this->internalDrawBitmapNine(bitmap, center, dst, paint); |
| 2077 | } |
| 2078 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2079 | class SkDeviceFilteredPaint { |
| 2080 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 2081 | SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) { |
| 2082 | SkBaseDevice::TextFlags flags; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2083 | if (device->filterTextFlags(paint, &flags)) { |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 2084 | SkPaint* newPaint = fLazy.set(paint); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2085 | newPaint->setFlags(flags.fFlags); |
| 2086 | newPaint->setHinting(flags.fHinting); |
| 2087 | fPaint = newPaint; |
| 2088 | } else { |
| 2089 | fPaint = &paint; |
| 2090 | } |
| 2091 | } |
| 2092 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2093 | const SkPaint& paint() const { return *fPaint; } |
| 2094 | |
| 2095 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 2096 | const SkPaint* fPaint; |
| 2097 | SkLazyPaint fLazy; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2098 | }; |
| 2099 | |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2100 | void SkCanvas::DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 2101 | const SkRect& r, SkScalar textSize) { |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 2102 | if (paint.getStyle() == SkPaint::kFill_Style) { |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2103 | draw.fDevice->drawRect(draw, r, paint); |
| 2104 | } else { |
| 2105 | SkPaint p(paint); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 2106 | p.setStrokeWidth(SkScalarMul(textSize, paint.getStrokeWidth())); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2107 | draw.fDevice->drawRect(draw, r, p); |
| 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | void SkCanvas::DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 2112 | const char text[], size_t byteLength, |
| 2113 | SkScalar x, SkScalar y) { |
| 2114 | SkASSERT(byteLength == 0 || text != NULL); |
| 2115 | |
| 2116 | // nothing to draw |
| 2117 | if (text == NULL || byteLength == 0 || |
| 2118 | draw.fClip->isEmpty() || |
| 2119 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 2120 | return; |
| 2121 | } |
| 2122 | |
| 2123 | SkScalar width = 0; |
| 2124 | SkPoint start; |
| 2125 | |
| 2126 | start.set(0, 0); // to avoid warning |
| 2127 | if (paint.getFlags() & (SkPaint::kUnderlineText_Flag | |
| 2128 | SkPaint::kStrikeThruText_Flag)) { |
| 2129 | width = paint.measureText(text, byteLength); |
| 2130 | |
| 2131 | SkScalar offsetX = 0; |
| 2132 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 2133 | offsetX = SkScalarHalf(width); |
| 2134 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
| 2135 | offsetX = width; |
| 2136 | } |
| 2137 | start.set(x - offsetX, y); |
| 2138 | } |
| 2139 | |
| 2140 | if (0 == width) { |
| 2141 | return; |
| 2142 | } |
| 2143 | |
| 2144 | uint32_t flags = paint.getFlags(); |
| 2145 | |
| 2146 | if (flags & (SkPaint::kUnderlineText_Flag | |
| 2147 | SkPaint::kStrikeThruText_Flag)) { |
| 2148 | SkScalar textSize = paint.getTextSize(); |
| 2149 | SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness); |
| 2150 | SkRect r; |
| 2151 | |
| 2152 | r.fLeft = start.fX; |
| 2153 | r.fRight = start.fX + width; |
| 2154 | |
| 2155 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 2156 | SkScalar offset = SkScalarMulAdd(textSize, kStdUnderline_Offset, |
| 2157 | start.fY); |
| 2158 | r.fTop = offset; |
| 2159 | r.fBottom = offset + height; |
| 2160 | DrawRect(draw, paint, r, textSize); |
| 2161 | } |
| 2162 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 2163 | SkScalar offset = SkScalarMulAdd(textSize, kStdStrikeThru_Offset, |
| 2164 | start.fY); |
| 2165 | r.fTop = offset; |
| 2166 | r.fBottom = offset + height; |
| 2167 | DrawRect(draw, paint, r, textSize); |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2172 | void SkCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 2173 | const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2174 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2175 | |
| 2176 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2177 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2178 | iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint()); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2179 | DrawTextDecorations(iter, dfp.paint(), |
| 2180 | static_cast<const char*>(text), byteLength, x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2183 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2186 | void SkCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 2187 | const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2188 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2189 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2190 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2191 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2192 | iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2193 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2194 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2195 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2196 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2199 | void SkCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 2200 | SkScalar constY, const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2201 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2203 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2204 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2205 | iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2206 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2207 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2208 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2209 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2212 | void SkCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 2213 | const SkMatrix* matrix, const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2214 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2216 | while (iter.next()) { |
| 2217 | iter.fDevice->drawTextOnPath(iter, text, byteLength, path, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2218 | matrix, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2219 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2220 | |
commit-bot@chromium.org | 945ec3a | 2014-04-22 20:07:30 +0000 | [diff] [blame] | 2221 | LOOPER_END |
commit-bot@chromium.org | 4325d11 | 2014-04-22 19:03:02 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2224 | // These will become non-virtual, so they always call the (virtual) onDraw... method |
| 2225 | void SkCanvas::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 2226 | const SkPaint& paint) { |
| 2227 | this->onDrawText(text, byteLength, x, y, paint); |
| 2228 | } |
| 2229 | void SkCanvas::drawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 2230 | const SkPaint& paint) { |
| 2231 | this->onDrawPosText(text, byteLength, pos, paint); |
| 2232 | } |
| 2233 | void SkCanvas::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 2234 | SkScalar constY, const SkPaint& paint) { |
| 2235 | this->onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| 2236 | } |
| 2237 | void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 2238 | const SkMatrix* matrix, const SkPaint& paint) { |
| 2239 | this->onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| 2240 | } |
| 2241 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2242 | void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 2243 | const SkPoint verts[], const SkPoint texs[], |
| 2244 | const SkColor colors[], SkXfermode* xmode, |
| 2245 | const uint16_t indices[], int indexCount, |
| 2246 | const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2247 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2248 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2249 | while (iter.next()) { |
| 2250 | iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2251 | colors, xmode, indices, indexCount, |
| 2252 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2253 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2254 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2255 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame^] | 2258 | void SkCanvas::drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 2259 | const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) { |
| 2260 | if (NULL == cubics) { |
| 2261 | return; |
| 2262 | } |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2263 | |
| 2264 | // Since a patch is always within the convex hull of the control points, we discard it when its |
| 2265 | // bounding rectangle is completely outside the current clip. |
| 2266 | SkRect bounds; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame^] | 2267 | bounds.set(cubics, SkPatchUtils::kNumCtrlPts); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2268 | if (this->quickReject(bounds)) { |
| 2269 | return; |
| 2270 | } |
| 2271 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame^] | 2272 | this->onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| 2273 | } |
| 2274 | |
| 2275 | void SkCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 2276 | const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) { |
| 2277 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2278 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL) |
| 2279 | |
| 2280 | while (iter.next()) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame^] | 2281 | iter.fDevice->drawPatch(iter, cubics, colors, texCoords, xmode, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | LOOPER_END |
| 2285 | } |
| 2286 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2287 | ////////////////////////////////////////////////////////////////////////////// |
| 2288 | // These methods are NOT virtual, and therefore must call back into virtual |
| 2289 | // methods, rather than actually drawing themselves. |
| 2290 | ////////////////////////////////////////////////////////////////////////////// |
| 2291 | |
| 2292 | void SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2293 | SkXfermode::Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2294 | SkPaint paint; |
| 2295 | |
| 2296 | paint.setARGB(a, r, g, b); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2297 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2298 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2299 | } |
| 2300 | this->drawPaint(paint); |
| 2301 | } |
| 2302 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2303 | void SkCanvas::drawColor(SkColor c, SkXfermode::Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2304 | SkPaint paint; |
| 2305 | |
| 2306 | paint.setColor(c); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2307 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2308 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2309 | } |
| 2310 | this->drawPaint(paint); |
| 2311 | } |
| 2312 | |
| 2313 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
| 2314 | SkPoint pt; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2316 | pt.set(x, y); |
| 2317 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2318 | } |
| 2319 | |
| 2320 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) { |
| 2321 | SkPoint pt; |
| 2322 | SkPaint paint; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2323 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2324 | pt.set(x, y); |
| 2325 | paint.setColor(color); |
| 2326 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2327 | } |
| 2328 | |
| 2329 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 2330 | const SkPaint& paint) { |
| 2331 | SkPoint pts[2]; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2332 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2333 | pts[0].set(x0, y0); |
| 2334 | pts[1].set(x1, y1); |
| 2335 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 2336 | } |
| 2337 | |
| 2338 | void SkCanvas::drawRectCoords(SkScalar left, SkScalar top, |
| 2339 | SkScalar right, SkScalar bottom, |
| 2340 | const SkPaint& paint) { |
| 2341 | SkRect r; |
| 2342 | |
| 2343 | r.set(left, top, right, bottom); |
| 2344 | this->drawRect(r, paint); |
| 2345 | } |
| 2346 | |
| 2347 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 2348 | const SkPaint& paint) { |
| 2349 | if (radius < 0) { |
| 2350 | radius = 0; |
| 2351 | } |
| 2352 | |
| 2353 | SkRect r; |
| 2354 | r.set(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2355 | this->drawOval(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 2359 | const SkPaint& paint) { |
| 2360 | if (rx > 0 && ry > 0) { |
| 2361 | if (paint.canComputeFastBounds()) { |
| 2362 | SkRect storage; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 2363 | if (this->quickReject(paint.computeFastBounds(r, &storage))) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2364 | return; |
| 2365 | } |
| 2366 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2367 | SkRRect rrect; |
| 2368 | rrect.setRectXY(r, rx, ry); |
| 2369 | this->drawRRect(rrect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2370 | } else { |
| 2371 | this->drawRect(r, paint); |
| 2372 | } |
| 2373 | } |
| 2374 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2375 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 2376 | SkScalar sweepAngle, bool useCenter, |
| 2377 | const SkPaint& paint) { |
| 2378 | if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) { |
| 2379 | this->drawOval(oval, paint); |
| 2380 | } else { |
| 2381 | SkPath path; |
| 2382 | if (useCenter) { |
| 2383 | path.moveTo(oval.centerX(), oval.centerY()); |
| 2384 | } |
| 2385 | path.arcTo(oval, startAngle, sweepAngle, !useCenter); |
| 2386 | if (useCenter) { |
| 2387 | path.close(); |
| 2388 | } |
| 2389 | this->drawPath(path, paint); |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | void SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength, |
| 2394 | const SkPath& path, SkScalar hOffset, |
| 2395 | SkScalar vOffset, const SkPaint& paint) { |
| 2396 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2397 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2398 | matrix.setTranslate(hOffset, vOffset); |
| 2399 | this->drawTextOnPath(text, byteLength, path, &matrix, paint); |
| 2400 | } |
| 2401 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 2402 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2403 | void SkCanvas::EXPERIMENTAL_optimize(const SkPicture* picture) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2404 | SkBaseDevice* device = this->getDevice(); |
| 2405 | if (NULL != device) { |
| 2406 | device->EXPERIMENTAL_optimize(picture); |
| 2407 | } |
| 2408 | } |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 2409 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2410 | void SkCanvas::drawPicture(const SkPicture* picture) { |
| 2411 | if (NULL != picture) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2412 | this->onDrawPicture(picture, NULL, NULL); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2416 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { |
| 2417 | if (NULL != picture) { |
| 2418 | if (matrix && matrix->isIdentity()) { |
| 2419 | matrix = NULL; |
| 2420 | } |
| 2421 | this->onDrawPicture(picture, matrix, paint); |
| 2422 | } |
| 2423 | } |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2424 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2425 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 2426 | const SkPaint* paint) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2427 | SkBaseDevice* device = this->getTopDevice(); |
| 2428 | if (NULL != device) { |
| 2429 | // Canvas has to first give the device the opportunity to render |
| 2430 | // the picture itself. |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2431 | if (device->EXPERIMENTAL_drawPicture(this, picture, matrix, paint)) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2432 | return; // the device has rendered the entire picture |
| 2433 | } |
| 2434 | } |
| 2435 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2436 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->width(), picture->height()); |
| 2437 | |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2438 | picture->draw(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2441 | /////////////////////////////////////////////////////////////////////////////// |
| 2442 | /////////////////////////////////////////////////////////////////////////////// |
| 2443 | |
| 2444 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) { |
reed@google.com | d642329 | 2010-12-20 20:53:13 +0000 | [diff] [blame] | 2445 | SK_COMPILE_ASSERT(sizeof(fStorage) >= sizeof(SkDrawIter), fStorage_too_small); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2446 | |
| 2447 | SkASSERT(canvas); |
| 2448 | |
| 2449 | fImpl = new (fStorage) SkDrawIter(canvas, skipEmptyClips); |
| 2450 | fDone = !fImpl->next(); |
| 2451 | } |
| 2452 | |
| 2453 | SkCanvas::LayerIter::~LayerIter() { |
| 2454 | fImpl->~SkDrawIter(); |
| 2455 | } |
| 2456 | |
| 2457 | void SkCanvas::LayerIter::next() { |
| 2458 | fDone = !fImpl->next(); |
| 2459 | } |
| 2460 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 2461 | SkBaseDevice* SkCanvas::LayerIter::device() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2462 | return fImpl->getDevice(); |
| 2463 | } |
| 2464 | |
| 2465 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
| 2466 | return fImpl->getMatrix(); |
| 2467 | } |
| 2468 | |
| 2469 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 2470 | const SkPaint* paint = fImpl->getPaint(); |
| 2471 | if (NULL == paint) { |
| 2472 | paint = &fDefaultPaint; |
| 2473 | } |
| 2474 | return *paint; |
| 2475 | } |
| 2476 | |
| 2477 | const SkRegion& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); } |
| 2478 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 2479 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 2480 | |
| 2481 | /////////////////////////////////////////////////////////////////////////////// |
| 2482 | |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 2483 | SkCanvasClipVisitor::~SkCanvasClipVisitor() { } |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 2484 | |
| 2485 | /////////////////////////////////////////////////////////////////////////////// |
| 2486 | |
| 2487 | static bool supported_for_raster_canvas(const SkImageInfo& info) { |
| 2488 | switch (info.alphaType()) { |
| 2489 | case kPremul_SkAlphaType: |
| 2490 | case kOpaque_SkAlphaType: |
| 2491 | break; |
| 2492 | default: |
| 2493 | return false; |
| 2494 | } |
| 2495 | |
| 2496 | switch (info.colorType()) { |
| 2497 | case kAlpha_8_SkColorType: |
| 2498 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 2499 | case kN32_SkColorType: |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 2500 | break; |
| 2501 | default: |
| 2502 | return false; |
| 2503 | } |
| 2504 | |
| 2505 | return true; |
| 2506 | } |
| 2507 | |
| 2508 | SkCanvas* SkCanvas::NewRaster(const SkImageInfo& info) { |
| 2509 | if (!supported_for_raster_canvas(info)) { |
| 2510 | return NULL; |
| 2511 | } |
| 2512 | |
| 2513 | SkBitmap bitmap; |
| 2514 | if (!bitmap.allocPixels(info)) { |
| 2515 | return NULL; |
| 2516 | } |
| 2517 | |
| 2518 | // should this functionality be moved into allocPixels()? |
| 2519 | if (!bitmap.info().isOpaque()) { |
| 2520 | bitmap.eraseColor(0); |
| 2521 | } |
| 2522 | return SkNEW_ARGS(SkCanvas, (bitmap)); |
| 2523 | } |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 2524 | |
| 2525 | SkCanvas* SkCanvas::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 2526 | if (!supported_for_raster_canvas(info)) { |
| 2527 | return NULL; |
| 2528 | } |
skia.committer@gmail.com | eb849e5 | 2014-03-17 03:02:17 +0000 | [diff] [blame] | 2529 | |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 2530 | SkBitmap bitmap; |
| 2531 | if (!bitmap.installPixels(info, pixels, rowBytes)) { |
| 2532 | return NULL; |
| 2533 | } |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 2534 | return SkNEW_ARGS(SkCanvas, (bitmap)); |
| 2535 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2536 | |
| 2537 | /////////////////////////////////////////////////////////////////////////////// |
| 2538 | |
| 2539 | SkAutoCanvasMatrixPaint::SkAutoCanvasMatrixPaint(SkCanvas* canvas, const SkMatrix* matrix, |
| 2540 | const SkPaint* paint, int width, int height) |
| 2541 | : fCanvas(canvas) |
| 2542 | , fSaveCount(canvas->getSaveCount()) |
| 2543 | { |
| 2544 | if (NULL != paint) { |
| 2545 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)); |
| 2546 | if (matrix) { |
| 2547 | matrix->mapRect(&bounds); |
| 2548 | } |
| 2549 | canvas->saveLayer(&bounds, paint); |
| 2550 | } else if (NULL != matrix) { |
| 2551 | canvas->save(); |
| 2552 | } |
| 2553 | |
| 2554 | if (NULL != matrix) { |
| 2555 | canvas->concat(*matrix); |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
| 2560 | fCanvas->restoreToCount(fSaveCount); |
| 2561 | } |