blob: 9e7aa70d563d9342cefa477c75a0ff4103b7f3f6 [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 Klein8aa0edf2020-10-16 11:04:18 -050018#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/core/SkBlurMask.h"
Mike Reedb11e6272020-06-24 16:56:33 -040021#include "src/core/SkColorFilterBase.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040022#include "src/core/SkColorFilterPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkDevice.h"
24#include "src/core/SkDrawShadowInfo.h"
25#include "src/core/SkEffectPriv.h"
Jim Van Verthee90eb42019-04-26 12:07:13 -040026#include "src/core/SkPathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/core/SkRasterPipeline.h"
28#include "src/core/SkResourceCache.h"
Brian Osmand1b530a2021-05-25 16:09:16 -040029#include "src/core/SkRuntimeEffectPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/core/SkTLazy.h"
Mike Reedf36b37f2020-03-27 15:11:10 -040031#include "src/core/SkVM.h"
Mike Reedba962562020-03-12 20:33:21 -040032#include "src/core/SkVerticesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/utils/SkShadowTessellator.h"
Mike Klein79aea6a2018-06-11 10:45:26 -040034#include <new>
Brian Salomon5e689522017-02-01 12:07:17 -050035#if SK_SUPPORT_GPU
Brian Osman9a4c9652021-05-20 16:05:19 -040036#include "src/gpu/effects/GrSkSLFP.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000037#include "src/gpu/geometry/GrStyledShape.h"
Mike Reed4204da22017-05-17 08:53:36 -040038#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050039
40/**
41* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
42* then blends with the color's G value.
43* Final result is black with alpha of Gaussian(B)*G.
44* The assumption is that the original color's alpha is 1.
45*/
Mike Reedb11e6272020-06-24 16:56:33 -040046class SkGaussianColorFilter : public SkColorFilterBase {
Jim Van Verthefe3ded2017-01-30 13:11:45 -050047public:
Mike Reedf36b37f2020-03-27 15:11:10 -040048 SkGaussianColorFilter() : INHERITED() {}
Jim Van Verthefe3ded2017-01-30 13:11:45 -050049
Jim Van Verthefe3ded2017-01-30 13:11:45 -050050#if SK_SUPPORT_GPU
John Stiles43206642020-06-29 12:03:26 -040051 GrFPResult asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
52 GrRecordingContext*, const GrColorInfo&) const override;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050053#endif
54
Mike Klein40f91382019-08-01 21:07:29 +000055protected:
Jim Van Verthefe3ded2017-01-30 13:11:45 -050056 void flatten(SkWriteBuffer&) const override {}
Mike Klein40f91382019-08-01 21:07:29 +000057 bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
Mike Reed1386b2d2019-03-13 21:15:05 -040058 rec.fPipeline->append(SkRasterPipeline::gauss_a_to_rgba);
Mike Reed2fdbeae2019-03-30 14:27:53 -040059 return true;
Mike Reed65331592017-05-24 16:45:34 -040060 }
Mike Reedf36b37f2020-03-27 15:11:10 -040061
62 skvm::Color onProgram(skvm::Builder* p, skvm::Color c, SkColorSpace* dstCS, skvm::Uniforms*,
63 SkArenaAlloc*) const override {
64 // x = 1 - x;
65 // exp(-x * x * 4) - 0.018f;
66 // ... now approximate with quartic
67 //
Mike Reedf3b9a302020-04-01 13:18:02 -040068 skvm::F32 x = p->splat(-2.26661229133605957031f);
69 x = c.a * x + 2.89795351028442382812f;
70 x = c.a * x + 0.21345567703247070312f;
71 x = c.a * x + 0.15489584207534790039f;
72 x = c.a * x + 0.00030726194381713867f;
Mike Reedf36b37f2020-03-27 15:11:10 -040073 return {x, x, x, x};
74 }
75
Mike Klein40f91382019-08-01 21:07:29 +000076private:
Mike Klein4fee3232018-10-18 17:27:16 -040077 SK_FLATTENABLE_HOOKS(SkGaussianColorFilter)
78
John Stiles7571f9e2020-09-02 22:42:33 -040079 using INHERITED = SkColorFilterBase;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050080};
81
Jim Van Verthefe3ded2017-01-30 13:11:45 -050082sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
Mike Reedf36b37f2020-03-27 15:11:10 -040083 return SkColorFilterPriv::MakeGaussian();
Jim Van Verthefe3ded2017-01-30 13:11:45 -050084}
85
Jim Van Verthefe3ded2017-01-30 13:11:45 -050086#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050087
John Stiles43206642020-06-29 12:03:26 -040088GrFPResult SkGaussianColorFilter::asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
89 GrRecordingContext*,
90 const GrColorInfo&) const {
Brian Osmand1b530a2021-05-25 16:09:16 -040091 static auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, R"(
Brian Osman9a4c9652021-05-20 16:05:19 -040092 half4 main(half4 inColor) {
93 half factor = 1 - inColor.a;
94 factor = exp(-factor * factor * 4) - 0.018;
95 return half4(factor);
96 }
Brian Osmand1b530a2021-05-25 16:09:16 -040097 )");
Brian Osmane384c062021-06-04 14:09:51 -040098 auto fp = GrSkSLFP::Make(effect, "gaussian_fp");
99 return GrFPSuccess(GrFragmentProcessor::Compose(std::move(fp), std::move(inputFP)));
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500100}
101#endif
102
Mike Reedf36b37f2020-03-27 15:11:10 -0400103sk_sp<SkColorFilter> SkColorFilterPriv::MakeGaussian() {
104 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
105}
106
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500107///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -0500108
109namespace {
110
Brian Salomonbc9956d2017-02-22 13:49:09 -0500111uint64_t resource_cache_shared_id() {
112 return 0x2020776f64616873llu; // 'shadow '
113}
114
Brian Salomond1ac9822017-02-03 14:25:02 -0500115/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500116struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -0400117 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500118 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400119 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -0500120
Brian Salomond1ac9822017-02-03 14:25:02 -0500121 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -0400122 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500123 return false;
124 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400125 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500126 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500127 }
Brian Salomon5e689522017-02-01 12:07:17 -0500128
Jim Van Verth8793e382017-05-22 15:52:21 -0400129 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
130 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400131 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400132 // pick a canonical place to generate shadow
133 SkMatrix noTrans(ctm);
134 if (!ctm.hasPerspective()) {
135 noTrans[SkMatrix::kMTransX] = 0;
136 noTrans[SkMatrix::kMTransY] = 0;
137 }
138 *translate = fOffset;
139 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500140 }
141};
142
Brian Salomond1ac9822017-02-03 14:25:02 -0500143/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500144struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500145 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400146 // The umbra cannot be dropped out because either the occluder is not opaque,
147 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500148 kTransparent,
149 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400150 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500151 // It is known that the entire umbra is occluded.
Jim Van Verth63f03542020-12-16 11:56:11 -0500152 kOpaqueNoUmbra,
153 // The light is directional
154 kDirectional
Brian Salomond1ac9822017-02-03 14:25:02 -0500155 };
156
Brian Salomon5e689522017-02-01 12:07:17 -0500157 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400158 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400159 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
160 SkPoint3 fDevLightPos;
161 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500162 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500163
Brian Salomond1ac9822017-02-03 14:25:02 -0500164 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400165 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400166 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500167 return false;
168 }
169 switch (fOccluderType) {
170 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400171 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500172 // 'this' and 'that' will either both have no umbra removed or both have all the
173 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400174 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500175 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400176 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500177 // In this case we partially remove the umbra differently for 'this' and 'that'
178 // if the offsets don't match.
179 if (fOffset == that.fOffset) {
180 translate->set(0, 0);
181 return true;
182 }
183 return false;
Jim Van Verth63f03542020-12-16 11:56:11 -0500184 case OccluderType::kDirectional:
185 *translate = that.fOffset - fOffset;
186 return true;
Brian Salomond1ac9822017-02-03 14:25:02 -0500187 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400188 SK_ABORT("Uninitialized occluder type?");
Brian Salomon5e689522017-02-01 12:07:17 -0500189 }
Brian Salomon5e689522017-02-01 12:07:17 -0500190
Jim Van Verth8793e382017-05-22 15:52:21 -0400191 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
192 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500193 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verth63f03542020-12-16 11:56:11 -0500194 bool directional = OccluderType::kDirectional == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400195 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth63f03542020-12-16 11:56:11 -0500196 if (directional) {
Jim Van Verth8793e382017-05-22 15:52:21 -0400197 translate->set(0, 0);
Jim Van Verth63f03542020-12-16 11:56:11 -0500198 return SkShadowTessellator::MakeSpot(path, ctm, zParams, fDevLightPos, fLightRadius,
199 transparent, true);
200 } else if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
201 translate->set(0, 0);
202 return SkShadowTessellator::MakeSpot(path, ctm, zParams, fDevLightPos, fLightRadius,
203 transparent, false);
Jim Van Verth8793e382017-05-22 15:52:21 -0400204 } else {
205 // pick a canonical place to generate shadow, with light centered over path
206 SkMatrix noTrans(ctm);
207 noTrans[SkMatrix::kMTransX] = 0;
208 noTrans[SkMatrix::kMTransY] = 0;
209 SkPoint devCenter(fLocalCenter);
210 noTrans.mapPoints(&devCenter, 1);
211 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
212 *translate = fOffset;
213 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
Jim Van Verth63f03542020-12-16 11:56:11 -0500214 centerLightPos, fLightRadius, transparent, false);
Jim Van Verth8793e382017-05-22 15:52:21 -0400215 }
Brian Salomon5e689522017-02-01 12:07:17 -0500216 }
217};
218
219/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500220 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
221 * 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 -0400222 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500223 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500224 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500225class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500226public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500227 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
228
Brian Salomonaff27a22017-02-06 15:47:44 -0500229 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
230 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500231 return fAmbientSet.find(ambient, matrix, translate);
232 }
233
Brian Salomonaff27a22017-02-06 15:47:44 -0500234 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400235 const SkMatrix& matrix, SkVector* translate) {
236 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500237 }
238
Brian Salomonaff27a22017-02-06 15:47:44 -0500239 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
240 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500241 return fSpotSet.find(spot, matrix, translate);
242 }
243
Brian Salomonaff27a22017-02-06 15:47:44 -0500244 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400245 const SkMatrix& matrix, SkVector* translate) {
246 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500247 }
248
249private:
250 template <typename FACTORY, int MAX_ENTRIES>
251 class Set {
252 public:
253 size_t size() const { return fSize; }
254
Brian Salomonaff27a22017-02-06 15:47:44 -0500255 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
256 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500257 for (int i = 0; i < MAX_ENTRIES; ++i) {
258 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
259 const SkMatrix& m = fEntries[i].fMatrix;
260 if (matrix.hasPerspective() || m.hasPerspective()) {
261 if (matrix != fEntries[i].fMatrix) {
262 continue;
263 }
264 } else if (matrix.getScaleX() != m.getScaleX() ||
265 matrix.getSkewX() != m.getSkewX() ||
266 matrix.getScaleY() != m.getScaleY() ||
267 matrix.getSkewY() != m.getSkewY()) {
268 continue;
269 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500270 return fEntries[i].fVertices;
271 }
272 }
273 return nullptr;
274 }
275
Jim Van Verth8793e382017-05-22 15:52:21 -0400276 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
277 SkVector* translate) {
278 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500279 if (!vertices) {
280 return nullptr;
281 }
282 int i;
283 if (fCount < MAX_ENTRIES) {
284 i = fCount++;
285 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400286 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400287 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500288 }
289 fEntries[i].fFactory = factory;
290 fEntries[i].fVertices = vertices;
291 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400292 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500293 return vertices;
294 }
295
296 private:
297 struct Entry {
298 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500299 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500300 SkMatrix fMatrix;
301 };
302 Entry fEntries[MAX_ENTRIES];
303 int fCount = 0;
304 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400305 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500306 };
307
308 Set<AmbientVerticesFactory, 4> fAmbientSet;
309 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500310};
311
Brian Salomond1ac9822017-02-03 14:25:02 -0500312/**
313 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
314 * path. The key represents the path's geometry and not any shadow params.
315 */
316class CachedTessellationsRec : public SkResourceCache::Rec {
317public:
318 CachedTessellationsRec(const SkResourceCache::Key& key,
319 sk_sp<CachedTessellations> tessellations)
320 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500321 fKey.reset(new uint8_t[key.size()]);
322 memcpy(fKey.get(), &key, key.size());
323 }
324
325 const Key& getKey() const override {
326 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
327 }
Brian Salomon5e689522017-02-01 12:07:17 -0500328
Brian Salomond1ac9822017-02-03 14:25:02 -0500329 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500330
Brian Salomond1ac9822017-02-03 14:25:02 -0500331 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500332
Brian Salomond1ac9822017-02-03 14:25:02 -0500333 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500334
Brian Salomond1ac9822017-02-03 14:25:02 -0500335 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500336 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
337 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500338 return fTessellations->find(factory, matrix, translate);
339 }
Brian Salomon5e689522017-02-01 12:07:17 -0500340
341private:
342 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500343 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500344};
345
346/**
347 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500348 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
349 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
350 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500351 */
352template <typename FACTORY>
353struct FindContext {
354 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
355 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500356 const SkMatrix* const fViewMatrix;
357 // If this is valid after Find is called then we found the vertices and they should be drawn
358 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500359 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500360 SkVector fTranslate = {0, 0};
361
362 // If this is valid after Find then the caller should add the vertices to the tessellation set
363 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
364 sk_sp<CachedTessellations> fTessellationsOnFailure;
365
Brian Salomon5e689522017-02-01 12:07:17 -0500366 const FACTORY* fFactory;
367};
368
369/**
370 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
371 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
372 * necessary translation vector are set on the FindContext.
373 */
374template <typename FACTORY>
375bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
376 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500377 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
378 findContext->fVertices =
379 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
380 if (findContext->fVertices) {
381 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500382 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500383 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
384 // manipulated we will add a new Rec.
385 findContext->fTessellationsOnFailure = rec.refTessellations();
386 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500387}
388
389class ShadowedPath {
390public:
391 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500392 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500393 , fViewMatrix(viewMatrix)
394#if SK_SUPPORT_GPU
395 , fShapeForKey(*path, GrStyle::SimpleFill())
396#endif
397 {}
398
Jim Van Vertha84898d2017-02-06 13:38:23 -0500399 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500400 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
401#if SK_SUPPORT_GPU
402 /** Negative means the vertices should not be cached for this path. */
403 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
404 void writeKey(void* key) const {
405 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
406 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500407 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500408#else
409 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400410 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500411 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500412#endif
413
414private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500415 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500416 const SkMatrix* fViewMatrix;
417#if SK_SUPPORT_GPU
Michael Ludwig2686d692020-04-17 20:21:37 +0000418 GrStyledShape fShapeForKey;
Brian Salomon5e689522017-02-01 12:07:17 -0500419#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500420};
421
Brian Salomond1ac9822017-02-03 14:25:02 -0500422// This creates a domain of keys in SkResourceCache used by this file.
423static void* kNamespace;
424
Jim Van Verthee90eb42019-04-26 12:07:13 -0400425// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key.
Brian Salomon99a813c2020-03-02 12:50:47 -0500426class ShadowInvalidator : public SkIDChangeListener {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400427public:
428 ShadowInvalidator(const SkResourceCache::Key& key) {
429 fKey.reset(new uint8_t[key.size()]);
430 memcpy(fKey.get(), &key, key.size());
431 }
432
433private:
434 const SkResourceCache::Key& getKey() const {
435 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
436 }
437
438 // always purge
439 static bool FindVisitor(const SkResourceCache::Rec&, void*) {
440 return false;
441 }
442
Brian Salomon99a813c2020-03-02 12:50:47 -0500443 void changed() override {
Jim Van Verthee90eb42019-04-26 12:07:13 -0400444 SkResourceCache::Find(this->getKey(), ShadowInvalidator::FindVisitor, nullptr);
445 }
446
447 std::unique_ptr<uint8_t[]> fKey;
448};
449
Brian Salomon5e689522017-02-01 12:07:17 -0500450/**
451 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
452 * they are first found in SkResourceCache.
453 */
454template <typename FACTORY>
Jim Van Verth22526362018-02-28 14:51:19 -0500455bool draw_shadow(const FACTORY& factory,
456 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400457 SkScalar tx, SkScalar ty, bool)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500458 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500459
460 SkResourceCache::Key* key = nullptr;
461 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
462 int keyDataBytes = path.keyBytes();
463 if (keyDataBytes >= 0) {
464 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
465 key = new (keyStorage.begin()) SkResourceCache::Key();
466 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500467 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400468 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500469 }
470
Brian Salomonaff27a22017-02-06 15:47:44 -0500471 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500472 bool foundInCache = SkToBool(context.fVertices);
473 if (foundInCache) {
474 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500475 } else {
476 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500477 if (key) {
478 // Update or initialize a tessellation set and add it to the cache.
479 sk_sp<CachedTessellations> tessellations;
480 if (context.fTessellationsOnFailure) {
481 tessellations = std::move(context.fTessellationsOnFailure);
482 } else {
483 tessellations.reset(new CachedTessellations());
484 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400485 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
486 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500487 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500488 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500489 }
Brian Salomon804e0912017-02-23 09:34:03 -0500490 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verthee90eb42019-04-26 12:07:13 -0400491 SkPathPriv::AddGenIDChangeListener(path.path(), sk_make_sp<ShadowInvalidator>(*key));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400492 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500493 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400494 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
495 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500496 if (!vertices) {
Jim Van Verth22526362018-02-28 14:51:19 -0500497 return false;
Brian Salomond1ac9822017-02-03 14:25:02 -0500498 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500499 }
Brian Salomon5e689522017-02-01 12:07:17 -0500500 }
501
502 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500503 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
504 // that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500505 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400506 SkColorFilters::Blend(color, SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400507 SkColorFilterPriv::MakeGaussian()));
Mike Reed4204da22017-05-17 08:53:36 -0400508
Jim Van Verth8793e382017-05-22 15:52:21 -0400509 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400510 context.fTranslate.fX, context.fTranslate.fY, path.viewMatrix().hasPerspective());
Jim Van Verth22526362018-02-28 14:51:19 -0500511
512 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500513}
John Stilesa6841be2020-08-06 14:11:56 -0400514} // namespace
Brian Salomon5e689522017-02-01 12:07:17 -0500515
Mike Reed4204da22017-05-17 08:53:36 -0400516static bool tilted(const SkPoint3& zPlaneParams) {
517 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
518}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400519
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500520void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
521 SkColor* outAmbientColor, SkColor* outSpotColor) {
522 // For tonal color we only compute color values for the spot shadow.
523 // The ambient shadow is greyscale only.
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400524
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500525 // Ambient
526 *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400527
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500528 // Spot
529 int spotR = SkColorGetR(inSpotColor);
530 int spotG = SkColorGetG(inSpotColor);
531 int spotB = SkColorGetB(inSpotColor);
Brian Osman788b9162020-02-07 10:36:46 -0500532 int max = std::max(std::max(spotR, spotG), spotB);
533 int min = std::min(std::min(spotR, spotG), spotB);
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500534 SkScalar luminance = 0.5f*(max + min)/255.f;
535 SkScalar origA = SkColorGetA(inSpotColor)/255.f;
536
537 // We compute a color alpha value based on the luminance of the color, scaled by an
538 // adjusted alpha value. We want the following properties to match the UX examples
539 // (assuming a = 0.25) and to ensure that we have reasonable results when the color
540 // is black and/or the alpha is 0:
541 // f(0, a) = 0
542 // f(luminance, 0) = 0
543 // f(1, 0.25) = .5
544 // f(0.5, 0.25) = .4
545 // f(1, 1) = 1
546 // The following functions match this as closely as possible.
547 SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA;
548 SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance;
549 colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f);
550
551 // Similarly, we set the greyscale alpha based on luminance and alpha so that
552 // f(0, a) = a
553 // f(luminance, 0) = 0
554 // f(1, 0.25) = 0.15
555 SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f);
556
557 // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an
558 // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a)
559 // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and
560 // ignoring edge falloff, this becomes
561 //
562 // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb
563 //
564 // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and
565 // set the alpha to (S_a + C_a - S_a*C_a).
566 SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha);
567 SkScalar tonalAlpha = colorScale + greyscaleAlpha;
568 SkScalar unPremulScale = colorScale / tonalAlpha;
569 *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f,
570 unPremulScale*spotR,
571 unPremulScale*spotG,
572 unPremulScale*spotB);
Jim Van Verth060d9822017-05-04 09:58:17 -0400573}
574
Jim Van Verthea4aa392021-01-11 11:01:09 -0500575static bool fill_shadow_rec(const SkPath& path, const SkPoint3& zPlaneParams,
576 const SkPoint3& lightPos, SkScalar lightRadius,
577 SkColor ambientColor, SkColor spotColor,
578 uint32_t flags, const SkMatrix& ctm, SkDrawShadowRec* rec) {
Jim Van Verth63f03542020-12-16 11:56:11 -0500579 SkPoint pt = { lightPos.fX, lightPos.fY };
580 if (!SkToBool(flags & kDirectionalLight_ShadowFlag)) {
581 // If light position is in device space, need to transform to local space
582 // before applying to SkCanvas.
583 SkMatrix inverse;
Jim Van Verthea4aa392021-01-11 11:01:09 -0500584 if (!ctm.invert(&inverse)) {
585 return false;
Jim Van Verth63f03542020-12-16 11:56:11 -0500586 }
587 inverse.mapPoints(&pt, 1);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500588 }
589
Jim Van Verthea4aa392021-01-11 11:01:09 -0500590 rec->fZPlaneParams = zPlaneParams;
591 rec->fLightPos = { pt.fX, pt.fY, lightPos.fZ };
592 rec->fLightRadius = lightRadius;
593 rec->fAmbientColor = ambientColor;
594 rec->fSpotColor = spotColor;
595 rec->fFlags = flags;
596
597 return true;
598}
599
600// Draw an offset spot shadow and outlining ambient shadow for the given path.
601void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
602 const SkPoint3& lightPos, SkScalar lightRadius,
603 SkColor ambientColor, SkColor spotColor,
604 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400605 SkDrawShadowRec rec;
Jim Van Verthea4aa392021-01-11 11:01:09 -0500606 if (!fill_shadow_rec(path, zPlaneParams, lightPos, lightRadius, ambientColor, spotColor,
607 flags, canvas->getTotalMatrix(), &rec)) {
608 return;
609 }
Mike Reed4204da22017-05-17 08:53:36 -0400610
611 canvas->private_draw_shadow_rec(path, rec);
612}
613
Jim Van Verthea4aa392021-01-11 11:01:09 -0500614bool SkShadowUtils::GetLocalBounds(const SkMatrix& ctm, const SkPath& path,
615 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
616 SkScalar lightRadius, uint32_t flags, SkRect* bounds) {
617 SkDrawShadowRec rec;
618 if (!fill_shadow_rec(path, zPlaneParams, lightPos, lightRadius, SK_ColorBLACK, SK_ColorBLACK,
619 flags, ctm, &rec)) {
620 return false;
621 }
622
623 SkDrawShadowMetrics::GetLocalBounds(path, rec, ctm, bounds);
624
625 return true;
626}
627
628//////////////////////////////////////////////////////////////////////////////////////////////
629
Jim Van Vertha947e292018-02-26 13:54:34 -0500630static bool validate_rec(const SkDrawShadowRec& rec) {
631 return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() &&
632 SkScalarIsFinite(rec.fLightRadius);
633}
634
Mike Reed4204da22017-05-17 08:53:36 -0400635void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
636 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
Jim Van Verth1aaad022019-03-14 14:21:51 -0400637 SkScalar tx, SkScalar ty, bool hasPerspective) {
Brian Osman8cbedf92020-03-31 10:38:31 -0400638 if (vertices->priv().vertexCount()) {
Jim Van Verth1aaad022019-03-14 14:21:51 -0400639 // For perspective shadows we've already computed the shadow in world space,
640 // and we can't translate it without changing it. Otherwise we concat the
641 // change in translation from the cached version.
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400642 SkAutoDeviceTransformRestore adr(
643 this,
644 hasPerspective ? SkMatrix::I()
Mike Reed1f607332020-05-21 12:11:27 -0400645 : this->localToDevice() * SkMatrix::Translate(tx, ty));
Mike Reed5caf9352020-03-02 14:57:09 -0500646 this->drawVertices(vertices, mode, paint);
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400647 }
Mike Reed4204da22017-05-17 08:53:36 -0400648 };
649
Jim Van Vertha947e292018-02-26 13:54:34 -0500650 if (!validate_rec(rec)) {
651 return;
652 }
653
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400654 SkMatrix viewMatrix = this->localToDevice();
655 SkAutoDeviceTransformRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500656
Brian Salomon5e689522017-02-01 12:07:17 -0500657 ShadowedPath shadowedPath(&path, &viewMatrix);
658
Mike Reed4204da22017-05-17 08:53:36 -0400659 bool tiltZPlane = tilted(rec.fZPlaneParams);
660 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth63f03542020-12-16 11:56:11 -0500661 bool directional = SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400662 bool uncached = tiltZPlane || path.isVolatile();
Brian Salomon958fbc42017-01-30 17:01:28 -0500663
Mike Reed4204da22017-05-17 08:53:36 -0400664 SkPoint3 zPlaneParams = rec.fZPlaneParams;
Jim Van Verth63f03542020-12-16 11:56:11 -0500665 SkPoint3 devLightPos = rec.fLightPos;
666 if (directional) {
Jim Van Vertha8682202020-12-17 10:18:16 -0500667 devLightPos.normalize();
Jim Van Verth63f03542020-12-16 11:56:11 -0500668 } else {
669 viewMatrix.mapPoints((SkPoint*)&devLightPos.fX, 1);
670 }
Mike Reed4204da22017-05-17 08:53:36 -0400671 float lightRadius = rec.fLightRadius;
672
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500673 if (SkColorGetA(rec.fAmbientColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500674 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400675 if (uncached) {
676 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
677 zPlaneParams,
678 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400679 if (vertices) {
680 SkPaint paint;
681 // Run the vertex color through a GaussianColorFilter and then modulate the
682 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500683 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400684 SkColorFilters::Blend(rec.fAmbientColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500685 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400686 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500687 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500688 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400689 }
Jim Van Verth22526362018-02-28 14:51:19 -0500690 }
691
692 if (!success) {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400693 AmbientVerticesFactory factory;
694 factory.fOccluderHeight = zPlaneParams.fZ;
695 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400696 if (viewMatrix.hasPerspective()) {
697 factory.fOffset.set(0, 0);
698 } else {
699 factory.fOffset.fX = viewMatrix.getTranslateX();
700 factory.fOffset.fY = viewMatrix.getTranslateY();
701 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400702
Jim Van Verth22526362018-02-28 14:51:19 -0500703 if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) {
704 // Pretransform the path to avoid transforming the stroke, below.
705 SkPath devSpacePath;
706 path.transform(viewMatrix, &devSpacePath);
Robert Phillipsed3dbf42019-03-18 12:20:15 -0400707 devSpacePath.setIsVolatile(true);
Jim Van Verth22526362018-02-28 14:51:19 -0500708
709 // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of
Jim Van Verth3a039d52018-09-14 17:14:47 -0400710 // the tesselation, and sets the alpha on the path to 1/AmbientRecipAlpha (or 'a').
Jim Van Verth22526362018-02-28 14:51:19 -0500711 //
712 // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f')
713 // can be calculated by interpolating:
714 //
715 // original edge outer edge
716 // | |<---------- r ------>|
717 // |<------|--- f -------------->|
718 // | | |
719 // alpha = 1 alpha = a alpha = 0
720 //
721 // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2.
722 //
723 // We now need to outset the path to place the new edge in the center of the
724 // blur region:
725 //
726 // original new
727 // | |<------|--- r ------>|
728 // |<------|--- f -|------------>|
729 // | |<- o ->|<--- f/2 --->|
730 //
731 // r = o + f/2, so o = r - f/2
732 //
733 // We outset by using the stroker, so the strokeWidth is o/2.
734 //
735 SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ);
736 SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ);
737 SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA;
738 SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius);
739
740 // Now draw with blur
741 SkPaint paint;
742 paint.setColor(rec.fAmbientColor);
743 paint.setStrokeWidth(strokeWidth);
744 paint.setStyle(SkPaint::kStrokeAndFill_Style);
Mike Reed8e03f692018-03-09 16:18:56 -0500745 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius);
Mike Reed18e75562018-03-12 14:03:47 -0400746 bool respectCTM = false;
747 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500748 this->drawPath(devSpacePath, paint);
749 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500750 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400751 }
752
Jim Van Verthb1b80f72018-01-18 15:19:13 -0500753 if (SkColorGetA(rec.fSpotColor) > 0) {
Jim Van Verth22526362018-02-28 14:51:19 -0500754 bool success = false;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400755 if (uncached) {
756 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
757 zPlaneParams,
758 devLightPos, lightRadius,
Jim Van Verth63f03542020-12-16 11:56:11 -0500759 transparent,
760 directional);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400761 if (vertices) {
762 SkPaint paint;
763 // Run the vertex color through a GaussianColorFilter and then modulate the
764 // grayscale result of that against our 'color' param.
Mike Reed19d7bd62018-02-19 14:10:57 -0500765 paint.setColorFilter(
Mike Reedb286bc22019-04-08 16:23:20 -0400766 SkColorFilters::Blend(rec.fSpotColor,
Mike Reed19d7bd62018-02-19 14:10:57 -0500767 SkBlendMode::kModulate)->makeComposed(
Mike Reedf36b37f2020-03-27 15:11:10 -0400768 SkColorFilterPriv::MakeGaussian()));
Mike Reed5caf9352020-03-02 14:57:09 -0500769 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
Jim Van Verth22526362018-02-28 14:51:19 -0500770 success = true;
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400771 }
Jim Van Verth22526362018-02-28 14:51:19 -0500772 }
Jim Van Vertha783c362017-05-11 17:05:28 -0400773
Jim Van Verth22526362018-02-28 14:51:19 -0500774 if (!success) {
775 SpotVerticesFactory factory;
776 factory.fOccluderHeight = zPlaneParams.fZ;
777 factory.fDevLightPos = devLightPos;
778 factory.fLightRadius = lightRadius;
779
Jim Van Verth37c5a962017-05-10 14:13:24 -0400780 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400781 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400782 viewMatrix.mapPoints(&center, 1);
Jim Van Verth22526362018-02-28 14:51:19 -0500783 SkScalar radius, scale;
Jim Van Verth63f03542020-12-16 11:56:11 -0500784 if (SkToBool(rec.fFlags & kDirectionalLight_ShadowFlag)) {
785 SkDrawShadowMetrics::GetDirectionalParams(zPlaneParams.fZ, devLightPos.fX,
786 devLightPos.fY, devLightPos.fZ,
787 lightRadius, &radius, &scale,
788 &factory.fOffset);
789 } else {
790 SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX,
791 devLightPos.fY - center.fY, devLightPos.fZ,
792 lightRadius, &radius, &scale, &factory.fOffset);
793 }
794
Jim Van Vertha783c362017-05-11 17:05:28 -0400795 SkRect devBounds;
796 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth63f03542020-12-16 11:56:11 -0500797 if (directional) {
798 factory.fOccluderType = SpotVerticesFactory::OccluderType::kDirectional;
799 } else if (transparent ||
800 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
801 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400802 // if the translation of the shadow is big enough we're going to end up
803 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400804 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400805 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400806 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400807 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400808 } else if (path.isConvex()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400809 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400810 } else {
811 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Vertha783c362017-05-11 17:05:28 -0400812 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400813 // need to add this after we classify the shadow
814 factory.fOffset.fX += viewMatrix.getTranslateX();
815 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Verth22526362018-02-28 14:51:19 -0500816
817 SkColor color = rec.fSpotColor;
Jim Van Vertha783c362017-05-11 17:05:28 -0400818#ifdef DEBUG_SHADOW_CHECKS
819 switch (factory.fOccluderType) {
820 case SpotVerticesFactory::OccluderType::kTransparent:
821 color = 0xFFD2B48C; // tan for transparent
822 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400823 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400824 color = 0xFFFFA500; // orange for opaque
825 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400826 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
827 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400828 break;
Jim Van Verth63f03542020-12-16 11:56:11 -0500829 case SpotVerticesFactory::OccluderType::kDirectional:
830 color = 0xFF550000; // dark red for directional
831 break;
Jim Van Vertha783c362017-05-11 17:05:28 -0400832 }
833#endif
Jim Van Verth22526362018-02-28 14:51:19 -0500834 if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) {
835 // draw with blur
Jim Van Verth22526362018-02-28 14:51:19 -0500836 SkMatrix shadowMatrix;
Jim Van Verth3a039d52018-09-14 17:14:47 -0400837 if (!SkDrawShadowMetrics::GetSpotShadowTransform(devLightPos, lightRadius,
838 viewMatrix, zPlaneParams,
Jim Van Verth63f03542020-12-16 11:56:11 -0500839 path.getBounds(), directional,
Jim Van Verth3a039d52018-09-14 17:14:47 -0400840 &shadowMatrix, &radius)) {
841 return;
842 }
Michael Ludwigc89d1b52019-10-18 11:32:56 -0400843 SkAutoDeviceTransformRestore adr(this, shadowMatrix);
Jim Van Verth22526362018-02-28 14:51:19 -0500844
845 SkPaint paint;
846 paint.setColor(rec.fSpotColor);
Mike Reed8e03f692018-03-09 16:18:56 -0500847 SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius);
Mike Reed18e75562018-03-12 14:03:47 -0400848 bool respectCTM = false;
849 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM));
Jim Van Verth22526362018-02-28 14:51:19 -0500850 this->drawPath(path, paint);
851 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400852 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400853 }
854}