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