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