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