blob: 4e30c40348e60c7c2be0f1834eb8bdf60355294b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/effects/generated/GrBlurredEdgeFragmentProcessor.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 {
90 return GrFPSuccess(GrBlurredEdgeFragmentProcessor::Make(
91 std::move(inputFP), GrBlurredEdgeFragmentProcessor::Mode::kGaussian));
Jim Van Verthefe3ded2017-01-30 13:11:45 -050092}
93#endif
94
Mike Reedf36b37f2020-03-27 15:11:10 -040095sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
96 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
97}
98
Jim Van Verthefe3ded2017-01-30 13:11:45 -050099///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -0500100
101namespace {
102
Brian Salomonbc9956d2017-02-22 13:49:09 -0500103uint64_t resource_cache_shared_id() {
104 return 0x2020776f64616873llu; // 'shadow '
105}
106
Brian Salomond1ac9822017-02-03 14:25:02 -0500107/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500108struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400109 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500110 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400111 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500112
Brian Salomond1ac9822017-02-03 14:25:02 -0500113 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400114 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500115 return false;
116 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400117 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500118 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500119 }
Brian Salomon5e689522017-02-01 12:07:17 -0500120
Jim Van Verth8793e382017-05-22 15:52:21 -0400121 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
122 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400123 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400124 // pick a canonical place to generate shadow
125 SkMatrix noTrans(ctm);
126 if (!ctm.hasPerspective()) {
127 noTrans[SkMatrix::kMTransX] = 0;
128 noTrans[SkMatrix::kMTransY] = 0;
129 }
130 *translate = fOffset;
131 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500132 }
133};
134
Brian Salomond1ac9822017-02-03 14:25:02 -0500135/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500136struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500137 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400138 // The umbra cannot be dropped out because either the occluder is not opaque,
139 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500140 kTransparent,
141 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400142 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500143 // It is known that the entire umbra is occluded.
Jim Van Verth63f03542020-12-16 11:56:11 -0500144 kOpaqueNoUmbra,
145 // The light is directional
146 kDirectional
Brian Salomond1ac9822017-02-03 14:25:02 -0500147 };
148
Brian Salomon5e689522017-02-01 12:07:17 -0500149 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400150 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400151 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
152 SkPoint3 fDevLightPos;
153 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500154 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500155
Brian Salomond1ac9822017-02-03 14:25:02 -0500156 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400157 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400158 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500159 return false;
160 }
161 switch (fOccluderType) {
162 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400163 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500164 // 'this' and 'that' will either both have no umbra removed or both have all the
165 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400166 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500167 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400168 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500169 // In this case we partially remove the umbra differently for 'this' and 'that'
170 // if the offsets don't match.
171 if (fOffset == that.fOffset) {
172 translate->set(0, 0);
173 return true;
174 }
175 return false;
Jim Van Verth63f03542020-12-16 11:56:11 -0500176 case OccluderType::kDirectional:
177 *translate = that.fOffset - fOffset;
178 return true;
Brian Salomond1ac9822017-02-03 14:25:02 -0500179 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400180 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500181 }
Brian Salomon5e689522017-02-01 12:07:17 -0500182
Jim Van Verth8793e382017-05-22 15:52:21 -0400183 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
184 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500185 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verth63f03542020-12-16 11:56:11 -0500186 bool directional = OccluderType::kDirectional == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400187 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth63f03542020-12-16 11:56:11 -0500188 if (directional) {
Jim Van Verth8793e382017-05-22 15:52:21 -0400189 translate->set(0, 0);
Jim Van Verth63f03542020-12-16 11:56:11 -0500190 return SkShadowTessellator::MakeSpot(path, ctm, zParams, fDevLightPos, fLightRadius,
191 transparent, true);
192 } else if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
193 translate->set(0, 0);
194 return SkShadowTessellator::MakeSpot(path, ctm, zParams, fDevLightPos, fLightRadius,
195 transparent, false);
Jim Van Verth8793e382017-05-22 15:52:21 -0400196 } else {
197 // pick a canonical place to generate shadow, with light centered over path
198 SkMatrix noTrans(ctm);
199 noTrans[SkMatrix::kMTransX] = 0;
200 noTrans[SkMatrix::kMTransY] = 0;
201 SkPoint devCenter(fLocalCenter);
202 noTrans.mapPoints(&devCenter, 1);
203 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
204 *translate = fOffset;
205 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
Jim Van Verth63f03542020-12-16 11:56:11 -0500206 centerLightPos, fLightRadius, transparent, false);
Jim Van Verth8793e382017-05-22 15:52:21 -0400207 }
Brian Salomon5e689522017-02-01 12:07:17 -0500208 }
209};
210
211/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500212 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
213 * 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 -0400214 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500215 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500216 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500217class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500218public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500219 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
220
Brian Salomonaff27a22017-02-06 15:47:44 -0500221 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
222 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500223 return fAmbientSet.find(ambient, matrix, translate);
224 }
225
Brian Salomonaff27a22017-02-06 15:47:44 -0500226 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400227 const SkMatrix& matrix, SkVector* translate) {
228 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500229 }
230
Brian Salomonaff27a22017-02-06 15:47:44 -0500231 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
232 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500233 return fSpotSet.find(spot, matrix, translate);
234 }
235
Brian Salomonaff27a22017-02-06 15:47:44 -0500236 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400237 const SkMatrix& matrix, SkVector* translate) {
238 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500239 }
240
241private:
242 template <typename FACTORY, int MAX_ENTRIES>
243 class Set {
244 public:
245 size_t size() const { return fSize; }
246
Brian Salomonaff27a22017-02-06 15:47:44 -0500247 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
248 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500249 for (int i = 0; i < MAX_ENTRIES; ++i) {
250 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
251 const SkMatrix& m = fEntries[i].fMatrix;
252 if (matrix.hasPerspective() || m.hasPerspective()) {
253 if (matrix != fEntries[i].fMatrix) {
254 continue;
255 }
256 } else if (matrix.getScaleX() != m.getScaleX() ||
257 matrix.getSkewX() != m.getSkewX() ||
258 matrix.getScaleY() != m.getScaleY() ||
259 matrix.getSkewY() != m.getSkewY()) {
260 continue;
261 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500262 return fEntries[i].fVertices;
263 }
264 }
265 return nullptr;
266 }
267
Jim Van Verth8793e382017-05-22 15:52:21 -0400268 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
269 SkVector* translate) {
270 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500271 if (!vertices) {
272 return nullptr;
273 }
274 int i;
275 if (fCount < MAX_ENTRIES) {
276 i = fCount++;
277 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400278 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400279 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500280 }
281 fEntries[i].fFactory = factory;
282 fEntries[i].fVertices = vertices;
283 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400284 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500285 return vertices;
286 }
287
288 private:
289 struct Entry {
290 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500291 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500292 SkMatrix fMatrix;
293 };
294 Entry fEntries[MAX_ENTRIES];
295 int fCount = 0;
296 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400297 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500298 };
299
300 Set<AmbientVerticesFactory, 4> fAmbientSet;
301 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500302};
303
Brian Salomond1ac9822017-02-03 14:25:02 -0500304/**
305 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
306 * path. The key represents the path's geometry and not any shadow params.
307 */
308class CachedTessellationsRec : public SkResourceCache::Rec {
309public:
310 CachedTessellationsRec(const SkResourceCache::Key& key,
311 sk_sp<CachedTessellations> tessellations)
312 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500313 fKey.reset(new uint8_t[key.size()]);
314 memcpy(fKey.get(), &key, key.size());
315 }
316
317 const Key& getKey() const override {
318 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
319 }
Brian Salomon5e689522017-02-01 12:07:17 -0500320
Brian Salomond1ac9822017-02-03 14:25:02 -0500321 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500322
Brian Salomond1ac9822017-02-03 14:25:02 -0500323 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500324
Brian Salomond1ac9822017-02-03 14:25:02 -0500325 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500326
Brian Salomond1ac9822017-02-03 14:25:02 -0500327 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500328 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
329 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500330 return fTessellations->find(factory, matrix, translate);
331 }
Brian Salomon5e689522017-02-01 12:07:17 -0500332
333private:
334 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500335 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500336};
337
338/**
339 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500340 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
341 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
342 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500343 */
344template <typename FACTORY>
345struct FindContext {
346 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
347 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500348 const SkMatrix* const fViewMatrix;
349 // If this is valid after Find is called then we found the vertices and they should be drawn
350 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500351 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500352 SkVector fTranslate = {0, 0};
353
354 // If this is valid after Find then the caller should add the vertices to the tessellation set
355 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
356 sk_sp<CachedTessellations> fTessellationsOnFailure;
357
Brian Salomon5e689522017-02-01 12:07:17 -0500358 const FACTORY* fFactory;
359};
360
361/**
362 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
363 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
364 * necessary translation vector are set on the FindContext.
365 */
366template <typename FACTORY>
367bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
368 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500369 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
370 findContext->fVertices =
371 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
372 if (findContext->fVertices) {
373 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500374 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500375 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
376 // manipulated we will add a new Rec.
377 findContext->fTessellationsOnFailure = rec.refTessellations();
378 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500379}
380
381class ShadowedPath {
382public:
383 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500384 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500385 , fViewMatrix(viewMatrix)
386#if SK_SUPPORT_GPU
387 , fShapeForKey(*path, GrStyle::SimpleFill())
388#endif
389 {}
390
Jim Van Vertha84898d2017-02-06 13:38:23 -0500391 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500392 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
393#if SK_SUPPORT_GPU
394 /** Negative means the vertices should not be cached for this path. */
395 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
396 void writeKey(void* key) const {
397 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
398 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500399 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500400#else
401 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400402 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500403 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500404#endif
405
406private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500407 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500408 const SkMatrix* fViewMatrix;
409#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000410 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500411#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500412};
413
Brian Salomond1ac9822017-02-03 14:25:02 -0500414// This creates a domain of keys in SkResourceCache used by this file.
415static void* kNamespace;
416
Jim Van Verthee90eb42019-04-26 12:07:13 -0400417// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500418class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400419public:
420 ShadowInvalidator(const SkResourceCache::Key& key) {
421 fKey.reset(new uint8_t[key.size()]);
422 memcpy(fKey.get(), &key, key.size());
423 }
424
425private:
426 const SkResourceCache::Key& getKey() const {
427 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
428 }
429
430 // always purge
431 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
432 return false;
433 }
434
Brian Salomon99a813c2020-03-02 12:50:47 -0500435 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400436 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
437 }
438
439 std::unique_ptr<uint8_t[]> fKey;
440};
441
Brian Salomon5e689522017-02-01 12:07:17 -0500442/**
443 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
444 * they are first found in SkResourceCache.
445 */
446template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500447bool draw_shadow(const FACTORY& factory,
448 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400449 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500450 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500451
452 SkResourceCache::Key* key = nullptr;
453 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
454 int keyDataBytes = path.keyBytes();
455 if (keyDataBytes >= 0) {
456 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
457 key = new (keyStorage.begin()) SkResourceCache::Key();
458 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500459 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400460 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500461 }
462
Brian Salomonaff27a22017-02-06 15:47:44 -0500463 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500464 bool foundInCache = SkToBool(context.fVertices);
465 if (foundInCache) {
466 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500467 } else {
468 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500469 if (key) {
470 // Update or initialize a tessellation set and add it to the cache.
471 sk_sp<CachedTessellations> tessellations;
472 if (context.fTessellationsOnFailure) {
473 tessellations = std::move(context.fTessellationsOnFailure);
474 } else {
475 tessellations.reset(new CachedTessellations());
476 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400477 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
478 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500479 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500480 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500481 }
Brian Salomon804e0912017-02-23 09:34:03 -0500482 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400483 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400484 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500485 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400486 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
487 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500488 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500489 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500490 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500491 }
Brian Salomon5e689522017-02-01 12:07:17 -0500492 }
493
494 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500495 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
496 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500497 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400498 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400499 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400500
Jim Van Verth8793e382017-05-22 15:52:21 -0400501 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400502 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500503
504 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500505}
John Stilesa6841be2020-08-06 14:11:56 -0400506} // namespace
Brian Salomon5e689522017-02-01 12:07:17 -0500507
Mike Reed4204da22017-05-17 08:53:36 -0400508static bool tilted(const SkPoint3& zPlaneParams) {
509 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
510}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400511
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500512void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
513 SkColor* outAmbientColor, SkColor* outSpotColor) {
514 // For tonal color we only compute color values for the spot shadow.
515 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400516
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500517 // Ambient
518 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400519
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500520 // Spot
521 int spotR = SkColorGetR(inSpotColor);
522 int spotG = SkColorGetG(inSpotColor);
523 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500524 int max = std::max(std::max(spotR, spotG), spotB);
525 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500526 SkScalar luminance = 0.5f*(max + min)/255.f;
527 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
528
529 // We compute a color alpha value based on the luminance of the color, scaled by an
530 // adjusted alpha value. We want the following properties to match the UX examples
531 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
532 // is black and/or the alpha is 0:
533 // f(0, a) = 0
534 // f(luminance, 0) = 0
535 // f(1, 0.25) = .5
536 // f(0.5, 0.25) = .4
537 // f(1, 1) = 1
538 // The following functions match this as closely as possible.
539 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
540 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
541 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
542
543 // Similarly, we set the greyscale alpha based on luminance and alpha so that
544 // f(0, a) = a
545 // f(luminance, 0) = 0
546 // f(1, 0.25) = 0.15
547 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
548
549 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
550 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
551 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
552 // ignoring edge falloff, this becomes
553 //
554 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
555 //
556 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
557 // set the alpha to (S_a + C_a - S_a*C_a).
558 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
559 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
560 SkScalar unPremulScale = colorScale / tonalAlpha;
561 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
562 unPremulScale*spotR,
563 unPremulScale*spotG,
564 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400565}
566
Jim Van Verthea4aa392021-01-11 11:01:09 -0500567static bool fill_shadow_rec(const SkPath& path, const SkPoint3& zPlaneParams,
568 const SkPoint3& lightPos, SkScalar lightRadius,
569 SkColor ambientColor, SkColor spotColor,
570 uint32_t flags, const SkMatrix& ctm, SkDrawShadowRec* rec) {
Jim Van Verth63f03542020-12-16 11:56:11 -0500571 SkPoint pt = { lightPos.fX, lightPos.fY };
572 if (!SkToBool(flags & kDirectionalLight_ShadowFlag)) {
573 // If light position is in device space, need to transform to local space
574 // before applying to SkCanvas.
575 SkMatrix inverse;
Jim Van Verthea4aa392021-01-11 11:01:09 -0500576 if (!ctm.invert(&inverse)) {
577 return false;
Jim Van Verth63f03542020-12-16 11:56:11 -0500578 }
579 inverse.mapPoints(&pt, 1);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500580 }
581
Jim Van Verthea4aa392021-01-11 11:01:09 -0500582 rec->fZPlaneParams = zPlaneParams;
583 rec->fLightPos = { pt.fX, pt.fY, lightPos.fZ };
584 rec->fLightRadius = lightRadius;
585 rec->fAmbientColor = ambientColor;
586 rec->fSpotColor = spotColor;
587 rec->fFlags = flags;
588
589 return true;
590}
591
592// Draw an offset spot shadow and outlining ambient shadow for the given path.
593void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
594 const SkPoint3& lightPos, SkScalar lightRadius,
595 SkColor ambientColor, SkColor spotColor,
596 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400597 SkDrawShadowRec rec;
Jim Van Verthea4aa392021-01-11 11:01:09 -0500598 if (!fill_shadow_rec(path, zPlaneParams, lightPos, lightRadius, ambientColor, spotColor,
599 flags, canvas->getTotalMatrix(), &rec)) {
600 return;
601 }
Mike Reed4204da22017-05-17 08:53:36 -0400602
603 canvas->private_draw_shadow_rec(path, rec);
604}
605
Jim Van Verthea4aa392021-01-11 11:01:09 -0500606bool SkShadowUtils::GetLocalBounds(const SkMatrix& ctm, const SkPath& path,
607 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
608 SkScalar lightRadius, uint32_t flags, SkRect* bounds) {
609 SkDrawShadowRec rec;
610 if (!fill_shadow_rec(path, zPlaneParams, lightPos, lightRadius, SK_ColorBLACK, SK_ColorBLACK,
611 flags, ctm, &rec)) {
612 return false;
613 }
614
615 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, bounds);
616
617 return true;
618}
619
620//////////////////////////////////////////////////////////////////////////////////////////////
621
Jim Van Vertha947e292018-02-26 13:54:34 -0500622static bool validate_rec(const SkDrawShadowRec& rec) {
623 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
624 SkScalarIsFinite(rec.fLightRadius);
625}
626
Mike Reed4204da22017-05-17 08:53:36 -0400627void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
628 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400629 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400630 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400631 // For perspective shadows we've already computed the shadow in world space,
632 // and we can't translate it without changing it. Otherwise we concat the
633 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400634 SkAutoDeviceTransformRestore adr(
635 this,
636 hasPerspective ? SkMatrix::I()
Mike Reed1f607332020-05-21 12:11:27 -0400637 : this->localToDevice() * SkMatrix::Translate(tx, ty));
Mike Reed5caf9352020-03-02 14:57:09 -0500638 this->drawVertices(vertices, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400639 }
Mike Reed4204da22017-05-17 08:53:36 -0400640 };
641
Jim Van Vertha947e292018-02-26 13:54:34 -0500642 if (!validate_rec(rec)) {
643 return;
644 }
645
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400646 SkMatrix viewMatrix = this->localToDevice();
647 SkAutoDeviceTransformRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500648
Brian Salomon5e689522017-02-01 12:07:17 -0500649 ShadowedPath shadowedPath(&path, &viewMatrix);
650
Mike Reed4204da22017-05-17 08:53:36 -0400651 bool tiltZPlane = tilted(rec.fZPlaneParams);
652 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth63f03542020-12-16 11:56:11 -0500653 bool directional = SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400654 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500655
Mike Reed4204da22017-05-17 08:53:36 -0400656 SkPoint3 zPlaneParams = rec.fZPlaneParams;
Jim Van Verth63f03542020-12-16 11:56:11 -0500657 SkPoint3 devLightPos = rec.fLightPos;
658 if (directional) {
Jim Van Vertha8682202020-12-17 10:18:16 -0500659 devLightPos.normalize();
Jim Van Verth63f03542020-12-16 11:56:11 -0500660 } else {
661 viewMatrix.mapPoints((SkPoint*)&devLightPos.fX, 1);
662 }
Mike Reed4204da22017-05-17 08:53:36 -0400663 float lightRadius = rec.fLightRadius;
664
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500665 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500666 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400667 if (uncached) {
668 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
669 zPlaneParams,
670 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400671 if (vertices) {
672 SkPaint paint;
673 // Run the vertex color through a GaussianColorFilter and then modulate the
674 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500675 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400676 SkColorFilters::Blend(rec.fAmbientColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500677 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400678 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500679 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500680 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400681 }
Jim Van Verth22526362018-02-28 14:51:19 -0500682 }
683
684 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400685 AmbientVerticesFactory factory;
686 factory.fOccluderHeight = zPlaneParams.fZ;
687 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400688 if (viewMatrix.hasPerspective()) {
689 factory.fOffset.set(0, 0);
690 } else {
691 factory.fOffset.fX = viewMatrix.getTranslateX();
692 factory.fOffset.fY = viewMatrix.getTranslateY();
693 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400694
Jim Van Verth22526362018-02-28 14:51:19 -0500695 if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) {
696 // Pretransform the path to avoid transforming the stroke, below.
697 SkPath devSpacePath;
698 path.transform(viewMatrix, &devSpacePath);
Robert Phillipsed3dbf42019-03-18 12:20:15 -0400699 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500700
701 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400702 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500703 //
704 // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f')
705 // can be calculated by interpolating:
706 //
707 // original edge outer edge
708 // | |<---------- r ------>|
709 // |<------|--- f -------------->|
710 // | | |
711 // alpha = 1 alpha = a alpha = 0
712 //
713 // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2.
714 //
715 // We now need to outset the path to place the new edge in the center of the
716 // blur region:
717 //
718 // original new
719 // | |<------|--- r ------>|
720 // |<------|--- f -|------------>|
721 // | |<- o ->|<--- f/2 --->|
722 //
723 // r = o + f/2, so o = r - f/2
724 //
725 // We outset by using the stroker, so the strokeWidth is o/2.
726 //
727 SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ);
728 SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ);
729 SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA;
730 SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius);
731
732 // Now draw with blur
733 SkPaint paint;
734 paint.setColor(rec.fAmbientColor);
735 paint.setStrokeWidth(strokeWidth);
736 paint.setStyle(SkPaint::kStrokeAndFill_Style);
Mike Reed8e03f692018-03-09 16:18:56 -0500737 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400738 bool respectCTM = false;
739 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500740 this->drawPath(devSpacePath, paint);
741 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500742 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400743 }
744
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500745 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500746 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400747 if (uncached) {
748 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
749 zPlaneParams,
750 devLightPos, lightRadius,
Jim Van Verth63f03542020-12-16 11:56:11 -0500751 transparent,
752 directional);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400753 if (vertices) {
754 SkPaint paint;
755 // Run the vertex color through a GaussianColorFilter and then modulate the
756 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500757 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400758 SkColorFilters::Blend(rec.fSpotColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500759 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400760 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500761 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500762 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400763 }
Jim Van Verth22526362018-02-28 14:51:19 -0500764 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400765
Jim Van Verth22526362018-02-28 14:51:19 -0500766 if (!success) {
767 SpotVerticesFactory factory;
768 factory.fOccluderHeight = zPlaneParams.fZ;
769 factory.fDevLightPos = devLightPos;
770 factory.fLightRadius = lightRadius;
771
Jim Van Verth37c5a962017-05-10 14:13:24 -0400772 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400773 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400774 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500775 SkScalar radius, scale;
Jim Van Verth63f03542020-12-16 11:56:11 -0500776 if (SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag)) {
777 SkDrawShadowMetrics::GetDirectionalParams(zPlaneParams.fZ, devLightPos.fX,
778 devLightPos.fY, devLightPos.fZ,
779 lightRadius, &radius, &scale,
780 &factory.fOffset);
781 } else {
782 SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX,
783 devLightPos.fY - center.fY, devLightPos.fZ,
784 lightRadius, &radius, &scale, &factory.fOffset);
785 }
786
Jim Van Vertha783c362017-05-11 17:05:28 -0400787 SkRect devBounds;
788 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth63f03542020-12-16 11:56:11 -0500789 if (directional) {
790 factory.fOccluderType = SpotVerticesFactory::OccluderType::kDirectional;
791 } else if (transparent ||
792 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
793 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400794 // if the translation of the shadow is big enough we're going to end up
795 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400796 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400797 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400798 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400799 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400800 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400801 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400802 } else {
803 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400804 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400805 // need to add this after we classify the shadow
806 factory.fOffset.fX += viewMatrix.getTranslateX();
807 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500808
809 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400810#ifdef DEBUG_SHADOW_CHECKS
811 switch (factory.fOccluderType) {
812 case SpotVerticesFactory::OccluderType::kTransparent:
813 color = 0xFFD2B48C; // tan for transparent
814 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400815 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400816 color = 0xFFFFA500; // orange for opaque
817 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400818 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
819 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400820 break;
Jim Van Verth63f03542020-12-16 11:56:11 -0500821 case SpotVerticesFactory::OccluderType::kDirectional:
822 color = 0xFF550000; // dark red for directional
823 break;
Jim Van Vertha783c362017-05-11 17:05:28 -0400824 }
825#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500826 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
827 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500828 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400829 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
830 viewMatrix, zPlaneParams,
Jim Van Verth63f03542020-12-16 11:56:11 -0500831 path.getBounds(), directional,
Jim Van Verth3a039d52018-09-14 17:14:47 -0400832 &shadowMatrix, &radius)) {
833 return;
834 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400835 SkAutoDeviceTransformRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500836
837 SkPaint paint;
838 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500839 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400840 bool respectCTM = false;
841 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500842 this->drawPath(path, paint);
843 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400844 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400845 }
846}