Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 1 | /* |
| 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 Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
| 11 | #include "SkPath.h" |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 12 | #include "SkRandom.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 13 | #include "SkResourceCache.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 14 | #include "SkShadowTessellator.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 15 | #include "SkTLazy.h" |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 16 | #include "SkVertices.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrShape.h" |
| 19 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
| 20 | #endif |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 21 | |
| 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 | */ |
| 28 | class SK_API SkGaussianColorFilter : public SkColorFilter { |
| 29 | public: |
| 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 | |
| 43 | protected: |
| 44 | void flatten(SkWriteBuffer&) const override {} |
| 45 | |
| 46 | private: |
| 47 | SkGaussianColorFilter() : INHERITED() {} |
| 48 | |
| 49 | typedef SkColorFilter INHERITED; |
| 50 | }; |
| 51 | |
| 52 | void 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 Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 59 | SkScalar a = factor * SkGetPackedG32(c); |
| 60 | dst[i] = SkPackARGB32(a, a, a, a); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) { |
| 65 | return Make(); |
| 66 | } |
| 67 | |
| 68 | #ifndef SK_IGNORE_TO_STRING |
| 69 | void SkGaussianColorFilter::toString(SkString* str) const { |
| 70 | str->append("SkGaussianColorFilter "); |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #if SK_SUPPORT_GPU |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 75 | |
| 76 | sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*, |
| 77 | SkColorSpace*) const { |
| 78 | return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode); |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 83 | |
| 84 | namespace { |
| 85 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 86 | /** Factory for an ambient shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 87 | struct AmbientVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 88 | SkScalar fRadius = SK_ScalarNaN; // NaN so that isCompatible will always fail until init'ed. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 89 | SkColor fUmbraColor; |
| 90 | SkColor fPenumbraColor; |
| 91 | bool fTransparent; |
| 92 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 93 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 100 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 101 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 102 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const { |
| 103 | return SkShadowTessellator::MakeAmbient(path, ctm, fRadius, fUmbraColor, fPenumbraColor, |
| 104 | fTransparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 108 | /** Factory for an spot shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 109 | struct SpotVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 110 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 120 | SkColor fUmbraColor; |
| 121 | SkColor fPenumbraColor; |
| 122 | SkScalar fScale; |
| 123 | SkVector fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 124 | OccluderType fOccluderType; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 125 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 126 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 150 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 151 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 152 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 153 | bool transparent = OccluderType::kTransparent == fOccluderType; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 154 | return SkShadowTessellator::MakeSpot(path, ctm, fScale, fOffset, fRadius, fUmbraColor, |
| 155 | fPenumbraColor, transparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 156 | } |
| 157 | }; |
| 158 | |
| 159 | /** |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 160 | * 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 164 | */ |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 165 | class CachedTessellations : public SkRefCnt { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 166 | public: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 167 | size_t size() const { return fAmbientSet.size() + fSpotSet.size(); } |
| 168 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 169 | sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix, |
| 170 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 171 | return fAmbientSet.find(ambient, matrix, translate); |
| 172 | } |
| 173 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 174 | sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient, |
| 175 | const SkMatrix& matrix) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 176 | return fAmbientSet.add(devPath, ambient, matrix); |
| 177 | } |
| 178 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 179 | sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix, |
| 180 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 181 | return fSpotSet.find(spot, matrix, translate); |
| 182 | } |
| 183 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 184 | sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot, |
| 185 | const SkMatrix& matrix) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 186 | return fSpotSet.add(devPath, spot, matrix); |
| 187 | } |
| 188 | |
| 189 | private: |
| 190 | template <typename FACTORY, int MAX_ENTRIES> |
| 191 | class Set { |
| 192 | public: |
| 193 | size_t size() const { return fSize; } |
| 194 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 195 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 196 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 197 | 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 Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 218 | sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix) { |
| 219 | sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 220 | 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 Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 240 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 241 | 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 | |
| 254 | SkRandom 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 | */ |
| 260 | class CachedTessellationsRec : public SkResourceCache::Rec { |
| 261 | public: |
| 262 | CachedTessellationsRec(const SkResourceCache::Key& key, |
| 263 | sk_sp<CachedTessellations> tessellations) |
| 264 | : fTessellations(std::move(tessellations)) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 265 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 272 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 273 | size_t bytesUsed() const override { return fTessellations->size(); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 274 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 275 | const char* getCategory() const override { return "tessellated shadow masks"; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 276 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 277 | sk_sp<CachedTessellations> refTessellations() const { return fTessellations; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 278 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 279 | template <typename FACTORY> |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 280 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 281 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 282 | return fTessellations->find(factory, matrix, translate); |
| 283 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 284 | |
| 285 | private: |
| 286 | std::unique_ptr<uint8_t[]> fKey; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 287 | sk_sp<CachedTessellations> fTessellations; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | /** |
| 291 | * Used by FindVisitor to determine whether a cache entry can be reused and if so returns the |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 292 | * 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 295 | */ |
| 296 | template <typename FACTORY> |
| 297 | struct FindContext { |
| 298 | FindContext(const SkMatrix* viewMatrix, const FACTORY* factory) |
| 299 | : fViewMatrix(viewMatrix), fFactory(factory) {} |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 300 | 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 Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 303 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 304 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 310 | 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 | */ |
| 318 | template <typename FACTORY> |
| 319 | bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) { |
| 320 | FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 321 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 326 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 327 | // 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | class ShadowedPath { |
| 334 | public: |
| 335 | ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix) |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 336 | : fPath(path) |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 337 | , fViewMatrix(viewMatrix) |
| 338 | #if SK_SUPPORT_GPU |
| 339 | , fShapeForKey(*path, GrStyle::SimpleFill()) |
| 340 | #endif |
| 341 | {} |
| 342 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 343 | const SkPath& path() const { return *fPath; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 344 | 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 Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 351 | bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 352 | #else |
| 353 | int keyBytes() const { return -1; } |
| 354 | void writeKey(void* key) const { SkFAIL("Should never be called"); } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 355 | bool isRRect(SkRRect* rrect) { return false; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 356 | #endif |
| 357 | |
| 358 | private: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 359 | const SkPath* fPath; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 360 | const SkMatrix* fViewMatrix; |
| 361 | #if SK_SUPPORT_GPU |
| 362 | GrShape fShapeForKey; |
| 363 | #endif |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 364 | }; |
| 365 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 366 | // This creates a domain of keys in SkResourceCache used by this file. |
| 367 | static void* kNamespace; |
| 368 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 369 | /** |
| 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 | */ |
| 373 | template <typename FACTORY> |
| 374 | void draw_shadow(const FACTORY& factory, SkCanvas* canvas, ShadowedPath& path, SkColor color) { |
| 375 | FindContext<FACTORY> context(&path.viewMatrix(), &factory); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 376 | |
| 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 Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 388 | sk_sp<SkVertices> vertices; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 389 | 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 Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 397 | 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 Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 405 | vertices = tessellations->add(path.path(), factory, path.viewMatrix()); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 406 | if (!vertices) { |
| 407 | return; |
| 408 | } |
| 409 | SkResourceCache::Add(new CachedTessellationsRec(*key, std::move(tessellations))); |
| 410 | } else { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 411 | vertices = factory.makeVertices(path.path(), path.viewMatrix()); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 412 | if (!vertices) { |
| 413 | return; |
| 414 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 415 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 416 | translate = &kZeroTranslate; |
| 417 | } |
| 418 | |
| 419 | SkPaint paint; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 420 | // 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 425 | if (translate->fX || translate->fY) { |
| 426 | canvas->save(); |
| 427 | canvas->translate(translate->fX, translate->fY); |
| 428 | } |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame^] | 429 | canvas->drawVertices(vertices, SkBlendMode::kModulate, paint); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 430 | if (translate->fX || translate->fY) { |
| 431 | canvas->restore(); |
| 432 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 436 | static const float kHeightFactor = 1.0f / 128.0f; |
| 437 | static const float kGeomFactor = 64.0f; |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 438 | |
| 439 | // Draw an offset spot shadow and outlining ambient shadow for the given path. |
| 440 | void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight, |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 441 | const SkPoint3& devLightPos, SkScalar lightRadius, |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 442 | SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color, |
| 443 | uint32_t flags) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 444 | SkAutoCanvasRestore acr(canvas, true); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 445 | SkMatrix viewMatrix = canvas->getTotalMatrix(); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 446 | canvas->resetMatrix(); |
| 447 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 448 | ShadowedPath shadowedPath(&path, &viewMatrix); |
| 449 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 450 | bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
| 451 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 452 | if (ambientAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 453 | ambientAlpha = SkTMin(ambientAlpha, 1.f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 454 | AmbientVerticesFactory factory; |
| 455 | factory.fRadius = occluderHeight * kHeightFactor * kGeomFactor; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 456 | 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 Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 461 | 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 Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 465 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 466 | draw_shadow(factory, canvas, shadowedPath, color); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (spotAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 470 | spotAlpha = SkTMin(spotAlpha, 1.f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 471 | SpotVerticesFactory factory; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 472 | float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 473 | factory.fRadius = lightRadius * zRatio; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 474 | |
| 475 | // Compute the scale and translation for the spot shadow. |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 476 | factory.fScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 477 | |
| 478 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 479 | viewMatrix.mapPoints(¢er, 1); |
| 480 | factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX), |
| 481 | zRatio * (center.fY - devLightPos.fY)); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 482 | factory.fUmbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 255); |
| 483 | factory.fPenumbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 0); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 484 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 485 | 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 Salomon | 67386d4 | 2017-02-06 14:23:53 -0500 | [diff] [blame] | 500 | // 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 Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 504 | 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 Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 519 | if (factory.fOccluderType == SpotVerticesFactory::OccluderType::kOpaque) { |
| 520 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 521 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 522 | draw_shadow(factory, canvas, shadowedPath, color); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 523 | } |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 524 | } |