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" |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 15 | #include "SkString.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 16 | #include "SkTLazy.h" |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 17 | #include "SkVertices.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrShape.h" |
| 20 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
| 21 | #endif |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 22 | #include "../../src/effects/shadows/SkAmbientShadowMaskFilter.h" |
| 23 | #include "../../src/effects/shadows/SkSpotShadowMaskFilter.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Gaussian color filter -- produces a Gaussian ramp based on the color's B value, |
| 27 | * then blends with the color's G value. |
| 28 | * Final result is black with alpha of Gaussian(B)*G. |
| 29 | * The assumption is that the original color's alpha is 1. |
| 30 | */ |
| 31 | class SK_API SkGaussianColorFilter : public SkColorFilter { |
| 32 | public: |
| 33 | static sk_sp<SkColorFilter> Make() { |
| 34 | return sk_sp<SkColorFilter>(new SkGaussianColorFilter); |
| 35 | } |
| 36 | |
| 37 | void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override; |
| 38 | |
| 39 | #if SK_SUPPORT_GPU |
| 40 | sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override; |
| 41 | #endif |
| 42 | |
| 43 | SK_TO_STRING_OVERRIDE() |
| 44 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter) |
| 45 | |
| 46 | protected: |
| 47 | void flatten(SkWriteBuffer&) const override {} |
| 48 | |
| 49 | private: |
| 50 | SkGaussianColorFilter() : INHERITED() {} |
| 51 | |
| 52 | typedef SkColorFilter INHERITED; |
| 53 | }; |
| 54 | |
| 55 | void SkGaussianColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const { |
| 56 | for (int i = 0; i < count; ++i) { |
| 57 | SkPMColor c = src[i]; |
| 58 | |
| 59 | SkScalar factor = SK_Scalar1 - SkGetPackedB32(c) / 255.f; |
| 60 | factor = SkScalarExp(-factor * factor * 4) - 0.018f; |
| 61 | |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 62 | SkScalar a = factor * SkGetPackedG32(c); |
| 63 | dst[i] = SkPackARGB32(a, a, a, a); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) { |
| 68 | return Make(); |
| 69 | } |
| 70 | |
| 71 | #ifndef SK_IGNORE_TO_STRING |
| 72 | void SkGaussianColorFilter::toString(SkString* str) const { |
| 73 | str->append("SkGaussianColorFilter "); |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | #if SK_SUPPORT_GPU |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 78 | |
| 79 | sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*, |
| 80 | SkColorSpace*) const { |
| 81 | return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 86 | |
| 87 | namespace { |
| 88 | |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 89 | uint64_t resource_cache_shared_id() { |
| 90 | return 0x2020776f64616873llu; // 'shadow ' |
| 91 | } |
| 92 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 93 | /** Factory for an ambient shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 94 | struct AmbientVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 95 | 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] | 96 | SkColor fUmbraColor; |
| 97 | SkColor fPenumbraColor; |
| 98 | bool fTransparent; |
| 99 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 100 | bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const { |
| 101 | if (fRadius != that.fRadius || fUmbraColor != that.fUmbraColor || |
| 102 | fPenumbraColor != that.fPenumbraColor || fTransparent != that.fTransparent) { |
| 103 | return false; |
| 104 | } |
| 105 | translate->set(0, 0); |
| 106 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 107 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 108 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 109 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const { |
| 110 | return SkShadowTessellator::MakeAmbient(path, ctm, fRadius, fUmbraColor, fPenumbraColor, |
| 111 | fTransparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 115 | /** Factory for an spot shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 116 | struct SpotVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 117 | enum class OccluderType { |
| 118 | // The umbra cannot be dropped out because the occluder is not opaque. |
| 119 | kTransparent, |
| 120 | // The umbra can be dropped where it is occluded. |
| 121 | kOpaque, |
| 122 | // It is known that the entire umbra is occluded. |
| 123 | kOpaqueCoversUmbra |
| 124 | }; |
| 125 | |
| 126 | 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] | 127 | SkColor fUmbraColor; |
| 128 | SkColor fPenumbraColor; |
| 129 | SkScalar fScale; |
| 130 | SkVector fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 131 | OccluderType fOccluderType; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 132 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 133 | bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const { |
| 134 | if (fRadius != that.fRadius || fUmbraColor != that.fUmbraColor || |
| 135 | fPenumbraColor != that.fPenumbraColor || fOccluderType != that.fOccluderType || |
| 136 | fScale != that.fScale) { |
| 137 | return false; |
| 138 | } |
| 139 | switch (fOccluderType) { |
| 140 | case OccluderType::kTransparent: |
| 141 | case OccluderType::kOpaqueCoversUmbra: |
| 142 | // 'this' and 'that' will either both have no umbra removed or both have all the |
| 143 | // umbra removed. |
| 144 | *translate = that.fOffset - fOffset; |
| 145 | return true; |
| 146 | case OccluderType::kOpaque: |
| 147 | // In this case we partially remove the umbra differently for 'this' and 'that' |
| 148 | // if the offsets don't match. |
| 149 | if (fOffset == that.fOffset) { |
| 150 | translate->set(0, 0); |
| 151 | return true; |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | SkFAIL("Uninitialized occluder type?"); |
| 156 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 157 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 158 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 159 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 160 | bool transparent = OccluderType::kTransparent == fOccluderType; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 161 | return SkShadowTessellator::MakeSpot(path, ctm, fScale, fOffset, fRadius, fUmbraColor, |
| 162 | fPenumbraColor, transparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 163 | } |
| 164 | }; |
| 165 | |
| 166 | /** |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 167 | * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache |
| 168 | * records are immutable this is not itself a Rec. When we need to update it we return this on |
| 169 | * the FindVisitor and let the cache destory the Rec. We'll update the tessellations and then add |
| 170 | * a new Rec with an adjusted size for any deletions/additions. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 171 | */ |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 172 | class CachedTessellations : public SkRefCnt { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 173 | public: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 174 | size_t size() const { return fAmbientSet.size() + fSpotSet.size(); } |
| 175 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 176 | sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix, |
| 177 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 178 | return fAmbientSet.find(ambient, matrix, translate); |
| 179 | } |
| 180 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 181 | sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient, |
| 182 | const SkMatrix& matrix) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 183 | return fAmbientSet.add(devPath, ambient, matrix); |
| 184 | } |
| 185 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 186 | sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix, |
| 187 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 188 | return fSpotSet.find(spot, matrix, translate); |
| 189 | } |
| 190 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 191 | sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot, |
| 192 | const SkMatrix& matrix) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 193 | return fSpotSet.add(devPath, spot, matrix); |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | template <typename FACTORY, int MAX_ENTRIES> |
| 198 | class Set { |
| 199 | public: |
| 200 | size_t size() const { return fSize; } |
| 201 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 202 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 203 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 204 | for (int i = 0; i < MAX_ENTRIES; ++i) { |
| 205 | if (fEntries[i].fFactory.isCompatible(factory, translate)) { |
| 206 | const SkMatrix& m = fEntries[i].fMatrix; |
| 207 | if (matrix.hasPerspective() || m.hasPerspective()) { |
| 208 | if (matrix != fEntries[i].fMatrix) { |
| 209 | continue; |
| 210 | } |
| 211 | } else if (matrix.getScaleX() != m.getScaleX() || |
| 212 | matrix.getSkewX() != m.getSkewX() || |
| 213 | matrix.getScaleY() != m.getScaleY() || |
| 214 | matrix.getSkewY() != m.getSkewY()) { |
| 215 | continue; |
| 216 | } |
| 217 | *translate += SkVector{matrix.getTranslateX() - m.getTranslateX(), |
| 218 | matrix.getTranslateY() - m.getTranslateY()}; |
| 219 | return fEntries[i].fVertices; |
| 220 | } |
| 221 | } |
| 222 | return nullptr; |
| 223 | } |
| 224 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 225 | sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix) { |
| 226 | sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 227 | if (!vertices) { |
| 228 | return nullptr; |
| 229 | } |
| 230 | int i; |
| 231 | if (fCount < MAX_ENTRIES) { |
| 232 | i = fCount++; |
| 233 | } else { |
| 234 | i = gRandom.nextULessThan(MAX_ENTRIES); |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 235 | fSize -= fEntries[i].fVertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 236 | } |
| 237 | fEntries[i].fFactory = factory; |
| 238 | fEntries[i].fVertices = vertices; |
| 239 | fEntries[i].fMatrix = matrix; |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 240 | fSize += vertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 241 | return vertices; |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | struct Entry { |
| 246 | FACTORY fFactory; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 247 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 248 | SkMatrix fMatrix; |
| 249 | }; |
| 250 | Entry fEntries[MAX_ENTRIES]; |
| 251 | int fCount = 0; |
| 252 | size_t fSize = 0; |
| 253 | }; |
| 254 | |
| 255 | Set<AmbientVerticesFactory, 4> fAmbientSet; |
| 256 | Set<SpotVerticesFactory, 4> fSpotSet; |
| 257 | |
| 258 | static SkRandom gRandom; |
| 259 | }; |
| 260 | |
| 261 | SkRandom CachedTessellations::gRandom; |
| 262 | |
| 263 | /** |
| 264 | * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular |
| 265 | * path. The key represents the path's geometry and not any shadow params. |
| 266 | */ |
| 267 | class CachedTessellationsRec : public SkResourceCache::Rec { |
| 268 | public: |
| 269 | CachedTessellationsRec(const SkResourceCache::Key& key, |
| 270 | sk_sp<CachedTessellations> tessellations) |
| 271 | : fTessellations(std::move(tessellations)) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 272 | fKey.reset(new uint8_t[key.size()]); |
| 273 | memcpy(fKey.get(), &key, key.size()); |
| 274 | } |
| 275 | |
| 276 | const Key& getKey() const override { |
| 277 | return *reinterpret_cast<SkResourceCache::Key*>(fKey.get()); |
| 278 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 279 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 280 | size_t bytesUsed() const override { return fTessellations->size(); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 281 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 282 | const char* getCategory() const override { return "tessellated shadow masks"; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 283 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 284 | sk_sp<CachedTessellations> refTessellations() const { return fTessellations; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 285 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 286 | template <typename FACTORY> |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 287 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 288 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 289 | return fTessellations->find(factory, matrix, translate); |
| 290 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 291 | |
| 292 | private: |
| 293 | std::unique_ptr<uint8_t[]> fKey; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 294 | sk_sp<CachedTessellations> fTessellations; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | /** |
| 298 | * 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] | 299 | * vertices and a translation vector. If the CachedTessellations does not contain a suitable |
| 300 | * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations |
| 301 | * 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] | 302 | */ |
| 303 | template <typename FACTORY> |
| 304 | struct FindContext { |
| 305 | FindContext(const SkMatrix* viewMatrix, const FACTORY* factory) |
| 306 | : fViewMatrix(viewMatrix), fFactory(factory) {} |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 307 | const SkMatrix* const fViewMatrix; |
| 308 | // If this is valid after Find is called then we found the vertices and they should be drawn |
| 309 | // with fTranslate applied. |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 310 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 311 | SkVector fTranslate = {0, 0}; |
| 312 | |
| 313 | // If this is valid after Find then the caller should add the vertices to the tessellation set |
| 314 | // and create a new CachedTessellationsRec and insert it into SkResourceCache. |
| 315 | sk_sp<CachedTessellations> fTessellationsOnFailure; |
| 316 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 317 | const FACTORY* fFactory; |
| 318 | }; |
| 319 | |
| 320 | /** |
| 321 | * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of |
| 322 | * the FindContext are used to determine if the vertices are reusable. If so the vertices and |
| 323 | * necessary translation vector are set on the FindContext. |
| 324 | */ |
| 325 | template <typename FACTORY> |
| 326 | bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) { |
| 327 | FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 328 | const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec); |
| 329 | findContext->fVertices = |
| 330 | rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate); |
| 331 | if (findContext->fVertices) { |
| 332 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 333 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 334 | // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been |
| 335 | // manipulated we will add a new Rec. |
| 336 | findContext->fTessellationsOnFailure = rec.refTessellations(); |
| 337 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | class ShadowedPath { |
| 341 | public: |
| 342 | ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix) |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 343 | : fPath(path) |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 344 | , fViewMatrix(viewMatrix) |
| 345 | #if SK_SUPPORT_GPU |
| 346 | , fShapeForKey(*path, GrStyle::SimpleFill()) |
| 347 | #endif |
| 348 | {} |
| 349 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 350 | const SkPath& path() const { return *fPath; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 351 | const SkMatrix& viewMatrix() const { return *fViewMatrix; } |
| 352 | #if SK_SUPPORT_GPU |
| 353 | /** Negative means the vertices should not be cached for this path. */ |
| 354 | int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); } |
| 355 | void writeKey(void* key) const { |
| 356 | fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key)); |
| 357 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 358 | bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 359 | #else |
| 360 | int keyBytes() const { return -1; } |
| 361 | void writeKey(void* key) const { SkFAIL("Should never be called"); } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 362 | bool isRRect(SkRRect* rrect) { return false; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 363 | #endif |
| 364 | |
| 365 | private: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 366 | const SkPath* fPath; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 367 | const SkMatrix* fViewMatrix; |
| 368 | #if SK_SUPPORT_GPU |
| 369 | GrShape fShapeForKey; |
| 370 | #endif |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 371 | }; |
| 372 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 373 | // This creates a domain of keys in SkResourceCache used by this file. |
| 374 | static void* kNamespace; |
| 375 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 376 | /** |
| 377 | * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless |
| 378 | * they are first found in SkResourceCache. |
| 379 | */ |
| 380 | template <typename FACTORY> |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 381 | void draw_shadow(const FACTORY& factory, SkCanvas* canvas, ShadowedPath& path, SkColor color, |
| 382 | SkResourceCache* cache) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 383 | FindContext<FACTORY> context(&path.viewMatrix(), &factory); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 384 | |
| 385 | SkResourceCache::Key* key = nullptr; |
| 386 | SkAutoSTArray<32 * 4, uint8_t> keyStorage; |
| 387 | int keyDataBytes = path.keyBytes(); |
| 388 | if (keyDataBytes >= 0) { |
| 389 | keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key)); |
| 390 | key = new (keyStorage.begin()) SkResourceCache::Key(); |
| 391 | path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key))); |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 392 | key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes); |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 393 | if (cache) { |
| 394 | cache->find(*key, FindVisitor<FACTORY>, &context); |
| 395 | } else { |
| 396 | SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context); |
| 397 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 400 | sk_sp<SkVertices> vertices; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 401 | const SkVector* translate; |
| 402 | static constexpr SkVector kZeroTranslate = {0, 0}; |
| 403 | bool foundInCache = SkToBool(context.fVertices); |
| 404 | if (foundInCache) { |
| 405 | vertices = std::move(context.fVertices); |
| 406 | translate = &context.fTranslate; |
| 407 | } else { |
| 408 | // TODO: handle transforming the path as part of the tessellator |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 409 | if (key) { |
| 410 | // Update or initialize a tessellation set and add it to the cache. |
| 411 | sk_sp<CachedTessellations> tessellations; |
| 412 | if (context.fTessellationsOnFailure) { |
| 413 | tessellations = std::move(context.fTessellationsOnFailure); |
| 414 | } else { |
| 415 | tessellations.reset(new CachedTessellations()); |
| 416 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 417 | vertices = tessellations->add(path.path(), factory, path.viewMatrix()); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 418 | if (!vertices) { |
| 419 | return; |
| 420 | } |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 421 | auto rec = new CachedTessellationsRec(*key, std::move(tessellations)); |
| 422 | if (cache) { |
| 423 | cache->add(rec); |
| 424 | } else { |
| 425 | SkResourceCache::Add(rec); |
| 426 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 427 | } else { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 428 | vertices = factory.makeVertices(path.path(), path.viewMatrix()); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 429 | if (!vertices) { |
| 430 | return; |
| 431 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 432 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 433 | translate = &kZeroTranslate; |
| 434 | } |
| 435 | |
| 436 | SkPaint paint; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 437 | // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of |
| 438 | // that against our 'color' param. |
| 439 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
| 440 | SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate), |
| 441 | SkGaussianColorFilter::Make())); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 442 | if (translate->fX || translate->fY) { |
| 443 | canvas->save(); |
| 444 | canvas->translate(translate->fX, translate->fY); |
| 445 | } |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 446 | canvas->drawVertices(vertices, SkBlendMode::kModulate, paint); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 447 | if (translate->fX || translate->fY) { |
| 448 | canvas->restore(); |
| 449 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 453 | static const float kHeightFactor = 1.0f / 128.0f; |
| 454 | static const float kGeomFactor = 64.0f; |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 455 | |
| 456 | // Draw an offset spot shadow and outlining ambient shadow for the given path. |
| 457 | void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight, |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 458 | const SkPoint3& devLightPos, SkScalar lightRadius, |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 459 | SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color, |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 460 | uint32_t flags, SkResourceCache* cache) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 461 | SkAutoCanvasRestore acr(canvas, true); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 462 | SkMatrix viewMatrix = canvas->getTotalMatrix(); |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 463 | |
| 464 | // try circular fast path |
| 465 | SkRect rect; |
| 466 | if (viewMatrix.isSimilarity() && |
| 467 | path.isOval(&rect) && rect.width() == rect.height()) { |
| 468 | SkPaint newPaint; |
| 469 | newPaint.setColor(color); |
| 470 | if (ambientAlpha > 0) { |
| 471 | newPaint.setMaskFilter(SkAmbientShadowMaskFilter::Make(occluderHeight, ambientAlpha, |
| 472 | flags)); |
| 473 | canvas->drawPath(path, newPaint); |
| 474 | } |
| 475 | if (spotAlpha > 0) { |
| 476 | newPaint.setMaskFilter(SkSpotShadowMaskFilter::Make(occluderHeight, devLightPos, |
| 477 | lightRadius, spotAlpha, flags)); |
| 478 | canvas->drawPath(path, newPaint); |
| 479 | } |
| 480 | return; |
| 481 | } |
| 482 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 483 | canvas->resetMatrix(); |
| 484 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 485 | ShadowedPath shadowedPath(&path, &viewMatrix); |
| 486 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 487 | bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
| 488 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 489 | if (ambientAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 490 | ambientAlpha = SkTMin(ambientAlpha, 1.f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 491 | AmbientVerticesFactory factory; |
| 492 | factory.fRadius = occluderHeight * kHeightFactor * kGeomFactor; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 493 | SkScalar umbraAlpha = SkScalarInvert((1.0f + SkTMax(occluderHeight*kHeightFactor, 0.0f))); |
| 494 | // umbraColor is the interior value, penumbraColor the exterior value. |
| 495 | // umbraAlpha is the factor that is linearly interpolated from outside to inside, and |
| 496 | // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get |
| 497 | // the final alpha. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 498 | factory.fUmbraColor = |
| 499 | SkColorSetARGB(255, 0, ambientAlpha * 255.9999f, umbraAlpha * 255.9999f); |
| 500 | factory.fPenumbraColor = SkColorSetARGB(255, 0, ambientAlpha * 255.9999f, 0); |
| 501 | factory.fTransparent = transparent; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 502 | |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 503 | draw_shadow(factory, canvas, shadowedPath, color, cache); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (spotAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 507 | spotAlpha = SkTMin(spotAlpha, 1.f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 508 | SpotVerticesFactory factory; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 509 | float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 510 | factory.fRadius = lightRadius * zRatio; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 511 | |
| 512 | // Compute the scale and translation for the spot shadow. |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 513 | factory.fScale = devLightPos.fZ / (devLightPos.fZ - occluderHeight); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 514 | |
| 515 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 516 | viewMatrix.mapPoints(¢er, 1); |
| 517 | factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX), |
| 518 | zRatio * (center.fY - devLightPos.fY)); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 519 | factory.fUmbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 255); |
| 520 | factory.fPenumbraColor = SkColorSetARGB(255, 0, spotAlpha * 255.9999f, 0); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 521 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 522 | SkRRect rrect; |
| 523 | if (transparent) { |
| 524 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 525 | } else { |
| 526 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaque; |
| 527 | if (shadowedPath.isRRect(&rrect)) { |
| 528 | SkRRect devRRect; |
| 529 | if (rrect.transform(viewMatrix, &devRRect)) { |
| 530 | SkScalar s = 1.f - factory.fScale; |
| 531 | SkScalar w = devRRect.width(); |
| 532 | SkScalar h = devRRect.height(); |
| 533 | SkScalar hw = w / 2.f; |
| 534 | SkScalar hh = h / 2.f; |
| 535 | SkScalar umbraInsetX = s * hw + factory.fRadius; |
| 536 | SkScalar umbraInsetY = s * hh + factory.fRadius; |
Brian Salomon | 67386d4 | 2017-02-06 14:23:53 -0500 | [diff] [blame] | 537 | // The umbra is inset by radius along the diagonal, so adjust for that. |
| 538 | SkScalar d = 1.f / SkScalarSqrt(hw * hw + hh * hh); |
| 539 | umbraInsetX *= hw * d; |
| 540 | umbraInsetY *= hh * d; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 541 | if (umbraInsetX > hw || umbraInsetY > hh) { |
| 542 | // There is no umbra to occlude. |
| 543 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 544 | } else if (fabsf(factory.fOffset.fX) < umbraInsetX && |
| 545 | fabsf(factory.fOffset.fY) < umbraInsetY) { |
| 546 | factory.fOccluderType = |
| 547 | SpotVerticesFactory::OccluderType::kOpaqueCoversUmbra; |
| 548 | } else if (factory.fOffset.fX > w - umbraInsetX || |
| 549 | factory.fOffset.fY > h - umbraInsetY) { |
| 550 | // There umbra is fully exposed, there is nothing to omit. |
| 551 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 556 | if (factory.fOccluderType == SpotVerticesFactory::OccluderType::kOpaque) { |
| 557 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 558 | } |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 559 | draw_shadow(factory, canvas, shadowedPath, color, cache); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 560 | } |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 561 | } |