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" |
Mike Reed | 8e03f69 | 2018-03-09 16:18:56 -0500 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 10 | #include "SkCanvas.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 12 | #include "SkColorData.h" |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 13 | #include "SkDevice.h" |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 14 | #include "SkDrawShadowInfo.h" |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame^] | 15 | #include "SkMaskFilter.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 16 | #include "SkPath.h" |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame] | 17 | #include "SkPM4f.h" |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 18 | #include "SkRandom.h" |
Mike Reed | 6533159 | 2017-05-24 16:45:34 -0400 | [diff] [blame] | 19 | #include "SkRasterPipeline.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 20 | #include "SkResourceCache.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 21 | #include "SkShadowTessellator.h" |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 22 | #include "SkString.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 23 | #include "SkTLazy.h" |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 24 | #include "SkVertices.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 25 | #if SK_SUPPORT_GPU |
| 26 | #include "GrShape.h" |
| 27 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 28 | #endif |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Gaussian color filter -- produces a Gaussian ramp based on the color's B value, |
| 32 | * then blends with the color's G value. |
| 33 | * Final result is black with alpha of Gaussian(B)*G. |
| 34 | * The assumption is that the original color's alpha is 1. |
| 35 | */ |
Mike Reed | 57c2b8b | 2017-12-31 15:23:54 -0500 | [diff] [blame] | 36 | class SkGaussianColorFilter : public SkColorFilter { |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 37 | public: |
| 38 | static sk_sp<SkColorFilter> Make() { |
| 39 | return sk_sp<SkColorFilter>(new SkGaussianColorFilter); |
| 40 | } |
| 41 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 42 | #if SK_SUPPORT_GPU |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 43 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor( |
| 44 | GrContext*, const GrColorSpaceInfo&) const override; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | SK_TO_STRING_OVERRIDE() |
| 48 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter) |
| 49 | |
| 50 | protected: |
| 51 | void flatten(SkWriteBuffer&) const override {} |
Mike Reed | 6533159 | 2017-05-24 16:45:34 -0400 | [diff] [blame] | 52 | void onAppendStages(SkRasterPipeline* pipeline, SkColorSpace* dstCS, SkArenaAlloc* alloc, |
| 53 | bool shaderIsOpaque) const override { |
| 54 | pipeline->append(SkRasterPipeline::gauss_a_to_rgba); |
| 55 | } |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 56 | private: |
| 57 | SkGaussianColorFilter() : INHERITED() {} |
| 58 | |
| 59 | typedef SkColorFilter INHERITED; |
| 60 | }; |
| 61 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 62 | sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) { |
| 63 | return Make(); |
| 64 | } |
| 65 | |
| 66 | #ifndef SK_IGNORE_TO_STRING |
| 67 | void SkGaussianColorFilter::toString(SkString* str) const { |
| 68 | str->append("SkGaussianColorFilter "); |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #if SK_SUPPORT_GPU |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 73 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 74 | std::unique_ptr<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor( |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 75 | GrContext*, const GrColorSpaceInfo&) const { |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 76 | return GrBlurredEdgeFragmentProcessor::Make(GrBlurredEdgeFragmentProcessor::Mode::kGaussian); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 77 | } |
| 78 | #endif |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 81 | |
| 82 | namespace { |
| 83 | |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 84 | uint64_t resource_cache_shared_id() { |
| 85 | return 0x2020776f64616873llu; // 'shadow ' |
| 86 | } |
| 87 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 88 | /** Factory for an ambient shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 89 | struct AmbientVerticesFactory { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 90 | 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] | 91 | bool fTransparent; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 92 | SkVector fOffset; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 93 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 94 | bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 95 | if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 96 | return false; |
| 97 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 98 | *translate = that.fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 99 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 100 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 101 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 102 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm, |
| 103 | SkVector* translate) const { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 104 | SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 105 | // pick a canonical place to generate shadow |
| 106 | SkMatrix noTrans(ctm); |
| 107 | if (!ctm.hasPerspective()) { |
| 108 | noTrans[SkMatrix::kMTransX] = 0; |
| 109 | noTrans[SkMatrix::kMTransY] = 0; |
| 110 | } |
| 111 | *translate = fOffset; |
| 112 | return SkShadowTessellator::MakeAmbient(path, noTrans, zParams, 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 { |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 119 | // The umbra cannot be dropped out because either the occluder is not opaque, |
| 120 | // or the center of the umbra is visible. |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 121 | kTransparent, |
| 122 | // The umbra can be dropped where it is occluded. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 123 | kOpaquePartialUmbra, |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 124 | // It is known that the entire umbra is occluded. |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 125 | kOpaqueNoUmbra |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 126 | }; |
| 127 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 128 | SkVector fOffset; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 129 | SkPoint fLocalCenter; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 130 | SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed. |
| 131 | SkPoint3 fDevLightPos; |
| 132 | SkScalar fLightRadius; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 133 | OccluderType fOccluderType; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 134 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 135 | bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 136 | if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ || |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 137 | fLightRadius != that.fLightRadius || 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: |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 142 | case OccluderType::kOpaqueNoUmbra: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 143 | // 'this' and 'that' will either both have no umbra removed or both have all the |
| 144 | // umbra removed. |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 145 | *translate = that.fOffset; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 146 | return true; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 147 | case OccluderType::kOpaquePartialUmbra: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 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 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 156 | SK_ABORT("Uninitialized occluder type?"); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 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 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 160 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm, |
| 161 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 162 | bool transparent = OccluderType::kTransparent == fOccluderType; |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 163 | SkPoint3 zParams = SkPoint3::Make(0, 0, fOccluderHeight); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 164 | if (ctm.hasPerspective() || OccluderType::kOpaquePartialUmbra == fOccluderType) { |
| 165 | translate->set(0, 0); |
| 166 | return SkShadowTessellator::MakeSpot(path, ctm, zParams, |
| 167 | fDevLightPos, fLightRadius, transparent); |
| 168 | } else { |
| 169 | // pick a canonical place to generate shadow, with light centered over path |
| 170 | SkMatrix noTrans(ctm); |
| 171 | noTrans[SkMatrix::kMTransX] = 0; |
| 172 | noTrans[SkMatrix::kMTransY] = 0; |
| 173 | SkPoint devCenter(fLocalCenter); |
| 174 | noTrans.mapPoints(&devCenter, 1); |
| 175 | SkPoint3 centerLightPos = SkPoint3::Make(devCenter.fX, devCenter.fY, fDevLightPos.fZ); |
| 176 | *translate = fOffset; |
| 177 | return SkShadowTessellator::MakeSpot(path, noTrans, zParams, |
| 178 | centerLightPos, fLightRadius, transparent); |
| 179 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 180 | } |
| 181 | }; |
| 182 | |
| 183 | /** |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 184 | * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache |
| 185 | * 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] | 186 | * 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] | 187 | * a new Rec with an adjusted size for any deletions/additions. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 188 | */ |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 189 | class CachedTessellations : public SkRefCnt { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 190 | public: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 191 | size_t size() const { return fAmbientSet.size() + fSpotSet.size(); } |
| 192 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 193 | sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix, |
| 194 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 195 | return fAmbientSet.find(ambient, matrix, translate); |
| 196 | } |
| 197 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 198 | sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient, |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 199 | const SkMatrix& matrix, SkVector* translate) { |
| 200 | return fAmbientSet.add(devPath, ambient, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 203 | sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix, |
| 204 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 205 | return fSpotSet.find(spot, matrix, translate); |
| 206 | } |
| 207 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 208 | sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot, |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 209 | const SkMatrix& matrix, SkVector* translate) { |
| 210 | return fSpotSet.add(devPath, spot, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | private: |
| 214 | template <typename FACTORY, int MAX_ENTRIES> |
| 215 | class Set { |
| 216 | public: |
| 217 | size_t size() const { return fSize; } |
| 218 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 219 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 220 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 221 | for (int i = 0; i < MAX_ENTRIES; ++i) { |
| 222 | if (fEntries[i].fFactory.isCompatible(factory, translate)) { |
| 223 | const SkMatrix& m = fEntries[i].fMatrix; |
| 224 | if (matrix.hasPerspective() || m.hasPerspective()) { |
| 225 | if (matrix != fEntries[i].fMatrix) { |
| 226 | continue; |
| 227 | } |
| 228 | } else if (matrix.getScaleX() != m.getScaleX() || |
| 229 | matrix.getSkewX() != m.getSkewX() || |
| 230 | matrix.getScaleY() != m.getScaleY() || |
| 231 | matrix.getSkewY() != m.getSkewY()) { |
| 232 | continue; |
| 233 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 234 | return fEntries[i].fVertices; |
| 235 | } |
| 236 | } |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 240 | sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix, |
| 241 | SkVector* translate) { |
| 242 | sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix, translate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 243 | if (!vertices) { |
| 244 | return nullptr; |
| 245 | } |
| 246 | int i; |
| 247 | if (fCount < MAX_ENTRIES) { |
| 248 | i = fCount++; |
| 249 | } else { |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 250 | i = fRandom.nextULessThan(MAX_ENTRIES); |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 251 | fSize -= fEntries[i].fVertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 252 | } |
| 253 | fEntries[i].fFactory = factory; |
| 254 | fEntries[i].fVertices = vertices; |
| 255 | fEntries[i].fMatrix = matrix; |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 256 | fSize += vertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 257 | return vertices; |
| 258 | } |
| 259 | |
| 260 | private: |
| 261 | struct Entry { |
| 262 | FACTORY fFactory; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 263 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 264 | SkMatrix fMatrix; |
| 265 | }; |
| 266 | Entry fEntries[MAX_ENTRIES]; |
| 267 | int fCount = 0; |
| 268 | size_t fSize = 0; |
Jim Van Verth | eb63eb7 | 2017-05-23 09:40:02 -0400 | [diff] [blame] | 269 | SkRandom fRandom; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | Set<AmbientVerticesFactory, 4> fAmbientSet; |
| 273 | Set<SpotVerticesFactory, 4> fSpotSet; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 274 | }; |
| 275 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 276 | /** |
| 277 | * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular |
| 278 | * path. The key represents the path's geometry and not any shadow params. |
| 279 | */ |
| 280 | class CachedTessellationsRec : public SkResourceCache::Rec { |
| 281 | public: |
| 282 | CachedTessellationsRec(const SkResourceCache::Key& key, |
| 283 | sk_sp<CachedTessellations> tessellations) |
| 284 | : fTessellations(std::move(tessellations)) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 285 | fKey.reset(new uint8_t[key.size()]); |
| 286 | memcpy(fKey.get(), &key, key.size()); |
| 287 | } |
| 288 | |
| 289 | const Key& getKey() const override { |
| 290 | return *reinterpret_cast<SkResourceCache::Key*>(fKey.get()); |
| 291 | } |
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 | size_t bytesUsed() const override { return fTessellations->size(); } |
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 | const char* getCategory() const override { return "tessellated shadow masks"; } |
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 | sk_sp<CachedTessellations> refTessellations() const { return fTessellations; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 298 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 299 | template <typename FACTORY> |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 300 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 301 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 302 | return fTessellations->find(factory, matrix, translate); |
| 303 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 304 | |
| 305 | private: |
| 306 | std::unique_ptr<uint8_t[]> fKey; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 307 | sk_sp<CachedTessellations> fTessellations; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | /** |
| 311 | * 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] | 312 | * vertices and a translation vector. If the CachedTessellations does not contain a suitable |
| 313 | * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations |
| 314 | * 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] | 315 | */ |
| 316 | template <typename FACTORY> |
| 317 | struct FindContext { |
| 318 | FindContext(const SkMatrix* viewMatrix, const FACTORY* factory) |
| 319 | : fViewMatrix(viewMatrix), fFactory(factory) {} |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 320 | const SkMatrix* const fViewMatrix; |
| 321 | // If this is valid after Find is called then we found the vertices and they should be drawn |
| 322 | // with fTranslate applied. |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 323 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 324 | SkVector fTranslate = {0, 0}; |
| 325 | |
| 326 | // If this is valid after Find then the caller should add the vertices to the tessellation set |
| 327 | // and create a new CachedTessellationsRec and insert it into SkResourceCache. |
| 328 | sk_sp<CachedTessellations> fTessellationsOnFailure; |
| 329 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 330 | const FACTORY* fFactory; |
| 331 | }; |
| 332 | |
| 333 | /** |
| 334 | * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of |
| 335 | * the FindContext are used to determine if the vertices are reusable. If so the vertices and |
| 336 | * necessary translation vector are set on the FindContext. |
| 337 | */ |
| 338 | template <typename FACTORY> |
| 339 | bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) { |
| 340 | FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 341 | const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec); |
| 342 | findContext->fVertices = |
| 343 | rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate); |
| 344 | if (findContext->fVertices) { |
| 345 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 346 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 347 | // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been |
| 348 | // manipulated we will add a new Rec. |
| 349 | findContext->fTessellationsOnFailure = rec.refTessellations(); |
| 350 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | class ShadowedPath { |
| 354 | public: |
| 355 | ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix) |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 356 | : fPath(path) |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 357 | , fViewMatrix(viewMatrix) |
| 358 | #if SK_SUPPORT_GPU |
| 359 | , fShapeForKey(*path, GrStyle::SimpleFill()) |
| 360 | #endif |
| 361 | {} |
| 362 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 363 | const SkPath& path() const { return *fPath; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 364 | const SkMatrix& viewMatrix() const { return *fViewMatrix; } |
| 365 | #if SK_SUPPORT_GPU |
| 366 | /** Negative means the vertices should not be cached for this path. */ |
| 367 | int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); } |
| 368 | void writeKey(void* key) const { |
| 369 | fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key)); |
| 370 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 371 | bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 372 | #else |
| 373 | int keyBytes() const { return -1; } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 374 | void writeKey(void* key) const { SK_ABORT("Should never be called"); } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 375 | bool isRRect(SkRRect* rrect) { return false; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 376 | #endif |
| 377 | |
| 378 | private: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 379 | const SkPath* fPath; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 380 | const SkMatrix* fViewMatrix; |
| 381 | #if SK_SUPPORT_GPU |
| 382 | GrShape fShapeForKey; |
| 383 | #endif |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 384 | }; |
| 385 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 386 | // This creates a domain of keys in SkResourceCache used by this file. |
| 387 | static void* kNamespace; |
| 388 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 389 | /** |
| 390 | * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless |
| 391 | * they are first found in SkResourceCache. |
| 392 | */ |
| 393 | template <typename FACTORY> |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 394 | bool draw_shadow(const FACTORY& factory, |
| 395 | std::function<void(const SkVertices*, SkBlendMode, const SkPaint&, |
| 396 | SkScalar tx, SkScalar ty)> drawProc, ShadowedPath& path, SkColor color) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 397 | FindContext<FACTORY> context(&path.viewMatrix(), &factory); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 398 | |
| 399 | SkResourceCache::Key* key = nullptr; |
| 400 | SkAutoSTArray<32 * 4, uint8_t> keyStorage; |
| 401 | int keyDataBytes = path.keyBytes(); |
| 402 | if (keyDataBytes >= 0) { |
| 403 | keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key)); |
| 404 | key = new (keyStorage.begin()) SkResourceCache::Key(); |
| 405 | path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key))); |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 406 | key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 407 | SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 410 | sk_sp<SkVertices> vertices; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 411 | bool foundInCache = SkToBool(context.fVertices); |
| 412 | if (foundInCache) { |
| 413 | vertices = std::move(context.fVertices); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 414 | } else { |
| 415 | // TODO: handle transforming the path as part of the tessellator |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 416 | if (key) { |
| 417 | // Update or initialize a tessellation set and add it to the cache. |
| 418 | sk_sp<CachedTessellations> tessellations; |
| 419 | if (context.fTessellationsOnFailure) { |
| 420 | tessellations = std::move(context.fTessellationsOnFailure); |
| 421 | } else { |
| 422 | tessellations.reset(new CachedTessellations()); |
| 423 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 424 | vertices = tessellations->add(path.path(), factory, path.viewMatrix(), |
| 425 | &context.fTranslate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 426 | if (!vertices) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 427 | return false; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 428 | } |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 429 | auto rec = new CachedTessellationsRec(*key, std::move(tessellations)); |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 430 | SkResourceCache::Add(rec); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 431 | } else { |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 432 | vertices = factory.makeVertices(path.path(), path.viewMatrix(), |
| 433 | &context.fTranslate); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 434 | if (!vertices) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 435 | return false; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 436 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 437 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 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. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 443 | paint.setColorFilter( |
| 444 | SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate)->makeComposed( |
| 445 | SkGaussianColorFilter::Make())); |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 446 | |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 447 | drawProc(vertices.get(), SkBlendMode::kModulate, paint, |
| 448 | context.fTranslate.fX, context.fTranslate.fY); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 449 | |
| 450 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 454 | static bool tilted(const SkPoint3& zPlaneParams) { |
| 455 | return !SkScalarNearlyZero(zPlaneParams.fX) || !SkScalarNearlyZero(zPlaneParams.fY); |
| 456 | } |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 457 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 458 | static SkPoint3 map(const SkMatrix& m, const SkPoint3& pt) { |
| 459 | SkPoint3 result; |
| 460 | m.mapXY(pt.fX, pt.fY, (SkPoint*)&result.fX); |
| 461 | result.fZ = pt.fZ; |
| 462 | return result; |
| 463 | } |
| 464 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 465 | void SkShadowUtils::ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor, |
| 466 | SkColor* outAmbientColor, SkColor* outSpotColor) { |
| 467 | // For tonal color we only compute color values for the spot shadow. |
| 468 | // The ambient shadow is greyscale only. |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 469 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 470 | // Ambient |
| 471 | *outAmbientColor = SkColorSetARGB(SkColorGetA(inAmbientColor), 0, 0, 0); |
Jim Van Verth | 34d6e4b | 2017-06-09 11:09:03 -0400 | [diff] [blame] | 472 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 473 | // Spot |
| 474 | int spotR = SkColorGetR(inSpotColor); |
| 475 | int spotG = SkColorGetG(inSpotColor); |
| 476 | int spotB = SkColorGetB(inSpotColor); |
| 477 | int max = SkTMax(SkTMax(spotR, spotG), spotB); |
| 478 | int min = SkTMin(SkTMin(spotR, spotG), spotB); |
| 479 | SkScalar luminance = 0.5f*(max + min)/255.f; |
| 480 | SkScalar origA = SkColorGetA(inSpotColor)/255.f; |
| 481 | |
| 482 | // We compute a color alpha value based on the luminance of the color, scaled by an |
| 483 | // adjusted alpha value. We want the following properties to match the UX examples |
| 484 | // (assuming a = 0.25) and to ensure that we have reasonable results when the color |
| 485 | // is black and/or the alpha is 0: |
| 486 | // f(0, a) = 0 |
| 487 | // f(luminance, 0) = 0 |
| 488 | // f(1, 0.25) = .5 |
| 489 | // f(0.5, 0.25) = .4 |
| 490 | // f(1, 1) = 1 |
| 491 | // The following functions match this as closely as possible. |
| 492 | SkScalar alphaAdjust = (2.6f + (-2.66667f + 1.06667f*origA)*origA)*origA; |
| 493 | SkScalar colorAlpha = (3.544762f + (-4.891428f + 2.3466f*luminance)*luminance)*luminance; |
| 494 | colorAlpha = SkTPin(alphaAdjust*colorAlpha, 0.0f, 1.0f); |
| 495 | |
| 496 | // Similarly, we set the greyscale alpha based on luminance and alpha so that |
| 497 | // f(0, a) = a |
| 498 | // f(luminance, 0) = 0 |
| 499 | // f(1, 0.25) = 0.15 |
| 500 | SkScalar greyscaleAlpha = SkTPin(origA*(1 - 0.4f*luminance), 0.0f, 1.0f); |
| 501 | |
| 502 | // The final color we want to emulate is generated by rendering a color shadow (C_rgb) using an |
| 503 | // alpha computed from the color's luminance (C_a), and then a black shadow with alpha (S_a) |
| 504 | // which is an adjusted value of 'a'. Assuming SrcOver, a background color of B_rgb, and |
| 505 | // ignoring edge falloff, this becomes |
| 506 | // |
| 507 | // (C_a - S_a*C_a)*C_rgb + (1 - (S_a + C_a - S_a*C_a))*B_rgb |
| 508 | // |
| 509 | // Assuming premultiplied alpha, this means we scale the color by (C_a - S_a*C_a) and |
| 510 | // set the alpha to (S_a + C_a - S_a*C_a). |
| 511 | SkScalar colorScale = colorAlpha*(SK_Scalar1 - greyscaleAlpha); |
| 512 | SkScalar tonalAlpha = colorScale + greyscaleAlpha; |
| 513 | SkScalar unPremulScale = colorScale / tonalAlpha; |
| 514 | *outSpotColor = SkColorSetARGB(tonalAlpha*255.999f, |
| 515 | unPremulScale*spotR, |
| 516 | unPremulScale*spotG, |
| 517 | unPremulScale*spotB); |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 520 | // 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] | 521 | void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 522 | const SkPoint3& devLightPos, SkScalar lightRadius, |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 523 | SkColor ambientColor, SkColor spotColor, |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 524 | uint32_t flags) { |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 525 | SkMatrix inverse; |
| 526 | if (!canvas->getTotalMatrix().invert(&inverse)) { |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 527 | return; |
| 528 | } |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 529 | SkPoint pt = inverse.mapXY(devLightPos.fX, devLightPos.fY); |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 530 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 531 | SkDrawShadowRec rec; |
| 532 | rec.fZPlaneParams = zPlaneParams; |
| 533 | rec.fLightPos = { pt.fX, pt.fY, devLightPos.fZ }; |
| 534 | rec.fLightRadius = lightRadius; |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 535 | rec.fAmbientColor = ambientColor; |
| 536 | rec.fSpotColor = spotColor; |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 537 | rec.fFlags = flags; |
| 538 | |
| 539 | canvas->private_draw_shadow_rec(path, rec); |
| 540 | } |
| 541 | |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 542 | static bool validate_rec(const SkDrawShadowRec& rec) { |
| 543 | return rec.fLightPos.isFinite() && rec.fZPlaneParams.isFinite() && |
| 544 | SkScalarIsFinite(rec.fLightRadius); |
| 545 | } |
| 546 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 547 | void SkBaseDevice::drawShadow(const SkPath& path, const SkDrawShadowRec& rec) { |
| 548 | auto drawVertsProc = [this](const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint, |
| 549 | SkScalar tx, SkScalar ty) { |
| 550 | SkAutoDeviceCTMRestore adr(this, SkMatrix::Concat(this->ctm(), |
| 551 | SkMatrix::MakeTrans(tx, ty))); |
| 552 | this->drawVertices(vertices, mode, paint); |
| 553 | }; |
| 554 | |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 555 | if (!validate_rec(rec)) { |
| 556 | return; |
| 557 | } |
| 558 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 559 | SkMatrix viewMatrix = this->ctm(); |
| 560 | SkAutoDeviceCTMRestore adr(this, SkMatrix::I()); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 561 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 562 | ShadowedPath shadowedPath(&path, &viewMatrix); |
| 563 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 564 | bool tiltZPlane = tilted(rec.fZPlaneParams); |
| 565 | bool transparent = SkToBool(rec.fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 566 | bool uncached = tiltZPlane || path.isVolatile(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 567 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 568 | SkPoint3 zPlaneParams = rec.fZPlaneParams; |
| 569 | SkPoint3 devLightPos = map(viewMatrix, rec.fLightPos); |
| 570 | float lightRadius = rec.fLightRadius; |
| 571 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 572 | if (SkColorGetA(rec.fAmbientColor) > 0) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 573 | bool success = false; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 574 | if (uncached) { |
| 575 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix, |
| 576 | zPlaneParams, |
| 577 | transparent); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 578 | if (vertices) { |
| 579 | SkPaint paint; |
| 580 | // Run the vertex color through a GaussianColorFilter and then modulate the |
| 581 | // grayscale result of that against our 'color' param. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 582 | paint.setColorFilter( |
| 583 | SkColorFilter::MakeModeFilter(rec.fAmbientColor, |
| 584 | SkBlendMode::kModulate)->makeComposed( |
| 585 | SkGaussianColorFilter::Make())); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 586 | this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 587 | success = true; |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 588 | } |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | if (!success) { |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 592 | AmbientVerticesFactory factory; |
| 593 | factory.fOccluderHeight = zPlaneParams.fZ; |
| 594 | factory.fTransparent = transparent; |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 595 | if (viewMatrix.hasPerspective()) { |
| 596 | factory.fOffset.set(0, 0); |
| 597 | } else { |
| 598 | factory.fOffset.fX = viewMatrix.getTranslateX(); |
| 599 | factory.fOffset.fY = viewMatrix.getTranslateY(); |
| 600 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 601 | |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 602 | if (!draw_shadow(factory, drawVertsProc, shadowedPath, rec.fAmbientColor)) { |
| 603 | // Pretransform the path to avoid transforming the stroke, below. |
| 604 | SkPath devSpacePath; |
| 605 | path.transform(viewMatrix, &devSpacePath); |
| 606 | |
| 607 | // The tesselator outsets by AmbientBlurRadius (or 'r') to get the outer ring of |
| 608 | // the tesselation, uses the original path as the inner ring, and sets the alpha |
| 609 | // of the inner ring to 1/AmbientRecipAlpha (or 'a'). |
| 610 | // |
| 611 | // We want to emulate this with a blur. The full blur width (2*blurRadius or 'f') |
| 612 | // can be calculated by interpolating: |
| 613 | // |
| 614 | // original edge outer edge |
| 615 | // | |<---------- r ------>| |
| 616 | // |<------|--- f -------------->| |
| 617 | // | | | |
| 618 | // alpha = 1 alpha = a alpha = 0 |
| 619 | // |
| 620 | // Taking ratios, f/1 = r/a, so f = r/a and blurRadius = f/2. |
| 621 | // |
| 622 | // We now need to outset the path to place the new edge in the center of the |
| 623 | // blur region: |
| 624 | // |
| 625 | // original new |
| 626 | // | |<------|--- r ------>| |
| 627 | // |<------|--- f -|------------>| |
| 628 | // | |<- o ->|<--- f/2 --->| |
| 629 | // |
| 630 | // r = o + f/2, so o = r - f/2 |
| 631 | // |
| 632 | // We outset by using the stroker, so the strokeWidth is o/2. |
| 633 | // |
| 634 | SkScalar devSpaceOutset = SkDrawShadowMetrics::AmbientBlurRadius(zPlaneParams.fZ); |
| 635 | SkScalar oneOverA = SkDrawShadowMetrics::AmbientRecipAlpha(zPlaneParams.fZ); |
| 636 | SkScalar blurRadius = 0.5f*devSpaceOutset*oneOverA; |
| 637 | SkScalar strokeWidth = 0.5f*(devSpaceOutset - blurRadius); |
| 638 | |
| 639 | // Now draw with blur |
| 640 | SkPaint paint; |
| 641 | paint.setColor(rec.fAmbientColor); |
| 642 | paint.setStrokeWidth(strokeWidth); |
| 643 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
Mike Reed | 8e03f69 | 2018-03-09 16:18:56 -0500 | [diff] [blame] | 644 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(blurRadius); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame^] | 645 | bool respectCTM = false; |
| 646 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM)); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 647 | this->drawPath(devSpacePath, paint); |
| 648 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 649 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Jim Van Verth | b1b80f7 | 2018-01-18 15:19:13 -0500 | [diff] [blame] | 652 | if (SkColorGetA(rec.fSpotColor) > 0) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 653 | bool success = false; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 654 | if (uncached) { |
| 655 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix, |
| 656 | zPlaneParams, |
| 657 | devLightPos, lightRadius, |
| 658 | transparent); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 659 | if (vertices) { |
| 660 | SkPaint paint; |
| 661 | // Run the vertex color through a GaussianColorFilter and then modulate the |
| 662 | // grayscale result of that against our 'color' param. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 663 | paint.setColorFilter( |
| 664 | SkColorFilter::MakeModeFilter(rec.fSpotColor, |
| 665 | SkBlendMode::kModulate)->makeComposed( |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 666 | SkGaussianColorFilter::Make())); |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 667 | this->drawVertices(vertices.get(), SkBlendMode::kModulate, paint); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 668 | success = true; |
Jim Van Verth | 7d8955e | 2017-07-13 15:13:52 -0400 | [diff] [blame] | 669 | } |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 670 | } |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 671 | |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 672 | if (!success) { |
| 673 | SpotVerticesFactory factory; |
| 674 | factory.fOccluderHeight = zPlaneParams.fZ; |
| 675 | factory.fDevLightPos = devLightPos; |
| 676 | factory.fLightRadius = lightRadius; |
| 677 | |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 678 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 679 | factory.fLocalCenter = center; |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 680 | viewMatrix.mapPoints(¢er, 1); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 681 | SkScalar radius, scale; |
| 682 | SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX - center.fX, |
| 683 | devLightPos.fY - center.fY, devLightPos.fZ, |
| 684 | lightRadius, &radius, &scale, &factory.fOffset); |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 685 | SkRect devBounds; |
| 686 | viewMatrix.mapRect(&devBounds, path.getBounds()); |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 687 | if (transparent || |
| 688 | SkTAbs(factory.fOffset.fX) > 0.5f*devBounds.width() || |
| 689 | SkTAbs(factory.fOffset.fY) > 0.5f*devBounds.height()) { |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 690 | // if the translation of the shadow is big enough we're going to end up |
| 691 | // 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] | 692 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 693 | } else if (factory.fOffset.length()*scale + scale < radius) { |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 694 | // 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] | 695 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaqueNoUmbra; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 696 | } else { |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 697 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaquePartialUmbra; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 698 | } |
Jim Van Verth | 8793e38 | 2017-05-22 15:52:21 -0400 | [diff] [blame] | 699 | // need to add this after we classify the shadow |
| 700 | factory.fOffset.fX += viewMatrix.getTranslateX(); |
| 701 | factory.fOffset.fY += viewMatrix.getTranslateY(); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 702 | |
| 703 | SkColor color = rec.fSpotColor; |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 704 | #ifdef DEBUG_SHADOW_CHECKS |
| 705 | switch (factory.fOccluderType) { |
| 706 | case SpotVerticesFactory::OccluderType::kTransparent: |
| 707 | color = 0xFFD2B48C; // tan for transparent |
| 708 | break; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 709 | case SpotVerticesFactory::OccluderType::kOpaquePartialUmbra: |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 710 | color = 0xFFFFA500; // orange for opaque |
| 711 | break; |
Jim Van Verth | 78c8f30 | 2017-05-15 10:44:22 -0400 | [diff] [blame] | 712 | case SpotVerticesFactory::OccluderType::kOpaqueNoUmbra: |
| 713 | color = 0xFFE5E500; // corn yellow for covered |
Jim Van Verth | a783c36 | 2017-05-11 17:05:28 -0400 | [diff] [blame] | 714 | break; |
| 715 | } |
| 716 | #endif |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 717 | if (!draw_shadow(factory, drawVertsProc, shadowedPath, color)) { |
| 718 | // draw with blur |
| 719 | SkVector translate; |
| 720 | SkDrawShadowMetrics::GetSpotParams(zPlaneParams.fZ, devLightPos.fX, |
| 721 | devLightPos.fY, devLightPos.fZ, |
| 722 | lightRadius, &radius, &scale, &translate); |
| 723 | SkMatrix shadowMatrix; |
| 724 | shadowMatrix.setScaleTranslate(scale, scale, translate.fX, translate.fY); |
| 725 | SkAutoDeviceCTMRestore adr(this, SkMatrix::Concat(shadowMatrix, viewMatrix)); |
| 726 | |
| 727 | SkPaint paint; |
| 728 | paint.setColor(rec.fSpotColor); |
Mike Reed | 8e03f69 | 2018-03-09 16:18:56 -0500 | [diff] [blame] | 729 | SkScalar sigma = SkBlurMask::ConvertRadiusToSigma(radius); |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame^] | 730 | bool respectCTM = false; |
| 731 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, respectCTM)); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 732 | this->drawPath(path, paint); |
| 733 | } |
Jim Van Verth | 37c5a96 | 2017-05-10 14:13:24 -0400 | [diff] [blame] | 734 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 735 | } |
| 736 | } |