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