blob: 4691b3fef269d5815d5e23a49a6c9aa25a187374 [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
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040049 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrRecordingContext*,
50 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
Brian Salomonaff329b2017-08-11 09:40:37 -040086std::unique_ptr<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040087 GrRecordingContext*, const GrColorInfo&) const {
John Stilescdc39bd2020-06-04 16:14:13 -040088 return GrBlurredEdgeFragmentProcessor::Make(
89 /*inputFP=*/nullptr, GrBlurredEdgeFragmentProcessor::Mode::kGaussian);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050090}
91#endif
92
Mike Reedf36b37f2020-03-27 15:11:10 -040093sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
94 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
95}
96
Jim Van Verthefe3ded2017-01-30 13:11:45 -050097///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050098
99namespace {
100
Brian Salomonbc9956d2017-02-22 13:49:09 -0500101uint64_t resource_cache_shared_id() {
102 return 0x2020776f64616873llu; // 'shadow '
103}
104
Brian Salomond1ac9822017-02-03 14:25:02 -0500105/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500106struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400107 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500108 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400109 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500110
Brian Salomond1ac9822017-02-03 14:25:02 -0500111 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400112 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500113 return false;
114 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400115 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500116 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500117 }
Brian Salomon5e689522017-02-01 12:07:17 -0500118
Jim Van Verth8793e382017-05-22 15:52:21 -0400119 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
120 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400121 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400122 // pick a canonical place to generate shadow
123 SkMatrix noTrans(ctm);
124 if (!ctm.hasPerspective()) {
125 noTrans[SkMatrix::kMTransX] = 0;
126 noTrans[SkMatrix::kMTransY] = 0;
127 }
128 *translate = fOffset;
129 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500130 }
131};
132
Brian Salomond1ac9822017-02-03 14:25:02 -0500133/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500134struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500135 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400136 // The umbra cannot be dropped out because either the occluder is not opaque,
137 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500138 kTransparent,
139 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400140 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500141 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400142 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500143 };
144
Brian Salomon5e689522017-02-01 12:07:17 -0500145 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400146 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400147 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
148 SkPoint3 fDevLightPos;
149 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500150 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500151
Brian Salomond1ac9822017-02-03 14:25:02 -0500152 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400153 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400154 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500155 return false;
156 }
157 switch (fOccluderType) {
158 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400159 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500160 // 'this' and 'that' will either both have no umbra removed or both have all the
161 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400162 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500163 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400164 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500165 // In this case we partially remove the umbra differently for 'this' and 'that'
166 // if the offsets don't match.
167 if (fOffset == that.fOffset) {
168 translate->set(0, 0);
169 return true;
170 }
171 return false;
172 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400173 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500174 }
Brian Salomon5e689522017-02-01 12:07:17 -0500175
Jim Van Verth8793e382017-05-22 15:52:21 -0400176 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
177 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500178 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400179 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400180 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
181 translate->set(0, 0);
182 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
183 fDevLightPos, fLightRadius, transparent);
184 } else {
185 // pick a canonical place to generate shadow, with light centered over path
186 SkMatrix noTrans(ctm);
187 noTrans[SkMatrix::kMTransX] = 0;
188 noTrans[SkMatrix::kMTransY] = 0;
189 SkPoint devCenter(fLocalCenter);
190 noTrans.mapPoints(&devCenter, 1);
191 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
192 *translate = fOffset;
193 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
194 centerLightPos, fLightRadius, transparent);
195 }
Brian Salomon5e689522017-02-01 12:07:17 -0500196 }
197};
198
199/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500200 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
201 * 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 -0400202 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500203 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500204 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500205class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500206public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500207 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
208
Brian Salomonaff27a22017-02-06 15:47:44 -0500209 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
210 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500211 return fAmbientSet.find(ambient, matrix, translate);
212 }
213
Brian Salomonaff27a22017-02-06 15:47:44 -0500214 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400215 const SkMatrix& matrix, SkVector* translate) {
216 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500217 }
218
Brian Salomonaff27a22017-02-06 15:47:44 -0500219 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
220 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500221 return fSpotSet.find(spot, matrix, translate);
222 }
223
Brian Salomonaff27a22017-02-06 15:47:44 -0500224 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400225 const SkMatrix& matrix, SkVector* translate) {
226 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500227 }
228
229private:
230 template <typename FACTORY, int MAX_ENTRIES>
231 class Set {
232 public:
233 size_t size() const { return fSize; }
234
Brian Salomonaff27a22017-02-06 15:47:44 -0500235 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
236 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500237 for (int i = 0; i < MAX_ENTRIES; ++i) {
238 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
239 const SkMatrix& m = fEntries[i].fMatrix;
240 if (matrix.hasPerspective() || m.hasPerspective()) {
241 if (matrix != fEntries[i].fMatrix) {
242 continue;
243 }
244 } else if (matrix.getScaleX() != m.getScaleX() ||
245 matrix.getSkewX() != m.getSkewX() ||
246 matrix.getScaleY() != m.getScaleY() ||
247 matrix.getSkewY() != m.getSkewY()) {
248 continue;
249 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500250 return fEntries[i].fVertices;
251 }
252 }
253 return nullptr;
254 }
255
Jim Van Verth8793e382017-05-22 15:52:21 -0400256 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
257 SkVector* translate) {
258 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500259 if (!vertices) {
260 return nullptr;
261 }
262 int i;
263 if (fCount < MAX_ENTRIES) {
264 i = fCount++;
265 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400266 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400267 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500268 }
269 fEntries[i].fFactory = factory;
270 fEntries[i].fVertices = vertices;
271 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400272 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500273 return vertices;
274 }
275
276 private:
277 struct Entry {
278 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500279 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500280 SkMatrix fMatrix;
281 };
282 Entry fEntries[MAX_ENTRIES];
283 int fCount = 0;
284 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400285 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500286 };
287
288 Set<AmbientVerticesFactory, 4> fAmbientSet;
289 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500290};
291
Brian Salomond1ac9822017-02-03 14:25:02 -0500292/**
293 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
294 * path. The key represents the path's geometry and not any shadow params.
295 */
296class CachedTessellationsRec : public SkResourceCache::Rec {
297public:
298 CachedTessellationsRec(const SkResourceCache::Key& key,
299 sk_sp<CachedTessellations> tessellations)
300 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500301 fKey.reset(new uint8_t[key.size()]);
302 memcpy(fKey.get(), &key, key.size());
303 }
304
305 const Key& getKey() const override {
306 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
307 }
Brian Salomon5e689522017-02-01 12:07:17 -0500308
Brian Salomond1ac9822017-02-03 14:25:02 -0500309 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500310
Brian Salomond1ac9822017-02-03 14:25:02 -0500311 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500312
Brian Salomond1ac9822017-02-03 14:25:02 -0500313 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500314
Brian Salomond1ac9822017-02-03 14:25:02 -0500315 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500316 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
317 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500318 return fTessellations->find(factory, matrix, translate);
319 }
Brian Salomon5e689522017-02-01 12:07:17 -0500320
321private:
322 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500323 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500324};
325
326/**
327 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500328 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
329 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
330 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500331 */
332template <typename FACTORY>
333struct FindContext {
334 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
335 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500336 const SkMatrix* const fViewMatrix;
337 // If this is valid after Find is called then we found the vertices and they should be drawn
338 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500339 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500340 SkVector fTranslate = {0, 0};
341
342 // If this is valid after Find then the caller should add the vertices to the tessellation set
343 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
344 sk_sp<CachedTessellations> fTessellationsOnFailure;
345
Brian Salomon5e689522017-02-01 12:07:17 -0500346 const FACTORY* fFactory;
347};
348
349/**
350 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
351 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
352 * necessary translation vector are set on the FindContext.
353 */
354template <typename FACTORY>
355bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
356 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500357 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
358 findContext->fVertices =
359 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
360 if (findContext->fVertices) {
361 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500362 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500363 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
364 // manipulated we will add a new Rec.
365 findContext->fTessellationsOnFailure = rec.refTessellations();
366 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500367}
368
369class ShadowedPath {
370public:
371 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500372 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500373 , fViewMatrix(viewMatrix)
374#if SK_SUPPORT_GPU
375 , fShapeForKey(*path, GrStyle::SimpleFill())
376#endif
377 {}
378
Jim Van Vertha84898d2017-02-06 13:38:23 -0500379 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500380 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
381#if SK_SUPPORT_GPU
382 /** Negative means the vertices should not be cached for this path. */
383 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
384 void writeKey(void* key) const {
385 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
386 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500387 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500388#else
389 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400390 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500391 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500392#endif
393
394private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500395 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500396 const SkMatrix* fViewMatrix;
397#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000398 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500399#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500400};
401
Brian Salomond1ac9822017-02-03 14:25:02 -0500402// This creates a domain of keys in SkResourceCache used by this file.
403static void* kNamespace;
404
Jim Van Verthee90eb42019-04-26 12:07:13 -0400405// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500406class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400407public:
408 ShadowInvalidator(const SkResourceCache::Key& key) {
409 fKey.reset(new uint8_t[key.size()]);
410 memcpy(fKey.get(), &key, key.size());
411 }
412
413private:
414 const SkResourceCache::Key& getKey() const {
415 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
416 }
417
418 // always purge
419 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
420 return false;
421 }
422
Brian Salomon99a813c2020-03-02 12:50:47 -0500423 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400424 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
425 }
426
427 std::unique_ptr<uint8_t[]> fKey;
428};
429
Brian Salomon5e689522017-02-01 12:07:17 -0500430/**
431 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
432 * they are first found in SkResourceCache.
433 */
434template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500435bool draw_shadow(const FACTORY& factory,
436 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400437 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500438 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500439
440 SkResourceCache::Key* key = nullptr;
441 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
442 int keyDataBytes = path.keyBytes();
443 if (keyDataBytes >= 0) {
444 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
445 key = new (keyStorage.begin()) SkResourceCache::Key();
446 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500447 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400448 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500449 }
450
Brian Salomonaff27a22017-02-06 15:47:44 -0500451 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500452 bool foundInCache = SkToBool(context.fVertices);
453 if (foundInCache) {
454 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500455 } else {
456 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500457 if (key) {
458 // Update or initialize a tessellation set and add it to the cache.
459 sk_sp<CachedTessellations> tessellations;
460 if (context.fTessellationsOnFailure) {
461 tessellations = std::move(context.fTessellationsOnFailure);
462 } else {
463 tessellations.reset(new CachedTessellations());
464 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400465 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
466 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500467 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500468 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500469 }
Brian Salomon804e0912017-02-23 09:34:03 -0500470 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400471 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400472 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500473 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400474 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
475 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500476 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500477 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500478 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500479 }
Brian Salomon5e689522017-02-01 12:07:17 -0500480 }
481
482 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500483 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
484 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500485 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400486 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400487 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400488
Jim Van Verth8793e382017-05-22 15:52:21 -0400489 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400490 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500491
492 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500493}
494}
495
Mike Reed4204da22017-05-17 08:53:36 -0400496static bool tilted(const SkPoint3& zPlaneParams) {
497 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
498}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400499
Mike Reed4204da22017-05-17 08:53:36 -0400500static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
501 SkPoint3 result;
502 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
503 result.fZ = pt.fZ;
504 return result;
505}
506
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500507void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
508 SkColor* outAmbientColor, SkColor* outSpotColor) {
509 // For tonal color we only compute color values for the spot shadow.
510 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400511
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500512 // Ambient
513 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400514
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500515 // Spot
516 int spotR = SkColorGetR(inSpotColor);
517 int spotG = SkColorGetG(inSpotColor);
518 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500519 int max = std::max(std::max(spotR, spotG), spotB);
520 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500521 SkScalar luminance = 0.5f*(max + min)/255.f;
522 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
523
524 // We compute a color alpha value based on the luminance of the color, scaled by an
525 // adjusted alpha value. We want the following properties to match the UX examples
526 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
527 // is black and/or the alpha is 0:
528 // f(0, a) = 0
529 // f(luminance, 0) = 0
530 // f(1, 0.25) = .5
531 // f(0.5, 0.25) = .4
532 // f(1, 1) = 1
533 // The following functions match this as closely as possible.
534 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
535 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
536 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
537
538 // Similarly, we set the greyscale alpha based on luminance and alpha so that
539 // f(0, a) = a
540 // f(luminance, 0) = 0
541 // f(1, 0.25) = 0.15
542 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
543
544 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
545 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
546 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
547 // ignoring edge falloff, this becomes
548 //
549 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
550 //
551 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
552 // set the alpha to (S_a + C_a - S_a*C_a).
553 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
554 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
555 SkScalar unPremulScale = colorScale / tonalAlpha;
556 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
557 unPremulScale*spotR,
558 unPremulScale*spotG,
559 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400560}
561
Jim Van Verth43475ad2017-01-13 14:37:37 -0500562// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400563void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500564 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500565 SkColor ambientColor, SkColor spotColor,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400566 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400567 SkMatrix inverse;
568 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500569 return;
570 }
Mike Reed4204da22017-05-17 08:53:36 -0400571 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500572
Mike Reed4204da22017-05-17 08:53:36 -0400573 SkDrawShadowRec rec;
574 rec.fZPlaneParams = zPlaneParams;
575 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
576 rec.fLightRadius = lightRadius;
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500577 rec.fAmbientColor = ambientColor;
578 rec.fSpotColor = spotColor;
Mike Reed4204da22017-05-17 08:53:36 -0400579 rec.fFlags = flags;
580
581 canvas->private_draw_shadow_rec(path, rec);
582}
583
Jim Van Vertha947e292018-02-26 13:54:34 -0500584static bool validate_rec(const SkDrawShadowRec& rec) {
585 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
586 SkScalarIsFinite(rec.fLightRadius);
587}
588
Mike Reed4204da22017-05-17 08:53:36 -0400589void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
590 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400591 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400592 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400593 // For perspective shadows we've already computed the shadow in world space,
594 // and we can't translate it without changing it. Otherwise we concat the
595 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400596 SkAutoDeviceTransformRestore adr(
597 this,
598 hasPerspective ? SkMatrix::I()
Mike Reed1f607332020-05-21 12:11:27 -0400599 : this->localToDevice() * SkMatrix::Translate(tx, ty));
Mike Reed5caf9352020-03-02 14:57:09 -0500600 this->drawVertices(vertices, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400601 }
Mike Reed4204da22017-05-17 08:53:36 -0400602 };
603
Jim Van Vertha947e292018-02-26 13:54:34 -0500604 if (!validate_rec(rec)) {
605 return;
606 }
607
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400608 SkMatrix viewMatrix = this->localToDevice();
609 SkAutoDeviceTransformRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500610
Brian Salomon5e689522017-02-01 12:07:17 -0500611 ShadowedPath shadowedPath(&path, &viewMatrix);
612
Mike Reed4204da22017-05-17 08:53:36 -0400613 bool tiltZPlane = tilted(rec.fZPlaneParams);
614 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400615 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500616
Mike Reed4204da22017-05-17 08:53:36 -0400617 SkPoint3 zPlaneParams = rec.fZPlaneParams;
618 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
619 float lightRadius = rec.fLightRadius;
620
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500621 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500622 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400623 if (uncached) {
624 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
625 zPlaneParams,
626 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400627 if (vertices) {
628 SkPaint paint;
629 // Run the vertex color through a GaussianColorFilter and then modulate the
630 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500631 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400632 SkColorFilters::Blend(rec.fAmbientColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500633 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400634 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500635 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500636 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400637 }
Jim Van Verth22526362018-02-28 14:51:19 -0500638 }
639
640 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400641 AmbientVerticesFactory factory;
642 factory.fOccluderHeight = zPlaneParams.fZ;
643 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400644 if (viewMatrix.hasPerspective()) {
645 factory.fOffset.set(0, 0);
646 } else {
647 factory.fOffset.fX = viewMatrix.getTranslateX();
648 factory.fOffset.fY = viewMatrix.getTranslateY();
649 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400650
Jim Van Verth22526362018-02-28 14:51:19 -0500651 if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) {
652 // Pretransform the path to avoid transforming the stroke, below.
653 SkPath devSpacePath;
654 path.transform(viewMatrix, &devSpacePath);
Robert Phillipsed3dbf42019-03-18 12:20:15 -0400655 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500656
657 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400658 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500659 //
660 // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f')
661 // can be calculated by interpolating:
662 //
663 // original edge outer edge
664 // | |<---------- r ------>|
665 // |<------|--- f -------------->|
666 // | | |
667 // alpha = 1 alpha = a alpha = 0
668 //
669 // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2.
670 //
671 // We now need to outset the path to place the new edge in the center of the
672 // blur region:
673 //
674 // original new
675 // | |<------|--- r ------>|
676 // |<------|--- f -|------------>|
677 // | |<- o ->|<--- f/2 --->|
678 //
679 // r = o + f/2, so o = r - f/2
680 //
681 // We outset by using the stroker, so the strokeWidth is o/2.
682 //
683 SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ);
684 SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ);
685 SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA;
686 SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius);
687
688 // Now draw with blur
689 SkPaint paint;
690 paint.setColor(rec.fAmbientColor);
691 paint.setStrokeWidth(strokeWidth);
692 paint.setStyle(SkPaint::kStrokeAndFill_Style);
Mike Reed8e03f692018-03-09 16:18:56 -0500693 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400694 bool respectCTM = false;
695 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500696 this->drawPath(devSpacePath, paint);
697 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500698 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400699 }
700
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500701 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500702 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400703 if (uncached) {
704 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
705 zPlaneParams,
706 devLightPos, lightRadius,
707 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400708 if (vertices) {
709 SkPaint paint;
710 // Run the vertex color through a GaussianColorFilter and then modulate the
711 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500712 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400713 SkColorFilters::Blend(rec.fSpotColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500714 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400715 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500716 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500717 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400718 }
Jim Van Verth22526362018-02-28 14:51:19 -0500719 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400720
Jim Van Verth22526362018-02-28 14:51:19 -0500721 if (!success) {
722 SpotVerticesFactory factory;
723 factory.fOccluderHeight = zPlaneParams.fZ;
724 factory.fDevLightPos = devLightPos;
725 factory.fLightRadius = lightRadius;
726
Jim Van Verth37c5a962017-05-10 14:13:24 -0400727 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400728 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400729 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500730 SkScalar radius, scale;
731 SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX,
732 devLightPos.fY - center.fY, devLightPos.fZ,
733 lightRadius, &radius, &scale, &factory.fOffset);
Jim Van Vertha783c362017-05-11 17:05:28 -0400734 SkRect devBounds;
735 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth8793e382017-05-22 15:52:21 -0400736 if (transparent ||
737 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
738 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400739 // if the translation of the shadow is big enough we're going to end up
740 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400741 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400742 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400743 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400744 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400745 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400746 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400747 } else {
748 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400749 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400750 // need to add this after we classify the shadow
751 factory.fOffset.fX += viewMatrix.getTranslateX();
752 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500753
754 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400755#ifdef DEBUG_SHADOW_CHECKS
756 switch (factory.fOccluderType) {
757 case SpotVerticesFactory::OccluderType::kTransparent:
758 color = 0xFFD2B48C; // tan for transparent
759 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400760 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400761 color = 0xFFFFA500; // orange for opaque
762 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400763 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
764 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400765 break;
766 }
767#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500768 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
769 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500770 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400771 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
772 viewMatrix, zPlaneParams,
773 path.getBounds(),
774 &shadowMatrix, &radius)) {
775 return;
776 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400777 SkAutoDeviceTransformRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500778
779 SkPaint paint;
780 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500781 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400782 bool respectCTM = false;
783 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500784 this->drawPath(path, paint);
785 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400786 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400787 }
788}