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