Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -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 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 8 | #include "SkShadowTessellator.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 9 | #include "SkColorData.h" |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 10 | #include "SkDrawShadowInfo.h" |
Jim Van Verth | 58abc9e | 2017-01-25 11:05:01 -0500 | [diff] [blame] | 11 | #include "SkGeometry.h" |
Jim Van Verth | 41964ed | 2018-03-28 10:10:30 -0400 | [diff] [blame] | 12 | #include "SkOffsetPolygon.h" |
Jim Van Verth | 85dc96b | 2017-01-30 14:49:21 -0500 | [diff] [blame] | 13 | #include "SkPath.h" |
Mike Reed | 54518ac | 2017-07-22 08:29:48 -0400 | [diff] [blame] | 14 | #include "SkPoint3.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 15 | #include "SkPointPriv.h" |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 16 | #include "SkVertices.h" |
Jim Van Verth | 85dc96b | 2017-01-30 14:49:21 -0500 | [diff] [blame] | 17 | |
| 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrPathUtils.h" |
| 20 | #endif |
Jim Van Verth | 58abc9e | 2017-01-25 11:05:01 -0500 | [diff] [blame] | 21 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 22 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 23 | /** |
| 24 | * Base class |
| 25 | */ |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 26 | class SkBaseShadowTessellator { |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 27 | public: |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 28 | SkBaseShadowTessellator(const SkPoint3& zPlaneParams, bool transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 29 | virtual ~SkBaseShadowTessellator() {} |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 30 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 31 | sk_sp<SkVertices> releaseVertices() { |
| 32 | if (!fSucceeded) { |
| 33 | return nullptr; |
| 34 | } |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 35 | return SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, this->vertexCount(), |
Mike Reed | 97eb4fe | 2017-03-14 12:04:16 -0400 | [diff] [blame] | 36 | fPositions.begin(), nullptr, fColors.begin(), |
| 37 | this->indexCount(), fIndices.begin()); |
Brian Salomon | 1a8b79a | 2017-01-31 11:26:26 -0500 | [diff] [blame] | 38 | } |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 39 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 40 | protected: |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 41 | static constexpr auto kMinHeight = 0.1f; |
| 42 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 43 | int vertexCount() const { return fPositions.count(); } |
| 44 | int indexCount() const { return fIndices.count(); } |
| 45 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 46 | bool setZOffset(const SkRect& bounds, bool perspective); |
| 47 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 48 | virtual void handleLine(const SkPoint& p) = 0; |
| 49 | void handleLine(const SkMatrix& m, SkPoint* p); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 50 | |
| 51 | void handleQuad(const SkPoint pts[3]); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 52 | void handleQuad(const SkMatrix& m, SkPoint pts[3]); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 53 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 54 | void handleCubic(const SkMatrix& m, SkPoint pts[4]); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 55 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 56 | void handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 57 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 58 | bool setTransformedHeightFunc(const SkMatrix& ctm); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 59 | |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 60 | bool addArc(const SkVector& nextNormal, bool finishArc); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 61 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 62 | void appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2); |
| 63 | void appendQuad(uint16_t index0, uint16_t index1, uint16_t index2, uint16_t index3); |
| 64 | |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 65 | SkScalar heightFunc(SkScalar x, SkScalar y) { |
| 66 | return fZPlaneParams.fX*x + fZPlaneParams.fY*y + fZPlaneParams.fZ; |
| 67 | } |
| 68 | |
| 69 | SkPoint3 fZPlaneParams; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 70 | std::function<SkScalar(const SkPoint&)> fTransformedHeightFunc; |
| 71 | SkScalar fZOffset; |
| 72 | // members for perspective height function |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 73 | SkPoint3 fTransformedZParams; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 74 | SkScalar fPartialDeterminants[3]; |
| 75 | |
| 76 | // first two points |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 77 | SkTDArray<SkPoint> fInitPoints; |
| 78 | // temporary buffer |
| 79 | SkTDArray<SkPoint> fPointBuffer; |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 80 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 81 | SkTDArray<SkPoint> fPositions; |
| 82 | SkTDArray<SkColor> fColors; |
| 83 | SkTDArray<uint16_t> fIndices; |
| 84 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 85 | int fFirstVertexIndex; |
| 86 | SkVector fFirstOutset; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 87 | SkPoint fFirstPoint; |
| 88 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 89 | bool fSucceeded; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 90 | bool fTransparent; |
| 91 | |
| 92 | SkColor fUmbraColor; |
| 93 | SkColor fPenumbraColor; |
| 94 | |
| 95 | SkScalar fRadius; |
| 96 | SkScalar fDirection; |
| 97 | int fPrevUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 98 | SkVector fPrevOutset; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 99 | SkPoint fPrevPoint; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 100 | }; |
| 101 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 102 | static bool compute_normal(const SkPoint& p0, const SkPoint& p1, SkScalar dir, |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 103 | SkVector* newNormal) { |
| 104 | SkVector normal; |
| 105 | // compute perpendicular |
| 106 | normal.fX = p0.fY - p1.fY; |
| 107 | normal.fY = p1.fX - p0.fX; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 108 | normal *= dir; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 109 | if (!normal.normalize()) { |
| 110 | return false; |
| 111 | } |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 112 | *newNormal = normal; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | static void compute_radial_steps(const SkVector& v1, const SkVector& v2, SkScalar r, |
| 117 | SkScalar* rotSin, SkScalar* rotCos, int* n) { |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 118 | const SkScalar kRecipPixelsPerArcSegment = 0.125f; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 119 | |
| 120 | SkScalar rCos = v1.dot(v2); |
| 121 | SkScalar rSin = v1.cross(v2); |
| 122 | SkScalar theta = SkScalarATan2(rSin, rCos); |
| 123 | |
Jim Van Verth | 4c8c1e8 | 2018-04-23 17:14:48 -0400 | [diff] [blame] | 124 | int steps = SkScalarRoundToInt(SkScalarAbs(r*theta*kRecipPixelsPerArcSegment)); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 125 | |
| 126 | SkScalar dTheta = theta / steps; |
| 127 | *rotSin = SkScalarSinCos(dTheta, rotCos); |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 128 | *n = steps; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 131 | SkBaseShadowTessellator::SkBaseShadowTessellator(const SkPoint3& zPlaneParams, bool transparent) |
| 132 | : fZPlaneParams(zPlaneParams) |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 133 | , fZOffset(0) |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 134 | , fFirstVertexIndex(-1) |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 135 | , fSucceeded(false) |
| 136 | , fTransparent(transparent) |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 137 | , fDirection(1) |
| 138 | , fPrevUmbraIndex(-1) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 139 | fInitPoints.setReserve(3); |
| 140 | |
| 141 | // child classes will set reserve for positions, colors and indices |
| 142 | } |
| 143 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 144 | bool SkBaseShadowTessellator::setZOffset(const SkRect& bounds, bool perspective) { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 145 | SkScalar minZ = this->heightFunc(bounds.fLeft, bounds.fTop); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 146 | if (perspective) { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 147 | SkScalar z = this->heightFunc(bounds.fLeft, bounds.fBottom); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 148 | if (z < minZ) { |
| 149 | minZ = z; |
| 150 | } |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 151 | z = this->heightFunc(bounds.fRight, bounds.fTop); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 152 | if (z < minZ) { |
| 153 | minZ = z; |
| 154 | } |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 155 | z = this->heightFunc(bounds.fRight, bounds.fBottom); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 156 | if (z < minZ) { |
| 157 | minZ = z; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (minZ < kMinHeight) { |
| 162 | fZOffset = -minZ + kMinHeight; |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 169 | // tesselation tolerance values, in device space pixels |
Mike Klein | b8b51e6 | 2017-02-09 15:22:53 -0500 | [diff] [blame] | 170 | #if SK_SUPPORT_GPU |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 171 | static const SkScalar kQuadTolerance = 0.2f; |
| 172 | static const SkScalar kCubicTolerance = 0.2f; |
Mike Klein | b8b51e6 | 2017-02-09 15:22:53 -0500 | [diff] [blame] | 173 | #endif |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 174 | static const SkScalar kConicTolerance = 0.5f; |
| 175 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 176 | void SkBaseShadowTessellator::handleLine(const SkMatrix& m, SkPoint* p) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 177 | m.mapPoints(p, 1); |
| 178 | this->handleLine(*p); |
| 179 | } |
| 180 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 181 | void SkBaseShadowTessellator::handleQuad(const SkPoint pts[3]) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 182 | #if SK_SUPPORT_GPU |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 183 | // check for degeneracy |
| 184 | SkVector v0 = pts[1] - pts[0]; |
| 185 | SkVector v1 = pts[2] - pts[0]; |
| 186 | if (SkScalarNearlyZero(v0.cross(v1))) { |
| 187 | return; |
| 188 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 189 | // TODO: Pull PathUtils out of Ganesh? |
| 190 | int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance); |
Mike Klein | cc9856c | 2018-04-19 09:18:33 -0400 | [diff] [blame] | 191 | fPointBuffer.setCount(maxCount); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 192 | SkPoint* target = fPointBuffer.begin(); |
| 193 | int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2], |
| 194 | kQuadTolerance, &target, maxCount); |
| 195 | fPointBuffer.setCount(count); |
| 196 | for (int i = 0; i < count; i++) { |
| 197 | this->handleLine(fPointBuffer[i]); |
| 198 | } |
| 199 | #else |
| 200 | // for now, just to draw something |
| 201 | this->handleLine(pts[1]); |
| 202 | this->handleLine(pts[2]); |
| 203 | #endif |
| 204 | } |
| 205 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 206 | void SkBaseShadowTessellator::handleQuad(const SkMatrix& m, SkPoint pts[3]) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 207 | m.mapPoints(pts, 3); |
| 208 | this->handleQuad(pts); |
| 209 | } |
| 210 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 211 | void SkBaseShadowTessellator::handleCubic(const SkMatrix& m, SkPoint pts[4]) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 212 | m.mapPoints(pts, 4); |
| 213 | #if SK_SUPPORT_GPU |
| 214 | // TODO: Pull PathUtils out of Ganesh? |
| 215 | int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance); |
Mike Klein | cc9856c | 2018-04-19 09:18:33 -0400 | [diff] [blame] | 216 | fPointBuffer.setCount(maxCount); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 217 | SkPoint* target = fPointBuffer.begin(); |
| 218 | int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3], |
| 219 | kCubicTolerance, &target, maxCount); |
| 220 | fPointBuffer.setCount(count); |
| 221 | for (int i = 0; i < count; i++) { |
| 222 | this->handleLine(fPointBuffer[i]); |
| 223 | } |
| 224 | #else |
| 225 | // for now, just to draw something |
| 226 | this->handleLine(pts[1]); |
| 227 | this->handleLine(pts[2]); |
| 228 | this->handleLine(pts[3]); |
| 229 | #endif |
| 230 | } |
| 231 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 232 | void SkBaseShadowTessellator::handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 233 | if (m.hasPerspective()) { |
| 234 | w = SkConic::TransformW(pts, w, m); |
| 235 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 236 | m.mapPoints(pts, 3); |
| 237 | SkAutoConicToQuads quadder; |
| 238 | const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance); |
| 239 | SkPoint lastPoint = *(quads++); |
| 240 | int count = quadder.countQuads(); |
| 241 | for (int i = 0; i < count; ++i) { |
| 242 | SkPoint quadPts[3]; |
| 243 | quadPts[0] = lastPoint; |
| 244 | quadPts[1] = quads[0]; |
| 245 | quadPts[2] = i == count - 1 ? pts[2] : quads[1]; |
| 246 | this->handleQuad(quadPts); |
| 247 | lastPoint = quadPts[2]; |
| 248 | quads += 2; |
| 249 | } |
| 250 | } |
| 251 | |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 252 | bool SkBaseShadowTessellator::addArc(const SkVector& nextNormal, bool finishArc) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 253 | // fill in fan from previous quad |
| 254 | SkScalar rotSin, rotCos; |
| 255 | int numSteps; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 256 | compute_radial_steps(fPrevOutset, nextNormal, fRadius, &rotSin, &rotCos, &numSteps); |
| 257 | SkVector prevNormal = fPrevOutset; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 258 | for (int i = 0; i < numSteps-1; ++i) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 259 | SkVector currNormal; |
| 260 | currNormal.fX = prevNormal.fX*rotCos - prevNormal.fY*rotSin; |
| 261 | currNormal.fY = prevNormal.fY*rotCos + prevNormal.fX*rotSin; |
| 262 | *fPositions.push() = fPrevPoint + currNormal; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 263 | *fColors.push() = fPenumbraColor; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 264 | this->appendTriangle(fPrevUmbraIndex, fPositions.count() - 1, fPositions.count() - 2); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 265 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 266 | prevNormal = currNormal; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 267 | } |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 268 | if (finishArc && numSteps) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 269 | *fPositions.push() = fPrevPoint + nextNormal; |
| 270 | *fColors.push() = fPenumbraColor; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 271 | this->appendTriangle(fPrevUmbraIndex, fPositions.count() - 1, fPositions.count() - 2); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 272 | } |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 273 | fPrevOutset = nextNormal; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 274 | |
| 275 | return (numSteps > 0); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 278 | void SkBaseShadowTessellator::appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2) { |
| 279 | auto indices = fIndices.append(3); |
| 280 | |
| 281 | indices[0] = index0; |
| 282 | indices[1] = index1; |
| 283 | indices[2] = index2; |
| 284 | } |
| 285 | |
| 286 | void SkBaseShadowTessellator::appendQuad(uint16_t index0, uint16_t index1, |
| 287 | uint16_t index2, uint16_t index3) { |
| 288 | auto indices = fIndices.append(6); |
| 289 | |
| 290 | indices[0] = index0; |
| 291 | indices[1] = index1; |
| 292 | indices[2] = index2; |
| 293 | |
| 294 | indices[3] = index2; |
| 295 | indices[4] = index1; |
| 296 | indices[5] = index3; |
| 297 | } |
| 298 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 299 | bool SkBaseShadowTessellator::setTransformedHeightFunc(const SkMatrix& ctm) { |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 300 | if (SkScalarNearlyZero(fZPlaneParams.fX) && SkScalarNearlyZero(fZPlaneParams.fY)) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 301 | fTransformedHeightFunc = [this](const SkPoint& p) { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 302 | return fZPlaneParams.fZ; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 303 | }; |
| 304 | } else { |
| 305 | SkMatrix ctmInverse; |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 306 | if (!ctm.invert(&ctmInverse) || !ctmInverse.isFinite()) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 307 | return false; |
| 308 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 309 | // multiply by transpose |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 310 | fTransformedZParams = SkPoint3::Make( |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 311 | ctmInverse[SkMatrix::kMScaleX] * fZPlaneParams.fX + |
| 312 | ctmInverse[SkMatrix::kMSkewY] * fZPlaneParams.fY + |
| 313 | ctmInverse[SkMatrix::kMPersp0] * fZPlaneParams.fZ, |
| 314 | |
| 315 | ctmInverse[SkMatrix::kMSkewX] * fZPlaneParams.fX + |
| 316 | ctmInverse[SkMatrix::kMScaleY] * fZPlaneParams.fY + |
| 317 | ctmInverse[SkMatrix::kMPersp1] * fZPlaneParams.fZ, |
| 318 | |
| 319 | ctmInverse[SkMatrix::kMTransX] * fZPlaneParams.fX + |
| 320 | ctmInverse[SkMatrix::kMTransY] * fZPlaneParams.fY + |
| 321 | ctmInverse[SkMatrix::kMPersp2] * fZPlaneParams.fZ |
| 322 | ); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 323 | |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 324 | if (ctm.hasPerspective()) { |
| 325 | // We use Cramer's rule to solve for the W value for a given post-divide X and Y, |
| 326 | // so pre-compute those values that are independent of X and Y. |
| 327 | // W is det(ctmInverse)/(PD[0]*X + PD[1]*Y + PD[2]) |
| 328 | fPartialDeterminants[0] = ctm[SkMatrix::kMSkewY] * ctm[SkMatrix::kMPersp1] - |
| 329 | ctm[SkMatrix::kMScaleY] * ctm[SkMatrix::kMPersp0]; |
| 330 | fPartialDeterminants[1] = ctm[SkMatrix::kMPersp0] * ctm[SkMatrix::kMSkewX] - |
| 331 | ctm[SkMatrix::kMPersp1] * ctm[SkMatrix::kMScaleX]; |
| 332 | fPartialDeterminants[2] = ctm[SkMatrix::kMScaleX] * ctm[SkMatrix::kMScaleY] - |
| 333 | ctm[SkMatrix::kMSkewX] * ctm[SkMatrix::kMSkewY]; |
| 334 | SkScalar ctmDeterminant = ctm[SkMatrix::kMTransX] * fPartialDeterminants[0] + |
| 335 | ctm[SkMatrix::kMTransY] * fPartialDeterminants[1] + |
| 336 | ctm[SkMatrix::kMPersp2] * fPartialDeterminants[2]; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 337 | |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 338 | // Pre-bake the numerator of Cramer's rule into the zParams to avoid another multiply. |
| 339 | // TODO: this may introduce numerical instability, but I haven't seen any issues yet. |
| 340 | fTransformedZParams.fX *= ctmDeterminant; |
| 341 | fTransformedZParams.fY *= ctmDeterminant; |
| 342 | fTransformedZParams.fZ *= ctmDeterminant; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 343 | |
Jim Van Verth | 4c9b893 | 2017-05-15 13:49:21 -0400 | [diff] [blame] | 344 | fTransformedHeightFunc = [this](const SkPoint& p) { |
| 345 | SkScalar denom = p.fX * fPartialDeterminants[0] + |
| 346 | p.fY * fPartialDeterminants[1] + |
| 347 | fPartialDeterminants[2]; |
| 348 | SkScalar w = SkScalarFastInvert(denom); |
| 349 | return fZOffset + w*(fTransformedZParams.fX * p.fX + |
| 350 | fTransformedZParams.fY * p.fY + |
| 351 | fTransformedZParams.fZ); |
| 352 | }; |
| 353 | } else { |
| 354 | fTransformedHeightFunc = [this](const SkPoint& p) { |
| 355 | return fZOffset + fTransformedZParams.fX * p.fX + |
| 356 | fTransformedZParams.fY * p.fY + fTransformedZParams.fZ; |
| 357 | }; |
| 358 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | return true; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | |
| 365 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 366 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 367 | class SkAmbientShadowTessellator : public SkBaseShadowTessellator { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 368 | public: |
| 369 | SkAmbientShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 370 | const SkPoint3& zPlaneParams, bool transparent); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 371 | |
| 372 | private: |
| 373 | void handleLine(const SkPoint& p) override; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 374 | void addEdge(const SkVector& nextPoint, const SkVector& nextNormal); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 375 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 376 | static constexpr auto kMaxEdgeLenSqr = 20 * 20; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 377 | static constexpr auto kInsetFactor = -0.5f; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 378 | |
| 379 | SkScalar offset(SkScalar z) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 380 | return SkDrawShadowMetrics::AmbientBlurRadius(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 381 | } |
| 382 | SkColor umbraColor(SkScalar z) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 383 | SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(z)); |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 384 | return SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 387 | int fCentroidCount; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 388 | bool fSplitFirstEdge; |
| 389 | bool fSplitPreviousEdge; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 390 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 391 | typedef SkBaseShadowTessellator INHERITED; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 392 | }; |
| 393 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 394 | SkAmbientShadowTessellator::SkAmbientShadowTessellator(const SkPath& path, |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 395 | const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 396 | const SkPoint3& zPlaneParams, |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 397 | bool transparent) |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 398 | : INHERITED(zPlaneParams, transparent) |
| 399 | , fSplitFirstEdge(false) |
| 400 | , fSplitPreviousEdge(false) { |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 401 | // TODO: support some concave paths |
Mike Reed | 9d5c674 | 2018-03-06 10:31:27 -0500 | [diff] [blame] | 402 | SkASSERT(path.isConvex()); |
Jim Van Verth | 2252636 | 2018-02-28 14:51:19 -0500 | [diff] [blame] | 403 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 404 | // Set base colors |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 405 | SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(heightFunc(0, 0))); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 406 | // umbraColor is the interior value, penumbraColor the exterior value. |
| 407 | // umbraAlpha is the factor that is linearly interpolated from outside to inside, and |
| 408 | // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get |
| 409 | // the final alpha. |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 410 | fUmbraColor = SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0); |
| 411 | fPenumbraColor = SkColorSetARGB(0, 0, 0, 0); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 412 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 413 | // make sure we're not below the canvas plane |
| 414 | this->setZOffset(path.getBounds(), ctm.hasPerspective()); |
| 415 | |
Jim Van Verth | 7c8008c | 2018-02-07 15:02:50 -0500 | [diff] [blame] | 416 | if (!this->setTransformedHeightFunc(ctm)) { |
| 417 | return; |
| 418 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 419 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 420 | // Outer ring: 3*numPts |
| 421 | // Middle ring: numPts |
| 422 | fPositions.setReserve(4 * path.countPoints()); |
| 423 | fColors.setReserve(4 * path.countPoints()); |
| 424 | // Outer ring: 12*numPts |
| 425 | // Middle ring: 0 |
| 426 | fIndices.setReserve(12 * path.countPoints()); |
| 427 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 428 | // walk around the path, tessellate and generate outer ring |
| 429 | // if original path is transparent, will accumulate sum of points for centroid |
| 430 | SkPath::Iter iter(path, true); |
| 431 | SkPoint pts[4]; |
| 432 | SkPath::Verb verb; |
| 433 | if (fTransparent) { |
| 434 | *fPositions.push() = SkPoint::Make(0, 0); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 435 | *fColors.push() = fUmbraColor; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 436 | fCentroidCount = 0; |
| 437 | } |
| 438 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 439 | switch (verb) { |
| 440 | case SkPath::kLine_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 441 | this->INHERITED::handleLine(ctm, &pts[1]); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 442 | break; |
| 443 | case SkPath::kQuad_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 444 | this->handleQuad(ctm, pts); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 445 | break; |
| 446 | case SkPath::kCubic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 447 | this->handleCubic(ctm, pts); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 448 | break; |
| 449 | case SkPath::kConic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 450 | this->handleConic(ctm, pts, iter.conicWeight()); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 451 | break; |
| 452 | case SkPath::kMove_Verb: |
| 453 | case SkPath::kClose_Verb: |
| 454 | case SkPath::kDone_Verb: |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 459 | if (!this->indexCount()) { |
| 460 | return; |
| 461 | } |
| 462 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 463 | // Finish up |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 464 | SkVector normal; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 465 | if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) { |
| 466 | SkScalar z = fTransformedHeightFunc(fPrevPoint); |
| 467 | fRadius = this->offset(z); |
| 468 | SkVector scaledNormal(normal); |
| 469 | scaledNormal *= fRadius; |
| 470 | this->addArc(scaledNormal, true); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 471 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 472 | // fix-up the last and first umbra points |
| 473 | SkVector inset = normal; |
| 474 | // adding to an average, so multiply by an additional half |
| 475 | inset *= 0.5f*kInsetFactor; |
| 476 | fPositions[fPrevUmbraIndex] += inset; |
| 477 | fPositions[fFirstVertexIndex] += inset; |
| 478 | // we multiply by another half because now we're adding to an average of an average |
| 479 | inset *= 0.5f; |
| 480 | if (fSplitPreviousEdge) { |
| 481 | fPositions[fPrevUmbraIndex - 2] += inset; |
| 482 | } |
| 483 | if (fSplitFirstEdge) { |
| 484 | fPositions[fFirstVertexIndex + 2] += inset; |
| 485 | } |
| 486 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 487 | // set up for final edge |
| 488 | z = fTransformedHeightFunc(fFirstPoint); |
| 489 | normal *= this->offset(z); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 490 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 491 | // make sure we don't end up with a sharp alpha edge along the quad diagonal |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 492 | if (fColors[fPrevUmbraIndex] != fColors[fFirstVertexIndex] && |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 493 | SkPointPriv::DistanceToSqd(fFirstPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 494 | SkPoint centerPoint = fPositions[fPrevUmbraIndex] + fPositions[fFirstVertexIndex]; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 495 | centerPoint *= 0.5f; |
| 496 | *fPositions.push() = centerPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 497 | *fColors.push() = SkPMLerp(fColors[fFirstVertexIndex], fColors[fPrevUmbraIndex], 128); |
| 498 | centerPoint = fPositions[fPositions.count()-2] + fPositions[fFirstVertexIndex+1]; |
| 499 | centerPoint *= 0.5f; |
| 500 | *fPositions.push() = centerPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 501 | *fColors.push() = fPenumbraColor; |
| 502 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 503 | if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 504 | this->appendQuad(fPrevUmbraIndex, fPositions.count() - 3, |
| 505 | fPositions.count() - 2, fPositions.count() - 1); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 506 | } else { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 507 | this->appendQuad(fPositions.count() - 2, fPositions.count() - 1, |
| 508 | fPrevUmbraIndex, fPositions.count() - 3); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 509 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 510 | |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 511 | // if transparent, add point to first one in array and add to center fan |
| 512 | if (fTransparent) { |
| 513 | fPositions[0] += centerPoint; |
| 514 | ++fCentroidCount; |
| 515 | |
| 516 | *fIndices.push() = 0; |
| 517 | *fIndices.push() = fPrevUmbraIndex; |
| 518 | *fIndices.push() = fPositions.count() - 2; |
| 519 | } |
| 520 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 521 | fPrevUmbraIndex = fPositions.count() - 2; |
| 522 | } |
| 523 | |
| 524 | // final edge |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 525 | *fPositions.push() = fFirstPoint + normal; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 526 | *fColors.push() = fPenumbraColor; |
| 527 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 528 | if (fColors[fPrevUmbraIndex] > fColors[fFirstVertexIndex]) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 529 | this->appendQuad(fPrevUmbraIndex, fPositions.count() - 2, |
| 530 | fFirstVertexIndex, fPositions.count() - 1); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 531 | } else { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 532 | this->appendQuad(fPositions.count() - 2, fPositions.count() - 1, |
| 533 | fPrevUmbraIndex, fFirstVertexIndex); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 534 | } |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 535 | fPrevOutset = normal; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | // finalize centroid |
| 539 | if (fTransparent) { |
| 540 | fPositions[0] *= SkScalarFastInvert(fCentroidCount); |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 541 | fColors[0] = this->umbraColor(fTransformedHeightFunc(fPositions[0])); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 542 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 543 | this->appendTriangle(0, fPrevUmbraIndex, fFirstVertexIndex); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // final fan |
| 547 | if (fPositions.count() >= 3) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 548 | fPrevUmbraIndex = fFirstVertexIndex; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 549 | fPrevPoint = fFirstPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 550 | fRadius = this->offset(fTransformedHeightFunc(fPrevPoint)); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 551 | if (this->addArc(fFirstOutset, false)) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 552 | this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1, fFirstVertexIndex + 1); |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 553 | } else { |
| 554 | // arc is too small, set the first penumbra point to be the same position |
| 555 | // as the last one |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 556 | fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1]; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 557 | } |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 558 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 559 | fSucceeded = true; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 562 | void SkAmbientShadowTessellator::handleLine(const SkPoint& p) { |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 563 | if (fInitPoints.count() < 2) { |
| 564 | *fInitPoints.push() = p; |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | if (fInitPoints.count() == 2) { |
| 569 | // determine if cw or ccw |
| 570 | SkVector v0 = fInitPoints[1] - fInitPoints[0]; |
| 571 | SkVector v1 = p - fInitPoints[0]; |
| 572 | SkScalar perpDot = v0.fX*v1.fY - v0.fY*v1.fX; |
| 573 | if (SkScalarNearlyZero(perpDot)) { |
| 574 | // nearly parallel, just treat as straight line and continue |
| 575 | fInitPoints[1] = p; |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | // if perpDot > 0, winding is ccw |
| 580 | fDirection = (perpDot > 0) ? -1 : 1; |
| 581 | |
| 582 | // add first quad |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 583 | SkVector normal; |
| 584 | if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &normal)) { |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 585 | // first two points are incident, make the third point the second and continue |
| 586 | fInitPoints[1] = p; |
| 587 | return; |
| 588 | } |
| 589 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 590 | fFirstPoint = fInitPoints[0]; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 591 | fFirstVertexIndex = fPositions.count(); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 592 | SkScalar z = fTransformedHeightFunc(fFirstPoint); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 593 | fFirstOutset = normal; |
| 594 | fFirstOutset *= this->offset(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 595 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 596 | fPrevOutset = fFirstOutset; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 597 | fPrevPoint = fFirstPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 598 | fPrevUmbraIndex = fFirstVertexIndex; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 599 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 600 | *fPositions.push() = fFirstPoint; |
| 601 | *fColors.push() = this->umbraColor(z); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 602 | *fPositions.push() = fFirstPoint + fFirstOutset; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 603 | *fColors.push() = fPenumbraColor; |
| 604 | if (fTransparent) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 605 | fPositions[0] += fFirstPoint; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 606 | fCentroidCount = 1; |
| 607 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 608 | |
| 609 | // add the first quad |
| 610 | z = fTransformedHeightFunc(fInitPoints[1]); |
| 611 | fRadius = this->offset(z); |
| 612 | fUmbraColor = this->umbraColor(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 613 | this->addEdge(fInitPoints[1], normal); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 614 | |
| 615 | // to ensure we skip this block next time |
| 616 | *fInitPoints.push() = p; |
| 617 | } |
| 618 | |
| 619 | SkVector normal; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 620 | if (compute_normal(fPrevPoint, p, fDirection, &normal)) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 621 | SkVector scaledNormal = normal; |
| 622 | scaledNormal *= fRadius; |
| 623 | this->addArc(scaledNormal, true); |
| 624 | SkScalar z = fTransformedHeightFunc(p); |
| 625 | fRadius = this->offset(z); |
| 626 | fUmbraColor = this->umbraColor(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 627 | this->addEdge(p, normal); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 631 | void SkAmbientShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 632 | // We compute the inset in two stages: first we inset by half the current normal, |
| 633 | // then on the next addEdge() we add half of the next normal to get an average of the two |
| 634 | SkVector insetNormal = nextNormal; |
| 635 | insetNormal *= 0.5f*kInsetFactor; |
| 636 | |
| 637 | // Adding the other half of the average for the previous edge |
| 638 | fPositions[fPrevUmbraIndex] += insetNormal; |
| 639 | |
| 640 | SkPoint umbraPoint = nextPoint + insetNormal; |
| 641 | SkVector outsetNormal = nextNormal; |
| 642 | outsetNormal *= fRadius; |
| 643 | SkPoint penumbraPoint = nextPoint + outsetNormal; |
| 644 | |
| 645 | // For split edges, we're adding an average of two averages, so we multiply by another half |
| 646 | if (fSplitPreviousEdge) { |
| 647 | insetNormal *= 0.5f; |
| 648 | fPositions[fPrevUmbraIndex - 2] += insetNormal; |
| 649 | } |
| 650 | |
| 651 | // Split the edge to make sure we don't end up with a sharp alpha edge along the quad diagonal |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 652 | if (fColors[fPrevUmbraIndex] != fUmbraColor && |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 653 | SkPointPriv::DistanceToSqd(nextPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 654 | |
| 655 | // This is lacking 1/4 of the next inset -- we'll add it the next time we call addEdge() |
| 656 | SkPoint centerPoint = fPositions[fPrevUmbraIndex] + umbraPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 657 | centerPoint *= 0.5f; |
| 658 | *fPositions.push() = centerPoint; |
| 659 | *fColors.push() = SkPMLerp(fUmbraColor, fColors[fPrevUmbraIndex], 128); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 660 | centerPoint = fPositions[fPositions.count()-2] + penumbraPoint; |
| 661 | centerPoint *= 0.5f; |
| 662 | *fPositions.push() = centerPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 663 | *fColors.push() = fPenumbraColor; |
| 664 | |
| 665 | // set triangularization to get best interpolation of color |
| 666 | if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 667 | this->appendQuad(fPrevUmbraIndex, fPositions.count() - 3, |
| 668 | fPositions.count() - 2, fPositions.count() - 1); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 669 | } else { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 670 | this->appendQuad(fPositions.count() - 2, fPositions.count() - 1, |
| 671 | fPrevUmbraIndex, fPositions.count() - 3); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 672 | } |
| 673 | |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 674 | // if transparent, add point to first one in array and add to center fan |
| 675 | if (fTransparent) { |
| 676 | fPositions[0] += centerPoint; |
| 677 | ++fCentroidCount; |
| 678 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 679 | this->appendTriangle(0, fPrevUmbraIndex, fPositions.count() - 2); |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 682 | fSplitPreviousEdge = true; |
| 683 | if (fPrevUmbraIndex == fFirstVertexIndex) { |
| 684 | fSplitFirstEdge = true; |
| 685 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 686 | fPrevUmbraIndex = fPositions.count() - 2; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 687 | } else { |
| 688 | fSplitPreviousEdge = false; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 691 | // add next quad |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 692 | *fPositions.push() = umbraPoint; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 693 | *fColors.push() = fUmbraColor; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 694 | *fPositions.push() = penumbraPoint; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 695 | *fColors.push() = fPenumbraColor; |
| 696 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 697 | // set triangularization to get best interpolation of color |
| 698 | if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 699 | this->appendQuad(fPrevUmbraIndex, fPositions.count() - 3, |
| 700 | fPositions.count() - 2, fPositions.count() - 1); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 701 | } else { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 702 | this->appendQuad(fPositions.count() - 2, fPositions.count() - 1, |
| 703 | fPrevUmbraIndex, fPositions.count() - 3); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 704 | } |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 705 | |
| 706 | // if transparent, add point to first one in array and add to center fan |
| 707 | if (fTransparent) { |
| 708 | fPositions[0] += nextPoint; |
| 709 | ++fCentroidCount; |
| 710 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 711 | this->appendTriangle(0, fPrevUmbraIndex, fPositions.count() - 2); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 712 | } |
| 713 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 714 | fPrevUmbraIndex = fPositions.count() - 2; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 715 | fPrevPoint = nextPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 716 | fPrevOutset = outsetNormal; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 717 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 718 | |
| 719 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 720 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 721 | class SkSpotShadowTessellator : public SkBaseShadowTessellator { |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 722 | public: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 723 | SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 724 | const SkPoint3& zPlaneParams, const SkPoint3& lightPos, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 725 | SkScalar lightRadius, bool transparent); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 726 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 727 | private: |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 728 | void computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 729 | const SkMatrix& shadowTransform); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 730 | void computeClipVectorsAndTestCentroid(); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 731 | bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 732 | int getClosestUmbraPoint(const SkPoint& point); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 733 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 734 | bool computeConvexShadow(SkScalar radius); |
| 735 | bool computeConcaveShadow(SkScalar radius); |
| 736 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 737 | void handleLine(const SkPoint& p) override; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 738 | bool handlePolyPoint(const SkPoint& p); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 739 | |
| 740 | void mapPoints(SkScalar scale, const SkVector& xlate, SkPoint* pts, int count); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 741 | bool addInnerPoint(const SkPoint& pathPoint); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 742 | void addEdge(const SkVector& nextPoint, const SkVector& nextNormal); |
| 743 | |
| 744 | SkScalar offset(SkScalar z) { |
| 745 | float zRatio = SkTPin(z / (fLightZ - z), 0.0f, 0.95f); |
| 746 | return fLightRadius*zRatio; |
| 747 | } |
| 748 | |
| 749 | SkScalar fLightZ; |
| 750 | SkScalar fLightRadius; |
| 751 | SkScalar fOffsetAdjust; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 752 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 753 | SkTDArray<SkPoint> fClipPolygon; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 754 | SkTDArray<SkVector> fClipVectors; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 755 | SkPoint fCentroid; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 756 | SkScalar fArea; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 757 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 758 | SkTDArray<SkPoint> fPathPolygon; |
| 759 | SkTDArray<SkPoint> fUmbraPolygon; |
| 760 | int fCurrClipPoint; |
| 761 | int fCurrUmbraPoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 762 | bool fPrevUmbraOutside; |
| 763 | bool fFirstUmbraOutside; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 764 | bool fValidUmbra; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 765 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 766 | typedef SkBaseShadowTessellator INHERITED; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 767 | }; |
| 768 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 769 | SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 770 | const SkPoint3& zPlaneParams, |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 771 | const SkPoint3& lightPos, SkScalar lightRadius, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 772 | bool transparent) |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 773 | : INHERITED(zPlaneParams, transparent) |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 774 | , fLightZ(lightPos.fZ) |
| 775 | , fLightRadius(lightRadius) |
| 776 | , fOffsetAdjust(0) |
| 777 | , fCurrClipPoint(0) |
| 778 | , fPrevUmbraOutside(false) |
| 779 | , fFirstUmbraOutside(false) |
| 780 | , fValidUmbra(true) { |
| 781 | |
| 782 | // make sure we're not below the canvas plane |
| 783 | if (this->setZOffset(path.getBounds(), ctm.hasPerspective())) { |
| 784 | // Adjust light height and radius |
| 785 | fLightRadius *= (fLightZ + fZOffset) / fLightZ; |
| 786 | fLightZ += fZOffset; |
| 787 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 788 | |
| 789 | // Set radius and colors |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 790 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 791 | SkScalar occluderHeight = this->heightFunc(center.fX, center.fY) + fZOffset; |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 792 | fUmbraColor = SkColorSetARGB(255, 0, 0, 0); |
| 793 | fPenumbraColor = SkColorSetARGB(0, 0, 0, 0); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 794 | |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 795 | // Compute the blur radius, scale and translation for the spot shadow. |
| 796 | SkScalar radius; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 797 | SkMatrix shadowTransform; |
| 798 | if (!ctm.hasPerspective()) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 799 | SkScalar scale; |
| 800 | SkVector translate; |
| 801 | SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY, fLightZ, |
| 802 | lightRadius, &radius, &scale, &translate); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 803 | shadowTransform.setScaleTranslate(scale, scale, translate.fX, translate.fY); |
| 804 | } else { |
| 805 | // For perspective, we have a scale, a z-shear, and another projective divide -- |
| 806 | // this varies at each point so we can't use an affine transform. |
| 807 | // We'll just apply this to each generated point in turn. |
| 808 | shadowTransform.reset(); |
| 809 | // Also can't cull the center (for now). |
| 810 | fTransparent = true; |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 811 | radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 812 | } |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 813 | fRadius = radius; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 814 | SkMatrix fullTransform = SkMatrix::Concat(shadowTransform, ctm); |
| 815 | |
| 816 | // Set up our reverse mapping |
Jim Van Verth | 7c8008c | 2018-02-07 15:02:50 -0500 | [diff] [blame] | 817 | if (!this->setTransformedHeightFunc(fullTransform)) { |
| 818 | return; |
| 819 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 820 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 821 | // TODO: calculate these reserves better |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 822 | // Penumbra ring: 3*numPts |
| 823 | // Umbra ring: numPts |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 824 | // Inner ring: numPts |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 825 | fPositions.setReserve(5 * path.countPoints()); |
| 826 | fColors.setReserve(5 * path.countPoints()); |
| 827 | // Penumbra ring: 12*numPts |
| 828 | // Umbra ring: 3*numPts |
| 829 | fIndices.setReserve(15 * path.countPoints()); |
Brian Salomon | e7c85c4 | 2017-03-24 16:00:35 +0000 | [diff] [blame] | 830 | fClipPolygon.setReserve(path.countPoints()); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 831 | |
| 832 | // compute rough clip bounds for umbra, plus offset polygon, plus centroid |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 833 | this->computeClipAndPathPolygons(path, ctm, shadowTransform); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 834 | if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3) { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 835 | return; |
| 836 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 837 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 838 | // check to see if umbra collapses |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 839 | if (path.isConvex()) { |
| 840 | SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, |
| 841 | fPathPolygon[0], |
| 842 | fPathPolygon[1]); |
| 843 | SkRect bounds; |
| 844 | bounds.setBounds(&fPathPolygon[0], fPathPolygon.count()); |
| 845 | for (int i = 1; i < fPathPolygon.count(); ++i) { |
| 846 | int j = i + 1; |
| 847 | if (i == fPathPolygon.count() - 1) { |
| 848 | j = 0; |
| 849 | } |
| 850 | SkPoint currPoint = fPathPolygon[i]; |
| 851 | SkPoint nextPoint = fPathPolygon[j]; |
| 852 | SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint, |
| 853 | nextPoint); |
| 854 | if (distSq < minDistSq) { |
| 855 | minDistSq = distSq; |
| 856 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 857 | } |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 858 | static constexpr auto kTolerance = 1.0e-2f; |
| 859 | if (minDistSq < (radius + kTolerance)*(radius + kTolerance)) { |
| 860 | // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha |
| 861 | SkScalar newRadius = SkScalarSqrt(minDistSq) - kTolerance; |
| 862 | fOffsetAdjust = newRadius - radius; |
| 863 | SkScalar ratio = 128 * (newRadius + radius) / radius; |
| 864 | // they aren't PMColors, but the interpolation algorithm is the same |
| 865 | fUmbraColor = SkPMLerp(fUmbraColor, fPenumbraColor, (unsigned)ratio); |
| 866 | radius = newRadius; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 869 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 870 | // compute vectors for clip tests |
| 871 | this->computeClipVectorsAndTestCentroid(); |
| 872 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 873 | if (ctm.hasPerspective()) { |
| 874 | for (int i = 0; i < fPositions.count(); ++i) { |
| 875 | SkScalar pathZ = fTransformedHeightFunc(fPositions[i]); |
| 876 | SkScalar factor = SkScalarInvert(fLightZ - pathZ); |
| 877 | fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor; |
| 878 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 881 | if (path.isConvex()) { |
| 882 | if (!this->computeConvexShadow(radius)) { |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 883 | return; |
| 884 | } |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 885 | } else { |
| 886 | if (!this->computeConcaveShadow(radius)) { |
| 887 | return; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 888 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 889 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 890 | |
| 891 | if (ctm.hasPerspective()) { |
| 892 | for (int i = 0; i < fPositions.count(); ++i) { |
| 893 | SkScalar pathZ = fTransformedHeightFunc(fPositions[i]); |
| 894 | SkScalar factor = SkScalarInvert(fLightZ - pathZ); |
| 895 | fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor; |
| 896 | fPositions[i].fY = (fPositions[i].fY*fLightZ - lightPos.fY*pathZ)*factor; |
| 897 | } |
| 898 | #ifdef DRAW_CENTROID |
| 899 | SkScalar pathZ = fTransformedHeightFunc(fCentroid); |
| 900 | SkScalar factor = SkScalarInvert(fLightZ - pathZ); |
| 901 | fCentroid.fX = (fCentroid.fX*fLightZ - lightPos.fX*pathZ)*factor; |
| 902 | fCentroid.fY = (fCentroid.fY*fLightZ - lightPos.fY*pathZ)*factor; |
| 903 | #endif |
| 904 | } |
| 905 | #ifdef DRAW_CENTROID |
| 906 | *fPositions.push() = fCentroid + SkVector::Make(-2, -2); |
| 907 | *fColors.push() = SkColorSetARGB(255, 0, 255, 255); |
| 908 | *fPositions.push() = fCentroid + SkVector::Make(2, -2); |
| 909 | *fColors.push() = SkColorSetARGB(255, 0, 255, 255); |
| 910 | *fPositions.push() = fCentroid + SkVector::Make(-2, 2); |
| 911 | *fColors.push() = SkColorSetARGB(255, 0, 255, 255); |
| 912 | *fPositions.push() = fCentroid + SkVector::Make(2, 2); |
| 913 | *fColors.push() = SkColorSetARGB(255, 0, 255, 255); |
| 914 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 915 | this->appendQuad(fPositions.count() - 2, fPositions.count() - 1, |
| 916 | fPositions.count() - 4, fPositions.count() - 3); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 917 | #endif |
| 918 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 919 | fSucceeded = true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 920 | } |
| 921 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 922 | void SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 923 | const SkMatrix& shadowTransform) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 924 | |
| 925 | fPathPolygon.setReserve(path.countPoints()); |
| 926 | |
| 927 | // Walk around the path and compute clip polygon and path polygon. |
| 928 | // Will also accumulate sum of areas for centroid. |
| 929 | // For Bezier curves, we compute additional interior points on curve. |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 930 | SkPath::Iter iter(path, true); |
| 931 | SkPoint pts[4]; |
| 932 | SkPath::Verb verb; |
| 933 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 934 | fClipPolygon.reset(); |
| 935 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 936 | // init centroid |
| 937 | fCentroid = SkPoint::Make(0, 0); |
| 938 | fArea = 0; |
| 939 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 940 | // coefficients to compute cubic Bezier at t = 5/16 |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 941 | static constexpr SkScalar kA = 0.32495117187f; |
| 942 | static constexpr SkScalar kB = 0.44311523437f; |
| 943 | static constexpr SkScalar kC = 0.20141601562f; |
| 944 | static constexpr SkScalar kD = 0.03051757812f; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 945 | |
| 946 | SkPoint curvePoint; |
| 947 | SkScalar w; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 948 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 949 | switch (verb) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 950 | case SkPath::kLine_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 951 | ctm.mapPoints(&pts[1], 1); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 952 | *fClipPolygon.push() = pts[1]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 953 | this->INHERITED::handleLine(shadowTransform, &pts[1]); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 954 | break; |
| 955 | case SkPath::kQuad_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 956 | ctm.mapPoints(pts, 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 957 | // point at t = 1/2 |
| 958 | curvePoint.fX = 0.25f*pts[0].fX + 0.5f*pts[1].fX + 0.25f*pts[2].fX; |
| 959 | curvePoint.fY = 0.25f*pts[0].fY + 0.5f*pts[1].fY + 0.25f*pts[2].fY; |
| 960 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 961 | *fClipPolygon.push() = pts[2]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 962 | this->handleQuad(shadowTransform, pts); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 963 | break; |
| 964 | case SkPath::kConic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 965 | ctm.mapPoints(pts, 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 966 | w = iter.conicWeight(); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 967 | // point at t = 1/2 |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 968 | curvePoint.fX = 0.25f*pts[0].fX + w*0.5f*pts[1].fX + 0.25f*pts[2].fX; |
| 969 | curvePoint.fY = 0.25f*pts[0].fY + w*0.5f*pts[1].fY + 0.25f*pts[2].fY; |
| 970 | curvePoint *= SkScalarInvert(0.5f + 0.5f*w); |
| 971 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 972 | *fClipPolygon.push() = pts[2]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 973 | this->handleConic(shadowTransform, pts, w); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 974 | break; |
| 975 | case SkPath::kCubic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 976 | ctm.mapPoints(pts, 4); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 977 | // point at t = 5/16 |
| 978 | curvePoint.fX = kA*pts[0].fX + kB*pts[1].fX + kC*pts[2].fX + kD*pts[3].fX; |
| 979 | curvePoint.fY = kA*pts[0].fY + kB*pts[1].fY + kC*pts[2].fY + kD*pts[3].fY; |
| 980 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 981 | // point at t = 11/16 |
| 982 | curvePoint.fX = kD*pts[0].fX + kC*pts[1].fX + kB*pts[2].fX + kA*pts[3].fX; |
| 983 | curvePoint.fY = kD*pts[0].fY + kC*pts[1].fY + kB*pts[2].fY + kA*pts[3].fY; |
| 984 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 985 | *fClipPolygon.push() = pts[3]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 986 | this->handleCubic(shadowTransform, pts); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 987 | break; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 988 | case SkPath::kMove_Verb: |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 989 | case SkPath::kClose_Verb: |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 990 | case SkPath::kDone_Verb: |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 991 | break; |
| 992 | default: |
| 993 | SkDEBUGFAIL("unknown verb"); |
| 994 | } |
| 995 | } |
| 996 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 997 | // finish centroid |
| 998 | if (fPathPolygon.count() > 0) { |
| 999 | SkPoint currPoint = fPathPolygon[fPathPolygon.count() - 1]; |
| 1000 | SkPoint nextPoint = fPathPolygon[0]; |
| 1001 | SkScalar quadArea = currPoint.cross(nextPoint); |
| 1002 | fCentroid.fX += (currPoint.fX + nextPoint.fX) * quadArea; |
| 1003 | fCentroid.fY += (currPoint.fY + nextPoint.fY) * quadArea; |
| 1004 | fArea += quadArea; |
| 1005 | fCentroid *= SK_Scalar1 / (3 * fArea); |
| 1006 | } |
| 1007 | |
| 1008 | fCurrClipPoint = fClipPolygon.count() - 1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1009 | } |
| 1010 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1011 | void SkSpotShadowTessellator::computeClipVectorsAndTestCentroid() { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1012 | SkASSERT(fClipPolygon.count() >= 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1013 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1014 | // init clip vectors |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1015 | SkVector v0 = fClipPolygon[1] - fClipPolygon[0]; |
| 1016 | *fClipVectors.push() = v0; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1017 | |
| 1018 | // init centroid check |
| 1019 | bool hiddenCentroid = true; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1020 | SkVector v1 = fCentroid - fClipPolygon[0]; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1021 | SkScalar initCross = v0.cross(v1); |
| 1022 | |
| 1023 | for (int p = 1; p < fClipPolygon.count(); ++p) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1024 | // add to clip vectors |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1025 | v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p]; |
| 1026 | *fClipVectors.push() = v0; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1027 | // Determine if transformed centroid is inside clipPolygon. |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1028 | v1 = fCentroid - fClipPolygon[p]; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1029 | if (initCross*v0.cross(v1) <= 0) { |
| 1030 | hiddenCentroid = false; |
| 1031 | } |
| 1032 | } |
| 1033 | SkASSERT(fClipVectors.count() == fClipPolygon.count()); |
| 1034 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1035 | fTransparent = fTransparent || !hiddenCentroid; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | bool SkSpotShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, |
| 1039 | SkPoint* clipPoint) { |
| 1040 | SkVector segmentVector = centroid - umbraPoint; |
| 1041 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1042 | int startClipPoint = fCurrClipPoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1043 | do { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1044 | SkVector dp = umbraPoint - fClipPolygon[fCurrClipPoint]; |
| 1045 | SkScalar denom = fClipVectors[fCurrClipPoint].cross(segmentVector); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1046 | SkScalar t_num = dp.cross(segmentVector); |
| 1047 | // if line segments are nearly parallel |
| 1048 | if (SkScalarNearlyZero(denom)) { |
| 1049 | // and collinear |
| 1050 | if (SkScalarNearlyZero(t_num)) { |
| 1051 | return false; |
| 1052 | } |
| 1053 | // otherwise are separate, will try the next poly segment |
| 1054 | // else if crossing lies within poly segment |
| 1055 | } else if (t_num >= 0 && t_num <= denom) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1056 | SkScalar s_num = dp.cross(fClipVectors[fCurrClipPoint]); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1057 | // if umbra point is inside the clip polygon |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1058 | if (s_num >= 0 && s_num <= denom) { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1059 | segmentVector *= s_num/denom; |
| 1060 | *clipPoint = umbraPoint + segmentVector; |
| 1061 | return true; |
| 1062 | } |
| 1063 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1064 | fCurrClipPoint = (fCurrClipPoint + 1) % fClipPolygon.count(); |
| 1065 | } while (fCurrClipPoint != startClipPoint); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1066 | |
| 1067 | return false; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1068 | } |
| 1069 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1070 | int SkSpotShadowTessellator::getClosestUmbraPoint(const SkPoint& p) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1071 | SkScalar minDistance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[fCurrUmbraPoint]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1072 | int index = fCurrUmbraPoint; |
| 1073 | int dir = 1; |
| 1074 | int next = (index + dir) % fUmbraPolygon.count(); |
| 1075 | |
| 1076 | // init travel direction |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1077 | SkScalar distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1078 | if (distance < minDistance) { |
| 1079 | index = next; |
| 1080 | minDistance = distance; |
| 1081 | } else { |
| 1082 | dir = fUmbraPolygon.count()-1; |
| 1083 | } |
| 1084 | |
| 1085 | // iterate until we find a point that increases the distance |
| 1086 | next = (index + dir) % fUmbraPolygon.count(); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1087 | distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1088 | while (distance < minDistance) { |
| 1089 | index = next; |
| 1090 | minDistance = distance; |
| 1091 | next = (index + dir) % fUmbraPolygon.count(); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1092 | distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | fCurrUmbraPoint = index; |
| 1096 | return index; |
| 1097 | } |
| 1098 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1099 | bool SkSpotShadowTessellator::computeConvexShadow(SkScalar radius) { |
| 1100 | // generate inner ring |
| 1101 | if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), radius, |
| 1102 | &fUmbraPolygon)) { |
| 1103 | // this shouldn't happen, but just in case we'll inset using the centroid |
| 1104 | fValidUmbra = false; |
| 1105 | } |
| 1106 | |
| 1107 | // walk around the path polygon, generate outer ring and connect to inner ring |
| 1108 | if (fTransparent) { |
| 1109 | *fPositions.push() = fCentroid; |
| 1110 | *fColors.push() = fUmbraColor; |
| 1111 | } |
| 1112 | fCurrUmbraPoint = 0; |
| 1113 | for (int i = 0; i < fPathPolygon.count(); ++i) { |
| 1114 | if (!this->handlePolyPoint(fPathPolygon[i])) { |
| 1115 | return false; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if (!this->indexCount()) { |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
| 1123 | // finish up the final verts |
| 1124 | SkVector normal; |
| 1125 | if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) { |
| 1126 | normal *= fRadius; |
| 1127 | this->addArc(normal, true); |
| 1128 | |
| 1129 | // add to center fan |
| 1130 | if (fTransparent) { |
| 1131 | this->appendTriangle(0, fPrevUmbraIndex, fFirstVertexIndex); |
| 1132 | // or to clip ring |
| 1133 | } else { |
| 1134 | if (fFirstUmbraOutside) { |
| 1135 | this->appendTriangle(fPrevUmbraIndex, fFirstVertexIndex, fFirstVertexIndex + 1); |
| 1136 | if (fPrevUmbraOutside) { |
| 1137 | // fill out quad |
| 1138 | this->appendTriangle(fPrevUmbraIndex, fFirstVertexIndex + 1, |
| 1139 | fPrevUmbraIndex + 1); |
| 1140 | } |
| 1141 | } else if (fPrevUmbraOutside) { |
| 1142 | // add tri |
| 1143 | this->appendTriangle(fPrevUmbraIndex, fFirstVertexIndex, fPrevUmbraIndex + 1); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | // add final edge |
| 1148 | *fPositions.push() = fFirstPoint + normal; |
| 1149 | *fColors.push() = fPenumbraColor; |
| 1150 | |
| 1151 | this->appendQuad(fPrevUmbraIndex, fPositions.count() - 2, |
| 1152 | fFirstVertexIndex, fPositions.count() - 1); |
| 1153 | |
| 1154 | fPrevOutset = normal; |
| 1155 | } |
| 1156 | |
| 1157 | // final fan |
| 1158 | if (fPositions.count() >= 3) { |
| 1159 | fPrevUmbraIndex = fFirstVertexIndex; |
| 1160 | fPrevPoint = fFirstPoint; |
| 1161 | if (this->addArc(fFirstOutset, false)) { |
| 1162 | if (fFirstUmbraOutside) { |
| 1163 | this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1, |
| 1164 | fFirstVertexIndex + 2); |
| 1165 | } else { |
| 1166 | this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1, |
| 1167 | fFirstVertexIndex + 1); |
| 1168 | } |
| 1169 | } else { |
| 1170 | // no arc added, fix up by setting first penumbra point position to last one |
| 1171 | if (fFirstUmbraOutside) { |
| 1172 | fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1]; |
| 1173 | } else { |
| 1174 | fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1]; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
| 1182 | bool SkSpotShadowTessellator::computeConcaveShadow(SkScalar radius) { |
| 1183 | // TODO: remove when we support filling the penumbra |
| 1184 | if (fTransparent) { |
| 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | // generate inner ring |
| 1189 | SkTDArray<int> umbraIndices; |
| 1190 | umbraIndices.setReserve(fPathPolygon.count()); |
| 1191 | if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), radius, |
| 1192 | &fUmbraPolygon, &umbraIndices)) { |
| 1193 | // TODO: figure out how to handle this case |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | // generate outer ring |
| 1198 | SkTDArray<SkPoint> penumbraPolygon; |
| 1199 | SkTDArray<int> penumbraIndices; |
| 1200 | penumbraPolygon.setReserve(fUmbraPolygon.count()); |
| 1201 | penumbraIndices.setReserve(fUmbraPolygon.count()); |
| 1202 | if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), -radius, |
| 1203 | &penumbraPolygon, &penumbraIndices)) { |
| 1204 | // TODO: figure out how to handle this case |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
| 1208 | if (!fUmbraPolygon.count() || !penumbraPolygon.count()) { |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | // attach the rings together |
| 1213 | |
| 1214 | // find minimum indices |
| 1215 | int minIndex = 0; |
| 1216 | int min = penumbraIndices[0]; |
| 1217 | for (int i = 1; i < penumbraIndices.count(); ++i) { |
| 1218 | if (penumbraIndices[i] < min) { |
| 1219 | min = penumbraIndices[i]; |
| 1220 | minIndex = i; |
| 1221 | } |
| 1222 | } |
| 1223 | int currPenumbra = minIndex; |
| 1224 | |
| 1225 | minIndex = 0; |
| 1226 | min = umbraIndices[0]; |
| 1227 | for (int i = 1; i < umbraIndices.count(); ++i) { |
| 1228 | if (umbraIndices[i] < min) { |
| 1229 | min = umbraIndices[i]; |
| 1230 | minIndex = i; |
| 1231 | } |
| 1232 | } |
| 1233 | int currUmbra = minIndex; |
| 1234 | |
| 1235 | // now find a case where the indices are equal (there should be at least one) |
| 1236 | int maxPenumbraIndex = fPathPolygon.count()-1; |
| 1237 | int maxUmbraIndex = fPathPolygon.count()-1; |
| 1238 | while (penumbraIndices[currPenumbra] != umbraIndices[currUmbra]) { |
| 1239 | if (penumbraIndices[currPenumbra] < umbraIndices[currUmbra]) { |
| 1240 | penumbraIndices[currPenumbra] += fPathPolygon.count(); |
| 1241 | maxPenumbraIndex = penumbraIndices[currPenumbra]; |
| 1242 | currPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 1243 | } else { |
| 1244 | umbraIndices[currUmbra] += fPathPolygon.count(); |
| 1245 | maxUmbraIndex = umbraIndices[currUmbra]; |
| 1246 | currUmbra = (currUmbra + 1) % fUmbraPolygon.count(); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | *fPositions.push() = penumbraPolygon[currPenumbra]; |
| 1251 | *fColors.push() = fPenumbraColor; |
| 1252 | int prevPenumbraIndex = 0; |
| 1253 | *fPositions.push() = fUmbraPolygon[currUmbra]; |
| 1254 | *fColors.push() = fUmbraColor; |
| 1255 | fPrevUmbraIndex = 1; |
| 1256 | |
| 1257 | int nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 1258 | int nextUmbra = (currUmbra + 1) % fUmbraPolygon.count(); |
| 1259 | while (penumbraIndices[nextPenumbra] <= maxPenumbraIndex || |
| 1260 | umbraIndices[nextUmbra] <= maxUmbraIndex) { |
| 1261 | |
| 1262 | if (umbraIndices[nextUmbra] == penumbraIndices[nextPenumbra]) { |
| 1263 | // advance both one step |
| 1264 | *fPositions.push() = penumbraPolygon[nextPenumbra]; |
| 1265 | *fColors.push() = fPenumbraColor; |
| 1266 | int currPenumbraIndex = fPositions.count() - 1; |
| 1267 | |
| 1268 | *fPositions.push() = fUmbraPolygon[nextUmbra]; |
| 1269 | *fColors.push() = fUmbraColor; |
| 1270 | int currUmbraIndex = fPositions.count() - 1; |
| 1271 | |
| 1272 | this->appendQuad(prevPenumbraIndex, currPenumbraIndex, |
| 1273 | fPrevUmbraIndex, currUmbraIndex); |
| 1274 | |
| 1275 | prevPenumbraIndex = currPenumbraIndex; |
| 1276 | penumbraIndices[currPenumbra] += fPathPolygon.count(); |
| 1277 | currPenumbra = nextPenumbra; |
| 1278 | nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 1279 | |
| 1280 | fPrevUmbraIndex = currUmbraIndex; |
| 1281 | umbraIndices[currUmbra] += fPathPolygon.count(); |
| 1282 | currUmbra = nextUmbra; |
| 1283 | nextUmbra = (currUmbra + 1) % fUmbraPolygon.count(); |
| 1284 | } |
| 1285 | |
| 1286 | while (penumbraIndices[nextPenumbra] < umbraIndices[nextUmbra] && |
| 1287 | penumbraIndices[nextPenumbra] <= maxPenumbraIndex) { |
| 1288 | // fill out penumbra arc |
| 1289 | *fPositions.push() = penumbraPolygon[nextPenumbra]; |
| 1290 | *fColors.push() = fPenumbraColor; |
| 1291 | int currPenumbraIndex = fPositions.count() - 1; |
| 1292 | |
| 1293 | this->appendTriangle(prevPenumbraIndex, currPenumbraIndex, fPrevUmbraIndex); |
| 1294 | |
| 1295 | prevPenumbraIndex = currPenumbraIndex; |
| 1296 | // this ensures the ordering when we wrap around |
| 1297 | penumbraIndices[currPenumbra] += fPathPolygon.count(); |
| 1298 | currPenumbra = nextPenumbra; |
| 1299 | nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 1300 | } |
| 1301 | |
| 1302 | while (umbraIndices[nextUmbra] < penumbraIndices[nextPenumbra] && |
| 1303 | umbraIndices[nextUmbra] <= maxUmbraIndex) { |
| 1304 | // fill out umbra arc |
| 1305 | *fPositions.push() = fUmbraPolygon[nextUmbra]; |
| 1306 | *fColors.push() = fUmbraColor; |
| 1307 | int currUmbraIndex = fPositions.count() - 1; |
| 1308 | |
| 1309 | this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex); |
| 1310 | |
| 1311 | fPrevUmbraIndex = currUmbraIndex; |
| 1312 | // this ensures the ordering when we wrap around |
| 1313 | umbraIndices[currUmbra] += fPathPolygon.count(); |
| 1314 | currUmbra = nextUmbra; |
| 1315 | nextUmbra = (currUmbra + 1) % fUmbraPolygon.count(); |
| 1316 | } |
| 1317 | } |
| 1318 | // finish up by advancing both one step |
| 1319 | *fPositions.push() = penumbraPolygon[nextPenumbra]; |
| 1320 | *fColors.push() = fPenumbraColor; |
| 1321 | int currPenumbraIndex = fPositions.count() - 1; |
| 1322 | |
| 1323 | *fPositions.push() = fUmbraPolygon[nextUmbra]; |
| 1324 | *fColors.push() = fUmbraColor; |
| 1325 | int currUmbraIndex = fPositions.count() - 1; |
| 1326 | |
| 1327 | this->appendQuad(prevPenumbraIndex, currPenumbraIndex, |
| 1328 | fPrevUmbraIndex, currUmbraIndex); |
| 1329 | |
| 1330 | if (fTransparent) { |
| 1331 | // TODO: fill penumbra |
| 1332 | } |
| 1333 | |
| 1334 | return true; |
| 1335 | } |
| 1336 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 1337 | void SkSpotShadowTessellator::mapPoints(SkScalar scale, const SkVector& xlate, |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1338 | SkPoint* pts, int count) { |
| 1339 | // TODO: vectorize |
| 1340 | for (int i = 0; i < count; ++i) { |
| 1341 | pts[i] *= scale; |
| 1342 | pts[i] += xlate; |
| 1343 | } |
| 1344 | } |
| 1345 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1346 | static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) { |
| 1347 | static constexpr SkScalar kClose = (SK_Scalar1 / 16); |
| 1348 | static constexpr SkScalar kCloseSqd = kClose*kClose; |
| 1349 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1350 | SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1351 | return distSq < kCloseSqd; |
| 1352 | } |
| 1353 | |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1354 | static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1355 | SkVector v0 = p1 - p0; |
| 1356 | SkVector v1 = p2 - p0; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1357 | return v0.cross(v1); |
| 1358 | } |
| 1359 | |
| 1360 | static bool is_collinear(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
| 1361 | return (SkScalarNearlyZero(perp_dot(p0, p1, p2))); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 1364 | void SkSpotShadowTessellator::handleLine(const SkPoint& p) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1365 | // remove coincident points and add to centroid |
| 1366 | if (fPathPolygon.count() > 0) { |
| 1367 | const SkPoint& lastPoint = fPathPolygon[fPathPolygon.count() - 1]; |
| 1368 | if (duplicate_pt(p, lastPoint)) { |
| 1369 | return; |
| 1370 | } |
| 1371 | SkScalar quadArea = lastPoint.cross(p); |
| 1372 | fCentroid.fX += (p.fX + lastPoint.fX) * quadArea; |
| 1373 | fCentroid.fY += (p.fY + lastPoint.fY) * quadArea; |
| 1374 | fArea += quadArea; |
| 1375 | } |
| 1376 | |
| 1377 | // try to remove collinear points |
| 1378 | if (fPathPolygon.count() > 1 && is_collinear(fPathPolygon[fPathPolygon.count()-2], |
| 1379 | fPathPolygon[fPathPolygon.count()-1], |
| 1380 | p)) { |
| 1381 | fPathPolygon[fPathPolygon.count() - 1] = p; |
| 1382 | } else { |
| 1383 | *fPathPolygon.push() = p; |
| 1384 | } |
| 1385 | } |
| 1386 | |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1387 | bool SkSpotShadowTessellator::handlePolyPoint(const SkPoint& p) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1388 | if (fInitPoints.count() < 2) { |
| 1389 | *fInitPoints.push() = p; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1390 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | if (fInitPoints.count() == 2) { |
| 1394 | // determine if cw or ccw |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1395 | SkScalar perpDot = perp_dot(fInitPoints[0], fInitPoints[1], p); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1396 | if (SkScalarNearlyZero(perpDot)) { |
| 1397 | // nearly parallel, just treat as straight line and continue |
| 1398 | fInitPoints[1] = p; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1399 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | // if perpDot > 0, winding is ccw |
| 1403 | fDirection = (perpDot > 0) ? -1 : 1; |
| 1404 | |
| 1405 | // add first quad |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1406 | if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &fFirstOutset)) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1407 | // first two points are incident, make the third point the second and continue |
| 1408 | fInitPoints[1] = p; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1409 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1410 | } |
| 1411 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1412 | fFirstOutset *= fRadius; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1413 | fFirstPoint = fInitPoints[0]; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1414 | fFirstVertexIndex = fPositions.count(); |
| 1415 | fPrevOutset = fFirstOutset; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1416 | fPrevPoint = fFirstPoint; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 1417 | fPrevUmbraIndex = -1; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1418 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1419 | this->addInnerPoint(fFirstPoint); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1420 | fPrevUmbraIndex = fFirstVertexIndex; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1421 | |
| 1422 | if (!fTransparent) { |
| 1423 | SkPoint clipPoint; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1424 | bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex], |
| 1425 | fCentroid, &clipPoint); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1426 | if (isOutside) { |
| 1427 | *fPositions.push() = clipPoint; |
| 1428 | *fColors.push() = fUmbraColor; |
| 1429 | } |
| 1430 | fPrevUmbraOutside = isOutside; |
| 1431 | fFirstUmbraOutside = isOutside; |
| 1432 | } |
| 1433 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1434 | SkPoint newPoint = fFirstPoint + fFirstOutset; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1435 | *fPositions.push() = newPoint; |
| 1436 | *fColors.push() = fPenumbraColor; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1437 | this->addEdge(fInitPoints[1], fFirstOutset); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1438 | |
| 1439 | // to ensure we skip this block next time |
| 1440 | *fInitPoints.push() = p; |
| 1441 | } |
| 1442 | |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1443 | // if concave, abort |
| 1444 | SkScalar perpDot = perp_dot(fInitPoints[1], fInitPoints[2], p); |
| 1445 | if (fDirection*perpDot > 0) { |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1449 | SkVector normal; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1450 | if (compute_normal(fPrevPoint, p, fDirection, &normal)) { |
| 1451 | normal *= fRadius; |
| 1452 | this->addArc(normal, true); |
| 1453 | this->addEdge(p, normal); |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1454 | fInitPoints[1] = fInitPoints[2]; |
| 1455 | fInitPoints[2] = p; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1456 | } |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1457 | |
| 1458 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1459 | } |
| 1460 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1461 | bool SkSpotShadowTessellator::addInnerPoint(const SkPoint& pathPoint) { |
| 1462 | SkPoint umbraPoint; |
| 1463 | if (!fValidUmbra) { |
| 1464 | SkVector v = fCentroid - pathPoint; |
| 1465 | v *= 0.95f; |
| 1466 | umbraPoint = pathPoint + v; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1467 | } else { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1468 | umbraPoint = fUmbraPolygon[this->getClosestUmbraPoint(pathPoint)]; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1469 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1470 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1471 | fPrevPoint = pathPoint; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1472 | |
| 1473 | // merge "close" points |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 1474 | if (fPrevUmbraIndex == -1 || |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1475 | !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) { |
| 1476 | *fPositions.push() = umbraPoint; |
| 1477 | *fColors.push() = fUmbraColor; |
| 1478 | |
| 1479 | return false; |
| 1480 | } else { |
| 1481 | return true; |
| 1482 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1483 | } |
| 1484 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 1485 | void SkSpotShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1486 | // add next umbra point |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1487 | bool duplicate = this->addInnerPoint(nextPoint); |
| 1488 | int prevPenumbraIndex = duplicate ? fPositions.count()-1 : fPositions.count()-2; |
| 1489 | int currUmbraIndex = duplicate ? fPrevUmbraIndex : fPositions.count()-1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1490 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1491 | if (!duplicate) { |
| 1492 | // add to center fan if transparent or centroid showing |
| 1493 | if (fTransparent) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1494 | this->appendTriangle(0, fPrevUmbraIndex, currUmbraIndex); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1495 | // otherwise add to clip ring |
| 1496 | } else { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1497 | SkPoint clipPoint; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1498 | bool isOutside = this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid, |
| 1499 | &clipPoint); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1500 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1501 | if (isOutside) { |
| 1502 | *fPositions.push() = clipPoint; |
| 1503 | *fColors.push() = fUmbraColor; |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1504 | this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, currUmbraIndex + 1); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1505 | if (fPrevUmbraOutside) { |
| 1506 | // fill out quad |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1507 | this->appendTriangle(fPrevUmbraIndex, currUmbraIndex + 1, |
| 1508 | fPrevUmbraIndex + 1); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1509 | } |
| 1510 | } else if (fPrevUmbraOutside) { |
| 1511 | // add tri |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1512 | this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, fPrevUmbraIndex + 1); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1513 | } |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1514 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1515 | fPrevUmbraOutside = isOutside; |
| 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | // add next penumbra point and quad |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1520 | SkPoint newPoint = nextPoint + nextNormal; |
| 1521 | *fPositions.push() = newPoint; |
| 1522 | *fColors.push() = fPenumbraColor; |
| 1523 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1524 | if (!duplicate) { |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1525 | this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1526 | } |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1527 | this->appendTriangle(prevPenumbraIndex, fPositions.count() - 1, currUmbraIndex); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1528 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1529 | fPrevUmbraIndex = currUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1530 | fPrevOutset = nextNormal; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1531 | } |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1532 | |
| 1533 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1534 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1535 | sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1536 | const SkPoint3& zPlane, bool transparent) { |
Mike Reed | 9d5c674 | 2018-03-06 10:31:27 -0500 | [diff] [blame] | 1537 | if (!path.isFinite() || !path.isConvex() || !ctm.isFinite() || !zPlane.isFinite()) { |
| 1538 | return nullptr; |
| 1539 | } |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1540 | SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1541 | return ambientTess.releaseVertices(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1542 | } |
| 1543 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1544 | sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1545 | const SkPoint3& zPlane, const SkPoint3& lightPos, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 1546 | SkScalar lightRadius, bool transparent) { |
Mike Reed | 9d5c674 | 2018-03-06 10:31:27 -0500 | [diff] [blame] | 1547 | if (!path.isFinite() || !path.isConvex() || !ctm.isFinite() || !zPlane.isFinite() || |
| 1548 | !lightPos.isFinite() || !SkScalarIsFinite(lightRadius) || !(lightRadius > 0)) { |
| 1549 | return nullptr; |
| 1550 | } |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1551 | SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1552 | return spotTess.releaseVertices(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1553 | } |