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