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