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