blob: 41e8180e75a12d9a2581cc257ff4424d2077afa7 [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
8#include "SkShadowUtils.h"
Mike Reed8e03f692018-03-09 16:18:56 -05009#include "SkBlurMask.h"
Jim Van Verth43475ad2017-01-13 14:37:37 -050010#include "SkCanvas.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050011#include "SkColorFilter.h"
Cary Clarka4083c92017-09-15 11:59:23 -040012#include "SkColorData.h"
Mike Reed4204da22017-05-17 08:53:36 -040013#include "SkDevice.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040014#include "SkDrawShadowInfo.h"
Mike Reed1386b2d2019-03-13 21:15:05 -040015#include "SkEffectPriv.h"
Mike Reed18e75562018-03-12 14:03:47 -040016#include "SkMaskFilter.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050017#include "SkPath.h"
Brian Salomond1ac9822017-02-03 14:25:02 -050018#include "SkRandom.h"
Mike Reed65331592017-05-24 16:45:34 -040019#include "SkRasterPipeline.h"
Brian Salomon5e689522017-02-01 12:07:17 -050020#include "SkResourceCache.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050021#include "SkShadowTessellator.h"
Ben Wagner4d1955c2017-03-10 13:08:15 -050022#include "SkString.h"
Brian Salomon5e689522017-02-01 12:07:17 -050023#include "SkTLazy.h"
Brian Salomonaff27a22017-02-06 15:47:44 -050024#include "SkVertices.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040025#include <new>
Brian Salomon5e689522017-02-01 12:07:17 -050026#if SK_SUPPORT_GPU
27#include "GrShape.h"
28#include "effects/GrBlurredEdgeFragmentProcessor.h"
Mike Reed4204da22017-05-17 08:53:36 -040029#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050030
31/**
32* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
33* then blends with the color's G value.
34* Final result is black with alpha of Gaussian(B)*G.
35* The assumption is that the original color's alpha is 1.
36*/
Mike Reed57c2b8b2017-12-31 15:23:54 -050037class SkGaussianColorFilter : public SkColorFilter {
Jim Van Verthefe3ded2017-01-30 13:11:45 -050038public:
39 static sk_sp<SkColorFilter> Make() {
40 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
41 }
42
Jim Van Verthefe3ded2017-01-30 13:11:45 -050043#if SK_SUPPORT_GPU
Brian Salomon4cbb6e62017-10-25 15:12:19 -040044 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
Robert Phillips1efecea2019-02-15 16:58:40 -050045 GrRecordingContext*, const GrColorSpaceInfo&) const override;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050046#endif
47
Jim Van Verthefe3ded2017-01-30 13:11:45 -050048protected:
49 void flatten(SkWriteBuffer&) const override {}
Mike Reed1386b2d2019-03-13 21:15:05 -040050 void onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
51 rec.fPipeline->append(SkRasterPipeline::gauss_a_to_rgba);
Mike Reed65331592017-05-24 16:45:34 -040052 }
Jim Van Verthefe3ded2017-01-30 13:11:45 -050053private:
Mike Klein4fee3232018-10-18 17:27:16 -040054 SK_FLATTENABLE_HOOKS(SkGaussianColorFilter)
55
Jim Van Verthefe3ded2017-01-30 13:11:45 -050056 SkGaussianColorFilter() : INHERITED() {}
57
58 typedef SkColorFilter INHERITED;
59};
60
Jim Van Verthefe3ded2017-01-30 13:11:45 -050061sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
62 return Make();
63}
64
Jim Van Verthefe3ded2017-01-30 13:11:45 -050065#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050066
Brian Salomonaff329b2017-08-11 09:40:37 -040067std::unique_ptr<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(
Robert Phillips1efecea2019-02-15 16:58:40 -050068 GrRecordingContext*, const GrColorSpaceInfo&) const {
Ethan Nicholasaae47c82017-11-10 15:34:03 -050069 return GrBlurredEdgeFragmentProcessor::Make(GrBlurredEdgeFragmentProcessor::Mode::kGaussian);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050070}
71#endif
72
73///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050074
75namespace {
76
Brian Salomonbc9956d2017-02-22 13:49:09 -050077uint64_t resource_cache_shared_id() {
78 return 0x2020776f64616873llu; // 'shadow '
79}
80
Brian Salomond1ac9822017-02-03 14:25:02 -050081/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -050082struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -040083 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -050084 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -040085 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -050086
Brian Salomond1ac9822017-02-03 14:25:02 -050087 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -040088 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -050089 return false;
90 }
Jim Van Verth8793e382017-05-22 15:52:21 -040091 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -050092 return true;
Brian Salomon5e689522017-02-01 12:07:17 -050093 }
Brian Salomon5e689522017-02-01 12:07:17 -050094
Jim Van Verth8793e382017-05-22 15:52:21 -040095 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
96 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -040097 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -040098 // pick a canonical place to generate shadow
99 SkMatrix noTrans(ctm);
100 if (!ctm.hasPerspective()) {
101 noTrans[SkMatrix::kMTransX] = 0;
102 noTrans[SkMatrix::kMTransY] = 0;
103 }
104 *translate = fOffset;
105 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500106 }
107};
108
Brian Salomond1ac9822017-02-03 14:25:02 -0500109/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500110struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500111 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400112 // The umbra cannot be dropped out because either the occluder is not opaque,
113 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500114 kTransparent,
115 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400116 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500117 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400118 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500119 };
120
Brian Salomon5e689522017-02-01 12:07:17 -0500121 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400122 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400123 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
124 SkPoint3 fDevLightPos;
125 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500126 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500127
Brian Salomond1ac9822017-02-03 14:25:02 -0500128 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400129 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400130 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500131 return false;
132 }
133 switch (fOccluderType) {
134 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400135 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500136 // 'this' and 'that' will either both have no umbra removed or both have all the
137 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400138 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500139 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400140 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500141 // In this case we partially remove the umbra differently for 'this' and 'that'
142 // if the offsets don't match.
143 if (fOffset == that.fOffset) {
144 translate->set(0, 0);
145 return true;
146 }
147 return false;
148 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400149 SK_ABORT("Uninitialized occluder type?");
Brian Salomond1ac9822017-02-03 14:25:02 -0500150 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500151 }
Brian Salomon5e689522017-02-01 12:07:17 -0500152
Jim Van Verth8793e382017-05-22 15:52:21 -0400153 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
154 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500155 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400156 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400157 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
158 translate->set(0, 0);
159 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
160 fDevLightPos, fLightRadius, transparent);
161 } else {
162 // pick a canonical place to generate shadow, with light centered over path
163 SkMatrix noTrans(ctm);
164 noTrans[SkMatrix::kMTransX] = 0;
165 noTrans[SkMatrix::kMTransY] = 0;
166 SkPoint devCenter(fLocalCenter);
167 noTrans.mapPoints(&devCenter, 1);
168 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
169 *translate = fOffset;
170 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
171 centerLightPos, fLightRadius, transparent);
172 }
Brian Salomon5e689522017-02-01 12:07:17 -0500173 }
174};
175
176/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500177 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
178 * 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 -0400179 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500180 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500181 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500182class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500183public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500184 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
185
Brian Salomonaff27a22017-02-06 15:47:44 -0500186 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
187 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500188 return fAmbientSet.find(ambient, matrix, translate);
189 }
190
Brian Salomonaff27a22017-02-06 15:47:44 -0500191 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400192 const SkMatrix& matrix, SkVector* translate) {
193 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500194 }
195
Brian Salomonaff27a22017-02-06 15:47:44 -0500196 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
197 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500198 return fSpotSet.find(spot, matrix, translate);
199 }
200
Brian Salomonaff27a22017-02-06 15:47:44 -0500201 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400202 const SkMatrix& matrix, SkVector* translate) {
203 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500204 }
205
206private:
207 template <typename FACTORY, int MAX_ENTRIES>
208 class Set {
209 public:
210 size_t size() const { return fSize; }
211
Brian Salomonaff27a22017-02-06 15:47:44 -0500212 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
213 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500214 for (int i = 0; i < MAX_ENTRIES; ++i) {
215 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
216 const SkMatrix& m = fEntries[i].fMatrix;
217 if (matrix.hasPerspective() || m.hasPerspective()) {
218 if (matrix != fEntries[i].fMatrix) {
219 continue;
220 }
221 } else if (matrix.getScaleX() != m.getScaleX() ||
222 matrix.getSkewX() != m.getSkewX() ||
223 matrix.getScaleY() != m.getScaleY() ||
224 matrix.getSkewY() != m.getSkewY()) {
225 continue;
226 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500227 return fEntries[i].fVertices;
228 }
229 }
230 return nullptr;
231 }
232
Jim Van Verth8793e382017-05-22 15:52:21 -0400233 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
234 SkVector* translate) {
235 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500236 if (!vertices) {
237 return nullptr;
238 }
239 int i;
240 if (fCount < MAX_ENTRIES) {
241 i = fCount++;
242 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400243 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400244 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500245 }
246 fEntries[i].fFactory = factory;
247 fEntries[i].fVertices = vertices;
248 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400249 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500250 return vertices;
251 }
252
253 private:
254 struct Entry {
255 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500256 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500257 SkMatrix fMatrix;
258 };
259 Entry fEntries[MAX_ENTRIES];
260 int fCount = 0;
261 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400262 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500263 };
264
265 Set<AmbientVerticesFactory, 4> fAmbientSet;
266 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500267};
268
Brian Salomond1ac9822017-02-03 14:25:02 -0500269/**
270 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
271 * path. The key represents the path's geometry and not any shadow params.
272 */
273class CachedTessellationsRec : public SkResourceCache::Rec {
274public:
275 CachedTessellationsRec(const SkResourceCache::Key& key,
276 sk_sp<CachedTessellations> tessellations)
277 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500278 fKey.reset(new uint8_t[key.size()]);
279 memcpy(fKey.get(), &key, key.size());
280 }
281
282 const Key& getKey() const override {
283 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
284 }
Brian Salomon5e689522017-02-01 12:07:17 -0500285
Brian Salomond1ac9822017-02-03 14:25:02 -0500286 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500287
Brian Salomond1ac9822017-02-03 14:25:02 -0500288 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500289
Brian Salomond1ac9822017-02-03 14:25:02 -0500290 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500291
Brian Salomond1ac9822017-02-03 14:25:02 -0500292 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500293 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
294 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500295 return fTessellations->find(factory, matrix, translate);
296 }
Brian Salomon5e689522017-02-01 12:07:17 -0500297
298private:
299 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500300 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500301};
302
303/**
304 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500305 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
306 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
307 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500308 */
309template <typename FACTORY>
310struct FindContext {
311 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
312 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500313 const SkMatrix* const fViewMatrix;
314 // If this is valid after Find is called then we found the vertices and they should be drawn
315 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500316 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500317 SkVector fTranslate = {0, 0};
318
319 // If this is valid after Find then the caller should add the vertices to the tessellation set
320 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
321 sk_sp<CachedTessellations> fTessellationsOnFailure;
322
Brian Salomon5e689522017-02-01 12:07:17 -0500323 const FACTORY* fFactory;
324};
325
326/**
327 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
328 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
329 * necessary translation vector are set on the FindContext.
330 */
331template <typename FACTORY>
332bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
333 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500334 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
335 findContext->fVertices =
336 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
337 if (findContext->fVertices) {
338 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500339 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500340 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
341 // manipulated we will add a new Rec.
342 findContext->fTessellationsOnFailure = rec.refTessellations();
343 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500344}
345
346class ShadowedPath {
347public:
348 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500349 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500350 , fViewMatrix(viewMatrix)
351#if SK_SUPPORT_GPU
352 , fShapeForKey(*path, GrStyle::SimpleFill())
353#endif
354 {}
355
Jim Van Vertha84898d2017-02-06 13:38:23 -0500356 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500357 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
358#if SK_SUPPORT_GPU
359 /** Negative means the vertices should not be cached for this path. */
360 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
361 void writeKey(void* key) const {
362 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
363 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500364 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500365#else
366 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400367 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500368 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500369#endif
370
371private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500372 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500373 const SkMatrix* fViewMatrix;
374#if SK_SUPPORT_GPU
375 GrShape fShapeForKey;
376#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500377};
378
Brian Salomond1ac9822017-02-03 14:25:02 -0500379// This creates a domain of keys in SkResourceCache used by this file.
380static void* kNamespace;
381
Brian Salomon5e689522017-02-01 12:07:17 -0500382/**
383 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
384 * they are first found in SkResourceCache.
385 */
386template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500387bool draw_shadow(const FACTORY& factory,
388 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400389 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500390 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500391
392 SkResourceCache::Key* key = nullptr;
393 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
394 int keyDataBytes = path.keyBytes();
395 if (keyDataBytes >= 0) {
396 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
397 key = new (keyStorage.begin()) SkResourceCache::Key();
398 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500399 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400400 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500401 }
402
Brian Salomonaff27a22017-02-06 15:47:44 -0500403 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500404 bool foundInCache = SkToBool(context.fVertices);
405 if (foundInCache) {
406 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500407 } else {
408 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500409 if (key) {
410 // Update or initialize a tessellation set and add it to the cache.
411 sk_sp<CachedTessellations> tessellations;
412 if (context.fTessellationsOnFailure) {
413 tessellations = std::move(context.fTessellationsOnFailure);
414 } else {
415 tessellations.reset(new CachedTessellations());
416 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400417 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
418 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500419 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500420 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500421 }
Brian Salomon804e0912017-02-23 09:34:03 -0500422 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400423 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500424 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400425 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
426 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500427 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500428 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500429 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500430 }
Brian Salomon5e689522017-02-01 12:07:17 -0500431 }
432
433 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500434 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
435 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500436 paint.setColorFilter(
437 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate)->makeComposed(
438 SkGaussianColorFilter::Make()));
Mike Reed4204da22017-05-17 08:53:36 -0400439
Jim Van Verth8793e382017-05-22 15:52:21 -0400440 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400441 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500442
443 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500444}
445}
446
Mike Reed4204da22017-05-17 08:53:36 -0400447static bool tilted(const SkPoint3& zPlaneParams) {
448 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
449}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400450
Mike Reed4204da22017-05-17 08:53:36 -0400451static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
452 SkPoint3 result;
453 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
454 result.fZ = pt.fZ;
455 return result;
456}
457
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500458void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
459 SkColor* outAmbientColor, SkColor* outSpotColor) {
460 // For tonal color we only compute color values for the spot shadow.
461 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400462
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500463 // Ambient
464 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400465
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500466 // Spot
467 int spotR = SkColorGetR(inSpotColor);
468 int spotG = SkColorGetG(inSpotColor);
469 int spotB = SkColorGetB(inSpotColor);
470 int max = SkTMax(SkTMax(spotR, spotG), spotB);
471 int min = SkTMin(SkTMin(spotR, spotG), spotB);
472 SkScalar luminance = 0.5f*(max + min)/255.f;
473 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
474
475 // We compute a color alpha value based on the luminance of the color, scaled by an
476 // adjusted alpha value. We want the following properties to match the UX examples
477 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
478 // is black and/or the alpha is 0:
479 // f(0, a) = 0
480 // f(luminance, 0) = 0
481 // f(1, 0.25) = .5
482 // f(0.5, 0.25) = .4
483 // f(1, 1) = 1
484 // The following functions match this as closely as possible.
485 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
486 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
487 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
488
489 // Similarly, we set the greyscale alpha based on luminance and alpha so that
490 // f(0, a) = a
491 // f(luminance, 0) = 0
492 // f(1, 0.25) = 0.15
493 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
494
495 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
496 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
497 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
498 // ignoring edge falloff, this becomes
499 //
500 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
501 //
502 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
503 // set the alpha to (S_a + C_a - S_a*C_a).
504 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
505 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
506 SkScalar unPremulScale = colorScale / tonalAlpha;
507 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
508 unPremulScale*spotR,
509 unPremulScale*spotG,
510 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400511}
512
Jim Van Verth43475ad2017-01-13 14:37:37 -0500513// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400514void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500515 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500516 SkColor ambientColor, SkColor spotColor,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400517 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400518 SkMatrix inverse;
519 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500520 return;
521 }
Mike Reed4204da22017-05-17 08:53:36 -0400522 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500523
Mike Reed4204da22017-05-17 08:53:36 -0400524 SkDrawShadowRec rec;
525 rec.fZPlaneParams = zPlaneParams;
526 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
527 rec.fLightRadius = lightRadius;
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500528 rec.fAmbientColor = ambientColor;
529 rec.fSpotColor = spotColor;
Mike Reed4204da22017-05-17 08:53:36 -0400530 rec.fFlags = flags;
531
532 canvas->private_draw_shadow_rec(path, rec);
533}
534
Jim Van Vertha947e292018-02-26 13:54:34 -0500535static bool validate_rec(const SkDrawShadowRec& rec) {
536 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
537 SkScalarIsFinite(rec.fLightRadius);
538}
539
Mike Reed4204da22017-05-17 08:53:36 -0400540void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
541 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400542 SkScalar tx, SkScalar ty, bool hasPerspective) {
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400543 if (vertices->vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400544 // For perspective shadows we've already computed the shadow in world space,
545 // and we can't translate it without changing it. Otherwise we concat the
546 // change in translation from the cached version.
547 SkAutoDeviceCTMRestore adr(
548 this,
549 hasPerspective ? SkMatrix::I()
550 : SkMatrix::Concat(this->ctm(), SkMatrix::MakeTrans(tx, ty)));
Ruiqi Maof5101492018-06-29 14:32:21 -0400551 this->drawVertices(vertices, nullptr, 0, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400552 }
Mike Reed4204da22017-05-17 08:53:36 -0400553 };
554
Jim Van Vertha947e292018-02-26 13:54:34 -0500555 if (!validate_rec(rec)) {
556 return;
557 }
558
Mike Reed4204da22017-05-17 08:53:36 -0400559 SkMatrix viewMatrix = this->ctm();
560 SkAutoDeviceCTMRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500561
Brian Salomon5e689522017-02-01 12:07:17 -0500562 ShadowedPath shadowedPath(&path, &viewMatrix);
563
Mike Reed4204da22017-05-17 08:53:36 -0400564 bool tiltZPlane = tilted(rec.fZPlaneParams);
565 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400566 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500567
Mike Reed4204da22017-05-17 08:53:36 -0400568 SkPoint3 zPlaneParams = rec.fZPlaneParams;
569 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
570 float lightRadius = rec.fLightRadius;
571
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500572 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500573 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400574 if (uncached) {
575 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
576 zPlaneParams,
577 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400578 if (vertices) {
579 SkPaint paint;
580 // Run the vertex color through a GaussianColorFilter and then modulate the
581 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500582 paint.setColorFilter(
583 SkColorFilter::MakeModeFilter(rec.fAmbientColor,
584 SkBlendMode::kModulate)->makeComposed(
585 SkGaussianColorFilter::Make()));
Ruiqi Maof5101492018-06-29 14:32:21 -0400586 this->drawVertices(vertices.get(), nullptr, 0, SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500587 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400588 }
Jim Van Verth22526362018-02-28 14:51:19 -0500589 }
590
591 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400592 AmbientVerticesFactory factory;
593 factory.fOccluderHeight = zPlaneParams.fZ;
594 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400595 if (viewMatrix.hasPerspective()) {
596 factory.fOffset.set(0, 0);
597 } else {
598 factory.fOffset.fX = viewMatrix.getTranslateX();
599 factory.fOffset.fY = viewMatrix.getTranslateY();
600 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400601
Jim Van Verth22526362018-02-28 14:51:19 -0500602 if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) {
603 // Pretransform the path to avoid transforming the stroke, below.
604 SkPath devSpacePath;
605 path.transform(viewMatrix, &devSpacePath);
Robert Phillipsed3dbf42019-03-18 12:20:15 -0400606 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500607
608 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400609 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500610 //
611 // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f')
612 // can be calculated by interpolating:
613 //
614 // original edge outer edge
615 // | |<---------- r ------>|
616 // |<------|--- f -------------->|
617 // | | |
618 // alpha = 1 alpha = a alpha = 0
619 //
620 // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2.
621 //
622 // We now need to outset the path to place the new edge in the center of the
623 // blur region:
624 //
625 // original new
626 // | |<------|--- r ------>|
627 // |<------|--- f -|------------>|
628 // | |<- o ->|<--- f/2 --->|
629 //
630 // r = o + f/2, so o = r - f/2
631 //
632 // We outset by using the stroker, so the strokeWidth is o/2.
633 //
634 SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ);
635 SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ);
636 SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA;
637 SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius);
638
639 // Now draw with blur
640 SkPaint paint;
641 paint.setColor(rec.fAmbientColor);
642 paint.setStrokeWidth(strokeWidth);
643 paint.setStyle(SkPaint::kStrokeAndFill_Style);
Mike Reed8e03f692018-03-09 16:18:56 -0500644 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400645 bool respectCTM = false;
646 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500647 this->drawPath(devSpacePath, paint);
648 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500649 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400650 }
651
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500652 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500653 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400654 if (uncached) {
655 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
656 zPlaneParams,
657 devLightPos, lightRadius,
658 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400659 if (vertices) {
660 SkPaint paint;
661 // Run the vertex color through a GaussianColorFilter and then modulate the
662 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500663 paint.setColorFilter(
664 SkColorFilter::MakeModeFilter(rec.fSpotColor,
665 SkBlendMode::kModulate)->makeComposed(
Jim Van Verth22526362018-02-28 14:51:19 -0500666 SkGaussianColorFilter::Make()));
Ruiqi Maof5101492018-06-29 14:32:21 -0400667 this->drawVertices(vertices.get(), nullptr, 0, SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500668 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400669 }
Jim Van Verth22526362018-02-28 14:51:19 -0500670 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400671
Jim Van Verth22526362018-02-28 14:51:19 -0500672 if (!success) {
673 SpotVerticesFactory factory;
674 factory.fOccluderHeight = zPlaneParams.fZ;
675 factory.fDevLightPos = devLightPos;
676 factory.fLightRadius = lightRadius;
677
Jim Van Verth37c5a962017-05-10 14:13:24 -0400678 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400679 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400680 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500681 SkScalar radius, scale;
682 SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX,
683 devLightPos.fY - center.fY, devLightPos.fZ,
684 lightRadius, &radius, &scale, &factory.fOffset);
Jim Van Vertha783c362017-05-11 17:05:28 -0400685 SkRect devBounds;
686 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth8793e382017-05-22 15:52:21 -0400687 if (transparent ||
688 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
689 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400690 // if the translation of the shadow is big enough we're going to end up
691 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400692 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400693 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400694 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400695 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400696 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400697 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400698 } else {
699 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400700 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400701 // need to add this after we classify the shadow
702 factory.fOffset.fX += viewMatrix.getTranslateX();
703 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500704
705 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400706#ifdef DEBUG_SHADOW_CHECKS
707 switch (factory.fOccluderType) {
708 case SpotVerticesFactory::OccluderType::kTransparent:
709 color = 0xFFD2B48C; // tan for transparent
710 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400711 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400712 color = 0xFFFFA500; // orange for opaque
713 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400714 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
715 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400716 break;
717 }
718#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500719 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
720 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500721 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400722 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
723 viewMatrix, zPlaneParams,
724 path.getBounds(),
725 &shadowMatrix, &radius)) {
726 return;
727 }
728 SkAutoDeviceCTMRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500729
730 SkPaint paint;
731 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500732 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400733 bool respectCTM = false;
734 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500735 this->drawPath(path, paint);
736 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400737 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400738 }
739}