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