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