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