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