blob: 88ee681da7806b82d8ee2b681c5b46778fa29f9a [file] [log] [blame]
Jim Van Verth43475ad2017-01-13 14:37:37 -05001/*
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 Salomon71fe9452020-03-02 16:59:40 -05008#include "include/utils/SkShadowUtils.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 Salomon71fe9452020-03-02 16:59:40 -050017#include "include/private/SkIDChangeListener.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050018#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkBlurMask.h"
Mike Reedb11e6272020-06-24 16:56:33 -040021#include "src/core/SkColorFilterBase.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040022#include "src/core/SkColorFilterPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkDevice.h"
24#include "src/core/SkDrawShadowInfo.h"
25#include "src/core/SkEffectPriv.h"
Jim Van Verthee90eb42019-04-26 12:07:13 -040026#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/core/SkRasterPipeline.h"
28#include "src/core/SkResourceCache.h"
29#include "src/core/SkTLazy.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040030#include "src/core/SkVM.h"
Mike Reedba962562020-03-12 20:33:21 -040031#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/utils/SkShadowTessellator.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040033#include <new>
Brian Salomon5e689522017-02-01 12:07:17 -050034#if SK_SUPPORT_GPU
Brian Osman9a4c9652021-05-20 16:05:19 -040035#include "src/gpu/effects/GrSkSLFP.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000036#include "src/gpu/geometry/GrStyledShape.h"
Mike Reed4204da22017-05-17 08:53:36 -040037#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050038
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 Reedb11e6272020-06-24 16:56:33 -040045class SkGaussianColorFilter : public SkColorFilterBase {
Jim Van Verthefe3ded2017-01-30 13:11:45 -050046public:
Mike Reedf36b37f2020-03-27 15:11:10 -040047 SkGaussianColorFilter() : INHERITED() {}
Jim Van Verthefe3ded2017-01-30 13:11:45 -050048
Jim Van Verthefe3ded2017-01-30 13:11:45 -050049#if SK_SUPPORT_GPU
John Stiles43206642020-06-29 12:03:26 -040050 GrFPResult asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
51 GrRecordingContext*, const GrColorInfo&) const override;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050052#endif
53
Mike Klein40f91382019-08-01 21:07:29 +000054protected:
Jim Van Verthefe3ded2017-01-30 13:11:45 -050055 void flatten(SkWriteBuffer&) const override {}
Mike Klein40f91382019-08-01 21:07:29 +000056 bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
Mike Reed1386b2d2019-03-13 21:15:05 -040057 rec.fPipeline->append(SkRasterPipeline::gauss_a_to_rgba);
Mike Reed2fdbeae2019-03-30 14:27:53 -040058 return true;
Mike Reed65331592017-05-24 16:45:34 -040059 }
Mike Reedf36b37f2020-03-27 15:11:10 -040060
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 Reedf3b9a302020-04-01 13:18:02 -040067 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 Reedf36b37f2020-03-27 15:11:10 -040072 return {x, x, x, x};
73 }
74
Mike Klein40f91382019-08-01 21:07:29 +000075private:
Mike Klein4fee3232018-10-18 17:27:16 -040076 SK_FLATTENABLE_HOOKS(SkGaussianColorFilter)
77
John Stiles7571f9e2020-09-02 22:42:33 -040078 using INHERITED = SkColorFilterBase;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050079};
80
Jim Van Verthefe3ded2017-01-30 13:11:45 -050081sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
Mike Reedf36b37f2020-03-27 15:11:10 -040082 return SkColorFilterPriv::MakeGaussian();
Jim Van Verthefe3ded2017-01-30 13:11:45 -050083}
84
Jim Van Verthefe3ded2017-01-30 13:11:45 -050085#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050086
John Stiles43206642020-06-29 12:03:26 -040087GrFPResult SkGaussianColorFilter::asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
88 GrRecordingContext*,
89 const GrColorInfo&) const {
Brian Osmane384c062021-06-04 14:09:51 -040090 static auto effect = SkRuntimeEffect::MakeForColorFilter(SkString(R"(
Brian Osman9a4c9652021-05-20 16:05:19 -040091 half4 main(half4 inColor) {
92 half factor = 1 - inColor.a;
93 factor = exp(-factor * factor * 4) - 0.018;
94 return half4(factor);
95 }
Brian Osmane384c062021-06-04 14:09:51 -040096 )")).effect;
97 auto fp = GrSkSLFP::Make(effect, "gaussian_fp");
98 return GrFPSuccess(GrFragmentProcessor::Compose(std::move(fp), std::move(inputFP)));
Jim Van Verthefe3ded2017-01-30 13:11:45 -050099}
100#endif
101
Mike Reedf36b37f2020-03-27 15:11:10 -0400102sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
103 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
104}
105
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500106///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -0500107
108namespace {
109
Brian Salomonbc9956d2017-02-22 13:49:09 -0500110uint64_t resource_cache_shared_id() {
111 return 0x2020776f64616873llu; // 'shadow '
112}
113
Brian Salomond1ac9822017-02-03 14:25:02 -0500114/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500115struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400116 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500117 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400118 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500119
Brian Salomond1ac9822017-02-03 14:25:02 -0500120 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400121 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500122 return false;
123 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400124 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500125 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500126 }
Brian Salomon5e689522017-02-01 12:07:17 -0500127
Jim Van Verth8793e382017-05-22 15:52:21 -0400128 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
129 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400130 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400131 // pick a canonical place to generate shadow
132 SkMatrix noTrans(ctm);
133 if (!ctm.hasPerspective()) {
134 noTrans[SkMatrix::kMTransX] = 0;
135 noTrans[SkMatrix::kMTransY] = 0;
136 }
137 *translate = fOffset;
138 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500139 }
140};
141
Brian Salomond1ac9822017-02-03 14:25:02 -0500142/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500143struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500144 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400145 // The umbra cannot be dropped out because either the occluder is not opaque,
146 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500147 kTransparent,
148 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400149 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500150 // It is known that the entire umbra is occluded.
Jim Van Verth63f03542020-12-16 11:56:11 -0500151 kOpaqueNoUmbra,
152 // The light is directional
153 kDirectional
Brian Salomond1ac9822017-02-03 14:25:02 -0500154 };
155
Brian Salomon5e689522017-02-01 12:07:17 -0500156 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400157 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400158 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
159 SkPoint3 fDevLightPos;
160 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500161 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500162
Brian Salomond1ac9822017-02-03 14:25:02 -0500163 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400164 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400165 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500166 return false;
167 }
168 switch (fOccluderType) {
169 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400170 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500171 // 'this' and 'that' will either both have no umbra removed or both have all the
172 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400173 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500174 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400175 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500176 // In this case we partially remove the umbra differently for 'this' and 'that'
177 // if the offsets don't match.
178 if (fOffset == that.fOffset) {
179 translate->set(0, 0);
180 return true;
181 }
182 return false;
Jim Van Verth63f03542020-12-16 11:56:11 -0500183 case OccluderType::kDirectional:
184 *translate = that.fOffset - fOffset;
185 return true;
Brian Salomond1ac9822017-02-03 14:25:02 -0500186 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400187 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500188 }
Brian Salomon5e689522017-02-01 12:07:17 -0500189
Jim Van Verth8793e382017-05-22 15:52:21 -0400190 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
191 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500192 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verth63f03542020-12-16 11:56:11 -0500193 bool directional = OccluderType::kDirectional == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400194 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth63f03542020-12-16 11:56:11 -0500195 if (directional) {
Jim Van Verth8793e382017-05-22 15:52:21 -0400196 translate->set(0, 0);
Jim Van Verth63f03542020-12-16 11:56:11 -0500197 return SkShadowTessellator::MakeSpot(path, ctm, zParams, fDevLightPos, fLightRadius,
198 transparent, true);
199 } else if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
200 translate->set(0, 0);
201 return SkShadowTessellator::MakeSpot(path, ctm, zParams, fDevLightPos, fLightRadius,
202 transparent, false);
Jim Van Verth8793e382017-05-22 15:52:21 -0400203 } else {
204 // pick a canonical place to generate shadow, with light centered over path
205 SkMatrix noTrans(ctm);
206 noTrans[SkMatrix::kMTransX] = 0;
207 noTrans[SkMatrix::kMTransY] = 0;
208 SkPoint devCenter(fLocalCenter);
209 noTrans.mapPoints(&devCenter, 1);
210 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
211 *translate = fOffset;
212 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
Jim Van Verth63f03542020-12-16 11:56:11 -0500213 centerLightPos, fLightRadius, transparent, false);
Jim Van Verth8793e382017-05-22 15:52:21 -0400214 }
Brian Salomon5e689522017-02-01 12:07:17 -0500215 }
216};
217
218/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500219 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
220 * records are immutable this is not itself a Rec. When we need to update it we return this on
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400221 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500222 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500223 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500224class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500225public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500226 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
227
Brian Salomonaff27a22017-02-06 15:47:44 -0500228 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
229 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500230 return fAmbientSet.find(ambient, matrix, translate);
231 }
232
Brian Salomonaff27a22017-02-06 15:47:44 -0500233 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400234 const SkMatrix& matrix, SkVector* translate) {
235 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500236 }
237
Brian Salomonaff27a22017-02-06 15:47:44 -0500238 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
239 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500240 return fSpotSet.find(spot, matrix, translate);
241 }
242
Brian Salomonaff27a22017-02-06 15:47:44 -0500243 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400244 const SkMatrix& matrix, SkVector* translate) {
245 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500246 }
247
248private:
249 template <typename FACTORY, int MAX_ENTRIES>
250 class Set {
251 public:
252 size_t size() const { return fSize; }
253
Brian Salomonaff27a22017-02-06 15:47:44 -0500254 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
255 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500256 for (int i = 0; i < MAX_ENTRIES; ++i) {
257 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
258 const SkMatrix& m = fEntries[i].fMatrix;
259 if (matrix.hasPerspective() || m.hasPerspective()) {
260 if (matrix != fEntries[i].fMatrix) {
261 continue;
262 }
263 } else if (matrix.getScaleX() != m.getScaleX() ||
264 matrix.getSkewX() != m.getSkewX() ||
265 matrix.getScaleY() != m.getScaleY() ||
266 matrix.getSkewY() != m.getSkewY()) {
267 continue;
268 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500269 return fEntries[i].fVertices;
270 }
271 }
272 return nullptr;
273 }
274
Jim Van Verth8793e382017-05-22 15:52:21 -0400275 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
276 SkVector* translate) {
277 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500278 if (!vertices) {
279 return nullptr;
280 }
281 int i;
282 if (fCount < MAX_ENTRIES) {
283 i = fCount++;
284 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400285 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400286 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500287 }
288 fEntries[i].fFactory = factory;
289 fEntries[i].fVertices = vertices;
290 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400291 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500292 return vertices;
293 }
294
295 private:
296 struct Entry {
297 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500298 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500299 SkMatrix fMatrix;
300 };
301 Entry fEntries[MAX_ENTRIES];
302 int fCount = 0;
303 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400304 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500305 };
306
307 Set<AmbientVerticesFactory, 4> fAmbientSet;
308 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500309};
310
Brian Salomond1ac9822017-02-03 14:25:02 -0500311/**
312 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
313 * path. The key represents the path's geometry and not any shadow params.
314 */
315class CachedTessellationsRec : public SkResourceCache::Rec {
316public:
317 CachedTessellationsRec(const SkResourceCache::Key& key,
318 sk_sp<CachedTessellations> tessellations)
319 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500320 fKey.reset(new uint8_t[key.size()]);
321 memcpy(fKey.get(), &key, key.size());
322 }
323
324 const Key& getKey() const override {
325 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
326 }
Brian Salomon5e689522017-02-01 12:07:17 -0500327
Brian Salomond1ac9822017-02-03 14:25:02 -0500328 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500329
Brian Salomond1ac9822017-02-03 14:25:02 -0500330 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500331
Brian Salomond1ac9822017-02-03 14:25:02 -0500332 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500333
Brian Salomond1ac9822017-02-03 14:25:02 -0500334 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500335 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
336 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500337 return fTessellations->find(factory, matrix, translate);
338 }
Brian Salomon5e689522017-02-01 12:07:17 -0500339
340private:
341 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500342 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500343};
344
345/**
346 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500347 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
348 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
349 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500350 */
351template <typename FACTORY>
352struct FindContext {
353 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
354 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500355 const SkMatrix* const fViewMatrix;
356 // If this is valid after Find is called then we found the vertices and they should be drawn
357 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500358 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500359 SkVector fTranslate = {0, 0};
360
361 // If this is valid after Find then the caller should add the vertices to the tessellation set
362 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
363 sk_sp<CachedTessellations> fTessellationsOnFailure;
364
Brian Salomon5e689522017-02-01 12:07:17 -0500365 const FACTORY* fFactory;
366};
367
368/**
369 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
370 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
371 * necessary translation vector are set on the FindContext.
372 */
373template <typename FACTORY>
374bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
375 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500376 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
377 findContext->fVertices =
378 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
379 if (findContext->fVertices) {
380 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500381 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500382 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
383 // manipulated we will add a new Rec.
384 findContext->fTessellationsOnFailure = rec.refTessellations();
385 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500386}
387
388class ShadowedPath {
389public:
390 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500391 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500392 , fViewMatrix(viewMatrix)
393#if SK_SUPPORT_GPU
394 , fShapeForKey(*path, GrStyle::SimpleFill())
395#endif
396 {}
397
Jim Van Vertha84898d2017-02-06 13:38:23 -0500398 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500399 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
400#if SK_SUPPORT_GPU
401 /** Negative means the vertices should not be cached for this path. */
402 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
403 void writeKey(void* key) const {
404 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
405 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500406 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500407#else
408 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400409 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500410 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500411#endif
412
413private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500414 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500415 const SkMatrix* fViewMatrix;
416#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000417 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500418#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500419};
420
Brian Salomond1ac9822017-02-03 14:25:02 -0500421// This creates a domain of keys in SkResourceCache used by this file.
422static void* kNamespace;
423
Jim Van Verthee90eb42019-04-26 12:07:13 -0400424// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500425class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400426public:
427 ShadowInvalidator(const SkResourceCache::Key& key) {
428 fKey.reset(new uint8_t[key.size()]);
429 memcpy(fKey.get(), &key, key.size());
430 }
431
432private:
433 const SkResourceCache::Key& getKey() const {
434 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
435 }
436
437 // always purge
438 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
439 return false;
440 }
441
Brian Salomon99a813c2020-03-02 12:50:47 -0500442 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400443 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
444 }
445
446 std::unique_ptr<uint8_t[]> fKey;
447};
448
Brian Salomon5e689522017-02-01 12:07:17 -0500449/**
450 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
451 * they are first found in SkResourceCache.
452 */
453template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500454bool draw_shadow(const FACTORY& factory,
455 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400456 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500457 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500458
459 SkResourceCache::Key* key = nullptr;
460 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
461 int keyDataBytes = path.keyBytes();
462 if (keyDataBytes >= 0) {
463 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
464 key = new (keyStorage.begin()) SkResourceCache::Key();
465 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500466 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400467 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500468 }
469
Brian Salomonaff27a22017-02-06 15:47:44 -0500470 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500471 bool foundInCache = SkToBool(context.fVertices);
472 if (foundInCache) {
473 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500474 } else {
475 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500476 if (key) {
477 // Update or initialize a tessellation set and add it to the cache.
478 sk_sp<CachedTessellations> tessellations;
479 if (context.fTessellationsOnFailure) {
480 tessellations = std::move(context.fTessellationsOnFailure);
481 } else {
482 tessellations.reset(new CachedTessellations());
483 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400484 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
485 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500486 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500487 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500488 }
Brian Salomon804e0912017-02-23 09:34:03 -0500489 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400490 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400491 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500492 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400493 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
494 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500495 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500496 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500497 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500498 }
Brian Salomon5e689522017-02-01 12:07:17 -0500499 }
500
501 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500502 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
503 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500504 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400505 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400506 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400507
Jim Van Verth8793e382017-05-22 15:52:21 -0400508 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400509 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500510
511 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500512}
John Stilesa6841be2020-08-06 14:11:56 -0400513} // namespace
Brian Salomon5e689522017-02-01 12:07:17 -0500514
Mike Reed4204da22017-05-17 08:53:36 -0400515static bool tilted(const SkPoint3& zPlaneParams) {
516 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
517}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400518
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500519void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
520 SkColor* outAmbientColor, SkColor* outSpotColor) {
521 // For tonal color we only compute color values for the spot shadow.
522 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400523
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500524 // Ambient
525 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400526
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500527 // Spot
528 int spotR = SkColorGetR(inSpotColor);
529 int spotG = SkColorGetG(inSpotColor);
530 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500531 int max = std::max(std::max(spotR, spotG), spotB);
532 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500533 SkScalar luminance = 0.5f*(max + min)/255.f;
534 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
535
536 // We compute a color alpha value based on the luminance of the color, scaled by an
537 // adjusted alpha value. We want the following properties to match the UX examples
538 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
539 // is black and/or the alpha is 0:
540 // f(0, a) = 0
541 // f(luminance, 0) = 0
542 // f(1, 0.25) = .5
543 // f(0.5, 0.25) = .4
544 // f(1, 1) = 1
545 // The following functions match this as closely as possible.
546 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
547 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
548 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
549
550 // Similarly, we set the greyscale alpha based on luminance and alpha so that
551 // f(0, a) = a
552 // f(luminance, 0) = 0
553 // f(1, 0.25) = 0.15
554 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
555
556 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
557 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
558 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
559 // ignoring edge falloff, this becomes
560 //
561 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
562 //
563 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
564 // set the alpha to (S_a + C_a - S_a*C_a).
565 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
566 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
567 SkScalar unPremulScale = colorScale / tonalAlpha;
568 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
569 unPremulScale*spotR,
570 unPremulScale*spotG,
571 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400572}
573
Jim Van Verthea4aa392021-01-11 11:01:09 -0500574static bool fill_shadow_rec(const SkPath& path, const SkPoint3& zPlaneParams,
575 const SkPoint3& lightPos, SkScalar lightRadius,
576 SkColor ambientColor, SkColor spotColor,
577 uint32_t flags, const SkMatrix& ctm, SkDrawShadowRec* rec) {
Jim Van Verth63f03542020-12-16 11:56:11 -0500578 SkPoint pt = { lightPos.fX, lightPos.fY };
579 if (!SkToBool(flags & kDirectionalLight_ShadowFlag)) {
580 // If light position is in device space, need to transform to local space
581 // before applying to SkCanvas.
582 SkMatrix inverse;
Jim Van Verthea4aa392021-01-11 11:01:09 -0500583 if (!ctm.invert(&inverse)) {
584 return false;
Jim Van Verth63f03542020-12-16 11:56:11 -0500585 }
586 inverse.mapPoints(&pt, 1);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500587 }
588
Jim Van Verthea4aa392021-01-11 11:01:09 -0500589 rec->fZPlaneParams = zPlaneParams;
590 rec->fLightPos = { pt.fX, pt.fY, lightPos.fZ };
591 rec->fLightRadius = lightRadius;
592 rec->fAmbientColor = ambientColor;
593 rec->fSpotColor = spotColor;
594 rec->fFlags = flags;
595
596 return true;
597}
598
599// Draw an offset spot shadow and outlining ambient shadow for the given path.
600void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
601 const SkPoint3& lightPos, SkScalar lightRadius,
602 SkColor ambientColor, SkColor spotColor,
603 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400604 SkDrawShadowRec rec;
Jim Van Verthea4aa392021-01-11 11:01:09 -0500605 if (!fill_shadow_rec(path, zPlaneParams, lightPos, lightRadius, ambientColor, spotColor,
606 flags, canvas->getTotalMatrix(), &rec)) {
607 return;
608 }
Mike Reed4204da22017-05-17 08:53:36 -0400609
610 canvas->private_draw_shadow_rec(path, rec);
611}
612
Jim Van Verthea4aa392021-01-11 11:01:09 -0500613bool SkShadowUtils::GetLocalBounds(const SkMatrix& ctm, const SkPath& path,
614 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
615 SkScalar lightRadius, uint32_t flags, SkRect* bounds) {
616 SkDrawShadowRec rec;
617 if (!fill_shadow_rec(path, zPlaneParams, lightPos, lightRadius, SK_ColorBLACK, SK_ColorBLACK,
618 flags, ctm, &rec)) {
619 return false;
620 }
621
622 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, bounds);
623
624 return true;
625}
626
627//////////////////////////////////////////////////////////////////////////////////////////////
628
Jim Van Vertha947e292018-02-26 13:54:34 -0500629static bool validate_rec(const SkDrawShadowRec& rec) {
630 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
631 SkScalarIsFinite(rec.fLightRadius);
632}
633
Mike Reed4204da22017-05-17 08:53:36 -0400634void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
635 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400636 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400637 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400638 // For perspective shadows we've already computed the shadow in world space,
639 // and we can't translate it without changing it. Otherwise we concat the
640 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400641 SkAutoDeviceTransformRestore adr(
642 this,
643 hasPerspective ? SkMatrix::I()
Mike Reed1f607332020-05-21 12:11:27 -0400644 : this->localToDevice() * SkMatrix::Translate(tx, ty));
Mike Reed5caf9352020-03-02 14:57:09 -0500645 this->drawVertices(vertices, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400646 }
Mike Reed4204da22017-05-17 08:53:36 -0400647 };
648
Jim Van Vertha947e292018-02-26 13:54:34 -0500649 if (!validate_rec(rec)) {
650 return;
651 }
652
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400653 SkMatrix viewMatrix = this->localToDevice();
654 SkAutoDeviceTransformRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500655
Brian Salomon5e689522017-02-01 12:07:17 -0500656 ShadowedPath shadowedPath(&path, &viewMatrix);
657
Mike Reed4204da22017-05-17 08:53:36 -0400658 bool tiltZPlane = tilted(rec.fZPlaneParams);
659 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth63f03542020-12-16 11:56:11 -0500660 bool directional = SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400661 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500662
Mike Reed4204da22017-05-17 08:53:36 -0400663 SkPoint3 zPlaneParams = rec.fZPlaneParams;
Jim Van Verth63f03542020-12-16 11:56:11 -0500664 SkPoint3 devLightPos = rec.fLightPos;
665 if (directional) {
Jim Van Vertha8682202020-12-17 10:18:16 -0500666 devLightPos.normalize();
Jim Van Verth63f03542020-12-16 11:56:11 -0500667 } else {
668 viewMatrix.mapPoints((SkPoint*)&devLightPos.fX, 1);
669 }
Mike Reed4204da22017-05-17 08:53:36 -0400670 float lightRadius = rec.fLightRadius;
671
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500672 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500673 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400674 if (uncached) {
675 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
676 zPlaneParams,
677 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400678 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 Reed19d7bd62018-02-19 14:10:57 -0500682 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400683 SkColorFilters::Blend(rec.fAmbientColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500684 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400685 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500686 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500687 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400688 }
Jim Van Verth22526362018-02-28 14:51:19 -0500689 }
690
691 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400692 AmbientVerticesFactory factory;
693 factory.fOccluderHeight = zPlaneParams.fZ;
694 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400695 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 Verth37c5a962017-05-10 14:13:24 -0400701
Jim Van Verth22526362018-02-28 14:51:19 -0500702 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 Phillipsed3dbf42019-03-18 12:20:15 -0400706 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500707
708 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400709 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500710 //
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 Reed8e03f692018-03-09 16:18:56 -0500744 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400745 bool respectCTM = false;
746 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500747 this->drawPath(devSpacePath, paint);
748 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500749 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400750 }
751
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500752 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500753 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400754 if (uncached) {
755 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
756 zPlaneParams,
757 devLightPos, lightRadius,
Jim Van Verth63f03542020-12-16 11:56:11 -0500758 transparent,
759 directional);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400760 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 Reed19d7bd62018-02-19 14:10:57 -0500764 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400765 SkColorFilters::Blend(rec.fSpotColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500766 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400767 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500768 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500769 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400770 }
Jim Van Verth22526362018-02-28 14:51:19 -0500771 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400772
Jim Van Verth22526362018-02-28 14:51:19 -0500773 if (!success) {
774 SpotVerticesFactory factory;
775 factory.fOccluderHeight = zPlaneParams.fZ;
776 factory.fDevLightPos = devLightPos;
777 factory.fLightRadius = lightRadius;
778
Jim Van Verth37c5a962017-05-10 14:13:24 -0400779 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400780 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400781 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500782 SkScalar radius, scale;
Jim Van Verth63f03542020-12-16 11:56:11 -0500783 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 Vertha783c362017-05-11 17:05:28 -0400794 SkRect devBounds;
795 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth63f03542020-12-16 11:56:11 -0500796 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 Verth78c8f302017-05-15 10:44:22 -0400801 // 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 Verth8793e382017-05-22 15:52:21 -0400803 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400804 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400805 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400806 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400807 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400808 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400809 } else {
810 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400811 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400812 // need to add this after we classify the shadow
813 factory.fOffset.fX += viewMatrix.getTranslateX();
814 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500815
816 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400817#ifdef DEBUG_SHADOW_CHECKS
818 switch (factory.fOccluderType) {
819 case SpotVerticesFactory::OccluderType::kTransparent:
820 color = 0xFFD2B48C; // tan for transparent
821 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400822 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400823 color = 0xFFFFA500; // orange for opaque
824 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400825 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
826 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400827 break;
Jim Van Verth63f03542020-12-16 11:56:11 -0500828 case SpotVerticesFactory::OccluderType::kDirectional:
829 color = 0xFF550000; // dark red for directional
830 break;
Jim Van Vertha783c362017-05-11 17:05:28 -0400831 }
832#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500833 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
834 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500835 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400836 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
837 viewMatrix, zPlaneParams,
Jim Van Verth63f03542020-12-16 11:56:11 -0500838 path.getBounds(), directional,
Jim Van Verth3a039d52018-09-14 17:14:47 -0400839 &shadowMatrix, &radius)) {
840 return;
841 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400842 SkAutoDeviceTransformRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500843
844 SkPaint paint;
845 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500846 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400847 bool respectCTM = false;
848 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500849 this->drawPath(path, paint);
850 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400851 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400852 }
853}