blob: 2e560b454eb78e19bd19f4983d94fe7476c7d5f0 [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 {
Jim Van Verthb4366552017-03-27 14:25:29 -040095 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
96 SkScalar fAmbientAlpha;
Brian Salomon5e689522017-02-01 12:07:17 -050097 bool fTransparent;
98
Brian Salomond1ac9822017-02-03 14:25:02 -050099 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400100 if (fOccluderHeight != that.fOccluderHeight || fAmbientAlpha != that.fAmbientAlpha ||
101 fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500102 return false;
103 }
104 translate->set(0, 0);
105 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500106 }
Brian Salomon5e689522017-02-01 12:07:17 -0500107
Brian Salomonaff27a22017-02-06 15:47:44 -0500108 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400109 SkScalar z = fOccluderHeight;
110 return SkShadowTessellator::MakeAmbient(path, ctm,
111 [z](SkScalar, SkScalar) { return z; },
112 fAmbientAlpha, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500113 }
114};
115
Brian Salomond1ac9822017-02-03 14:25:02 -0500116/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500117struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500118 enum class OccluderType {
119 // The umbra cannot be dropped out because the occluder is not opaque.
120 kTransparent,
121 // The umbra can be dropped where it is occluded.
122 kOpaque,
123 // It is known that the entire umbra is occluded.
124 kOpaqueCoversUmbra
125 };
126
Brian Salomon5e689522017-02-01 12:07:17 -0500127 SkVector fOffset;
Jim Van Verthb4366552017-03-27 14:25:29 -0400128 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
129 SkPoint3 fDevLightPos;
130 SkScalar fLightRadius;
131 SkScalar fSpotAlpha;
Brian Salomond1ac9822017-02-03 14:25:02 -0500132 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500133
Brian Salomond1ac9822017-02-03 14:25:02 -0500134 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400135 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
136 fLightRadius != that.fLightRadius || fSpotAlpha != that.fSpotAlpha ||
137 fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500138 return false;
139 }
140 switch (fOccluderType) {
141 case OccluderType::kTransparent:
142 case OccluderType::kOpaqueCoversUmbra:
143 // 'this' and 'that' will either both have no umbra removed or both have all the
144 // umbra removed.
145 *translate = that.fOffset - fOffset;
146 return true;
147 case OccluderType::kOpaque:
148 // In this case we partially remove the umbra differently for 'this' and 'that'
149 // if the offsets don't match.
150 if (fOffset == that.fOffset) {
151 translate->set(0, 0);
152 return true;
153 }
154 return false;
155 }
156 SkFAIL("Uninitialized occluder type?");
157 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500158 }
Brian Salomon5e689522017-02-01 12:07:17 -0500159
Brian Salomonaff27a22017-02-06 15:47:44 -0500160 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500161 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthb4366552017-03-27 14:25:29 -0400162 SkScalar z = fOccluderHeight;
163 return SkShadowTessellator::MakeSpot(path, ctm,
164 [z](SkScalar, SkScalar) -> SkScalar { return z; },
165 fDevLightPos, fLightRadius,
166 fSpotAlpha, transparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500167 }
168};
169
170/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500171 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
172 * records are immutable this is not itself a Rec. When we need to update it we return this on
173 * the FindVisitor and let the cache destory the Rec. We'll update the tessellations and then add
174 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500175 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500176class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500177public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500178 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
179
Brian Salomonaff27a22017-02-06 15:47:44 -0500180 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
181 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500182 return fAmbientSet.find(ambient, matrix, translate);
183 }
184
Brian Salomonaff27a22017-02-06 15:47:44 -0500185 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
186 const SkMatrix& matrix) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500187 return fAmbientSet.add(devPath, ambient, matrix);
188 }
189
Brian Salomonaff27a22017-02-06 15:47:44 -0500190 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
191 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500192 return fSpotSet.find(spot, matrix, translate);
193 }
194
Brian Salomonaff27a22017-02-06 15:47:44 -0500195 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
196 const SkMatrix& matrix) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500197 return fSpotSet.add(devPath, spot, matrix);
198 }
199
200private:
201 template <typename FACTORY, int MAX_ENTRIES>
202 class Set {
203 public:
204 size_t size() const { return fSize; }
205
Brian Salomonaff27a22017-02-06 15:47:44 -0500206 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
207 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500208 for (int i = 0; i < MAX_ENTRIES; ++i) {
209 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
210 const SkMatrix& m = fEntries[i].fMatrix;
211 if (matrix.hasPerspective() || m.hasPerspective()) {
212 if (matrix != fEntries[i].fMatrix) {
213 continue;
214 }
215 } else if (matrix.getScaleX() != m.getScaleX() ||
216 matrix.getSkewX() != m.getSkewX() ||
217 matrix.getScaleY() != m.getScaleY() ||
218 matrix.getSkewY() != m.getSkewY()) {
219 continue;
220 }
221 *translate += SkVector{matrix.getTranslateX() - m.getTranslateX(),
222 matrix.getTranslateY() - m.getTranslateY()};
223 return fEntries[i].fVertices;
224 }
225 }
226 return nullptr;
227 }
228
Brian Salomonaff27a22017-02-06 15:47:44 -0500229 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix) {
230 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix);
Brian Salomond1ac9822017-02-03 14:25:02 -0500231 if (!vertices) {
232 return nullptr;
233 }
234 int i;
235 if (fCount < MAX_ENTRIES) {
236 i = fCount++;
237 } else {
238 i = gRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400239 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500240 }
241 fEntries[i].fFactory = factory;
242 fEntries[i].fVertices = vertices;
243 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400244 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500245 return vertices;
246 }
247
248 private:
249 struct Entry {
250 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500251 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500252 SkMatrix fMatrix;
253 };
254 Entry fEntries[MAX_ENTRIES];
255 int fCount = 0;
256 size_t fSize = 0;
257 };
258
259 Set<AmbientVerticesFactory, 4> fAmbientSet;
260 Set<SpotVerticesFactory, 4> fSpotSet;
261
262 static SkRandom gRandom;
263};
264
265SkRandom CachedTessellations::gRandom;
266
267/**
268 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
269 * path. The key represents the path's geometry and not any shadow params.
270 */
271class CachedTessellationsRec : public SkResourceCache::Rec {
272public:
273 CachedTessellationsRec(const SkResourceCache::Key& key,
274 sk_sp<CachedTessellations> tessellations)
275 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500276 fKey.reset(new uint8_t[key.size()]);
277 memcpy(fKey.get(), &key, key.size());
278 }
279
280 const Key& getKey() const override {
281 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
282 }
Brian Salomon5e689522017-02-01 12:07:17 -0500283
Brian Salomond1ac9822017-02-03 14:25:02 -0500284 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500285
Brian Salomond1ac9822017-02-03 14:25:02 -0500286 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500287
Brian Salomond1ac9822017-02-03 14:25:02 -0500288 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500289
Brian Salomond1ac9822017-02-03 14:25:02 -0500290 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500291 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
292 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500293 return fTessellations->find(factory, matrix, translate);
294 }
Brian Salomon5e689522017-02-01 12:07:17 -0500295
296private:
297 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500298 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500299};
300
301/**
302 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500303 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
304 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
305 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500306 */
307template <typename FACTORY>
308struct FindContext {
309 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
310 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500311 const SkMatrix* const fViewMatrix;
312 // If this is valid after Find is called then we found the vertices and they should be drawn
313 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500314 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500315 SkVector fTranslate = {0, 0};
316
317 // If this is valid after Find then the caller should add the vertices to the tessellation set
318 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
319 sk_sp<CachedTessellations> fTessellationsOnFailure;
320
Brian Salomon5e689522017-02-01 12:07:17 -0500321 const FACTORY* fFactory;
322};
323
324/**
325 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
326 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
327 * necessary translation vector are set on the FindContext.
328 */
329template <typename FACTORY>
330bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
331 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500332 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
333 findContext->fVertices =
334 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
335 if (findContext->fVertices) {
336 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500337 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500338 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
339 // manipulated we will add a new Rec.
340 findContext->fTessellationsOnFailure = rec.refTessellations();
341 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500342}
343
344class ShadowedPath {
345public:
346 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500347 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500348 , fViewMatrix(viewMatrix)
349#if SK_SUPPORT_GPU
350 , fShapeForKey(*path, GrStyle::SimpleFill())
351#endif
352 {}
353
Jim Van Vertha84898d2017-02-06 13:38:23 -0500354 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500355 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
356#if SK_SUPPORT_GPU
357 /** Negative means the vertices should not be cached for this path. */
358 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
359 void writeKey(void* key) const {
360 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
361 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500362 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500363#else
364 int keyBytes() const { return -1; }
365 void writeKey(void* key) const { SkFAIL("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500366 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500367#endif
368
369private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500370 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500371 const SkMatrix* fViewMatrix;
372#if SK_SUPPORT_GPU
373 GrShape fShapeForKey;
374#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500375};
376
Brian Salomond1ac9822017-02-03 14:25:02 -0500377// This creates a domain of keys in SkResourceCache used by this file.
378static void* kNamespace;
379
Brian Salomon5e689522017-02-01 12:07:17 -0500380/**
381 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
382 * they are first found in SkResourceCache.
383 */
384template <typename FACTORY>
Brian Salomon804e0912017-02-23 09:34:03 -0500385void draw_shadow(const FACTORY& factory, SkCanvas* canvas, ShadowedPath& path, SkColor color,
386 SkResourceCache* cache) {
Brian Salomon5e689522017-02-01 12:07:17 -0500387 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500388
389 SkResourceCache::Key* key = nullptr;
390 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
391 int keyDataBytes = path.keyBytes();
392 if (keyDataBytes >= 0) {
393 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
394 key = new (keyStorage.begin()) SkResourceCache::Key();
395 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500396 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Brian Salomon804e0912017-02-23 09:34:03 -0500397 if (cache) {
398 cache->find(*key, FindVisitor<FACTORY>, &context);
399 } else {
400 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
401 }
Brian Salomon5e689522017-02-01 12:07:17 -0500402 }
403
Brian Salomonaff27a22017-02-06 15:47:44 -0500404 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500405 const SkVector* translate;
406 static constexpr SkVector kZeroTranslate = {0, 0};
407 bool foundInCache = SkToBool(context.fVertices);
408 if (foundInCache) {
409 vertices = std::move(context.fVertices);
410 translate = &context.fTranslate;
411 } else {
412 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500413 if (key) {
414 // Update or initialize a tessellation set and add it to the cache.
415 sk_sp<CachedTessellations> tessellations;
416 if (context.fTessellationsOnFailure) {
417 tessellations = std::move(context.fTessellationsOnFailure);
418 } else {
419 tessellations.reset(new CachedTessellations());
420 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500421 vertices = tessellations->add(path.path(), factory, path.viewMatrix());
Brian Salomond1ac9822017-02-03 14:25:02 -0500422 if (!vertices) {
423 return;
424 }
Brian Salomon804e0912017-02-23 09:34:03 -0500425 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
426 if (cache) {
427 cache->add(rec);
428 } else {
429 SkResourceCache::Add(rec);
430 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500431 } else {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500432 vertices = factory.makeVertices(path.path(), path.viewMatrix());
Brian Salomond1ac9822017-02-03 14:25:02 -0500433 if (!vertices) {
434 return;
435 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500436 }
Brian Salomon5e689522017-02-01 12:07:17 -0500437 translate = &kZeroTranslate;
438 }
439
440 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500441 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
442 // that against our 'color' param.
443 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
444 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
445 SkGaussianColorFilter::Make()));
Brian Salomon5e689522017-02-01 12:07:17 -0500446 if (translate->fX || translate->fY) {
447 canvas->save();
448 canvas->translate(translate->fX, translate->fY);
449 }
Brian Salomonaff27a22017-02-06 15:47:44 -0500450 canvas->drawVertices(vertices, SkBlendMode::kModulate, paint);
Brian Salomon5e689522017-02-01 12:07:17 -0500451 if (translate->fX || translate->fY) {
452 canvas->restore();
453 }
Brian Salomon5e689522017-02-01 12:07:17 -0500454}
455}
456
Jim Van Verth43475ad2017-01-13 14:37:37 -0500457// Draw an offset spot shadow and outlining ambient shadow for the given path.
458void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500459 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verth43475ad2017-01-13 14:37:37 -0500460 SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
Brian Salomon804e0912017-02-23 09:34:03 -0500461 uint32_t flags, SkResourceCache* cache) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500462 SkAutoCanvasRestore acr(canvas, true);
Brian Salomon5e689522017-02-01 12:07:17 -0500463 SkMatrix viewMatrix = canvas->getTotalMatrix();
Jim Van Verthcf40e302017-03-02 11:28:43 -0500464
465 // try circular fast path
466 SkRect rect;
467 if (viewMatrix.isSimilarity() &&
468 path.isOval(&rect) && rect.width() == rect.height()) {
469 SkPaint newPaint;
470 newPaint.setColor(color);
471 if (ambientAlpha > 0) {
472 newPaint.setMaskFilter(SkAmbientShadowMaskFilter::Make(occluderHeight, ambientAlpha,
473 flags));
474 canvas->drawPath(path, newPaint);
475 }
476 if (spotAlpha > 0) {
477 newPaint.setMaskFilter(SkSpotShadowMaskFilter::Make(occluderHeight, devLightPos,
478 lightRadius, spotAlpha, flags));
479 canvas->drawPath(path, newPaint);
480 }
481 return;
482 }
483
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500484 canvas->resetMatrix();
485
Brian Salomon5e689522017-02-01 12:07:17 -0500486 ShadowedPath shadowedPath(&path, &viewMatrix);
487
Brian Salomon958fbc42017-01-30 17:01:28 -0500488 bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
489
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500490 if (ambientAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500491 ambientAlpha = SkTMin(ambientAlpha, 1.f);
Brian Salomon5e689522017-02-01 12:07:17 -0500492 AmbientVerticesFactory factory;
Jim Van Verthb4366552017-03-27 14:25:29 -0400493 factory.fOccluderHeight = occluderHeight;
494 factory.fAmbientAlpha = ambientAlpha;
Brian Salomon5e689522017-02-01 12:07:17 -0500495 factory.fTransparent = transparent;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500496
Brian Salomon804e0912017-02-23 09:34:03 -0500497 draw_shadow(factory, canvas, shadowedPath, color, cache);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500498 }
499
500 if (spotAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500501 spotAlpha = SkTMin(spotAlpha, 1.f);
Brian Salomon5e689522017-02-01 12:07:17 -0500502 SpotVerticesFactory factory;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500503 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
Jim Van Verthb4366552017-03-27 14:25:29 -0400504 SkScalar radius = lightRadius * zRatio;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500505
506 // Compute the scale and translation for the spot shadow.
Jim Van Verthb4366552017-03-27 14:25:29 -0400507 SkScalar scale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500508
509 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Brian Salomon0bd699e2017-02-01 12:23:25 -0500510 viewMatrix.mapPoints(&center, 1);
511 factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX),
512 zRatio * (center.fY - devLightPos.fY));
Jim Van Verthb4366552017-03-27 14:25:29 -0400513 factory.fOccluderHeight = occluderHeight;
514 factory.fDevLightPos = devLightPos;
515 factory.fLightRadius = lightRadius;
516 factory.fSpotAlpha = spotAlpha;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500517
Brian Salomond1ac9822017-02-03 14:25:02 -0500518 SkRRect rrect;
519 if (transparent) {
520 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
521 } else {
522 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaque;
523 if (shadowedPath.isRRect(&rrect)) {
524 SkRRect devRRect;
525 if (rrect.transform(viewMatrix, &devRRect)) {
Jim Van Verthb4366552017-03-27 14:25:29 -0400526 SkScalar s = 1.f - scale;
Brian Salomond1ac9822017-02-03 14:25:02 -0500527 SkScalar w = devRRect.width();
528 SkScalar h = devRRect.height();
529 SkScalar hw = w / 2.f;
530 SkScalar hh = h / 2.f;
Jim Van Verthb4366552017-03-27 14:25:29 -0400531 SkScalar umbraInsetX = s * hw + radius;
532 SkScalar umbraInsetY = s * hh + radius;
Brian Salomon67386d42017-02-06 14:23:53 -0500533 // The umbra is inset by radius along the diagonal, so adjust for that.
534 SkScalar d = 1.f / SkScalarSqrt(hw * hw + hh * hh);
535 umbraInsetX *= hw * d;
536 umbraInsetY *= hh * d;
Brian Salomond1ac9822017-02-03 14:25:02 -0500537 if (umbraInsetX > hw || umbraInsetY > hh) {
538 // There is no umbra to occlude.
539 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
540 } else if (fabsf(factory.fOffset.fX) < umbraInsetX &&
541 fabsf(factory.fOffset.fY) < umbraInsetY) {
542 factory.fOccluderType =
543 SpotVerticesFactory::OccluderType::kOpaqueCoversUmbra;
544 } else if (factory.fOffset.fX > w - umbraInsetX ||
545 factory.fOffset.fY > h - umbraInsetY) {
546 // There umbra is fully exposed, there is nothing to omit.
547 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
548 }
549 }
550 }
551 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500552 if (factory.fOccluderType == SpotVerticesFactory::OccluderType::kOpaque) {
553 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
554 }
Brian Salomon804e0912017-02-23 09:34:03 -0500555 draw_shadow(factory, canvas, shadowedPath, color, cache);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500556 }
Jim Van Verth43475ad2017-01-13 14:37:37 -0500557}
Jim Van Verthb4366552017-03-27 14:25:29 -0400558
559// Draw an offset spot shadow and outlining ambient shadow for the given path,
560// without caching and using a function based on local position to compute the height.
561void SkShadowUtils::DrawUncachedShadow(SkCanvas* canvas, const SkPath& path,
562 std::function<SkScalar(SkScalar, SkScalar)> heightFunc,
563 const SkPoint3& lightPos, SkScalar lightRadius,
564 SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
565 uint32_t flags) {
566 SkAutoCanvasRestore acr(canvas, true);
567 SkMatrix viewMatrix = canvas->getTotalMatrix();
568 canvas->resetMatrix();
569
570 bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
571
572 if (ambientAlpha > 0) {
573 ambientAlpha = SkTMin(ambientAlpha, 1.f);
574 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
575 heightFunc, ambientAlpha,
576 transparent);
577 SkPaint paint;
578 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale
579 // result of that against our 'color' param.
580 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
581 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
582 SkGaussianColorFilter::Make()));
583 canvas->drawVertices(vertices, SkBlendMode::kModulate, paint);
584 }
585
586 if (spotAlpha > 0) {
587 spotAlpha = SkTMin(spotAlpha, 1.f);
588 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix, heightFunc,
589 lightPos, lightRadius,
590 spotAlpha, transparent);
591 SkPaint paint;
592 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale
593 // result of that against our 'color' param.
594 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
595 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
596 SkGaussianColorFilter::Make()));
597 canvas->drawVertices(vertices, SkBlendMode::kModulate, paint);
598 }
599}