blob: b6c57f370ec92bad73393455c50ec9afd5db7748 [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"
Florin Malitaab244f02017-05-03 19:16:58 +000011#include "SkColorPriv.h"
Mike Reed4204da22017-05-17 08:53:36 -040012#include "SkDevice.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040013#include "SkDrawShadowInfo.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050014#include "SkPath.h"
Mike Reedb9641bd2017-05-04 10:57:40 -040015#include "SkPM4f.h"
Brian Salomond1ac9822017-02-03 14:25:02 -050016#include "SkRandom.h"
Mike Reed65331592017-05-24 16:45:34 -040017#include "SkRasterPipeline.h"
Brian Salomon5e689522017-02-01 12:07:17 -050018#include "SkResourceCache.h"
Jim Van Verthefe3ded2017-01-30 13:11:45 -050019#include "SkShadowTessellator.h"
Ben Wagner4d1955c2017-03-10 13:08:15 -050020#include "SkString.h"
Brian Salomon5e689522017-02-01 12:07:17 -050021#include "SkTLazy.h"
Brian Salomonaff27a22017-02-06 15:47:44 -050022#include "SkVertices.h"
Brian Salomon5e689522017-02-01 12:07:17 -050023#if SK_SUPPORT_GPU
24#include "GrShape.h"
25#include "effects/GrBlurredEdgeFragmentProcessor.h"
Mike Reed4204da22017-05-17 08:53:36 -040026#endif
Jim Van Verthefe3ded2017-01-30 13:11:45 -050027
28/**
29* Gaussian color filter -- produces a Gaussian ramp based on the color's B value,
30* then blends with the color's G value.
31* Final result is black with alpha of Gaussian(B)*G.
32* The assumption is that the original color's alpha is 1.
33*/
34class SK_API SkGaussianColorFilter : public SkColorFilter {
35public:
36 static sk_sp<SkColorFilter> Make() {
37 return sk_sp<SkColorFilter>(new SkGaussianColorFilter);
38 }
39
Jim Van Verthefe3ded2017-01-30 13:11:45 -050040#if SK_SUPPORT_GPU
41 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override;
42#endif
43
44 SK_TO_STRING_OVERRIDE()
45 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter)
46
47protected:
48 void flatten(SkWriteBuffer&) const override {}
Mike Reed65331592017-05-24 16:45:34 -040049 void onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc,
50 bool shaderIsOpaque) const override {
51 pipeline->append(SkRasterPipeline::gauss_a_to_rgba);
52 }
Jim Van Verthefe3ded2017-01-30 13:11:45 -050053private:
54 SkGaussianColorFilter() : INHERITED() {}
55
56 typedef SkColorFilter INHERITED;
57};
58
Jim Van Verthefe3ded2017-01-30 13:11:45 -050059sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
60 return Make();
61}
62
63#ifndef SK_IGNORE_TO_STRING
64void SkGaussianColorFilter::toString(SkString* str) const {
65 str->append("SkGaussianColorFilter ");
66}
67#endif
68
69#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050070
71sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*,
72 SkColorSpace*) const {
Ethan Nicholas0274b302017-07-14 16:17:04 -040073 return GrBlurredEdgeFragmentProcessor::Make(GrBlurredEdgeFragmentProcessor::kGaussian_Mode);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050074}
75#endif
76
77///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050078
79namespace {
80
Brian Salomonbc9956d2017-02-22 13:49:09 -050081uint64_t resource_cache_shared_id() {
82 return 0x2020776f64616873llu; // 'shadow '
83}
84
Brian Salomond1ac9822017-02-03 14:25:02 -050085/** Factory for an ambient shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -050086struct AmbientVerticesFactory {
Jim Van Verthb4366552017-03-27 14:25:29 -040087 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -050088 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -040089 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -050090
Brian Salomond1ac9822017-02-03 14:25:02 -050091 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -040092 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -050093 return false;
94 }
Jim Van Verth8793e382017-05-22 15:52:21 -040095 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -050096 return true;
Brian Salomon5e689522017-02-01 12:07:17 -050097 }
Brian Salomon5e689522017-02-01 12:07:17 -050098
Jim Van Verth8793e382017-05-22 15:52:21 -040099 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
100 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400101 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400102 // pick a canonical place to generate shadow
103 SkMatrix noTrans(ctm);
104 if (!ctm.hasPerspective()) {
105 noTrans[SkMatrix::kMTransX] = 0;
106 noTrans[SkMatrix::kMTransY] = 0;
107 }
108 *translate = fOffset;
109 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500110 }
111};
112
Brian Salomond1ac9822017-02-03 14:25:02 -0500113/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500114struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500115 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400116 // The umbra cannot be dropped out because either the occluder is not opaque,
117 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500118 kTransparent,
119 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400120 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500121 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400122 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500123 };
124
Brian Salomon5e689522017-02-01 12:07:17 -0500125 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400126 SkPoint fLocalCenter;
Jim Van Verthb4366552017-03-27 14:25:29 -0400127 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
128 SkPoint3 fDevLightPos;
129 SkScalar fLightRadius;
Brian Salomond1ac9822017-02-03 14:25:02 -0500130 OccluderType fOccluderType;
Brian Salomon5e689522017-02-01 12:07:17 -0500131
Brian Salomond1ac9822017-02-03 14:25:02 -0500132 bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const {
Jim Van Verthb4366552017-03-27 14:25:29 -0400133 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400134 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500135 return false;
136 }
137 switch (fOccluderType) {
138 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400139 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500140 // 'this' and 'that' will either both have no umbra removed or both have all the
141 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400142 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500143 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400144 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500145 // In this case we partially remove the umbra differently for 'this' and 'that'
146 // if the offsets don't match.
147 if (fOffset == that.fOffset) {
148 translate->set(0, 0);
149 return true;
150 }
151 return false;
152 }
153 SkFAIL("Uninitialized occluder type?");
154 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500155 }
Brian Salomon5e689522017-02-01 12:07:17 -0500156
Jim Van Verth8793e382017-05-22 15:52:21 -0400157 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
158 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500159 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400160 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400161 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
162 translate->set(0, 0);
163 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
164 fDevLightPos, fLightRadius, transparent);
165 } else {
166 // pick a canonical place to generate shadow, with light centered over path
167 SkMatrix noTrans(ctm);
168 noTrans[SkMatrix::kMTransX] = 0;
169 noTrans[SkMatrix::kMTransY] = 0;
170 SkPoint devCenter(fLocalCenter);
171 noTrans.mapPoints(&devCenter, 1);
172 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
173 *translate = fOffset;
174 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
175 centerLightPos, fLightRadius, transparent);
176 }
Brian Salomon5e689522017-02-01 12:07:17 -0500177 }
178};
179
180/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500181 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
182 * 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 -0400183 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500184 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500185 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500186class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500187public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500188 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
189
Brian Salomonaff27a22017-02-06 15:47:44 -0500190 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
191 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500192 return fAmbientSet.find(ambient, matrix, translate);
193 }
194
Brian Salomonaff27a22017-02-06 15:47:44 -0500195 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400196 const SkMatrix& matrix, SkVector* translate) {
197 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500198 }
199
Brian Salomonaff27a22017-02-06 15:47:44 -0500200 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
201 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500202 return fSpotSet.find(spot, matrix, translate);
203 }
204
Brian Salomonaff27a22017-02-06 15:47:44 -0500205 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400206 const SkMatrix& matrix, SkVector* translate) {
207 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500208 }
209
210private:
211 template <typename FACTORY, int MAX_ENTRIES>
212 class Set {
213 public:
214 size_t size() const { return fSize; }
215
Brian Salomonaff27a22017-02-06 15:47:44 -0500216 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
217 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500218 for (int i = 0; i < MAX_ENTRIES; ++i) {
219 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
220 const SkMatrix& m = fEntries[i].fMatrix;
221 if (matrix.hasPerspective() || m.hasPerspective()) {
222 if (matrix != fEntries[i].fMatrix) {
223 continue;
224 }
225 } else if (matrix.getScaleX() != m.getScaleX() ||
226 matrix.getSkewX() != m.getSkewX() ||
227 matrix.getScaleY() != m.getScaleY() ||
228 matrix.getSkewY() != m.getSkewY()) {
229 continue;
230 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500231 return fEntries[i].fVertices;
232 }
233 }
234 return nullptr;
235 }
236
Jim Van Verth8793e382017-05-22 15:52:21 -0400237 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
238 SkVector* translate) {
239 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500240 if (!vertices) {
241 return nullptr;
242 }
243 int i;
244 if (fCount < MAX_ENTRIES) {
245 i = fCount++;
246 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400247 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400248 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500249 }
250 fEntries[i].fFactory = factory;
251 fEntries[i].fVertices = vertices;
252 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400253 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500254 return vertices;
255 }
256
257 private:
258 struct Entry {
259 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500260 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500261 SkMatrix fMatrix;
262 };
263 Entry fEntries[MAX_ENTRIES];
264 int fCount = 0;
265 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400266 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500267 };
268
269 Set<AmbientVerticesFactory, 4> fAmbientSet;
270 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500271};
272
Brian Salomond1ac9822017-02-03 14:25:02 -0500273/**
274 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
275 * path. The key represents the path's geometry and not any shadow params.
276 */
277class CachedTessellationsRec : public SkResourceCache::Rec {
278public:
279 CachedTessellationsRec(const SkResourceCache::Key& key,
280 sk_sp<CachedTessellations> tessellations)
281 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500282 fKey.reset(new uint8_t[key.size()]);
283 memcpy(fKey.get(), &key, key.size());
284 }
285
286 const Key& getKey() const override {
287 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
288 }
Brian Salomon5e689522017-02-01 12:07:17 -0500289
Brian Salomond1ac9822017-02-03 14:25:02 -0500290 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500291
Brian Salomond1ac9822017-02-03 14:25:02 -0500292 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500293
Brian Salomond1ac9822017-02-03 14:25:02 -0500294 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500295
Brian Salomond1ac9822017-02-03 14:25:02 -0500296 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500297 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
298 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500299 return fTessellations->find(factory, matrix, translate);
300 }
Brian Salomon5e689522017-02-01 12:07:17 -0500301
302private:
303 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500304 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500305};
306
307/**
308 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500309 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
310 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
311 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500312 */
313template <typename FACTORY>
314struct FindContext {
315 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
316 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500317 const SkMatrix* const fViewMatrix;
318 // If this is valid after Find is called then we found the vertices and they should be drawn
319 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500320 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500321 SkVector fTranslate = {0, 0};
322
323 // If this is valid after Find then the caller should add the vertices to the tessellation set
324 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
325 sk_sp<CachedTessellations> fTessellationsOnFailure;
326
Brian Salomon5e689522017-02-01 12:07:17 -0500327 const FACTORY* fFactory;
328};
329
330/**
331 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
332 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
333 * necessary translation vector are set on the FindContext.
334 */
335template <typename FACTORY>
336bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
337 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500338 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
339 findContext->fVertices =
340 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
341 if (findContext->fVertices) {
342 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500343 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500344 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
345 // manipulated we will add a new Rec.
346 findContext->fTessellationsOnFailure = rec.refTessellations();
347 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500348}
349
350class ShadowedPath {
351public:
352 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500353 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500354 , fViewMatrix(viewMatrix)
355#if SK_SUPPORT_GPU
356 , fShapeForKey(*path, GrStyle::SimpleFill())
357#endif
358 {}
359
Jim Van Vertha84898d2017-02-06 13:38:23 -0500360 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500361 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
362#if SK_SUPPORT_GPU
363 /** Negative means the vertices should not be cached for this path. */
364 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
365 void writeKey(void* key) const {
366 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
367 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500368 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500369#else
370 int keyBytes() const { return -1; }
371 void writeKey(void* key) const { SkFAIL("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500372 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500373#endif
374
375private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500376 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500377 const SkMatrix* fViewMatrix;
378#if SK_SUPPORT_GPU
379 GrShape fShapeForKey;
380#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500381};
382
Brian Salomond1ac9822017-02-03 14:25:02 -0500383// This creates a domain of keys in SkResourceCache used by this file.
384static void* kNamespace;
385
Brian Salomon5e689522017-02-01 12:07:17 -0500386/**
387 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
388 * they are first found in SkResourceCache.
389 */
390template <typename FACTORY>
Mike Reed4204da22017-05-17 08:53:36 -0400391 void draw_shadow(const FACTORY& factory,
392 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
393 SkScalar tx, SkScalar ty)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500394 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500395
396 SkResourceCache::Key* key = nullptr;
397 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
398 int keyDataBytes = path.keyBytes();
399 if (keyDataBytes >= 0) {
400 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
401 key = new (keyStorage.begin()) SkResourceCache::Key();
402 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500403 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400404 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500405 }
406
Brian Salomonaff27a22017-02-06 15:47:44 -0500407 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500408 bool foundInCache = SkToBool(context.fVertices);
409 if (foundInCache) {
410 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500411 } 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 Verth8793e382017-05-22 15:52:21 -0400421 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
422 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500423 if (!vertices) {
424 return;
425 }
Brian Salomon804e0912017-02-23 09:34:03 -0500426 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400427 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500428 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400429 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
430 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500431 if (!vertices) {
432 return;
433 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500434 }
Brian Salomon5e689522017-02-01 12:07:17 -0500435 }
436
437 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500438 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
439 // that against our 'color' param.
440 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
441 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
442 SkGaussianColorFilter::Make()));
Mike Reed4204da22017-05-17 08:53:36 -0400443
Jim Van Verth8793e382017-05-22 15:52:21 -0400444 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
445 context.fTranslate.fX, context.fTranslate.fY);
Brian Salomon5e689522017-02-01 12:07:17 -0500446}
447}
448
Mike Reed4204da22017-05-17 08:53:36 -0400449static bool tilted(const SkPoint3& zPlaneParams) {
450 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
451}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400452
Mike Reed4204da22017-05-17 08:53:36 -0400453static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
454 SkPoint3 result;
455 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
456 result.fZ = pt.fZ;
457 return result;
458}
459
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400460static SkColor compute_render_color(SkColor color, float alpha, bool useTonalColor) {
461 if (useTonalColor) {
462 SkScalar colorScale;
463 SkScalar tonalAlpha;
464 SkColor4f color4f = SkColor4f::FromColor(color);
465 SkShadowUtils::ComputeTonalColorParams(color4f.fR,
466 color4f.fG,
467 color4f.fB,
468 alpha,
469 &colorScale, &tonalAlpha);
470 // After pre-multiplying, we want the alpha to be scaled by tonalAlpha, and
471 // the color scaled by colorScale. This scale factor gives that.
472 SkScalar unPremulScale = colorScale / tonalAlpha;
473
474 return SkColorSetARGB(tonalAlpha*255.999f, unPremulScale*SkColorGetR(color),
475 unPremulScale*SkColorGetG(color), unPremulScale*SkColorGetB(color));
476 }
477
Jim Van Verth060d9822017-05-04 09:58:17 -0400478 return SkColorSetARGB(alpha*SkColorGetA(color), SkColorGetR(color),
479 SkColorGetG(color), SkColorGetB(color));
480}
481
Jim Van Verth43475ad2017-01-13 14:37:37 -0500482// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400483void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500484 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verth43475ad2017-01-13 14:37:37 -0500485 SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400486 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400487 SkMatrix inverse;
488 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500489 return;
490 }
Mike Reed4204da22017-05-17 08:53:36 -0400491 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500492
Mike Reed4204da22017-05-17 08:53:36 -0400493 SkDrawShadowRec rec;
494 rec.fZPlaneParams = zPlaneParams;
495 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
496 rec.fLightRadius = lightRadius;
497 rec.fAmbientAlpha = SkScalarToFloat(ambientAlpha);
498 rec.fSpotAlpha = SkScalarToFloat(spotAlpha);
499 rec.fColor = color;
500 rec.fFlags = flags;
501
502 canvas->private_draw_shadow_rec(path, rec);
503}
504
505void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
506 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
507 SkScalar tx, SkScalar ty) {
508 SkAutoDeviceCTMRestore adr(this, SkMatrix::Concat(this->ctm(),
509 SkMatrix::MakeTrans(tx, ty)));
510 this->drawVertices(vertices, mode, paint);
511 };
512
513 SkMatrix viewMatrix = this->ctm();
514 SkAutoDeviceCTMRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500515
Brian Salomon5e689522017-02-01 12:07:17 -0500516 ShadowedPath shadowedPath(&path, &viewMatrix);
517
Mike Reed4204da22017-05-17 08:53:36 -0400518 bool tiltZPlane = tilted(rec.fZPlaneParams);
519 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400520 bool uncached = tiltZPlane || path.isVolatile();
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400521 bool useTonalColor = SkToBool(rec.fFlags & kTonalColor_ShadowFlag);
Brian Salomon958fbc42017-01-30 17:01:28 -0500522
Mike Reed4204da22017-05-17 08:53:36 -0400523 SkColor color = rec.fColor;
524 SkPoint3 zPlaneParams = rec.fZPlaneParams;
525 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
526 float lightRadius = rec.fLightRadius;
527
528 float ambientAlpha = rec.fAmbientAlpha;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500529 if (ambientAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500530 ambientAlpha = SkTMin(ambientAlpha, 1.f);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400531 SkColor renderColor;
532 if (useTonalColor) {
533 renderColor = compute_render_color(SK_ColorBLACK, ambientAlpha, false);
534 } else {
535 renderColor = compute_render_color(color, ambientAlpha, false);
536 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400537 if (uncached) {
538 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
539 zPlaneParams,
540 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400541 if (vertices) {
542 SkPaint paint;
543 // Run the vertex color through a GaussianColorFilter and then modulate the
544 // grayscale result of that against our 'color' param.
545 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
546 SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate),
547 SkGaussianColorFilter::Make()));
548 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
549 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500550 } else {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400551 AmbientVerticesFactory factory;
552 factory.fOccluderHeight = zPlaneParams.fZ;
553 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400554 if (viewMatrix.hasPerspective()) {
555 factory.fOffset.set(0, 0);
556 } else {
557 factory.fOffset.fX = viewMatrix.getTranslateX();
558 factory.fOffset.fY = viewMatrix.getTranslateY();
559 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400560
Mike Reed4204da22017-05-17 08:53:36 -0400561 draw_shadow(factory, drawVertsProc, shadowedPath, renderColor);
Brian Salomond1ac9822017-02-03 14:25:02 -0500562 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400563 }
564
Mike Reed4204da22017-05-17 08:53:36 -0400565 float spotAlpha = rec.fSpotAlpha;
Jim Van Verthb4366552017-03-27 14:25:29 -0400566 if (spotAlpha > 0) {
567 spotAlpha = SkTMin(spotAlpha, 1.f);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400568 SkColor renderColor = compute_render_color(color, spotAlpha, useTonalColor);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400569 if (uncached) {
570 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
571 zPlaneParams,
572 devLightPos, lightRadius,
573 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400574 if (vertices) {
575 SkPaint paint;
576 // Run the vertex color through a GaussianColorFilter and then modulate the
577 // grayscale result of that against our 'color' param.
578 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
579 SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate),
580 SkGaussianColorFilter::Make()));
581 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
582 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400583 } else {
584 SpotVerticesFactory factory;
585 SkScalar occluderHeight = zPlaneParams.fZ;
586 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
Jim Van Vertha783c362017-05-11 17:05:28 -0400587 SkScalar radius = lightRadius * zRatio;
588
Jim Van Verth78c8f302017-05-15 10:44:22 -0400589 // Compute the scale and translation for the spot shadow.
590 SkScalar scale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400591 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400592 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400593 viewMatrix.mapPoints(&center, 1);
594 factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX),
595 zRatio * (center.fY - devLightPos.fY));
596 factory.fOccluderHeight = occluderHeight;
597 factory.fDevLightPos = devLightPos;
598 factory.fLightRadius = lightRadius;
Jim Van Vertha783c362017-05-11 17:05:28 -0400599 SkRect devBounds;
600 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth8793e382017-05-22 15:52:21 -0400601 if (transparent ||
602 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
603 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400604 // if the translation of the shadow is big enough we're going to end up
605 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400606 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400607 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400608 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400609 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Vertha783c362017-05-11 17:05:28 -0400610 } else {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400611 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Vertha783c362017-05-11 17:05:28 -0400612 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400613 // need to add this after we classify the shadow
614 factory.fOffset.fX += viewMatrix.getTranslateX();
615 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Vertha783c362017-05-11 17:05:28 -0400616#ifdef DEBUG_SHADOW_CHECKS
617 switch (factory.fOccluderType) {
618 case SpotVerticesFactory::OccluderType::kTransparent:
619 color = 0xFFD2B48C; // tan for transparent
620 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400621 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400622 color = 0xFFFFA500; // orange for opaque
623 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400624 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
625 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400626 break;
627 }
628#endif
Mike Reed4204da22017-05-17 08:53:36 -0400629 draw_shadow(factory, drawVertsProc, shadowedPath, renderColor);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400630 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400631 }
632}