Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 8 | #include "include/utils/SkShadowUtils.h" |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColorFilter.h" |
| 12 | #include "include/core/SkMaskFilter.h" |
| 13 | #include "include/core/SkPath.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/core/SkVertices.h" |
| 16 | #include "include/private/SkColorData.h" |
Brian Salomon | 71fe945 | 2020-03-02 16:59:40 -0500 | [diff] [blame] | 17 | #include "include/private/SkIDChangeListener.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/utils/SkRandom.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/core/SkBlurMask.h" |
Mike Reed | b11e627 | 2020-06-24 16:56:33 -0400 | [diff] [blame^] | 20 | #include "src/core/SkColorFilterBase.h" |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 21 | #include "src/core/SkColorFilterPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/core/SkDevice.h" |
| 23 | #include "src/core/SkDrawShadowInfo.h" |
| 24 | #include "src/core/SkEffectPriv.h" |
Jim Van Verth | ee90eb4 | 2019-04-26 12:07:13 -0400 | [diff] [blame] | 25 | #include "src/core/SkPathPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/core/SkRasterPipeline.h" |
| 27 | #include "src/core/SkResourceCache.h" |
| 28 | #include "src/core/SkTLazy.h" |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 29 | #include "src/core/SkVM.h" |
Mike Reed | ba96256 | 2020-03-12 20:33:21 -0400 | [diff] [blame] | 30 | #include "src/core/SkVerticesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/utils/SkShadowTessellator.h" |
Mike Klein | 79aea6a | 2018-06-11 10:45:26 -0400 | [diff] [blame] | 32 | #include <new> |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 33 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/gpu/effects/generated/GrBlurredEdgeFragmentProcessor.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 35 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 36 | #endif |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Gaussian color filter -- produces a Gaussian ramp based on the color's B value, |
| 40 | * then blends with the color's G value. |
| 41 | * Final result is black with alpha of Gaussian(B)*G. |
| 42 | * The assumption is that the original color's alpha is 1. |
| 43 | */ |
Mike Reed | b11e627 | 2020-06-24 16:56:33 -0400 | [diff] [blame^] | 44 | class SkGaussianColorFilter : public SkColorFilterBase { |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 45 | public: |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 46 | SkGaussianColorFilter() : INHERITED() {} |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 47 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 48 | #if SK_SUPPORT_GPU |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 49 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*, |
| 50 | const GrColorInfo&) const override; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 51 | #endif |
| 52 | |
Mike Klein | 40f9138 | 2019-08-01 21:07:29 +0000 | [diff] [blame] | 53 | protected: |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 54 | void flatten(SkWriteBuffer&) const override {} |
Mike Klein | 40f9138 | 2019-08-01 21:07:29 +0000 | [diff] [blame] | 55 | bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override { |
Mike Reed | 1386b2d | 2019-03-13 21:15:05 -0400 | [diff] [blame] | 56 | rec.fPipeline->append(SkRasterPipeline::gauss_a_to_rgba); |
Mike Reed | 2fdbeae | 2019-03-30 14:27:53 -0400 | [diff] [blame] | 57 | return true; |
Mike Reed | 6533159 | 2017-05-24 16:45:34 -0400 | [diff] [blame] | 58 | } |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 59 | |
| 60 | skvm::Color onProgram(skvm::Builder* p, skvm::Color c, SkColorSpace* dstCS, skvm::Uniforms*, |
| 61 | SkArenaAlloc*) const override { |
| 62 | // x = 1 - x; |
| 63 | // exp(-x * x * 4) - 0.018f; |
| 64 | // ... now approximate with quartic |
| 65 | // |
Mike Reed | f3b9a30 | 2020-04-01 13:18:02 -0400 | [diff] [blame] | 66 | skvm::F32 x = p->splat(-2.26661229133605957031f); |
| 67 | x = c.a * x + 2.89795351028442382812f; |
| 68 | x = c.a * x + 0.21345567703247070312f; |
| 69 | x = c.a * x + 0.15489584207534790039f; |
| 70 | x = c.a * x + 0.00030726194381713867f; |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 71 | return {x, x, x, x}; |
| 72 | } |
| 73 | |
Mike Klein | 40f9138 | 2019-08-01 21:07:29 +0000 | [diff] [blame] | 74 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 75 | SK_FLATTENABLE_HOOKS(SkGaussianColorFilter) |
| 76 | |
Mike Reed | b11e627 | 2020-06-24 16:56:33 -0400 | [diff] [blame^] | 77 | typedef SkColorFilterBase INHERITED; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 80 | sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) { |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 81 | return SkColorFilterPriv::MakeGaussian(); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 84 | #if SK_SUPPORT_GPU |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 85 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 86 | std::unique_ptr<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor( |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 87 | GrRecordingContext*, const GrColorInfo&) const { |
John Stiles | cdc39bd | 2020-06-04 16:14:13 -0400 | [diff] [blame] | 88 | return GrBlurredEdgeFragmentProcessor::Make( |
| 89 | /*inputFP=*/nullptr, GrBlurredEdgeFragmentProcessor::Mode::kGaussian); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 90 | } |
| 91 | #endif |
| 92 | |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 93 | sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() { |
| 94 | return sk_sp<SkColorFilter>(new SkGaussianColorFilter); |
| 95 | } |
| 96 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 97 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 98 | |
| 99 | namespace { |
| 100 | |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 101 | uint64_t resource_cache_shared_id() { |
| 102 | return 0x2020776f64616873llu; // 'shadow ' |
| 103 | } |
| 104 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 105 | /** Factory for an ambient shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 106 | struct AmbientVerticesFactory { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 107 | SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 108 | bool fTransparent; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 109 | SkVector fOffset; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 110 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 111 | bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 112 | if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 113 | return false; |
| 114 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 115 | *translate = that.fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 116 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 117 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 118 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 119 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm, |
| 120 | SkVector* translate) const { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 121 | SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 122 | // pick a canonical place to generate shadow |
| 123 | SkMatrix noTrans(ctm); |
| 124 | if (!ctm.hasPerspective()) { |
| 125 | noTrans[SkMatrix::kMTransX] = 0; |
| 126 | noTrans[SkMatrix::kMTransY] = 0; |
| 127 | } |
| 128 | *translate = fOffset; |
| 129 | return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 133 | /** Factory for an spot shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 134 | struct SpotVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 135 | enum class OccluderType { |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 136 | // The umbra cannot be dropped out because either the occluder is not opaque, |
| 137 | // or the center of the umbra is visible. |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 138 | kTransparent, |
| 139 | // The umbra can be dropped where it is occluded. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 140 | kOpaquePartialUmbra, |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 141 | // It is known that the entire umbra is occluded. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 142 | kOpaqueNoUmbra |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 145 | SkVector fOffset; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 146 | SkPoint fLocalCenter; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 147 | SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed. |
| 148 | SkPoint3 fDevLightPos; |
| 149 | SkScalar fLightRadius; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 150 | OccluderType fOccluderType; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 151 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 152 | bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 153 | if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ || |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 154 | fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | switch (fOccluderType) { |
| 158 | case OccluderType::kTransparent: |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 159 | case OccluderType::kOpaqueNoUmbra: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 160 | // 'this' and 'that' will either both have no umbra removed or both have all the |
| 161 | // umbra removed. |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 162 | *translate = that.fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 163 | return true; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 164 | case OccluderType::kOpaquePartialUmbra: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 165 | // In this case we partially remove the umbra differently for 'this' and 'that' |
| 166 | // if the offsets don't match. |
| 167 | if (fOffset == that.fOffset) { |
| 168 | translate->set(0, 0); |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 173 | SK_ABORT("Uninitialized occluder type?"); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 174 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 175 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 176 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm, |
| 177 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 178 | bool transparent = OccluderType::kTransparent == fOccluderType; |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 179 | SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 180 | if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) { |
| 181 | translate->set(0, 0); |
| 182 | return SkShadowTessellator::MakeSpot(path, ctm, zParams, |
| 183 | fDevLightPos, fLightRadius, transparent); |
| 184 | } else { |
| 185 | // pick a canonical place to generate shadow, with light centered over path |
| 186 | SkMatrix noTrans(ctm); |
| 187 | noTrans[SkMatrix::kMTransX] = 0; |
| 188 | noTrans[SkMatrix::kMTransY] = 0; |
| 189 | SkPoint devCenter(fLocalCenter); |
| 190 | noTrans.mapPoints(&devCenter, 1); |
| 191 | SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ); |
| 192 | *translate = fOffset; |
| 193 | return SkShadowTessellator::MakeSpot(path, noTrans, zParams, |
| 194 | centerLightPos, fLightRadius, transparent); |
| 195 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 196 | } |
| 197 | }; |
| 198 | |
| 199 | /** |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 200 | * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache |
| 201 | * records are immutable this is not itself a Rec. When we need to update it we return this on |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 202 | * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 203 | * a new Rec with an adjusted size for any deletions/additions. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 204 | */ |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 205 | class CachedTessellations : public SkRefCnt { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 206 | public: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 207 | size_t size() const { return fAmbientSet.size() + fSpotSet.size(); } |
| 208 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 209 | sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix, |
| 210 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 211 | return fAmbientSet.find(ambient, matrix, translate); |
| 212 | } |
| 213 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 214 | sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient, |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 215 | const SkMatrix& matrix, SkVector* translate) { |
| 216 | return fAmbientSet.add(devPath, ambient, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 219 | sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix, |
| 220 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 221 | return fSpotSet.find(spot, matrix, translate); |
| 222 | } |
| 223 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 224 | sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot, |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 225 | const SkMatrix& matrix, SkVector* translate) { |
| 226 | return fSpotSet.add(devPath, spot, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | private: |
| 230 | template <typename FACTORY, int MAX_ENTRIES> |
| 231 | class Set { |
| 232 | public: |
| 233 | size_t size() const { return fSize; } |
| 234 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 235 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 236 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 237 | for (int i = 0; i < MAX_ENTRIES; ++i) { |
| 238 | if (fEntries[i].fFactory.isCompatible(factory, translate)) { |
| 239 | const SkMatrix& m = fEntries[i].fMatrix; |
| 240 | if (matrix.hasPerspective() || m.hasPerspective()) { |
| 241 | if (matrix != fEntries[i].fMatrix) { |
| 242 | continue; |
| 243 | } |
| 244 | } else if (matrix.getScaleX() != m.getScaleX() || |
| 245 | matrix.getSkewX() != m.getSkewX() || |
| 246 | matrix.getScaleY() != m.getScaleY() || |
| 247 | matrix.getSkewY() != m.getSkewY()) { |
| 248 | continue; |
| 249 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 250 | return fEntries[i].fVertices; |
| 251 | } |
| 252 | } |
| 253 | return nullptr; |
| 254 | } |
| 255 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 256 | sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix, |
| 257 | SkVector* translate) { |
| 258 | sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 259 | if (!vertices) { |
| 260 | return nullptr; |
| 261 | } |
| 262 | int i; |
| 263 | if (fCount < MAX_ENTRIES) { |
| 264 | i = fCount++; |
| 265 | } else { |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 266 | i = fRandom.nextULessThan(MAX_ENTRIES); |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 267 | fSize -= fEntries[i].fVertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 268 | } |
| 269 | fEntries[i].fFactory = factory; |
| 270 | fEntries[i].fVertices = vertices; |
| 271 | fEntries[i].fMatrix = matrix; |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 272 | fSize += vertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 273 | return vertices; |
| 274 | } |
| 275 | |
| 276 | private: |
| 277 | struct Entry { |
| 278 | FACTORY fFactory; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 279 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 280 | SkMatrix fMatrix; |
| 281 | }; |
| 282 | Entry fEntries[MAX_ENTRIES]; |
| 283 | int fCount = 0; |
| 284 | size_t fSize = 0; |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 285 | SkRandom fRandom; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | Set<AmbientVerticesFactory, 4> fAmbientSet; |
| 289 | Set<SpotVerticesFactory, 4> fSpotSet; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 290 | }; |
| 291 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 292 | /** |
| 293 | * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular |
| 294 | * path. The key represents the path's geometry and not any shadow params. |
| 295 | */ |
| 296 | class CachedTessellationsRec : public SkResourceCache::Rec { |
| 297 | public: |
| 298 | CachedTessellationsRec(const SkResourceCache::Key& key, |
| 299 | sk_sp<CachedTessellations> tessellations) |
| 300 | : fTessellations(std::move(tessellations)) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 301 | fKey.reset(new uint8_t[key.size()]); |
| 302 | memcpy(fKey.get(), &key, key.size()); |
| 303 | } |
| 304 | |
| 305 | const Key& getKey() const override { |
| 306 | return *reinterpret_cast<SkResourceCache::Key*>(fKey.get()); |
| 307 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 308 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 309 | size_t bytesUsed() const override { return fTessellations->size(); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 310 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 311 | const char* getCategory() const override { return "tessellated shadow masks"; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 312 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 313 | sk_sp<CachedTessellations> refTessellations() const { return fTessellations; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 314 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 315 | template <typename FACTORY> |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 316 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 317 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 318 | return fTessellations->find(factory, matrix, translate); |
| 319 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 320 | |
| 321 | private: |
| 322 | std::unique_ptr<uint8_t[]> fKey; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 323 | sk_sp<CachedTessellations> fTessellations; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | /** |
| 327 | * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 328 | * vertices and a translation vector. If the CachedTessellations does not contain a suitable |
| 329 | * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations |
| 330 | * to the caller. The caller will update it and reinsert it back into the cache. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 331 | */ |
| 332 | template <typename FACTORY> |
| 333 | struct FindContext { |
| 334 | FindContext(const SkMatrix* viewMatrix, const FACTORY* factory) |
| 335 | : fViewMatrix(viewMatrix), fFactory(factory) {} |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 336 | const SkMatrix* const fViewMatrix; |
| 337 | // If this is valid after Find is called then we found the vertices and they should be drawn |
| 338 | // with fTranslate applied. |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 339 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 340 | SkVector fTranslate = {0, 0}; |
| 341 | |
| 342 | // If this is valid after Find then the caller should add the vertices to the tessellation set |
| 343 | // and create a new CachedTessellationsRec and insert it into SkResourceCache. |
| 344 | sk_sp<CachedTessellations> fTessellationsOnFailure; |
| 345 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 346 | const FACTORY* fFactory; |
| 347 | }; |
| 348 | |
| 349 | /** |
| 350 | * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of |
| 351 | * the FindContext are used to determine if the vertices are reusable. If so the vertices and |
| 352 | * necessary translation vector are set on the FindContext. |
| 353 | */ |
| 354 | template <typename FACTORY> |
| 355 | bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) { |
| 356 | FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 357 | const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec); |
| 358 | findContext->fVertices = |
| 359 | rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate); |
| 360 | if (findContext->fVertices) { |
| 361 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 362 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 363 | // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been |
| 364 | // manipulated we will add a new Rec. |
| 365 | findContext->fTessellationsOnFailure = rec.refTessellations(); |
| 366 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | class ShadowedPath { |
| 370 | public: |
| 371 | ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix) |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 372 | : fPath(path) |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 373 | , fViewMatrix(viewMatrix) |
| 374 | #if SK_SUPPORT_GPU |
| 375 | , fShapeForKey(*path, GrStyle::SimpleFill()) |
| 376 | #endif |
| 377 | {} |
| 378 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 379 | const SkPath& path() const { return *fPath; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 380 | const SkMatrix& viewMatrix() const { return *fViewMatrix; } |
| 381 | #if SK_SUPPORT_GPU |
| 382 | /** Negative means the vertices should not be cached for this path. */ |
| 383 | int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); } |
| 384 | void writeKey(void* key) const { |
| 385 | fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key)); |
| 386 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 387 | bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 388 | #else |
| 389 | int keyBytes() const { return -1; } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 390 | void writeKey(void* key) const { SK_ABORT("Should never be called"); } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 391 | bool isRRect(SkRRect* rrect) { return false; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 392 | #endif |
| 393 | |
| 394 | private: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 395 | const SkPath* fPath; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 396 | const SkMatrix* fViewMatrix; |
| 397 | #if SK_SUPPORT_GPU |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 398 | GrStyledShape fShapeForKey; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 399 | #endif |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 400 | }; |
| 401 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 402 | // This creates a domain of keys in SkResourceCache used by this file. |
| 403 | static void* kNamespace; |
| 404 | |
Jim Van Verth | ee90eb4 | 2019-04-26 12:07:13 -0400 | [diff] [blame] | 405 | // When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 406 | class ShadowInvalidator : public SkIDChangeListener { |
Jim Van Verth | ee90eb4 | 2019-04-26 12:07:13 -0400 | [diff] [blame] | 407 | public: |
| 408 | ShadowInvalidator(const SkResourceCache::Key& key) { |
| 409 | fKey.reset(new uint8_t[key.size()]); |
| 410 | memcpy(fKey.get(), &key, key.size()); |
| 411 | } |
| 412 | |
| 413 | private: |
| 414 | const SkResourceCache::Key& getKey() const { |
| 415 | return *reinterpret_cast<SkResourceCache::Key*>(fKey.get()); |
| 416 | } |
| 417 | |
| 418 | // always purge |
| 419 | static bool FindVisitor(const SkResourceCache::Rec&, void*) { |
| 420 | return false; |
| 421 | } |
| 422 | |
Brian Salomon | 99a813c | 2020-03-02 12:50:47 -0500 | [diff] [blame] | 423 | void changed() override { |
Jim Van Verth | ee90eb4 | 2019-04-26 12:07:13 -0400 | [diff] [blame] | 424 | SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr); |
| 425 | } |
| 426 | |
| 427 | std::unique_ptr<uint8_t[]> fKey; |
| 428 | }; |
| 429 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 430 | /** |
| 431 | * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless |
| 432 | * they are first found in SkResourceCache. |
| 433 | */ |
| 434 | template <typename FACTORY> |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 435 | bool draw_shadow(const FACTORY& factory, |
| 436 | std::function<void(const SkVertices*, SkBlendMode, const SkPaint&, |
Jim Van Verth | 1aaad02 | 2019-03-14 14:21:51 -0400 | [diff] [blame] | 437 | SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 438 | FindContext<FACTORY> context(&path.viewMatrix(), &factory); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 439 | |
| 440 | SkResourceCache::Key* key = nullptr; |
| 441 | SkAutoSTArray<32 * 4, uint8_t> keyStorage; |
| 442 | int keyDataBytes = path.keyBytes(); |
| 443 | if (keyDataBytes >= 0) { |
| 444 | keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key)); |
| 445 | key = new (keyStorage.begin()) SkResourceCache::Key(); |
| 446 | path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key))); |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 447 | key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 448 | SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 451 | sk_sp<SkVertices> vertices; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 452 | bool foundInCache = SkToBool(context.fVertices); |
| 453 | if (foundInCache) { |
| 454 | vertices = std::move(context.fVertices); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 455 | } else { |
| 456 | // TODO: handle transforming the path as part of the tessellator |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 457 | if (key) { |
| 458 | // Update or initialize a tessellation set and add it to the cache. |
| 459 | sk_sp<CachedTessellations> tessellations; |
| 460 | if (context.fTessellationsOnFailure) { |
| 461 | tessellations = std::move(context.fTessellationsOnFailure); |
| 462 | } else { |
| 463 | tessellations.reset(new CachedTessellations()); |
| 464 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 465 | vertices = tessellations->add(path.path(), factory, path.viewMatrix(), |
| 466 | &context.fTranslate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 467 | if (!vertices) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 468 | return false; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 469 | } |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 470 | auto rec = new CachedTessellationsRec(*key, std::move(tessellations)); |
Jim Van Verth | ee90eb4 | 2019-04-26 12:07:13 -0400 | [diff] [blame] | 471 | SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key)); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 472 | SkResourceCache::Add(rec); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 473 | } else { |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 474 | vertices = factory.makeVertices(path.path(), path.viewMatrix(), |
| 475 | &context.fTranslate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 476 | if (!vertices) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 477 | return false; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 478 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 479 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | SkPaint paint; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 483 | // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of |
| 484 | // that against our 'color' param. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 485 | paint.setColorFilter( |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 486 | SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed( |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 487 | SkColorFilterPriv::MakeGaussian())); |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 488 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 489 | drawProc(vertices.get(), SkBlendMode::kModulate, paint, |
Jim Van Verth | 1aaad02 | 2019-03-14 14:21:51 -0400 | [diff] [blame] | 490 | context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective()); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 491 | |
| 492 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 496 | static bool tilted(const SkPoint3& zPlaneParams) { |
| 497 | return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY); |
| 498 | } |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 499 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 500 | static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) { |
| 501 | SkPoint3 result; |
| 502 | m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX); |
| 503 | result.fZ = pt.fZ; |
| 504 | return result; |
| 505 | } |
| 506 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 507 | void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor, |
| 508 | SkColor* outAmbientColor, SkColor* outSpotColor) { |
| 509 | // For tonal color we only compute color values for the spot shadow. |
| 510 | // The ambient shadow is greyscale only. |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 511 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 512 | // Ambient |
| 513 | *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0); |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 514 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 515 | // Spot |
| 516 | int spotR = SkColorGetR(inSpotColor); |
| 517 | int spotG = SkColorGetG(inSpotColor); |
| 518 | int spotB = SkColorGetB(inSpotColor); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 519 | int max = std::max(std::max(spotR, spotG), spotB); |
| 520 | int min = std::min(std::min(spotR, spotG), spotB); |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 521 | SkScalar luminance = 0.5f*(max + min)/255.f; |
| 522 | SkScalar origA = SkColorGetA(inSpotColor)/255.f; |
| 523 | |
| 524 | // We compute a color alpha value based on the luminance of the color, scaled by an |
| 525 | // adjusted alpha value. We want the following properties to match the UX examples |
| 526 | // (assuming a = 0.25) and to ensure that we have reasonable results when the color |
| 527 | // is black and/or the alpha is 0: |
| 528 | // f(0, a) = 0 |
| 529 | // f(luminance, 0) = 0 |
| 530 | // f(1, 0.25) = .5 |
| 531 | // f(0.5, 0.25) = .4 |
| 532 | // f(1, 1) = 1 |
| 533 | // The following functions match this as closely as possible. |
| 534 | SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA; |
| 535 | SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance; |
| 536 | colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f); |
| 537 | |
| 538 | // Similarly, we set the greyscale alpha based on luminance and alpha so that |
| 539 | // f(0, a) = a |
| 540 | // f(luminance, 0) = 0 |
| 541 | // f(1, 0.25) = 0.15 |
| 542 | SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f); |
| 543 | |
| 544 | // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an |
| 545 | // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a) |
| 546 | // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and |
| 547 | // ignoring edge falloff, this becomes |
| 548 | // |
| 549 | // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb |
| 550 | // |
| 551 | // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and |
| 552 | // set the alpha to (S_a + C_a - S_a*C_a). |
| 553 | SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha); |
| 554 | SkScalar tonalAlpha = colorScale + greyscaleAlpha; |
| 555 | SkScalar unPremulScale = colorScale / tonalAlpha; |
| 556 | *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f, |
| 557 | unPremulScale*spotR, |
| 558 | unPremulScale*spotG, |
| 559 | unPremulScale*spotB); |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 562 | // Draw an offset spot shadow and outlining ambient shadow for the given path. |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 563 | void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 564 | const SkPoint3& devLightPos, SkScalar lightRadius, |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 565 | SkColor ambientColor, SkColor spotColor, |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 566 | uint32_t flags) { |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 567 | SkMatrix inverse; |
| 568 | if (!canvas->getTotalMatrix().invert(&inverse)) { |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 569 | return; |
| 570 | } |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 571 | SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY); |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 572 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 573 | SkDrawShadowRec rec; |
| 574 | rec.fZPlaneParams = zPlaneParams; |
| 575 | rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ }; |
| 576 | rec.fLightRadius = lightRadius; |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 577 | rec.fAmbientColor = ambientColor; |
| 578 | rec.fSpotColor = spotColor; |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 579 | rec.fFlags = flags; |
| 580 | |
| 581 | canvas->private_draw_shadow_rec(path, rec); |
| 582 | } |
| 583 | |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 584 | static bool validate_rec(const SkDrawShadowRec& rec) { |
| 585 | return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() && |
| 586 | SkScalarIsFinite(rec.fLightRadius); |
| 587 | } |
| 588 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 589 | void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) { |
| 590 | auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint, |
Jim Van Verth | 1aaad02 | 2019-03-14 14:21:51 -0400 | [diff] [blame] | 591 | SkScalar tx, SkScalar ty, bool hasPerspective) { |
Brian Osman | 8cbedf9 | 2020-03-31 10:38:31 -0400 | [diff] [blame] | 592 | if (vertices->priv().vertexCount()) { |
Jim Van Verth | 1aaad02 | 2019-03-14 14:21:51 -0400 | [diff] [blame] | 593 | // For perspective shadows we've already computed the shadow in world space, |
| 594 | // and we can't translate it without changing it. Otherwise we concat the |
| 595 | // change in translation from the cached version. |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 596 | SkAutoDeviceTransformRestore adr( |
| 597 | this, |
| 598 | hasPerspective ? SkMatrix::I() |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 599 | : this->localToDevice() * SkMatrix::Translate(tx, ty)); |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 600 | this->drawVertices(vertices, mode, paint); |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 601 | } |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 602 | }; |
| 603 | |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 604 | if (!validate_rec(rec)) { |
| 605 | return; |
| 606 | } |
| 607 | |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 608 | SkMatrix viewMatrix = this->localToDevice(); |
| 609 | SkAutoDeviceTransformRestore adr(this, SkMatrix::I()); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 610 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 611 | ShadowedPath shadowedPath(&path, &viewMatrix); |
| 612 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 613 | bool tiltZPlane = tilted(rec.fZPlaneParams); |
| 614 | bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 615 | bool uncached = tiltZPlane || path.isVolatile(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 616 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 617 | SkPoint3 zPlaneParams = rec.fZPlaneParams; |
| 618 | SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos); |
| 619 | float lightRadius = rec.fLightRadius; |
| 620 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 621 | if (SkColorGetA(rec.fAmbientColor) > 0) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 622 | bool success = false; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 623 | if (uncached) { |
| 624 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix, |
| 625 | zPlaneParams, |
| 626 | transparent); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 627 | if (vertices) { |
| 628 | SkPaint paint; |
| 629 | // Run the vertex color through a GaussianColorFilter and then modulate the |
| 630 | // grayscale result of that against our 'color' param. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 631 | paint.setColorFilter( |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 632 | SkColorFilters::Blend(rec.fAmbientColor, |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 633 | SkBlendMode::kModulate)->makeComposed( |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 634 | SkColorFilterPriv::MakeGaussian())); |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 635 | this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 636 | success = true; |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 637 | } |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (!success) { |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 641 | AmbientVerticesFactory factory; |
| 642 | factory.fOccluderHeight = zPlaneParams.fZ; |
| 643 | factory.fTransparent = transparent; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 644 | if (viewMatrix.hasPerspective()) { |
| 645 | factory.fOffset.set(0, 0); |
| 646 | } else { |
| 647 | factory.fOffset.fX = viewMatrix.getTranslateX(); |
| 648 | factory.fOffset.fY = viewMatrix.getTranslateY(); |
| 649 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 650 | |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 651 | if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) { |
| 652 | // Pretransform the path to avoid transforming the stroke, below. |
| 653 | SkPath devSpacePath; |
| 654 | path.transform(viewMatrix, &devSpacePath); |
Robert Phillips | ed3dbf4 | 2019-03-18 12:20:15 -0400 | [diff] [blame] | 655 | devSpacePath.setIsVolatile(true); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 656 | |
| 657 | // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of |
Jim Van Verth | 3a039d5 | 2018-09-14 17:14:47 -0400 | [diff] [blame] | 658 | // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a'). |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 659 | // |
| 660 | // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f') |
| 661 | // can be calculated by interpolating: |
| 662 | // |
| 663 | // original edge outer edge |
| 664 | // | |<---------- r ------>| |
| 665 | // |<------|--- f -------------->| |
| 666 | // | | | |
| 667 | // alpha = 1 alpha = a alpha = 0 |
| 668 | // |
| 669 | // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2. |
| 670 | // |
| 671 | // We now need to outset the path to place the new edge in the center of the |
| 672 | // blur region: |
| 673 | // |
| 674 | // original new |
| 675 | // | |<------|--- r ------>| |
| 676 | // |<------|--- f -|------------>| |
| 677 | // | |<- o ->|<--- f/2 --->| |
| 678 | // |
| 679 | // r = o + f/2, so o = r - f/2 |
| 680 | // |
| 681 | // We outset by using the stroker, so the strokeWidth is o/2. |
| 682 | // |
| 683 | SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ); |
| 684 | SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ); |
| 685 | SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA; |
| 686 | SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius); |
| 687 | |
| 688 | // Now draw with blur |
| 689 | SkPaint paint; |
| 690 | paint.setColor(rec.fAmbientColor); |
| 691 | paint.setStrokeWidth(strokeWidth); |
| 692 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
Mike Reed | 8e03f69 | 2018-03-09 16:18:56 -0500 | [diff] [blame] | 693 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 694 | bool respectCTM = false; |
| 695 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM)); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 696 | this->drawPath(devSpacePath, paint); |
| 697 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 698 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 699 | } |
| 700 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 701 | if (SkColorGetA(rec.fSpotColor) > 0) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 702 | bool success = false; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 703 | if (uncached) { |
| 704 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix, |
| 705 | zPlaneParams, |
| 706 | devLightPos, lightRadius, |
| 707 | transparent); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 708 | if (vertices) { |
| 709 | SkPaint paint; |
| 710 | // Run the vertex color through a GaussianColorFilter and then modulate the |
| 711 | // grayscale result of that against our 'color' param. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 712 | paint.setColorFilter( |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 713 | SkColorFilters::Blend(rec.fSpotColor, |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 714 | SkBlendMode::kModulate)->makeComposed( |
Mike Reed | f36b37f | 2020-03-27 15:11:10 -0400 | [diff] [blame] | 715 | SkColorFilterPriv::MakeGaussian())); |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 716 | this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 717 | success = true; |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 718 | } |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 719 | } |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 720 | |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 721 | if (!success) { |
| 722 | SpotVerticesFactory factory; |
| 723 | factory.fOccluderHeight = zPlaneParams.fZ; |
| 724 | factory.fDevLightPos = devLightPos; |
| 725 | factory.fLightRadius = lightRadius; |
| 726 | |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 727 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 728 | factory.fLocalCenter = center; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 729 | viewMatrix.mapPoints(¢er, 1); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 730 | SkScalar radius, scale; |
| 731 | SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX, |
| 732 | devLightPos.fY - center.fY, devLightPos.fZ, |
| 733 | lightRadius, &radius, &scale, &factory.fOffset); |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 734 | SkRect devBounds; |
| 735 | viewMatrix.mapRect(&devBounds, path.getBounds()); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 736 | if (transparent || |
| 737 | SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() || |
| 738 | SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) { |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 739 | // if the translation of the shadow is big enough we're going to end up |
| 740 | // filling the entire umbra, so we can treat these as all the same |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 741 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 742 | } else if (factory.fOffset.length()*scale + scale < radius) { |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 743 | // if we don't translate more than the blur distance, can assume umbra is covered |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 744 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 745 | } else if (path.isConvex()) { |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 746 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 747 | } else { |
| 748 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 749 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 750 | // need to add this after we classify the shadow |
| 751 | factory.fOffset.fX += viewMatrix.getTranslateX(); |
| 752 | factory.fOffset.fY += viewMatrix.getTranslateY(); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 753 | |
| 754 | SkColor color = rec.fSpotColor; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 755 | #ifdef DEBUG_SHADOW_CHECKS |
| 756 | switch (factory.fOccluderType) { |
| 757 | case SpotVerticesFactory::OccluderType::kTransparent: |
| 758 | color = 0xFFD2B48C; // tan for transparent |
| 759 | break; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 760 | case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra: |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 761 | color = 0xFFFFA500; // orange for opaque |
| 762 | break; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 763 | case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra: |
| 764 | color = 0xFFE5E500; // corn yellow for covered |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | #endif |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 768 | if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) { |
| 769 | // draw with blur |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 770 | SkMatrix shadowMatrix; |
Jim Van Verth | 3a039d5 | 2018-09-14 17:14:47 -0400 | [diff] [blame] | 771 | if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius, |
| 772 | viewMatrix, zPlaneParams, |
| 773 | path.getBounds(), |
| 774 | &shadowMatrix, &radius)) { |
| 775 | return; |
| 776 | } |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 777 | SkAutoDeviceTransformRestore adr(this, shadowMatrix); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 778 | |
| 779 | SkPaint paint; |
| 780 | paint.setColor(rec.fSpotColor); |
Mike Reed | 8e03f69 | 2018-03-09 16:18:56 -0500 | [diff] [blame] | 781 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 782 | bool respectCTM = false; |
| 783 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM)); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 784 | this->drawPath(path, paint); |
| 785 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 786 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 787 | } |
| 788 | } |