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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/core/SkImageFilter.h" |
| 13 | #include "include/core/SkPathEffect.h" |
| 14 | #include "include/core/SkPicture.h" |
| 15 | #include "include/core/SkRRect.h" |
| 16 | #include "include/core/SkRasterHandleAllocator.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTextBlob.h" |
| 19 | #include "include/core/SkVertices.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/private/SkNx.h" |
| 21 | #include "include/private/SkTo.h" |
| 22 | #include "include/utils/SkNoDrawCanvas.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 23 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/core/SkBitmapDevice.h" |
| 25 | #include "src/core/SkCanvasPriv.h" |
| 26 | #include "src/core/SkClipOpPriv.h" |
| 27 | #include "src/core/SkClipStack.h" |
| 28 | #include "src/core/SkDraw.h" |
| 29 | #include "src/core/SkGlyphRun.h" |
| 30 | #include "src/core/SkImageFilterCache.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 31 | #include "src/core/SkImageFilter_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/core/SkLatticeIter.h" |
| 33 | #include "src/core/SkMSAN.h" |
| 34 | #include "src/core/SkMakeUnique.h" |
| 35 | #include "src/core/SkMatrixUtils.h" |
| 36 | #include "src/core/SkPaintPriv.h" |
| 37 | #include "src/core/SkRasterClip.h" |
| 38 | #include "src/core/SkSpecialImage.h" |
| 39 | #include "src/core/SkStrikeCache.h" |
| 40 | #include "src/core/SkTLazy.h" |
| 41 | #include "src/core/SkTextFormatParams.h" |
| 42 | #include "src/core/SkTraceEvent.h" |
| 43 | #include "src/image/SkImage_Base.h" |
| 44 | #include "src/image/SkSurface_Base.h" |
| 45 | #include "src/utils/SkPatchUtils.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 46 | |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 47 | #include <new> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 49 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "include/gpu/GrContext.h" |
| 51 | #include "src/gpu/SkGr.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 54 | #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) |
Mike Reed | 74d6e11 | 2018-01-23 13:06:12 -0500 | [diff] [blame] | 55 | #define RETURN_ON_FALSE(pred) do { if (!(pred)) return; } while (0) |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 56 | |
Brian Salomon | b0047b5 | 2019-12-05 19:52:25 +0000 | [diff] [blame^] | 57 | // This is a test: static_assert with no message is a c++17 feature. |
| 58 | static_assert(true); |
| 59 | |
Mike Reed | 139e5e0 | 2017-03-08 11:29:33 -0500 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 61 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 62 | /* |
| 63 | * Return true if the drawing this rect would hit every pixels in the canvas. |
| 64 | * |
| 65 | * Returns false if |
| 66 | * - rect does not contain the canvas' bounds |
| 67 | * - paint is not fill |
| 68 | * - paint would blur or otherwise change the coverage of the rect |
| 69 | */ |
| 70 | bool SkCanvas::wouldOverwriteEntireSurface(const SkRect* rect, const SkPaint* paint, |
| 71 | ShaderOverrideOpacity overrideOpacity) const { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 72 | static_assert((int)SkPaintPriv::kNone_ShaderOverrideOpacity == |
| 73 | (int)kNone_ShaderOverrideOpacity, |
| 74 | "need_matching_enums0"); |
| 75 | static_assert((int)SkPaintPriv::kOpaque_ShaderOverrideOpacity == |
| 76 | (int)kOpaque_ShaderOverrideOpacity, |
| 77 | "need_matching_enums1"); |
| 78 | static_assert((int)SkPaintPriv::kNotOpaque_ShaderOverrideOpacity == |
| 79 | (int)kNotOpaque_ShaderOverrideOpacity, |
| 80 | "need_matching_enums2"); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 81 | |
| 82 | const SkISize size = this->getBaseLayerSize(); |
| 83 | const SkRect bounds = SkRect::MakeIWH(size.width(), size.height()); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 84 | |
| 85 | // if we're clipped at all, we can't overwrite the entire surface |
| 86 | { |
| 87 | SkBaseDevice* base = this->getDevice(); |
| 88 | SkBaseDevice* top = this->getTopDevice(); |
| 89 | if (base != top) { |
| 90 | return false; // we're in a saveLayer, so conservatively don't assume we'll overwrite |
| 91 | } |
| 92 | if (!base->clipIsWideOpen()) { |
| 93 | return false; |
| 94 | } |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (rect) { |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 98 | if (!this->getTotalMatrix().isScaleTranslate()) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 99 | return false; // conservative |
| 100 | } |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 101 | |
| 102 | SkRect devRect; |
| 103 | this->getTotalMatrix().mapRectScaleTranslate(&devRect, *rect); |
| 104 | if (!devRect.contains(bounds)) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (paint) { |
| 110 | SkPaint::Style paintStyle = paint->getStyle(); |
| 111 | if (!(paintStyle == SkPaint::kFill_Style || |
| 112 | paintStyle == SkPaint::kStrokeAndFill_Style)) { |
| 113 | return false; |
| 114 | } |
Mike Reed | 9dc0b9e | 2019-07-29 17:52:48 -0400 | [diff] [blame] | 115 | if (paint->getMaskFilter() || paint->getPathEffect() || paint->getImageFilter()) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 116 | return false; // conservative |
| 117 | } |
| 118 | } |
| 119 | return SkPaintPriv::Overwrites(paint, (SkPaintPriv::ShaderOverrideOpacity)overrideOpacity); |
| 120 | } |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 123 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 124 | // experimental for faster tiled drawing... |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | //#define SK_TRACE_SAVERESTORE |
| 126 | |
| 127 | #ifdef SK_TRACE_SAVERESTORE |
| 128 | static int gLayerCounter; |
| 129 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 130 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 131 | |
| 132 | static int gRecCounter; |
| 133 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 134 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 135 | |
| 136 | static int gCanvasCounter; |
| 137 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 138 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 139 | #else |
| 140 | #define inc_layer() |
| 141 | #define dec_layer() |
| 142 | #define inc_rec() |
| 143 | #define dec_rec() |
| 144 | #define inc_canvas() |
| 145 | #define dec_canvas() |
| 146 | #endif |
| 147 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 148 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 149 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 150 | void SkCanvas::predrawNotify(bool willOverwritesEntireSurface) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 151 | if (fSurfaceBase) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 152 | fSurfaceBase->aboutToDraw(willOverwritesEntireSurface |
| 153 | ? SkSurface::kDiscard_ContentChangeMode |
| 154 | : SkSurface::kRetain_ContentChangeMode); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void SkCanvas::predrawNotify(const SkRect* rect, const SkPaint* paint, |
| 159 | ShaderOverrideOpacity overrideOpacity) { |
| 160 | if (fSurfaceBase) { |
| 161 | SkSurface::ContentChangeMode mode = SkSurface::kRetain_ContentChangeMode; |
| 162 | // Since willOverwriteAllPixels() may not be complete free to call, we only do so if |
| 163 | // there is an outstanding snapshot, since w/o that, there will be no copy-on-write |
| 164 | // and therefore we don't care which mode we're in. |
| 165 | // |
| 166 | if (fSurfaceBase->outstandingImageSnapshot()) { |
| 167 | if (this->wouldOverwriteEntireSurface(rect, paint, overrideOpacity)) { |
| 168 | mode = SkSurface::kDiscard_ContentChangeMode; |
| 169 | } |
| 170 | } |
| 171 | fSurfaceBase->aboutToDraw(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 177 | /* 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] | 178 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 179 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 180 | these are used (assuming we're not on a layer) we rebuild these cache |
| 181 | values: they reflect the top of the save stack, but translated and clipped |
| 182 | by the device's XY offset and bitmap-bounds. |
| 183 | */ |
| 184 | struct DeviceCM { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 185 | DeviceCM* fNext; |
| 186 | sk_sp<SkBaseDevice> fDevice; |
| 187 | SkRasterClip fClip; |
| 188 | std::unique_ptr<const SkPaint> fPaint; // may be null (in the future) |
| 189 | SkMatrix fStashedMatrix; // original CTM; used by imagefilter in saveLayer |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 190 | sk_sp<SkImage> fClipImage; |
| 191 | SkMatrix fClipMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 193 | DeviceCM(sk_sp<SkBaseDevice> device, const SkPaint* paint, const SkMatrix& stashed, |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 194 | const SkImage* clipImage, const SkMatrix* clipMatrix) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | : fNext(nullptr) |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 196 | , fDevice(std::move(device)) |
| 197 | , fPaint(paint ? skstd::make_unique<SkPaint>(*paint) : nullptr) |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 198 | , fStashedMatrix(stashed) |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 199 | , fClipImage(sk_ref_sp(const_cast<SkImage*>(clipImage))) |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 200 | , fClipMatrix(clipMatrix ? *clipMatrix : SkMatrix::I()) |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 201 | {} |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 202 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 203 | void reset(const SkIRect& bounds) { |
| 204 | SkASSERT(!fPaint); |
| 205 | SkASSERT(!fNext); |
| 206 | SkASSERT(fDevice); |
| 207 | fClip.setRect(bounds); |
| 208 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 211 | namespace { |
| 212 | // Encapsulate state needed to restore from saveBehind() |
| 213 | struct BackImage { |
| 214 | sk_sp<SkSpecialImage> fImage; |
| 215 | SkIPoint fLoc; |
| 216 | }; |
| 217 | } |
| 218 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | /* This is the record we keep for each save/restore level in the stack. |
| 220 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 221 | for these fields. If the value is copied for this level, the copy is |
| 222 | stored in the ...Storage field, and the pointer points to that. If the |
| 223 | value is not copied for this level, we ignore ...Storage, and just point |
| 224 | at the corresponding value in the previous level in the stack. |
| 225 | */ |
| 226 | class SkCanvas::MCRec { |
| 227 | public: |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 228 | DeviceCM* fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | /* If there are any layers in the stack, this points to the top-most |
| 230 | one that is at or below this level in the stack (so we know what |
| 231 | bitmap/device to draw into from this level. This value is NOT |
| 232 | reference counted, since the real owner is either our fLayer field, |
| 233 | or a previous one in a lower level.) |
| 234 | */ |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 235 | DeviceCM* fTopLayer; |
| 236 | std::unique_ptr<BackImage> fBackImage; |
| 237 | SkConservativeClip fRasterClip; |
| 238 | SkMatrix fMatrix; |
| 239 | int fDeferredSaveCount; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 241 | MCRec() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 242 | fLayer = nullptr; |
| 243 | fTopLayer = nullptr; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 244 | fMatrix.reset(); |
| 245 | fDeferredSaveCount = 0; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 246 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 247 | // don't bother initializing fNext |
| 248 | inc_rec(); |
| 249 | } |
Jim Van Verth | 343fe49 | 2017-05-02 16:49:24 -0400 | [diff] [blame] | 250 | MCRec(const MCRec& prev) : fRasterClip(prev.fRasterClip), fMatrix(prev.fMatrix) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 251 | fLayer = nullptr; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 252 | fTopLayer = prev.fTopLayer; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 253 | fDeferredSaveCount = 0; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 254 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | // don't bother initializing fNext |
| 256 | inc_rec(); |
| 257 | } |
| 258 | ~MCRec() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 259 | delete fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | dec_rec(); |
| 261 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 262 | |
| 263 | void reset(const SkIRect& bounds) { |
| 264 | SkASSERT(fLayer); |
| 265 | SkASSERT(fDeferredSaveCount == 0); |
| 266 | |
| 267 | fMatrix.reset(); |
| 268 | fRasterClip.setRect(bounds); |
| 269 | fLayer->reset(bounds); |
| 270 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 273 | class SkDrawIter { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | public: |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 275 | SkDrawIter(SkCanvas* canvas) |
| 276 | : fDevice(nullptr), fCurrLayer(canvas->fMCRec->fTopLayer), fPaint(nullptr) |
| 277 | {} |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 278 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | bool next() { |
reed@google.com | f68c5e2 | 2012-02-24 16:38:58 +0000 | [diff] [blame] | 280 | const DeviceCM* rec = fCurrLayer; |
| 281 | if (rec && rec->fDevice) { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 282 | fDevice = rec->fDevice.get(); |
| 283 | fPaint = rec->fPaint.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | fCurrLayer = rec->fNext; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 285 | // fCurrLayer may be nullptr now |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | return false; |
| 289 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 290 | |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 291 | int getX() const { return fDevice->getOrigin().x(); } |
| 292 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 293 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 294 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 295 | SkBaseDevice* fDevice; |
| 296 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | const DeviceCM* fCurrLayer; |
| 299 | const SkPaint* fPaint; // May be null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 302 | #define FOR_EACH_TOP_DEVICE( code ) \ |
| 303 | do { \ |
| 304 | DeviceCM* layer = fMCRec->fTopLayer; \ |
| 305 | while (layer) { \ |
| 306 | SkBaseDevice* device = layer->fDevice.get(); \ |
| 307 | if (device) { \ |
| 308 | code; \ |
| 309 | } \ |
| 310 | layer = layer->fNext; \ |
| 311 | } \ |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 312 | } while (0) |
| 313 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | ///////////////////////////////////////////////////////////////////////////// |
| 315 | |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 316 | /** |
| 317 | * 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] | 318 | * colorfilter, else return nullptr. |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 319 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 320 | static sk_sp<SkColorFilter> image_to_color_filter(const SkPaint& paint) { |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 321 | SkImageFilter* imgf = paint.getImageFilter(); |
| 322 | if (!imgf) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 323 | return nullptr; |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 324 | } |
| 325 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 326 | SkColorFilter* imgCFPtr; |
| 327 | if (!imgf->asAColorFilter(&imgCFPtr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 328 | return nullptr; |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 329 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 330 | sk_sp<SkColorFilter> imgCF(imgCFPtr); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 331 | |
| 332 | SkColorFilter* paintCF = paint.getColorFilter(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 333 | if (nullptr == paintCF) { |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 334 | // there is no existing paint colorfilter, so we can just return the imagefilter's |
| 335 | return imgCF; |
| 336 | } |
| 337 | |
| 338 | // The paint has both a colorfilter(paintCF) and an imagefilter-which-is-a-colorfilter(imgCF) |
| 339 | // and we need to combine them into a single colorfilter. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 340 | return imgCF->makeComposed(sk_ref_sp(paintCF)); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 341 | } |
| 342 | |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 343 | /** |
| 344 | * There are many bounds in skia. A circle's bounds is just its center extended by its radius. |
| 345 | * However, if we stroke a circle, then the "bounds" of that is larger, since it will now draw |
| 346 | * outside of its raw-bounds by 1/2 the stroke width. SkPaint has lots of optional |
| 347 | * effects/attributes that can modify the effective bounds of a given primitive -- maskfilters, |
| 348 | * patheffects, stroking, etc. This function takes a raw bounds and a paint, and returns the |
| 349 | * conservative "effective" bounds based on the settings in the paint... with one exception. This |
| 350 | * function does *not* look at the imagefilter, which can also modify the effective bounds. It is |
| 351 | * deliberately ignored. |
| 352 | */ |
| 353 | static const SkRect& apply_paint_to_bounds_sans_imagefilter(const SkPaint& paint, |
| 354 | const SkRect& rawBounds, |
| 355 | SkRect* storage) { |
| 356 | SkPaint tmpUnfiltered(paint); |
| 357 | tmpUnfiltered.setImageFilter(nullptr); |
| 358 | if (tmpUnfiltered.canComputeFastBounds()) { |
| 359 | return tmpUnfiltered.computeFastBounds(rawBounds, storage); |
| 360 | } else { |
| 361 | return rawBounds; |
| 362 | } |
| 363 | } |
| 364 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 365 | class AutoLayerForImageFilter { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | public: |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 367 | // "rawBounds" is the original bounds of the primitive about to be drawn, unmodified by the |
| 368 | // paint. It's used to determine the size of the offscreen layer for filters. |
| 369 | // If null, the clip will be used instead. |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 370 | AutoLayerForImageFilter(SkCanvas* canvas, const SkPaint& origPaint, |
| 371 | bool skipLayerForImageFilter = false, |
| 372 | const SkRect* rawBounds = nullptr) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 373 | fCanvas = canvas; |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 374 | fPaint = &origPaint; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 375 | fSaveCount = canvas->getSaveCount(); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 376 | fTempLayerForImageFilter = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 378 | if (auto simplifiedCF = image_to_color_filter(origPaint)) { |
| 379 | SkASSERT(!fLazyPaint.isValid()); |
| 380 | SkPaint* paint = fLazyPaint.set(origPaint); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 381 | paint->setColorFilter(std::move(simplifiedCF)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 382 | paint->setImageFilter(nullptr); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 383 | fPaint = paint; |
| 384 | } |
| 385 | |
| 386 | if (!skipLayerForImageFilter && fPaint->getImageFilter()) { |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 387 | /** |
| 388 | * We implement ImageFilters for a given draw by creating a layer, then applying the |
| 389 | * imagefilter to the pixels of that layer (its backing surface/image), and then |
| 390 | * we call restore() to xfer that layer to the main canvas. |
| 391 | * |
| 392 | * 1. SaveLayer (with a paint containing the current imagefilter and xfermode) |
| 393 | * 2. Generate the src pixels: |
| 394 | * Remove the imagefilter and the xfermode from the paint that we (AutoDrawLooper) |
| 395 | * return (fPaint). We then draw the primitive (using srcover) into a cleared |
| 396 | * buffer/surface. |
| 397 | * 3. Restore the layer created in #1 |
| 398 | * The imagefilter is passed the buffer/surface from the layer (now filled with the |
| 399 | * src pixels of the primitive). It returns a new "filtered" buffer, which we |
| 400 | * draw onto the previous layer using the xfermode from the original paint. |
| 401 | */ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 402 | |
| 403 | SkPaint restorePaint; |
| 404 | restorePaint.setImageFilter(fPaint->refImageFilter()); |
| 405 | restorePaint.setBlendMode(fPaint->getBlendMode()); |
| 406 | |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 407 | SkRect storage; |
| 408 | if (rawBounds) { |
| 409 | // Make rawBounds include all paint outsets except for those due to image filters. |
| 410 | rawBounds = &apply_paint_to_bounds_sans_imagefilter(*fPaint, *rawBounds, &storage); |
| 411 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 412 | (void)canvas->internalSaveLayer(SkCanvas::SaveLayerRec(rawBounds, &restorePaint), |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 413 | SkCanvas::kFullLayer_SaveLayerStrategy); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 414 | fTempLayerForImageFilter = true; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 415 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 416 | // Remove the restorePaint fields from our "working" paint |
| 417 | SkASSERT(!fLazyPaint.isValid()); |
| 418 | SkPaint* paint = fLazyPaint.set(origPaint); |
| 419 | paint->setImageFilter(nullptr); |
| 420 | paint->setBlendMode(SkBlendMode::kSrcOver); |
| 421 | fPaint = paint; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 424 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 425 | ~AutoLayerForImageFilter() { |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 426 | if (fTempLayerForImageFilter) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 427 | fCanvas->internalRestore(); |
| 428 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 429 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 431 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 432 | const SkPaint& paint() const { |
| 433 | SkASSERT(fPaint); |
| 434 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 435 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 436 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | private: |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 438 | SkLazyPaint fLazyPaint; // base paint storage in case we need to modify it |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 439 | SkCanvas* fCanvas; |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 440 | const SkPaint* fPaint; // points to either the original paint, or lazy (if we needed it) |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 441 | int fSaveCount; |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 442 | bool fTempLayerForImageFilter; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 443 | }; |
| 444 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 445 | ////////// macros to place around the internal draw calls ////////////////// |
| 446 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 447 | #define DRAW_BEGIN_DRAWBITMAP(paint, skipLayerForFilter, bounds) \ |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 448 | this->predrawNotify(); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 449 | AutoLayerForImageFilter draw(this, paint, skipLayerForFilter, bounds); \ |
| 450 | { SkDrawIter iter(this); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 451 | |
| 452 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 453 | #define DRAW_BEGIN_DRAWDEVICE(paint) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 454 | this->predrawNotify(); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 455 | AutoLayerForImageFilter draw(this, paint, true); \ |
| 456 | { SkDrawIter iter(this); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 457 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 458 | #define DRAW_BEGIN(paint, bounds) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 459 | this->predrawNotify(); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 460 | AutoLayerForImageFilter draw(this, paint, false, bounds); \ |
| 461 | { SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 462 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 463 | #define DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, bounds, auxOpaque) \ |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 464 | this->predrawNotify(bounds, &paint, auxOpaque); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 465 | AutoLayerForImageFilter draw(this, paint, false, bounds); \ |
| 466 | { SkDrawIter iter(this); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 467 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 468 | #define DRAW_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 469 | |
| 470 | //////////////////////////////////////////////////////////////////////////// |
| 471 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 472 | static inline SkRect qr_clip_bounds(const SkIRect& bounds) { |
| 473 | if (bounds.isEmpty()) { |
| 474 | return SkRect::MakeEmpty(); |
| 475 | } |
| 476 | |
| 477 | // Expand bounds out by 1 in case we are anti-aliasing. We store the |
| 478 | // bounds as floats to enable a faster quick reject implementation. |
| 479 | SkRect dst; |
| 480 | SkNx_cast<float>(Sk4i::Load(&bounds.fLeft) + Sk4i(-1,-1,1,1)).store(&dst.fLeft); |
| 481 | return dst; |
| 482 | } |
| 483 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 484 | void SkCanvas::resetForNextPicture(const SkIRect& bounds) { |
| 485 | this->restoreToCount(1); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 486 | fMCRec->reset(bounds); |
| 487 | |
| 488 | // We're peering through a lot of structs here. Only at this scope do we |
Mike Reed | 139e5e0 | 2017-03-08 11:29:33 -0500 | [diff] [blame] | 489 | // know that the device is a SkNoPixelsDevice. |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 490 | static_cast<SkNoPixelsDevice*>(fMCRec->fLayer->fDevice.get())->resetForNextPicture(bounds); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 491 | fDeviceClipBounds = qr_clip_bounds(bounds); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 492 | fIsScaleTranslate = true; |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 495 | void SkCanvas::init(sk_sp<SkBaseDevice> device) { |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 496 | fAllowSimplifyClip = false; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 497 | fSaveCount = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 498 | |
| 499 | fMCRec = (MCRec*)fMCStack.push_back(); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 500 | new (fMCRec) MCRec; |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 501 | fMCRec->fRasterClip.setDeviceClipRestriction(&fClipRestrictionRect); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 502 | fIsScaleTranslate = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 503 | |
reed | a499f90 | 2015-05-01 09:34:31 -0700 | [diff] [blame] | 504 | SkASSERT(sizeof(DeviceCM) <= sizeof(fDeviceCMStorage)); |
| 505 | fMCRec->fLayer = (DeviceCM*)fDeviceCMStorage; |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 506 | new (fDeviceCMStorage) DeviceCM(device, nullptr, fMCRec->fMatrix, nullptr, nullptr); |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 507 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 508 | fMCRec->fTopLayer = fMCRec->fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 509 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 510 | fSurfaceBase = nullptr; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 511 | |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 512 | if (device) { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 513 | // The root device and the canvas should always have the same pixel geometry |
| 514 | SkASSERT(fProps.pixelGeometry() == device->surfaceProps().pixelGeometry()); |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 515 | fMCRec->fRasterClip.setRect(device->getGlobalBounds()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 516 | fDeviceClipBounds = qr_clip_bounds(device->getGlobalBounds()); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 517 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 518 | device->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect); |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 519 | } |
Herb Derby | 4ffa027 | 2018-06-04 15:49:15 -0400 | [diff] [blame] | 520 | |
Herb Derby | 59d997a | 2018-06-07 12:44:09 -0400 | [diff] [blame] | 521 | fScratchGlyphRunBuilder = skstd::make_unique<SkGlyphRunBuilder>(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 522 | } |
| 523 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 524 | SkCanvas::SkCanvas() |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 525 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 526 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 527 | { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 528 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 529 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 530 | this->init(nullptr); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 531 | } |
| 532 | |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 533 | SkCanvas::SkCanvas(int width, int height, const SkSurfaceProps* props) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 534 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 535 | , fProps(SkSurfacePropsCopyOrDefault(props)) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 536 | { |
| 537 | inc_canvas(); |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 538 | this->init(sk_make_sp<SkNoPixelsDevice>( |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 539 | SkIRect::MakeWH(SkTMax(width, 0), SkTMax(height, 0)), fProps)); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 540 | } |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 541 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 542 | SkCanvas::SkCanvas(const SkIRect& bounds) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 543 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 544 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 545 | { |
| 546 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 547 | |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 548 | SkIRect r = bounds.isEmpty() ? SkIRect::MakeEmpty() : bounds; |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 549 | this->init(sk_make_sp<SkNoPixelsDevice>(r, fProps)); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 552 | SkCanvas::SkCanvas(sk_sp<SkBaseDevice> device) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 553 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 554 | , fProps(device->surfaceProps()) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 555 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 556 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 557 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 558 | this->init(device); |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 559 | } |
| 560 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 561 | SkCanvas::SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 562 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 563 | , fProps(props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 564 | { |
| 565 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 566 | |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 567 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps, nullptr, nullptr)); |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 568 | this->init(device); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 569 | } |
reed | 29c857d | 2014-09-21 10:25:07 -0700 | [diff] [blame] | 570 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 571 | SkCanvas::SkCanvas(const SkBitmap& bitmap, std::unique_ptr<SkRasterHandleAllocator> alloc, |
| 572 | SkRasterHandleAllocator::Handle hndl) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 573 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 574 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 575 | , fAllocator(std::move(alloc)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 576 | { |
| 577 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 578 | |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 579 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps, hndl, nullptr)); |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 580 | this->init(device); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 583 | SkCanvas::SkCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap, nullptr, nullptr) {} |
| 584 | |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 585 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 586 | SkCanvas::SkCanvas(const SkBitmap& bitmap, ColorBehavior) |
| 587 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 588 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
| 589 | , fAllocator(nullptr) |
| 590 | { |
| 591 | inc_canvas(); |
| 592 | |
| 593 | SkBitmap tmp(bitmap); |
| 594 | *const_cast<SkImageInfo*>(&tmp.info()) = tmp.info().makeColorSpace(nullptr); |
Mike Reed | c247a4e | 2018-04-25 15:40:09 -0400 | [diff] [blame] | 595 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(tmp, fProps, nullptr, nullptr)); |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 596 | this->init(device); |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 597 | } |
| 598 | #endif |
| 599 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 600 | SkCanvas::~SkCanvas() { |
| 601 | // free up the contents of our deque |
| 602 | this->restoreToCount(1); // restore everything but the last |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 603 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 604 | this->internalRestore(); // restore the last, since we're going away |
| 605 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | dec_canvas(); |
| 607 | } |
| 608 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 609 | /////////////////////////////////////////////////////////////////////////////// |
| 610 | |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 611 | void SkCanvas::flush() { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 612 | this->onFlush(); |
| 613 | } |
| 614 | |
| 615 | void SkCanvas::onFlush() { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 616 | SkBaseDevice* device = this->getDevice(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 617 | if (device) { |
| 618 | device->flush(); |
| 619 | } |
| 620 | } |
| 621 | |
Mike Reed | a2b3b9e | 2019-11-15 15:00:27 -0500 | [diff] [blame] | 622 | SkSurface* SkCanvas::getSurface() const { |
| 623 | return fSurfaceBase; |
| 624 | } |
| 625 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 626 | SkISize SkCanvas::getBaseLayerSize() const { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 627 | SkBaseDevice* d = this->getDevice(); |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 628 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 629 | } |
| 630 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 631 | SkIRect SkCanvas::getTopLayerBounds() const { |
| 632 | SkBaseDevice* d = this->getTopDevice(); |
| 633 | if (!d) { |
| 634 | return SkIRect::MakeEmpty(); |
| 635 | } |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 636 | return SkIRect::MakeXYWH(d->getOrigin().x(), d->getOrigin().y(), d->width(), d->height()); |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 637 | } |
| 638 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 639 | SkBaseDevice* SkCanvas::getDevice() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 640 | // return root device |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 641 | MCRec* rec = (MCRec*) fMCStack.front(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 642 | SkASSERT(rec && rec->fLayer); |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 643 | return rec->fLayer->fDevice.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Florin Malita | 0ed3b64 | 2017-01-13 16:56:38 +0000 | [diff] [blame] | 646 | SkBaseDevice* SkCanvas::getTopDevice() const { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 647 | return fMCRec->fTopLayer->fDevice.get(); |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 650 | bool SkCanvas::readPixels(const SkPixmap& pm, int x, int y) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 651 | SkBaseDevice* device = this->getDevice(); |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 652 | return device && pm.addr() && device->readPixels(pm, x, y); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 655 | bool SkCanvas::readPixels(const SkImageInfo& dstInfo, void* dstP, size_t rowBytes, int x, int y) { |
| 656 | return this->readPixels({ dstInfo, dstP, rowBytes}, x, y); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | bool SkCanvas::readPixels(const SkBitmap& bm, int x, int y) { |
| 660 | SkPixmap pm; |
| 661 | return bm.peekPixels(&pm) && this->readPixels(pm, x, y); |
| 662 | } |
| 663 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 664 | bool SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) { |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 665 | SkPixmap pm; |
| 666 | if (bitmap.peekPixels(&pm)) { |
reed | cf01e31 | 2015-05-23 19:14:51 -0700 | [diff] [blame] | 667 | 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] | 668 | } |
| 669 | return false; |
| 670 | } |
| 671 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 672 | bool SkCanvas::writePixels(const SkImageInfo& srcInfo, const void* pixels, size_t rowBytes, |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 673 | int x, int y) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 674 | SkBaseDevice* device = this->getDevice(); |
| 675 | if (!device) { |
| 676 | return false; |
| 677 | } |
| 678 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 679 | // This check gives us an early out and prevents generation ID churn on the surface. |
| 680 | // This is purely optional: it is a subset of the checks performed by SkWritePixelsRec. |
| 681 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, srcInfo.width(), srcInfo.height()); |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 682 | if (!srcRect.intersect({0, 0, device->width(), device->height()})) { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 683 | return false; |
Matt Sarett | 26ecfe0 | 2017-01-23 15:51:01 +0000 | [diff] [blame] | 684 | } |
Matt Sarett | 26ecfe0 | 2017-01-23 15:51:01 +0000 | [diff] [blame] | 685 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 686 | // Tell our owning surface to bump its generation ID. |
| 687 | const bool completeOverwrite = |
| 688 | srcRect.size() == SkISize::Make(device->width(), device->height()); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 689 | this->predrawNotify(completeOverwrite); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 690 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 691 | // This can still fail, most notably in the case of a invalid color type or alpha type |
| 692 | // conversion. We could pull those checks into this function and avoid the unnecessary |
| 693 | // generation ID bump. But then we would be performing those checks twice, since they |
| 694 | // are also necessary at the bitmap/pixmap entry points. |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 695 | return device->writePixels({srcInfo, pixels, rowBytes}, x, y); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 696 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 697 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 698 | ////////////////////////////////////////////////////////////////////////////// |
| 699 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 700 | void SkCanvas::checkForDeferredSave() { |
| 701 | if (fMCRec->fDeferredSaveCount > 0) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 702 | this->doSave(); |
| 703 | } |
| 704 | } |
| 705 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 706 | int SkCanvas::getSaveCount() const { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 707 | #ifdef SK_DEBUG |
| 708 | int count = 0; |
| 709 | SkDeque::Iter iter(fMCStack, SkDeque::Iter::kFront_IterStart); |
| 710 | for (;;) { |
| 711 | const MCRec* rec = (const MCRec*)iter.next(); |
| 712 | if (!rec) { |
| 713 | break; |
| 714 | } |
| 715 | count += 1 + rec->fDeferredSaveCount; |
| 716 | } |
| 717 | SkASSERT(count == fSaveCount); |
| 718 | #endif |
| 719 | return fSaveCount; |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | int SkCanvas::save() { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 723 | fSaveCount += 1; |
| 724 | fMCRec->fDeferredSaveCount += 1; |
| 725 | return this->getSaveCount() - 1; // return our prev value |
| 726 | } |
| 727 | |
| 728 | void SkCanvas::doSave() { |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 729 | this->willSave(); |
fmalita | a62d32d | 2015-04-28 08:08:57 -0700 | [diff] [blame] | 730 | |
| 731 | SkASSERT(fMCRec->fDeferredSaveCount > 0); |
| 732 | fMCRec->fDeferredSaveCount -= 1; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 733 | this->internalSave(); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | void SkCanvas::restore() { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 737 | if (fMCRec->fDeferredSaveCount > 0) { |
| 738 | SkASSERT(fSaveCount > 1); |
| 739 | fSaveCount -= 1; |
| 740 | fMCRec->fDeferredSaveCount -= 1; |
| 741 | } else { |
| 742 | // check for underflow |
| 743 | if (fMCStack.count() > 1) { |
| 744 | this->willRestore(); |
| 745 | SkASSERT(fSaveCount > 1); |
reed | a644116 | 2015-03-26 13:40:09 -0700 | [diff] [blame] | 746 | fSaveCount -= 1; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 747 | this->internalRestore(); |
| 748 | this->didRestore(); |
| 749 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | void SkCanvas::restoreToCount(int count) { |
| 754 | // sanity check |
| 755 | if (count < 1) { |
| 756 | count = 1; |
| 757 | } |
mtklein | f0f1411 | 2014-12-12 08:46:25 -0800 | [diff] [blame] | 758 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 759 | int n = this->getSaveCount() - count; |
| 760 | for (int i = 0; i < n; ++i) { |
| 761 | this->restore(); |
| 762 | } |
| 763 | } |
| 764 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 765 | void SkCanvas::internalSave() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 766 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 767 | new (newTop) MCRec(*fMCRec); // balanced in restore() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 768 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 769 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 770 | FOR_EACH_TOP_DEVICE(device->save()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 771 | } |
| 772 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 773 | bool SkCanvas::BoundsAffectsClip(SaveLayerFlags saveLayerFlags) { |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 774 | return !(saveLayerFlags & SkCanvasPriv::kDontClipToLayer_SaveLayerFlag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 775 | } |
| 776 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 777 | bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveLayerFlags saveLayerFlags, |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 778 | SkIRect* intersection, const SkImageFilter* imageFilter) { |
Michael Ludwig | aa861a1 | 2019-07-19 10:13:47 -0400 | [diff] [blame] | 779 | // clipRectBounds() is called to determine the input layer size needed for a given image filter. |
| 780 | // The coordinate space of the rectangle passed to filterBounds(kReverse) is meant to be in the |
| 781 | // filtering layer space. Here, 'clipBounds' is always in the true device space. When an image |
| 782 | // filter does not require a decomposed CTM matrix, the filter space and device space are the |
| 783 | // same. When it has been decomposed, we want the original image filter node to process the |
| 784 | // bounds in the layer space represented by the decomposed scale matrix. 'imageFilter' is no |
| 785 | // longer the original filter, but has the remainder matrix baked into it, and passing in the |
| 786 | // the true device clip bounds ensures that the matrix image filter provides a layer clip bounds |
| 787 | // to the original filter node (barring inflation from consecutive calls to mapRect). While |
| 788 | // initially counter-intuitive given the apparent inconsistency of coordinate spaces, always |
| 789 | // passing getDeviceClipBounds() to 'imageFilter' is correct. |
| 790 | // FIXME (michaelludwig) - When the remainder matrix is instead applied as a final draw, it will |
| 791 | // be important to more accurately calculate the clip bounds in the layer space for the original |
| 792 | // image filter (similar to how matrix image filter does it, but ideally without the inflation). |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 793 | SkIRect clipBounds = this->getDeviceClipBounds(); |
| 794 | if (clipBounds.isEmpty()) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 795 | return false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 796 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 797 | |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 798 | const SkMatrix& ctm = fMCRec->fMatrix; // this->getTotalMatrix() |
| 799 | |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 800 | if (imageFilter && bounds && !imageFilter->canComputeFastBounds()) { |
| 801 | // If the image filter DAG affects transparent black then we will need to render |
| 802 | // out to the clip bounds |
| 803 | bounds = nullptr; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 804 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 805 | |
| 806 | SkIRect inputSaveLayerBounds; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 807 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 808 | SkRect r; |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 809 | ctm.mapRect(&r, *bounds); |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 810 | r.roundOut(&inputSaveLayerBounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 811 | } else { // no user bounds, so just use the clip |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 812 | inputSaveLayerBounds = clipBounds; |
| 813 | } |
| 814 | |
| 815 | if (imageFilter) { |
| 816 | // expand the clip bounds by the image filter DAG to include extra content that might |
| 817 | // be required by the image filters. |
| 818 | clipBounds = imageFilter->filterBounds(clipBounds, ctm, |
| 819 | SkImageFilter::kReverse_MapDirection, |
| 820 | &inputSaveLayerBounds); |
| 821 | } |
| 822 | |
| 823 | SkIRect clippedSaveLayerBounds; |
| 824 | if (bounds) { |
| 825 | // For better or for worse, user bounds currently act as a hard clip on the layer's |
| 826 | // extent (i.e., they implement the CSS filter-effects 'filter region' feature). |
| 827 | clippedSaveLayerBounds = inputSaveLayerBounds; |
| 828 | } else { |
| 829 | // If there are no user bounds, we don't want to artificially restrict the resulting |
| 830 | // layer bounds, so allow the expanded clip bounds free reign. |
| 831 | clippedSaveLayerBounds = clipBounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 832 | } |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 833 | |
| 834 | // early exit if the layer's bounds are clipped out |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 835 | if (!clippedSaveLayerBounds.intersect(clipBounds)) { |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 836 | if (BoundsAffectsClip(saveLayerFlags)) { |
| 837 | fMCRec->fTopLayer->fDevice->clipRegion(SkRegion(), SkClipOp::kIntersect); // empty |
| 838 | fMCRec->fRasterClip.setEmpty(); |
| 839 | fDeviceClipBounds.setEmpty(); |
| 840 | } |
| 841 | return false; |
| 842 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 843 | SkASSERT(!clippedSaveLayerBounds.isEmpty()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 844 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 845 | if (BoundsAffectsClip(saveLayerFlags)) { |
reed | 180aec4 | 2015-03-11 10:39:04 -0700 | [diff] [blame] | 846 | // Simplify the current clips since they will be applied properly during restore() |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 847 | fMCRec->fRasterClip.setRect(clippedSaveLayerBounds); |
| 848 | fDeviceClipBounds = qr_clip_bounds(clippedSaveLayerBounds); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | if (intersection) { |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 852 | *intersection = clippedSaveLayerBounds; |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 853 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 854 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 855 | return true; |
| 856 | } |
| 857 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 858 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint) { |
| 859 | return this->saveLayer(SaveLayerRec(bounds, paint, 0)); |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Cary Clark | e041e31 | 2018-03-06 13:00:52 -0500 | [diff] [blame] | 862 | int SkCanvas::saveLayer(const SaveLayerRec& rec) { |
Yuqian Li | f7c723c | 2018-08-29 16:25:47 -0700 | [diff] [blame] | 863 | TRACE_EVENT0("skia", TRACE_FUNC); |
Mike Reed | 49f8da0 | 2018-08-27 10:48:52 -0400 | [diff] [blame] | 864 | if (rec.fPaint && rec.fPaint->nothingToDraw()) { |
| 865 | // no need for the layer (or any of the draws until the matching restore() |
| 866 | this->save(); |
| 867 | this->clipRect({0,0,0,0}); |
| 868 | } else { |
| 869 | SaveLayerStrategy strategy = this->getSaveLayerStrategy(rec); |
| 870 | fSaveCount += 1; |
| 871 | this->internalSaveLayer(rec, strategy); |
| 872 | } |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 873 | return this->getSaveCount() - 1; |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 876 | int SkCanvas::only_axis_aligned_saveBehind(const SkRect* bounds) { |
| 877 | if (bounds && !this->getLocalClipBounds().intersects(*bounds)) { |
| 878 | // Assuming clips never expand, if the request bounds is outside of the current clip |
| 879 | // there is no need to copy/restore the area, so just devolve back to a regular save. |
| 880 | this->save(); |
| 881 | } else { |
| 882 | bool doTheWork = this->onDoSaveBehind(bounds); |
| 883 | fSaveCount += 1; |
| 884 | this->internalSave(); |
| 885 | if (doTheWork) { |
| 886 | this->internalSaveBehind(bounds); |
| 887 | } |
| 888 | } |
| 889 | return this->getSaveCount() - 1; |
| 890 | } |
| 891 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 892 | void SkCanvas::DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter, |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 893 | SkBaseDevice* dst, const SkIPoint& dstOrigin, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 894 | const SkMatrix& ctm) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 895 | // The local bounds of the src device; all the bounds passed to snapSpecial must be intersected |
| 896 | // with this rect. |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 897 | const SkIRect srcDevRect = SkIRect::MakeWH(src->width(), src->height()); |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 898 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 899 | if (!filter) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 900 | // All non-filtered devices are currently axis aligned, so they only differ by their origin. |
| 901 | // This means that we only have to copy a dst-sized block of pixels out of src and translate |
| 902 | // it to the matching position relative to dst's origin. |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 903 | SkIRect snapBounds = SkIRect::MakeXYWH(dstOrigin.x() - src->getOrigin().x(), |
| 904 | dstOrigin.y() - src->getOrigin().y(), |
| 905 | dst->width(), dst->height()); |
| 906 | if (!snapBounds.intersect(srcDevRect)) { |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 907 | return; |
| 908 | } |
| 909 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 910 | auto special = src->snapSpecial(snapBounds); |
| 911 | if (special) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 912 | // The image is drawn at 1-1 scale with integer translation, so no filtering is needed. |
| 913 | SkPaint p; |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 914 | dst->drawSpecial(special.get(), 0, 0, p, nullptr, SkMatrix::I()); |
| 915 | } |
| 916 | return; |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 917 | } |
| 918 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 919 | // First decompose the ctm into a post-filter transform and a filter matrix that is supported |
| 920 | // by the backdrop filter. |
| 921 | SkMatrix toRoot, layerMatrix; |
| 922 | SkSize scale; |
| 923 | if (ctm.isScaleTranslate() || as_IFB(filter)->canHandleComplexCTM()) { |
| 924 | toRoot = SkMatrix::I(); |
| 925 | layerMatrix = ctm; |
| 926 | } else if (ctm.decomposeScale(&scale, &toRoot)) { |
| 927 | layerMatrix = SkMatrix::MakeScale(scale.fWidth, scale.fHeight); |
| 928 | } else { |
| 929 | // Perspective, for now, do no scaling of the layer itself. |
| 930 | // TODO (michaelludwig) - perhaps it'd be better to explore a heuristic scale pulled from |
| 931 | // the matrix, e.g. based on the midpoint of the near/far planes? |
| 932 | toRoot = ctm; |
| 933 | layerMatrix = SkMatrix::I(); |
| 934 | } |
| 935 | |
| 936 | // We have to map the dst bounds from the root space into the layer space where filtering will |
| 937 | // occur. If we knew the input bounds of the content that defined the original dst bounds, we |
| 938 | // could map that forward by layerMatrix and have tighter bounds, but toRoot^-1 * dst bounds |
| 939 | // is a safe, conservative estimate. |
| 940 | SkMatrix fromRoot; |
| 941 | if (!toRoot.invert(&fromRoot)) { |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | // This represents what the backdrop filter needs to produce in the layer space, and is sized |
| 946 | // such that drawing it into dst with the toRoot transform will cover the actual dst device. |
| 947 | SkIRect layerTargetBounds = fromRoot.mapRect( |
| 948 | SkRect::MakeXYWH(dstOrigin.x(), dstOrigin.y(), dst->width(), dst->height())).roundOut(); |
| 949 | // While layerTargetBounds is what needs to be output by the filter, the filtering process may |
| 950 | // require some extra input pixels. |
| 951 | SkIRect layerInputBounds = filter->filterBounds( |
| 952 | layerTargetBounds, layerMatrix, SkImageFilter::kReverse_MapDirection, |
| 953 | &layerTargetBounds); |
| 954 | |
| 955 | // Map the required input into the root space, then make relative to the src device. This will |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 956 | // be the conservative contents required to fill a layerInputBounds-sized surface with the |
| 957 | // backdrop content (transformed back into the layer space using fromRoot). |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 958 | SkIRect backdropBounds = toRoot.mapRect(SkRect::Make(layerInputBounds)).roundOut(); |
| 959 | backdropBounds.offset(-src->getOrigin().x(), -src->getOrigin().y()); |
| 960 | if (!backdropBounds.intersect(srcDevRect)) { |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | auto special = src->snapSpecial(backdropBounds); |
| 965 | if (!special) { |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | SkColorType colorType = src->imageInfo().colorType(); |
| 970 | if (colorType == kUnknown_SkColorType) { |
| 971 | colorType = kRGBA_8888_SkColorType; |
| 972 | } |
| 973 | SkColorSpace* colorSpace = src->imageInfo().colorSpace(); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 974 | |
| 975 | SkPaint p; |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 976 | if (!toRoot.isIdentity()) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 977 | // Drawing the temporary and final filtered image requires a higher filter quality if the |
| 978 | // 'toRoot' transformation is not identity, in order to minimize the impact on already |
| 979 | // rendered edges/content. |
| 980 | // TODO (michaelludwig) - Explore reducing this quality, identify visual tradeoffs |
| 981 | p.setFilterQuality(kHigh_SkFilterQuality); |
| 982 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 983 | // The snapped backdrop content needs to be transformed by fromRoot into the layer space, |
| 984 | // and stored in a temporary surface, which is then used as the input to the actual filter. |
| 985 | auto tmpSurface = special->makeSurface(colorType, colorSpace, layerInputBounds.size()); |
| 986 | if (!tmpSurface) { |
| 987 | return; |
| 988 | } |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 989 | |
| 990 | auto tmpCanvas = tmpSurface->getCanvas(); |
| 991 | tmpCanvas->clear(SK_ColorTRANSPARENT); |
| 992 | // Reading in reverse, this takes the backdrop bounds from src device space into the root |
| 993 | // space, then maps from root space into the layer space, then maps it so the input layer's |
| 994 | // top left corner is (0, 0). This transformation automatically accounts for any cropping |
| 995 | // performed on backdropBounds. |
| 996 | tmpCanvas->translate(-layerInputBounds.fLeft, -layerInputBounds.fTop); |
| 997 | tmpCanvas->concat(fromRoot); |
| 998 | tmpCanvas->translate(src->getOrigin().x(), src->getOrigin().y()); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 999 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1000 | tmpCanvas->drawImageRect(special->asImage(), special->subset(), |
| 1001 | SkRect::Make(backdropBounds), &p, kStrict_SrcRectConstraint); |
| 1002 | special = tmpSurface->makeImageSnapshot(); |
| 1003 | } else { |
| 1004 | // Since there is no extra transform that was done, update the input bounds to reflect |
| 1005 | // cropping of the snapped backdrop image. In this case toRoot = I, so layerInputBounds |
| 1006 | // was equal to backdropBounds before it was made relative to the src device and cropped. |
| 1007 | // When we use the original snapped image directly, just map the update backdrop bounds |
| 1008 | // back into the shared layer space |
| 1009 | layerInputBounds = backdropBounds; |
| 1010 | layerInputBounds.offset(src->getOrigin().x(), src->getOrigin().y()); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1011 | |
| 1012 | // Similar to the unfiltered case above, when toRoot is the identity, then the final |
| 1013 | // draw will be 1-1 so there is no need to increase filter quality. |
| 1014 | p.setFilterQuality(kNone_SkFilterQuality); |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | // Now evaluate the filter on 'special', which contains the backdrop content mapped back into |
| 1018 | // layer space. This has to further offset everything so that filter evaluation thinks the |
| 1019 | // source image's top left corner is (0, 0). |
| 1020 | // TODO (michaelludwig) - Once image filters are robust to non-(0,0) image origins for inputs, |
| 1021 | // this can be simplified. |
| 1022 | layerTargetBounds.offset(-layerInputBounds.fLeft, -layerInputBounds.fTop); |
| 1023 | SkMatrix filterCTM = layerMatrix; |
| 1024 | filterCTM.postTranslate(-layerInputBounds.fLeft, -layerInputBounds.fTop); |
| 1025 | skif::Context ctx(filterCTM, layerTargetBounds, nullptr, colorType, colorSpace, special.get()); |
| 1026 | |
| 1027 | SkIPoint offset; |
| 1028 | special = as_IFB(filter)->filterImage(ctx).imageAndOffset(&offset); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1029 | if (special) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1030 | // Draw the filtered backdrop content into the dst device. We add layerInputBounds origin |
| 1031 | // to offset because the original value in 'offset' was relative to 'filterCTM'. 'filterCTM' |
| 1032 | // had subtracted the layerInputBounds origin, so adding that back makes 'offset' relative |
| 1033 | // to 'layerMatrix' (what we need it to be when drawing the image by 'toRoot'). |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1034 | offset += layerInputBounds.topLeft(); |
| 1035 | |
| 1036 | // Manually setting the device's CTM requires accounting for the device's origin. |
| 1037 | // TODO (michaelludwig) - This could be simpler if the dst device had its origin configured |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 1038 | // before filtering the backdrop device, and if SkAutoDeviceTransformRestore had a way to accept |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1039 | // a global CTM instead of a device CTM. |
| 1040 | SkMatrix dstCTM = toRoot; |
| 1041 | dstCTM.postTranslate(-dstOrigin.x(), -dstOrigin.y()); |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 1042 | SkAutoDeviceTransformRestore adr(dst, dstCTM); |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1043 | |
| 1044 | // And because devices don't have a special-image draw function that supports arbitrary |
| 1045 | // matrices, we are abusing the asImage() functionality here... |
| 1046 | SkRect specialSrc = SkRect::Make(special->subset()); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1047 | auto looseImage = special->asImage(); |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1048 | dst->drawImageRect( |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1049 | looseImage.get(), &specialSrc, |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1050 | SkRect::MakeXYWH(offset.x(), offset.y(), special->width(), special->height()), |
| 1051 | p, kStrict_SrcRectConstraint); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1052 | } |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 1053 | } |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1054 | |
Mike Klein | e083f7c | 2018-02-07 12:54:27 -0500 | [diff] [blame] | 1055 | static SkImageInfo make_layer_info(const SkImageInfo& prev, int w, int h, const SkPaint* paint) { |
Mike Klein | 649fb73 | 2018-02-26 15:09:16 -0500 | [diff] [blame] | 1056 | SkColorType ct = prev.colorType(); |
Brian Osman | d7a5975 | 2019-09-20 11:16:58 -0400 | [diff] [blame] | 1057 | if (prev.bytesPerPixel() <= 4 && |
| 1058 | prev.colorType() != kRGBA_8888_SkColorType && |
| 1059 | prev.colorType() != kBGRA_8888_SkColorType) { |
Mike Klein | 649fb73 | 2018-02-26 15:09:16 -0500 | [diff] [blame] | 1060 | // "Upgrade" A8, G8, 565, 4444, 1010102, 101010x, and 888x to 8888, |
| 1061 | // ensuring plenty of alpha bits for the layer, perhaps losing some color bits in return. |
| 1062 | ct = kN32_SkColorType; |
| 1063 | } |
| 1064 | return SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType, prev.refColorSpace()); |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1067 | void SkCanvas::internalSaveLayer(const SaveLayerRec& rec, SaveLayerStrategy strategy) { |
Yuqian Li | f7c723c | 2018-08-29 16:25:47 -0700 | [diff] [blame] | 1068 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1069 | const SkRect* bounds = rec.fBounds; |
| 1070 | const SkPaint* paint = rec.fPaint; |
| 1071 | SaveLayerFlags saveLayerFlags = rec.fSaveLayerFlags; |
| 1072 | |
Mike Reed | 5532c2a | 2019-02-23 12:00:32 -0500 | [diff] [blame] | 1073 | // If we have a backdrop filter, then we must apply it to the entire layer (clip-bounds) |
| 1074 | // regardless of any hint-rect from the caller. skbug.com/8783 |
| 1075 | if (rec.fBackdrop) { |
| 1076 | bounds = nullptr; |
| 1077 | } |
| 1078 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1079 | SkLazyPaint lazyP; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 1080 | SkImageFilter* imageFilter = paint ? paint->getImageFilter() : nullptr; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1081 | SkMatrix stashedMatrix = fMCRec->fMatrix; |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 1082 | MCRec* modifiedRec = nullptr; |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1083 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1084 | /* |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1085 | * Many ImageFilters (so far) do not (on their own) correctly handle matrices (CTM) that |
| 1086 | * contain rotation/skew/etc. We rely on applyCTM to create a new image filter DAG as needed to |
| 1087 | * accommodate this, but it requires update the CTM we use when drawing into the layer. |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1088 | * |
| 1089 | * 1. Stash off the current CTM |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1090 | * 2. Apply the CTM to imagefilter, which decomposes it into simple and complex transforms |
| 1091 | * if necessary. |
| 1092 | * 3. Wack the CTM to be the remaining scale matrix and use the modified imagefilter, which |
| 1093 | * is a MatrixImageFilter that contains the complex matrix. |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1094 | * 4. Proceed as usual, allowing the client to draw into the layer (now with a scale-only CTM) |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1095 | * 5. During restore, the MatrixImageFilter automatically applies complex stage to the output |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1096 | * of the original imagefilter, and draw that (via drawSprite) |
| 1097 | * 6. Unwack the CTM to its original state (i.e. stashedMatrix) |
| 1098 | * |
| 1099 | * Perhaps in the future we could augment #5 to apply REMAINDER as part of the draw (no longer |
| 1100 | * a sprite operation) to avoid the extra buffer/overhead of MatrixImageFilter. |
| 1101 | */ |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1102 | if (imageFilter) { |
| 1103 | SkMatrix modifiedCTM; |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 1104 | sk_sp<SkImageFilter> modifiedFilter = as_IFB(imageFilter)->applyCTM(stashedMatrix, |
| 1105 | &modifiedCTM); |
| 1106 | if (as_IFB(modifiedFilter)->uniqueID() != as_IFB(imageFilter)->uniqueID()) { |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1107 | // The original filter couldn't support the CTM entirely |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 1108 | SkASSERT(modifiedCTM.isScaleTranslate() || as_IFB(imageFilter)->canHandleComplexCTM()); |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1109 | modifiedRec = fMCRec; |
| 1110 | this->internalSetMatrix(modifiedCTM); |
| 1111 | SkPaint* p = lazyP.set(*paint); |
| 1112 | p->setImageFilter(std::move(modifiedFilter)); |
| 1113 | imageFilter = p->getImageFilter(); |
| 1114 | paint = p; |
| 1115 | } |
| 1116 | // Else the filter didn't change, so modifiedCTM == stashedMatrix and there's nothing |
| 1117 | // left to do since the stack already has that as the CTM. |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1118 | } |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1119 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1120 | // do this before we create the layer. We don't call the public save() since |
| 1121 | // that would invoke a possibly overridden virtual |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1122 | this->internalSave(); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1123 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1124 | SkIRect ir; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1125 | if (!this->clipRectBounds(bounds, saveLayerFlags, &ir, imageFilter)) { |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 1126 | if (modifiedRec) { |
| 1127 | // In this case there will be no layer in which to stash the matrix so we need to |
| 1128 | // revert the prior MCRec to its earlier state. |
| 1129 | modifiedRec->fMatrix = stashedMatrix; |
| 1130 | } |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1131 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1134 | // FIXME: do willSaveLayer() overriders returning kNoLayer_SaveLayerStrategy really care about |
| 1135 | // the clipRectBounds() call above? |
| 1136 | if (kNoLayer_SaveLayerStrategy == strategy) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1137 | return; |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 1140 | SkPixelGeometry geo = fProps.pixelGeometry(); |
| 1141 | if (paint) { |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 1142 | // TODO: perhaps add a query to filters so we might preserve opaqueness... |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 1143 | if (paint->getImageFilter() || paint->getColorFilter()) { |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 1144 | geo = kUnknown_SkPixelGeometry; |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1145 | } |
| 1146 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1147 | |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1148 | SkBaseDevice* priorDevice = this->getTopDevice(); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1149 | if (nullptr == priorDevice) { // Do we still need this check??? |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1150 | SkDebugf("Unable to find device for layer."); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1151 | return; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1152 | } |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1153 | |
Mike Klein | e083f7c | 2018-02-07 12:54:27 -0500 | [diff] [blame] | 1154 | SkImageInfo info = make_layer_info(priorDevice->imageInfo(), ir.width(), ir.height(), paint); |
Mike Reed | 1f3548c | 2019-07-12 12:53:11 -0400 | [diff] [blame] | 1155 | if (rec.fSaveLayerFlags & kF16ColorType) { |
| 1156 | info = info.makeColorType(kRGBA_F16_SkColorType); |
| 1157 | } |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1158 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1159 | sk_sp<SkBaseDevice> newDevice; |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1160 | { |
Florin Malita | 4571e49 | 2019-07-16 10:25:58 -0400 | [diff] [blame] | 1161 | SkASSERT(info.alphaType() != kOpaque_SkAlphaType); |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 1162 | const SkBaseDevice::TileUsage usage = SkBaseDevice::kNever_TileUsage; |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 1163 | const bool trackCoverage = |
| 1164 | SkToBool(saveLayerFlags & kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag); |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1165 | const SkBaseDevice::CreateInfo createInfo = SkBaseDevice::CreateInfo(info, usage, geo, |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 1166 | trackCoverage, |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 1167 | fAllocator.get()); |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1168 | newDevice.reset(priorDevice->onCreateDevice(createInfo, paint)); |
| 1169 | if (!newDevice) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1170 | return; |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1171 | } |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 1172 | } |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1173 | DeviceCM* layer = new DeviceCM(newDevice, paint, stashedMatrix, rec.fClipMask, rec.fClipMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1174 | |
Mike Reed | b43a3e0 | 2017-02-11 10:18:58 -0500 | [diff] [blame] | 1175 | // only have a "next" if this new layer doesn't affect the clip (rare) |
| 1176 | layer->fNext = BoundsAffectsClip(saveLayerFlags) ? nullptr : fMCRec->fTopLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1177 | fMCRec->fLayer = layer; |
| 1178 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1179 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 1180 | if ((rec.fSaveLayerFlags & kInitWithPrevious_SaveLayerFlag) || rec.fBackdrop) { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1181 | DrawDeviceWithFilter(priorDevice, rec.fBackdrop, newDevice.get(), { ir.fLeft, ir.fTop }, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1182 | fMCRec->fMatrix); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1183 | } |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1184 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1185 | newDevice->setOrigin(fMCRec->fMatrix, ir.fLeft, ir.fTop); |
| 1186 | |
| 1187 | newDevice->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect); |
| 1188 | if (layer->fNext) { |
| 1189 | // need to punch a hole in the previous device, so we don't draw there, given that |
| 1190 | // the new top-layer will allow drawing to happen "below" it. |
| 1191 | SkRegion hole(ir); |
| 1192 | do { |
| 1193 | layer = layer->fNext; |
| 1194 | layer->fDevice->clipRegion(hole, SkClipOp::kDifference); |
| 1195 | } while (layer->fNext); |
| 1196 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1199 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) { |
reed | bada188 | 2015-12-21 13:09:44 -0800 | [diff] [blame] | 1200 | if (0xFF == alpha) { |
| 1201 | return this->saveLayer(bounds, nullptr); |
| 1202 | } else { |
| 1203 | SkPaint tmpPaint; |
| 1204 | tmpPaint.setAlpha(alpha); |
| 1205 | return this->saveLayer(bounds, &tmpPaint); |
| 1206 | } |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1209 | void SkCanvas::internalSaveBehind(const SkRect* localBounds) { |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1210 | SkIRect devBounds; |
| 1211 | if (localBounds) { |
| 1212 | SkRect tmp; |
| 1213 | fMCRec->fMatrix.mapRect(&tmp, *localBounds); |
| 1214 | if (!devBounds.intersect(tmp.round(), this->getDeviceClipBounds())) { |
| 1215 | devBounds.setEmpty(); |
| 1216 | } |
| 1217 | } else { |
| 1218 | devBounds = this->getDeviceClipBounds(); |
| 1219 | } |
| 1220 | if (devBounds.isEmpty()) { |
| 1221 | return; |
| 1222 | } |
| 1223 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1224 | SkBaseDevice* device = this->getTopDevice(); |
| 1225 | if (nullptr == device) { // Do we still need this check??? |
| 1226 | return; |
| 1227 | } |
| 1228 | |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1229 | // need the bounds relative to the device itself |
| 1230 | devBounds.offset(-device->fOrigin.fX, -device->fOrigin.fY); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1231 | |
Michael Ludwig | ac35212 | 2019-08-28 21:03:05 +0000 | [diff] [blame] | 1232 | // This is getting the special image from the current device, which is then drawn into (both by |
| 1233 | // a client, and the drawClippedToSaveBehind below). Since this is not saving a layer, with its |
| 1234 | // own device, we need to explicitly copy the back image contents so that its original content |
| 1235 | // is available when we splat it back later during restore. |
| 1236 | auto backImage = device->snapSpecial(devBounds, /* copy */ true); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1237 | if (!backImage) { |
| 1238 | return; |
| 1239 | } |
| 1240 | |
| 1241 | // we really need the save, so we can wack the fMCRec |
| 1242 | this->checkForDeferredSave(); |
| 1243 | |
| 1244 | fMCRec->fBackImage.reset(new BackImage{std::move(backImage), devBounds.topLeft()}); |
| 1245 | |
| 1246 | SkPaint paint; |
| 1247 | paint.setBlendMode(SkBlendMode::kClear); |
Mike Reed | 9adc82c | 2019-04-23 10:28:13 -0400 | [diff] [blame] | 1248 | this->drawClippedToSaveBehind(paint); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1249 | } |
| 1250 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1251 | void SkCanvas::internalRestore() { |
| 1252 | SkASSERT(fMCStack.count() != 0); |
| 1253 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1254 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1255 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 1256 | // 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] | 1257 | fMCRec->fLayer = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1258 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1259 | // move this out before we do the actual restore |
| 1260 | auto backImage = std::move(fMCRec->fBackImage); |
| 1261 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1262 | // now do the normal restore() |
| 1263 | fMCRec->~MCRec(); // balanced in save() |
| 1264 | fMCStack.pop_back(); |
| 1265 | fMCRec = (MCRec*)fMCStack.back(); |
| 1266 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1267 | if (fMCRec) { |
| 1268 | FOR_EACH_TOP_DEVICE(device->restore(fMCRec->fMatrix)); |
| 1269 | } |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1270 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1271 | if (backImage) { |
| 1272 | SkPaint paint; |
| 1273 | paint.setBlendMode(SkBlendMode::kDstOver); |
| 1274 | const int x = backImage->fLoc.x(); |
| 1275 | const int y = backImage->fLoc.y(); |
| 1276 | this->getTopDevice()->drawSpecial(backImage->fImage.get(), x, y, paint, |
| 1277 | nullptr, SkMatrix::I()); |
| 1278 | } |
| 1279 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1280 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 1281 | since if we're being recorded, we don't want to record this (the |
| 1282 | recorder will have already recorded the restore). |
| 1283 | */ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1284 | if (layer) { |
Mike Reed | b43a3e0 | 2017-02-11 10:18:58 -0500 | [diff] [blame] | 1285 | if (fMCRec) { |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1286 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
Lee Salzman | 5d8949c | 2018-09-19 15:36:54 -0400 | [diff] [blame] | 1287 | layer->fDevice->setImmutable(); |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1288 | this->internalDrawDevice(layer->fDevice.get(), origin.x(), origin.y(), |
| 1289 | layer->fPaint.get(), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1290 | layer->fClipImage.get(), layer->fClipMatrix); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1291 | // restore what we smashed in internalSaveLayer |
Ben Wagner | 738a256 | 2018-04-23 17:23:30 -0400 | [diff] [blame] | 1292 | this->internalSetMatrix(layer->fStashedMatrix); |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1293 | // reset this, since internalDrawDevice will have set it to true |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1294 | delete layer; |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 1295 | } else { |
| 1296 | // we're at the root |
reed | a499f90 | 2015-05-01 09:34:31 -0700 | [diff] [blame] | 1297 | SkASSERT(layer == (void*)fDeviceCMStorage); |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 1298 | layer->~DeviceCM(); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1299 | // no need to update fMCRec, 'cause we're killing the canvas |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1300 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1301 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1302 | |
| 1303 | if (fMCRec) { |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1304 | fIsScaleTranslate = fMCRec->fMatrix.isScaleTranslate(); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1305 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
| 1306 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1309 | sk_sp<SkSurface> SkCanvas::makeSurface(const SkImageInfo& info, const SkSurfaceProps* props) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1310 | if (nullptr == props) { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1311 | props = &fProps; |
| 1312 | } |
| 1313 | return this->onNewSurface(info, *props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1316 | sk_sp<SkSurface> SkCanvas::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1317 | SkBaseDevice* dev = this->getDevice(); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 1318 | return dev ? dev->makeSurface(info, props) : nullptr; |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1321 | SkImageInfo SkCanvas::imageInfo() const { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 1322 | return this->onImageInfo(); |
| 1323 | } |
| 1324 | |
| 1325 | SkImageInfo SkCanvas::onImageInfo() const { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1326 | SkBaseDevice* dev = this->getDevice(); |
| 1327 | if (dev) { |
| 1328 | return dev->imageInfo(); |
| 1329 | } else { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1330 | return SkImageInfo::MakeUnknown(0, 0); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 1334 | bool SkCanvas::getProps(SkSurfaceProps* props) const { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 1335 | return this->onGetProps(props); |
| 1336 | } |
| 1337 | |
| 1338 | bool SkCanvas::onGetProps(SkSurfaceProps* props) const { |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 1339 | SkBaseDevice* dev = this->getDevice(); |
| 1340 | if (dev) { |
| 1341 | if (props) { |
| 1342 | *props = fProps; |
| 1343 | } |
| 1344 | return true; |
| 1345 | } else { |
| 1346 | return false; |
| 1347 | } |
| 1348 | } |
| 1349 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 1350 | bool SkCanvas::peekPixels(SkPixmap* pmap) { |
| 1351 | return this->onPeekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1354 | bool SkCanvas::onPeekPixels(SkPixmap* pmap) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1355 | SkBaseDevice* dev = this->getDevice(); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1356 | return dev && dev->peekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1359 | void* SkCanvas::accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1360 | SkPixmap pmap; |
| 1361 | if (!this->onAccessTopLayerPixels(&pmap)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1362 | return nullptr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1363 | } |
| 1364 | if (info) { |
| 1365 | *info = pmap.info(); |
| 1366 | } |
| 1367 | if (rowBytes) { |
| 1368 | *rowBytes = pmap.rowBytes(); |
| 1369 | } |
| 1370 | if (origin) { |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1371 | *origin = this->getTopDevice()->getOrigin(); |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1372 | } |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1373 | return pmap.writable_addr(); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1376 | bool SkCanvas::onAccessTopLayerPixels(SkPixmap* pmap) { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1377 | SkBaseDevice* dev = this->getTopDevice(); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1378 | return dev && dev->accessPixels(pmap); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1381 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1382 | |
Mike Reed | 8bcd128 | 2019-03-13 16:51:54 -0400 | [diff] [blame] | 1383 | // In our current design/features, we should never have a layer (src) in a different colorspace |
| 1384 | // than its parent (dst), so we assert that here. This is called out from other asserts, in case |
| 1385 | // we add some feature in the future to allow a given layer/imagefilter to operate in a specific |
| 1386 | // colorspace. |
| 1387 | static void check_drawdevice_colorspaces(SkColorSpace* src, SkColorSpace* dst) { |
| 1388 | SkASSERT(src == dst); |
| 1389 | } |
| 1390 | |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1391 | void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, const SkPaint* paint, |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1392 | SkImage* clipImage, const SkMatrix& clipMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1393 | SkPaint tmp; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1394 | if (nullptr == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1395 | paint = &tmp; |
| 1396 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1397 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 1398 | DRAW_BEGIN_DRAWDEVICE(*paint) |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1399 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1400 | while (iter.next()) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1401 | SkBaseDevice* dstDev = iter.fDevice; |
Mike Reed | 8bcd128 | 2019-03-13 16:51:54 -0400 | [diff] [blame] | 1402 | check_drawdevice_colorspaces(dstDev->imageInfo().colorSpace(), |
| 1403 | srcDev->imageInfo().colorSpace()); |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 1404 | paint = &draw.paint(); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1405 | SkImageFilter* filter = paint->getImageFilter(); |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 1406 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1407 | if (filter || clipImage) { |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 1408 | sk_sp<SkSpecialImage> specialImage = srcDev->snapSpecial(); |
| 1409 | if (specialImage) { |
Mike Reed | 8bcd128 | 2019-03-13 16:51:54 -0400 | [diff] [blame] | 1410 | check_drawdevice_colorspaces(dstDev->imageInfo().colorSpace(), |
| 1411 | specialImage->getColorSpace()); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1412 | dstDev->drawSpecial(specialImage.get(), pos.x(), pos.y(), *paint, |
| 1413 | clipImage, clipMatrix); |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 1414 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1415 | } else { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1416 | dstDev->drawDevice(srcDev, pos.x(), pos.y(), *paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1417 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1418 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1419 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 1420 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
reed | 3270467 | 2015-12-16 08:27:10 -0800 | [diff] [blame] | 1423 | ///////////////////////////////////////////////////////////////////////////// |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1424 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1425 | void SkCanvas::translate(SkScalar dx, SkScalar dy) { |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1426 | if (dx || dy) { |
| 1427 | this->checkForDeferredSave(); |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1428 | fMCRec->fMatrix.preTranslate(dx,dy); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 1429 | |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1430 | // Translate shouldn't affect the is-scale-translateness of the matrix. |
| 1431 | SkASSERT(fIsScaleTranslate == fMCRec->fMatrix.isScaleTranslate()); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 1432 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1433 | FOR_EACH_TOP_DEVICE(device->setGlobalCTM(fMCRec->fMatrix)); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1434 | |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1435 | this->didTranslate(dx,dy); |
| 1436 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1439 | void SkCanvas::scale(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1440 | SkMatrix m; |
| 1441 | m.setScale(sx, sy); |
| 1442 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1445 | void SkCanvas::rotate(SkScalar degrees) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1446 | SkMatrix m; |
| 1447 | m.setRotate(degrees); |
| 1448 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 1451 | void SkCanvas::rotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 1452 | SkMatrix m; |
| 1453 | m.setRotate(degrees, px, py); |
| 1454 | this->concat(m); |
| 1455 | } |
| 1456 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1457 | void SkCanvas::skew(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1458 | SkMatrix m; |
| 1459 | m.setSkew(sx, sy); |
| 1460 | this->concat(m); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1463 | void SkCanvas::concat(const SkMatrix& matrix) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1464 | if (matrix.isIdentity()) { |
| 1465 | return; |
| 1466 | } |
| 1467 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1468 | this->checkForDeferredSave(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1469 | fMCRec->fMatrix.preConcat(matrix); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1470 | fIsScaleTranslate = fMCRec->fMatrix.isScaleTranslate(); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1471 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1472 | FOR_EACH_TOP_DEVICE(device->setGlobalCTM(fMCRec->fMatrix)); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1473 | |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1474 | this->didConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1477 | void SkCanvas::internalSetMatrix(const SkMatrix& matrix) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1478 | fMCRec->fMatrix = matrix; |
msarett | 9da5a5a | 2016-08-19 08:38:36 -0700 | [diff] [blame] | 1479 | fIsScaleTranslate = matrix.isScaleTranslate(); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1480 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1481 | FOR_EACH_TOP_DEVICE(device->setGlobalCTM(fMCRec->fMatrix)); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 1485 | this->checkForDeferredSave(); |
| 1486 | this->internalSetMatrix(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1487 | this->didSetMatrix(matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1490 | void SkCanvas::resetMatrix() { |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1491 | this->setMatrix(SkMatrix::I()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | ////////////////////////////////////////////////////////////////////////////// |
| 1495 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1496 | void SkCanvas::clipRect(const SkRect& rect, SkClipOp op, bool doAA) { |
Mike Reed | 9cec1bc | 2018-01-19 12:57:01 -0500 | [diff] [blame] | 1497 | if (!rect.isFinite()) { |
| 1498 | return; |
| 1499 | } |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1500 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1501 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1502 | this->onClipRect(rect, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1505 | void SkCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1506 | const bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1507 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1508 | FOR_EACH_TOP_DEVICE(device->clipRect(rect, op, isAA)); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1509 | |
reed | c64eff5 | 2015-11-21 12:39:45 -0800 | [diff] [blame] | 1510 | AutoValidateClip avc(this); |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1511 | fMCRec->fRasterClip.opRect(rect, fMCRec->fMatrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1512 | isAA); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1513 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1516 | void SkCanvas::androidFramework_setDeviceClipRestriction(const SkIRect& rect) { |
| 1517 | fClipRestrictionRect = rect; |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1518 | if (fClipRestrictionRect.isEmpty()) { |
| 1519 | // we notify the device, but we *dont* resolve deferred saves (since we're just |
| 1520 | // removing the restriction if the rect is empty. how I hate this api. |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1521 | FOR_EACH_TOP_DEVICE(device->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect)); |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1522 | } else { |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1523 | this->checkForDeferredSave(); |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1524 | FOR_EACH_TOP_DEVICE(device->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect)); |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1525 | AutoValidateClip avc(this); |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1526 | fMCRec->fRasterClip.opIRect(fClipRestrictionRect, SkRegion::kIntersect_Op); |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1527 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
| 1528 | } |
| 1529 | } |
| 1530 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1531 | void SkCanvas::clipRRect(const SkRRect& rrect, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1532 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1533 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1534 | if (rrect.isRect()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1535 | this->onClipRect(rrect.getBounds(), op, edgeStyle); |
| 1536 | } else { |
| 1537 | this->onClipRRect(rrect, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1538 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1539 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1540 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1541 | void SkCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1542 | AutoValidateClip avc(this); |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1543 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1544 | bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1545 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1546 | FOR_EACH_TOP_DEVICE(device->clipRRect(rrect, op, isAA)); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1547 | |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1548 | fMCRec->fRasterClip.opRRect(rrect, fMCRec->fMatrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1549 | isAA); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1550 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1553 | void SkCanvas::clipPath(const SkPath& path, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1554 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1555 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
robertphillips | 39f0538 | 2015-11-24 09:30:12 -0800 | [diff] [blame] | 1556 | |
| 1557 | if (!path.isInverseFillType() && fMCRec->fMatrix.rectStaysRect()) { |
| 1558 | SkRect r; |
| 1559 | if (path.isRect(&r)) { |
| 1560 | this->onClipRect(r, op, edgeStyle); |
| 1561 | return; |
| 1562 | } |
| 1563 | SkRRect rrect; |
| 1564 | if (path.isOval(&r)) { |
| 1565 | rrect.setOval(r); |
| 1566 | this->onClipRRect(rrect, op, edgeStyle); |
| 1567 | return; |
| 1568 | } |
| 1569 | if (path.isRRect(&rrect)) { |
| 1570 | this->onClipRRect(rrect, op, edgeStyle); |
| 1571 | return; |
| 1572 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1573 | } |
robertphillips | 39f0538 | 2015-11-24 09:30:12 -0800 | [diff] [blame] | 1574 | |
| 1575 | this->onClipPath(path, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1578 | void SkCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1579 | AutoValidateClip avc(this); |
| 1580 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1581 | bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1582 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1583 | FOR_EACH_TOP_DEVICE(device->clipPath(path, op, isAA)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1584 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1585 | const SkPath* rasterClipPath = &path; |
| 1586 | const SkMatrix* matrix = &fMCRec->fMatrix; |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1587 | fMCRec->fRasterClip.opPath(*rasterClipPath, *matrix, this->getTopLayerBounds(), |
| 1588 | (SkRegion::Op)op, isAA); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1589 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1592 | void SkCanvas::clipRegion(const SkRegion& rgn, SkClipOp op) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1593 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1594 | this->onClipRegion(rgn, op); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1597 | void SkCanvas::onClipRegion(const SkRegion& rgn, SkClipOp op) { |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1598 | FOR_EACH_TOP_DEVICE(device->clipRegion(rgn, op)); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1599 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1600 | AutoValidateClip avc(this); |
| 1601 | |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1602 | fMCRec->fRasterClip.opRegion(rgn, (SkRegion::Op)op); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1603 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1606 | #ifdef SK_DEBUG |
| 1607 | void SkCanvas::validateClip() const { |
| 1608 | // construct clipRgn from the clipstack |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1609 | const SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1610 | if (!device) { |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1611 | SkASSERT(this->isClipEmpty()); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1614 | } |
| 1615 | #endif |
| 1616 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1617 | bool SkCanvas::androidFramework_isClipAA() const { |
| 1618 | bool containsAA = false; |
| 1619 | |
| 1620 | FOR_EACH_TOP_DEVICE(containsAA |= device->onClipIsAA()); |
| 1621 | |
| 1622 | return containsAA; |
| 1623 | } |
| 1624 | |
| 1625 | class RgnAccumulator { |
| 1626 | SkRegion* fRgn; |
| 1627 | public: |
| 1628 | RgnAccumulator(SkRegion* total) : fRgn(total) {} |
| 1629 | void accumulate(SkBaseDevice* device, SkRegion* rgn) { |
| 1630 | SkIPoint origin = device->getOrigin(); |
| 1631 | if (origin.x() | origin.y()) { |
| 1632 | rgn->translate(origin.x(), origin.y()); |
| 1633 | } |
| 1634 | fRgn->op(*rgn, SkRegion::kUnion_Op); |
| 1635 | } |
| 1636 | }; |
| 1637 | |
| 1638 | void SkCanvas::temporary_internal_getRgnClip(SkRegion* rgn) { |
| 1639 | RgnAccumulator accum(rgn); |
| 1640 | SkRegion tmp; |
| 1641 | |
| 1642 | rgn->setEmpty(); |
| 1643 | FOR_EACH_TOP_DEVICE(device->onAsRgnClip(&tmp); accum.accumulate(device, &tmp)); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1646 | /////////////////////////////////////////////////////////////////////////////// |
| 1647 | |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1648 | bool SkCanvas::isClipEmpty() const { |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 1649 | return fMCRec->fRasterClip.isEmpty(); |
| 1650 | |
| 1651 | // TODO: should we only use the conservative answer in a recording canvas? |
| 1652 | #if 0 |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1653 | SkBaseDevice* dev = this->getTopDevice(); |
| 1654 | // if no device we return true |
| 1655 | return !dev || dev->onGetClipType() == SkBaseDevice::kEmpty_ClipType; |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 1656 | #endif |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1659 | bool SkCanvas::isClipRect() const { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1660 | SkBaseDevice* dev = this->getTopDevice(); |
| 1661 | // if no device we return false |
Dave Tapuska | 7139f13 | 2019-08-15 09:22:11 -0400 | [diff] [blame] | 1662 | return dev && dev->onGetClipType() == SkBaseDevice::ClipType::kRect; |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1665 | static inline bool is_nan_or_clipped(const Sk4f& devRect, const Sk4f& devClip) { |
| 1666 | #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 1667 | __m128 lLtT = _mm_unpacklo_ps(devRect.fVec, devClip.fVec); |
| 1668 | __m128 RrBb = _mm_unpackhi_ps(devClip.fVec, devRect.fVec); |
| 1669 | __m128 mask = _mm_cmplt_ps(lLtT, RrBb); |
| 1670 | return 0xF != _mm_movemask_ps(mask); |
| 1671 | #elif !defined(SKNX_NO_SIMD) && defined(SK_ARM_HAS_NEON) |
| 1672 | float32x4_t lLtT = vzipq_f32(devRect.fVec, devClip.fVec).val[0]; |
| 1673 | float32x4_t RrBb = vzipq_f32(devClip.fVec, devRect.fVec).val[1]; |
| 1674 | uint32x4_t mask = vcltq_f32(lLtT, RrBb); |
| 1675 | return 0xFFFFFFFFFFFFFFFF != (uint64_t) vmovn_u32(mask); |
| 1676 | #else |
| 1677 | SkRect devRectAsRect; |
| 1678 | SkRect devClipAsRect; |
| 1679 | devRect.store(&devRectAsRect.fLeft); |
| 1680 | devClip.store(&devClipAsRect.fLeft); |
| 1681 | return !devRectAsRect.isFinite() || !devRectAsRect.intersect(devClipAsRect); |
| 1682 | #endif |
| 1683 | } |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1684 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1685 | // It's important for this function to not be inlined. Otherwise the compiler will share code |
| 1686 | // between the fast path and the slow path, resulting in two slow paths. |
| 1687 | static SK_NEVER_INLINE bool quick_reject_slow_path(const SkRect& src, const SkRect& deviceClip, |
| 1688 | const SkMatrix& matrix) { |
| 1689 | SkRect deviceRect; |
| 1690 | matrix.mapRect(&deviceRect, src); |
| 1691 | return !deviceRect.isFinite() || !deviceRect.intersect(deviceClip); |
| 1692 | } |
| 1693 | |
| 1694 | bool SkCanvas::quickReject(const SkRect& src) const { |
| 1695 | #ifdef SK_DEBUG |
| 1696 | // Verify that fDeviceClipBounds are set properly. |
| 1697 | SkRect tmp = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1698 | if (fMCRec->fRasterClip.isEmpty()) { |
msarett | 0c685ee | 2016-08-14 13:51:16 -0700 | [diff] [blame] | 1699 | SkASSERT(fDeviceClipBounds.isEmpty()); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1700 | } else { |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1701 | SkASSERT(tmp == fDeviceClipBounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1702 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1703 | |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1704 | // Verify that fIsScaleTranslate is set properly. |
| 1705 | SkASSERT(fIsScaleTranslate == fMCRec->fMatrix.isScaleTranslate()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1706 | #endif |
| 1707 | |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1708 | if (!fIsScaleTranslate) { |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1709 | return quick_reject_slow_path(src, fDeviceClipBounds, fMCRec->fMatrix); |
| 1710 | } |
| 1711 | |
| 1712 | // We inline the implementation of mapScaleTranslate() for the fast path. |
| 1713 | float sx = fMCRec->fMatrix.getScaleX(); |
| 1714 | float sy = fMCRec->fMatrix.getScaleY(); |
| 1715 | float tx = fMCRec->fMatrix.getTranslateX(); |
| 1716 | float ty = fMCRec->fMatrix.getTranslateY(); |
| 1717 | Sk4f scale(sx, sy, sx, sy); |
| 1718 | Sk4f trans(tx, ty, tx, ty); |
| 1719 | |
| 1720 | // Apply matrix. |
| 1721 | Sk4f ltrb = Sk4f::Load(&src.fLeft) * scale + trans; |
| 1722 | |
| 1723 | // Make sure left < right, top < bottom. |
| 1724 | Sk4f rblt(ltrb[2], ltrb[3], ltrb[0], ltrb[1]); |
| 1725 | Sk4f min = Sk4f::Min(ltrb, rblt); |
| 1726 | Sk4f max = Sk4f::Max(ltrb, rblt); |
| 1727 | // We can extract either pair [0,1] or [2,3] from min and max and be correct, but on |
| 1728 | // ARM this sequence generates the fastest (a single instruction). |
| 1729 | Sk4f devRect = Sk4f(min[2], min[3], max[0], max[1]); |
| 1730 | |
| 1731 | // Check if the device rect is NaN or outside the clip. |
| 1732 | return is_nan_or_clipped(devRect, Sk4f::Load(&fDeviceClipBounds.fLeft)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1735 | bool SkCanvas::quickReject(const SkPath& path) const { |
| 1736 | return path.isEmpty() || this->quickReject(path.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Mike Klein | 83c8dd9 | 2017-11-28 17:08:45 -0500 | [diff] [blame] | 1739 | SkRect SkCanvas::getLocalClipBounds() const { |
| 1740 | SkIRect ibounds = this->getDeviceClipBounds(); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 1741 | if (ibounds.isEmpty()) { |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1742 | return SkRect::MakeEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1745 | SkMatrix inverse; |
| 1746 | // 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] | 1747 | if (!fMCRec->fMatrix.invert(&inverse)) { |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1748 | return SkRect::MakeEmpty(); |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1749 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1750 | |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1751 | SkRect bounds; |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1752 | // adjust it outwards in case we are antialiasing |
Mike Reed | b57b931 | 2018-04-23 12:12:54 -0400 | [diff] [blame] | 1753 | const int margin = 1; |
reed@google.com | fa4d5bd | 2012-01-30 17:36:27 +0000 | [diff] [blame] | 1754 | |
Mike Reed | b57b931 | 2018-04-23 12:12:54 -0400 | [diff] [blame] | 1755 | SkRect r = SkRect::Make(ibounds.makeOutset(margin, margin)); |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1756 | inverse.mapRect(&bounds, r); |
| 1757 | return bounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Mike Klein | 83c8dd9 | 2017-11-28 17:08:45 -0500 | [diff] [blame] | 1760 | SkIRect SkCanvas::getDeviceClipBounds() const { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1761 | return fMCRec->fRasterClip.getBounds(); |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1764 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1765 | return fMCRec->fMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1768 | GrRenderTargetContext* SkCanvas::internal_private_accessTopLayerRenderTargetContext() { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1769 | SkBaseDevice* dev = this->getTopDevice(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1770 | return dev ? dev->accessRenderTargetContext() : nullptr; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1773 | GrContext* SkCanvas::getGrContext() { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1774 | SkBaseDevice* device = this->getTopDevice(); |
reed | 86ae3d1 | 2016-04-26 06:57:31 -0700 | [diff] [blame] | 1775 | return device ? device->context() : nullptr; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1776 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1777 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1778 | void SkCanvas::drawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1779 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 1780 | TRACE_EVENT0("skia", TRACE_FUNC); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1781 | if (outer.isEmpty()) { |
| 1782 | return; |
| 1783 | } |
| 1784 | if (inner.isEmpty()) { |
| 1785 | this->drawRRect(outer, paint); |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | // We don't have this method (yet), but technically this is what we should |
Cary Clark | e0b7287 | 2017-04-12 12:03:15 -0400 | [diff] [blame] | 1790 | // be able to return ... |
| 1791 | // if (!outer.contains(inner))) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1792 | // |
| 1793 | // For now at least check for containment of bounds |
Cary Clark | e0b7287 | 2017-04-12 12:03:15 -0400 | [diff] [blame] | 1794 | if (!outer.getBounds().contains(inner.getBounds())) { |
| 1795 | return; |
| 1796 | } |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1797 | |
| 1798 | this->onDrawDRRect(outer, inner, paint); |
| 1799 | } |
| 1800 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1801 | void SkCanvas::drawPaint(const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1802 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1803 | this->onDrawPaint(paint); |
| 1804 | } |
| 1805 | |
| 1806 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1807 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 1808 | // To avoid redundant logic in our culling code and various backends, we always sort rects |
| 1809 | // before passing them along. |
| 1810 | this->onDrawRect(r.makeSorted(), paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 1813 | void SkCanvas::drawClippedToSaveBehind(const SkPaint& paint) { |
| 1814 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 1815 | this->onDrawBehind(paint); |
| 1816 | } |
| 1817 | |
msarett | dca352e | 2016-08-26 06:37:45 -0700 | [diff] [blame] | 1818 | void SkCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1819 | TRACE_EVENT0("skia", TRACE_FUNC); |
msarett | dca352e | 2016-08-26 06:37:45 -0700 | [diff] [blame] | 1820 | if (region.isEmpty()) { |
| 1821 | return; |
| 1822 | } |
| 1823 | |
| 1824 | if (region.isRect()) { |
| 1825 | return this->drawIRect(region.getBounds(), paint); |
| 1826 | } |
| 1827 | |
| 1828 | this->onDrawRegion(region, paint); |
| 1829 | } |
| 1830 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1831 | void SkCanvas::drawOval(const SkRect& r, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1832 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 1833 | // To avoid redundant logic in our culling code and various backends, we always sort rects |
| 1834 | // before passing them along. |
| 1835 | this->onDrawOval(r.makeSorted(), paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1839 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1840 | this->onDrawRRect(rrect, paint); |
| 1841 | } |
| 1842 | |
| 1843 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1844 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1845 | this->onDrawPoints(mode, count, pts, paint); |
| 1846 | } |
| 1847 | |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 1848 | void SkCanvas::drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, |
| 1849 | const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1850 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 1851 | RETURN_ON_NULL(vertices); |
Brian Salomon | cccafe8 | 2018-04-28 16:13:08 -0400 | [diff] [blame] | 1852 | // We expect fans to be converted to triangles when building or deserializing SkVertices. |
| 1853 | SkASSERT(vertices->mode() != SkVertices::kTriangleFan_VertexMode); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1854 | this->onDrawVerticesObject(vertices.get(), nullptr, 0, mode, paint); |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | void SkCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1858 | TRACE_EVENT0("skia", TRACE_FUNC); |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 1859 | RETURN_ON_NULL(vertices); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1860 | this->onDrawVerticesObject(vertices, nullptr, 0, mode, paint); |
| 1861 | } |
| 1862 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 1863 | void SkCanvas::drawVertices(const sk_sp<SkVertices>& vertices, const SkVertices::Bone bones[], |
| 1864 | int boneCount, SkBlendMode mode, const SkPaint& paint) { |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1865 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 1866 | RETURN_ON_NULL(vertices); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1867 | SkASSERT(boneCount <= 80); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1868 | this->onDrawVerticesObject(vertices.get(), bones, boneCount, mode, paint); |
| 1869 | } |
| 1870 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 1871 | void SkCanvas::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[], |
| 1872 | int boneCount, SkBlendMode mode, const SkPaint& paint) { |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1873 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 1874 | RETURN_ON_NULL(vertices); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1875 | SkASSERT(boneCount <= 80); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1876 | this->onDrawVerticesObject(vertices, bones, boneCount, mode, paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1880 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1881 | this->onDrawPath(path, paint); |
| 1882 | } |
| 1883 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1884 | void SkCanvas::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1885 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1886 | RETURN_ON_NULL(image); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1887 | this->onDrawImage(image, x, y, paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1888 | } |
| 1889 | |
Mike Reed | c4e3109 | 2018-01-30 11:15:27 -0500 | [diff] [blame] | 1890 | // Returns true if the rect can be "filled" : non-empty and finite |
| 1891 | static bool fillable(const SkRect& r) { |
| 1892 | SkScalar w = r.width(); |
| 1893 | SkScalar h = r.height(); |
| 1894 | return SkScalarIsFinite(w) && w > 0 && SkScalarIsFinite(h) && h > 0; |
| 1895 | } |
| 1896 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1897 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst, |
| 1898 | const SkPaint* paint, SrcRectConstraint constraint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1899 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1900 | RETURN_ON_NULL(image); |
Mike Reed | c4e3109 | 2018-01-30 11:15:27 -0500 | [diff] [blame] | 1901 | if (!fillable(dst) || !fillable(src)) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1902 | return; |
| 1903 | } |
| 1904 | this->onDrawImageRect(image, &src, dst, paint, constraint); |
| 1905 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1906 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1907 | void SkCanvas::drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst, |
| 1908 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1909 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1910 | this->drawImageRect(image, SkRect::Make(isrc), dst, paint, constraint); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
Brian Salomon | f08002c | 2018-10-26 16:15:46 -0400 | [diff] [blame] | 1913 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1914 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1915 | this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), dst, paint, |
Brian Salomon | f08002c | 2018-10-26 16:15:46 -0400 | [diff] [blame] | 1916 | kFast_SrcRectConstraint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1917 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1918 | |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1919 | namespace { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1920 | class LatticePaint : SkNoncopyable { |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1921 | public: |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1922 | LatticePaint(const SkPaint* origPaint) : fPaint(origPaint) { |
| 1923 | if (!origPaint) { |
| 1924 | return; |
| 1925 | } |
| 1926 | if (origPaint->getFilterQuality() > kLow_SkFilterQuality) { |
| 1927 | fPaint.writable()->setFilterQuality(kLow_SkFilterQuality); |
| 1928 | } |
| 1929 | if (origPaint->getMaskFilter()) { |
| 1930 | fPaint.writable()->setMaskFilter(nullptr); |
| 1931 | } |
| 1932 | if (origPaint->isAntiAlias()) { |
| 1933 | fPaint.writable()->setAntiAlias(false); |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | const SkPaint* get() const { |
| 1938 | return fPaint; |
| 1939 | } |
| 1940 | |
| 1941 | private: |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1942 | SkTCopyOnFirstWrite<SkPaint> fPaint; |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1943 | }; |
| 1944 | } // namespace |
| 1945 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1946 | void SkCanvas::drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 1947 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1948 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1949 | RETURN_ON_NULL(image); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1950 | if (dst.isEmpty()) { |
| 1951 | return; |
| 1952 | } |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1953 | if (SkLatticeIter::Valid(image->width(), image->height(), center)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1954 | LatticePaint latticePaint(paint); |
| 1955 | this->onDrawImageNine(image, center, dst, latticePaint.get()); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1956 | } else { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1957 | this->drawImageRect(image, dst, paint); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1958 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1961 | void SkCanvas::drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 1962 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1963 | TRACE_EVENT0("skia", TRACE_FUNC); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1964 | RETURN_ON_NULL(image); |
| 1965 | if (dst.isEmpty()) { |
| 1966 | return; |
| 1967 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 1968 | |
| 1969 | SkIRect bounds; |
| 1970 | Lattice latticePlusBounds = lattice; |
| 1971 | if (!latticePlusBounds.fBounds) { |
| 1972 | bounds = SkIRect::MakeWH(image->width(), image->height()); |
| 1973 | latticePlusBounds.fBounds = &bounds; |
| 1974 | } |
| 1975 | |
| 1976 | if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1977 | LatticePaint latticePaint(paint); |
| 1978 | this->onDrawImageLattice(image, latticePlusBounds, dst, latticePaint.get()); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1979 | } else { |
| 1980 | this->drawImageRect(image, dst, paint); |
| 1981 | } |
| 1982 | } |
| 1983 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1984 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1985 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1986 | if (bitmap.drawsNothing()) { |
tomhudson | 2df6fd6 | 2015-04-09 09:20:19 -0700 | [diff] [blame] | 1987 | return; |
| 1988 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1989 | this->onDrawBitmap(bitmap, dx, dy, paint); |
| 1990 | } |
| 1991 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1992 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1993 | const SkPaint* paint, SrcRectConstraint constraint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1994 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1995 | if (bitmap.drawsNothing() || dst.isEmpty() || src.isEmpty()) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1996 | return; |
| 1997 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1998 | this->onDrawBitmapRect(bitmap, &src, dst, paint, constraint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1999 | } |
| 2000 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 2001 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst, |
| 2002 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2003 | this->drawBitmapRect(bitmap, SkRect::Make(isrc), dst, paint, constraint); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2006 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint, |
| 2007 | SrcRectConstraint constraint) { |
| 2008 | this->drawBitmapRect(bitmap, SkRect::MakeIWH(bitmap.width(), bitmap.height()), dst, paint, |
| 2009 | constraint); |
| 2010 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2011 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2012 | void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
| 2013 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2014 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2015 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
tomhudson | 2df6fd6 | 2015-04-09 09:20:19 -0700 | [diff] [blame] | 2016 | return; |
| 2017 | } |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 2018 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), center)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 2019 | LatticePaint latticePaint(paint); |
| 2020 | this->onDrawBitmapNine(bitmap, center, dst, latticePaint.get()); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 2021 | } else { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 2022 | this->drawBitmapRect(bitmap, dst, paint); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2023 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2026 | void SkCanvas::drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst, |
| 2027 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2028 | TRACE_EVENT0("skia", TRACE_FUNC); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2029 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2030 | return; |
| 2031 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 2032 | |
| 2033 | SkIRect bounds; |
| 2034 | Lattice latticePlusBounds = lattice; |
| 2035 | if (!latticePlusBounds.fBounds) { |
| 2036 | bounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 2037 | latticePlusBounds.fBounds = &bounds; |
| 2038 | } |
| 2039 | |
| 2040 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), latticePlusBounds)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 2041 | LatticePaint latticePaint(paint); |
| 2042 | this->onDrawBitmapLattice(bitmap, latticePlusBounds, dst, latticePaint.get()); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 2043 | } else { |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2044 | this->drawBitmapRect(bitmap, dst, paint); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2045 | } |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2046 | } |
| 2047 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2048 | void SkCanvas::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2049 | const SkColor colors[], int count, SkBlendMode mode, |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2050 | const SkRect* cull, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2051 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2052 | RETURN_ON_NULL(atlas); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2053 | if (count <= 0) { |
| 2054 | return; |
| 2055 | } |
Joe Gregorio | c485907 | 2017-01-20 14:21:27 +0000 | [diff] [blame] | 2056 | SkASSERT(atlas); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2057 | SkASSERT(tex); |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2058 | this->onDrawAtlas(atlas, xform, tex, colors, count, mode, cull, paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2059 | } |
| 2060 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2061 | void SkCanvas::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2062 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2063 | if (key) { |
| 2064 | this->onDrawAnnotation(rect, key, value); |
| 2065 | } |
| 2066 | } |
| 2067 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2068 | void SkCanvas::legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
| 2069 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 2070 | if (src) { |
| 2071 | this->drawImageRect(image, *src, dst, paint, constraint); |
| 2072 | } else { |
| 2073 | this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), |
| 2074 | dst, paint, constraint); |
| 2075 | } |
| 2076 | } |
| 2077 | void SkCanvas::legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, |
| 2078 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 2079 | if (src) { |
| 2080 | this->drawBitmapRect(bitmap, *src, dst, paint, constraint); |
| 2081 | } else { |
| 2082 | this->drawBitmapRect(bitmap, SkRect::MakeIWH(bitmap.width(), bitmap.height()), |
| 2083 | dst, paint, constraint); |
| 2084 | } |
| 2085 | } |
| 2086 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2087 | void SkCanvas::private_draw_shadow_rec(const SkPath& path, const SkDrawShadowRec& rec) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2088 | TRACE_EVENT0("skia", TRACE_FUNC); |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2089 | this->onDrawShadowRec(path, rec); |
| 2090 | } |
| 2091 | |
| 2092 | void SkCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) { |
| 2093 | SkPaint paint; |
| 2094 | const SkRect& pathBounds = path.getBounds(); |
| 2095 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2096 | DRAW_BEGIN(paint, &pathBounds) |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2097 | while (iter.next()) { |
| 2098 | iter.fDevice->drawShadow(path, rec); |
| 2099 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2100 | DRAW_END |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2101 | } |
| 2102 | |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2103 | void SkCanvas::experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
Michael Ludwig | a595f86 | 2019-08-27 15:25:49 -0400 | [diff] [blame] | 2104 | QuadAAFlags aaFlags, const SkColor4f& color, |
| 2105 | SkBlendMode mode) { |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2106 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 2107 | // Make sure the rect is sorted before passing it along |
| 2108 | this->onDrawEdgeAAQuad(rect.makeSorted(), clip, aaFlags, color, mode); |
| 2109 | } |
| 2110 | |
| 2111 | void SkCanvas::experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[], int cnt, |
| 2112 | const SkPoint dstClips[], |
| 2113 | const SkMatrix preViewMatrices[], |
| 2114 | const SkPaint* paint, |
| 2115 | SrcRectConstraint constraint) { |
| 2116 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 2117 | this->onDrawEdgeAAImageSet(imageSet, cnt, dstClips, preViewMatrices, paint, constraint); |
| 2118 | } |
| 2119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2120 | ////////////////////////////////////////////////////////////////////////////// |
| 2121 | // These are the virtual drawing methods |
| 2122 | ////////////////////////////////////////////////////////////////////////////// |
| 2123 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 2124 | void SkCanvas::onDiscard() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2125 | if (fSurfaceBase) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 2126 | fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); |
| 2127 | } |
| 2128 | } |
| 2129 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2130 | void SkCanvas::onDrawPaint(const SkPaint& paint) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2131 | this->internalDrawPaint(paint); |
| 2132 | } |
| 2133 | |
| 2134 | void SkCanvas::internalDrawPaint(const SkPaint& paint) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2135 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, nullptr, false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2136 | |
| 2137 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2138 | iter.fDevice->drawPaint(draw.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2141 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2144 | void SkCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 2145 | const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2146 | if ((long)count <= 0) { |
| 2147 | return; |
| 2148 | } |
| 2149 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2150 | SkRect r; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2151 | const SkRect* bounds = nullptr; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2152 | if (paint.canComputeFastBounds()) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2153 | // special-case 2 points (common for drawing a single line) |
| 2154 | if (2 == count) { |
| 2155 | r.set(pts[0], pts[1]); |
| 2156 | } else { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 2157 | r.setBounds(pts, SkToInt(count)); |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2158 | } |
Jim Van Verth | 5d32b83 | 2018-02-21 11:14:32 -0500 | [diff] [blame] | 2159 | if (!r.isFinite()) { |
| 2160 | return; |
| 2161 | } |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2162 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2163 | if (this->quickReject(paint.computeFastStrokeBounds(r, &storage))) { |
| 2164 | return; |
| 2165 | } |
| 2166 | bounds = &r; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2167 | } |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2168 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2169 | SkASSERT(pts != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2170 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2171 | DRAW_BEGIN(paint, bounds) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2172 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2173 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2174 | iter.fDevice->drawPoints(mode, count, pts, draw.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2175 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2176 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2177 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2180 | static bool needs_autodrawlooper(SkCanvas* canvas, const SkPaint& paint) { |
Mike Reed | 9dc0b9e | 2019-07-29 17:52:48 -0400 | [diff] [blame] | 2181 | return paint.getImageFilter() != nullptr; |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2184 | void SkCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) { |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2185 | SkASSERT(r.isSorted()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2186 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2187 | SkRect storage; |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2188 | if (this->quickReject(paint.computeFastBounds(r, &storage))) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2189 | return; |
| 2190 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2191 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2192 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2193 | if (needs_autodrawlooper(this, paint)) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2194 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, &r, false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2195 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2196 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2197 | iter.fDevice->drawRect(r, draw.paint()); |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2200 | DRAW_END |
Mike Reed | 49f8da0 | 2018-08-27 10:48:52 -0400 | [diff] [blame] | 2201 | } else if (!paint.nothingToDraw()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2202 | this->predrawNotify(&r, &paint, false); |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2203 | SkDrawIter iter(this); |
| 2204 | while (iter.next()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2205 | iter.fDevice->drawRect(r, paint); |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2206 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2207 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2210 | void SkCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) { |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2211 | SkRect regionRect = SkRect::Make(region.getBounds()); |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2212 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2213 | SkRect storage; |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2214 | if (this->quickReject(paint.computeFastBounds(regionRect, &storage))) { |
| 2215 | return; |
| 2216 | } |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2219 | DRAW_BEGIN(paint, ®ionRect) |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2220 | |
| 2221 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2222 | iter.fDevice->drawRegion(region, draw.paint()); |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2225 | DRAW_END |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2228 | void SkCanvas::onDrawBehind(const SkPaint& paint) { |
| 2229 | SkIRect bounds; |
| 2230 | SkDeque::Iter iter(fMCStack, SkDeque::Iter::kBack_IterStart); |
| 2231 | for (;;) { |
| 2232 | const MCRec* rec = (const MCRec*)iter.prev(); |
| 2233 | if (!rec) { |
| 2234 | return; // no backimages, so nothing to draw |
| 2235 | } |
| 2236 | if (rec->fBackImage) { |
| 2237 | bounds = SkIRect::MakeXYWH(rec->fBackImage->fLoc.fX, rec->fBackImage->fLoc.fY, |
| 2238 | rec->fBackImage->fImage->width(), |
| 2239 | rec->fBackImage->fImage->height()); |
| 2240 | break; |
| 2241 | } |
| 2242 | } |
| 2243 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2244 | DRAW_BEGIN(paint, nullptr) |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2245 | |
| 2246 | while (iter.next()) { |
| 2247 | SkBaseDevice* dev = iter.fDevice; |
| 2248 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2249 | dev->save(); |
| 2250 | // We use clipRegion because it is already defined to operate in dev-space |
| 2251 | // (i.e. ignores the ctm). However, it is going to first translate by -origin, |
| 2252 | // but we don't want that, so we undo that before calling in. |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 2253 | SkRegion rgn(bounds.makeOffset(dev->fOrigin)); |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2254 | dev->clipRegion(rgn, SkClipOp::kIntersect); |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2255 | dev->drawPaint(draw.paint()); |
Mike Reed | 9adc82c | 2019-04-23 10:28:13 -0400 | [diff] [blame] | 2256 | dev->restore(fMCRec->fMatrix); |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2257 | } |
| 2258 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2259 | DRAW_END |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2260 | } |
| 2261 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2262 | void SkCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) { |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2263 | SkASSERT(oval.isSorted()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2264 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2265 | SkRect storage; |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2266 | if (this->quickReject(paint.computeFastBounds(oval, &storage))) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2267 | return; |
| 2268 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2269 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 2270 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2271 | DRAW_BEGIN(paint, &oval) |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 2272 | |
| 2273 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2274 | iter.fDevice->drawOval(oval, draw.paint()); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2277 | DRAW_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2280 | void SkCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, |
| 2281 | SkScalar sweepAngle, bool useCenter, |
| 2282 | const SkPaint& paint) { |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2283 | SkASSERT(oval.isSorted()); |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2284 | if (paint.canComputeFastBounds()) { |
| 2285 | SkRect storage; |
| 2286 | // Note we're using the entire oval as the bounds. |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2287 | if (this->quickReject(paint.computeFastBounds(oval, &storage))) { |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2288 | return; |
| 2289 | } |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2292 | DRAW_BEGIN(paint, &oval) |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2293 | |
| 2294 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2295 | iter.fDevice->drawArc(oval, startAngle, sweepAngle, useCenter, draw.paint()); |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2296 | } |
| 2297 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2298 | DRAW_END |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2301 | void SkCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2302 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2303 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2304 | if (this->quickReject(paint.computeFastBounds(rrect.getBounds(), &storage))) { |
| 2305 | return; |
| 2306 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | if (rrect.isRect()) { |
| 2310 | // call the non-virtual version |
| 2311 | this->SkCanvas::drawRect(rrect.getBounds(), paint); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2312 | return; |
| 2313 | } else if (rrect.isOval()) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2314 | // call the non-virtual version |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2315 | this->SkCanvas::drawOval(rrect.getBounds(), paint); |
| 2316 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2317 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2318 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2319 | DRAW_BEGIN(paint, &rrect.getBounds()) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2320 | |
| 2321 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2322 | iter.fDevice->drawRRect(rrect, draw.paint()); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2325 | DRAW_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2328 | void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2329 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2330 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2331 | if (this->quickReject(paint.computeFastBounds(outer.getBounds(), &storage))) { |
| 2332 | return; |
| 2333 | } |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2334 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2335 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2336 | DRAW_BEGIN(paint, &outer.getBounds()) |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2337 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2338 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2339 | iter.fDevice->drawDRRect(outer, inner, draw.paint()); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2340 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2341 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2342 | DRAW_END |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2343 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2344 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2345 | void SkCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | 9364511 | 2012-07-26 16:11:47 +0000 | [diff] [blame] | 2346 | if (!path.isFinite()) { |
| 2347 | return; |
| 2348 | } |
| 2349 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2350 | const SkRect& pathBounds = path.getBounds(); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2351 | if (!path.isInverseFillType() && paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2352 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2353 | if (this->quickReject(paint.computeFastBounds(pathBounds, &storage))) { |
| 2354 | return; |
| 2355 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2356 | } |
commit-bot@chromium.org | 0b45dc4 | 2014-02-21 05:42:57 +0000 | [diff] [blame] | 2357 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2358 | if (pathBounds.width() <= 0 && pathBounds.height() <= 0) { |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 2359 | if (path.isInverseFillType()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2360 | this->internalDrawPaint(paint); |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 2361 | return; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2362 | } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2363 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2364 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2365 | DRAW_BEGIN(paint, &pathBounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2366 | |
| 2367 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2368 | iter.fDevice->drawPath(path, draw.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2371 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2374 | 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] | 2375 | if (!paint.getImageFilter()) { |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
| 2379 | const SkMatrix& ctm = this->getTotalMatrix(); |
fmalita | c7e211a | 2016-01-07 10:34:46 -0800 | [diff] [blame] | 2380 | if (!SkTreatAsSprite(ctm, SkISize::Make(w, h), paint)) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2381 | return false; |
| 2382 | } |
| 2383 | |
| 2384 | // Currently we can only use the filterSprite code if we are clipped to the bitmap's bounds. |
| 2385 | // Once we can filter and the filter will return a result larger than itself, we should be |
| 2386 | // able to remove this constraint. |
| 2387 | // skbug.com/4526 |
| 2388 | // |
| 2389 | SkPoint pt; |
| 2390 | ctm.mapXY(x, y, &pt); |
| 2391 | SkIRect ir = SkIRect::MakeXYWH(SkScalarRoundToInt(pt.x()), SkScalarRoundToInt(pt.y()), w, h); |
| 2392 | return ir.contains(fMCRec->fRasterClip.getBounds()); |
| 2393 | } |
| 2394 | |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2395 | // Given storage for a real paint, and an optional paint parameter, clean-up the param (if non-null) |
| 2396 | // given the drawing semantics for drawImage/bitmap (skbug.com/7804) and return it, or the original |
| 2397 | // null. |
| 2398 | static const SkPaint* init_image_paint(SkPaint* real, const SkPaint* paintParam) { |
| 2399 | if (paintParam) { |
| 2400 | *real = *paintParam; |
| 2401 | real->setStyle(SkPaint::kFill_Style); |
| 2402 | real->setPathEffect(nullptr); |
| 2403 | paintParam = real; |
| 2404 | } |
| 2405 | return paintParam; |
| 2406 | } |
| 2407 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2408 | void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2409 | SkPaint realPaint; |
| 2410 | paint = init_image_paint(&realPaint, paint); |
| 2411 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2412 | SkRect bounds = SkRect::MakeXYWH(x, y, |
| 2413 | SkIntToScalar(image->width()), SkIntToScalar(image->height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2414 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2415 | SkRect tmp = bounds; |
| 2416 | if (paint) { |
| 2417 | paint->computeFastBounds(tmp, &tmp); |
| 2418 | } |
| 2419 | if (this->quickReject(tmp)) { |
| 2420 | return; |
| 2421 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2422 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2423 | // At this point we need a real paint object. If the caller passed null, then we should |
| 2424 | // use realPaint (in its default state). If the caller did pass a paint, then we have copied |
| 2425 | // (and modified) it in realPaint. Thus either way, "realPaint" is what we want to use. |
| 2426 | paint = &realPaint; |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2427 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2428 | sk_sp<SkSpecialImage> special; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2429 | bool drawAsSprite = this->canDrawBitmapAsSprite(x, y, image->width(), image->height(), |
| 2430 | *paint); |
| 2431 | if (drawAsSprite && paint->getImageFilter()) { |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2432 | special = this->getDevice()->makeSpecial(image); |
| 2433 | if (!special) { |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2434 | drawAsSprite = false; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2435 | } |
| 2436 | } |
| 2437 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2438 | DRAW_BEGIN_DRAWBITMAP(*paint, drawAsSprite, &bounds) |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2439 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2440 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2441 | const SkPaint& pnt = draw.paint(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2442 | if (special) { |
| 2443 | SkPoint pt; |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 2444 | iter.fDevice->localToDevice().mapXY(x, y, &pt); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2445 | iter.fDevice->drawSpecial(special.get(), |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2446 | SkScalarRoundToInt(pt.fX), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 2447 | SkScalarRoundToInt(pt.fY), pnt, |
| 2448 | nullptr, SkMatrix::I()); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2449 | } else { |
Michael Ludwig | b7d64b9 | 2019-02-11 11:09:15 -0500 | [diff] [blame] | 2450 | iter.fDevice->drawImageRect( |
| 2451 | image, nullptr, SkRect::MakeXYWH(x, y, image->width(), image->height()), pnt, |
| 2452 | kStrict_SrcRectConstraint); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2453 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2454 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2455 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2456 | DRAW_END |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2459 | void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2460 | const SkPaint* paint, SrcRectConstraint constraint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2461 | SkPaint realPaint; |
| 2462 | paint = init_image_paint(&realPaint, paint); |
| 2463 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2464 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2465 | SkRect storage = dst; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2466 | if (paint) { |
| 2467 | paint->computeFastBounds(dst, &storage); |
| 2468 | } |
| 2469 | if (this->quickReject(storage)) { |
| 2470 | return; |
| 2471 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2472 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2473 | paint = &realPaint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2474 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2475 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(*paint, &dst, image->isOpaque()) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2476 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2477 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2478 | iter.fDevice->drawImageRect(image, src, dst, draw.paint(), constraint); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2479 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2480 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2481 | DRAW_END |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2484 | void SkCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2485 | SkDEBUGCODE(bitmap.validate();) |
| 2486 | |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2487 | if (bitmap.drawsNothing()) { |
| 2488 | return; |
| 2489 | } |
| 2490 | |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2491 | SkPaint realPaint; |
| 2492 | init_image_paint(&realPaint, paint); |
| 2493 | paint = &realPaint; |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2494 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2495 | SkRect bounds; |
| 2496 | bitmap.getBounds(&bounds); |
| 2497 | bounds.offset(x, y); |
| 2498 | bool canFastBounds = paint->canComputeFastBounds(); |
| 2499 | if (canFastBounds) { |
| 2500 | SkRect storage; |
| 2501 | if (this->quickReject(paint->computeFastBounds(bounds, &storage))) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2502 | return; |
| 2503 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2504 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2505 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2506 | sk_sp<SkSpecialImage> special; |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2507 | bool drawAsSprite = canFastBounds && this->canDrawBitmapAsSprite(x, y, bitmap.width(), |
| 2508 | bitmap.height(), *paint); |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2509 | if (drawAsSprite && paint->getImageFilter()) { |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2510 | special = this->getDevice()->makeSpecial(bitmap); |
| 2511 | if (!special) { |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2512 | drawAsSprite = false; |
| 2513 | } |
| 2514 | } |
| 2515 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2516 | DRAW_BEGIN_DRAWBITMAP(*paint, drawAsSprite, &bounds) |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2517 | |
| 2518 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2519 | const SkPaint& pnt = draw.paint(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2520 | if (special) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2521 | SkPoint pt; |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 2522 | iter.fDevice->localToDevice().mapXY(x, y, &pt); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2523 | iter.fDevice->drawSpecial(special.get(), |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2524 | SkScalarRoundToInt(pt.fX), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 2525 | SkScalarRoundToInt(pt.fY), pnt, |
| 2526 | nullptr, SkMatrix::I()); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2527 | } else { |
Michael Ludwig | b7d64b9 | 2019-02-11 11:09:15 -0500 | [diff] [blame] | 2528 | SkRect fullImage = SkRect::MakeWH(bitmap.width(), bitmap.height()); |
| 2529 | iter.fDevice->drawBitmapRect(bitmap, &fullImage, fullImage.makeOffset(x, y), pnt, |
| 2530 | kStrict_SrcRectConstraint); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2531 | } |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2532 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 2533 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2534 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2537 | // 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] | 2538 | void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 2539 | const SkRect& dst, const SkPaint* paint, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 2540 | SrcRectConstraint constraint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 2541 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2544 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2545 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2546 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2547 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2548 | return; |
| 2549 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2550 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2551 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2552 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2553 | if (nullptr == paint) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2554 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2555 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2556 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2557 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(*paint, &dst, bitmap.isOpaque()) |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2558 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2559 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2560 | iter.fDevice->drawBitmapRect(bitmap, src, dst, draw.paint(), constraint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2561 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2562 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2563 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2566 | void SkCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2567 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2568 | SkDEBUGCODE(bitmap.validate();) |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2569 | this->internalDrawBitmapRect(bitmap, src, dst, paint, constraint); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2572 | void SkCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 2573 | const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2574 | SkPaint realPaint; |
| 2575 | paint = init_image_paint(&realPaint, paint); |
| 2576 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2577 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2578 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2579 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2580 | return; |
| 2581 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2582 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2583 | paint = &realPaint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2584 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2585 | DRAW_BEGIN(*paint, &dst) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2586 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2587 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2588 | iter.fDevice->drawImageNine(image, center, dst, draw.paint()); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2589 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2590 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2591 | DRAW_END |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2594 | void SkCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
| 2595 | const SkPaint* paint) { |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2596 | SkDEBUGCODE(bitmap.validate();) |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2597 | SkPaint realPaint; |
| 2598 | paint = init_image_paint(&realPaint, paint); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2599 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2600 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2601 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2602 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2603 | return; |
| 2604 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2605 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2606 | paint = &realPaint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2607 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2608 | DRAW_BEGIN(*paint, &dst) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2609 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2610 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2611 | iter.fDevice->drawBitmapNine(bitmap, center, dst, draw.paint()); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2612 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2613 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2614 | DRAW_END |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2617 | void SkCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 2618 | const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2619 | SkPaint realPaint; |
| 2620 | paint = init_image_paint(&realPaint, paint); |
| 2621 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2622 | if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2623 | SkRect storage; |
| 2624 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2625 | return; |
| 2626 | } |
| 2627 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2628 | paint = &realPaint; |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2629 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2630 | DRAW_BEGIN(*paint, &dst) |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2631 | |
| 2632 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2633 | iter.fDevice->drawImageLattice(image, lattice, dst, draw.paint()); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2636 | DRAW_END |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | void SkCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, |
| 2640 | const SkRect& dst, const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2641 | SkPaint realPaint; |
| 2642 | paint = init_image_paint(&realPaint, paint); |
| 2643 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2644 | if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2645 | SkRect storage; |
| 2646 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2647 | return; |
| 2648 | } |
| 2649 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2650 | paint = &realPaint; |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2651 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2652 | DRAW_BEGIN(*paint, &dst) |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2653 | |
| 2654 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2655 | iter.fDevice->drawBitmapLattice(bitmap, lattice, dst, draw.paint()); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2656 | } |
| 2657 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2658 | DRAW_END |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2661 | void SkCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2662 | const SkPaint& paint) { |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2663 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2664 | const SkRect* bounds = nullptr; |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 2665 | if (paint.canComputeFastBounds()) { |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2666 | storage = blob->bounds().makeOffset(x, y); |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2667 | SkRect tmp; |
| 2668 | if (this->quickReject(paint.computeFastBounds(storage, &tmp))) { |
| 2669 | return; |
| 2670 | } |
| 2671 | bounds = &storage; |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 2672 | } |
| 2673 | |
fmalita | 024f996 | 2015-03-03 19:08:17 -0800 | [diff] [blame] | 2674 | // We cannot filter in the looper as we normally do, because the paint is |
| 2675 | // incomplete at this point (text-related attributes are embedded within blob run paints). |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2676 | DRAW_BEGIN(paint, bounds) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2677 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2678 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2679 | fScratchGlyphRunBuilder->drawTextBlob(draw.paint(), *blob, {x, y}, iter.fDevice); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2680 | } |
| 2681 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2682 | DRAW_END |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 2685 | // These call the (virtual) onDraw... method |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 2686 | void SkCanvas::drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding, |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 2687 | SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) { |
| 2688 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 2689 | if (byteLength) { |
| 2690 | sk_msan_assert_initialized(text, SkTAddOffset<const void>(text, byteLength)); |
Mike Reed | 704a342 | 2018-12-06 15:44:14 -0500 | [diff] [blame] | 2691 | this->drawTextBlob(SkTextBlob::MakeFromText(text, byteLength, font, encoding), x, y, paint); |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 2692 | } |
| 2693 | } |
Mike Reed | 4f81bb7 | 2019-01-23 09:23:00 -0500 | [diff] [blame] | 2694 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2695 | void SkCanvas::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2696 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 2697 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2698 | RETURN_ON_NULL(blob); |
Mike Reed | 74d6e11 | 2018-01-23 13:06:12 -0500 | [diff] [blame] | 2699 | RETURN_ON_FALSE(blob->bounds().makeOffset(x, y).isFinite()); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2700 | this->onDrawTextBlob(blob, x, y, paint); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2701 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2702 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 2703 | void SkCanvas::onDrawVerticesObject(const SkVertices* vertices, const SkVertices::Bone bones[], |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 2704 | int boneCount, SkBlendMode bmode, const SkPaint& paint) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2705 | DRAW_BEGIN(paint, nullptr) |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 2706 | |
| 2707 | while (iter.next()) { |
| 2708 | // In the common case of one iteration we could std::move vertices here. |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2709 | iter.fDevice->drawVertices(vertices, bones, boneCount, bmode, draw.paint()); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 2710 | } |
| 2711 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2712 | DRAW_END |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 2713 | } |
| 2714 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2715 | void SkCanvas::drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2716 | const SkPoint texCoords[4], SkBlendMode bmode, |
| 2717 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 2718 | TRACE_EVENT0("skia", TRACE_FUNC); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2719 | if (nullptr == cubics) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2720 | return; |
| 2721 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2722 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2723 | this->onDrawPatch(cubics, colors, texCoords, bmode, paint); |
msarett | 9340c26 | 2016-09-22 05:20:21 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | void SkCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2727 | const SkPoint texCoords[4], SkBlendMode bmode, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2728 | const SkPaint& paint) { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2729 | // Since a patch is always within the convex hull of the control points, we discard it when its |
| 2730 | // bounding rectangle is completely outside the current clip. |
| 2731 | SkRect bounds; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 2732 | bounds.setBounds(cubics, SkPatchUtils::kNumCtrlPts); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2733 | if (this->quickReject(bounds)) { |
| 2734 | return; |
| 2735 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2736 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2737 | DRAW_BEGIN(paint, nullptr) |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2738 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2739 | while (iter.next()) { |
Brian Osman | e0a9962 | 2018-07-09 16:12:27 -0400 | [diff] [blame] | 2740 | iter.fDevice->drawPatch(cubics, colors, texCoords, bmode, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2741 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2742 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2743 | DRAW_END |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2746 | void SkCanvas::drawDrawable(SkDrawable* dr, SkScalar x, SkScalar y) { |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2747 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2748 | TRACE_EVENT0("skia", TRACE_FUNC); |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2749 | #endif |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2750 | RETURN_ON_NULL(dr); |
| 2751 | if (x || y) { |
| 2752 | SkMatrix matrix = SkMatrix::MakeTrans(x, y); |
| 2753 | this->onDrawDrawable(dr, &matrix); |
| 2754 | } else { |
| 2755 | this->onDrawDrawable(dr, nullptr); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2759 | void SkCanvas::drawDrawable(SkDrawable* dr, const SkMatrix* matrix) { |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2760 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2761 | TRACE_EVENT0("skia", TRACE_FUNC); |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2762 | #endif |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2763 | RETURN_ON_NULL(dr); |
| 2764 | if (matrix && matrix->isIdentity()) { |
| 2765 | matrix = nullptr; |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2766 | } |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2767 | this->onDrawDrawable(dr, matrix); |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | void SkCanvas::onDrawDrawable(SkDrawable* dr, const SkMatrix* matrix) { |
reed | 2a62e85 | 2016-10-03 10:35:37 -0700 | [diff] [blame] | 2771 | // drawable bounds are no longer reliable (e.g. android displaylist) |
| 2772 | // so don't use them for quick-reject |
Greg Daniel | 9ed1a2c | 2018-10-18 12:43:25 -0400 | [diff] [blame] | 2773 | this->getDevice()->drawDrawable(dr, matrix, this); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2774 | } |
| 2775 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2776 | void SkCanvas::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2777 | const SkColor colors[], int count, SkBlendMode bmode, |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2778 | const SkRect* cull, const SkPaint* paint) { |
| 2779 | if (cull && this->quickReject(*cull)) { |
| 2780 | return; |
| 2781 | } |
| 2782 | |
| 2783 | SkPaint pnt; |
| 2784 | if (paint) { |
| 2785 | pnt = *paint; |
| 2786 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2787 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2788 | DRAW_BEGIN(pnt, nullptr) |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2789 | while (iter.next()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2790 | iter.fDevice->drawAtlas(atlas, xform, tex, colors, count, bmode, pnt); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2791 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2792 | DRAW_END |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2795 | void SkCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 2796 | SkASSERT(key); |
| 2797 | |
| 2798 | SkPaint paint; |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2799 | DRAW_BEGIN(paint, nullptr) |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2800 | while (iter.next()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2801 | iter.fDevice->drawAnnotation(rect, key, value); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2802 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2803 | DRAW_END |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
Michael Ludwig | a595f86 | 2019-08-27 15:25:49 -0400 | [diff] [blame] | 2806 | void SkCanvas::onDrawEdgeAAQuad(const SkRect& r, const SkPoint clip[4], QuadAAFlags edgeAA, |
| 2807 | const SkColor4f& color, SkBlendMode mode) { |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2808 | SkASSERT(r.isSorted()); |
| 2809 | |
| 2810 | // If this used a paint, it would be a filled color with blend mode, which does not |
| 2811 | // need to use an autodraw loop, so use SkDrawIter directly. |
| 2812 | if (this->quickReject(r)) { |
| 2813 | return; |
| 2814 | } |
| 2815 | |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2816 | this->predrawNotify(&r, nullptr, false); |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2817 | SkDrawIter iter(this); |
| 2818 | while(iter.next()) { |
| 2819 | iter.fDevice->drawEdgeAAQuad(r, clip, edgeAA, color, mode); |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | void SkCanvas::onDrawEdgeAAImageSet(const ImageSetEntry imageSet[], int count, |
| 2824 | const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
| 2825 | const SkPaint* paint, SrcRectConstraint constraint) { |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2826 | if (count <= 0) { |
| 2827 | // Nothing to draw |
| 2828 | return; |
| 2829 | } |
| 2830 | |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2831 | SkPaint realPaint; |
| 2832 | init_image_paint(&realPaint, paint); |
| 2833 | |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2834 | // We could calculate the set's dstRect union to always check quickReject(), but we can't reject |
| 2835 | // individual entries and Chromium's occlusion culling already makes it likely that at least one |
| 2836 | // entry will be visible. So, we only calculate the draw bounds when it's trivial (count == 1), |
| 2837 | // or we need it for the autolooper (since it greatly improves image filter perf). |
| 2838 | bool needsAutoLooper = needs_autodrawlooper(this, realPaint); |
| 2839 | bool setBoundsValid = count == 1 || needsAutoLooper; |
| 2840 | SkRect setBounds = imageSet[0].fDstRect; |
| 2841 | if (imageSet[0].fMatrixIndex >= 0) { |
| 2842 | // Account for the per-entry transform that is applied prior to the CTM when drawing |
| 2843 | preViewMatrices[imageSet[0].fMatrixIndex].mapRect(&setBounds); |
Michael Ludwig | 977b50a | 2019-08-27 13:23:20 -0400 | [diff] [blame] | 2844 | } |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2845 | if (needsAutoLooper) { |
| 2846 | for (int i = 1; i < count; ++i) { |
| 2847 | SkRect entryBounds = imageSet[i].fDstRect; |
| 2848 | if (imageSet[i].fMatrixIndex >= 0) { |
| 2849 | preViewMatrices[imageSet[i].fMatrixIndex].mapRect(&entryBounds); |
| 2850 | } |
| 2851 | setBounds.joinPossiblyEmptyRect(entryBounds); |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | // If we happen to have the draw bounds, though, might as well check quickReject(). |
| 2856 | if (setBoundsValid && realPaint.canComputeFastBounds()) { |
| 2857 | SkRect tmp; |
| 2858 | if (this->quickReject(realPaint.computeFastBounds(setBounds, &tmp))) { |
| 2859 | return; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | if (needsAutoLooper) { |
| 2864 | SkASSERT(setBoundsValid); |
| 2865 | DRAW_BEGIN(realPaint, &setBounds) |
| 2866 | while (iter.next()) { |
| 2867 | iter.fDevice->drawEdgeAAImageSet( |
| 2868 | imageSet, count, dstClips, preViewMatrices, draw.paint(), constraint); |
| 2869 | } |
| 2870 | DRAW_END |
| 2871 | } else { |
| 2872 | this->predrawNotify(); |
| 2873 | SkDrawIter iter(this); |
| 2874 | while(iter.next()) { |
| 2875 | iter.fDevice->drawEdgeAAImageSet( |
| 2876 | imageSet, count, dstClips, preViewMatrices, realPaint, constraint); |
| 2877 | } |
| 2878 | } |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2879 | } |
| 2880 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2881 | ////////////////////////////////////////////////////////////////////////////// |
| 2882 | // These methods are NOT virtual, and therefore must call back into virtual |
| 2883 | // methods, rather than actually drawing themselves. |
| 2884 | ////////////////////////////////////////////////////////////////////////////// |
| 2885 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2886 | void SkCanvas::drawColor(SkColor c, SkBlendMode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2887 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2888 | paint.setColor(c); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2889 | paint.setBlendMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2890 | this->drawPaint(paint); |
| 2891 | } |
| 2892 | |
| 2893 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 2894 | const SkPoint pt = { x, y }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2895 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2896 | } |
| 2897 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 2898 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2899 | SkPoint pts[2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2900 | pts[0].set(x0, y0); |
| 2901 | pts[1].set(x1, y1); |
| 2902 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 2903 | } |
| 2904 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 2905 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2906 | if (radius < 0) { |
| 2907 | radius = 0; |
| 2908 | } |
| 2909 | |
| 2910 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 2911 | r.setLTRB(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2912 | this->drawOval(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 2916 | const SkPaint& paint) { |
| 2917 | if (rx > 0 && ry > 0) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2918 | SkRRect rrect; |
| 2919 | rrect.setRectXY(r, rx, ry); |
| 2920 | this->drawRRect(rrect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2921 | } else { |
| 2922 | this->drawRect(r, paint); |
| 2923 | } |
| 2924 | } |
| 2925 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2926 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 2927 | SkScalar sweepAngle, bool useCenter, |
| 2928 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 2929 | TRACE_EVENT0("skia", TRACE_FUNC); |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 2930 | if (oval.isEmpty() || !sweepAngle) { |
| 2931 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2932 | } |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 2933 | this->onDrawArc(oval, startAngle, sweepAngle, useCenter, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 2936 | /////////////////////////////////////////////////////////////////////////////// |
Kevin Lubick | 32dfdbe | 2018-10-18 09:47:01 -0400 | [diff] [blame] | 2937 | #ifdef SK_DISABLE_SKPICTURE |
| 2938 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) {} |
reed | 1c2c441 | 2015-04-30 13:09:24 -0700 | [diff] [blame] | 2939 | |
Kevin Lubick | 32dfdbe | 2018-10-18 09:47:01 -0400 | [diff] [blame] | 2940 | |
| 2941 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 2942 | const SkPaint* paint) {} |
| 2943 | #else |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 2944 | /** |
| 2945 | * This constant is trying to balance the speed of ref'ing a subpicture into a parent picture, |
| 2946 | * against the playback cost of recursing into the subpicture to get at its actual ops. |
| 2947 | * |
| 2948 | * For now we pick a conservatively small value, though measurement (and other heuristics like |
| 2949 | * the type of ops contained) may justify changing this value. |
| 2950 | */ |
| 2951 | #define kMaxPictureOpsToUnrollInsteadOfRef 1 |
| 2952 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2953 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2954 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2955 | RETURN_ON_NULL(picture); |
| 2956 | |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2957 | if (matrix && matrix->isIdentity()) { |
| 2958 | matrix = nullptr; |
| 2959 | } |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 2960 | if (picture->approximateOpCount() <= kMaxPictureOpsToUnrollInsteadOfRef) { |
| 2961 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
| 2962 | picture->playback(this); |
| 2963 | } else { |
| 2964 | this->onDrawPicture(picture, matrix, paint); |
| 2965 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2966 | } |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2967 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2968 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 2969 | const SkPaint* paint) { |
fmalita | d028180 | 2015-08-20 12:08:18 -0700 | [diff] [blame] | 2970 | if (!paint || paint->canComputeFastBounds()) { |
| 2971 | SkRect bounds = picture->cullRect(); |
| 2972 | if (paint) { |
| 2973 | paint->computeFastBounds(bounds, &bounds); |
| 2974 | } |
| 2975 | if (matrix) { |
| 2976 | matrix->mapRect(&bounds); |
| 2977 | } |
| 2978 | if (this->quickReject(bounds)) { |
| 2979 | return; |
| 2980 | } |
| 2981 | } |
| 2982 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2983 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 2984 | picture->playback(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2985 | } |
Kevin Lubick | 32dfdbe | 2018-10-18 09:47:01 -0400 | [diff] [blame] | 2986 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2987 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2988 | /////////////////////////////////////////////////////////////////////////////// |
| 2989 | /////////////////////////////////////////////////////////////////////////////// |
| 2990 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 2991 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas) { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 2992 | static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2993 | |
| 2994 | SkASSERT(canvas); |
| 2995 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 2996 | fImpl = new (fStorage) SkDrawIter(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2997 | fDone = !fImpl->next(); |
| 2998 | } |
| 2999 | |
| 3000 | SkCanvas::LayerIter::~LayerIter() { |
| 3001 | fImpl->~SkDrawIter(); |
| 3002 | } |
| 3003 | |
| 3004 | void SkCanvas::LayerIter::next() { |
| 3005 | fDone = !fImpl->next(); |
| 3006 | } |
| 3007 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 3008 | SkBaseDevice* SkCanvas::LayerIter::device() const { |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 3009 | return fImpl->fDevice; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 3013 | return fImpl->fDevice->localToDevice(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 3017 | const SkPaint* paint = fImpl->getPaint(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3018 | if (nullptr == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3019 | paint = &fDefaultPaint; |
| 3020 | } |
| 3021 | return *paint; |
| 3022 | } |
| 3023 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 3024 | SkIRect SkCanvas::LayerIter::clipBounds() const { |
| 3025 | return fImpl->fDevice->getGlobalBounds(); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 3026 | } |
| 3027 | |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 3028 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 3029 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 3030 | |
| 3031 | /////////////////////////////////////////////////////////////////////////////// |
| 3032 | |
Brian Osman | e8a9863 | 2019-04-10 10:26:10 -0400 | [diff] [blame] | 3033 | SkCanvas::ImageSetEntry::ImageSetEntry() = default; |
| 3034 | SkCanvas::ImageSetEntry::~ImageSetEntry() = default; |
| 3035 | SkCanvas::ImageSetEntry::ImageSetEntry(const ImageSetEntry&) = default; |
| 3036 | SkCanvas::ImageSetEntry& SkCanvas::ImageSetEntry::operator=(const ImageSetEntry&) = default; |
| 3037 | |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 3038 | SkCanvas::ImageSetEntry::ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, |
| 3039 | const SkRect& dstRect, int matrixIndex, float alpha, |
| 3040 | unsigned aaFlags, bool hasClip) |
| 3041 | : fImage(std::move(image)) |
| 3042 | , fSrcRect(srcRect) |
| 3043 | , fDstRect(dstRect) |
| 3044 | , fMatrixIndex(matrixIndex) |
| 3045 | , fAlpha(alpha) |
| 3046 | , fAAFlags(aaFlags) |
| 3047 | , fHasClip(hasClip) {} |
| 3048 | |
| 3049 | SkCanvas::ImageSetEntry::ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, |
| 3050 | const SkRect& dstRect, float alpha, unsigned aaFlags) |
| 3051 | : fImage(std::move(image)) |
| 3052 | , fSrcRect(srcRect) |
| 3053 | , fDstRect(dstRect) |
| 3054 | , fAlpha(alpha) |
| 3055 | , fAAFlags(aaFlags) {} |
| 3056 | |
| 3057 | /////////////////////////////////////////////////////////////////////////////// |
| 3058 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 3059 | std::unique_ptr<SkCanvas> SkCanvas::MakeRasterDirect(const SkImageInfo& info, void* pixels, |
Mike Reed | 12f7734 | 2017-11-08 11:19:52 -0500 | [diff] [blame] | 3060 | size_t rowBytes, const SkSurfaceProps* props) { |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 3061 | if (!SkSurfaceValidateRasterInfo(info, rowBytes)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3062 | return nullptr; |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3063 | } |
skia.committer@gmail.com | eb849e5 | 2014-03-17 03:02:17 +0000 | [diff] [blame] | 3064 | |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3065 | SkBitmap bitmap; |
| 3066 | if (!bitmap.installPixels(info, pixels, rowBytes)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3067 | return nullptr; |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3068 | } |
Mike Reed | 12f7734 | 2017-11-08 11:19:52 -0500 | [diff] [blame] | 3069 | |
| 3070 | return props ? |
| 3071 | skstd::make_unique<SkCanvas>(bitmap, *props) : |
| 3072 | skstd::make_unique<SkCanvas>(bitmap); |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3073 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3074 | |
| 3075 | /////////////////////////////////////////////////////////////////////////////// |
| 3076 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3077 | SkNoDrawCanvas::SkNoDrawCanvas(int width, int height) |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 3078 | : INHERITED(SkIRect::MakeWH(width, height)) {} |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3079 | |
Florin Malita | 439ace9 | 2016-12-02 12:05:41 -0500 | [diff] [blame] | 3080 | SkNoDrawCanvas::SkNoDrawCanvas(const SkIRect& bounds) |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 3081 | : INHERITED(bounds) {} |
Florin Malita | 439ace9 | 2016-12-02 12:05:41 -0500 | [diff] [blame] | 3082 | |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 3083 | SkNoDrawCanvas::SkNoDrawCanvas(sk_sp<SkBaseDevice> device) |
Herb Derby | 76d69b4 | 2018-03-15 17:34:40 -0400 | [diff] [blame] | 3084 | : INHERITED(device) {} |
| 3085 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3086 | SkCanvas::SaveLayerStrategy SkNoDrawCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) { |
| 3087 | (void)this->INHERITED::getSaveLayerStrategy(rec); |
| 3088 | return kNoLayer_SaveLayerStrategy; |
| 3089 | } |
| 3090 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 3091 | bool SkNoDrawCanvas::onDoSaveBehind(const SkRect*) { |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3095 | /////////////////////////////////////////////////////////////////////////////// |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 3096 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 3097 | static_assert((int)SkRegion::kDifference_Op == (int)kDifference_SkClipOp, ""); |
| 3098 | static_assert((int)SkRegion::kIntersect_Op == (int)kIntersect_SkClipOp, ""); |
| 3099 | static_assert((int)SkRegion::kUnion_Op == (int)kUnion_SkClipOp, ""); |
| 3100 | static_assert((int)SkRegion::kXOR_Op == (int)kXOR_SkClipOp, ""); |
| 3101 | static_assert((int)SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkClipOp, ""); |
| 3102 | static_assert((int)SkRegion::kReplace_Op == (int)kReplace_SkClipOp, ""); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3103 | |
| 3104 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 3105 | |
| 3106 | SkRasterHandleAllocator::Handle SkCanvas::accessTopRasterHandle() const { |
| 3107 | if (fAllocator && fMCRec->fTopLayer->fDevice) { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 3108 | const auto& dev = fMCRec->fTopLayer->fDevice; |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3109 | SkRasterHandleAllocator::Handle handle = dev->getRasterHandle(); |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 3110 | SkIPoint origin = dev->getOrigin(); |
| 3111 | SkMatrix ctm = this->getTotalMatrix(); |
| 3112 | ctm.preTranslate(SkIntToScalar(-origin.x()), SkIntToScalar(-origin.y())); |
| 3113 | |
| 3114 | SkIRect clip = fMCRec->fRasterClip.getBounds(); |
| 3115 | clip.offset(-origin.x(), -origin.y()); |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 3116 | if (!clip.intersect({0, 0, dev->width(), dev->height()})) { |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3117 | clip.setEmpty(); |
| 3118 | } |
| 3119 | |
Michael Ludwig | 915b779 | 2019-10-22 17:40:41 +0000 | [diff] [blame] | 3120 | fAllocator->updateHandle(handle, ctm, clip); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3121 | return handle; |
| 3122 | } |
| 3123 | return nullptr; |
| 3124 | } |
| 3125 | |
| 3126 | static bool install(SkBitmap* bm, const SkImageInfo& info, |
| 3127 | const SkRasterHandleAllocator::Rec& rec) { |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 3128 | return bm->installPixels(info, rec.fPixels, rec.fRowBytes, rec.fReleaseProc, rec.fReleaseCtx); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | SkRasterHandleAllocator::Handle SkRasterHandleAllocator::allocBitmap(const SkImageInfo& info, |
| 3132 | SkBitmap* bm) { |
| 3133 | SkRasterHandleAllocator::Rec rec; |
| 3134 | if (!this->allocHandle(info, &rec) || !install(bm, info, rec)) { |
| 3135 | return nullptr; |
| 3136 | } |
| 3137 | return rec.fHandle; |
| 3138 | } |
| 3139 | |
| 3140 | std::unique_ptr<SkCanvas> |
| 3141 | SkRasterHandleAllocator::MakeCanvas(std::unique_ptr<SkRasterHandleAllocator> alloc, |
| 3142 | const SkImageInfo& info, const Rec* rec) { |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 3143 | if (!alloc || !SkSurfaceValidateRasterInfo(info, rec ? rec->fRowBytes : kIgnoreRowBytesValue)) { |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3144 | return nullptr; |
| 3145 | } |
| 3146 | |
| 3147 | SkBitmap bm; |
| 3148 | Handle hndl; |
| 3149 | |
| 3150 | if (rec) { |
| 3151 | hndl = install(&bm, info, *rec) ? rec->fHandle : nullptr; |
| 3152 | } else { |
| 3153 | hndl = alloc->allocBitmap(info, &bm); |
| 3154 | } |
| 3155 | return hndl ? std::unique_ptr<SkCanvas>(new SkCanvas(bm, std::move(alloc), hndl)) : nullptr; |
| 3156 | } |
Mike Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 3157 | |
| 3158 | /////////////////////////////////////////////////////////////////////////////////////////////////// |