blob: 869997f7b34cfda24ba40e7c3f41a3fa38be590d [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 Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkBlurMask.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040020#include "src/core/SkColorFilterPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/core/SkDevice.h"
22#include "src/core/SkDrawShadowInfo.h"
23#include "src/core/SkEffectPriv.h"
Jim Van Verthee90eb42019-04-26 12:07:13 -040024#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkRasterPipeline.h"
26#include "src/core/SkResourceCache.h"
27#include "src/core/SkTLazy.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040028#include "src/core/SkVM.h"
Mike Reedba962562020-03-12 20:33:21 -040029#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/utils/SkShadowTessellator.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040031#include <new>
Brian Salomon5e689522017-02-01 12:07:17 -050032#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/effects/generated/GrBlurredEdgeFragmentProcessor.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000034#include "src/gpu/geometry/GrStyledShape.h"
Mike Reed4204da22017-05-17 08:53:36 -040035#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050036
37/**
38* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
39* then blends with the color's G value.
40* Final result is black with alpha of Gaussian(B)*G.
41* The assumption is that the original color's alpha is 1.
42*/
Mike Reed57c2b8b2017-12-31 15:23:54 -050043class SkGaussianColorFilter : public SkColorFilter {
Jim Van Verthefe3ded2017-01-30 13:11:45 -050044public:
Mike Reedf36b37f2020-03-27 15:11:10 -040045 SkGaussianColorFilter() : INHERITED() {}
Jim Van Verthefe3ded2017-01-30 13:11:45 -050046
Jim Van Verthefe3ded2017-01-30 13:11:45 -050047#if SK_SUPPORT_GPU
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040048 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*,
49 const GrColorInfo&) const override;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050050#endif
51
Mike Klein40f91382019-08-01 21:07:29 +000052protected:
Jim Van Verthefe3ded2017-01-30 13:11:45 -050053 void flatten(SkWriteBuffer&) const override {}
Mike Klein40f91382019-08-01 21:07:29 +000054 bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
Mike Reed1386b2d2019-03-13 21:15:05 -040055 rec.fPipeline->append(SkRasterPipeline::gauss_a_to_rgba);
Mike Reed2fdbeae2019-03-30 14:27:53 -040056 return true;
Mike Reed65331592017-05-24 16:45:34 -040057 }
Mike Reedf36b37f2020-03-27 15:11:10 -040058
59 skvm::Color onProgram(skvm::Builder* p, skvm::Color c, SkColorSpace* dstCS, skvm::Uniforms*,
60 SkArenaAlloc*) const override {
61 // x = 1 - x;
62 // exp(-x * x * 4) - 0.018f;
63 // ... now approximate with quartic
64 //
Mike Reedf3b9a302020-04-01 13:18:02 -040065 skvm::F32 x = p->splat(-2.26661229133605957031f);
66 x = c.a * x + 2.89795351028442382812f;
67 x = c.a * x + 0.21345567703247070312f;
68 x = c.a * x + 0.15489584207534790039f;
69 x = c.a * x + 0.00030726194381713867f;
Mike Reedf36b37f2020-03-27 15:11:10 -040070 return {x, x, x, x};
71 }
72
Mike Klein40f91382019-08-01 21:07:29 +000073private:
Mike Klein4fee3232018-10-18 17:27:16 -040074 SK_FLATTENABLE_HOOKS(SkGaussianColorFilter)
75
Jim Van Verthefe3ded2017-01-30 13:11:45 -050076 typedef SkColorFilter INHERITED;
77};
78
Jim Van Verthefe3ded2017-01-30 13:11:45 -050079sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
Mike Reedf36b37f2020-03-27 15:11:10 -040080 return SkColorFilterPriv::MakeGaussian();
Jim Van Verthefe3ded2017-01-30 13:11:45 -050081}
82
Jim Van Verthefe3ded2017-01-30 13:11:45 -050083#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050084
Brian Salomonaff329b2017-08-11 09:40:37 -040085std::unique_ptr<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040086 GrRecordingContext*, const GrColorInfo&) const {
Ethan Nicholasaae47c82017-11-10 15:34:03 -050087 return GrBlurredEdgeFragmentProcessor::Make(GrBlurredEdgeFragmentProcessor::Mode::kGaussian);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050088}
89#endif
90
Mike Reedf36b37f2020-03-27 15:11:10 -040091sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
92 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
93}
94
Jim Van Verthefe3ded2017-01-30 13:11:45 -050095///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050096
97namespace {
98
Brian Salomonbc9956d2017-02-22 13:49:09 -050099uint64_t resource_cache_shared_id() {
100 return 0x2020776f64616873llu; // 'shadow '
101}
102
Brian Salomond1ac9822017-02-03 14:25:02 -0500103/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500104struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400105 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500106 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400107 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500108
Brian Salomond1ac9822017-02-03 14:25:02 -0500109 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400110 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500111 return false;
112 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400113 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500114 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500115 }
Brian Salomon5e689522017-02-01 12:07:17 -0500116
Jim Van Verth8793e382017-05-22 15:52:21 -0400117 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
118 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400119 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400120 // pick a canonical place to generate shadow
121 SkMatrix noTrans(ctm);
122 if (!ctm.hasPerspective()) {
123 noTrans[SkMatrix::kMTransX] = 0;
124 noTrans[SkMatrix::kMTransY] = 0;
125 }
126 *translate = fOffset;
127 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500128 }
129};
130
Brian Salomond1ac9822017-02-03 14:25:02 -0500131/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500132struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500133 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400134 // The umbra cannot be dropped out because either the occluder is not opaque,
135 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500136 kTransparent,
137 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400138 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500139 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400140 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500141 };
142
Brian Salomon5e689522017-02-01 12:07:17 -0500143 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400144 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400145 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
146 SkPoint3 fDevLightPos;
147 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500148 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500149
Brian Salomond1ac9822017-02-03 14:25:02 -0500150 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400151 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400152 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500153 return false;
154 }
155 switch (fOccluderType) {
156 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400157 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500158 // 'this' and 'that' will either both have no umbra removed or both have all the
159 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400160 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500161 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400162 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500163 // In this case we partially remove the umbra differently for 'this' and 'that'
164 // if the offsets don't match.
165 if (fOffset == that.fOffset) {
166 translate->set(0, 0);
167 return true;
168 }
169 return false;
170 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400171 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500172 }
Brian Salomon5e689522017-02-01 12:07:17 -0500173
Jim Van Verth8793e382017-05-22 15:52:21 -0400174 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
175 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500176 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400177 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400178 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
179 translate->set(0, 0);
180 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
181 fDevLightPos, fLightRadius, transparent);
182 } else {
183 // pick a canonical place to generate shadow, with light centered over path
184 SkMatrix noTrans(ctm);
185 noTrans[SkMatrix::kMTransX] = 0;
186 noTrans[SkMatrix::kMTransY] = 0;
187 SkPoint devCenter(fLocalCenter);
188 noTrans.mapPoints(&devCenter, 1);
189 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
190 *translate = fOffset;
191 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
192 centerLightPos, fLightRadius, transparent);
193 }
Brian Salomon5e689522017-02-01 12:07:17 -0500194 }
195};
196
197/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500198 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
199 * 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 -0400200 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500201 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500202 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500203class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500204public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500205 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
206
Brian Salomonaff27a22017-02-06 15:47:44 -0500207 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
208 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500209 return fAmbientSet.find(ambient, matrix, translate);
210 }
211
Brian Salomonaff27a22017-02-06 15:47:44 -0500212 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400213 const SkMatrix& matrix, SkVector* translate) {
214 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500215 }
216
Brian Salomonaff27a22017-02-06 15:47:44 -0500217 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
218 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500219 return fSpotSet.find(spot, matrix, translate);
220 }
221
Brian Salomonaff27a22017-02-06 15:47:44 -0500222 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400223 const SkMatrix& matrix, SkVector* translate) {
224 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500225 }
226
227private:
228 template <typename FACTORY, int MAX_ENTRIES>
229 class Set {
230 public:
231 size_t size() const { return fSize; }
232
Brian Salomonaff27a22017-02-06 15:47:44 -0500233 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
234 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500235 for (int i = 0; i < MAX_ENTRIES; ++i) {
236 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
237 const SkMatrix& m = fEntries[i].fMatrix;
238 if (matrix.hasPerspective() || m.hasPerspective()) {
239 if (matrix != fEntries[i].fMatrix) {
240 continue;
241 }
242 } else if (matrix.getScaleX() != m.getScaleX() ||
243 matrix.getSkewX() != m.getSkewX() ||
244 matrix.getScaleY() != m.getScaleY() ||
245 matrix.getSkewY() != m.getSkewY()) {
246 continue;
247 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500248 return fEntries[i].fVertices;
249 }
250 }
251 return nullptr;
252 }
253
Jim Van Verth8793e382017-05-22 15:52:21 -0400254 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
255 SkVector* translate) {
256 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500257 if (!vertices) {
258 return nullptr;
259 }
260 int i;
261 if (fCount < MAX_ENTRIES) {
262 i = fCount++;
263 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400264 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400265 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500266 }
267 fEntries[i].fFactory = factory;
268 fEntries[i].fVertices = vertices;
269 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400270 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500271 return vertices;
272 }
273
274 private:
275 struct Entry {
276 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500277 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500278 SkMatrix fMatrix;
279 };
280 Entry fEntries[MAX_ENTRIES];
281 int fCount = 0;
282 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400283 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500284 };
285
286 Set<AmbientVerticesFactory, 4> fAmbientSet;
287 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500288};
289
Brian Salomond1ac9822017-02-03 14:25:02 -0500290/**
291 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
292 * path. The key represents the path's geometry and not any shadow params.
293 */
294class CachedTessellationsRec : public SkResourceCache::Rec {
295public:
296 CachedTessellationsRec(const SkResourceCache::Key& key,
297 sk_sp<CachedTessellations> tessellations)
298 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500299 fKey.reset(new uint8_t[key.size()]);
300 memcpy(fKey.get(), &key, key.size());
301 }
302
303 const Key& getKey() const override {
304 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
305 }
Brian Salomon5e689522017-02-01 12:07:17 -0500306
Brian Salomond1ac9822017-02-03 14:25:02 -0500307 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500308
Brian Salomond1ac9822017-02-03 14:25:02 -0500309 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500310
Brian Salomond1ac9822017-02-03 14:25:02 -0500311 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500312
Brian Salomond1ac9822017-02-03 14:25:02 -0500313 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500314 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
315 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500316 return fTessellations->find(factory, matrix, translate);
317 }
Brian Salomon5e689522017-02-01 12:07:17 -0500318
319private:
320 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500321 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500322};
323
324/**
325 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500326 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
327 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
328 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500329 */
330template <typename FACTORY>
331struct FindContext {
332 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
333 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500334 const SkMatrix* const fViewMatrix;
335 // If this is valid after Find is called then we found the vertices and they should be drawn
336 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500337 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500338 SkVector fTranslate = {0, 0};
339
340 // If this is valid after Find then the caller should add the vertices to the tessellation set
341 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
342 sk_sp<CachedTessellations> fTessellationsOnFailure;
343
Brian Salomon5e689522017-02-01 12:07:17 -0500344 const FACTORY* fFactory;
345};
346
347/**
348 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
349 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
350 * necessary translation vector are set on the FindContext.
351 */
352template <typename FACTORY>
353bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
354 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500355 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
356 findContext->fVertices =
357 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
358 if (findContext->fVertices) {
359 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500360 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500361 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
362 // manipulated we will add a new Rec.
363 findContext->fTessellationsOnFailure = rec.refTessellations();
364 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500365}
366
367class ShadowedPath {
368public:
369 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500370 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500371 , fViewMatrix(viewMatrix)
372#if SK_SUPPORT_GPU
373 , fShapeForKey(*path, GrStyle::SimpleFill())
374#endif
375 {}
376
Jim Van Vertha84898d2017-02-06 13:38:23 -0500377 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500378 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
379#if SK_SUPPORT_GPU
380 /** Negative means the vertices should not be cached for this path. */
381 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
382 void writeKey(void* key) const {
383 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
384 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500385 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500386#else
387 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400388 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500389 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500390#endif
391
392private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500393 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500394 const SkMatrix* fViewMatrix;
395#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000396 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500397#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500398};
399
Brian Salomond1ac9822017-02-03 14:25:02 -0500400// This creates a domain of keys in SkResourceCache used by this file.
401static void* kNamespace;
402
Jim Van Verthee90eb42019-04-26 12:07:13 -0400403// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500404class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400405public:
406 ShadowInvalidator(const SkResourceCache::Key& key) {
407 fKey.reset(new uint8_t[key.size()]);
408 memcpy(fKey.get(), &key, key.size());
409 }
410
411private:
412 const SkResourceCache::Key& getKey() const {
413 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
414 }
415
416 // always purge
417 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
418 return false;
419 }
420
Brian Salomon99a813c2020-03-02 12:50:47 -0500421 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400422 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
423 }
424
425 std::unique_ptr<uint8_t[]> fKey;
426};
427
Brian Salomon5e689522017-02-01 12:07:17 -0500428/**
429 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
430 * they are first found in SkResourceCache.
431 */
432template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500433bool draw_shadow(const FACTORY& factory,
434 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400435 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500436 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500437
438 SkResourceCache::Key* key = nullptr;
439 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
440 int keyDataBytes = path.keyBytes();
441 if (keyDataBytes >= 0) {
442 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
443 key = new (keyStorage.begin()) SkResourceCache::Key();
444 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500445 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400446 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500447 }
448
Brian Salomonaff27a22017-02-06 15:47:44 -0500449 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500450 bool foundInCache = SkToBool(context.fVertices);
451 if (foundInCache) {
452 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500453 } else {
454 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500455 if (key) {
456 // Update or initialize a tessellation set and add it to the cache.
457 sk_sp<CachedTessellations> tessellations;
458 if (context.fTessellationsOnFailure) {
459 tessellations = std::move(context.fTessellationsOnFailure);
460 } else {
461 tessellations.reset(new CachedTessellations());
462 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400463 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
464 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500465 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500466 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500467 }
Brian Salomon804e0912017-02-23 09:34:03 -0500468 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400469 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400470 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500471 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400472 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
473 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500474 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500475 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500476 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500477 }
Brian Salomon5e689522017-02-01 12:07:17 -0500478 }
479
480 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500481 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
482 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500483 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400484 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400485 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400486
Jim Van Verth8793e382017-05-22 15:52:21 -0400487 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400488 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500489
490 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500491}
492}
493
Mike Reed4204da22017-05-17 08:53:36 -0400494static bool tilted(const SkPoint3& zPlaneParams) {
495 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
496}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400497
Mike Reed4204da22017-05-17 08:53:36 -0400498static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
499 SkPoint3 result;
500 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
501 result.fZ = pt.fZ;
502 return result;
503}
504
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500505void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
506 SkColor* outAmbientColor, SkColor* outSpotColor) {
507 // For tonal color we only compute color values for the spot shadow.
508 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400509
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500510 // Ambient
511 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400512
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500513 // Spot
514 int spotR = SkColorGetR(inSpotColor);
515 int spotG = SkColorGetG(inSpotColor);
516 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500517 int max = std::max(std::max(spotR, spotG), spotB);
518 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500519 SkScalar luminance = 0.5f*(max + min)/255.f;
520 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
521
522 // We compute a color alpha value based on the luminance of the color, scaled by an
523 // adjusted alpha value. We want the following properties to match the UX examples
524 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
525 // is black and/or the alpha is 0:
526 // f(0, a) = 0
527 // f(luminance, 0) = 0
528 // f(1, 0.25) = .5
529 // f(0.5, 0.25) = .4
530 // f(1, 1) = 1
531 // The following functions match this as closely as possible.
532 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
533 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
534 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
535
536 // Similarly, we set the greyscale alpha based on luminance and alpha so that
537 // f(0, a) = a
538 // f(luminance, 0) = 0
539 // f(1, 0.25) = 0.15
540 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
541
542 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
543 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
544 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
545 // ignoring edge falloff, this becomes
546 //
547 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
548 //
549 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
550 // set the alpha to (S_a + C_a - S_a*C_a).
551 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
552 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
553 SkScalar unPremulScale = colorScale / tonalAlpha;
554 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
555 unPremulScale*spotR,
556 unPremulScale*spotG,
557 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400558}
559
Jim Van Verth43475ad2017-01-13 14:37:37 -0500560// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400561void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500562 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500563 SkColor ambientColor, SkColor spotColor,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400564 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400565 SkMatrix inverse;
566 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500567 return;
568 }
Mike Reed4204da22017-05-17 08:53:36 -0400569 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500570
Mike Reed4204da22017-05-17 08:53:36 -0400571 SkDrawShadowRec rec;
572 rec.fZPlaneParams = zPlaneParams;
573 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
574 rec.fLightRadius = lightRadius;
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500575 rec.fAmbientColor = ambientColor;
576 rec.fSpotColor = spotColor;
Mike Reed4204da22017-05-17 08:53:36 -0400577 rec.fFlags = flags;
578
579 canvas->private_draw_shadow_rec(path, rec);
580}
581
Jim Van Vertha947e292018-02-26 13:54:34 -0500582static bool validate_rec(const SkDrawShadowRec& rec) {
583 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
584 SkScalarIsFinite(rec.fLightRadius);
585}
586
Mike Reed4204da22017-05-17 08:53:36 -0400587void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
588 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400589 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400590 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400591 // For perspective shadows we've already computed the shadow in world space,
592 // and we can't translate it without changing it. Otherwise we concat the
593 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400594 SkAutoDeviceTransformRestore adr(
595 this,
596 hasPerspective ? SkMatrix::I()
597 : SkMatrix::Concat(this->localToDevice(),
598 SkMatrix::MakeTrans(tx, ty)));
Mike Reed5caf9352020-03-02 14:57:09 -0500599 this->drawVertices(vertices, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400600 }
Mike Reed4204da22017-05-17 08:53:36 -0400601 };
602
Jim Van Vertha947e292018-02-26 13:54:34 -0500603 if (!validate_rec(rec)) {
604 return;
605 }
606
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400607 SkMatrix viewMatrix = this->localToDevice();
608 SkAutoDeviceTransformRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500609
Brian Salomon5e689522017-02-01 12:07:17 -0500610 ShadowedPath shadowedPath(&path, &viewMatrix);
611
Mike Reed4204da22017-05-17 08:53:36 -0400612 bool tiltZPlane = tilted(rec.fZPlaneParams);
613 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400614 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500615
Mike Reed4204da22017-05-17 08:53:36 -0400616 SkPoint3 zPlaneParams = rec.fZPlaneParams;
617 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
618 float lightRadius = rec.fLightRadius;
619
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500620 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500621 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400622 if (uncached) {
623 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
624 zPlaneParams,
625 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400626 if (vertices) {
627 SkPaint paint;
628 // Run the vertex color through a GaussianColorFilter and then modulate the
629 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500630 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400631 SkColorFilters::Blend(rec.fAmbientColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500632 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400633 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500634 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500635 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400636 }
Jim Van Verth22526362018-02-28 14:51:19 -0500637 }
638
639 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400640 AmbientVerticesFactory factory;
641 factory.fOccluderHeight = zPlaneParams.fZ;
642 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400643 if (viewMatrix.hasPerspective()) {
644 factory.fOffset.set(0, 0);
645 } else {
646 factory.fOffset.fX = viewMatrix.getTranslateX();
647 factory.fOffset.fY = viewMatrix.getTranslateY();
648 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400649
Jim Van Verth22526362018-02-28 14:51:19 -0500650 if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) {
651 // Pretransform the path to avoid transforming the stroke, below.
652 SkPath devSpacePath;
653 path.transform(viewMatrix, &devSpacePath);
Robert Phillipsed3dbf42019-03-18 12:20:15 -0400654 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500655
656 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400657 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500658 //
659 // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f')
660 // can be calculated by interpolating:
661 //
662 // original edge outer edge
663 // | |<---------- r ------>|
664 // |<------|--- f -------------->|
665 // | | |
666 // alpha = 1 alpha = a alpha = 0
667 //
668 // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2.
669 //
670 // We now need to outset the path to place the new edge in the center of the
671 // blur region:
672 //
673 // original new
674 // | |<------|--- r ------>|
675 // |<------|--- f -|------------>|
676 // | |<- o ->|<--- f/2 --->|
677 //
678 // r = o + f/2, so o = r - f/2
679 //
680 // We outset by using the stroker, so the strokeWidth is o/2.
681 //
682 SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ);
683 SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ);
684 SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA;
685 SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius);
686
687 // Now draw with blur
688 SkPaint paint;
689 paint.setColor(rec.fAmbientColor);
690 paint.setStrokeWidth(strokeWidth);
691 paint.setStyle(SkPaint::kStrokeAndFill_Style);
Mike Reed8e03f692018-03-09 16:18:56 -0500692 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400693 bool respectCTM = false;
694 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500695 this->drawPath(devSpacePath, paint);
696 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500697 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400698 }
699
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500700 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500701 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400702 if (uncached) {
703 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
704 zPlaneParams,
705 devLightPos, lightRadius,
706 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400707 if (vertices) {
708 SkPaint paint;
709 // Run the vertex color through a GaussianColorFilter and then modulate the
710 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500711 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400712 SkColorFilters::Blend(rec.fSpotColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500713 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400714 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500715 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500716 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400717 }
Jim Van Verth22526362018-02-28 14:51:19 -0500718 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400719
Jim Van Verth22526362018-02-28 14:51:19 -0500720 if (!success) {
721 SpotVerticesFactory factory;
722 factory.fOccluderHeight = zPlaneParams.fZ;
723 factory.fDevLightPos = devLightPos;
724 factory.fLightRadius = lightRadius;
725
Jim Van Verth37c5a962017-05-10 14:13:24 -0400726 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400727 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400728 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500729 SkScalar radius, scale;
730 SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX,
731 devLightPos.fY - center.fY, devLightPos.fZ,
732 lightRadius, &radius, &scale, &factory.fOffset);
Jim Van Vertha783c362017-05-11 17:05:28 -0400733 SkRect devBounds;
734 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth8793e382017-05-22 15:52:21 -0400735 if (transparent ||
736 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
737 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400738 // if the translation of the shadow is big enough we're going to end up
739 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400740 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400741 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400742 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400743 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400744 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400745 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400746 } else {
747 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400748 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400749 // need to add this after we classify the shadow
750 factory.fOffset.fX += viewMatrix.getTranslateX();
751 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500752
753 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400754#ifdef DEBUG_SHADOW_CHECKS
755 switch (factory.fOccluderType) {
756 case SpotVerticesFactory::OccluderType::kTransparent:
757 color = 0xFFD2B48C; // tan for transparent
758 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400759 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400760 color = 0xFFFFA500; // orange for opaque
761 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400762 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
763 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400764 break;
765 }
766#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500767 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
768 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500769 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400770 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
771 viewMatrix, zPlaneParams,
772 path.getBounds(),
773 &shadowMatrix, &radius)) {
774 return;
775 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400776 SkAutoDeviceTransformRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500777
778 SkPaint paint;
779 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500780 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400781 bool respectCTM = false;
782 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500783 this->drawPath(path, paint);
784 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400785 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400786 }
787}