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