blob: 8374aef68f164843f48a8ee3adb6c2663f5c092c [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"
Brian Salomon5e689522017-02-01 12:07:17 -050015#include "SkTLazy.h"
Brian Salomonaff27a22017-02-06 15:47:44 -050016#include "SkVertices.h"
Brian Salomon5e689522017-02-01 12:07:17 -050017#if SK_SUPPORT_GPU
18#include "GrShape.h"
19#include "effects/GrBlurredEdgeFragmentProcessor.h"
20#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050021
22/**
23* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
24* then blends with the color's G value.
25* Final result is black with alpha of Gaussian(B)*G.
26* The assumption is that the original color's alpha is 1.
27*/
28class SK_API SkGaussianColorFilter : public SkColorFilter {
29public:
30 static sk_sp<SkColorFilter> Make() {
31 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
32 }
33
34 void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override;
35
36#if SK_SUPPORT_GPU
37 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override;
38#endif
39
40 SK_TO_STRING_OVERRIDE()
41 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter)
42
43protected:
44 void flatten(SkWriteBuffer&) const override {}
45
46private:
47 SkGaussianColorFilter() : INHERITED() {}
48
49 typedef SkColorFilter INHERITED;
50};
51
52void SkGaussianColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const {
53 for (int i = 0; i < count; ++i) {
54 SkPMColor c = src[i];
55
56 SkScalar factor = SK_Scalar1 - SkGetPackedB32(c) / 255.f;
57 factor = SkScalarExp(-factor * factor * 4) - 0.018f;
58
Brian Salomon0bd699e2017-02-01 12:23:25 -050059 SkScalar a = factor * SkGetPackedG32(c);
60 dst[i] = SkPackARGB32(a, a, a, a);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050061 }
62}
63
64sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
65 return Make();
66}
67
68#ifndef SK_IGNORE_TO_STRING
69void SkGaussianColorFilter::toString(SkString* str) const {
70 str->append("SkGaussianColorFilter ");
71}
72#endif
73
74#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050075
76sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*,
77 SkColorSpace*) const {
78 return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode);
79}
80#endif
81
82///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050083
84namespace {
85
Brian Salomond1ac9822017-02-03 14:25:02 -050086/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -050087struct AmbientVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -050088 SkScalar fRadius = SK_ScalarNaN; // NaN so that isCompatible will always fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -050089 SkColor fUmbraColor;
90 SkColor fPenumbraColor;
91 bool fTransparent;
92
Brian Salomond1ac9822017-02-03 14:25:02 -050093 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
94 if (fRadius != that.fRadius || fUmbraColor != that.fUmbraColor ||
95 fPenumbraColor != that.fPenumbraColor || fTransparent != that.fTransparent) {
96 return false;
97 }
98 translate->set(0, 0);
99 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500100 }
Brian Salomon5e689522017-02-01 12:07:17 -0500101
Brian Salomonaff27a22017-02-06 15:47:44 -0500102 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const {
103 return SkShadowTessellator::MakeAmbient(path, ctm, fRadius, fUmbraColor, fPenumbraColor,
104 fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500105 }
106};
107
Brian Salomond1ac9822017-02-03 14:25:02 -0500108/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500109struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500110 enum class OccluderType {
111 // The umbra cannot be dropped out because the occluder is not opaque.
112 kTransparent,
113 // The umbra can be dropped where it is occluded.
114 kOpaque,
115 // It is known that the entire umbra is occluded.
116 kOpaqueCoversUmbra
117 };
118
119 SkScalar fRadius = SK_ScalarNaN; // NaN so that isCompatible will always fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -0500120 SkColor fUmbraColor;
121 SkColor fPenumbraColor;
122 SkScalar fScale;
123 SkVector fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500124 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500125
Brian Salomond1ac9822017-02-03 14:25:02 -0500126 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
127 if (fRadius != that.fRadius || fUmbraColor != that.fUmbraColor ||
128 fPenumbraColor != that.fPenumbraColor || fOccluderType != that.fOccluderType ||
129 fScale != that.fScale) {
130 return false;
131 }
132 switch (fOccluderType) {
133 case OccluderType::kTransparent:
134 case OccluderType::kOpaqueCoversUmbra:
135 // 'this' and 'that' will either both have no umbra removed or both have all the
136 // umbra removed.
137 *translate = that.fOffset - fOffset;
138 return true;
139 case OccluderType::kOpaque:
140 // In this case we partially remove the umbra differently for 'this' and 'that'
141 // if the offsets don't match.
142 if (fOffset == that.fOffset) {
143 translate->set(0, 0);
144 return true;
145 }
146 return false;
147 }
148 SkFAIL("Uninitialized occluder type?");
149 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500150 }
Brian Salomon5e689522017-02-01 12:07:17 -0500151
Brian Salomonaff27a22017-02-06 15:47:44 -0500152 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500153 bool transparent = OccluderType::kTransparent == fOccluderType;
Brian Salomonaff27a22017-02-06 15:47:44 -0500154 return SkShadowTessellator::MakeSpot(path, ctm, fScale, fOffset, fRadius, fUmbraColor,
155 fPenumbraColor, transparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500156 }
157};
158
159/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500160 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
161 * records are immutable this is not itself a Rec. When we need to update it we return this on
162 * the FindVisitor and let the cache destory the Rec. We'll update the tessellations and then add
163 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500164 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500165class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500166public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500167 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
168
Brian Salomonaff27a22017-02-06 15:47:44 -0500169 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
170 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500171 return fAmbientSet.find(ambient, matrix, translate);
172 }
173
Brian Salomonaff27a22017-02-06 15:47:44 -0500174 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
175 const SkMatrix& matrix) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500176 return fAmbientSet.add(devPath, ambient, matrix);
177 }
178
Brian Salomonaff27a22017-02-06 15:47:44 -0500179 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
180 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500181 return fSpotSet.find(spot, matrix, translate);
182 }
183
Brian Salomonaff27a22017-02-06 15:47:44 -0500184 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
185 const SkMatrix& matrix) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500186 return fSpotSet.add(devPath, spot, matrix);
187 }
188
189private:
190 template <typename FACTORY, int MAX_ENTRIES>
191 class Set {
192 public:
193 size_t size() const { return fSize; }
194
Brian Salomonaff27a22017-02-06 15:47:44 -0500195 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
196 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500197 for (int i = 0; i < MAX_ENTRIES; ++i) {
198 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
199 const SkMatrix& m = fEntries[i].fMatrix;
200 if (matrix.hasPerspective() || m.hasPerspective()) {
201 if (matrix != fEntries[i].fMatrix) {
202 continue;
203 }
204 } else if (matrix.getScaleX() != m.getScaleX() ||
205 matrix.getSkewX() != m.getSkewX() ||
206 matrix.getScaleY() != m.getScaleY() ||
207 matrix.getSkewY() != m.getSkewY()) {
208 continue;
209 }
210 *translate += SkVector{matrix.getTranslateX() - m.getTranslateX(),
211 matrix.getTranslateY() - m.getTranslateY()};
212 return fEntries[i].fVertices;
213 }
214 }
215 return nullptr;
216 }
217
Brian Salomonaff27a22017-02-06 15:47:44 -0500218 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix) {
219 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix);
Brian Salomond1ac9822017-02-03 14:25:02 -0500220 if (!vertices) {
221 return nullptr;
222 }
223 int i;
224 if (fCount < MAX_ENTRIES) {
225 i = fCount++;
226 } else {
227 i = gRandom.nextULessThan(MAX_ENTRIES);
228 fSize -= fEntries[i].fVertices->size();
229 }
230 fEntries[i].fFactory = factory;
231 fEntries[i].fVertices = vertices;
232 fEntries[i].fMatrix = matrix;
233 fSize += vertices->size();
234 return vertices;
235 }
236
237 private:
238 struct Entry {
239 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500240 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500241 SkMatrix fMatrix;
242 };
243 Entry fEntries[MAX_ENTRIES];
244 int fCount = 0;
245 size_t fSize = 0;
246 };
247
248 Set<AmbientVerticesFactory, 4> fAmbientSet;
249 Set<SpotVerticesFactory, 4> fSpotSet;
250
251 static SkRandom gRandom;
252};
253
254SkRandom CachedTessellations::gRandom;
255
256/**
257 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
258 * path. The key represents the path's geometry and not any shadow params.
259 */
260class CachedTessellationsRec : public SkResourceCache::Rec {
261public:
262 CachedTessellationsRec(const SkResourceCache::Key& key,
263 sk_sp<CachedTessellations> tessellations)
264 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500265 fKey.reset(new uint8_t[key.size()]);
266 memcpy(fKey.get(), &key, key.size());
267 }
268
269 const Key& getKey() const override {
270 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
271 }
Brian Salomon5e689522017-02-01 12:07:17 -0500272
Brian Salomond1ac9822017-02-03 14:25:02 -0500273 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500274
Brian Salomond1ac9822017-02-03 14:25:02 -0500275 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500276
Brian Salomond1ac9822017-02-03 14:25:02 -0500277 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500278
Brian Salomond1ac9822017-02-03 14:25:02 -0500279 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500280 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
281 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500282 return fTessellations->find(factory, matrix, translate);
283 }
Brian Salomon5e689522017-02-01 12:07:17 -0500284
285private:
286 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500287 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500288};
289
290/**
291 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500292 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
293 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
294 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500295 */
296template <typename FACTORY>
297struct FindContext {
298 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
299 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500300 const SkMatrix* const fViewMatrix;
301 // If this is valid after Find is called then we found the vertices and they should be drawn
302 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500303 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500304 SkVector fTranslate = {0, 0};
305
306 // If this is valid after Find then the caller should add the vertices to the tessellation set
307 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
308 sk_sp<CachedTessellations> fTessellationsOnFailure;
309
Brian Salomon5e689522017-02-01 12:07:17 -0500310 const FACTORY* fFactory;
311};
312
313/**
314 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
315 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
316 * necessary translation vector are set on the FindContext.
317 */
318template <typename FACTORY>
319bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
320 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500321 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
322 findContext->fVertices =
323 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
324 if (findContext->fVertices) {
325 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500326 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500327 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
328 // manipulated we will add a new Rec.
329 findContext->fTessellationsOnFailure = rec.refTessellations();
330 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500331}
332
333class ShadowedPath {
334public:
335 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500336 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500337 , fViewMatrix(viewMatrix)
338#if SK_SUPPORT_GPU
339 , fShapeForKey(*path, GrStyle::SimpleFill())
340#endif
341 {}
342
Jim Van Vertha84898d2017-02-06 13:38:23 -0500343 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500344 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
345#if SK_SUPPORT_GPU
346 /** Negative means the vertices should not be cached for this path. */
347 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
348 void writeKey(void* key) const {
349 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
350 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500351 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500352#else
353 int keyBytes() const { return -1; }
354 void writeKey(void* key) const { SkFAIL("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500355 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500356#endif
357
358private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500359 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500360 const SkMatrix* fViewMatrix;
361#if SK_SUPPORT_GPU
362 GrShape fShapeForKey;
363#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500364};
365
Brian Salomond1ac9822017-02-03 14:25:02 -0500366// This creates a domain of keys in SkResourceCache used by this file.
367static void* kNamespace;
368
Brian Salomon5e689522017-02-01 12:07:17 -0500369/**
370 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
371 * they are first found in SkResourceCache.
372 */
373template <typename FACTORY>
374void draw_shadow(const FACTORY& factory, SkCanvas* canvas, ShadowedPath& path, SkColor color) {
375 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500376
377 SkResourceCache::Key* key = nullptr;
378 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
379 int keyDataBytes = path.keyBytes();
380 if (keyDataBytes >= 0) {
381 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
382 key = new (keyStorage.begin()) SkResourceCache::Key();
383 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
384 key->init(&kNamespace, 0, keyDataBytes);
385 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
386 }
387
Brian Salomonaff27a22017-02-06 15:47:44 -0500388 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500389 const SkVector* translate;
390 static constexpr SkVector kZeroTranslate = {0, 0};
391 bool foundInCache = SkToBool(context.fVertices);
392 if (foundInCache) {
393 vertices = std::move(context.fVertices);
394 translate = &context.fTranslate;
395 } else {
396 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500397 if (key) {
398 // Update or initialize a tessellation set and add it to the cache.
399 sk_sp<CachedTessellations> tessellations;
400 if (context.fTessellationsOnFailure) {
401 tessellations = std::move(context.fTessellationsOnFailure);
402 } else {
403 tessellations.reset(new CachedTessellations());
404 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500405 vertices = tessellations->add(path.path(), factory, path.viewMatrix());
Brian Salomond1ac9822017-02-03 14:25:02 -0500406 if (!vertices) {
407 return;
408 }
409 SkResourceCache::Add(new CachedTessellationsRec(*key, std::move(tessellations)));
410 } else {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500411 vertices = factory.makeVertices(path.path(), path.viewMatrix());
Brian Salomond1ac9822017-02-03 14:25:02 -0500412 if (!vertices) {
413 return;
414 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500415 }
Brian Salomon5e689522017-02-01 12:07:17 -0500416 translate = &kZeroTranslate;
417 }
418
419 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500420 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
421 // that against our 'color' param.
422 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
423 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
424 SkGaussianColorFilter::Make()));
Brian Salomon5e689522017-02-01 12:07:17 -0500425 if (translate->fX || translate->fY) {
426 canvas->save();
427 canvas->translate(translate->fX, translate->fY);
428 }
Brian Salomonaff27a22017-02-06 15:47:44 -0500429 canvas->drawVertices(vertices, SkBlendMode::kModulate, paint);
Brian Salomon5e689522017-02-01 12:07:17 -0500430 if (translate->fX || translate->fY) {
431 canvas->restore();
432 }
Brian Salomon5e689522017-02-01 12:07:17 -0500433}
434}
435
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500436static const float kHeightFactor = 1.0f / 128.0f;
437static const float kGeomFactor = 64.0f;
Jim Van Verth43475ad2017-01-13 14:37:37 -0500438
439// Draw an offset spot shadow and outlining ambient shadow for the given path.
440void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500441 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verth43475ad2017-01-13 14:37:37 -0500442 SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
443 uint32_t flags) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500444 SkAutoCanvasRestore acr(canvas, true);
Brian Salomon5e689522017-02-01 12:07:17 -0500445 SkMatrix viewMatrix = canvas->getTotalMatrix();
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500446 canvas->resetMatrix();
447
Brian Salomon5e689522017-02-01 12:07:17 -0500448 ShadowedPath shadowedPath(&path, &viewMatrix);
449
Brian Salomon958fbc42017-01-30 17:01:28 -0500450 bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
451
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500452 if (ambientAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500453 ambientAlpha = SkTMin(ambientAlpha, 1.f);
Brian Salomon5e689522017-02-01 12:07:17 -0500454 AmbientVerticesFactory factory;
455 factory.fRadius = occluderHeight * kHeightFactor * kGeomFactor;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500456 SkScalar umbraAlpha = SkScalarInvert((1.0f + SkTMax(occluderHeight*kHeightFactor, 0.0f)));
457 // umbraColor is the interior value, penumbraColor the exterior value.
458 // umbraAlpha is the factor that is linearly interpolated from outside to inside, and
459 // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get
460 // the final alpha.
Brian Salomon5e689522017-02-01 12:07:17 -0500461 factory.fUmbraColor =
462 SkColorSetARGB(255, 0, ambientAlpha * 255.9999f, umbraAlpha * 255.9999f);
463 factory.fPenumbraColor = SkColorSetARGB(255, 0, ambientAlpha * 255.9999f, 0);
464 factory.fTransparent = transparent;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500465
Brian Salomon5e689522017-02-01 12:07:17 -0500466 draw_shadow(factory, canvas, shadowedPath, color);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500467 }
468
469 if (spotAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500470 spotAlpha = SkTMin(spotAlpha, 1.f);
Brian Salomon5e689522017-02-01 12:07:17 -0500471 SpotVerticesFactory factory;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500472 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
Brian Salomon5e689522017-02-01 12:07:17 -0500473 factory.fRadius = lightRadius * zRatio;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500474
475 // Compute the scale and translation for the spot shadow.
Brian Salomon0bd699e2017-02-01 12:23:25 -0500476 factory.fScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500477
478 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Brian Salomon0bd699e2017-02-01 12:23:25 -0500479 viewMatrix.mapPoints(&center, 1);
480 factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX),
481 zRatio * (center.fY - devLightPos.fY));
Brian Salomon5e689522017-02-01 12:07:17 -0500482 factory.fUmbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 255);
483 factory.fPenumbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 0);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500484
Brian Salomond1ac9822017-02-03 14:25:02 -0500485 SkRRect rrect;
486 if (transparent) {
487 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
488 } else {
489 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaque;
490 if (shadowedPath.isRRect(&rrect)) {
491 SkRRect devRRect;
492 if (rrect.transform(viewMatrix, &devRRect)) {
493 SkScalar s = 1.f - factory.fScale;
494 SkScalar w = devRRect.width();
495 SkScalar h = devRRect.height();
496 SkScalar hw = w / 2.f;
497 SkScalar hh = h / 2.f;
498 SkScalar umbraInsetX = s * hw + factory.fRadius;
499 SkScalar umbraInsetY = s * hh + factory.fRadius;
Brian Salomon67386d42017-02-06 14:23:53 -0500500 // The umbra is inset by radius along the diagonal, so adjust for that.
501 SkScalar d = 1.f / SkScalarSqrt(hw * hw + hh * hh);
502 umbraInsetX *= hw * d;
503 umbraInsetY *= hh * d;
Brian Salomond1ac9822017-02-03 14:25:02 -0500504 if (umbraInsetX > hw || umbraInsetY > hh) {
505 // There is no umbra to occlude.
506 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
507 } else if (fabsf(factory.fOffset.fX) < umbraInsetX &&
508 fabsf(factory.fOffset.fY) < umbraInsetY) {
509 factory.fOccluderType =
510 SpotVerticesFactory::OccluderType::kOpaqueCoversUmbra;
511 } else if (factory.fOffset.fX > w - umbraInsetX ||
512 factory.fOffset.fY > h - umbraInsetY) {
513 // There umbra is fully exposed, there is nothing to omit.
514 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
515 }
516 }
517 }
518 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500519 if (factory.fOccluderType == SpotVerticesFactory::OccluderType::kOpaque) {
520 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
521 }
Brian Salomon5e689522017-02-01 12:07:17 -0500522 draw_shadow(factory, canvas, shadowedPath, color);
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500523 }
Jim Van Verth43475ad2017-01-13 14:37:37 -0500524}