blob: 402dca5c9265c9d69b5a23569efcb7742296793a [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"
9#include "SkCanvas.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050010#include "SkColorFilter.h"
11#include "SkPath.h"
Brian Salomond1ac9822017-02-03 14:25:02 -050012#include "SkRandom.h"
Brian Salomon5e689522017-02-01 12:07:17 -050013#include "SkResourceCache.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050014#include "SkShadowTessellator.h"
Ben Wagner4d1955c2017-03-10 13:08:15 -050015#include "SkString.h"
Brian Salomon5e689522017-02-01 12:07:17 -050016#include "SkTLazy.h"
Brian Salomonaff27a22017-02-06 15:47:44 -050017#include "SkVertices.h"
Brian Salomon5e689522017-02-01 12:07:17 -050018#if SK_SUPPORT_GPU
19#include "GrShape.h"
20#include "effects/GrBlurredEdgeFragmentProcessor.h"
21#endif
Jim Van Verthcf40e302017-03-02 11:28:43 -050022#include "../../src/effects/shadows/SkAmbientShadowMaskFilter.h"
23#include "../../src/effects/shadows/SkSpotShadowMaskFilter.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050024
25/**
26* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
27* then blends with the color's G value.
28* Final result is black with alpha of Gaussian(B)*G.
29* The assumption is that the original color's alpha is 1.
30*/
31class SK_API SkGaussianColorFilter : public SkColorFilter {
32public:
33 static sk_sp<SkColorFilter> Make() {
34 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
35 }
36
37 void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override;
38
39#if SK_SUPPORT_GPU
40 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override;
41#endif
42
43 SK_TO_STRING_OVERRIDE()
44 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter)
45
46protected:
47 void flatten(SkWriteBuffer&) const override {}
48
49private:
50 SkGaussianColorFilter() : INHERITED() {}
51
52 typedef SkColorFilter INHERITED;
53};
54
55void SkGaussianColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const {
56 for (int i = 0; i < count; ++i) {
57 SkPMColor c = src[i];
58
59 SkScalar factor = SK_Scalar1 - SkGetPackedB32(c) / 255.f;
60 factor = SkScalarExp(-factor * factor * 4) - 0.018f;
61
Brian Salomon0bd699e2017-02-01 12:23:25 -050062 SkScalar a = factor * SkGetPackedG32(c);
63 dst[i] = SkPackARGB32(a, a, a, a);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050064 }
65}
66
67sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
68 return Make();
69}
70
71#ifndef SK_IGNORE_TO_STRING
72void SkGaussianColorFilter::toString(SkString* str) const {
73 str->append("SkGaussianColorFilter ");
74}
75#endif
76
77#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050078
79sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*,
80 SkColorSpace*) const {
81 return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode);
82}
83#endif
84
85///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050086
87namespace {
88
Brian Salomonbc9956d2017-02-22 13:49:09 -050089uint64_t resource_cache_shared_id() {
90 return 0x2020776f64616873llu; // 'shadow '
91}
92
Brian Salomond1ac9822017-02-03 14:25:02 -050093/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -050094struct AmbientVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -050095 SkScalar fRadius = SK_ScalarNaN; // NaN so that isCompatible will always fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -050096 SkColor fUmbraColor;
97 SkColor fPenumbraColor;
98 bool fTransparent;
99
Brian Salomond1ac9822017-02-03 14:25:02 -0500100 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
101 if (fRadius != that.fRadius || fUmbraColor != that.fUmbraColor ||
102 fPenumbraColor != that.fPenumbraColor || fTransparent != that.fTransparent) {
103 return false;
104 }
105 translate->set(0, 0);
106 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500107 }
Brian Salomon5e689522017-02-01 12:07:17 -0500108
Brian Salomonaff27a22017-02-06 15:47:44 -0500109 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const {
110 return SkShadowTessellator::MakeAmbient(path, ctm, fRadius, fUmbraColor, fPenumbraColor,
111 fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500112 }
113};
114
Brian Salomond1ac9822017-02-03 14:25:02 -0500115/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500116struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500117 enum class OccluderType {
118 // The umbra cannot be dropped out because the occluder is not opaque.
119 kTransparent,
120 // The umbra can be dropped where it is occluded.
121 kOpaque,
122 // It is known that the entire umbra is occluded.
123 kOpaqueCoversUmbra
124 };
125
126 SkScalar fRadius = SK_ScalarNaN; // NaN so that isCompatible will always fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500127 SkColor fUmbraColor;
128 SkColor fPenumbraColor;
129 SkScalar fScale;
130 SkVector fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500131 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500132
Brian Salomond1ac9822017-02-03 14:25:02 -0500133 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
134 if (fRadius != that.fRadius || fUmbraColor != that.fUmbraColor ||
135 fPenumbraColor != that.fPenumbraColor || fOccluderType != that.fOccluderType ||
136 fScale != that.fScale) {
137 return false;
138 }
139 switch (fOccluderType) {
140 case OccluderType::kTransparent:
141 case OccluderType::kOpaqueCoversUmbra:
142 // 'this' and 'that' will either both have no umbra removed or both have all the
143 // umbra removed.
144 *translate = that.fOffset - fOffset;
145 return true;
146 case OccluderType::kOpaque:
147 // In this case we partially remove the umbra differently for 'this' and 'that'
148 // if the offsets don't match.
149 if (fOffset == that.fOffset) {
150 translate->set(0, 0);
151 return true;
152 }
153 return false;
154 }
155 SkFAIL("Uninitialized occluder type?");
156 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500157 }
Brian Salomon5e689522017-02-01 12:07:17 -0500158
Brian Salomonaff27a22017-02-06 15:47:44 -0500159 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500160 bool transparent = OccluderType::kTransparent == fOccluderType;
Brian Salomonaff27a22017-02-06 15:47:44 -0500161 return SkShadowTessellator::MakeSpot(path, ctm, fScale, fOffset, fRadius, fUmbraColor,
162 fPenumbraColor, transparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500163 }
164};
165
166/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500167 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
168 * records are immutable this is not itself a Rec. When we need to update it we return this on
169 * the FindVisitor and let the cache destory the Rec. We'll update the tessellations and then add
170 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500171 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500172class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500173public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500174 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
175
Brian Salomonaff27a22017-02-06 15:47:44 -0500176 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
177 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500178 return fAmbientSet.find(ambient, matrix, translate);
179 }
180
Brian Salomonaff27a22017-02-06 15:47:44 -0500181 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
182 const SkMatrix& matrix) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500183 return fAmbientSet.add(devPath, ambient, matrix);
184 }
185
Brian Salomonaff27a22017-02-06 15:47:44 -0500186 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
187 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500188 return fSpotSet.find(spot, matrix, translate);
189 }
190
Brian Salomonaff27a22017-02-06 15:47:44 -0500191 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
192 const SkMatrix& matrix) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500193 return fSpotSet.add(devPath, spot, matrix);
194 }
195
196private:
197 template <typename FACTORY, int MAX_ENTRIES>
198 class Set {
199 public:
200 size_t size() const { return fSize; }
201
Brian Salomonaff27a22017-02-06 15:47:44 -0500202 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
203 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500204 for (int i = 0; i < MAX_ENTRIES; ++i) {
205 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
206 const SkMatrix& m = fEntries[i].fMatrix;
207 if (matrix.hasPerspective() || m.hasPerspective()) {
208 if (matrix != fEntries[i].fMatrix) {
209 continue;
210 }
211 } else if (matrix.getScaleX() != m.getScaleX() ||
212 matrix.getSkewX() != m.getSkewX() ||
213 matrix.getScaleY() != m.getScaleY() ||
214 matrix.getSkewY() != m.getSkewY()) {
215 continue;
216 }
217 *translate += SkVector{matrix.getTranslateX() - m.getTranslateX(),
218 matrix.getTranslateY() - m.getTranslateY()};
219 return fEntries[i].fVertices;
220 }
221 }
222 return nullptr;
223 }
224
Brian Salomonaff27a22017-02-06 15:47:44 -0500225 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix) {
226 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix);
Brian Salomond1ac9822017-02-03 14:25:02 -0500227 if (!vertices) {
228 return nullptr;
229 }
230 int i;
231 if (fCount < MAX_ENTRIES) {
232 i = fCount++;
233 } else {
234 i = gRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400235 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500236 }
237 fEntries[i].fFactory = factory;
238 fEntries[i].fVertices = vertices;
239 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400240 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500241 return vertices;
242 }
243
244 private:
245 struct Entry {
246 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500247 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500248 SkMatrix fMatrix;
249 };
250 Entry fEntries[MAX_ENTRIES];
251 int fCount = 0;
252 size_t fSize = 0;
253 };
254
255 Set<AmbientVerticesFactory, 4> fAmbientSet;
256 Set<SpotVerticesFactory, 4> fSpotSet;
257
258 static SkRandom gRandom;
259};
260
261SkRandom CachedTessellations::gRandom;
262
263/**
264 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
265 * path. The key represents the path's geometry and not any shadow params.
266 */
267class CachedTessellationsRec : public SkResourceCache::Rec {
268public:
269 CachedTessellationsRec(const SkResourceCache::Key& key,
270 sk_sp<CachedTessellations> tessellations)
271 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500272 fKey.reset(new uint8_t[key.size()]);
273 memcpy(fKey.get(), &key, key.size());
274 }
275
276 const Key& getKey() const override {
277 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
278 }
Brian Salomon5e689522017-02-01 12:07:17 -0500279
Brian Salomond1ac9822017-02-03 14:25:02 -0500280 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500281
Brian Salomond1ac9822017-02-03 14:25:02 -0500282 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500283
Brian Salomond1ac9822017-02-03 14:25:02 -0500284 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500285
Brian Salomond1ac9822017-02-03 14:25:02 -0500286 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500287 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
288 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500289 return fTessellations->find(factory, matrix, translate);
290 }
Brian Salomon5e689522017-02-01 12:07:17 -0500291
292private:
293 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500294 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500295};
296
297/**
298 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500299 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
300 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
301 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500302 */
303template <typename FACTORY>
304struct FindContext {
305 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
306 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500307 const SkMatrix* const fViewMatrix;
308 // If this is valid after Find is called then we found the vertices and they should be drawn
309 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500310 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500311 SkVector fTranslate = {0, 0};
312
313 // If this is valid after Find then the caller should add the vertices to the tessellation set
314 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
315 sk_sp<CachedTessellations> fTessellationsOnFailure;
316
Brian Salomon5e689522017-02-01 12:07:17 -0500317 const FACTORY* fFactory;
318};
319
320/**
321 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
322 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
323 * necessary translation vector are set on the FindContext.
324 */
325template <typename FACTORY>
326bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
327 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500328 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
329 findContext->fVertices =
330 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
331 if (findContext->fVertices) {
332 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500333 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500334 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
335 // manipulated we will add a new Rec.
336 findContext->fTessellationsOnFailure = rec.refTessellations();
337 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500338}
339
340class ShadowedPath {
341public:
342 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500343 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500344 , fViewMatrix(viewMatrix)
345#if SK_SUPPORT_GPU
346 , fShapeForKey(*path, GrStyle::SimpleFill())
347#endif
348 {}
349
Jim Van Vertha84898d2017-02-06 13:38:23 -0500350 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500351 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
352#if SK_SUPPORT_GPU
353 /** Negative means the vertices should not be cached for this path. */
354 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
355 void writeKey(void* key) const {
356 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
357 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500358 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500359#else
360 int keyBytes() const { return -1; }
361 void writeKey(void* key) const { SkFAIL("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500362 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500363#endif
364
365private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500366 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500367 const SkMatrix* fViewMatrix;
368#if SK_SUPPORT_GPU
369 GrShape fShapeForKey;
370#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500371};
372
Brian Salomond1ac9822017-02-03 14:25:02 -0500373// This creates a domain of keys in SkResourceCache used by this file.
374static void* kNamespace;
375
Brian Salomon5e689522017-02-01 12:07:17 -0500376/**
377 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
378 * they are first found in SkResourceCache.
379 */
380template <typename FACTORY>
Brian Salomon804e0912017-02-23 09:34:03 -0500381void draw_shadow(const FACTORY& factory, SkCanvas* canvas, ShadowedPath& path, SkColor color,
382 SkResourceCache* cache) {
Brian Salomon5e689522017-02-01 12:07:17 -0500383 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500384
385 SkResourceCache::Key* key = nullptr;
386 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
387 int keyDataBytes = path.keyBytes();
388 if (keyDataBytes >= 0) {
389 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
390 key = new (keyStorage.begin()) SkResourceCache::Key();
391 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500392 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Brian Salomon804e0912017-02-23 09:34:03 -0500393 if (cache) {
394 cache->find(*key, FindVisitor<FACTORY>, &context);
395 } else {
396 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
397 }
Brian Salomon5e689522017-02-01 12:07:17 -0500398 }
399
Brian Salomonaff27a22017-02-06 15:47:44 -0500400 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500401 const SkVector* translate;
402 static constexpr SkVector kZeroTranslate = {0, 0};
403 bool foundInCache = SkToBool(context.fVertices);
404 if (foundInCache) {
405 vertices = std::move(context.fVertices);
406 translate = &context.fTranslate;
407 } 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 Vertha84898d2017-02-06 13:38:23 -0500417 vertices = tessellations->add(path.path(), factory, path.viewMatrix());
Brian Salomond1ac9822017-02-03 14:25:02 -0500418 if (!vertices) {
419 return;
420 }
Brian Salomon804e0912017-02-23 09:34:03 -0500421 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
422 if (cache) {
423 cache->add(rec);
424 } else {
425 SkResourceCache::Add(rec);
426 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500427 } else {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500428 vertices = factory.makeVertices(path.path(), path.viewMatrix());
Brian Salomond1ac9822017-02-03 14:25:02 -0500429 if (!vertices) {
430 return;
431 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500432 }
Brian Salomon5e689522017-02-01 12:07:17 -0500433 translate = &kZeroTranslate;
434 }
435
436 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500437 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
438 // that against our 'color' param.
439 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
440 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
441 SkGaussianColorFilter::Make()));
Brian Salomon5e689522017-02-01 12:07:17 -0500442 if (translate->fX || translate->fY) {
443 canvas->save();
444 canvas->translate(translate->fX, translate->fY);
445 }
Brian Salomonaff27a22017-02-06 15:47:44 -0500446 canvas->drawVertices(vertices, SkBlendMode::kModulate, paint);
Brian Salomon5e689522017-02-01 12:07:17 -0500447 if (translate->fX || translate->fY) {
448 canvas->restore();
449 }
Brian Salomon5e689522017-02-01 12:07:17 -0500450}
451}
452
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500453static const float kHeightFactor = 1.0f / 128.0f;
454static const float kGeomFactor = 64.0f;
Jim Van Verth43475ad2017-01-13 14:37:37 -0500455
456// Draw an offset spot shadow and outlining ambient shadow for the given path.
457void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500458 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verth43475ad2017-01-13 14:37:37 -0500459 SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
Brian Salomon804e0912017-02-23 09:34:03 -0500460 uint32_t flags, SkResourceCache* cache) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500461 SkAutoCanvasRestore acr(canvas, true);
Brian Salomon5e689522017-02-01 12:07:17 -0500462 SkMatrix viewMatrix = canvas->getTotalMatrix();
Jim Van Verthcf40e302017-03-02 11:28:43 -0500463
464 // try circular fast path
465 SkRect rect;
466 if (viewMatrix.isSimilarity() &&
467 path.isOval(&rect) && rect.width() == rect.height()) {
468 SkPaint newPaint;
469 newPaint.setColor(color);
470 if (ambientAlpha > 0) {
471 newPaint.setMaskFilter(SkAmbientShadowMaskFilter::Make(occluderHeight, ambientAlpha,
472 flags));
473 canvas->drawPath(path, newPaint);
474 }
475 if (spotAlpha > 0) {
476 newPaint.setMaskFilter(SkSpotShadowMaskFilter::Make(occluderHeight, devLightPos,
477 lightRadius, spotAlpha, flags));
478 canvas->drawPath(path, newPaint);
479 }
480 return;
481 }
482
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500483 canvas->resetMatrix();
484
Brian Salomon5e689522017-02-01 12:07:17 -0500485 ShadowedPath shadowedPath(&path, &viewMatrix);
486
Brian Salomon958fbc42017-01-30 17:01:28 -0500487 bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
488
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500489 if (ambientAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500490 ambientAlpha = SkTMin(ambientAlpha, 1.f);
Brian Salomon5e689522017-02-01 12:07:17 -0500491 AmbientVerticesFactory factory;
492 factory.fRadius = occluderHeight * kHeightFactor * kGeomFactor;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500493 SkScalar umbraAlpha = SkScalarInvert((1.0f + SkTMax(occluderHeight*kHeightFactor, 0.0f)));
494 // umbraColor is the interior value, penumbraColor the exterior value.
495 // umbraAlpha is the factor that is linearly interpolated from outside to inside, and
496 // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get
497 // the final alpha.
Brian Salomon5e689522017-02-01 12:07:17 -0500498 factory.fUmbraColor =
499 SkColorSetARGB(255, 0, ambientAlpha * 255.9999f, umbraAlpha * 255.9999f);
500 factory.fPenumbraColor = SkColorSetARGB(255, 0, ambientAlpha * 255.9999f, 0);
501 factory.fTransparent = transparent;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500502
Brian Salomon804e0912017-02-23 09:34:03 -0500503 draw_shadow(factory, canvas, shadowedPath, color, cache);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500504 }
505
506 if (spotAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500507 spotAlpha = SkTMin(spotAlpha, 1.f);
Brian Salomon5e689522017-02-01 12:07:17 -0500508 SpotVerticesFactory factory;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500509 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
Brian Salomon5e689522017-02-01 12:07:17 -0500510 factory.fRadius = lightRadius * zRatio;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500511
512 // Compute the scale and translation for the spot shadow.
Brian Salomon0bd699e2017-02-01 12:23:25 -0500513 factory.fScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500514
515 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Brian Salomon0bd699e2017-02-01 12:23:25 -0500516 viewMatrix.mapPoints(&center, 1);
517 factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX),
518 zRatio * (center.fY - devLightPos.fY));
Brian Salomon5e689522017-02-01 12:07:17 -0500519 factory.fUmbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 255);
520 factory.fPenumbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 0);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500521
Brian Salomond1ac9822017-02-03 14:25:02 -0500522 SkRRect rrect;
523 if (transparent) {
524 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
525 } else {
526 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaque;
527 if (shadowedPath.isRRect(&rrect)) {
528 SkRRect devRRect;
529 if (rrect.transform(viewMatrix, &devRRect)) {
530 SkScalar s = 1.f - factory.fScale;
531 SkScalar w = devRRect.width();
532 SkScalar h = devRRect.height();
533 SkScalar hw = w / 2.f;
534 SkScalar hh = h / 2.f;
535 SkScalar umbraInsetX = s * hw + factory.fRadius;
536 SkScalar umbraInsetY = s * hh + factory.fRadius;
Brian Salomon67386d42017-02-06 14:23:53 -0500537 // The umbra is inset by radius along the diagonal, so adjust for that.
538 SkScalar d = 1.f / SkScalarSqrt(hw * hw + hh * hh);
539 umbraInsetX *= hw * d;
540 umbraInsetY *= hh * d;
Brian Salomond1ac9822017-02-03 14:25:02 -0500541 if (umbraInsetX > hw || umbraInsetY > hh) {
542 // There is no umbra to occlude.
543 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
544 } else if (fabsf(factory.fOffset.fX) < umbraInsetX &&
545 fabsf(factory.fOffset.fY) < umbraInsetY) {
546 factory.fOccluderType =
547 SpotVerticesFactory::OccluderType::kOpaqueCoversUmbra;
548 } else if (factory.fOffset.fX > w - umbraInsetX ||
549 factory.fOffset.fY > h - umbraInsetY) {
550 // There umbra is fully exposed, there is nothing to omit.
551 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
552 }
553 }
554 }
555 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500556 if (factory.fOccluderType == SpotVerticesFactory::OccluderType::kOpaque) {
557 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
558 }
Brian Salomon804e0912017-02-23 09:34:03 -0500559 draw_shadow(factory, canvas, shadowedPath, color, cache);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500560 }
Jim Van Verth43475ad2017-01-13 14:37:37 -0500561}