blob: f287fc03060b0d6e951bf4987ea8bcd288f8d36f [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 {
John Stilescdc39bd2020-06-04 16:14:13 -040087 return GrBlurredEdgeFragmentProcessor::Make(
88 /*inputFP=*/nullptr, GrBlurredEdgeFragmentProcessor::Mode::kGaussian);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050089}
90#endif
91
Mike Reedf36b37f2020-03-27 15:11:10 -040092sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
93 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
94}
95
Jim Van Verthefe3ded2017-01-30 13:11:45 -050096///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050097
98namespace {
99
Brian Salomonbc9956d2017-02-22 13:49:09 -0500100uint64_t resource_cache_shared_id() {
101 return 0x2020776f64616873llu; // 'shadow '
102}
103
Brian Salomond1ac9822017-02-03 14:25:02 -0500104/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500105struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400106 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500107 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400108 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500109
Brian Salomond1ac9822017-02-03 14:25:02 -0500110 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400111 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500112 return false;
113 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400114 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500115 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500116 }
Brian Salomon5e689522017-02-01 12:07:17 -0500117
Jim Van Verth8793e382017-05-22 15:52:21 -0400118 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
119 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400120 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400121 // pick a canonical place to generate shadow
122 SkMatrix noTrans(ctm);
123 if (!ctm.hasPerspective()) {
124 noTrans[SkMatrix::kMTransX] = 0;
125 noTrans[SkMatrix::kMTransY] = 0;
126 }
127 *translate = fOffset;
128 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500129 }
130};
131
Brian Salomond1ac9822017-02-03 14:25:02 -0500132/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500133struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500134 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400135 // The umbra cannot be dropped out because either the occluder is not opaque,
136 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500137 kTransparent,
138 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400139 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500140 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400141 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500142 };
143
Brian Salomon5e689522017-02-01 12:07:17 -0500144 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400145 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400146 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
147 SkPoint3 fDevLightPos;
148 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500149 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500150
Brian Salomond1ac9822017-02-03 14:25:02 -0500151 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400152 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400153 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500154 return false;
155 }
156 switch (fOccluderType) {
157 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400158 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500159 // 'this' and 'that' will either both have no umbra removed or both have all the
160 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400161 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500162 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400163 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500164 // In this case we partially remove the umbra differently for 'this' and 'that'
165 // if the offsets don't match.
166 if (fOffset == that.fOffset) {
167 translate->set(0, 0);
168 return true;
169 }
170 return false;
171 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400172 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500173 }
Brian Salomon5e689522017-02-01 12:07:17 -0500174
Jim Van Verth8793e382017-05-22 15:52:21 -0400175 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
176 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500177 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400178 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400179 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
180 translate->set(0, 0);
181 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
182 fDevLightPos, fLightRadius, transparent);
183 } else {
184 // pick a canonical place to generate shadow, with light centered over path
185 SkMatrix noTrans(ctm);
186 noTrans[SkMatrix::kMTransX] = 0;
187 noTrans[SkMatrix::kMTransY] = 0;
188 SkPoint devCenter(fLocalCenter);
189 noTrans.mapPoints(&devCenter, 1);
190 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
191 *translate = fOffset;
192 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
193 centerLightPos, fLightRadius, transparent);
194 }
Brian Salomon5e689522017-02-01 12:07:17 -0500195 }
196};
197
198/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500199 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
200 * 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 -0400201 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500202 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500203 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500204class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500205public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500206 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
207
Brian Salomonaff27a22017-02-06 15:47:44 -0500208 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
209 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500210 return fAmbientSet.find(ambient, matrix, translate);
211 }
212
Brian Salomonaff27a22017-02-06 15:47:44 -0500213 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400214 const SkMatrix& matrix, SkVector* translate) {
215 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500216 }
217
Brian Salomonaff27a22017-02-06 15:47:44 -0500218 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
219 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500220 return fSpotSet.find(spot, matrix, translate);
221 }
222
Brian Salomonaff27a22017-02-06 15:47:44 -0500223 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400224 const SkMatrix& matrix, SkVector* translate) {
225 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500226 }
227
228private:
229 template <typename FACTORY, int MAX_ENTRIES>
230 class Set {
231 public:
232 size_t size() const { return fSize; }
233
Brian Salomonaff27a22017-02-06 15:47:44 -0500234 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
235 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500236 for (int i = 0; i < MAX_ENTRIES; ++i) {
237 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
238 const SkMatrix& m = fEntries[i].fMatrix;
239 if (matrix.hasPerspective() || m.hasPerspective()) {
240 if (matrix != fEntries[i].fMatrix) {
241 continue;
242 }
243 } else if (matrix.getScaleX() != m.getScaleX() ||
244 matrix.getSkewX() != m.getSkewX() ||
245 matrix.getScaleY() != m.getScaleY() ||
246 matrix.getSkewY() != m.getSkewY()) {
247 continue;
248 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500249 return fEntries[i].fVertices;
250 }
251 }
252 return nullptr;
253 }
254
Jim Van Verth8793e382017-05-22 15:52:21 -0400255 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
256 SkVector* translate) {
257 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500258 if (!vertices) {
259 return nullptr;
260 }
261 int i;
262 if (fCount < MAX_ENTRIES) {
263 i = fCount++;
264 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400265 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400266 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500267 }
268 fEntries[i].fFactory = factory;
269 fEntries[i].fVertices = vertices;
270 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400271 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500272 return vertices;
273 }
274
275 private:
276 struct Entry {
277 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500278 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500279 SkMatrix fMatrix;
280 };
281 Entry fEntries[MAX_ENTRIES];
282 int fCount = 0;
283 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400284 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500285 };
286
287 Set<AmbientVerticesFactory, 4> fAmbientSet;
288 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500289};
290
Brian Salomond1ac9822017-02-03 14:25:02 -0500291/**
292 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
293 * path. The key represents the path's geometry and not any shadow params.
294 */
295class CachedTessellationsRec : public SkResourceCache::Rec {
296public:
297 CachedTessellationsRec(const SkResourceCache::Key& key,
298 sk_sp<CachedTessellations> tessellations)
299 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500300 fKey.reset(new uint8_t[key.size()]);
301 memcpy(fKey.get(), &key, key.size());
302 }
303
304 const Key& getKey() const override {
305 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
306 }
Brian Salomon5e689522017-02-01 12:07:17 -0500307
Brian Salomond1ac9822017-02-03 14:25:02 -0500308 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500309
Brian Salomond1ac9822017-02-03 14:25:02 -0500310 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500311
Brian Salomond1ac9822017-02-03 14:25:02 -0500312 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500313
Brian Salomond1ac9822017-02-03 14:25:02 -0500314 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500315 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
316 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500317 return fTessellations->find(factory, matrix, translate);
318 }
Brian Salomon5e689522017-02-01 12:07:17 -0500319
320private:
321 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500322 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500323};
324
325/**
326 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500327 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
328 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
329 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500330 */
331template <typename FACTORY>
332struct FindContext {
333 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
334 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500335 const SkMatrix* const fViewMatrix;
336 // If this is valid after Find is called then we found the vertices and they should be drawn
337 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500338 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500339 SkVector fTranslate = {0, 0};
340
341 // If this is valid after Find then the caller should add the vertices to the tessellation set
342 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
343 sk_sp<CachedTessellations> fTessellationsOnFailure;
344
Brian Salomon5e689522017-02-01 12:07:17 -0500345 const FACTORY* fFactory;
346};
347
348/**
349 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
350 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
351 * necessary translation vector are set on the FindContext.
352 */
353template <typename FACTORY>
354bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
355 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500356 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
357 findContext->fVertices =
358 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
359 if (findContext->fVertices) {
360 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500361 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500362 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
363 // manipulated we will add a new Rec.
364 findContext->fTessellationsOnFailure = rec.refTessellations();
365 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500366}
367
368class ShadowedPath {
369public:
370 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500371 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500372 , fViewMatrix(viewMatrix)
373#if SK_SUPPORT_GPU
374 , fShapeForKey(*path, GrStyle::SimpleFill())
375#endif
376 {}
377
Jim Van Vertha84898d2017-02-06 13:38:23 -0500378 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500379 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
380#if SK_SUPPORT_GPU
381 /** Negative means the vertices should not be cached for this path. */
382 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
383 void writeKey(void* key) const {
384 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
385 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500386 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500387#else
388 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400389 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500390 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500391#endif
392
393private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500394 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500395 const SkMatrix* fViewMatrix;
396#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000397 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500398#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500399};
400
Brian Salomond1ac9822017-02-03 14:25:02 -0500401// This creates a domain of keys in SkResourceCache used by this file.
402static void* kNamespace;
403
Jim Van Verthee90eb42019-04-26 12:07:13 -0400404// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500405class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400406public:
407 ShadowInvalidator(const SkResourceCache::Key& key) {
408 fKey.reset(new uint8_t[key.size()]);
409 memcpy(fKey.get(), &key, key.size());
410 }
411
412private:
413 const SkResourceCache::Key& getKey() const {
414 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
415 }
416
417 // always purge
418 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
419 return false;
420 }
421
Brian Salomon99a813c2020-03-02 12:50:47 -0500422 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400423 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
424 }
425
426 std::unique_ptr<uint8_t[]> fKey;
427};
428
Brian Salomon5e689522017-02-01 12:07:17 -0500429/**
430 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
431 * they are first found in SkResourceCache.
432 */
433template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500434bool draw_shadow(const FACTORY& factory,
435 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400436 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500437 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500438
439 SkResourceCache::Key* key = nullptr;
440 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
441 int keyDataBytes = path.keyBytes();
442 if (keyDataBytes >= 0) {
443 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
444 key = new (keyStorage.begin()) SkResourceCache::Key();
445 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500446 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400447 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500448 }
449
Brian Salomonaff27a22017-02-06 15:47:44 -0500450 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500451 bool foundInCache = SkToBool(context.fVertices);
452 if (foundInCache) {
453 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500454 } else {
455 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500456 if (key) {
457 // Update or initialize a tessellation set and add it to the cache.
458 sk_sp<CachedTessellations> tessellations;
459 if (context.fTessellationsOnFailure) {
460 tessellations = std::move(context.fTessellationsOnFailure);
461 } else {
462 tessellations.reset(new CachedTessellations());
463 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400464 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
465 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500466 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500467 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500468 }
Brian Salomon804e0912017-02-23 09:34:03 -0500469 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400470 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400471 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500472 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400473 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
474 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500475 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500476 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500477 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500478 }
Brian Salomon5e689522017-02-01 12:07:17 -0500479 }
480
481 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500482 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
483 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500484 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400485 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400486 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400487
Jim Van Verth8793e382017-05-22 15:52:21 -0400488 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400489 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500490
491 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500492}
493}
494
Mike Reed4204da22017-05-17 08:53:36 -0400495static bool tilted(const SkPoint3& zPlaneParams) {
496 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
497}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400498
Mike Reed4204da22017-05-17 08:53:36 -0400499static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
500 SkPoint3 result;
501 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
502 result.fZ = pt.fZ;
503 return result;
504}
505
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500506void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
507 SkColor* outAmbientColor, SkColor* outSpotColor) {
508 // For tonal color we only compute color values for the spot shadow.
509 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400510
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500511 // Ambient
512 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400513
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500514 // Spot
515 int spotR = SkColorGetR(inSpotColor);
516 int spotG = SkColorGetG(inSpotColor);
517 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500518 int max = std::max(std::max(spotR, spotG), spotB);
519 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500520 SkScalar luminance = 0.5f*(max + min)/255.f;
521 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
522
523 // We compute a color alpha value based on the luminance of the color, scaled by an
524 // adjusted alpha value. We want the following properties to match the UX examples
525 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
526 // is black and/or the alpha is 0:
527 // f(0, a) = 0
528 // f(luminance, 0) = 0
529 // f(1, 0.25) = .5
530 // f(0.5, 0.25) = .4
531 // f(1, 1) = 1
532 // The following functions match this as closely as possible.
533 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
534 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
535 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
536
537 // Similarly, we set the greyscale alpha based on luminance and alpha so that
538 // f(0, a) = a
539 // f(luminance, 0) = 0
540 // f(1, 0.25) = 0.15
541 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
542
543 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
544 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
545 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
546 // ignoring edge falloff, this becomes
547 //
548 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
549 //
550 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
551 // set the alpha to (S_a + C_a - S_a*C_a).
552 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
553 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
554 SkScalar unPremulScale = colorScale / tonalAlpha;
555 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
556 unPremulScale*spotR,
557 unPremulScale*spotG,
558 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400559}
560
Jim Van Verth43475ad2017-01-13 14:37:37 -0500561// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400562void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500563 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500564 SkColor ambientColor, SkColor spotColor,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400565 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400566 SkMatrix inverse;
567 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500568 return;
569 }
Mike Reed4204da22017-05-17 08:53:36 -0400570 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500571
Mike Reed4204da22017-05-17 08:53:36 -0400572 SkDrawShadowRec rec;
573 rec.fZPlaneParams = zPlaneParams;
574 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
575 rec.fLightRadius = lightRadius;
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500576 rec.fAmbientColor = ambientColor;
577 rec.fSpotColor = spotColor;
Mike Reed4204da22017-05-17 08:53:36 -0400578 rec.fFlags = flags;
579
580 canvas->private_draw_shadow_rec(path, rec);
581}
582
Jim Van Vertha947e292018-02-26 13:54:34 -0500583static bool validate_rec(const SkDrawShadowRec& rec) {
584 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
585 SkScalarIsFinite(rec.fLightRadius);
586}
587
Mike Reed4204da22017-05-17 08:53:36 -0400588void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
589 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400590 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400591 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400592 // For perspective shadows we've already computed the shadow in world space,
593 // and we can't translate it without changing it. Otherwise we concat the
594 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400595 SkAutoDeviceTransformRestore adr(
596 this,
597 hasPerspective ? SkMatrix::I()
Mike Reed1f607332020-05-21 12:11:27 -0400598 : this->localToDevice() * SkMatrix::Translate(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}