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