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