blob: 288ae9608b5d3c554c689e7ff823d2aac739f035 [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"
Cary Clarka4083c92017-09-15 11:59:23 -040011#include "SkColorData.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
Brian Salomonaff329b2017-08-11 09:40:37 -040041 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(GrContext*,
42 SkColorSpace*) const override;
Jim Van Verthefe3ded2017-01-30 13:11:45 -050043#endif
44
45 SK_TO_STRING_OVERRIDE()
46 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter)
47
48protected:
49 void flatten(SkWriteBuffer&) const override {}
Mike Reed65331592017-05-24 16:45:34 -040050 void onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc,
51 bool shaderIsOpaque) const override {
52 pipeline->append(SkRasterPipeline::gauss_a_to_rgba);
53 }
Jim Van Verthefe3ded2017-01-30 13:11:45 -050054private:
55 SkGaussianColorFilter() : INHERITED() {}
56
57 typedef SkColorFilter INHERITED;
58};
59
Jim Van Verthefe3ded2017-01-30 13:11:45 -050060sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) {
61 return Make();
62}
63
64#ifndef SK_IGNORE_TO_STRING
65void SkGaussianColorFilter::toString(SkString* str) const {
66 str->append("SkGaussianColorFilter ");
67}
68#endif
69
70#if SK_SUPPORT_GPU
Jim Van Verthefe3ded2017-01-30 13:11:45 -050071
Brian Salomonaff329b2017-08-11 09:40:37 -040072std::unique_ptr<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(
73 GrContext*, SkColorSpace*) const {
Ethan Nicholas0274b302017-07-14 16:17:04 -040074 return GrBlurredEdgeFragmentProcessor::Make(GrBlurredEdgeFragmentProcessor::kGaussian_Mode);
Jim Van Verthefe3ded2017-01-30 13:11:45 -050075}
76#endif
77
78///////////////////////////////////////////////////////////////////////////////////////////////////
Brian Salomon5e689522017-02-01 12:07:17 -050079
80namespace {
81
Brian Salomonbc9956d2017-02-22 13:49:09 -050082uint64_t resource_cache_shared_id() {
83 return 0x2020776f64616873llu; // 'shadow '
84}
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 {
Jim Van Verthb4366552017-03-27 14:25:29 -040088 SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed.
Brian Salomon5e689522017-02-01 12:07:17 -050089 bool fTransparent;
Jim Van Verth8793e382017-05-22 15:52:21 -040090 SkVector fOffset;
Brian Salomon5e689522017-02-01 12:07:17 -050091
Brian Salomond1ac9822017-02-03 14:25:02 -050092 bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const {
Jim Van Verth060d9822017-05-04 09:58:17 -040093 if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) {
Brian Salomond1ac9822017-02-03 14:25:02 -050094 return false;
95 }
Jim Van Verth8793e382017-05-22 15:52:21 -040096 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -050097 return true;
Brian Salomon5e689522017-02-01 12:07:17 -050098 }
Brian Salomon5e689522017-02-01 12:07:17 -050099
Jim Van Verth8793e382017-05-22 15:52:21 -0400100 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
101 SkVector* translate) const {
Jim Van Verthe308a122017-05-08 14:19:30 -0400102 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400103 // pick a canonical place to generate shadow
104 SkMatrix noTrans(ctm);
105 if (!ctm.hasPerspective()) {
106 noTrans[SkMatrix::kMTransX] = 0;
107 noTrans[SkMatrix::kMTransY] = 0;
108 }
109 *translate = fOffset;
110 return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent);
Brian Salomon5e689522017-02-01 12:07:17 -0500111 }
112};
113
Brian Salomond1ac9822017-02-03 14:25:02 -0500114/** Factory for an spot shadow mesh with particular shadow properties. */
Brian Salomon5e689522017-02-01 12:07:17 -0500115struct SpotVerticesFactory {
Brian Salomond1ac9822017-02-03 14:25:02 -0500116 enum class OccluderType {
Jim Van Verth8793e382017-05-22 15:52:21 -0400117 // The umbra cannot be dropped out because either the occluder is not opaque,
118 // or the center of the umbra is visible.
Brian Salomond1ac9822017-02-03 14:25:02 -0500119 kTransparent,
120 // The umbra can be dropped where it is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400121 kOpaquePartialUmbra,
Brian Salomond1ac9822017-02-03 14:25:02 -0500122 // It is known that the entire umbra is occluded.
Jim Van Verth78c8f302017-05-15 10:44:22 -0400123 kOpaqueNoUmbra
Brian Salomond1ac9822017-02-03 14:25:02 -0500124 };
125
Brian Salomon5e689522017-02-01 12:07:17 -0500126 SkVector fOffset;
Jim Van Verth8793e382017-05-22 15:52:21 -0400127 SkPoint fLocalCenter;
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;
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 {
Jim Van Verthb4366552017-03-27 14:25:29 -0400134 if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ ||
Jim Van Verth060d9822017-05-04 09:58:17 -0400135 fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) {
Brian Salomond1ac9822017-02-03 14:25:02 -0500136 return false;
137 }
138 switch (fOccluderType) {
139 case OccluderType::kTransparent:
Jim Van Verth78c8f302017-05-15 10:44:22 -0400140 case OccluderType::kOpaqueNoUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500141 // 'this' and 'that' will either both have no umbra removed or both have all the
142 // umbra removed.
Jim Van Verth8793e382017-05-22 15:52:21 -0400143 *translate = that.fOffset;
Brian Salomond1ac9822017-02-03 14:25:02 -0500144 return true;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400145 case OccluderType::kOpaquePartialUmbra:
Brian Salomond1ac9822017-02-03 14:25:02 -0500146 // In this case we partially remove the umbra differently for 'this' and 'that'
147 // if the offsets don't match.
148 if (fOffset == that.fOffset) {
149 translate->set(0, 0);
150 return true;
151 }
152 return false;
153 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400154 SK_ABORT("Uninitialized occluder type?");
Brian Salomond1ac9822017-02-03 14:25:02 -0500155 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500156 }
Brian Salomon5e689522017-02-01 12:07:17 -0500157
Jim Van Verth8793e382017-05-22 15:52:21 -0400158 sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm,
159 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500160 bool transparent = OccluderType::kTransparent == fOccluderType;
Jim Van Verthe308a122017-05-08 14:19:30 -0400161 SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight);
Jim Van Verth8793e382017-05-22 15:52:21 -0400162 if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) {
163 translate->set(0, 0);
164 return SkShadowTessellator::MakeSpot(path, ctm, zParams,
165 fDevLightPos, fLightRadius, transparent);
166 } else {
167 // pick a canonical place to generate shadow, with light centered over path
168 SkMatrix noTrans(ctm);
169 noTrans[SkMatrix::kMTransX] = 0;
170 noTrans[SkMatrix::kMTransY] = 0;
171 SkPoint devCenter(fLocalCenter);
172 noTrans.mapPoints(&devCenter, 1);
173 SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ);
174 *translate = fOffset;
175 return SkShadowTessellator::MakeSpot(path, noTrans, zParams,
176 centerLightPos, fLightRadius, transparent);
177 }
Brian Salomon5e689522017-02-01 12:07:17 -0500178 }
179};
180
181/**
Brian Salomond1ac9822017-02-03 14:25:02 -0500182 * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache
183 * 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 -0400184 * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add
Brian Salomond1ac9822017-02-03 14:25:02 -0500185 * a new Rec with an adjusted size for any deletions/additions.
Brian Salomon5e689522017-02-01 12:07:17 -0500186 */
Brian Salomond1ac9822017-02-03 14:25:02 -0500187class CachedTessellations : public SkRefCnt {
Brian Salomon5e689522017-02-01 12:07:17 -0500188public:
Brian Salomond1ac9822017-02-03 14:25:02 -0500189 size_t size() const { return fAmbientSet.size() + fSpotSet.size(); }
190
Brian Salomonaff27a22017-02-06 15:47:44 -0500191 sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix,
192 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500193 return fAmbientSet.find(ambient, matrix, translate);
194 }
195
Brian Salomonaff27a22017-02-06 15:47:44 -0500196 sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient,
Jim Van Verth8793e382017-05-22 15:52:21 -0400197 const SkMatrix& matrix, SkVector* translate) {
198 return fAmbientSet.add(devPath, ambient, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500199 }
200
Brian Salomonaff27a22017-02-06 15:47:44 -0500201 sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix,
202 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500203 return fSpotSet.find(spot, matrix, translate);
204 }
205
Brian Salomonaff27a22017-02-06 15:47:44 -0500206 sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot,
Jim Van Verth8793e382017-05-22 15:52:21 -0400207 const SkMatrix& matrix, SkVector* translate) {
208 return fSpotSet.add(devPath, spot, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500209 }
210
211private:
212 template <typename FACTORY, int MAX_ENTRIES>
213 class Set {
214 public:
215 size_t size() const { return fSize; }
216
Brian Salomonaff27a22017-02-06 15:47:44 -0500217 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
218 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500219 for (int i = 0; i < MAX_ENTRIES; ++i) {
220 if (fEntries[i].fFactory.isCompatible(factory, translate)) {
221 const SkMatrix& m = fEntries[i].fMatrix;
222 if (matrix.hasPerspective() || m.hasPerspective()) {
223 if (matrix != fEntries[i].fMatrix) {
224 continue;
225 }
226 } else if (matrix.getScaleX() != m.getScaleX() ||
227 matrix.getSkewX() != m.getSkewX() ||
228 matrix.getScaleY() != m.getScaleY() ||
229 matrix.getSkewY() != m.getSkewY()) {
230 continue;
231 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500232 return fEntries[i].fVertices;
233 }
234 }
235 return nullptr;
236 }
237
Jim Van Verth8793e382017-05-22 15:52:21 -0400238 sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix,
239 SkVector* translate) {
240 sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500241 if (!vertices) {
242 return nullptr;
243 }
244 int i;
245 if (fCount < MAX_ENTRIES) {
246 i = fCount++;
247 } else {
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400248 i = fRandom.nextULessThan(MAX_ENTRIES);
Mike Reedaa9e3322017-03-16 14:38:48 -0400249 fSize -= fEntries[i].fVertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500250 }
251 fEntries[i].fFactory = factory;
252 fEntries[i].fVertices = vertices;
253 fEntries[i].fMatrix = matrix;
Mike Reedaa9e3322017-03-16 14:38:48 -0400254 fSize += vertices->approximateSize();
Brian Salomond1ac9822017-02-03 14:25:02 -0500255 return vertices;
256 }
257
258 private:
259 struct Entry {
260 FACTORY fFactory;
Brian Salomonaff27a22017-02-06 15:47:44 -0500261 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500262 SkMatrix fMatrix;
263 };
264 Entry fEntries[MAX_ENTRIES];
265 int fCount = 0;
266 size_t fSize = 0;
Jim Van Vertheb63eb72017-05-23 09:40:02 -0400267 SkRandom fRandom;
Brian Salomond1ac9822017-02-03 14:25:02 -0500268 };
269
270 Set<AmbientVerticesFactory, 4> fAmbientSet;
271 Set<SpotVerticesFactory, 4> fSpotSet;
Brian Salomond1ac9822017-02-03 14:25:02 -0500272};
273
Brian Salomond1ac9822017-02-03 14:25:02 -0500274/**
275 * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular
276 * path. The key represents the path's geometry and not any shadow params.
277 */
278class CachedTessellationsRec : public SkResourceCache::Rec {
279public:
280 CachedTessellationsRec(const SkResourceCache::Key& key,
281 sk_sp<CachedTessellations> tessellations)
282 : fTessellations(std::move(tessellations)) {
Brian Salomon5e689522017-02-01 12:07:17 -0500283 fKey.reset(new uint8_t[key.size()]);
284 memcpy(fKey.get(), &key, key.size());
285 }
286
287 const Key& getKey() const override {
288 return *reinterpret_cast<SkResourceCache::Key*>(fKey.get());
289 }
Brian Salomon5e689522017-02-01 12:07:17 -0500290
Brian Salomond1ac9822017-02-03 14:25:02 -0500291 size_t bytesUsed() const override { return fTessellations->size(); }
Brian Salomon5e689522017-02-01 12:07:17 -0500292
Brian Salomond1ac9822017-02-03 14:25:02 -0500293 const char* getCategory() const override { return "tessellated shadow masks"; }
Brian Salomon5e689522017-02-01 12:07:17 -0500294
Brian Salomond1ac9822017-02-03 14:25:02 -0500295 sk_sp<CachedTessellations> refTessellations() const { return fTessellations; }
Brian Salomon5e689522017-02-01 12:07:17 -0500296
Brian Salomond1ac9822017-02-03 14:25:02 -0500297 template <typename FACTORY>
Brian Salomonaff27a22017-02-06 15:47:44 -0500298 sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix,
299 SkVector* translate) const {
Brian Salomond1ac9822017-02-03 14:25:02 -0500300 return fTessellations->find(factory, matrix, translate);
301 }
Brian Salomon5e689522017-02-01 12:07:17 -0500302
303private:
304 std::unique_ptr<uint8_t[]> fKey;
Brian Salomond1ac9822017-02-03 14:25:02 -0500305 sk_sp<CachedTessellations> fTessellations;
Brian Salomon5e689522017-02-01 12:07:17 -0500306};
307
308/**
309 * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the
Brian Salomond1ac9822017-02-03 14:25:02 -0500310 * vertices and a translation vector. If the CachedTessellations does not contain a suitable
311 * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations
312 * to the caller. The caller will update it and reinsert it back into the cache.
Brian Salomon5e689522017-02-01 12:07:17 -0500313 */
314template <typename FACTORY>
315struct FindContext {
316 FindContext(const SkMatrix* viewMatrix, const FACTORY* factory)
317 : fViewMatrix(viewMatrix), fFactory(factory) {}
Brian Salomond1ac9822017-02-03 14:25:02 -0500318 const SkMatrix* const fViewMatrix;
319 // If this is valid after Find is called then we found the vertices and they should be drawn
320 // with fTranslate applied.
Brian Salomonaff27a22017-02-06 15:47:44 -0500321 sk_sp<SkVertices> fVertices;
Brian Salomond1ac9822017-02-03 14:25:02 -0500322 SkVector fTranslate = {0, 0};
323
324 // If this is valid after Find then the caller should add the vertices to the tessellation set
325 // and create a new CachedTessellationsRec and insert it into SkResourceCache.
326 sk_sp<CachedTessellations> fTessellationsOnFailure;
327
Brian Salomon5e689522017-02-01 12:07:17 -0500328 const FACTORY* fFactory;
329};
330
331/**
332 * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of
333 * the FindContext are used to determine if the vertices are reusable. If so the vertices and
334 * necessary translation vector are set on the FindContext.
335 */
336template <typename FACTORY>
337bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) {
338 FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx;
Brian Salomond1ac9822017-02-03 14:25:02 -0500339 const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec);
340 findContext->fVertices =
341 rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate);
342 if (findContext->fVertices) {
343 return true;
Brian Salomon5e689522017-02-01 12:07:17 -0500344 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500345 // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been
346 // manipulated we will add a new Rec.
347 findContext->fTessellationsOnFailure = rec.refTessellations();
348 return false;
Brian Salomon5e689522017-02-01 12:07:17 -0500349}
350
351class ShadowedPath {
352public:
353 ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix)
Jim Van Vertha84898d2017-02-06 13:38:23 -0500354 : fPath(path)
Brian Salomon5e689522017-02-01 12:07:17 -0500355 , fViewMatrix(viewMatrix)
356#if SK_SUPPORT_GPU
357 , fShapeForKey(*path, GrStyle::SimpleFill())
358#endif
359 {}
360
Jim Van Vertha84898d2017-02-06 13:38:23 -0500361 const SkPath& path() const { return *fPath; }
Brian Salomon5e689522017-02-01 12:07:17 -0500362 const SkMatrix& viewMatrix() const { return *fViewMatrix; }
363#if SK_SUPPORT_GPU
364 /** Negative means the vertices should not be cached for this path. */
365 int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); }
366 void writeKey(void* key) const {
367 fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key));
368 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500369 bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); }
Brian Salomon5e689522017-02-01 12:07:17 -0500370#else
371 int keyBytes() const { return -1; }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400372 void writeKey(void* key) const { SK_ABORT("Should never be called"); }
Brian Salomond1ac9822017-02-03 14:25:02 -0500373 bool isRRect(SkRRect* rrect) { return false; }
Brian Salomon5e689522017-02-01 12:07:17 -0500374#endif
375
376private:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500377 const SkPath* fPath;
Brian Salomon5e689522017-02-01 12:07:17 -0500378 const SkMatrix* fViewMatrix;
379#if SK_SUPPORT_GPU
380 GrShape fShapeForKey;
381#endif
Brian Salomon5e689522017-02-01 12:07:17 -0500382};
383
Brian Salomond1ac9822017-02-03 14:25:02 -0500384// This creates a domain of keys in SkResourceCache used by this file.
385static void* kNamespace;
386
Brian Salomon5e689522017-02-01 12:07:17 -0500387/**
388 * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless
389 * they are first found in SkResourceCache.
390 */
391template <typename FACTORY>
Mike Reed4204da22017-05-17 08:53:36 -0400392 void draw_shadow(const FACTORY& factory,
393 std::function<void(const SkVertices*, SkBlendMode, const SkPaint&,
394 SkScalar tx, SkScalar ty)> drawProc, ShadowedPath& path, SkColor color) {
Brian Salomon5e689522017-02-01 12:07:17 -0500395 FindContext<FACTORY> context(&path.viewMatrix(), &factory);
Brian Salomon5e689522017-02-01 12:07:17 -0500396
397 SkResourceCache::Key* key = nullptr;
398 SkAutoSTArray<32 * 4, uint8_t> keyStorage;
399 int keyDataBytes = path.keyBytes();
400 if (keyDataBytes >= 0) {
401 keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key));
402 key = new (keyStorage.begin()) SkResourceCache::Key();
403 path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key)));
Brian Salomonbc9956d2017-02-22 13:49:09 -0500404 key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400405 SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context);
Brian Salomon5e689522017-02-01 12:07:17 -0500406 }
407
Brian Salomonaff27a22017-02-06 15:47:44 -0500408 sk_sp<SkVertices> vertices;
Brian Salomon5e689522017-02-01 12:07:17 -0500409 bool foundInCache = SkToBool(context.fVertices);
410 if (foundInCache) {
411 vertices = std::move(context.fVertices);
Brian Salomon5e689522017-02-01 12:07:17 -0500412 } else {
413 // TODO: handle transforming the path as part of the tessellator
Brian Salomond1ac9822017-02-03 14:25:02 -0500414 if (key) {
415 // Update or initialize a tessellation set and add it to the cache.
416 sk_sp<CachedTessellations> tessellations;
417 if (context.fTessellationsOnFailure) {
418 tessellations = std::move(context.fTessellationsOnFailure);
419 } else {
420 tessellations.reset(new CachedTessellations());
421 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400422 vertices = tessellations->add(path.path(), factory, path.viewMatrix(),
423 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500424 if (!vertices) {
425 return;
426 }
Brian Salomon804e0912017-02-23 09:34:03 -0500427 auto rec = new CachedTessellationsRec(*key, std::move(tessellations));
Jim Van Verth37c5a962017-05-10 14:13:24 -0400428 SkResourceCache::Add(rec);
Brian Salomond1ac9822017-02-03 14:25:02 -0500429 } else {
Jim Van Verth8793e382017-05-22 15:52:21 -0400430 vertices = factory.makeVertices(path.path(), path.viewMatrix(),
431 &context.fTranslate);
Brian Salomond1ac9822017-02-03 14:25:02 -0500432 if (!vertices) {
433 return;
434 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500435 }
Brian Salomon5e689522017-02-01 12:07:17 -0500436 }
437
438 SkPaint paint;
Brian Salomon0bd699e2017-02-01 12:23:25 -0500439 // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of
440 // that against our 'color' param.
441 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
442 SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate),
443 SkGaussianColorFilter::Make()));
Mike Reed4204da22017-05-17 08:53:36 -0400444
Jim Van Verth8793e382017-05-22 15:52:21 -0400445 drawProc(vertices.get(), SkBlendMode::kModulate, paint,
446 context.fTranslate.fX, context.fTranslate.fY);
Brian Salomon5e689522017-02-01 12:07:17 -0500447}
448}
449
Mike Reed4204da22017-05-17 08:53:36 -0400450static bool tilted(const SkPoint3& zPlaneParams) {
451 return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY);
452}
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400453
Mike Reed4204da22017-05-17 08:53:36 -0400454static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) {
455 SkPoint3 result;
456 m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX);
457 result.fZ = pt.fZ;
458 return result;
459}
460
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400461static SkColor compute_render_color(SkColor color, float alpha, bool useTonalColor) {
462 if (useTonalColor) {
463 SkScalar colorScale;
464 SkScalar tonalAlpha;
465 SkColor4f color4f = SkColor4f::FromColor(color);
466 SkShadowUtils::ComputeTonalColorParams(color4f.fR,
467 color4f.fG,
468 color4f.fB,
469 alpha,
470 &colorScale, &tonalAlpha);
471 // After pre-multiplying, we want the alpha to be scaled by tonalAlpha, and
472 // the color scaled by colorScale. This scale factor gives that.
473 SkScalar unPremulScale = colorScale / tonalAlpha;
474
475 return SkColorSetARGB(tonalAlpha*255.999f, unPremulScale*SkColorGetR(color),
476 unPremulScale*SkColorGetG(color), unPremulScale*SkColorGetB(color));
477 }
478
Jim Van Verth060d9822017-05-04 09:58:17 -0400479 return SkColorSetARGB(alpha*SkColorGetA(color), SkColorGetR(color),
480 SkColorGetG(color), SkColorGetB(color));
481}
482
Jim Van Verth43475ad2017-01-13 14:37:37 -0500483// Draw an offset spot shadow and outlining ambient shadow for the given path.
Jim Van Verth37c5a962017-05-10 14:13:24 -0400484void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
Brian Salomon0bd699e2017-02-01 12:23:25 -0500485 const SkPoint3& devLightPos, SkScalar lightRadius,
Jim Van Verth43475ad2017-01-13 14:37:37 -0500486 SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
Jim Van Verth37c5a962017-05-10 14:13:24 -0400487 uint32_t flags) {
Mike Reed4204da22017-05-17 08:53:36 -0400488 SkMatrix inverse;
489 if (!canvas->getTotalMatrix().invert(&inverse)) {
Jim Van Verthcf40e302017-03-02 11:28:43 -0500490 return;
491 }
Mike Reed4204da22017-05-17 08:53:36 -0400492 SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY);
Jim Van Verthcf40e302017-03-02 11:28:43 -0500493
Mike Reed4204da22017-05-17 08:53:36 -0400494 SkDrawShadowRec rec;
495 rec.fZPlaneParams = zPlaneParams;
496 rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ };
497 rec.fLightRadius = lightRadius;
498 rec.fAmbientAlpha = SkScalarToFloat(ambientAlpha);
499 rec.fSpotAlpha = SkScalarToFloat(spotAlpha);
500 rec.fColor = color;
501 rec.fFlags = flags;
502
503 canvas->private_draw_shadow_rec(path, rec);
504}
505
506void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) {
507 auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint,
508 SkScalar tx, SkScalar ty) {
509 SkAutoDeviceCTMRestore adr(this, SkMatrix::Concat(this->ctm(),
510 SkMatrix::MakeTrans(tx, ty)));
511 this->drawVertices(vertices, mode, paint);
512 };
513
514 SkMatrix viewMatrix = this->ctm();
515 SkAutoDeviceCTMRestore adr(this, SkMatrix::I());
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500516
Brian Salomon5e689522017-02-01 12:07:17 -0500517 ShadowedPath shadowedPath(&path, &viewMatrix);
518
Mike Reed4204da22017-05-17 08:53:36 -0400519 bool tiltZPlane = tilted(rec.fZPlaneParams);
520 bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag);
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400521 bool uncached = tiltZPlane || path.isVolatile();
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400522 bool useTonalColor = SkToBool(rec.fFlags & kTonalColor_ShadowFlag);
Brian Salomon958fbc42017-01-30 17:01:28 -0500523
Mike Reed4204da22017-05-17 08:53:36 -0400524 SkColor color = rec.fColor;
525 SkPoint3 zPlaneParams = rec.fZPlaneParams;
526 SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos);
527 float lightRadius = rec.fLightRadius;
528
529 float ambientAlpha = rec.fAmbientAlpha;
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500530 if (ambientAlpha > 0) {
Brian Salomon0bd699e2017-02-01 12:23:25 -0500531 ambientAlpha = SkTMin(ambientAlpha, 1.f);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400532 SkColor renderColor;
533 if (useTonalColor) {
534 renderColor = compute_render_color(SK_ColorBLACK, ambientAlpha, false);
535 } else {
536 renderColor = compute_render_color(color, ambientAlpha, false);
537 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400538 if (uncached) {
539 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix,
540 zPlaneParams,
541 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400542 if (vertices) {
543 SkPaint paint;
544 // Run the vertex color through a GaussianColorFilter and then modulate the
545 // grayscale result of that against our 'color' param.
546 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
547 SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate),
548 SkGaussianColorFilter::Make()));
549 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
550 }
Brian Salomond1ac9822017-02-03 14:25:02 -0500551 } else {
Jim Van Verth37c5a962017-05-10 14:13:24 -0400552 AmbientVerticesFactory factory;
553 factory.fOccluderHeight = zPlaneParams.fZ;
554 factory.fTransparent = transparent;
Jim Van Verth8793e382017-05-22 15:52:21 -0400555 if (viewMatrix.hasPerspective()) {
556 factory.fOffset.set(0, 0);
557 } else {
558 factory.fOffset.fX = viewMatrix.getTranslateX();
559 factory.fOffset.fY = viewMatrix.getTranslateY();
560 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400561
Mike Reed4204da22017-05-17 08:53:36 -0400562 draw_shadow(factory, drawVertsProc, shadowedPath, renderColor);
Brian Salomond1ac9822017-02-03 14:25:02 -0500563 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400564 }
565
Mike Reed4204da22017-05-17 08:53:36 -0400566 float spotAlpha = rec.fSpotAlpha;
Jim Van Verthb4366552017-03-27 14:25:29 -0400567 if (spotAlpha > 0) {
568 spotAlpha = SkTMin(spotAlpha, 1.f);
Jim Van Verth34d6e4b2017-06-09 11:09:03 -0400569 SkColor renderColor = compute_render_color(color, spotAlpha, useTonalColor);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400570 if (uncached) {
571 sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix,
572 zPlaneParams,
573 devLightPos, lightRadius,
574 transparent);
Jim Van Verth7d8955e2017-07-13 15:13:52 -0400575 if (vertices) {
576 SkPaint paint;
577 // Run the vertex color through a GaussianColorFilter and then modulate the
578 // grayscale result of that against our 'color' param.
579 paint.setColorFilter(SkColorFilter::MakeComposeFilter(
580 SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate),
581 SkGaussianColorFilter::Make()));
582 this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint);
583 }
Jim Van Verth37c5a962017-05-10 14:13:24 -0400584 } else {
585 SpotVerticesFactory factory;
586 SkScalar occluderHeight = zPlaneParams.fZ;
587 float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f);
Jim Van Vertha783c362017-05-11 17:05:28 -0400588 SkScalar radius = lightRadius * zRatio;
589
Jim Van Verth78c8f302017-05-15 10:44:22 -0400590 // Compute the scale and translation for the spot shadow.
591 SkScalar scale = devLightPos.fZ / (devLightPos.fZ - occluderHeight);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400592 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verth8793e382017-05-22 15:52:21 -0400593 factory.fLocalCenter = center;
Jim Van Verth37c5a962017-05-10 14:13:24 -0400594 viewMatrix.mapPoints(&center, 1);
595 factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX),
596 zRatio * (center.fY - devLightPos.fY));
597 factory.fOccluderHeight = occluderHeight;
598 factory.fDevLightPos = devLightPos;
599 factory.fLightRadius = lightRadius;
Jim Van Vertha783c362017-05-11 17:05:28 -0400600 SkRect devBounds;
601 viewMatrix.mapRect(&devBounds, path.getBounds());
Jim Van Verth8793e382017-05-22 15:52:21 -0400602 if (transparent ||
603 SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() ||
604 SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400605 // if the translation of the shadow is big enough we're going to end up
606 // filling the entire umbra, so we can treat these as all the same
Jim Van Verth8793e382017-05-22 15:52:21 -0400607 factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400608 } else if (factory.fOffset.length()*scale + scale < radius) {
Jim Van Vertha783c362017-05-11 17:05:28 -0400609 // if we don't translate more than the blur distance, can assume umbra is covered
Jim Van Verth78c8f302017-05-15 10:44:22 -0400610 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra;
Jim Van Vertha783c362017-05-11 17:05:28 -0400611 } else {
Jim Van Verth78c8f302017-05-15 10:44:22 -0400612 factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra;
Jim Van Vertha783c362017-05-11 17:05:28 -0400613 }
Jim Van Verth8793e382017-05-22 15:52:21 -0400614 // need to add this after we classify the shadow
615 factory.fOffset.fX += viewMatrix.getTranslateX();
616 factory.fOffset.fY += viewMatrix.getTranslateY();
Jim Van Vertha783c362017-05-11 17:05:28 -0400617#ifdef DEBUG_SHADOW_CHECKS
618 switch (factory.fOccluderType) {
619 case SpotVerticesFactory::OccluderType::kTransparent:
620 color = 0xFFD2B48C; // tan for transparent
621 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400622 case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra:
Jim Van Vertha783c362017-05-11 17:05:28 -0400623 color = 0xFFFFA500; // orange for opaque
624 break;
Jim Van Verth78c8f302017-05-15 10:44:22 -0400625 case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra:
626 color = 0xFFE5E500; // corn yellow for covered
Jim Van Vertha783c362017-05-11 17:05:28 -0400627 break;
628 }
629#endif
Mike Reed4204da22017-05-17 08:53:36 -0400630 draw_shadow(factory, drawVertsProc, shadowedPath, renderColor);
Jim Van Verth37c5a962017-05-10 14:13:24 -0400631 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400632 }
633}