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" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 12 | #include "SkDevice.h" |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 13 | #include "SkDrawShadowInfo.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 14 | #include "SkPath.h" |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame] | 15 | #include "SkPM4f.h" |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 16 | #include "SkRandom.h" |
Mike Reed | 6533159 | 2017-05-24 16:45:34 -0400 | [diff] [blame] | 17 | #include "SkRasterPipeline.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 18 | #include "SkResourceCache.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 19 | #include "SkShadowTessellator.h" |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 20 | #include "SkString.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 21 | #include "SkTLazy.h" |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 22 | #include "SkVertices.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 23 | #if SK_SUPPORT_GPU |
| 24 | #include "GrShape.h" |
| 25 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 26 | #endif |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Gaussian color filter -- produces a Gaussian ramp based on the color's B value, |
| 30 | * then blends with the color's G value. |
| 31 | * Final result is black with alpha of Gaussian(B)*G. |
| 32 | * The assumption is that the original color's alpha is 1. |
| 33 | */ |
| 34 | class SK_API SkGaussianColorFilter : public SkColorFilter { |
| 35 | public: |
| 36 | static sk_sp<SkColorFilter> Make() { |
| 37 | return sk_sp<SkColorFilter>(new SkGaussianColorFilter); |
| 38 | } |
| 39 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 40 | #if SK_SUPPORT_GPU |
| 41 | sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override; |
| 42 | #endif |
| 43 | |
| 44 | SK_TO_STRING_OVERRIDE() |
| 45 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter) |
| 46 | |
| 47 | protected: |
| 48 | void flatten(SkWriteBuffer&) const override {} |
Mike Reed | 6533159 | 2017-05-24 16:45:34 -0400 | [diff] [blame] | 49 | void onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc, |
| 50 | bool shaderIsOpaque) const override { |
| 51 | pipeline->append(SkRasterPipeline::gauss_a_to_rgba); |
| 52 | } |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 53 | private: |
| 54 | SkGaussianColorFilter() : INHERITED() {} |
| 55 | |
| 56 | typedef SkColorFilter INHERITED; |
| 57 | }; |
| 58 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 59 | sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) { |
| 60 | return Make(); |
| 61 | } |
| 62 | |
| 63 | #ifndef SK_IGNORE_TO_STRING |
| 64 | void SkGaussianColorFilter::toString(SkString* str) const { |
| 65 | str->append("SkGaussianColorFilter "); |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | #if SK_SUPPORT_GPU |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 70 | |
| 71 | sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*, |
| 72 | SkColorSpace*) const { |
Ethan Nicholas | 0274b30 | 2017-07-14 16:17:04 -0400 | [diff] [blame] | 73 | return GrBlurredEdgeFragmentProcessor::Make(GrBlurredEdgeFragmentProcessor::kGaussian_Mode); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 74 | } |
| 75 | #endif |
| 76 | |
| 77 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 78 | |
| 79 | namespace { |
| 80 | |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 81 | uint64_t resource_cache_shared_id() { |
| 82 | return 0x2020776f64616873llu; // 'shadow ' |
| 83 | } |
| 84 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 85 | /** Factory for an ambient shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 86 | struct AmbientVerticesFactory { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 87 | SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 88 | bool fTransparent; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 89 | SkVector fOffset; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 90 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 91 | bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 92 | if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 93 | return false; |
| 94 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 95 | *translate = that.fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 96 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 97 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 98 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 99 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm, |
| 100 | SkVector* translate) const { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 101 | SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 102 | // pick a canonical place to generate shadow |
| 103 | SkMatrix noTrans(ctm); |
| 104 | if (!ctm.hasPerspective()) { |
| 105 | noTrans[SkMatrix::kMTransX] = 0; |
| 106 | noTrans[SkMatrix::kMTransY] = 0; |
| 107 | } |
| 108 | *translate = fOffset; |
| 109 | return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, fTransparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 113 | /** Factory for an spot shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 114 | struct SpotVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 115 | enum class OccluderType { |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 116 | // The umbra cannot be dropped out because either the occluder is not opaque, |
| 117 | // or the center of the umbra is visible. |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 118 | kTransparent, |
| 119 | // The umbra can be dropped where it is occluded. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 120 | kOpaquePartialUmbra, |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 121 | // It is known that the entire umbra is occluded. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 122 | kOpaqueNoUmbra |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 123 | }; |
| 124 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 125 | SkVector fOffset; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 126 | SkPoint fLocalCenter; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 127 | SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed. |
| 128 | SkPoint3 fDevLightPos; |
| 129 | SkScalar fLightRadius; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 130 | OccluderType fOccluderType; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 131 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 132 | bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 133 | if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ || |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 134 | fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | switch (fOccluderType) { |
| 138 | case OccluderType::kTransparent: |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 139 | case OccluderType::kOpaqueNoUmbra: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 140 | // 'this' and 'that' will either both have no umbra removed or both have all the |
| 141 | // umbra removed. |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 142 | *translate = that.fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 143 | return true; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 144 | case OccluderType::kOpaquePartialUmbra: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 145 | // In this case we partially remove the umbra differently for 'this' and 'that' |
| 146 | // if the offsets don't match. |
| 147 | if (fOffset == that.fOffset) { |
| 148 | translate->set(0, 0); |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | SkFAIL("Uninitialized occluder type?"); |
| 154 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 155 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 156 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 157 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm, |
| 158 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 159 | bool transparent = OccluderType::kTransparent == fOccluderType; |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 160 | SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 161 | if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) { |
| 162 | translate->set(0, 0); |
| 163 | return SkShadowTessellator::MakeSpot(path, ctm, zParams, |
| 164 | fDevLightPos, fLightRadius, transparent); |
| 165 | } else { |
| 166 | // pick a canonical place to generate shadow, with light centered over path |
| 167 | SkMatrix noTrans(ctm); |
| 168 | noTrans[SkMatrix::kMTransX] = 0; |
| 169 | noTrans[SkMatrix::kMTransY] = 0; |
| 170 | SkPoint devCenter(fLocalCenter); |
| 171 | noTrans.mapPoints(&devCenter, 1); |
| 172 | SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ); |
| 173 | *translate = fOffset; |
| 174 | return SkShadowTessellator::MakeSpot(path, noTrans, zParams, |
| 175 | centerLightPos, fLightRadius, transparent); |
| 176 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 177 | } |
| 178 | }; |
| 179 | |
| 180 | /** |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 181 | * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache |
| 182 | * records are immutable this is not itself a Rec. When we need to update it we return this on |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 183 | * the FindVisitor and let the cache destroy the Rec. We'll update the tessellations and then add |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 184 | * a new Rec with an adjusted size for any deletions/additions. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 185 | */ |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 186 | class CachedTessellations : public SkRefCnt { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 187 | public: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 188 | size_t size() const { return fAmbientSet.size() + fSpotSet.size(); } |
| 189 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 190 | sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix, |
| 191 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 192 | return fAmbientSet.find(ambient, 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 AmbientVerticesFactory& ambient, |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 196 | const SkMatrix& matrix, SkVector* translate) { |
| 197 | return fAmbientSet.add(devPath, ambient, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 200 | sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix, |
| 201 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 202 | return fSpotSet.find(spot, matrix, translate); |
| 203 | } |
| 204 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 205 | sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot, |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 206 | const SkMatrix& matrix, SkVector* translate) { |
| 207 | return fSpotSet.add(devPath, spot, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | private: |
| 211 | template <typename FACTORY, int MAX_ENTRIES> |
| 212 | class Set { |
| 213 | public: |
| 214 | size_t size() const { return fSize; } |
| 215 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 216 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 217 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 218 | for (int i = 0; i < MAX_ENTRIES; ++i) { |
| 219 | if (fEntries[i].fFactory.isCompatible(factory, translate)) { |
| 220 | const SkMatrix& m = fEntries[i].fMatrix; |
| 221 | if (matrix.hasPerspective() || m.hasPerspective()) { |
| 222 | if (matrix != fEntries[i].fMatrix) { |
| 223 | continue; |
| 224 | } |
| 225 | } else if (matrix.getScaleX() != m.getScaleX() || |
| 226 | matrix.getSkewX() != m.getSkewX() || |
| 227 | matrix.getScaleY() != m.getScaleY() || |
| 228 | matrix.getSkewY() != m.getSkewY()) { |
| 229 | continue; |
| 230 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 231 | return fEntries[i].fVertices; |
| 232 | } |
| 233 | } |
| 234 | return nullptr; |
| 235 | } |
| 236 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 237 | sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix, |
| 238 | SkVector* translate) { |
| 239 | sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 240 | if (!vertices) { |
| 241 | return nullptr; |
| 242 | } |
| 243 | int i; |
| 244 | if (fCount < MAX_ENTRIES) { |
| 245 | i = fCount++; |
| 246 | } else { |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 247 | i = fRandom.nextULessThan(MAX_ENTRIES); |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 248 | fSize -= fEntries[i].fVertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 249 | } |
| 250 | fEntries[i].fFactory = factory; |
| 251 | fEntries[i].fVertices = vertices; |
| 252 | fEntries[i].fMatrix = matrix; |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 253 | fSize += vertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 254 | return vertices; |
| 255 | } |
| 256 | |
| 257 | private: |
| 258 | struct Entry { |
| 259 | FACTORY fFactory; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 260 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 261 | SkMatrix fMatrix; |
| 262 | }; |
| 263 | Entry fEntries[MAX_ENTRIES]; |
| 264 | int fCount = 0; |
| 265 | size_t fSize = 0; |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 266 | SkRandom fRandom; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | Set<AmbientVerticesFactory, 4> fAmbientSet; |
| 270 | Set<SpotVerticesFactory, 4> fSpotSet; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 271 | }; |
| 272 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 273 | /** |
| 274 | * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular |
| 275 | * path. The key represents the path's geometry and not any shadow params. |
| 276 | */ |
| 277 | class CachedTessellationsRec : public SkResourceCache::Rec { |
| 278 | public: |
| 279 | CachedTessellationsRec(const SkResourceCache::Key& key, |
| 280 | sk_sp<CachedTessellations> tessellations) |
| 281 | : fTessellations(std::move(tessellations)) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 282 | fKey.reset(new uint8_t[key.size()]); |
| 283 | memcpy(fKey.get(), &key, key.size()); |
| 284 | } |
| 285 | |
| 286 | const Key& getKey() const override { |
| 287 | return *reinterpret_cast<SkResourceCache::Key*>(fKey.get()); |
| 288 | } |
Brian 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 | size_t bytesUsed() const override { return fTessellations->size(); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 291 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 292 | const char* getCategory() const override { return "tessellated shadow masks"; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 293 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 294 | sk_sp<CachedTessellations> refTessellations() const { return fTessellations; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 295 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 296 | template <typename FACTORY> |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 297 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 298 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 299 | return fTessellations->find(factory, matrix, translate); |
| 300 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 301 | |
| 302 | private: |
| 303 | std::unique_ptr<uint8_t[]> fKey; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 304 | sk_sp<CachedTessellations> fTessellations; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /** |
| 308 | * 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] | 309 | * vertices and a translation vector. If the CachedTessellations does not contain a suitable |
| 310 | * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations |
| 311 | * to the caller. The caller will update it and reinsert it back into the cache. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 312 | */ |
| 313 | template <typename FACTORY> |
| 314 | struct FindContext { |
| 315 | FindContext(const SkMatrix* viewMatrix, const FACTORY* factory) |
| 316 | : fViewMatrix(viewMatrix), fFactory(factory) {} |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 317 | const SkMatrix* const fViewMatrix; |
| 318 | // If this is valid after Find is called then we found the vertices and they should be drawn |
| 319 | // with fTranslate applied. |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 320 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 321 | SkVector fTranslate = {0, 0}; |
| 322 | |
| 323 | // If this is valid after Find then the caller should add the vertices to the tessellation set |
| 324 | // and create a new CachedTessellationsRec and insert it into SkResourceCache. |
| 325 | sk_sp<CachedTessellations> fTessellationsOnFailure; |
| 326 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 327 | const FACTORY* fFactory; |
| 328 | }; |
| 329 | |
| 330 | /** |
| 331 | * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of |
| 332 | * the FindContext are used to determine if the vertices are reusable. If so the vertices and |
| 333 | * necessary translation vector are set on the FindContext. |
| 334 | */ |
| 335 | template <typename FACTORY> |
| 336 | bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) { |
| 337 | FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 338 | const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec); |
| 339 | findContext->fVertices = |
| 340 | rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate); |
| 341 | if (findContext->fVertices) { |
| 342 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 343 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 344 | // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been |
| 345 | // manipulated we will add a new Rec. |
| 346 | findContext->fTessellationsOnFailure = rec.refTessellations(); |
| 347 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | class ShadowedPath { |
| 351 | public: |
| 352 | ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix) |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 353 | : fPath(path) |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 354 | , fViewMatrix(viewMatrix) |
| 355 | #if SK_SUPPORT_GPU |
| 356 | , fShapeForKey(*path, GrStyle::SimpleFill()) |
| 357 | #endif |
| 358 | {} |
| 359 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 360 | const SkPath& path() const { return *fPath; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 361 | const SkMatrix& viewMatrix() const { return *fViewMatrix; } |
| 362 | #if SK_SUPPORT_GPU |
| 363 | /** Negative means the vertices should not be cached for this path. */ |
| 364 | int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); } |
| 365 | void writeKey(void* key) const { |
| 366 | fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key)); |
| 367 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 368 | bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 369 | #else |
| 370 | int keyBytes() const { return -1; } |
| 371 | void writeKey(void* key) const { SkFAIL("Should never be called"); } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 372 | bool isRRect(SkRRect* rrect) { return false; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 373 | #endif |
| 374 | |
| 375 | private: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 376 | const SkPath* fPath; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 377 | const SkMatrix* fViewMatrix; |
| 378 | #if SK_SUPPORT_GPU |
| 379 | GrShape fShapeForKey; |
| 380 | #endif |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 381 | }; |
| 382 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 383 | // This creates a domain of keys in SkResourceCache used by this file. |
| 384 | static void* kNamespace; |
| 385 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 386 | /** |
| 387 | * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless |
| 388 | * they are first found in SkResourceCache. |
| 389 | */ |
| 390 | template <typename FACTORY> |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 391 | void draw_shadow(const FACTORY& factory, |
| 392 | std::function<void(const SkVertices*, SkBlendMode, const SkPaint&, |
| 393 | SkScalar tx, SkScalar ty)> drawProc, ShadowedPath& path, SkColor color) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 394 | FindContext<FACTORY> context(&path.viewMatrix(), &factory); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 395 | |
| 396 | SkResourceCache::Key* key = nullptr; |
| 397 | SkAutoSTArray<32 * 4, uint8_t> keyStorage; |
| 398 | int keyDataBytes = path.keyBytes(); |
| 399 | if (keyDataBytes >= 0) { |
| 400 | keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key)); |
| 401 | key = new (keyStorage.begin()) SkResourceCache::Key(); |
| 402 | path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key))); |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 403 | key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 404 | SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 405 | } |
| 406 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 407 | sk_sp<SkVertices> vertices; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 408 | bool foundInCache = SkToBool(context.fVertices); |
| 409 | if (foundInCache) { |
| 410 | vertices = std::move(context.fVertices); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 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 | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 421 | vertices = tessellations->add(path.path(), factory, path.viewMatrix(), |
| 422 | &context.fTranslate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 423 | if (!vertices) { |
| 424 | return; |
| 425 | } |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 426 | auto rec = new CachedTessellationsRec(*key, std::move(tessellations)); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 427 | SkResourceCache::Add(rec); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 428 | } else { |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 429 | vertices = factory.makeVertices(path.path(), path.viewMatrix(), |
| 430 | &context.fTranslate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 431 | if (!vertices) { |
| 432 | return; |
| 433 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 434 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | SkPaint paint; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 438 | // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of |
| 439 | // that against our 'color' param. |
| 440 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
| 441 | SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate), |
| 442 | SkGaussianColorFilter::Make())); |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 443 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 444 | drawProc(vertices.get(), SkBlendMode::kModulate, paint, |
| 445 | context.fTranslate.fX, context.fTranslate.fY); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 449 | static bool tilted(const SkPoint3& zPlaneParams) { |
| 450 | return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY); |
| 451 | } |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 452 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 453 | static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) { |
| 454 | SkPoint3 result; |
| 455 | m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX); |
| 456 | result.fZ = pt.fZ; |
| 457 | return result; |
| 458 | } |
| 459 | |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 460 | static SkColor compute_render_color(SkColor color, float alpha, bool useTonalColor) { |
| 461 | if (useTonalColor) { |
| 462 | SkScalar colorScale; |
| 463 | SkScalar tonalAlpha; |
| 464 | SkColor4f color4f = SkColor4f::FromColor(color); |
| 465 | SkShadowUtils::ComputeTonalColorParams(color4f.fR, |
| 466 | color4f.fG, |
| 467 | color4f.fB, |
| 468 | alpha, |
| 469 | &colorScale, &tonalAlpha); |
| 470 | // After pre-multiplying, we want the alpha to be scaled by tonalAlpha, and |
| 471 | // the color scaled by colorScale. This scale factor gives that. |
| 472 | SkScalar unPremulScale = colorScale / tonalAlpha; |
| 473 | |
| 474 | return SkColorSetARGB(tonalAlpha*255.999f, unPremulScale*SkColorGetR(color), |
| 475 | unPremulScale*SkColorGetG(color), unPremulScale*SkColorGetB(color)); |
| 476 | } |
| 477 | |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 478 | return SkColorSetARGB(alpha*SkColorGetA(color), SkColorGetR(color), |
| 479 | SkColorGetG(color), SkColorGetB(color)); |
| 480 | } |
| 481 | |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 482 | // Draw an offset spot shadow and outlining ambient shadow for the given path. |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 483 | void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 484 | const SkPoint3& devLightPos, SkScalar lightRadius, |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 485 | SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color, |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 486 | uint32_t flags) { |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 487 | SkMatrix inverse; |
| 488 | if (!canvas->getTotalMatrix().invert(&inverse)) { |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 489 | return; |
| 490 | } |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 491 | SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY); |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 492 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 493 | SkDrawShadowRec rec; |
| 494 | rec.fZPlaneParams = zPlaneParams; |
| 495 | rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ }; |
| 496 | rec.fLightRadius = lightRadius; |
| 497 | rec.fAmbientAlpha = SkScalarToFloat(ambientAlpha); |
| 498 | rec.fSpotAlpha = SkScalarToFloat(spotAlpha); |
| 499 | rec.fColor = color; |
| 500 | rec.fFlags = flags; |
| 501 | |
| 502 | canvas->private_draw_shadow_rec(path, rec); |
| 503 | } |
| 504 | |
| 505 | void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) { |
| 506 | auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint, |
| 507 | SkScalar tx, SkScalar ty) { |
| 508 | SkAutoDeviceCTMRestore adr(this, SkMatrix::Concat(this->ctm(), |
| 509 | SkMatrix::MakeTrans(tx, ty))); |
| 510 | this->drawVertices(vertices, mode, paint); |
| 511 | }; |
| 512 | |
| 513 | SkMatrix viewMatrix = this->ctm(); |
| 514 | SkAutoDeviceCTMRestore adr(this, SkMatrix::I()); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 515 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 516 | ShadowedPath shadowedPath(&path, &viewMatrix); |
| 517 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 518 | bool tiltZPlane = tilted(rec.fZPlaneParams); |
| 519 | bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 520 | bool uncached = tiltZPlane || path.isVolatile(); |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 521 | bool useTonalColor = SkToBool(rec.fFlags & kTonalColor_ShadowFlag); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 522 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 523 | SkColor color = rec.fColor; |
| 524 | SkPoint3 zPlaneParams = rec.fZPlaneParams; |
| 525 | SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos); |
| 526 | float lightRadius = rec.fLightRadius; |
| 527 | |
| 528 | float ambientAlpha = rec.fAmbientAlpha; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 529 | if (ambientAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 530 | ambientAlpha = SkTMin(ambientAlpha, 1.f); |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 531 | SkColor renderColor; |
| 532 | if (useTonalColor) { |
| 533 | renderColor = compute_render_color(SK_ColorBLACK, ambientAlpha, false); |
| 534 | } else { |
| 535 | renderColor = compute_render_color(color, ambientAlpha, false); |
| 536 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 537 | if (uncached) { |
| 538 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix, |
| 539 | zPlaneParams, |
| 540 | transparent); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 541 | if (vertices) { |
| 542 | SkPaint paint; |
| 543 | // Run the vertex color through a GaussianColorFilter and then modulate the |
| 544 | // grayscale result of that against our 'color' param. |
| 545 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
| 546 | SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate), |
| 547 | SkGaussianColorFilter::Make())); |
| 548 | this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint); |
| 549 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 550 | } else { |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 551 | AmbientVerticesFactory factory; |
| 552 | factory.fOccluderHeight = zPlaneParams.fZ; |
| 553 | factory.fTransparent = transparent; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 554 | if (viewMatrix.hasPerspective()) { |
| 555 | factory.fOffset.set(0, 0); |
| 556 | } else { |
| 557 | factory.fOffset.fX = viewMatrix.getTranslateX(); |
| 558 | factory.fOffset.fY = viewMatrix.getTranslateY(); |
| 559 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 560 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 561 | draw_shadow(factory, drawVertsProc, shadowedPath, renderColor); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 562 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 563 | } |
| 564 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 565 | float spotAlpha = rec.fSpotAlpha; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 566 | if (spotAlpha > 0) { |
| 567 | spotAlpha = SkTMin(spotAlpha, 1.f); |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 568 | SkColor renderColor = compute_render_color(color, spotAlpha, useTonalColor); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 569 | if (uncached) { |
| 570 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix, |
| 571 | zPlaneParams, |
| 572 | devLightPos, lightRadius, |
| 573 | transparent); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 574 | if (vertices) { |
| 575 | SkPaint paint; |
| 576 | // Run the vertex color through a GaussianColorFilter and then modulate the |
| 577 | // grayscale result of that against our 'color' param. |
| 578 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
| 579 | SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate), |
| 580 | SkGaussianColorFilter::Make())); |
| 581 | this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint); |
| 582 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 583 | } else { |
| 584 | SpotVerticesFactory factory; |
| 585 | SkScalar occluderHeight = zPlaneParams.fZ; |
| 586 | float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f); |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 587 | SkScalar radius = lightRadius * zRatio; |
| 588 | |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 589 | // Compute the scale and translation for the spot shadow. |
| 590 | SkScalar scale = devLightPos.fZ / (devLightPos.fZ - occluderHeight); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 591 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 592 | factory.fLocalCenter = center; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 593 | viewMatrix.mapPoints(¢er, 1); |
| 594 | factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX), |
| 595 | zRatio * (center.fY - devLightPos.fY)); |
| 596 | factory.fOccluderHeight = occluderHeight; |
| 597 | factory.fDevLightPos = devLightPos; |
| 598 | factory.fLightRadius = lightRadius; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 599 | SkRect devBounds; |
| 600 | viewMatrix.mapRect(&devBounds, path.getBounds()); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 601 | if (transparent || |
| 602 | SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() || |
| 603 | SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) { |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 604 | // if the translation of the shadow is big enough we're going to end up |
| 605 | // filling the entire umbra, so we can treat these as all the same |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 606 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 607 | } else if (factory.fOffset.length()*scale + scale < radius) { |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 608 | // if we don't translate more than the blur distance, can assume umbra is covered |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 609 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 610 | } else { |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 611 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 612 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 613 | // need to add this after we classify the shadow |
| 614 | factory.fOffset.fX += viewMatrix.getTranslateX(); |
| 615 | factory.fOffset.fY += viewMatrix.getTranslateY(); |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 616 | #ifdef DEBUG_SHADOW_CHECKS |
| 617 | switch (factory.fOccluderType) { |
| 618 | case SpotVerticesFactory::OccluderType::kTransparent: |
| 619 | color = 0xFFD2B48C; // tan for transparent |
| 620 | break; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 621 | case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra: |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 622 | color = 0xFFFFA500; // orange for opaque |
| 623 | break; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 624 | case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra: |
| 625 | color = 0xFFE5E500; // corn yellow for covered |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | #endif |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 629 | draw_shadow(factory, drawVertsProc, shadowedPath, renderColor); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 630 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 631 | } |
| 632 | } |