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