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