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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkPath.h" |
| 9 | #include "include/core/SkPoint3.h" |
| 10 | #include "include/core/SkVertices.h" |
| 11 | #include "include/private/SkColorData.h" |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame] | 12 | #include "include/private/SkTPin.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkDrawShadowInfo.h" |
| 14 | #include "src/core/SkGeometry.h" |
| 15 | #include "src/core/SkPointPriv.h" |
| 16 | #include "src/utils/SkPolyUtils.h" |
| 17 | #include "src/utils/SkShadowTessellator.h" |
Jim Van Verth | 85dc96b | 2017-01-30 14:49:21 -0500 | [diff] [blame] | 18 | |
| 19 | #if SK_SUPPORT_GPU |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 20 | #include "src/gpu/geometry/GrPathUtils.h" |
Jim Van Verth | 85dc96b | 2017-01-30 14:49:21 -0500 | [diff] [blame] | 21 | #endif |
Jim Van Verth | 58abc9e | 2017-01-25 11:05:01 -0500 | [diff] [blame] | 22 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 23 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 24 | /** |
| 25 | * Base class |
| 26 | */ |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 27 | class SkBaseShadowTessellator { |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 28 | public: |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 29 | SkBaseShadowTessellator(const SkPoint3& zPlaneParams, const SkRect& bounds, bool transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 30 | virtual ~SkBaseShadowTessellator() {} |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 31 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 32 | sk_sp<SkVertices> releaseVertices() { |
| 33 | if (!fSucceeded) { |
| 34 | return nullptr; |
| 35 | } |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 36 | return SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, this->vertexCount(), |
Mike Reed | 97eb4fe | 2017-03-14 12:04:16 -0400 | [diff] [blame] | 37 | fPositions.begin(), nullptr, fColors.begin(), |
| 38 | this->indexCount(), fIndices.begin()); |
Brian Salomon | 1a8b79a | 2017-01-31 11:26:26 -0500 | [diff] [blame] | 39 | } |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 40 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 41 | protected: |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 42 | static constexpr auto kMinHeight = 0.1f; |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 43 | static constexpr auto kPenumbraColor = SK_ColorTRANSPARENT; |
| 44 | static constexpr auto kUmbraColor = SK_ColorBLACK; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 45 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 46 | int vertexCount() const { return fPositions.count(); } |
| 47 | int indexCount() const { return fIndices.count(); } |
| 48 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 49 | // initialization methods |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 50 | bool accumulateCentroid(const SkPoint& c, const SkPoint& n); |
| 51 | bool checkConvexity(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2); |
| 52 | void finishPathPolygon(); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 53 | |
| 54 | // convex shadow methods |
| 55 | bool computeConvexShadow(SkScalar inset, SkScalar outset, bool doClip); |
| 56 | void computeClipVectorsAndTestCentroid(); |
| 57 | bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 58 | void addEdge(const SkVector& nextPoint, const SkVector& nextNormal, SkColor umbraColor, |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 59 | const SkTDArray<SkPoint>& umbraPolygon, bool lastEdge, bool doClip); |
| 60 | bool addInnerPoint(const SkPoint& pathPoint, SkColor umbraColor, |
| 61 | const SkTDArray<SkPoint>& umbraPolygon, int* currUmbraIndex); |
| 62 | int getClosestUmbraIndex(const SkPoint& point, const SkTDArray<SkPoint>& umbraPolygon); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 63 | |
| 64 | // concave shadow methods |
| 65 | bool computeConcaveShadow(SkScalar inset, SkScalar outset); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 66 | void stitchConcaveRings(const SkTDArray<SkPoint>& umbraPolygon, |
| 67 | SkTDArray<int>* umbraIndices, |
| 68 | const SkTDArray<SkPoint>& penumbraPolygon, |
| 69 | SkTDArray<int>* penumbraIndices); |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 70 | |
| 71 | void handleLine(const SkPoint& p); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 72 | void handleLine(const SkMatrix& m, SkPoint* p); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 73 | |
| 74 | void handleQuad(const SkPoint pts[3]); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 75 | void handleQuad(const SkMatrix& m, SkPoint pts[3]); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 76 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 77 | void handleCubic(const SkMatrix& m, SkPoint pts[4]); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 78 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 79 | void handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 80 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 81 | bool addArc(const SkVector& nextNormal, SkScalar offset, bool finishArc); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 82 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 83 | void appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2); |
| 84 | void appendQuad(uint16_t index0, uint16_t index1, uint16_t index2, uint16_t index3); |
| 85 | |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 86 | SkScalar heightFunc(SkScalar x, SkScalar y) { |
| 87 | return fZPlaneParams.fX*x + fZPlaneParams.fY*y + fZPlaneParams.fZ; |
| 88 | } |
| 89 | |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 90 | SkPoint3 fZPlaneParams; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 91 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 92 | // temporary buffer |
| 93 | SkTDArray<SkPoint> fPointBuffer; |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 94 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 95 | SkTDArray<SkPoint> fPositions; |
| 96 | SkTDArray<SkColor> fColors; |
| 97 | SkTDArray<uint16_t> fIndices; |
| 98 | |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 99 | SkTDArray<SkPoint> fPathPolygon; |
| 100 | SkTDArray<SkPoint> fClipPolygon; |
| 101 | SkTDArray<SkVector> fClipVectors; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 102 | |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 103 | SkRect fPathBounds; |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 104 | SkPoint fCentroid; |
| 105 | SkScalar fArea; |
| 106 | SkScalar fLastArea; |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 107 | SkScalar fLastCross; |
| 108 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 109 | int fFirstVertexIndex; |
| 110 | SkVector fFirstOutset; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 111 | SkPoint fFirstPoint; |
| 112 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 113 | bool fSucceeded; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 114 | bool fTransparent; |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 115 | bool fIsConvex; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 116 | bool fValidUmbra; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 117 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 118 | SkScalar fDirection; |
| 119 | int fPrevUmbraIndex; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 120 | int fCurrUmbraIndex; |
| 121 | int fCurrClipIndex; |
| 122 | bool fPrevUmbraOutside; |
| 123 | bool fFirstUmbraOutside; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 124 | SkVector fPrevOutset; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 125 | SkPoint fPrevPoint; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 126 | }; |
| 127 | |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 128 | // make external linkage happy |
| 129 | constexpr SkColor SkBaseShadowTessellator::kUmbraColor; |
| 130 | constexpr SkColor SkBaseShadowTessellator::kPenumbraColor; |
| 131 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 132 | 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] | 133 | SkVector* newNormal) { |
| 134 | SkVector normal; |
| 135 | // compute perpendicular |
| 136 | normal.fX = p0.fY - p1.fY; |
| 137 | normal.fY = p1.fX - p0.fX; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 138 | normal *= dir; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 139 | if (!normal.normalize()) { |
| 140 | return false; |
| 141 | } |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 142 | *newNormal = normal; |
| 143 | return true; |
| 144 | } |
| 145 | |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 146 | static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) { |
| 147 | static constexpr SkScalar kClose = (SK_Scalar1 / 16); |
| 148 | static constexpr SkScalar kCloseSqd = kClose * kClose; |
| 149 | |
| 150 | SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1); |
| 151 | return distSq < kCloseSqd; |
| 152 | } |
| 153 | |
| 154 | static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
| 155 | SkVector v0 = p1 - p0; |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 156 | SkVector v1 = p2 - p1; |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 157 | return v0.cross(v1); |
| 158 | } |
| 159 | |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 160 | SkBaseShadowTessellator::SkBaseShadowTessellator(const SkPoint3& zPlaneParams, const SkRect& bounds, |
| 161 | bool transparent) |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 162 | : fZPlaneParams(zPlaneParams) |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 163 | , fPathBounds(bounds) |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 164 | , fCentroid({0, 0}) |
| 165 | , fArea(0) |
| 166 | , fLastArea(0) |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 167 | , fLastCross(0) |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 168 | , fFirstVertexIndex(-1) |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 169 | , fSucceeded(false) |
| 170 | , fTransparent(transparent) |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 171 | , fIsConvex(true) |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 172 | , fValidUmbra(true) |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 173 | , fDirection(1) |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 174 | , fPrevUmbraIndex(-1) |
| 175 | , fCurrUmbraIndex(0) |
| 176 | , fCurrClipIndex(0) |
| 177 | , fPrevUmbraOutside(false) |
| 178 | , fFirstUmbraOutside(false) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 179 | // child classes will set reserve for positions, colors and indices |
| 180 | } |
| 181 | |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 182 | bool SkBaseShadowTessellator::accumulateCentroid(const SkPoint& curr, const SkPoint& next) { |
| 183 | if (duplicate_pt(curr, next)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 187 | SkASSERT(fPathPolygon.count() > 0); |
| 188 | SkVector v0 = curr - fPathPolygon[0]; |
| 189 | SkVector v1 = next - fPathPolygon[0]; |
| 190 | SkScalar quadArea = v0.cross(v1); |
| 191 | fCentroid.fX += (v0.fX + v1.fX) * quadArea; |
| 192 | fCentroid.fY += (v0.fY + v1.fY) * quadArea; |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 193 | fArea += quadArea; |
| 194 | // convexity check |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 195 | if (quadArea*fLastArea < 0) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 196 | fIsConvex = false; |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 197 | } |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 198 | if (0 != quadArea) { |
| 199 | fLastArea = quadArea; |
| 200 | } |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | bool SkBaseShadowTessellator::checkConvexity(const SkPoint& p0, |
| 206 | const SkPoint& p1, |
| 207 | const SkPoint& p2) { |
| 208 | SkScalar cross = perp_dot(p0, p1, p2); |
| 209 | // skip collinear point |
| 210 | if (SkScalarNearlyZero(cross)) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // check for convexity |
| 215 | if (fLastCross*cross < 0) { |
| 216 | fIsConvex = false; |
| 217 | } |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 218 | if (0 != cross) { |
| 219 | fLastCross = cross; |
| 220 | } |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | void SkBaseShadowTessellator::finishPathPolygon() { |
| 226 | if (fPathPolygon.count() > 1) { |
| 227 | if (!this->accumulateCentroid(fPathPolygon[fPathPolygon.count() - 1], fPathPolygon[0])) { |
| 228 | // remove coincident point |
| 229 | fPathPolygon.pop(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (fPathPolygon.count() > 2) { |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 234 | // do this before the final convexity check, so we use the correct fPathPolygon[0] |
| 235 | fCentroid *= sk_ieee_float_divide(1, 3 * fArea); |
| 236 | fCentroid += fPathPolygon[0]; |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 237 | if (!checkConvexity(fPathPolygon[fPathPolygon.count() - 2], |
| 238 | fPathPolygon[fPathPolygon.count() - 1], |
| 239 | fPathPolygon[0])) { |
| 240 | // remove collinear point |
| 241 | fPathPolygon[0] = fPathPolygon[fPathPolygon.count() - 1]; |
| 242 | fPathPolygon.pop(); |
| 243 | } |
| 244 | } |
| 245 | |
Jim Van Verth | 7deacf4 | 2018-06-08 15:13:25 -0400 | [diff] [blame] | 246 | // if area is positive, winding is ccw |
| 247 | fDirection = fArea > 0 ? -1 : 1; |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 250 | bool SkBaseShadowTessellator::computeConvexShadow(SkScalar inset, SkScalar outset, bool doClip) { |
| 251 | if (doClip) { |
| 252 | this->computeClipVectorsAndTestCentroid(); |
| 253 | } |
| 254 | |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 255 | // adjust inset distance and umbra color if necessary |
| 256 | auto umbraColor = kUmbraColor; |
| 257 | SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, |
| 258 | fPathPolygon[0], |
| 259 | fPathPolygon[1]); |
| 260 | SkRect bounds; |
| 261 | bounds.setBounds(&fPathPolygon[0], fPathPolygon.count()); |
| 262 | for (int i = 1; i < fPathPolygon.count(); ++i) { |
| 263 | int j = i + 1; |
| 264 | if (i == fPathPolygon.count() - 1) { |
| 265 | j = 0; |
| 266 | } |
| 267 | SkPoint currPoint = fPathPolygon[i]; |
| 268 | SkPoint nextPoint = fPathPolygon[j]; |
| 269 | SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint, |
| 270 | nextPoint); |
| 271 | if (distSq < minDistSq) { |
| 272 | minDistSq = distSq; |
| 273 | } |
| 274 | } |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 275 | |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 276 | SkTDArray<SkPoint> insetPolygon; |
| 277 | if (inset > SK_ScalarNearlyZero) { |
| 278 | static constexpr auto kTolerance = 1.0e-2f; |
| 279 | if (minDistSq < (inset + kTolerance)*(inset + kTolerance)) { |
| 280 | // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha |
| 281 | auto newInset = SkScalarSqrt(minDistSq) - kTolerance; |
| 282 | auto ratio = 128 * (newInset / inset + 1); |
| 283 | SkASSERT(SkScalarIsFinite(ratio)); |
| 284 | // they aren't PMColors, but the interpolation algorithm is the same |
| 285 | umbraColor = SkPMLerp(kUmbraColor, kPenumbraColor, (unsigned)ratio); |
| 286 | inset = newInset; |
| 287 | } |
| 288 | |
| 289 | // generate inner ring |
| 290 | if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), inset, |
| 291 | &insetPolygon)) { |
| 292 | // not ideal, but in this case we'll inset using the centroid |
| 293 | fValidUmbra = false; |
| 294 | } |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 295 | } |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 296 | const SkTDArray<SkPoint>& umbraPolygon = (inset > SK_ScalarNearlyZero) ? insetPolygon |
| 297 | : fPathPolygon; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 298 | |
| 299 | // walk around the path polygon, generate outer ring and connect to inner ring |
| 300 | if (fTransparent) { |
| 301 | fPositions.push_back(fCentroid); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 302 | fColors.push_back(umbraColor); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 303 | } |
| 304 | fCurrUmbraIndex = 0; |
| 305 | |
| 306 | // initial setup |
| 307 | // add first quad |
| 308 | int polyCount = fPathPolygon.count(); |
| 309 | if (!compute_normal(fPathPolygon[polyCount - 1], fPathPolygon[0], fDirection, &fFirstOutset)) { |
| 310 | // polygon should be sanitized by this point, so this is unrecoverable |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | fFirstOutset *= outset; |
| 315 | fFirstPoint = fPathPolygon[polyCount - 1]; |
| 316 | fFirstVertexIndex = fPositions.count(); |
| 317 | fPrevOutset = fFirstOutset; |
| 318 | fPrevPoint = fFirstPoint; |
| 319 | fPrevUmbraIndex = -1; |
| 320 | |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 321 | this->addInnerPoint(fFirstPoint, umbraColor, umbraPolygon, &fPrevUmbraIndex); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 322 | |
| 323 | if (!fTransparent && doClip) { |
| 324 | SkPoint clipPoint; |
| 325 | bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex], |
| 326 | fCentroid, &clipPoint); |
| 327 | if (isOutside) { |
| 328 | fPositions.push_back(clipPoint); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 329 | fColors.push_back(umbraColor); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 330 | } |
| 331 | fPrevUmbraOutside = isOutside; |
| 332 | fFirstUmbraOutside = isOutside; |
| 333 | } |
| 334 | |
| 335 | SkPoint newPoint = fFirstPoint + fFirstOutset; |
| 336 | fPositions.push_back(newPoint); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 337 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 338 | this->addEdge(fPathPolygon[0], fFirstOutset, umbraColor, umbraPolygon, false, doClip); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 339 | |
| 340 | for (int i = 1; i < polyCount; ++i) { |
| 341 | SkVector normal; |
| 342 | if (!compute_normal(fPrevPoint, fPathPolygon[i], fDirection, &normal)) { |
| 343 | return false; |
| 344 | } |
| 345 | normal *= outset; |
| 346 | this->addArc(normal, outset, true); |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 347 | this->addEdge(fPathPolygon[i], normal, umbraColor, umbraPolygon, |
| 348 | i == polyCount - 1, doClip); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 349 | } |
| 350 | SkASSERT(this->indexCount()); |
| 351 | |
| 352 | // final fan |
| 353 | SkASSERT(fPositions.count() >= 3); |
| 354 | if (this->addArc(fFirstOutset, outset, false)) { |
| 355 | if (fFirstUmbraOutside) { |
| 356 | this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1, |
| 357 | fFirstVertexIndex + 2); |
| 358 | } else { |
| 359 | this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1, |
| 360 | fFirstVertexIndex + 1); |
| 361 | } |
| 362 | } else { |
| 363 | // no arc added, fix up by setting first penumbra point position to last one |
| 364 | if (fFirstUmbraOutside) { |
| 365 | fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1]; |
| 366 | } else { |
| 367 | fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1]; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | void SkBaseShadowTessellator::computeClipVectorsAndTestCentroid() { |
| 375 | SkASSERT(fClipPolygon.count() >= 3); |
| 376 | fCurrClipIndex = fClipPolygon.count() - 1; |
| 377 | |
| 378 | // init clip vectors |
| 379 | SkVector v0 = fClipPolygon[1] - fClipPolygon[0]; |
| 380 | SkVector v1 = fClipPolygon[2] - fClipPolygon[0]; |
| 381 | fClipVectors.push_back(v0); |
| 382 | |
| 383 | // init centroid check |
| 384 | bool hiddenCentroid = true; |
| 385 | v1 = fCentroid - fClipPolygon[0]; |
| 386 | SkScalar initCross = v0.cross(v1); |
| 387 | |
| 388 | for (int p = 1; p < fClipPolygon.count(); ++p) { |
| 389 | // add to clip vectors |
| 390 | v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p]; |
| 391 | fClipVectors.push_back(v0); |
| 392 | // Determine if transformed centroid is inside clipPolygon. |
| 393 | v1 = fCentroid - fClipPolygon[p]; |
| 394 | if (initCross*v0.cross(v1) <= 0) { |
| 395 | hiddenCentroid = false; |
| 396 | } |
| 397 | } |
| 398 | SkASSERT(fClipVectors.count() == fClipPolygon.count()); |
| 399 | |
| 400 | fTransparent = fTransparent || !hiddenCentroid; |
| 401 | } |
| 402 | |
| 403 | void SkBaseShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal, |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 404 | SkColor umbraColor, const SkTDArray<SkPoint>& umbraPolygon, |
| 405 | bool lastEdge, bool doClip) { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 406 | // add next umbra point |
| 407 | int currUmbraIndex; |
| 408 | bool duplicate; |
| 409 | if (lastEdge) { |
| 410 | duplicate = false; |
| 411 | currUmbraIndex = fFirstVertexIndex; |
| 412 | fPrevPoint = nextPoint; |
| 413 | } else { |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 414 | duplicate = this->addInnerPoint(nextPoint, umbraColor, umbraPolygon, &currUmbraIndex); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 415 | } |
| 416 | int prevPenumbraIndex = duplicate || (currUmbraIndex == fFirstVertexIndex) |
| 417 | ? fPositions.count() - 1 |
| 418 | : fPositions.count() - 2; |
| 419 | if (!duplicate) { |
| 420 | // add to center fan if transparent or centroid showing |
| 421 | if (fTransparent) { |
| 422 | this->appendTriangle(0, fPrevUmbraIndex, currUmbraIndex); |
| 423 | // otherwise add to clip ring |
| 424 | } else if (doClip) { |
| 425 | SkPoint clipPoint; |
| 426 | bool isOutside = lastEdge ? fFirstUmbraOutside |
| 427 | : this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid, |
| 428 | &clipPoint); |
| 429 | if (isOutside) { |
| 430 | if (!lastEdge) { |
| 431 | fPositions.push_back(clipPoint); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 432 | fColors.push_back(umbraColor); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 433 | } |
| 434 | this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, currUmbraIndex + 1); |
| 435 | if (fPrevUmbraOutside) { |
| 436 | // fill out quad |
| 437 | this->appendTriangle(fPrevUmbraIndex, currUmbraIndex + 1, |
| 438 | fPrevUmbraIndex + 1); |
| 439 | } |
| 440 | } else if (fPrevUmbraOutside) { |
| 441 | // add tri |
| 442 | this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, fPrevUmbraIndex + 1); |
| 443 | } |
| 444 | |
| 445 | fPrevUmbraOutside = isOutside; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // add next penumbra point and quad |
| 450 | SkPoint newPoint = nextPoint + nextNormal; |
| 451 | fPositions.push_back(newPoint); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 452 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 453 | |
| 454 | if (!duplicate) { |
| 455 | this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex); |
| 456 | } |
| 457 | this->appendTriangle(prevPenumbraIndex, fPositions.count() - 1, currUmbraIndex); |
| 458 | |
| 459 | fPrevUmbraIndex = currUmbraIndex; |
| 460 | fPrevOutset = nextNormal; |
| 461 | } |
| 462 | |
| 463 | bool SkBaseShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, |
| 464 | SkPoint* clipPoint) { |
| 465 | SkVector segmentVector = centroid - umbraPoint; |
| 466 | |
| 467 | int startClipPoint = fCurrClipIndex; |
| 468 | do { |
| 469 | SkVector dp = umbraPoint - fClipPolygon[fCurrClipIndex]; |
| 470 | SkScalar denom = fClipVectors[fCurrClipIndex].cross(segmentVector); |
| 471 | SkScalar t_num = dp.cross(segmentVector); |
| 472 | // if line segments are nearly parallel |
| 473 | if (SkScalarNearlyZero(denom)) { |
| 474 | // and collinear |
| 475 | if (SkScalarNearlyZero(t_num)) { |
| 476 | return false; |
| 477 | } |
| 478 | // otherwise are separate, will try the next poly segment |
| 479 | // else if crossing lies within poly segment |
| 480 | } else if (t_num >= 0 && t_num <= denom) { |
| 481 | SkScalar s_num = dp.cross(fClipVectors[fCurrClipIndex]); |
| 482 | // if umbra point is inside the clip polygon |
| 483 | if (s_num >= 0 && s_num <= denom) { |
| 484 | segmentVector *= s_num / denom; |
| 485 | *clipPoint = umbraPoint + segmentVector; |
| 486 | return true; |
| 487 | } |
| 488 | } |
| 489 | fCurrClipIndex = (fCurrClipIndex + 1) % fClipPolygon.count(); |
| 490 | } while (fCurrClipIndex != startClipPoint); |
| 491 | |
| 492 | return false; |
| 493 | } |
| 494 | |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 495 | bool SkBaseShadowTessellator::addInnerPoint(const SkPoint& pathPoint, SkColor umbraColor, |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 496 | const SkTDArray<SkPoint>& umbraPolygon, |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 497 | int* currUmbraIndex) { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 498 | SkPoint umbraPoint; |
| 499 | if (!fValidUmbra) { |
| 500 | SkVector v = fCentroid - pathPoint; |
| 501 | v *= 0.95f; |
| 502 | umbraPoint = pathPoint + v; |
| 503 | } else { |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 504 | umbraPoint = umbraPolygon[this->getClosestUmbraIndex(pathPoint, umbraPolygon)]; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | fPrevPoint = pathPoint; |
| 508 | |
| 509 | // merge "close" points |
| 510 | if (fPrevUmbraIndex == -1 || |
| 511 | !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) { |
| 512 | // if we've wrapped around, don't add a new point |
| 513 | if (fPrevUmbraIndex >= 0 && duplicate_pt(umbraPoint, fPositions[fFirstVertexIndex])) { |
| 514 | *currUmbraIndex = fFirstVertexIndex; |
| 515 | } else { |
| 516 | *currUmbraIndex = fPositions.count(); |
| 517 | fPositions.push_back(umbraPoint); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 518 | fColors.push_back(umbraColor); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 519 | } |
| 520 | return false; |
| 521 | } else { |
| 522 | *currUmbraIndex = fPrevUmbraIndex; |
| 523 | return true; |
| 524 | } |
| 525 | } |
| 526 | |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 527 | int SkBaseShadowTessellator::getClosestUmbraIndex(const SkPoint& p, |
| 528 | const SkTDArray<SkPoint>& umbraPolygon) { |
| 529 | SkScalar minDistance = SkPointPriv::DistanceToSqd(p, umbraPolygon[fCurrUmbraIndex]); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 530 | int index = fCurrUmbraIndex; |
| 531 | int dir = 1; |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 532 | int next = (index + dir) % umbraPolygon.count(); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 533 | |
| 534 | // init travel direction |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 535 | SkScalar distance = SkPointPriv::DistanceToSqd(p, umbraPolygon[next]); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 536 | if (distance < minDistance) { |
| 537 | index = next; |
| 538 | minDistance = distance; |
| 539 | } else { |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 540 | dir = umbraPolygon.count() - 1; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // iterate until we find a point that increases the distance |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 544 | next = (index + dir) % umbraPolygon.count(); |
| 545 | distance = SkPointPriv::DistanceToSqd(p, umbraPolygon[next]); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 546 | while (distance < minDistance) { |
| 547 | index = next; |
| 548 | minDistance = distance; |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 549 | next = (index + dir) % umbraPolygon.count(); |
| 550 | distance = SkPointPriv::DistanceToSqd(p, umbraPolygon[next]); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | fCurrUmbraIndex = index; |
| 554 | return index; |
| 555 | } |
| 556 | |
| 557 | bool SkBaseShadowTessellator::computeConcaveShadow(SkScalar inset, SkScalar outset) { |
| 558 | if (!SkIsSimplePolygon(&fPathPolygon[0], fPathPolygon.count())) { |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | // generate inner ring |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 563 | SkTDArray<SkPoint> umbraPolygon; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 564 | SkTDArray<int> umbraIndices; |
| 565 | umbraIndices.setReserve(fPathPolygon.count()); |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 566 | if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), fPathBounds, inset, |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 567 | &umbraPolygon, &umbraIndices)) { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 568 | // TODO: figure out how to handle this case |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | // generate outer ring |
| 573 | SkTDArray<SkPoint> penumbraPolygon; |
| 574 | SkTDArray<int> penumbraIndices; |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 575 | penumbraPolygon.setReserve(umbraPolygon.count()); |
| 576 | penumbraIndices.setReserve(umbraPolygon.count()); |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 577 | if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), fPathBounds, -outset, |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 578 | &penumbraPolygon, &penumbraIndices)) { |
| 579 | // TODO: figure out how to handle this case |
| 580 | return false; |
| 581 | } |
| 582 | |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 583 | if (!umbraPolygon.count() || !penumbraPolygon.count()) { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 584 | return false; |
| 585 | } |
| 586 | |
| 587 | // attach the rings together |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 588 | this->stitchConcaveRings(umbraPolygon, &umbraIndices, penumbraPolygon, &penumbraIndices); |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 589 | |
| 590 | return true; |
| 591 | } |
| 592 | |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 593 | void SkBaseShadowTessellator::stitchConcaveRings(const SkTDArray<SkPoint>& umbraPolygon, |
| 594 | SkTDArray<int>* umbraIndices, |
| 595 | const SkTDArray<SkPoint>& penumbraPolygon, |
| 596 | SkTDArray<int>* penumbraIndices) { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 597 | // TODO: only create and fill indexMap when fTransparent is true? |
| 598 | SkAutoSTMalloc<64, uint16_t> indexMap(umbraPolygon.count()); |
| 599 | |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 600 | // find minimum indices |
| 601 | int minIndex = 0; |
| 602 | int min = (*penumbraIndices)[0]; |
| 603 | for (int i = 1; i < (*penumbraIndices).count(); ++i) { |
| 604 | if ((*penumbraIndices)[i] < min) { |
| 605 | min = (*penumbraIndices)[i]; |
| 606 | minIndex = i; |
| 607 | } |
| 608 | } |
| 609 | int currPenumbra = minIndex; |
| 610 | |
| 611 | minIndex = 0; |
| 612 | min = (*umbraIndices)[0]; |
| 613 | for (int i = 1; i < (*umbraIndices).count(); ++i) { |
| 614 | if ((*umbraIndices)[i] < min) { |
| 615 | min = (*umbraIndices)[i]; |
| 616 | minIndex = i; |
| 617 | } |
| 618 | } |
| 619 | int currUmbra = minIndex; |
| 620 | |
| 621 | // now find a case where the indices are equal (there should be at least one) |
| 622 | int maxPenumbraIndex = fPathPolygon.count() - 1; |
| 623 | int maxUmbraIndex = fPathPolygon.count() - 1; |
| 624 | while ((*penumbraIndices)[currPenumbra] != (*umbraIndices)[currUmbra]) { |
| 625 | if ((*penumbraIndices)[currPenumbra] < (*umbraIndices)[currUmbra]) { |
| 626 | (*penumbraIndices)[currPenumbra] += fPathPolygon.count(); |
| 627 | maxPenumbraIndex = (*penumbraIndices)[currPenumbra]; |
| 628 | currPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 629 | } else { |
| 630 | (*umbraIndices)[currUmbra] += fPathPolygon.count(); |
| 631 | maxUmbraIndex = (*umbraIndices)[currUmbra]; |
| 632 | currUmbra = (currUmbra + 1) % umbraPolygon.count(); |
| 633 | } |
| 634 | } |
| 635 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 636 | fPositions.push_back(penumbraPolygon[currPenumbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 637 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 638 | int prevPenumbraIndex = 0; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 639 | fPositions.push_back(umbraPolygon[currUmbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 640 | fColors.push_back(kUmbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 641 | fPrevUmbraIndex = 1; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 642 | indexMap[currUmbra] = 1; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 643 | |
| 644 | int nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 645 | int nextUmbra = (currUmbra + 1) % umbraPolygon.count(); |
| 646 | while ((*penumbraIndices)[nextPenumbra] <= maxPenumbraIndex || |
| 647 | (*umbraIndices)[nextUmbra] <= maxUmbraIndex) { |
| 648 | |
| 649 | if ((*umbraIndices)[nextUmbra] == (*penumbraIndices)[nextPenumbra]) { |
| 650 | // advance both one step |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 651 | fPositions.push_back(penumbraPolygon[nextPenumbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 652 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 653 | int currPenumbraIndex = fPositions.count() - 1; |
| 654 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 655 | fPositions.push_back(umbraPolygon[nextUmbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 656 | fColors.push_back(kUmbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 657 | int currUmbraIndex = fPositions.count() - 1; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 658 | indexMap[nextUmbra] = currUmbraIndex; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 659 | |
| 660 | this->appendQuad(prevPenumbraIndex, currPenumbraIndex, |
| 661 | fPrevUmbraIndex, currUmbraIndex); |
| 662 | |
| 663 | prevPenumbraIndex = currPenumbraIndex; |
| 664 | (*penumbraIndices)[currPenumbra] += fPathPolygon.count(); |
| 665 | currPenumbra = nextPenumbra; |
| 666 | nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 667 | |
| 668 | fPrevUmbraIndex = currUmbraIndex; |
| 669 | (*umbraIndices)[currUmbra] += fPathPolygon.count(); |
| 670 | currUmbra = nextUmbra; |
| 671 | nextUmbra = (currUmbra + 1) % umbraPolygon.count(); |
| 672 | } |
| 673 | |
| 674 | while ((*penumbraIndices)[nextPenumbra] < (*umbraIndices)[nextUmbra] && |
| 675 | (*penumbraIndices)[nextPenumbra] <= maxPenumbraIndex) { |
| 676 | // fill out penumbra arc |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 677 | fPositions.push_back(penumbraPolygon[nextPenumbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 678 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 679 | int currPenumbraIndex = fPositions.count() - 1; |
| 680 | |
| 681 | this->appendTriangle(prevPenumbraIndex, currPenumbraIndex, fPrevUmbraIndex); |
| 682 | |
| 683 | prevPenumbraIndex = currPenumbraIndex; |
| 684 | // this ensures the ordering when we wrap around |
| 685 | (*penumbraIndices)[currPenumbra] += fPathPolygon.count(); |
| 686 | currPenumbra = nextPenumbra; |
| 687 | nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count(); |
| 688 | } |
| 689 | |
| 690 | while ((*umbraIndices)[nextUmbra] < (*penumbraIndices)[nextPenumbra] && |
| 691 | (*umbraIndices)[nextUmbra] <= maxUmbraIndex) { |
| 692 | // fill out umbra arc |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 693 | fPositions.push_back(umbraPolygon[nextUmbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 694 | fColors.push_back(kUmbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 695 | int currUmbraIndex = fPositions.count() - 1; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 696 | indexMap[nextUmbra] = currUmbraIndex; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 697 | |
| 698 | this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex); |
| 699 | |
| 700 | fPrevUmbraIndex = currUmbraIndex; |
| 701 | // this ensures the ordering when we wrap around |
| 702 | (*umbraIndices)[currUmbra] += fPathPolygon.count(); |
| 703 | currUmbra = nextUmbra; |
| 704 | nextUmbra = (currUmbra + 1) % umbraPolygon.count(); |
| 705 | } |
| 706 | } |
| 707 | // finish up by advancing both one step |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 708 | fPositions.push_back(penumbraPolygon[nextPenumbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 709 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 710 | int currPenumbraIndex = fPositions.count() - 1; |
| 711 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 712 | fPositions.push_back(umbraPolygon[nextUmbra]); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 713 | fColors.push_back(kUmbraColor); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 714 | int currUmbraIndex = fPositions.count() - 1; |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 715 | indexMap[nextUmbra] = currUmbraIndex; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 716 | |
| 717 | this->appendQuad(prevPenumbraIndex, currPenumbraIndex, |
| 718 | fPrevUmbraIndex, currUmbraIndex); |
| 719 | |
| 720 | if (fTransparent) { |
Jim Van Verth | 8664a1d | 2018-06-28 16:26:50 -0400 | [diff] [blame] | 721 | SkTriangulateSimplePolygon(umbraPolygon.begin(), indexMap, umbraPolygon.count(), |
| 722 | &fIndices); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 727 | // tesselation tolerance values, in device space pixels |
Mike Klein | b8b51e6 | 2017-02-09 15:22:53 -0500 | [diff] [blame] | 728 | #if SK_SUPPORT_GPU |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 729 | static const SkScalar kQuadTolerance = 0.2f; |
| 730 | static const SkScalar kCubicTolerance = 0.2f; |
Mike Klein | b8b51e6 | 2017-02-09 15:22:53 -0500 | [diff] [blame] | 731 | #endif |
Brian Osman | e3deee1 | 2018-11-20 11:10:15 -0500 | [diff] [blame] | 732 | static const SkScalar kConicTolerance = 0.25f; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 733 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 734 | // clamps the point to the nearest 16th of a pixel |
| 735 | static void sanitize_point(const SkPoint& in, SkPoint* out) { |
| 736 | out->fX = SkScalarRoundToScalar(16.f*in.fX)*0.0625f; |
| 737 | out->fY = SkScalarRoundToScalar(16.f*in.fY)*0.0625f; |
| 738 | } |
| 739 | |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 740 | void SkBaseShadowTessellator::handleLine(const SkPoint& p) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 741 | SkPoint pSanitized; |
| 742 | sanitize_point(p, &pSanitized); |
| 743 | |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 744 | if (fPathPolygon.count() > 0) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 745 | if (!this->accumulateCentroid(fPathPolygon[fPathPolygon.count() - 1], pSanitized)) { |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 746 | // skip coincident point |
| 747 | return; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | if (fPathPolygon.count() > 1) { |
| 752 | if (!checkConvexity(fPathPolygon[fPathPolygon.count() - 2], |
| 753 | fPathPolygon[fPathPolygon.count() - 1], |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 754 | pSanitized)) { |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 755 | // remove collinear point |
| 756 | fPathPolygon.pop(); |
Jim Van Verth | 6bdfebe | 2018-09-17 11:46:50 -0400 | [diff] [blame] | 757 | // it's possible that the previous point is coincident with the new one now |
| 758 | if (duplicate_pt(fPathPolygon[fPathPolygon.count() - 1], pSanitized)) { |
| 759 | fPathPolygon.pop(); |
| 760 | } |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 764 | fPathPolygon.push_back(pSanitized); |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 765 | } |
| 766 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 767 | void SkBaseShadowTessellator::handleLine(const SkMatrix& m, SkPoint* p) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 768 | m.mapPoints(p, 1); |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 769 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 770 | this->handleLine(*p); |
| 771 | } |
| 772 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 773 | void SkBaseShadowTessellator::handleQuad(const SkPoint pts[3]) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 774 | #if SK_SUPPORT_GPU |
Jim Van Verth | a947e29 | 2018-02-26 13:54:34 -0500 | [diff] [blame] | 775 | // check for degeneracy |
| 776 | SkVector v0 = pts[1] - pts[0]; |
| 777 | SkVector v1 = pts[2] - pts[0]; |
| 778 | if (SkScalarNearlyZero(v0.cross(v1))) { |
| 779 | return; |
| 780 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 781 | // TODO: Pull PathUtils out of Ganesh? |
| 782 | int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance); |
Mike Klein | cc9856c | 2018-04-19 09:18:33 -0400 | [diff] [blame] | 783 | fPointBuffer.setCount(maxCount); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 784 | SkPoint* target = fPointBuffer.begin(); |
| 785 | int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2], |
| 786 | kQuadTolerance, &target, maxCount); |
| 787 | fPointBuffer.setCount(count); |
| 788 | for (int i = 0; i < count; i++) { |
| 789 | this->handleLine(fPointBuffer[i]); |
| 790 | } |
| 791 | #else |
| 792 | // for now, just to draw something |
| 793 | this->handleLine(pts[1]); |
| 794 | this->handleLine(pts[2]); |
| 795 | #endif |
| 796 | } |
| 797 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 798 | void SkBaseShadowTessellator::handleQuad(const SkMatrix& m, SkPoint pts[3]) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 799 | m.mapPoints(pts, 3); |
| 800 | this->handleQuad(pts); |
| 801 | } |
| 802 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 803 | void SkBaseShadowTessellator::handleCubic(const SkMatrix& m, SkPoint pts[4]) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 804 | m.mapPoints(pts, 4); |
| 805 | #if SK_SUPPORT_GPU |
| 806 | // TODO: Pull PathUtils out of Ganesh? |
| 807 | int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance); |
Mike Klein | cc9856c | 2018-04-19 09:18:33 -0400 | [diff] [blame] | 808 | fPointBuffer.setCount(maxCount); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 809 | SkPoint* target = fPointBuffer.begin(); |
| 810 | int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3], |
| 811 | kCubicTolerance, &target, maxCount); |
| 812 | fPointBuffer.setCount(count); |
| 813 | for (int i = 0; i < count; i++) { |
| 814 | this->handleLine(fPointBuffer[i]); |
| 815 | } |
| 816 | #else |
| 817 | // for now, just to draw something |
| 818 | this->handleLine(pts[1]); |
| 819 | this->handleLine(pts[2]); |
| 820 | this->handleLine(pts[3]); |
| 821 | #endif |
| 822 | } |
| 823 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 824 | void SkBaseShadowTessellator::handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 825 | if (m.hasPerspective()) { |
| 826 | w = SkConic::TransformW(pts, w, m); |
| 827 | } |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 828 | m.mapPoints(pts, 3); |
| 829 | SkAutoConicToQuads quadder; |
| 830 | const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance); |
| 831 | SkPoint lastPoint = *(quads++); |
| 832 | int count = quadder.countQuads(); |
| 833 | for (int i = 0; i < count; ++i) { |
| 834 | SkPoint quadPts[3]; |
| 835 | quadPts[0] = lastPoint; |
| 836 | quadPts[1] = quads[0]; |
| 837 | quadPts[2] = i == count - 1 ? pts[2] : quads[1]; |
| 838 | this->handleQuad(quadPts); |
| 839 | lastPoint = quadPts[2]; |
| 840 | quads += 2; |
| 841 | } |
| 842 | } |
| 843 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 844 | bool SkBaseShadowTessellator::addArc(const SkVector& nextNormal, SkScalar offset, bool finishArc) { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 845 | // fill in fan from previous quad |
| 846 | SkScalar rotSin, rotCos; |
| 847 | int numSteps; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 848 | if (!SkComputeRadialSteps(fPrevOutset, nextNormal, offset, &rotSin, &rotCos, &numSteps)) { |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 849 | // recover as best we can |
| 850 | numSteps = 0; |
| 851 | } |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 852 | SkVector prevNormal = fPrevOutset; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 853 | for (int i = 0; i < numSteps-1; ++i) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 854 | SkVector currNormal; |
| 855 | currNormal.fX = prevNormal.fX*rotCos - prevNormal.fY*rotSin; |
| 856 | currNormal.fY = prevNormal.fY*rotCos + prevNormal.fX*rotSin; |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 857 | fPositions.push_back(fPrevPoint + currNormal); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 858 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 859 | this->appendTriangle(fPrevUmbraIndex, fPositions.count() - 1, fPositions.count() - 2); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 860 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 861 | prevNormal = currNormal; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 862 | } |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 863 | if (finishArc && numSteps) { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 864 | fPositions.push_back(fPrevPoint + nextNormal); |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 865 | fColors.push_back(kPenumbraColor); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 866 | this->appendTriangle(fPrevUmbraIndex, fPositions.count() - 1, fPositions.count() - 2); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 867 | } |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 868 | fPrevOutset = nextNormal; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 869 | |
| 870 | return (numSteps > 0); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 871 | } |
| 872 | |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 873 | void SkBaseShadowTessellator::appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2) { |
| 874 | auto indices = fIndices.append(3); |
| 875 | |
| 876 | indices[0] = index0; |
| 877 | indices[1] = index1; |
| 878 | indices[2] = index2; |
| 879 | } |
| 880 | |
| 881 | void SkBaseShadowTessellator::appendQuad(uint16_t index0, uint16_t index1, |
| 882 | uint16_t index2, uint16_t index3) { |
| 883 | auto indices = fIndices.append(6); |
| 884 | |
| 885 | indices[0] = index0; |
| 886 | indices[1] = index1; |
| 887 | indices[2] = index2; |
| 888 | |
| 889 | indices[3] = index2; |
| 890 | indices[4] = index1; |
| 891 | indices[5] = index3; |
| 892 | } |
| 893 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 894 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 895 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 896 | class SkAmbientShadowTessellator : public SkBaseShadowTessellator { |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 897 | public: |
| 898 | SkAmbientShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 899 | const SkPoint3& zPlaneParams, bool transparent); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 900 | |
| 901 | private: |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 902 | bool computePathPolygon(const SkPath& path, const SkMatrix& ctm); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 903 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 904 | using INHERITED = SkBaseShadowTessellator; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 905 | }; |
| 906 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 907 | SkAmbientShadowTessellator::SkAmbientShadowTessellator(const SkPath& path, |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 908 | const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 909 | const SkPoint3& zPlaneParams, |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 910 | bool transparent) |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 911 | : INHERITED(zPlaneParams, path.getBounds(), transparent) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 912 | // Set base colors |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 913 | auto baseZ = heightFunc(fPathBounds.centerX(), fPathBounds.centerY()); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 914 | // umbraColor is the interior value, penumbraColor the exterior value. |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 915 | auto outset = SkDrawShadowMetrics::AmbientBlurRadius(baseZ); |
| 916 | auto inset = outset * SkDrawShadowMetrics::AmbientRecipAlpha(baseZ) - outset; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 917 | inset = SkTPin(inset, 0.0f, std::min(path.getBounds().width(), |
Brian Osman | aba642c | 2020-02-06 12:52:25 -0500 | [diff] [blame] | 918 | path.getBounds().height())); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 919 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 920 | if (!this->computePathPolygon(path, ctm)) { |
| 921 | return; |
| 922 | } |
| 923 | if (fPathPolygon.count() < 3 || !SkScalarIsFinite(fArea)) { |
| 924 | fSucceeded = true; // We don't want to try to blur these cases, so we will |
| 925 | // return an empty SkVertices instead. |
| 926 | return; |
| 927 | } |
| 928 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 929 | // Outer ring: 3*numPts |
| 930 | // Middle ring: numPts |
| 931 | fPositions.setReserve(4 * path.countPoints()); |
| 932 | fColors.setReserve(4 * path.countPoints()); |
| 933 | // Outer ring: 12*numPts |
| 934 | // Middle ring: 0 |
| 935 | fIndices.setReserve(12 * path.countPoints()); |
| 936 | |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 937 | if (fIsConvex) { |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 938 | fSucceeded = this->computeConvexShadow(inset, outset, false); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 939 | } else { |
Jim Van Verth | fb18639 | 2018-09-11 11:37:46 -0400 | [diff] [blame] | 940 | fSucceeded = this->computeConcaveShadow(inset, outset); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 944 | bool SkAmbientShadowTessellator::computePathPolygon(const SkPath& path, const SkMatrix& ctm) { |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 945 | fPathPolygon.setReserve(path.countPoints()); |
| 946 | |
| 947 | // walk around the path, tessellate and generate outer ring |
| 948 | // if original path is transparent, will accumulate sum of points for centroid |
| 949 | SkPath::Iter iter(path, true); |
| 950 | SkPoint pts[4]; |
| 951 | SkPath::Verb verb; |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 952 | bool verbSeen = false; |
| 953 | bool closeSeen = false; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 954 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 955 | if (closeSeen) { |
| 956 | return false; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 957 | } |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 958 | switch (verb) { |
| 959 | case SkPath::kLine_Verb: |
| 960 | this->handleLine(ctm, &pts[1]); |
| 961 | break; |
| 962 | case SkPath::kQuad_Verb: |
| 963 | this->handleQuad(ctm, pts); |
| 964 | break; |
| 965 | case SkPath::kCubic_Verb: |
| 966 | this->handleCubic(ctm, pts); |
| 967 | break; |
| 968 | case SkPath::kConic_Verb: |
| 969 | this->handleConic(ctm, pts, iter.conicWeight()); |
| 970 | break; |
| 971 | case SkPath::kMove_Verb: |
| 972 | if (verbSeen) { |
| 973 | return false; |
| 974 | } |
| 975 | break; |
| 976 | case SkPath::kClose_Verb: |
| 977 | case SkPath::kDone_Verb: |
| 978 | closeSeen = true; |
| 979 | break; |
| 980 | } |
| 981 | verbSeen = true; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | this->finishPathPolygon(); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 985 | return true; |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 986 | } |
| 987 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 988 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 989 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 990 | class SkSpotShadowTessellator : public SkBaseShadowTessellator { |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 991 | public: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 992 | SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 993 | const SkPoint3& zPlaneParams, const SkPoint3& lightPos, |
Jim Van Verth | 63f0354 | 2020-12-16 11:56:11 -0500 | [diff] [blame] | 994 | SkScalar lightRadius, bool transparent, bool directional); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 995 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 996 | private: |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 997 | bool computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 998 | const SkMatrix& shadowTransform); |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 999 | void addToClip(const SkVector& nextPoint); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1000 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 1001 | using INHERITED = SkBaseShadowTessellator; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1002 | }; |
| 1003 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1004 | SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1005 | const SkPoint3& zPlaneParams, |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 1006 | const SkPoint3& lightPos, SkScalar lightRadius, |
Jim Van Verth | 63f0354 | 2020-12-16 11:56:11 -0500 | [diff] [blame] | 1007 | bool transparent, bool directional) |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 1008 | : INHERITED(zPlaneParams, path.getBounds(), transparent) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1009 | |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 1010 | // Compute the blur radius, scale and translation for the spot shadow. |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1011 | SkMatrix shadowTransform; |
Jim Van Verth | 3a039d5 | 2018-09-14 17:14:47 -0400 | [diff] [blame] | 1012 | SkScalar outset; |
Jim Van Verth | 63f0354 | 2020-12-16 11:56:11 -0500 | [diff] [blame] | 1013 | if (!SkDrawShadowMetrics::GetSpotShadowTransform(lightPos, lightRadius, ctm, zPlaneParams, |
| 1014 | path.getBounds(), directional, |
Jim Van Verth | 3a039d5 | 2018-09-14 17:14:47 -0400 | [diff] [blame] | 1015 | &shadowTransform, &outset)) { |
| 1016 | return; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1017 | } |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 1018 | SkScalar inset = outset; |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 1019 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1020 | // compute rough clip bounds for umbra, plus offset polygon, plus centroid |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1021 | if (!this->computeClipAndPathPolygons(path, ctm, shadowTransform)) { |
| 1022 | return; |
| 1023 | } |
| 1024 | if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3 || !SkScalarIsFinite(fArea)) { |
| 1025 | fSucceeded = true; // We don't want to try to blur these cases, so we will |
| 1026 | // return an empty SkVertices instead. |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1027 | return; |
| 1028 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1029 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 1030 | // TODO: calculate these reserves better |
| 1031 | // Penumbra ring: 3*numPts |
| 1032 | // Umbra ring: numPts |
| 1033 | // Inner ring: numPts |
| 1034 | fPositions.setReserve(5 * path.countPoints()); |
| 1035 | fColors.setReserve(5 * path.countPoints()); |
| 1036 | // Penumbra ring: 12*numPts |
| 1037 | // Umbra ring: 3*numPts |
| 1038 | fIndices.setReserve(15 * path.countPoints()); |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1039 | |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1040 | if (fIsConvex) { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 1041 | fSucceeded = this->computeConvexShadow(inset, outset, true); |
Jim Van Verth | 872da6b | 2018-04-10 11:24:11 -0400 | [diff] [blame] | 1042 | } else { |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 1043 | fSucceeded = this->computeConcaveShadow(inset, outset); |
Jim Van Verth | 8760e2f | 2018-06-12 14:21:38 -0400 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | if (!fSucceeded) { |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1047 | return; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1048 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1049 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 1050 | fSucceeded = true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1051 | } |
| 1052 | |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1053 | bool SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1054 | const SkMatrix& shadowTransform) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1055 | |
| 1056 | fPathPolygon.setReserve(path.countPoints()); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1057 | fClipPolygon.setReserve(path.countPoints()); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Walk around the path and compute clip polygon and path polygon. |
| 1060 | // Will also accumulate sum of areas for centroid. |
| 1061 | // For Bezier curves, we compute additional interior points on curve. |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1062 | SkPath::Iter iter(path, true); |
| 1063 | SkPoint pts[4]; |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1064 | SkPoint clipPts[4]; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1065 | SkPath::Verb verb; |
| 1066 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1067 | // coefficients to compute cubic Bezier at t = 5/16 |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1068 | static constexpr SkScalar kA = 0.32495117187f; |
| 1069 | static constexpr SkScalar kB = 0.44311523437f; |
| 1070 | static constexpr SkScalar kC = 0.20141601562f; |
| 1071 | static constexpr SkScalar kD = 0.03051757812f; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1072 | |
| 1073 | SkPoint curvePoint; |
| 1074 | SkScalar w; |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1075 | bool closeSeen = false; |
| 1076 | bool verbSeen = false; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1077 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1078 | if (closeSeen) { |
| 1079 | return false; |
| 1080 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1081 | switch (verb) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1082 | case SkPath::kLine_Verb: |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1083 | ctm.mapPoints(clipPts, &pts[1], 1); |
| 1084 | this->addToClip(clipPts[0]); |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 1085 | this->handleLine(shadowTransform, &pts[1]); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1086 | break; |
| 1087 | case SkPath::kQuad_Verb: |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1088 | ctm.mapPoints(clipPts, pts, 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1089 | // point at t = 1/2 |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1090 | curvePoint.fX = 0.25f*clipPts[0].fX + 0.5f*clipPts[1].fX + 0.25f*clipPts[2].fX; |
| 1091 | curvePoint.fY = 0.25f*clipPts[0].fY + 0.5f*clipPts[1].fY + 0.25f*clipPts[2].fY; |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1092 | this->addToClip(curvePoint); |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1093 | this->addToClip(clipPts[2]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1094 | this->handleQuad(shadowTransform, pts); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1095 | break; |
| 1096 | case SkPath::kConic_Verb: |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1097 | ctm.mapPoints(clipPts, pts, 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1098 | w = iter.conicWeight(); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1099 | // point at t = 1/2 |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1100 | curvePoint.fX = 0.25f*clipPts[0].fX + w*0.5f*clipPts[1].fX + 0.25f*clipPts[2].fX; |
| 1101 | curvePoint.fY = 0.25f*clipPts[0].fY + w*0.5f*clipPts[1].fY + 0.25f*clipPts[2].fY; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1102 | curvePoint *= SkScalarInvert(0.5f + 0.5f*w); |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1103 | this->addToClip(curvePoint); |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1104 | this->addToClip(clipPts[2]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1105 | this->handleConic(shadowTransform, pts, w); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1106 | break; |
| 1107 | case SkPath::kCubic_Verb: |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1108 | ctm.mapPoints(clipPts, pts, 4); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1109 | // point at t = 5/16 |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1110 | curvePoint.fX = kA*clipPts[0].fX + kB*clipPts[1].fX |
| 1111 | + kC*clipPts[2].fX + kD*clipPts[3].fX; |
| 1112 | curvePoint.fY = kA*clipPts[0].fY + kB*clipPts[1].fY |
| 1113 | + kC*clipPts[2].fY + kD*clipPts[3].fY; |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1114 | this->addToClip(curvePoint); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1115 | // point at t = 11/16 |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1116 | curvePoint.fX = kD*clipPts[0].fX + kC*clipPts[1].fX |
| 1117 | + kB*clipPts[2].fX + kA*clipPts[3].fX; |
| 1118 | curvePoint.fY = kD*clipPts[0].fY + kC*clipPts[1].fY |
| 1119 | + kB*clipPts[2].fY + kA*clipPts[3].fY; |
Jim Van Verth | f507c28 | 2018-05-11 10:48:20 -0400 | [diff] [blame] | 1120 | this->addToClip(curvePoint); |
Jim Van Verth | 0b7645f | 2018-08-31 12:36:52 -0400 | [diff] [blame] | 1121 | this->addToClip(clipPts[3]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1122 | this->handleCubic(shadowTransform, pts); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1123 | break; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1124 | case SkPath::kMove_Verb: |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1125 | if (verbSeen) { |
| 1126 | return false; |
| 1127 | } |
| 1128 | break; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1129 | case SkPath::kClose_Verb: |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1130 | case SkPath::kDone_Verb: |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1131 | closeSeen = true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1132 | break; |
| 1133 | default: |
| 1134 | SkDEBUGFAIL("unknown verb"); |
| 1135 | } |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1136 | verbSeen = true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1137 | } |
| 1138 | |
Jim Van Verth | cdaf661 | 2018-06-05 15:21:13 -0400 | [diff] [blame] | 1139 | this->finishPathPolygon(); |
Jim Van Verth | 3645bb0 | 2018-06-26 14:58:58 -0400 | [diff] [blame] | 1140 | return true; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1141 | } |
| 1142 | |
Jim Van Verth | d737ab9 | 2018-09-10 15:19:01 -0400 | [diff] [blame] | 1143 | void SkSpotShadowTessellator::addToClip(const SkPoint& point) { |
| 1144 | if (fClipPolygon.isEmpty() || !duplicate_pt(point, fClipPolygon[fClipPolygon.count() - 1])) { |
| 1145 | fClipPolygon.push_back(point); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1146 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1147 | } |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1148 | |
| 1149 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1150 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1151 | sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1152 | const SkPoint3& zPlane, bool transparent) { |
Mike Reed | 27575e8 | 2018-05-17 10:11:31 -0400 | [diff] [blame] | 1153 | if (!ctm.mapRect(path.getBounds()).isFinite() || !zPlane.isFinite()) { |
Mike Reed | 9d5c674 | 2018-03-06 10:31:27 -0500 | [diff] [blame] | 1154 | return nullptr; |
| 1155 | } |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1156 | SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1157 | return ambientTess.releaseVertices(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1158 | } |
| 1159 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1160 | sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1161 | const SkPoint3& zPlane, const SkPoint3& lightPos, |
Jim Van Verth | 63f0354 | 2020-12-16 11:56:11 -0500 | [diff] [blame] | 1162 | SkScalar lightRadius, bool transparent, |
| 1163 | bool directional) { |
Mike Reed | 27575e8 | 2018-05-17 10:11:31 -0400 | [diff] [blame] | 1164 | if (!ctm.mapRect(path.getBounds()).isFinite() || !zPlane.isFinite() || |
Jim Van Verth | 1989c49 | 2018-05-31 13:15:16 -0400 | [diff] [blame] | 1165 | !lightPos.isFinite() || !(lightPos.fZ >= SK_ScalarNearlyZero) || |
| 1166 | !SkScalarIsFinite(lightRadius) || !(lightRadius >= SK_ScalarNearlyZero)) { |
Mike Reed | 9d5c674 | 2018-03-06 10:31:27 -0500 | [diff] [blame] | 1167 | return nullptr; |
| 1168 | } |
Jim Van Verth | 63f0354 | 2020-12-16 11:56:11 -0500 | [diff] [blame] | 1169 | SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent, |
| 1170 | directional); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1171 | return spotTess.releaseVertices(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1172 | } |