Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkShadowUtils.h" |
| 9 | #include "SkCanvas.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 12 | #include "SkPath.h" |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 13 | #include "SkPM4f.h" |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 14 | #include "SkRandom.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 15 | #include "SkResourceCache.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 16 | #include "SkShadowTessellator.h" |
Ben Wagner | 4d1955c | 2017-03-10 13:08:15 -0500 | [diff] [blame] | 17 | #include "SkString.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 18 | #include "SkTLazy.h" |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 19 | #include "SkVertices.h" |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrShape.h" |
| 22 | #include "effects/GrBlurredEdgeFragmentProcessor.h" |
| 23 | #endif |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 24 | #include "../../src/effects/shadows/SkAmbientShadowMaskFilter.h" |
| 25 | #include "../../src/effects/shadows/SkSpotShadowMaskFilter.h" |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Gaussian color filter -- produces a Gaussian ramp based on the color's B value, |
| 29 | * then blends with the color's G value. |
| 30 | * Final result is black with alpha of Gaussian(B)*G. |
| 31 | * The assumption is that the original color's alpha is 1. |
| 32 | */ |
| 33 | class SK_API SkGaussianColorFilter : public SkColorFilter { |
| 34 | public: |
| 35 | static sk_sp<SkColorFilter> Make() { |
| 36 | return sk_sp<SkColorFilter>(new SkGaussianColorFilter); |
| 37 | } |
| 38 | |
| 39 | void filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const override; |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 40 | void filterSpan4f(const SkPM4f src[], int count, SkPM4f result[]) const override; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 41 | |
| 42 | #if SK_SUPPORT_GPU |
| 43 | sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, SkColorSpace*) const override; |
| 44 | #endif |
| 45 | |
| 46 | SK_TO_STRING_OVERRIDE() |
| 47 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkGaussianColorFilter) |
| 48 | |
| 49 | protected: |
| 50 | void flatten(SkWriteBuffer&) const override {} |
| 51 | |
| 52 | private: |
| 53 | SkGaussianColorFilter() : INHERITED() {} |
| 54 | |
| 55 | typedef SkColorFilter INHERITED; |
| 56 | }; |
| 57 | |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 58 | static inline float eval_gaussian(float x) { |
| 59 | float factor = 1 - x; |
| 60 | return sk_float_exp(-factor * factor * 4) - 0.018f; |
| 61 | } |
| 62 | |
Mike Reed | 37f1655 | 2017-05-02 15:13:43 -0400 | [diff] [blame] | 63 | static void build_table() { |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 64 | SkDebugf("const uint8_t gByteExpU8Table[256] = {"); |
Mike Reed | 37f1655 | 2017-05-02 15:13:43 -0400 | [diff] [blame] | 65 | for (int i = 0; i <= 255; ++i) { |
| 66 | if (!(i % 8)) { |
| 67 | SkDebugf("\n"); |
| 68 | } |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 69 | int v = (int)(eval_gaussian(i / 255.f) * 256); |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 70 | SkDebugf(" 0x%02X,", v); |
Mike Reed | 37f1655 | 2017-05-02 15:13:43 -0400 | [diff] [blame] | 71 | } |
| 72 | SkDebugf("\n};\n"); |
| 73 | } |
| 74 | |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 75 | const uint8_t gByteExpU8Table[256] = { |
| 76 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, |
| 77 | 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, |
| 78 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 79 | 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x07, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 80 | 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, |
| 81 | 0x0A, 0x0A, 0x0B, 0x0B, 0x0B, 0x0C, 0x0C, 0x0D, |
| 82 | 0x0D, 0x0E, 0x0E, 0x0F, 0x0F, 0x10, 0x10, 0x11, |
| 83 | 0x11, 0x12, 0x12, 0x13, 0x14, 0x14, 0x15, 0x15, |
| 84 | 0x16, 0x17, 0x17, 0x18, 0x19, 0x19, 0x1A, 0x1B, |
| 85 | 0x1C, 0x1C, 0x1D, 0x1E, 0x1F, 0x1F, 0x20, 0x21, |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 86 | 0x22, 0x23, 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 87 | 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, |
| 88 | 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x38, 0x39, |
| 89 | 0x3A, 0x3B, 0x3C, 0x3D, 0x3F, 0x40, 0x41, 0x42, |
| 90 | 0x44, 0x45, 0x46, 0x48, 0x49, 0x4A, 0x4C, 0x4D, |
| 91 | 0x4E, 0x50, 0x51, 0x53, 0x54, 0x55, 0x57, 0x58, |
| 92 | 0x5A, 0x5B, 0x5D, 0x5E, 0x60, 0x61, 0x63, 0x64, |
| 93 | 0x66, 0x68, 0x69, 0x6B, 0x6C, 0x6E, 0x70, 0x71, |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 94 | 0x73, 0x75, 0x76, 0x78, 0x79, 0x7B, 0x7D, 0x7F, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 95 | 0x80, 0x82, 0x84, 0x85, 0x87, 0x89, 0x8A, 0x8C, |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 96 | 0x8E, 0x90, 0x91, 0x93, 0x95, 0x96, 0x98, 0x9A, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 97 | 0x9C, 0x9D, 0x9F, 0xA1, 0xA2, 0xA4, 0xA6, 0xA8, |
| 98 | 0xA9, 0xAB, 0xAD, 0xAE, 0xB0, 0xB2, 0xB3, 0xB5, |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 99 | 0xB7, 0xB8, 0xBA, 0xBC, 0xBD, 0xBF, 0xC0, 0xC2, |
| 100 | 0xC3, 0xC5, 0xC7, 0xC8, 0xCA, 0xCB, 0xCD, 0xCE, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 101 | 0xCF, 0xD1, 0xD2, 0xD4, 0xD5, 0xD6, 0xD8, 0xD9, |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 102 | 0xDA, 0xDC, 0xDD, 0xDE, 0xDF, 0xE1, 0xE2, 0xE3, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 103 | 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, |
| 104 | 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF0, 0xF1, 0xF2, |
| 105 | 0xF3, 0xF3, 0xF4, 0xF5, 0xF5, 0xF6, 0xF6, 0xF7, |
| 106 | 0xF7, 0xF8, 0xF8, 0xF9, 0xF9, 0xF9, 0xFA, 0xFA, |
| 107 | 0xFA, 0xFA, 0xFA, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, |
Mike Reed | 37f1655 | 2017-05-02 15:13:43 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 110 | void SkGaussianColorFilter::filterSpan(const SkPMColor src[], int count, SkPMColor dst[]) const { |
Mike Reed | 37f1655 | 2017-05-02 15:13:43 -0400 | [diff] [blame] | 111 | // to re-build the table, call build_table() which will dump it out using SkDebugf. |
| 112 | if (false) { |
| 113 | build_table(); |
| 114 | } |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 115 | for (int i = 0; i < count; ++i) { |
| 116 | SkPMColor c = src[i]; |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 117 | uint8_t a = gByteExpU8Table[SkGetPackedA32(c)]; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 118 | dst[i] = SkPackARGB32(a, a, a, a); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Mike Reed | b9641bd | 2017-05-04 10:57:40 -0400 | [diff] [blame^] | 122 | void SkGaussianColorFilter::filterSpan4f(const SkPM4f src[], int count, SkPM4f dst[]) const { |
| 123 | for (int i = 0; i < count; ++i) { |
| 124 | float v = eval_gaussian(src[i].a()); |
| 125 | dst[i] = SkPM4f::FromPremulRGBA(v, v, v, v); |
| 126 | } |
| 127 | } |
| 128 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 129 | sk_sp<SkFlattenable> SkGaussianColorFilter::CreateProc(SkReadBuffer&) { |
| 130 | return Make(); |
| 131 | } |
| 132 | |
| 133 | #ifndef SK_IGNORE_TO_STRING |
| 134 | void SkGaussianColorFilter::toString(SkString* str) const { |
| 135 | str->append("SkGaussianColorFilter "); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | #if SK_SUPPORT_GPU |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 140 | |
| 141 | sk_sp<GrFragmentProcessor> SkGaussianColorFilter::asFragmentProcessor(GrContext*, |
| 142 | SkColorSpace*) const { |
| 143 | return GrBlurredEdgeFP::Make(GrBlurredEdgeFP::kGaussian_Mode); |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 148 | |
| 149 | namespace { |
| 150 | |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 151 | uint64_t resource_cache_shared_id() { |
| 152 | return 0x2020776f64616873llu; // 'shadow ' |
| 153 | } |
| 154 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 155 | /** Factory for an ambient shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 156 | struct AmbientVerticesFactory { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 157 | 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] | 158 | bool fTransparent; |
| 159 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 160 | bool isCompatible(const AmbientVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 161 | if (fOccluderHeight != that.fOccluderHeight || fTransparent != that.fTransparent) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | translate->set(0, 0); |
| 165 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 166 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 167 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 168 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 169 | SkScalar z = fOccluderHeight; |
| 170 | return SkShadowTessellator::MakeAmbient(path, ctm, |
| 171 | [z](SkScalar, SkScalar) { return z; }, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 172 | fTransparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 173 | } |
| 174 | }; |
| 175 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 176 | /** Factory for an spot shadow mesh with particular shadow properties. */ |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 177 | struct SpotVerticesFactory { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 178 | enum class OccluderType { |
| 179 | // The umbra cannot be dropped out because the occluder is not opaque. |
| 180 | kTransparent, |
| 181 | // The umbra can be dropped where it is occluded. |
| 182 | kOpaque, |
| 183 | // It is known that the entire umbra is occluded. |
| 184 | kOpaqueCoversUmbra |
| 185 | }; |
| 186 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 187 | SkVector fOffset; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 188 | SkScalar fOccluderHeight = SK_ScalarNaN; // NaN so that isCompatible will fail until init'ed. |
| 189 | SkPoint3 fDevLightPos; |
| 190 | SkScalar fLightRadius; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 191 | OccluderType fOccluderType; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 192 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 193 | bool isCompatible(const SpotVerticesFactory& that, SkVector* translate) const { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 194 | if (fOccluderHeight != that.fOccluderHeight || fDevLightPos.fZ != that.fDevLightPos.fZ || |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 195 | fLightRadius != that.fLightRadius || fOccluderType != that.fOccluderType) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 196 | return false; |
| 197 | } |
| 198 | switch (fOccluderType) { |
| 199 | case OccluderType::kTransparent: |
| 200 | case OccluderType::kOpaqueCoversUmbra: |
| 201 | // 'this' and 'that' will either both have no umbra removed or both have all the |
| 202 | // umbra removed. |
| 203 | *translate = that.fOffset - fOffset; |
| 204 | return true; |
| 205 | case OccluderType::kOpaque: |
| 206 | // In this case we partially remove the umbra differently for 'this' and 'that' |
| 207 | // if the offsets don't match. |
| 208 | if (fOffset == that.fOffset) { |
| 209 | translate->set(0, 0); |
| 210 | return true; |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | SkFAIL("Uninitialized occluder type?"); |
| 215 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 216 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 217 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 218 | sk_sp<SkVertices> makeVertices(const SkPath& path, const SkMatrix& ctm) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 219 | bool transparent = OccluderType::kTransparent == fOccluderType; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 220 | SkScalar z = fOccluderHeight; |
| 221 | return SkShadowTessellator::MakeSpot(path, ctm, |
| 222 | [z](SkScalar, SkScalar) -> SkScalar { return z; }, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 223 | fDevLightPos, fLightRadius, transparent); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 224 | } |
| 225 | }; |
| 226 | |
| 227 | /** |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 228 | * This manages a set of tessellations for a given shape in the cache. Because SkResourceCache |
| 229 | * records are immutable this is not itself a Rec. When we need to update it we return this on |
| 230 | * the FindVisitor and let the cache destory the Rec. We'll update the tessellations and then add |
| 231 | * a new Rec with an adjusted size for any deletions/additions. |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 232 | */ |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 233 | class CachedTessellations : public SkRefCnt { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 234 | public: |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 235 | size_t size() const { return fAmbientSet.size() + fSpotSet.size(); } |
| 236 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 237 | sk_sp<SkVertices> find(const AmbientVerticesFactory& ambient, const SkMatrix& matrix, |
| 238 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 239 | return fAmbientSet.find(ambient, matrix, translate); |
| 240 | } |
| 241 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 242 | sk_sp<SkVertices> add(const SkPath& devPath, const AmbientVerticesFactory& ambient, |
| 243 | const SkMatrix& matrix) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 244 | return fAmbientSet.add(devPath, ambient, matrix); |
| 245 | } |
| 246 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 247 | sk_sp<SkVertices> find(const SpotVerticesFactory& spot, const SkMatrix& matrix, |
| 248 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 249 | return fSpotSet.find(spot, matrix, translate); |
| 250 | } |
| 251 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 252 | sk_sp<SkVertices> add(const SkPath& devPath, const SpotVerticesFactory& spot, |
| 253 | const SkMatrix& matrix) { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 254 | return fSpotSet.add(devPath, spot, matrix); |
| 255 | } |
| 256 | |
| 257 | private: |
| 258 | template <typename FACTORY, int MAX_ENTRIES> |
| 259 | class Set { |
| 260 | public: |
| 261 | size_t size() const { return fSize; } |
| 262 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 263 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 264 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 265 | for (int i = 0; i < MAX_ENTRIES; ++i) { |
| 266 | if (fEntries[i].fFactory.isCompatible(factory, translate)) { |
| 267 | const SkMatrix& m = fEntries[i].fMatrix; |
| 268 | if (matrix.hasPerspective() || m.hasPerspective()) { |
| 269 | if (matrix != fEntries[i].fMatrix) { |
| 270 | continue; |
| 271 | } |
| 272 | } else if (matrix.getScaleX() != m.getScaleX() || |
| 273 | matrix.getSkewX() != m.getSkewX() || |
| 274 | matrix.getScaleY() != m.getScaleY() || |
| 275 | matrix.getSkewY() != m.getSkewY()) { |
| 276 | continue; |
| 277 | } |
| 278 | *translate += SkVector{matrix.getTranslateX() - m.getTranslateX(), |
| 279 | matrix.getTranslateY() - m.getTranslateY()}; |
| 280 | return fEntries[i].fVertices; |
| 281 | } |
| 282 | } |
| 283 | return nullptr; |
| 284 | } |
| 285 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 286 | sk_sp<SkVertices> add(const SkPath& path, const FACTORY& factory, const SkMatrix& matrix) { |
| 287 | sk_sp<SkVertices> vertices = factory.makeVertices(path, matrix); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 288 | if (!vertices) { |
| 289 | return nullptr; |
| 290 | } |
| 291 | int i; |
| 292 | if (fCount < MAX_ENTRIES) { |
| 293 | i = fCount++; |
| 294 | } else { |
| 295 | i = gRandom.nextULessThan(MAX_ENTRIES); |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 296 | fSize -= fEntries[i].fVertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 297 | } |
| 298 | fEntries[i].fFactory = factory; |
| 299 | fEntries[i].fVertices = vertices; |
| 300 | fEntries[i].fMatrix = matrix; |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 301 | fSize += vertices->approximateSize(); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 302 | return vertices; |
| 303 | } |
| 304 | |
| 305 | private: |
| 306 | struct Entry { |
| 307 | FACTORY fFactory; |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 308 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 309 | SkMatrix fMatrix; |
| 310 | }; |
| 311 | Entry fEntries[MAX_ENTRIES]; |
| 312 | int fCount = 0; |
| 313 | size_t fSize = 0; |
| 314 | }; |
| 315 | |
| 316 | Set<AmbientVerticesFactory, 4> fAmbientSet; |
| 317 | Set<SpotVerticesFactory, 4> fSpotSet; |
| 318 | |
| 319 | static SkRandom gRandom; |
| 320 | }; |
| 321 | |
| 322 | SkRandom CachedTessellations::gRandom; |
| 323 | |
| 324 | /** |
| 325 | * A record of shadow vertices stored in SkResourceCache of CachedTessellations for a particular |
| 326 | * path. The key represents the path's geometry and not any shadow params. |
| 327 | */ |
| 328 | class CachedTessellationsRec : public SkResourceCache::Rec { |
| 329 | public: |
| 330 | CachedTessellationsRec(const SkResourceCache::Key& key, |
| 331 | sk_sp<CachedTessellations> tessellations) |
| 332 | : fTessellations(std::move(tessellations)) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 333 | fKey.reset(new uint8_t[key.size()]); |
| 334 | memcpy(fKey.get(), &key, key.size()); |
| 335 | } |
| 336 | |
| 337 | const Key& getKey() const override { |
| 338 | return *reinterpret_cast<SkResourceCache::Key*>(fKey.get()); |
| 339 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 340 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 341 | size_t bytesUsed() const override { return fTessellations->size(); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 342 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 343 | const char* getCategory() const override { return "tessellated shadow masks"; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 344 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 345 | sk_sp<CachedTessellations> refTessellations() const { return fTessellations; } |
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 | template <typename FACTORY> |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 348 | sk_sp<SkVertices> find(const FACTORY& factory, const SkMatrix& matrix, |
| 349 | SkVector* translate) const { |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 350 | return fTessellations->find(factory, matrix, translate); |
| 351 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 352 | |
| 353 | private: |
| 354 | std::unique_ptr<uint8_t[]> fKey; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 355 | sk_sp<CachedTessellations> fTessellations; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | /** |
| 359 | * 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] | 360 | * vertices and a translation vector. If the CachedTessellations does not contain a suitable |
| 361 | * mesh then we inform SkResourceCache to destroy the Rec and we return the CachedTessellations |
| 362 | * 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] | 363 | */ |
| 364 | template <typename FACTORY> |
| 365 | struct FindContext { |
| 366 | FindContext(const SkMatrix* viewMatrix, const FACTORY* factory) |
| 367 | : fViewMatrix(viewMatrix), fFactory(factory) {} |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 368 | const SkMatrix* const fViewMatrix; |
| 369 | // If this is valid after Find is called then we found the vertices and they should be drawn |
| 370 | // with fTranslate applied. |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 371 | sk_sp<SkVertices> fVertices; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 372 | SkVector fTranslate = {0, 0}; |
| 373 | |
| 374 | // If this is valid after Find then the caller should add the vertices to the tessellation set |
| 375 | // and create a new CachedTessellationsRec and insert it into SkResourceCache. |
| 376 | sk_sp<CachedTessellations> fTessellationsOnFailure; |
| 377 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 378 | const FACTORY* fFactory; |
| 379 | }; |
| 380 | |
| 381 | /** |
| 382 | * Function called by SkResourceCache when a matching cache key is found. The FACTORY and matrix of |
| 383 | * the FindContext are used to determine if the vertices are reusable. If so the vertices and |
| 384 | * necessary translation vector are set on the FindContext. |
| 385 | */ |
| 386 | template <typename FACTORY> |
| 387 | bool FindVisitor(const SkResourceCache::Rec& baseRec, void* ctx) { |
| 388 | FindContext<FACTORY>* findContext = (FindContext<FACTORY>*)ctx; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 389 | const CachedTessellationsRec& rec = static_cast<const CachedTessellationsRec&>(baseRec); |
| 390 | findContext->fVertices = |
| 391 | rec.find(*findContext->fFactory, *findContext->fViewMatrix, &findContext->fTranslate); |
| 392 | if (findContext->fVertices) { |
| 393 | return true; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 394 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 395 | // We ref the tessellations and let the cache destroy the Rec. Once the tessellations have been |
| 396 | // manipulated we will add a new Rec. |
| 397 | findContext->fTessellationsOnFailure = rec.refTessellations(); |
| 398 | return false; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | class ShadowedPath { |
| 402 | public: |
| 403 | ShadowedPath(const SkPath* path, const SkMatrix* viewMatrix) |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 404 | : fPath(path) |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 405 | , fViewMatrix(viewMatrix) |
| 406 | #if SK_SUPPORT_GPU |
| 407 | , fShapeForKey(*path, GrStyle::SimpleFill()) |
| 408 | #endif |
| 409 | {} |
| 410 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 411 | const SkPath& path() const { return *fPath; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 412 | const SkMatrix& viewMatrix() const { return *fViewMatrix; } |
| 413 | #if SK_SUPPORT_GPU |
| 414 | /** Negative means the vertices should not be cached for this path. */ |
| 415 | int keyBytes() const { return fShapeForKey.unstyledKeySize() * sizeof(uint32_t); } |
| 416 | void writeKey(void* key) const { |
| 417 | fShapeForKey.writeUnstyledKey(reinterpret_cast<uint32_t*>(key)); |
| 418 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 419 | bool isRRect(SkRRect* rrect) { return fShapeForKey.asRRect(rrect, nullptr, nullptr, nullptr); } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 420 | #else |
| 421 | int keyBytes() const { return -1; } |
| 422 | void writeKey(void* key) const { SkFAIL("Should never be called"); } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 423 | bool isRRect(SkRRect* rrect) { return false; } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 424 | #endif |
| 425 | |
| 426 | private: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 427 | const SkPath* fPath; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 428 | const SkMatrix* fViewMatrix; |
| 429 | #if SK_SUPPORT_GPU |
| 430 | GrShape fShapeForKey; |
| 431 | #endif |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 432 | }; |
| 433 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 434 | // This creates a domain of keys in SkResourceCache used by this file. |
| 435 | static void* kNamespace; |
| 436 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 437 | /** |
| 438 | * Draws a shadow to 'canvas'. The vertices used to draw the shadow are created by 'factory' unless |
| 439 | * they are first found in SkResourceCache. |
| 440 | */ |
| 441 | template <typename FACTORY> |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 442 | void draw_shadow(const FACTORY& factory, SkCanvas* canvas, ShadowedPath& path, SkColor color, |
| 443 | SkResourceCache* cache) { |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 444 | FindContext<FACTORY> context(&path.viewMatrix(), &factory); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 445 | |
| 446 | SkResourceCache::Key* key = nullptr; |
| 447 | SkAutoSTArray<32 * 4, uint8_t> keyStorage; |
| 448 | int keyDataBytes = path.keyBytes(); |
| 449 | if (keyDataBytes >= 0) { |
| 450 | keyStorage.reset(keyDataBytes + sizeof(SkResourceCache::Key)); |
| 451 | key = new (keyStorage.begin()) SkResourceCache::Key(); |
| 452 | path.writeKey((uint32_t*)(keyStorage.begin() + sizeof(*key))); |
Brian Salomon | bc9956d | 2017-02-22 13:49:09 -0500 | [diff] [blame] | 453 | key->init(&kNamespace, resource_cache_shared_id(), keyDataBytes); |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 454 | if (cache) { |
| 455 | cache->find(*key, FindVisitor<FACTORY>, &context); |
| 456 | } else { |
| 457 | SkResourceCache::Find(*key, FindVisitor<FACTORY>, &context); |
| 458 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 459 | } |
| 460 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 461 | sk_sp<SkVertices> vertices; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 462 | const SkVector* translate; |
| 463 | static constexpr SkVector kZeroTranslate = {0, 0}; |
| 464 | bool foundInCache = SkToBool(context.fVertices); |
| 465 | if (foundInCache) { |
| 466 | vertices = std::move(context.fVertices); |
| 467 | translate = &context.fTranslate; |
| 468 | } else { |
| 469 | // TODO: handle transforming the path as part of the tessellator |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 470 | if (key) { |
| 471 | // Update or initialize a tessellation set and add it to the cache. |
| 472 | sk_sp<CachedTessellations> tessellations; |
| 473 | if (context.fTessellationsOnFailure) { |
| 474 | tessellations = std::move(context.fTessellationsOnFailure); |
| 475 | } else { |
| 476 | tessellations.reset(new CachedTessellations()); |
| 477 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 478 | vertices = tessellations->add(path.path(), factory, path.viewMatrix()); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 479 | if (!vertices) { |
| 480 | return; |
| 481 | } |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 482 | auto rec = new CachedTessellationsRec(*key, std::move(tessellations)); |
| 483 | if (cache) { |
| 484 | cache->add(rec); |
| 485 | } else { |
| 486 | SkResourceCache::Add(rec); |
| 487 | } |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 488 | } else { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 489 | vertices = factory.makeVertices(path.path(), path.viewMatrix()); |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 490 | if (!vertices) { |
| 491 | return; |
| 492 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 493 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 494 | translate = &kZeroTranslate; |
| 495 | } |
| 496 | |
| 497 | SkPaint paint; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 498 | // Run the vertex color through a GaussianColorFilter and then modulate the grayscale result of |
| 499 | // that against our 'color' param. |
| 500 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
| 501 | SkColorFilter::MakeModeFilter(color, SkBlendMode::kModulate), |
| 502 | SkGaussianColorFilter::Make())); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 503 | if (translate->fX || translate->fY) { |
| 504 | canvas->save(); |
| 505 | canvas->translate(translate->fX, translate->fY); |
| 506 | } |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 507 | canvas->drawVertices(vertices, SkBlendMode::kModulate, paint); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 508 | if (translate->fX || translate->fY) { |
| 509 | canvas->restore(); |
| 510 | } |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 514 | static bool draw_analytic_shadows(SkCanvas* canvas, const SkPath& path, SkScalar occluderZ, |
| 515 | const SkPoint3& devLightPos, SkScalar lightRadius, |
| 516 | SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color, |
| 517 | uint32_t flags) { |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 518 | // only supported in GPU code |
| 519 | if (!canvas->getGrContext()) { |
| 520 | return false; |
| 521 | } |
| 522 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 523 | SkRect rect; |
| 524 | SkRRect rrect; |
| 525 | if (canvas->getTotalMatrix().isSimilarity()) { |
| 526 | if (path.isRect(&rect)) { |
| 527 | SkPaint newPaint; |
| 528 | newPaint.setColor(color); |
| 529 | if (ambientAlpha > 0) { |
| 530 | newPaint.setMaskFilter(SkAmbientShadowMaskFilter::Make(occluderZ, |
| 531 | ambientAlpha, flags)); |
| 532 | canvas->drawRect(rect, newPaint); |
| 533 | } |
| 534 | if (spotAlpha > 0) { |
| 535 | newPaint.setMaskFilter(SkSpotShadowMaskFilter::Make(occluderZ, devLightPos, |
| 536 | lightRadius, spotAlpha, |
| 537 | flags)); |
| 538 | canvas->drawRect(rect, newPaint); |
| 539 | } |
| 540 | return true; |
| 541 | } else if (path.isRRect(&rrect) && rrect.isSimpleCircular() && |
| 542 | rrect.radii(SkRRect::kUpperLeft_Corner).fX > SK_ScalarNearlyZero) { |
| 543 | SkPaint newPaint; |
| 544 | newPaint.setColor(color); |
| 545 | if (ambientAlpha > 0) { |
| 546 | newPaint.setMaskFilter(SkAmbientShadowMaskFilter::Make(occluderZ, |
| 547 | ambientAlpha, flags)); |
| 548 | canvas->drawRRect(rrect, newPaint); |
| 549 | } |
| 550 | if (spotAlpha > 0) { |
| 551 | newPaint.setMaskFilter(SkSpotShadowMaskFilter::Make(occluderZ, devLightPos, |
| 552 | lightRadius, spotAlpha, |
| 553 | flags)); |
| 554 | canvas->drawRRect(rrect, newPaint); |
| 555 | } |
| 556 | return true; |
| 557 | } else if (path.isOval(&rect) && SkScalarNearlyEqual(rect.width(), rect.height()) && |
| 558 | rect.width() > SK_ScalarNearlyZero) { |
| 559 | SkPaint newPaint; |
| 560 | newPaint.setColor(color); |
| 561 | if (ambientAlpha > 0) { |
| 562 | newPaint.setMaskFilter(SkAmbientShadowMaskFilter::Make(occluderZ, |
| 563 | ambientAlpha, flags)); |
| 564 | canvas->drawOval(rect, newPaint); |
| 565 | } |
| 566 | if (spotAlpha > 0) { |
| 567 | newPaint.setMaskFilter(SkSpotShadowMaskFilter::Make(occluderZ, devLightPos, |
| 568 | lightRadius, spotAlpha, |
| 569 | flags)); |
| 570 | canvas->drawOval(rect, newPaint); |
| 571 | } |
| 572 | return true; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | return false; |
| 577 | } |
| 578 | |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 579 | static SkColor compute_render_color(SkColor color, float alpha) { |
| 580 | return SkColorSetARGB(alpha*SkColorGetA(color), SkColorGetR(color), |
| 581 | SkColorGetG(color), SkColorGetB(color)); |
| 582 | } |
| 583 | |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 584 | // Draw an offset spot shadow and outlining ambient shadow for the given path. |
| 585 | void SkShadowUtils::DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight, |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 586 | const SkPoint3& devLightPos, SkScalar lightRadius, |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 587 | SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color, |
Brian Salomon | 804e091 | 2017-02-23 09:34:03 -0500 | [diff] [blame] | 588 | uint32_t flags, SkResourceCache* cache) { |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 589 | // try fast paths |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 590 | bool skipAnalytic = SkToBool(flags & SkShadowFlags::kGeometricOnly_ShadowFlag); |
| 591 | if (!skipAnalytic && draw_analytic_shadows(canvas, path, occluderHeight, devLightPos, |
| 592 | lightRadius, ambientAlpha, spotAlpha, color, |
| 593 | flags)) { |
Jim Van Verth | cf40e30 | 2017-03-02 11:28:43 -0500 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 597 | SkAutoCanvasRestore acr(canvas, true); |
| 598 | SkMatrix viewMatrix = canvas->getTotalMatrix(); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 599 | canvas->resetMatrix(); |
| 600 | |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 601 | ShadowedPath shadowedPath(&path, &viewMatrix); |
| 602 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 603 | bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
| 604 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 605 | if (ambientAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 606 | ambientAlpha = SkTMin(ambientAlpha, 1.f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 607 | AmbientVerticesFactory factory; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 608 | factory.fOccluderHeight = occluderHeight; |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 609 | factory.fTransparent = transparent; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 610 | |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 611 | SkColor renderColor = compute_render_color(color, ambientAlpha); |
| 612 | draw_shadow(factory, canvas, shadowedPath, renderColor, cache); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | if (spotAlpha > 0) { |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 616 | spotAlpha = SkTMin(spotAlpha, 1.f); |
Brian Salomon | 5e68952 | 2017-02-01 12:07:17 -0500 | [diff] [blame] | 617 | SpotVerticesFactory factory; |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 618 | float zRatio = SkTPin(occluderHeight / (devLightPos.fZ - occluderHeight), 0.0f, 0.95f); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 619 | SkScalar radius = lightRadius * zRatio; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 620 | |
| 621 | // Compute the scale and translation for the spot shadow. |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 622 | SkScalar scale = devLightPos.fZ / (devLightPos.fZ - occluderHeight); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 623 | |
| 624 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Brian Salomon | 0bd699e | 2017-02-01 12:23:25 -0500 | [diff] [blame] | 625 | viewMatrix.mapPoints(¢er, 1); |
| 626 | factory.fOffset = SkVector::Make(zRatio * (center.fX - devLightPos.fX), |
| 627 | zRatio * (center.fY - devLightPos.fY)); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 628 | factory.fOccluderHeight = occluderHeight; |
| 629 | factory.fDevLightPos = devLightPos; |
| 630 | factory.fLightRadius = lightRadius; |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 631 | |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 632 | SkRRect rrect; |
| 633 | if (transparent) { |
| 634 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 635 | } else { |
| 636 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kOpaque; |
| 637 | if (shadowedPath.isRRect(&rrect)) { |
| 638 | SkRRect devRRect; |
| 639 | if (rrect.transform(viewMatrix, &devRRect)) { |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 640 | SkScalar s = 1.f - scale; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 641 | SkScalar w = devRRect.width(); |
| 642 | SkScalar h = devRRect.height(); |
| 643 | SkScalar hw = w / 2.f; |
| 644 | SkScalar hh = h / 2.f; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 645 | SkScalar umbraInsetX = s * hw + radius; |
| 646 | SkScalar umbraInsetY = s * hh + radius; |
Brian Salomon | 67386d4 | 2017-02-06 14:23:53 -0500 | [diff] [blame] | 647 | // The umbra is inset by radius along the diagonal, so adjust for that. |
| 648 | SkScalar d = 1.f / SkScalarSqrt(hw * hw + hh * hh); |
| 649 | umbraInsetX *= hw * d; |
| 650 | umbraInsetY *= hh * d; |
Brian Salomon | d1ac982 | 2017-02-03 14:25:02 -0500 | [diff] [blame] | 651 | if (umbraInsetX > hw || umbraInsetY > hh) { |
| 652 | // There is no umbra to occlude. |
| 653 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 654 | } else if (fabsf(factory.fOffset.fX) < umbraInsetX && |
| 655 | fabsf(factory.fOffset.fY) < umbraInsetY) { |
| 656 | factory.fOccluderType = |
| 657 | SpotVerticesFactory::OccluderType::kOpaqueCoversUmbra; |
| 658 | } else if (factory.fOffset.fX > w - umbraInsetX || |
| 659 | factory.fOffset.fY > h - umbraInsetY) { |
| 660 | // There umbra is fully exposed, there is nothing to omit. |
| 661 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 666 | if (factory.fOccluderType == SpotVerticesFactory::OccluderType::kOpaque) { |
| 667 | factory.fOccluderType = SpotVerticesFactory::OccluderType::kTransparent; |
| 668 | } |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 669 | |
| 670 | SkColor renderColor = compute_render_color(color, spotAlpha); |
| 671 | draw_shadow(factory, canvas, shadowedPath, renderColor, cache); |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 672 | } |
Jim Van Verth | 43475ad | 2017-01-13 14:37:37 -0500 | [diff] [blame] | 673 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 674 | |
| 675 | // Draw an offset spot shadow and outlining ambient shadow for the given path, |
| 676 | // without caching and using a function based on local position to compute the height. |
| 677 | void SkShadowUtils::DrawUncachedShadow(SkCanvas* canvas, const SkPath& path, |
| 678 | std::function<SkScalar(SkScalar, SkScalar)> heightFunc, |
| 679 | const SkPoint3& lightPos, SkScalar lightRadius, |
| 680 | SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color, |
| 681 | uint32_t flags) { |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 682 | // try fast paths |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 683 | bool skipAnalytic = SkToBool(flags & SkShadowFlags::kGeometricOnly_ShadowFlag); |
| 684 | if (!skipAnalytic && draw_analytic_shadows(canvas, path, heightFunc(0, 0), lightPos, |
| 685 | lightRadius, ambientAlpha, spotAlpha, color, |
| 686 | flags)) { |
Jim Van Verth | 8f7dc9f | 2017-04-20 15:48:37 -0400 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 690 | SkAutoCanvasRestore acr(canvas, true); |
| 691 | SkMatrix viewMatrix = canvas->getTotalMatrix(); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 692 | canvas->resetMatrix(); |
| 693 | |
| 694 | bool transparent = SkToBool(flags & SkShadowFlags::kTransparentOccluder_ShadowFlag); |
| 695 | |
| 696 | if (ambientAlpha > 0) { |
| 697 | ambientAlpha = SkTMin(ambientAlpha, 1.f); |
| 698 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeAmbient(path, viewMatrix, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 699 | heightFunc, transparent); |
| 700 | SkColor renderColor = compute_render_color(color, ambientAlpha); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 701 | SkPaint paint; |
| 702 | // Run the vertex color through a GaussianColorFilter and then modulate the grayscale |
| 703 | // result of that against our 'color' param. |
| 704 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 705 | SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate), |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 706 | SkGaussianColorFilter::Make())); |
| 707 | canvas->drawVertices(vertices, SkBlendMode::kModulate, paint); |
| 708 | } |
| 709 | |
| 710 | if (spotAlpha > 0) { |
| 711 | spotAlpha = SkTMin(spotAlpha, 1.f); |
| 712 | sk_sp<SkVertices> vertices = SkShadowTessellator::MakeSpot(path, viewMatrix, heightFunc, |
| 713 | lightPos, lightRadius, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 714 | transparent); |
| 715 | SkColor renderColor = compute_render_color(color, spotAlpha); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 716 | SkPaint paint; |
| 717 | // Run the vertex color through a GaussianColorFilter and then modulate the grayscale |
| 718 | // result of that against our 'color' param. |
| 719 | paint.setColorFilter(SkColorFilter::MakeComposeFilter( |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 720 | SkColorFilter::MakeModeFilter(renderColor, SkBlendMode::kModulate), |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 721 | SkGaussianColorFilter::Make())); |
| 722 | canvas->drawVertices(vertices, SkBlendMode::kModulate, paint); |
| 723 | } |
| 724 | } |