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