blob: 6ef6781eee0162a83e1abfdc6f0d4834e9355736 [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 Reedb11e6272020-06-24 16:56:33 -040020#include "src/core/SkColorFilterBase.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040021#include "src/core/SkColorFilterPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkDevice.h"
23#include "src/core/SkDrawShadowInfo.h"
24#include "src/core/SkEffectPriv.h"
Jim Van Verthee90eb42019-04-26 12:07:13 -040025#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/core/SkRasterPipeline.h"
27#include "src/core/SkResourceCache.h"
28#include "src/core/SkTLazy.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040029#include "src/core/SkVM.h"
Mike Reedba962562020-03-12 20:33:21 -040030#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/utils/SkShadowTessellator.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040032#include <new>
Brian Salomon5e689522017-02-01 12:07:17 -050033#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/effects/generated/GrBlurredEdgeFragmentProcessor.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000035#include "src/gpu/geometry/GrStyledShape.h"
Mike Reed4204da22017-05-17 08:53:36 -040036#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050037
38/**
39* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
40* then blends with the color's G value.
41* Final result is black with alpha of Gaussian(B)*G.
42* The assumption is that the original color's alpha is 1.
43*/
Mike Reedb11e6272020-06-24 16:56:33 -040044class SkGaussianColorFilter : public SkColorFilterBase {
Jim Van Verthefe3ded2017-01-30 13:11:45 -050045public:
Mike Reedf36b37f2020-03-27 15:11:10 -040046 SkGaussianColorFilter() : INHERITED() {}
Jim Van Verthefe3ded2017-01-30 13:11:45 -050047
Jim Van Verthefe3ded2017-01-30 13:11:45 -050048#if SK_SUPPORT_GPU
John Stiles43206642020-06-29 12:03:26 -040049 GrFPResult asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
50 GrRecordingContext*, const GrColorInfo&) const override;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050051#endif
52
Mike Klein40f91382019-08-01 21:07:29 +000053protected:
Jim Van Verthefe3ded2017-01-30 13:11:45 -050054 void flatten(SkWriteBuffer&) const override {}
Mike Klein40f91382019-08-01 21:07:29 +000055 bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
Mike Reed1386b2d2019-03-13 21:15:05 -040056 rec.fPipeline->append(SkRasterPipeline::gauss_a_to_rgba);
Mike Reed2fdbeae2019-03-30 14:27:53 -040057 return true;
Mike Reed65331592017-05-24 16:45:34 -040058 }
Mike Reedf36b37f2020-03-27 15:11:10 -040059
60 skvm::Color onProgram(skvm::Builder* p, skvm::Color c, SkColorSpace* dstCS, skvm::Uniforms*,
61 SkArenaAlloc*) const override {
62 // x = 1 - x;
63 // exp(-x * x * 4) - 0.018f;
64 // ... now approximate with quartic
65 //
Mike Reedf3b9a302020-04-01 13:18:02 -040066 skvm::F32 x = p->splat(-2.26661229133605957031f);
67 x = c.a * x + 2.89795351028442382812f;
68 x = c.a * x + 0.21345567703247070312f;
69 x = c.a * x + 0.15489584207534790039f;
70 x = c.a * x + 0.00030726194381713867f;
Mike Reedf36b37f2020-03-27 15:11:10 -040071 return {x, x, x, x};
72 }
73
Mike Klein40f91382019-08-01 21:07:29 +000074private:
Mike Klein4fee3232018-10-18 17:27:16 -040075 SK_FLATTENABLE_HOOKS(SkGaussianColorFilter)
76
Mike Reedb11e6272020-06-24 16:56:33 -040077 typedef SkColorFilterBase INHERITED;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050078};
79
Jim Van Verthefe3ded2017-01-30 13:11:45 -050080sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
Mike Reedf36b37f2020-03-27 15:11:10 -040081 return SkColorFilterPriv::MakeGaussian();
Jim Van Verthefe3ded2017-01-30 13:11:45 -050082}
83
Jim Van Verthefe3ded2017-01-30 13:11:45 -050084#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050085
John Stiles43206642020-06-29 12:03:26 -040086GrFPResult SkGaussianColorFilter::asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
87 GrRecordingContext*,
88 const GrColorInfo&) const {
89 return GrFPSuccess(GrBlurredEdgeFragmentProcessor::Make(
90 std::move(inputFP), GrBlurredEdgeFragmentProcessor::Mode::kGaussian));
Jim Van Verthefe3ded2017-01-30 13:11:45 -050091}
92#endif
93
Mike Reedf36b37f2020-03-27 15:11:10 -040094sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
95 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
96}
97
Jim Van Verthefe3ded2017-01-30 13:11:45 -050098///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050099
100namespace {
101
Brian Salomonbc9956d2017-02-22 13:49:09 -0500102uint64_t resource_cache_shared_id() {
103 return 0x2020776f64616873llu; // 'shadow '
104}
105
Brian Salomond1ac9822017-02-03 14:25:02 -0500106/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500107struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400108 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500109 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400110 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500111
Brian Salomond1ac9822017-02-03 14:25:02 -0500112 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400113 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500114 return false;
115 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400116 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500117 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500118 }
Brian Salomon5e689522017-02-01 12:07:17 -0500119
Jim Van Verth8793e382017-05-22 15:52:21 -0400120 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
121 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400122 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400123 // pick a canonical place to generate shadow
124 SkMatrix noTrans(ctm);
125 if (!ctm.hasPerspective()) {
126 noTrans[SkMatrix::kMTransX] = 0;
127 noTrans[SkMatrix::kMTransY] = 0;
128 }
129 *translate = fOffset;
130 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500131 }
132};
133
Brian Salomond1ac9822017-02-03 14:25:02 -0500134/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500135struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500136 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400137 // The umbra cannot be dropped out because either the occluder is not opaque,
138 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500139 kTransparent,
140 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400141 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500142 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400143 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500144 };
145
Brian Salomon5e689522017-02-01 12:07:17 -0500146 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400147 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400148 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
149 SkPoint3 fDevLightPos;
150 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500151 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500152
Brian Salomond1ac9822017-02-03 14:25:02 -0500153 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400154 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400155 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500156 return false;
157 }
158 switch (fOccluderType) {
159 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400160 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500161 // 'this' and 'that' will either both have no umbra removed or both have all the
162 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400163 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500164 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400165 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500166 // In this case we partially remove the umbra differently for 'this' and 'that'
167 // if the offsets don't match.
168 if (fOffset == that.fOffset) {
169 translate->set(0, 0);
170 return true;
171 }
172 return false;
173 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400174 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500175 }
Brian Salomon5e689522017-02-01 12:07:17 -0500176
Jim Van Verth8793e382017-05-22 15:52:21 -0400177 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
178 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500179 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400180 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400181 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
182 translate->set(0, 0);
183 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
184 fDevLightPos, fLightRadius, transparent);
185 } else {
186 // pick a canonical place to generate shadow, with light centered over path
187 SkMatrix noTrans(ctm);
188 noTrans[SkMatrix::kMTransX] = 0;
189 noTrans[SkMatrix::kMTransY] = 0;
190 SkPoint devCenter(fLocalCenter);
191 noTrans.mapPoints(&devCenter, 1);
192 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
193 *translate = fOffset;
194 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
195 centerLightPos, fLightRadius, transparent);
196 }
Brian Salomon5e689522017-02-01 12:07:17 -0500197 }
198};
199
200/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500201 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
202 * 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 -0400203 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500204 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500205 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500206class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500207public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500208 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
209
Brian Salomonaff27a22017-02-06 15:47:44 -0500210 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
211 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500212 return fAmbientSet.find(ambient, matrix, translate);
213 }
214
Brian Salomonaff27a22017-02-06 15:47:44 -0500215 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400216 const SkMatrix& matrix, SkVector* translate) {
217 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500218 }
219
Brian Salomonaff27a22017-02-06 15:47:44 -0500220 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
221 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500222 return fSpotSet.find(spot, matrix, translate);
223 }
224
Brian Salomonaff27a22017-02-06 15:47:44 -0500225 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400226 const SkMatrix& matrix, SkVector* translate) {
227 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500228 }
229
230private:
231 template <typename FACTORY, int MAX_ENTRIES>
232 class Set {
233 public:
234 size_t size() const { return fSize; }
235
Brian Salomonaff27a22017-02-06 15:47:44 -0500236 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
237 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500238 for (int i = 0; i < MAX_ENTRIES; ++i) {
239 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
240 const SkMatrix& m = fEntries[i].fMatrix;
241 if (matrix.hasPerspective() || m.hasPerspective()) {
242 if (matrix != fEntries[i].fMatrix) {
243 continue;
244 }
245 } else if (matrix.getScaleX() != m.getScaleX() ||
246 matrix.getSkewX() != m.getSkewX() ||
247 matrix.getScaleY() != m.getScaleY() ||
248 matrix.getSkewY() != m.getSkewY()) {
249 continue;
250 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500251 return fEntries[i].fVertices;
252 }
253 }
254 return nullptr;
255 }
256
Jim Van Verth8793e382017-05-22 15:52:21 -0400257 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
258 SkVector* translate) {
259 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500260 if (!vertices) {
261 return nullptr;
262 }
263 int i;
264 if (fCount < MAX_ENTRIES) {
265 i = fCount++;
266 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400267 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400268 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500269 }
270 fEntries[i].fFactory = factory;
271 fEntries[i].fVertices = vertices;
272 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400273 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500274 return vertices;
275 }
276
277 private:
278 struct Entry {
279 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500280 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500281 SkMatrix fMatrix;
282 };
283 Entry fEntries[MAX_ENTRIES];
284 int fCount = 0;
285 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400286 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500287 };
288
289 Set<AmbientVerticesFactory, 4> fAmbientSet;
290 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500291};
292
Brian Salomond1ac9822017-02-03 14:25:02 -0500293/**
294 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
295 * path. The key represents the path's geometry and not any shadow params.
296 */
297class CachedTessellationsRec : public SkResourceCache::Rec {
298public:
299 CachedTessellationsRec(const SkResourceCache::Key& key,
300 sk_sp<CachedTessellations> tessellations)
301 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500302 fKey.reset(new uint8_t[key.size()]);
303 memcpy(fKey.get(), &key, key.size());
304 }
305
306 const Key& getKey() const override {
307 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
308 }
Brian Salomon5e689522017-02-01 12:07:17 -0500309
Brian Salomond1ac9822017-02-03 14:25:02 -0500310 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500311
Brian Salomond1ac9822017-02-03 14:25:02 -0500312 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500313
Brian Salomond1ac9822017-02-03 14:25:02 -0500314 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500315
Brian Salomond1ac9822017-02-03 14:25:02 -0500316 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500317 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
318 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500319 return fTessellations->find(factory, matrix, translate);
320 }
Brian Salomon5e689522017-02-01 12:07:17 -0500321
322private:
323 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500324 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500325};
326
327/**
328 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500329 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
330 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
331 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500332 */
333template <typename FACTORY>
334struct FindContext {
335 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
336 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500337 const SkMatrix* const fViewMatrix;
338 // If this is valid after Find is called then we found the vertices and they should be drawn
339 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500340 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500341 SkVector fTranslate = {0, 0};
342
343 // If this is valid after Find then the caller should add the vertices to the tessellation set
344 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
345 sk_sp<CachedTessellations> fTessellationsOnFailure;
346
Brian Salomon5e689522017-02-01 12:07:17 -0500347 const FACTORY* fFactory;
348};
349
350/**
351 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
352 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
353 * necessary translation vector are set on the FindContext.
354 */
355template <typename FACTORY>
356bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
357 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500358 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
359 findContext->fVertices =
360 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
361 if (findContext->fVertices) {
362 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500363 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500364 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
365 // manipulated we will add a new Rec.
366 findContext->fTessellationsOnFailure = rec.refTessellations();
367 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500368}
369
370class ShadowedPath {
371public:
372 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500373 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500374 , fViewMatrix(viewMatrix)
375#if SK_SUPPORT_GPU
376 , fShapeForKey(*path, GrStyle::SimpleFill())
377#endif
378 {}
379
Jim Van Vertha84898d2017-02-06 13:38:23 -0500380 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500381 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
382#if SK_SUPPORT_GPU
383 /** Negative means the vertices should not be cached for this path. */
384 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
385 void writeKey(void* key) const {
386 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
387 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500388 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500389#else
390 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400391 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500392 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500393#endif
394
395private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500396 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500397 const SkMatrix* fViewMatrix;
398#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000399 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500400#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500401};
402
Brian Salomond1ac9822017-02-03 14:25:02 -0500403// This creates a domain of keys in SkResourceCache used by this file.
404static void* kNamespace;
405
Jim Van Verthee90eb42019-04-26 12:07:13 -0400406// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500407class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400408public:
409 ShadowInvalidator(const SkResourceCache::Key& key) {
410 fKey.reset(new uint8_t[key.size()]);
411 memcpy(fKey.get(), &key, key.size());
412 }
413
414private:
415 const SkResourceCache::Key& getKey() const {
416 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
417 }
418
419 // always purge
420 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
421 return false;
422 }
423
Brian Salomon99a813c2020-03-02 12:50:47 -0500424 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400425 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
426 }
427
428 std::unique_ptr<uint8_t[]> fKey;
429};
430
Brian Salomon5e689522017-02-01 12:07:17 -0500431/**
432 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
433 * they are first found in SkResourceCache.
434 */
435template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500436bool draw_shadow(const FACTORY& factory,
437 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400438 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500439 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500440
441 SkResourceCache::Key* key = nullptr;
442 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
443 int keyDataBytes = path.keyBytes();
444 if (keyDataBytes >= 0) {
445 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
446 key = new (keyStorage.begin()) SkResourceCache::Key();
447 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500448 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400449 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500450 }
451
Brian Salomonaff27a22017-02-06 15:47:44 -0500452 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500453 bool foundInCache = SkToBool(context.fVertices);
454 if (foundInCache) {
455 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500456 } else {
457 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500458 if (key) {
459 // Update or initialize a tessellation set and add it to the cache.
460 sk_sp<CachedTessellations> tessellations;
461 if (context.fTessellationsOnFailure) {
462 tessellations = std::move(context.fTessellationsOnFailure);
463 } else {
464 tessellations.reset(new CachedTessellations());
465 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400466 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
467 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500468 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500469 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500470 }
Brian Salomon804e0912017-02-23 09:34:03 -0500471 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400472 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400473 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500474 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400475 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
476 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500477 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500478 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500479 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500480 }
Brian Salomon5e689522017-02-01 12:07:17 -0500481 }
482
483 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500484 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
485 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500486 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400487 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400488 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400489
Jim Van Verth8793e382017-05-22 15:52:21 -0400490 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400491 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500492
493 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500494}
John Stilesa6841be2020-08-06 14:11:56 -0400495} // namespace
Brian Salomon5e689522017-02-01 12:07:17 -0500496
Mike Reed4204da22017-05-17 08:53:36 -0400497static bool tilted(const SkPoint3& zPlaneParams) {
498 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
499}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400500
Mike Reed4204da22017-05-17 08:53:36 -0400501static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
502 SkPoint3 result;
503 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
504 result.fZ = pt.fZ;
505 return result;
506}
507
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500508void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
509 SkColor* outAmbientColor, SkColor* outSpotColor) {
510 // For tonal color we only compute color values for the spot shadow.
511 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400512
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500513 // Ambient
514 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400515
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500516 // Spot
517 int spotR = SkColorGetR(inSpotColor);
518 int spotG = SkColorGetG(inSpotColor);
519 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500520 int max = std::max(std::max(spotR, spotG), spotB);
521 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500522 SkScalar luminance = 0.5f*(max + min)/255.f;
523 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
524
525 // We compute a color alpha value based on the luminance of the color, scaled by an
526 // adjusted alpha value. We want the following properties to match the UX examples
527 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
528 // is black and/or the alpha is 0:
529 // f(0, a) = 0
530 // f(luminance, 0) = 0
531 // f(1, 0.25) = .5
532 // f(0.5, 0.25) = .4
533 // f(1, 1) = 1
534 // The following functions match this as closely as possible.
535 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
536 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
537 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
538
539 // Similarly, we set the greyscale alpha based on luminance and alpha so that
540 // f(0, a) = a
541 // f(luminance, 0) = 0
542 // f(1, 0.25) = 0.15
543 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
544
545 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
546 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
547 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
548 // ignoring edge falloff, this becomes
549 //
550 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
551 //
552 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
553 // set the alpha to (S_a + C_a - S_a*C_a).
554 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
555 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
556 SkScalar unPremulScale = colorScale / tonalAlpha;
557 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
558 unPremulScale*spotR,
559 unPremulScale*spotG,
560 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400561}
562
Jim Van Verth43475ad2017-01-13 14:37:37 -0500563// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400564void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500565 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500566 SkColor ambientColor, SkColor spotColor,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400567 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400568 SkMatrix inverse;
569 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500570 return;
571 }
Mike Reed4204da22017-05-17 08:53:36 -0400572 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500573
Mike Reed4204da22017-05-17 08:53:36 -0400574 SkDrawShadowRec rec;
575 rec.fZPlaneParams = zPlaneParams;
576 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
577 rec.fLightRadius = lightRadius;
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500578 rec.fAmbientColor = ambientColor;
579 rec.fSpotColor = spotColor;
Mike Reed4204da22017-05-17 08:53:36 -0400580 rec.fFlags = flags;
581
582 canvas->private_draw_shadow_rec(path, rec);
583}
584
Jim Van Vertha947e292018-02-26 13:54:34 -0500585static bool validate_rec(const SkDrawShadowRec& rec) {
586 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
587 SkScalarIsFinite(rec.fLightRadius);
588}
589
Mike Reed4204da22017-05-17 08:53:36 -0400590void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
591 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400592 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400593 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400594 // For perspective shadows we've already computed the shadow in world space,
595 // and we can't translate it without changing it. Otherwise we concat the
596 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400597 SkAutoDeviceTransformRestore adr(
598 this,
599 hasPerspective ? SkMatrix::I()
Mike Reed1f607332020-05-21 12:11:27 -0400600 : this->localToDevice() * SkMatrix::Translate(tx, ty));
Mike Reed5caf9352020-03-02 14:57:09 -0500601 this->drawVertices(vertices, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400602 }
Mike Reed4204da22017-05-17 08:53:36 -0400603 };
604
Jim Van Vertha947e292018-02-26 13:54:34 -0500605 if (!validate_rec(rec)) {
606 return;
607 }
608
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400609 SkMatrix viewMatrix = this->localToDevice();
610 SkAutoDeviceTransformRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500611
Brian Salomon5e689522017-02-01 12:07:17 -0500612 ShadowedPath shadowedPath(&path, &viewMatrix);
613
Mike Reed4204da22017-05-17 08:53:36 -0400614 bool tiltZPlane = tilted(rec.fZPlaneParams);
615 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400616 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500617
Mike Reed4204da22017-05-17 08:53:36 -0400618 SkPoint3 zPlaneParams = rec.fZPlaneParams;
619 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
620 float lightRadius = rec.fLightRadius;
621
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500622 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500623 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400624 if (uncached) {
625 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
626 zPlaneParams,
627 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400628 if (vertices) {
629 SkPaint paint;
630 // Run the vertex color through a GaussianColorFilter and then modulate the
631 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500632 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400633 SkColorFilters::Blend(rec.fAmbientColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500634 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400635 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500636 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500637 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400638 }
Jim Van Verth22526362018-02-28 14:51:19 -0500639 }
640
641 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400642 AmbientVerticesFactory factory;
643 factory.fOccluderHeight = zPlaneParams.fZ;
644 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400645 if (viewMatrix.hasPerspective()) {
646 factory.fOffset.set(0, 0);
647 } else {
648 factory.fOffset.fX = viewMatrix.getTranslateX();
649 factory.fOffset.fY = viewMatrix.getTranslateY();
650 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400651
Jim Van Verth22526362018-02-28 14:51:19 -0500652 if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) {
653 // Pretransform the path to avoid transforming the stroke, below.
654 SkPath devSpacePath;
655 path.transform(viewMatrix, &devSpacePath);
Robert Phillipsed3dbf42019-03-18 12:20:15 -0400656 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500657
658 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400659 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500660 //
661 // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f')
662 // can be calculated by interpolating:
663 //
664 // original edge outer edge
665 // | |<---------- r ------>|
666 // |<------|--- f -------------->|
667 // | | |
668 // alpha = 1 alpha = a alpha = 0
669 //
670 // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2.
671 //
672 // We now need to outset the path to place the new edge in the center of the
673 // blur region:
674 //
675 // original new
676 // | |<------|--- r ------>|
677 // |<------|--- f -|------------>|
678 // | |<- o ->|<--- f/2 --->|
679 //
680 // r = o + f/2, so o = r - f/2
681 //
682 // We outset by using the stroker, so the strokeWidth is o/2.
683 //
684 SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ);
685 SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ);
686 SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA;
687 SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius);
688
689 // Now draw with blur
690 SkPaint paint;
691 paint.setColor(rec.fAmbientColor);
692 paint.setStrokeWidth(strokeWidth);
693 paint.setStyle(SkPaint::kStrokeAndFill_Style);
Mike Reed8e03f692018-03-09 16:18:56 -0500694 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400695 bool respectCTM = false;
696 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500697 this->drawPath(devSpacePath, paint);
698 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500699 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400700 }
701
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500702 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500703 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400704 if (uncached) {
705 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
706 zPlaneParams,
707 devLightPos, lightRadius,
708 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400709 if (vertices) {
710 SkPaint paint;
711 // Run the vertex color through a GaussianColorFilter and then modulate the
712 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500713 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400714 SkColorFilters::Blend(rec.fSpotColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500715 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400716 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500717 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500718 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400719 }
Jim Van Verth22526362018-02-28 14:51:19 -0500720 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400721
Jim Van Verth22526362018-02-28 14:51:19 -0500722 if (!success) {
723 SpotVerticesFactory factory;
724 factory.fOccluderHeight = zPlaneParams.fZ;
725 factory.fDevLightPos = devLightPos;
726 factory.fLightRadius = lightRadius;
727
Jim Van Verth37c5a962017-05-10 14:13:24 -0400728 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400729 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400730 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500731 SkScalar radius, scale;
732 SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX,
733 devLightPos.fY - center.fY, devLightPos.fZ,
734 lightRadius, &radius, &scale, &factory.fOffset);
Jim Van Vertha783c362017-05-11 17:05:28 -0400735 SkRect devBounds;
736 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth8793e382017-05-22 15:52:21 -0400737 if (transparent ||
738 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
739 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400740 // if the translation of the shadow is big enough we're going to end up
741 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400742 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400743 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400744 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400745 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400746 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400747 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400748 } else {
749 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400750 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400751 // need to add this after we classify the shadow
752 factory.fOffset.fX += viewMatrix.getTranslateX();
753 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500754
755 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400756#ifdef DEBUG_SHADOW_CHECKS
757 switch (factory.fOccluderType) {
758 case SpotVerticesFactory::OccluderType::kTransparent:
759 color = 0xFFD2B48C; // tan for transparent
760 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400761 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400762 color = 0xFFFFA500; // orange for opaque
763 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400764 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
765 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400766 break;
767 }
768#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500769 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
770 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500771 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400772 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
773 viewMatrix, zPlaneParams,
774 path.getBounds(),
775 &shadowMatrix, &radius)) {
776 return;
777 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400778 SkAutoDeviceTransformRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500779
780 SkPaint paint;
781 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500782 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400783 bool respectCTM = false;
784 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500785 this->drawPath(path, paint);
786 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400787 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400788 }
789}