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