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