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 | |
Jim Van Verth | 7c8008c | 2018-02-07 15:02:50 -0500 | [diff] [blame] | 387 | if (!this->setTransformedHeightFunc(ctm)) { |
| 388 | return; |
| 389 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 390 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 391 | // Outer ring: 3*numPts |
| 392 | // Middle ring: numPts |
| 393 | fPositions.setReserve(4 * path.countPoints()); |
| 394 | fColors.setReserve(4 * path.countPoints()); |
| 395 | // Outer ring: 12*numPts |
| 396 | // Middle ring: 0 |
| 397 | fIndices.setReserve(12 * path.countPoints()); |
| 398 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 399 | // walk around the path, tessellate and generate outer ring |
| 400 | // if original path is transparent, will accumulate sum of points for centroid |
| 401 | SkPath::Iter iter(path, true); |
| 402 | SkPoint pts[4]; |
| 403 | SkPath::Verb verb; |
| 404 | if (fTransparent) { |
| 405 | *fPositions.push() = SkPoint::Make(0, 0); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 406 | *fColors.push() = fUmbraColor; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 407 | fCentroidCount = 0; |
| 408 | } |
| 409 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 410 | switch (verb) { |
| 411 | case SkPath::kLine_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 412 | this->INHERITED::handleLine(ctm, &pts[1]); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 413 | break; |
| 414 | case SkPath::kQuad_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 415 | this->handleQuad(ctm, pts); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 416 | break; |
| 417 | case SkPath::kCubic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 418 | this->handleCubic(ctm, pts); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 419 | break; |
| 420 | case SkPath::kConic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 421 | this->handleConic(ctm, pts, iter.conicWeight()); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 422 | break; |
| 423 | case SkPath::kMove_Verb: |
| 424 | case SkPath::kClose_Verb: |
| 425 | case SkPath::kDone_Verb: |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 430 | if (!this->indexCount()) { |
| 431 | return; |
| 432 | } |
| 433 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 434 | // Finish up |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 435 | SkVector normal; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 436 | if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) { |
| 437 | SkScalar z = fTransformedHeightFunc(fPrevPoint); |
| 438 | fRadius = this->offset(z); |
| 439 | SkVector scaledNormal(normal); |
| 440 | scaledNormal *= fRadius; |
| 441 | this->addArc(scaledNormal, true); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 442 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 443 | // fix-up the last and first umbra points |
| 444 | SkVector inset = normal; |
| 445 | // adding to an average, so multiply by an additional half |
| 446 | inset *= 0.5f*kInsetFactor; |
| 447 | fPositions[fPrevUmbraIndex] += inset; |
| 448 | fPositions[fFirstVertexIndex] += inset; |
| 449 | // we multiply by another half because now we're adding to an average of an average |
| 450 | inset *= 0.5f; |
| 451 | if (fSplitPreviousEdge) { |
| 452 | fPositions[fPrevUmbraIndex - 2] += inset; |
| 453 | } |
| 454 | if (fSplitFirstEdge) { |
| 455 | fPositions[fFirstVertexIndex + 2] += inset; |
| 456 | } |
| 457 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 458 | // set up for final edge |
| 459 | z = fTransformedHeightFunc(fFirstPoint); |
| 460 | normal *= this->offset(z); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 461 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 462 | // 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] | 463 | if (fColors[fPrevUmbraIndex] != fColors[fFirstVertexIndex] && |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 464 | SkPointPriv::DistanceToSqd(fFirstPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 465 | SkPoint centerPoint = fPositions[fPrevUmbraIndex] + fPositions[fFirstVertexIndex]; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 466 | centerPoint *= 0.5f; |
| 467 | *fPositions.push() = centerPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 468 | *fColors.push() = SkPMLerp(fColors[fFirstVertexIndex], fColors[fPrevUmbraIndex], 128); |
| 469 | centerPoint = fPositions[fPositions.count()-2] + fPositions[fFirstVertexIndex+1]; |
| 470 | centerPoint *= 0.5f; |
| 471 | *fPositions.push() = centerPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 472 | *fColors.push() = fPenumbraColor; |
| 473 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 474 | if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) { |
| 475 | *fIndices.push() = fPrevUmbraIndex; |
| 476 | *fIndices.push() = fPositions.count() - 3; |
| 477 | *fIndices.push() = fPositions.count() - 2; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 478 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 479 | *fIndices.push() = fPositions.count() - 3; |
| 480 | *fIndices.push() = fPositions.count() - 1; |
| 481 | *fIndices.push() = fPositions.count() - 2; |
| 482 | } else { |
| 483 | *fIndices.push() = fPrevUmbraIndex; |
| 484 | *fIndices.push() = fPositions.count() - 2; |
| 485 | *fIndices.push() = fPositions.count() - 1; |
| 486 | |
| 487 | *fIndices.push() = fPrevUmbraIndex; |
| 488 | *fIndices.push() = fPositions.count() - 1; |
| 489 | *fIndices.push() = fPositions.count() - 3; |
| 490 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 491 | |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 492 | // if transparent, add point to first one in array and add to center fan |
| 493 | if (fTransparent) { |
| 494 | fPositions[0] += centerPoint; |
| 495 | ++fCentroidCount; |
| 496 | |
| 497 | *fIndices.push() = 0; |
| 498 | *fIndices.push() = fPrevUmbraIndex; |
| 499 | *fIndices.push() = fPositions.count() - 2; |
| 500 | } |
| 501 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 502 | fPrevUmbraIndex = fPositions.count() - 2; |
| 503 | } |
| 504 | |
| 505 | // final edge |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 506 | *fPositions.push() = fFirstPoint + normal; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 507 | *fColors.push() = fPenumbraColor; |
| 508 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 509 | if (fColors[fPrevUmbraIndex] > fColors[fFirstVertexIndex]) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 510 | *fIndices.push() = fPrevUmbraIndex; |
| 511 | *fIndices.push() = fPositions.count() - 2; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 512 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 513 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 514 | *fIndices.push() = fPositions.count() - 2; |
| 515 | *fIndices.push() = fPositions.count() - 1; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 516 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 517 | } else { |
| 518 | *fIndices.push() = fPrevUmbraIndex; |
| 519 | *fIndices.push() = fPositions.count() - 2; |
| 520 | *fIndices.push() = fPositions.count() - 1; |
| 521 | |
| 522 | *fIndices.push() = fPrevUmbraIndex; |
| 523 | *fIndices.push() = fPositions.count() - 1; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 524 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 525 | } |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 526 | fPrevOutset = normal; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // finalize centroid |
| 530 | if (fTransparent) { |
| 531 | fPositions[0] *= SkScalarFastInvert(fCentroidCount); |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 532 | fColors[0] = this->umbraColor(fTransformedHeightFunc(fPositions[0])); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 533 | |
| 534 | *fIndices.push() = 0; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 535 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 536 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // final fan |
| 540 | if (fPositions.count() >= 3) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 541 | fPrevUmbraIndex = fFirstVertexIndex; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 542 | fPrevPoint = fFirstPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 543 | fRadius = this->offset(fTransformedHeightFunc(fPrevPoint)); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 544 | if (this->addArc(fFirstOutset, false)) { |
| 545 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 546 | *fIndices.push() = fPositions.count() - 1; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 547 | *fIndices.push() = fFirstVertexIndex + 1; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 548 | } else { |
| 549 | // arc is too small, set the first penumbra point to be the same position |
| 550 | // as the last one |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 551 | fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1]; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 552 | } |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 553 | } |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 554 | fSucceeded = true; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 555 | } |
| 556 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 557 | void SkAmbientShadowTessellator::handleLine(const SkPoint& p) { |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 558 | if (fInitPoints.count() < 2) { |
| 559 | *fInitPoints.push() = p; |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | if (fInitPoints.count() == 2) { |
| 564 | // determine if cw or ccw |
| 565 | SkVector v0 = fInitPoints[1] - fInitPoints[0]; |
| 566 | SkVector v1 = p - fInitPoints[0]; |
| 567 | SkScalar perpDot = v0.fX*v1.fY - v0.fY*v1.fX; |
| 568 | if (SkScalarNearlyZero(perpDot)) { |
| 569 | // nearly parallel, just treat as straight line and continue |
| 570 | fInitPoints[1] = p; |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | // if perpDot > 0, winding is ccw |
| 575 | fDirection = (perpDot > 0) ? -1 : 1; |
| 576 | |
| 577 | // add first quad |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 578 | SkVector normal; |
| 579 | if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &normal)) { |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 580 | // first two points are incident, make the third point the second and continue |
| 581 | fInitPoints[1] = p; |
| 582 | return; |
| 583 | } |
| 584 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 585 | fFirstPoint = fInitPoints[0]; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 586 | fFirstVertexIndex = fPositions.count(); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 587 | SkScalar z = fTransformedHeightFunc(fFirstPoint); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 588 | fFirstOutset = normal; |
| 589 | fFirstOutset *= this->offset(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 590 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 591 | fPrevOutset = fFirstOutset; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 592 | fPrevPoint = fFirstPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 593 | fPrevUmbraIndex = fFirstVertexIndex; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 594 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 595 | *fPositions.push() = fFirstPoint; |
| 596 | *fColors.push() = this->umbraColor(z); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 597 | *fPositions.push() = fFirstPoint + fFirstOutset; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 598 | *fColors.push() = fPenumbraColor; |
| 599 | if (fTransparent) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 600 | fPositions[0] += fFirstPoint; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 601 | fCentroidCount = 1; |
| 602 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 603 | |
| 604 | // add the first quad |
| 605 | z = fTransformedHeightFunc(fInitPoints[1]); |
| 606 | fRadius = this->offset(z); |
| 607 | fUmbraColor = this->umbraColor(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 608 | this->addEdge(fInitPoints[1], normal); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 609 | |
| 610 | // to ensure we skip this block next time |
| 611 | *fInitPoints.push() = p; |
| 612 | } |
| 613 | |
| 614 | SkVector normal; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 615 | if (compute_normal(fPrevPoint, p, fDirection, &normal)) { |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 616 | SkVector scaledNormal = normal; |
| 617 | scaledNormal *= fRadius; |
| 618 | this->addArc(scaledNormal, true); |
| 619 | SkScalar z = fTransformedHeightFunc(p); |
| 620 | fRadius = this->offset(z); |
| 621 | fUmbraColor = this->umbraColor(z); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 622 | this->addEdge(p, normal); |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 626 | void SkAmbientShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 627 | // We compute the inset in two stages: first we inset by half the current normal, |
| 628 | // then on the next addEdge() we add half of the next normal to get an average of the two |
| 629 | SkVector insetNormal = nextNormal; |
| 630 | insetNormal *= 0.5f*kInsetFactor; |
| 631 | |
| 632 | // Adding the other half of the average for the previous edge |
| 633 | fPositions[fPrevUmbraIndex] += insetNormal; |
| 634 | |
| 635 | SkPoint umbraPoint = nextPoint + insetNormal; |
| 636 | SkVector outsetNormal = nextNormal; |
| 637 | outsetNormal *= fRadius; |
| 638 | SkPoint penumbraPoint = nextPoint + outsetNormal; |
| 639 | |
| 640 | // For split edges, we're adding an average of two averages, so we multiply by another half |
| 641 | if (fSplitPreviousEdge) { |
| 642 | insetNormal *= 0.5f; |
| 643 | fPositions[fPrevUmbraIndex - 2] += insetNormal; |
| 644 | } |
| 645 | |
| 646 | // 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] | 647 | if (fColors[fPrevUmbraIndex] != fUmbraColor && |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 648 | SkPointPriv::DistanceToSqd(nextPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 649 | |
| 650 | // This is lacking 1/4 of the next inset -- we'll add it the next time we call addEdge() |
| 651 | SkPoint centerPoint = fPositions[fPrevUmbraIndex] + umbraPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 652 | centerPoint *= 0.5f; |
| 653 | *fPositions.push() = centerPoint; |
| 654 | *fColors.push() = SkPMLerp(fUmbraColor, fColors[fPrevUmbraIndex], 128); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 655 | centerPoint = fPositions[fPositions.count()-2] + penumbraPoint; |
| 656 | centerPoint *= 0.5f; |
| 657 | *fPositions.push() = centerPoint; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 658 | *fColors.push() = fPenumbraColor; |
| 659 | |
| 660 | // set triangularization to get best interpolation of color |
| 661 | if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) { |
| 662 | *fIndices.push() = fPrevUmbraIndex; |
| 663 | *fIndices.push() = fPositions.count() - 3; |
| 664 | *fIndices.push() = fPositions.count() - 2; |
| 665 | |
| 666 | *fIndices.push() = fPositions.count() - 3; |
| 667 | *fIndices.push() = fPositions.count() - 1; |
| 668 | *fIndices.push() = fPositions.count() - 2; |
| 669 | } else { |
| 670 | *fIndices.push() = fPrevUmbraIndex; |
| 671 | *fIndices.push() = fPositions.count() - 2; |
| 672 | *fIndices.push() = fPositions.count() - 1; |
| 673 | |
| 674 | *fIndices.push() = fPrevUmbraIndex; |
| 675 | *fIndices.push() = fPositions.count() - 1; |
| 676 | *fIndices.push() = fPositions.count() - 3; |
| 677 | } |
| 678 | |
Jim Van Verth | 1c4c114 | 2017-05-11 10:23:53 -0400 | [diff] [blame] | 679 | // if transparent, add point to first one in array and add to center fan |
| 680 | if (fTransparent) { |
| 681 | fPositions[0] += centerPoint; |
| 682 | ++fCentroidCount; |
| 683 | |
| 684 | *fIndices.push() = 0; |
| 685 | *fIndices.push() = fPrevUmbraIndex; |
| 686 | *fIndices.push() = fPositions.count() - 2; |
| 687 | } |
| 688 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 689 | fSplitPreviousEdge = true; |
| 690 | if (fPrevUmbraIndex == fFirstVertexIndex) { |
| 691 | fSplitFirstEdge = true; |
| 692 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 693 | fPrevUmbraIndex = fPositions.count() - 2; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 694 | } else { |
| 695 | fSplitPreviousEdge = false; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 696 | } |
| 697 | |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 698 | // add next quad |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 699 | *fPositions.push() = umbraPoint; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 700 | *fColors.push() = fUmbraColor; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 701 | *fPositions.push() = penumbraPoint; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 702 | *fColors.push() = fPenumbraColor; |
| 703 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 704 | // set triangularization to get best interpolation of color |
| 705 | if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) { |
| 706 | *fIndices.push() = fPrevUmbraIndex; |
| 707 | *fIndices.push() = fPositions.count() - 3; |
| 708 | *fIndices.push() = fPositions.count() - 2; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 709 | |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 710 | *fIndices.push() = fPositions.count() - 3; |
| 711 | *fIndices.push() = fPositions.count() - 1; |
| 712 | *fIndices.push() = fPositions.count() - 2; |
| 713 | } else { |
| 714 | *fIndices.push() = fPrevUmbraIndex; |
| 715 | *fIndices.push() = fPositions.count() - 2; |
| 716 | *fIndices.push() = fPositions.count() - 1; |
| 717 | |
| 718 | *fIndices.push() = fPrevUmbraIndex; |
| 719 | *fIndices.push() = fPositions.count() - 1; |
| 720 | *fIndices.push() = fPositions.count() - 3; |
| 721 | } |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 722 | |
| 723 | // if transparent, add point to first one in array and add to center fan |
| 724 | if (fTransparent) { |
| 725 | fPositions[0] += nextPoint; |
| 726 | ++fCentroidCount; |
| 727 | |
| 728 | *fIndices.push() = 0; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 729 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 730 | *fIndices.push() = fPositions.count() - 2; |
| 731 | } |
| 732 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 733 | fPrevUmbraIndex = fPositions.count() - 2; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 734 | fPrevPoint = nextPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 735 | fPrevOutset = outsetNormal; |
Jim Van Verth | bce7496 | 2017-01-25 09:39:46 -0500 | [diff] [blame] | 736 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 737 | |
| 738 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 739 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 740 | class SkSpotShadowTessellator : public SkBaseShadowTessellator { |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 741 | public: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 742 | SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 743 | const SkPoint3& zPlaneParams, const SkPoint3& lightPos, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 744 | SkScalar lightRadius, bool transparent); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 745 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 746 | private: |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 747 | void computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 748 | const SkMatrix& shadowTransform); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 749 | void computeClipVectorsAndTestCentroid(); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 750 | bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 751 | int getClosestUmbraPoint(const SkPoint& point); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 752 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 753 | void handleLine(const SkPoint& p) override; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 754 | bool handlePolyPoint(const SkPoint& p); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 755 | |
| 756 | void mapPoints(SkScalar scale, const SkVector& xlate, SkPoint* pts, int count); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 757 | bool addInnerPoint(const SkPoint& pathPoint); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 758 | void addEdge(const SkVector& nextPoint, const SkVector& nextNormal); |
| 759 | |
| 760 | SkScalar offset(SkScalar z) { |
| 761 | float zRatio = SkTPin(z / (fLightZ - z), 0.0f, 0.95f); |
| 762 | return fLightRadius*zRatio; |
| 763 | } |
| 764 | |
| 765 | SkScalar fLightZ; |
| 766 | SkScalar fLightRadius; |
| 767 | SkScalar fOffsetAdjust; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 768 | |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 769 | SkTDArray<SkPoint> fClipPolygon; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 770 | SkTDArray<SkVector> fClipVectors; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 771 | SkPoint fCentroid; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 772 | SkScalar fArea; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 773 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 774 | SkTDArray<SkPoint> fPathPolygon; |
| 775 | SkTDArray<SkPoint> fUmbraPolygon; |
| 776 | int fCurrClipPoint; |
| 777 | int fCurrUmbraPoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 778 | bool fPrevUmbraOutside; |
| 779 | bool fFirstUmbraOutside; |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 780 | bool fValidUmbra; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 781 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 782 | typedef SkBaseShadowTessellator INHERITED; |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 783 | }; |
| 784 | |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 785 | SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 786 | const SkPoint3& zPlaneParams, |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 787 | const SkPoint3& lightPos, SkScalar lightRadius, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 788 | bool transparent) |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 789 | : INHERITED(zPlaneParams, transparent) |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 790 | , fLightZ(lightPos.fZ) |
| 791 | , fLightRadius(lightRadius) |
| 792 | , fOffsetAdjust(0) |
| 793 | , fCurrClipPoint(0) |
| 794 | , fPrevUmbraOutside(false) |
| 795 | , fFirstUmbraOutside(false) |
| 796 | , fValidUmbra(true) { |
| 797 | |
| 798 | // make sure we're not below the canvas plane |
| 799 | if (this->setZOffset(path.getBounds(), ctm.hasPerspective())) { |
| 800 | // Adjust light height and radius |
| 801 | fLightRadius *= (fLightZ + fZOffset) / fLightZ; |
| 802 | fLightZ += fZOffset; |
| 803 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 804 | |
| 805 | // Set radius and colors |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 806 | SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY()); |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 807 | SkScalar occluderHeight = this->heightFunc(center.fX, center.fY) + fZOffset; |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 808 | fUmbraColor = SkColorSetARGB(255, 0, 0, 0); |
| 809 | fPenumbraColor = SkColorSetARGB(0, 0, 0, 0); |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 810 | |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 811 | // Compute the blur radius, scale and translation for the spot shadow. |
| 812 | SkScalar radius; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 813 | SkMatrix shadowTransform; |
| 814 | if (!ctm.hasPerspective()) { |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 815 | SkScalar scale; |
| 816 | SkVector translate; |
| 817 | SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY, fLightZ, |
| 818 | lightRadius, &radius, &scale, &translate); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 819 | shadowTransform.setScaleTranslate(scale, scale, translate.fX, translate.fY); |
| 820 | } else { |
| 821 | // For perspective, we have a scale, a z-shear, and another projective divide -- |
| 822 | // this varies at each point so we can't use an affine transform. |
| 823 | // We'll just apply this to each generated point in turn. |
| 824 | shadowTransform.reset(); |
| 825 | // Also can't cull the center (for now). |
| 826 | fTransparent = true; |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 827 | radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 828 | } |
Jim Van Verth | 1af03d4 | 2017-07-31 09:34:58 -0400 | [diff] [blame] | 829 | fRadius = radius; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 830 | SkMatrix fullTransform = SkMatrix::Concat(shadowTransform, ctm); |
| 831 | |
| 832 | // Set up our reverse mapping |
Jim Van Verth | 7c8008c | 2018-02-07 15:02:50 -0500 | [diff] [blame] | 833 | if (!this->setTransformedHeightFunc(fullTransform)) { |
| 834 | return; |
| 835 | } |
Jim Van Verth | b436655 | 2017-03-27 14:25:29 -0400 | [diff] [blame] | 836 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 837 | // TODO: calculate these reserves better |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 838 | // Penumbra ring: 3*numPts |
| 839 | // Umbra ring: numPts |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 840 | // Inner ring: numPts |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 841 | fPositions.setReserve(5 * path.countPoints()); |
| 842 | fColors.setReserve(5 * path.countPoints()); |
| 843 | // Penumbra ring: 12*numPts |
| 844 | // Umbra ring: 3*numPts |
| 845 | fIndices.setReserve(15 * path.countPoints()); |
Brian Salomon | e7c85c4 | 2017-03-24 16:00:35 +0000 | [diff] [blame] | 846 | fClipPolygon.setReserve(path.countPoints()); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 847 | |
| 848 | // compute rough clip bounds for umbra, plus offset polygon, plus centroid |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 849 | this->computeClipAndPathPolygons(path, ctm, shadowTransform); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 850 | if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3) { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 851 | return; |
| 852 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 853 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 854 | // check to see if umbra collapses |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 855 | SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, fPathPolygon[0], |
| 856 | fPathPolygon[1]); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 857 | SkRect bounds; |
| 858 | bounds.setBounds(&fPathPolygon[0], fPathPolygon.count()); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 859 | for (int i = 1; i < fPathPolygon.count(); ++i) { |
| 860 | int j = i + 1; |
| 861 | if (i == fPathPolygon.count() - 1) { |
| 862 | j = 0; |
| 863 | } |
| 864 | SkPoint currPoint = fPathPolygon[i]; |
| 865 | SkPoint nextPoint = fPathPolygon[j]; |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 866 | SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint, |
| 867 | nextPoint); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 868 | if (distSq < minDistSq) { |
| 869 | minDistSq = distSq; |
| 870 | } |
| 871 | } |
| 872 | static constexpr auto kTolerance = 1.0e-2f; |
| 873 | if (minDistSq < (radius + kTolerance)*(radius + kTolerance)) { |
| 874 | // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha |
| 875 | SkScalar newRadius = SkScalarSqrt(minDistSq) - kTolerance; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 876 | fOffsetAdjust = newRadius - radius; |
Jim Van Verth | b6069df | 2017-04-28 11:00:35 -0400 | [diff] [blame] | 877 | SkScalar ratio = 128 * (newRadius + radius) / radius; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 878 | // they aren't PMColors, but the interpolation algorithm is the same |
| 879 | fUmbraColor = SkPMLerp(fUmbraColor, fPenumbraColor, (unsigned)ratio); |
| 880 | radius = newRadius; |
| 881 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 882 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 883 | // compute vectors for clip tests |
| 884 | this->computeClipVectorsAndTestCentroid(); |
| 885 | |
| 886 | // generate inner ring |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 887 | if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), radius, |
| 888 | &fUmbraPolygon)) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 889 | // this shouldn't happen, but just in case we'll inset using the centroid |
| 890 | fValidUmbra = false; |
| 891 | } |
| 892 | |
| 893 | // 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] | 894 | if (fTransparent) { |
| 895 | *fPositions.push() = fCentroid; |
| 896 | *fColors.push() = fUmbraColor; |
| 897 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 898 | fCurrUmbraPoint = 0; |
| 899 | for (int i = 0; i < fPathPolygon.count(); ++i) { |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 900 | if (!this->handlePolyPoint(fPathPolygon[i])) { |
| 901 | return; |
| 902 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 903 | } |
| 904 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 905 | if (!this->indexCount()) { |
| 906 | return; |
| 907 | } |
| 908 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 909 | // finish up the final verts |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 910 | SkVector normal; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 911 | if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) { |
| 912 | normal *= fRadius; |
| 913 | this->addArc(normal, true); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 914 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 915 | // add to center fan |
| 916 | if (fTransparent) { |
| 917 | *fIndices.push() = 0; |
| 918 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 919 | *fIndices.push() = fFirstVertexIndex; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 920 | // or to clip ring |
| 921 | } else { |
| 922 | if (fFirstUmbraOutside) { |
| 923 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 924 | *fIndices.push() = fFirstVertexIndex; |
| 925 | *fIndices.push() = fFirstVertexIndex + 1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 926 | if (fPrevUmbraOutside) { |
| 927 | // fill out quad |
| 928 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 929 | *fIndices.push() = fFirstVertexIndex + 1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 930 | *fIndices.push() = fPrevUmbraIndex + 1; |
| 931 | } |
| 932 | } else if (fPrevUmbraOutside) { |
| 933 | // add tri |
| 934 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 935 | *fIndices.push() = fFirstVertexIndex; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 936 | *fIndices.push() = fPrevUmbraIndex + 1; |
| 937 | } |
| 938 | } |
| 939 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 940 | // add final edge |
| 941 | *fPositions.push() = fFirstPoint + normal; |
| 942 | *fColors.push() = fPenumbraColor; |
| 943 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 944 | *fIndices.push() = fPrevUmbraIndex; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 945 | *fIndices.push() = fPositions.count() - 2; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 946 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 947 | |
| 948 | *fIndices.push() = fPositions.count() - 2; |
| 949 | *fIndices.push() = fPositions.count() - 1; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 950 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 951 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 952 | fPrevOutset = normal; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // final fan |
| 956 | if (fPositions.count() >= 3) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 957 | fPrevUmbraIndex = fFirstVertexIndex; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 958 | fPrevPoint = fFirstPoint; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 959 | if (this->addArc(fFirstOutset, false)) { |
| 960 | *fIndices.push() = fFirstVertexIndex; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 961 | *fIndices.push() = fPositions.count() - 1; |
| 962 | if (fFirstUmbraOutside) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 963 | *fIndices.push() = fFirstVertexIndex + 2; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 964 | } else { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 965 | *fIndices.push() = fFirstVertexIndex + 1; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 966 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 967 | } else { |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 968 | // no arc added, fix up by setting first penumbra point position to last one |
| 969 | if (fFirstUmbraOutside) { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 970 | fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1]; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 971 | } else { |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 972 | fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1]; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 973 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 974 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 975 | } |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 976 | |
| 977 | if (ctm.hasPerspective()) { |
| 978 | for (int i = 0; i < fPositions.count(); ++i) { |
| 979 | SkScalar pathZ = fTransformedHeightFunc(fPositions[i]); |
| 980 | SkScalar factor = SkScalarInvert(fLightZ - pathZ); |
| 981 | fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor; |
| 982 | fPositions[i].fY = (fPositions[i].fY*fLightZ - lightPos.fY*pathZ)*factor; |
| 983 | } |
| 984 | #ifdef DRAW_CENTROID |
| 985 | SkScalar pathZ = fTransformedHeightFunc(fCentroid); |
| 986 | SkScalar factor = SkScalarInvert(fLightZ - pathZ); |
| 987 | fCentroid.fX = (fCentroid.fX*fLightZ - lightPos.fX*pathZ)*factor; |
| 988 | fCentroid.fY = (fCentroid.fY*fLightZ - lightPos.fY*pathZ)*factor; |
| 989 | #endif |
| 990 | } |
| 991 | #ifdef DRAW_CENTROID |
| 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 | *fPositions.push() = fCentroid + SkVector::Make(-2, 2); |
| 997 | *fColors.push() = SkColorSetARGB(255, 0, 255, 255); |
| 998 | *fPositions.push() = fCentroid + SkVector::Make(2, 2); |
| 999 | *fColors.push() = SkColorSetARGB(255, 0, 255, 255); |
| 1000 | |
| 1001 | *fIndices.push() = fPositions.count() - 4; |
| 1002 | *fIndices.push() = fPositions.count() - 2; |
| 1003 | *fIndices.push() = fPositions.count() - 1; |
| 1004 | |
| 1005 | *fIndices.push() = fPositions.count() - 4; |
| 1006 | *fIndices.push() = fPositions.count() - 1; |
| 1007 | *fIndices.push() = fPositions.count() - 3; |
| 1008 | #endif |
| 1009 | |
Brian Salomon | 0dda9cb | 2017-02-03 10:33:25 -0500 | [diff] [blame] | 1010 | fSucceeded = true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1013 | void SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1014 | const SkMatrix& shadowTransform) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1015 | |
| 1016 | fPathPolygon.setReserve(path.countPoints()); |
| 1017 | |
| 1018 | // Walk around the path and compute clip polygon and path polygon. |
| 1019 | // Will also accumulate sum of areas for centroid. |
| 1020 | // For Bezier curves, we compute additional interior points on curve. |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1021 | SkPath::Iter iter(path, true); |
| 1022 | SkPoint pts[4]; |
| 1023 | SkPath::Verb verb; |
| 1024 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1025 | fClipPolygon.reset(); |
| 1026 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1027 | // init centroid |
| 1028 | fCentroid = SkPoint::Make(0, 0); |
| 1029 | fArea = 0; |
| 1030 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1031 | // coefficients to compute cubic Bezier at t = 5/16 |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1032 | static constexpr SkScalar kA = 0.32495117187f; |
| 1033 | static constexpr SkScalar kB = 0.44311523437f; |
| 1034 | static constexpr SkScalar kC = 0.20141601562f; |
| 1035 | static constexpr SkScalar kD = 0.03051757812f; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1036 | |
| 1037 | SkPoint curvePoint; |
| 1038 | SkScalar w; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1039 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
| 1040 | switch (verb) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1041 | case SkPath::kLine_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1042 | ctm.mapPoints(&pts[1], 1); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1043 | *fClipPolygon.push() = pts[1]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1044 | this->INHERITED::handleLine(shadowTransform, &pts[1]); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1045 | break; |
| 1046 | case SkPath::kQuad_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1047 | ctm.mapPoints(pts, 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1048 | // point at t = 1/2 |
| 1049 | curvePoint.fX = 0.25f*pts[0].fX + 0.5f*pts[1].fX + 0.25f*pts[2].fX; |
| 1050 | curvePoint.fY = 0.25f*pts[0].fY + 0.5f*pts[1].fY + 0.25f*pts[2].fY; |
| 1051 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1052 | *fClipPolygon.push() = pts[2]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1053 | this->handleQuad(shadowTransform, pts); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1054 | break; |
| 1055 | case SkPath::kConic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1056 | ctm.mapPoints(pts, 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1057 | w = iter.conicWeight(); |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1058 | // point at t = 1/2 |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1059 | curvePoint.fX = 0.25f*pts[0].fX + w*0.5f*pts[1].fX + 0.25f*pts[2].fX; |
| 1060 | curvePoint.fY = 0.25f*pts[0].fY + w*0.5f*pts[1].fY + 0.25f*pts[2].fY; |
| 1061 | curvePoint *= SkScalarInvert(0.5f + 0.5f*w); |
| 1062 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1063 | *fClipPolygon.push() = pts[2]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1064 | this->handleConic(shadowTransform, pts, w); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1065 | break; |
| 1066 | case SkPath::kCubic_Verb: |
Jim Van Verth | a84898d | 2017-02-06 13:38:23 -0500 | [diff] [blame] | 1067 | ctm.mapPoints(pts, 4); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1068 | // point at t = 5/16 |
| 1069 | curvePoint.fX = kA*pts[0].fX + kB*pts[1].fX + kC*pts[2].fX + kD*pts[3].fX; |
| 1070 | curvePoint.fY = kA*pts[0].fY + kB*pts[1].fY + kC*pts[2].fY + kD*pts[3].fY; |
| 1071 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1072 | // point at t = 11/16 |
| 1073 | curvePoint.fX = kD*pts[0].fX + kC*pts[1].fX + kB*pts[2].fX + kA*pts[3].fX; |
| 1074 | curvePoint.fY = kD*pts[0].fY + kC*pts[1].fY + kB*pts[2].fY + kA*pts[3].fY; |
| 1075 | *fClipPolygon.push() = curvePoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1076 | *fClipPolygon.push() = pts[3]; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1077 | this->handleCubic(shadowTransform, pts); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1078 | break; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1079 | case SkPath::kMove_Verb: |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1080 | case SkPath::kClose_Verb: |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1081 | case SkPath::kDone_Verb: |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1082 | break; |
| 1083 | default: |
| 1084 | SkDEBUGFAIL("unknown verb"); |
| 1085 | } |
| 1086 | } |
| 1087 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1088 | // finish centroid |
| 1089 | if (fPathPolygon.count() > 0) { |
| 1090 | SkPoint currPoint = fPathPolygon[fPathPolygon.count() - 1]; |
| 1091 | SkPoint nextPoint = fPathPolygon[0]; |
| 1092 | SkScalar quadArea = currPoint.cross(nextPoint); |
| 1093 | fCentroid.fX += (currPoint.fX + nextPoint.fX) * quadArea; |
| 1094 | fCentroid.fY += (currPoint.fY + nextPoint.fY) * quadArea; |
| 1095 | fArea += quadArea; |
| 1096 | fCentroid *= SK_Scalar1 / (3 * fArea); |
| 1097 | } |
| 1098 | |
| 1099 | fCurrClipPoint = fClipPolygon.count() - 1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1100 | } |
| 1101 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1102 | void SkSpotShadowTessellator::computeClipVectorsAndTestCentroid() { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1103 | SkASSERT(fClipPolygon.count() >= 3); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1104 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1105 | // init clip vectors |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1106 | SkVector v0 = fClipPolygon[1] - fClipPolygon[0]; |
| 1107 | *fClipVectors.push() = v0; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1108 | |
| 1109 | // init centroid check |
| 1110 | bool hiddenCentroid = true; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1111 | SkVector v1 = fCentroid - fClipPolygon[0]; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1112 | SkScalar initCross = v0.cross(v1); |
| 1113 | |
| 1114 | for (int p = 1; p < fClipPolygon.count(); ++p) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1115 | // add to clip vectors |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1116 | v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p]; |
| 1117 | *fClipVectors.push() = v0; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1118 | // Determine if transformed centroid is inside clipPolygon. |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1119 | v1 = fCentroid - fClipPolygon[p]; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1120 | if (initCross*v0.cross(v1) <= 0) { |
| 1121 | hiddenCentroid = false; |
| 1122 | } |
| 1123 | } |
| 1124 | SkASSERT(fClipVectors.count() == fClipPolygon.count()); |
| 1125 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1126 | fTransparent = fTransparent || !hiddenCentroid; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | bool SkSpotShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, |
| 1130 | SkPoint* clipPoint) { |
| 1131 | SkVector segmentVector = centroid - umbraPoint; |
| 1132 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1133 | int startClipPoint = fCurrClipPoint; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1134 | do { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1135 | SkVector dp = umbraPoint - fClipPolygon[fCurrClipPoint]; |
| 1136 | SkScalar denom = fClipVectors[fCurrClipPoint].cross(segmentVector); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1137 | SkScalar t_num = dp.cross(segmentVector); |
| 1138 | // if line segments are nearly parallel |
| 1139 | if (SkScalarNearlyZero(denom)) { |
| 1140 | // and collinear |
| 1141 | if (SkScalarNearlyZero(t_num)) { |
| 1142 | return false; |
| 1143 | } |
| 1144 | // otherwise are separate, will try the next poly segment |
| 1145 | // else if crossing lies within poly segment |
| 1146 | } else if (t_num >= 0 && t_num <= denom) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1147 | SkScalar s_num = dp.cross(fClipVectors[fCurrClipPoint]); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1148 | // if umbra point is inside the clip polygon |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1149 | if (s_num >= 0 && s_num <= denom) { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1150 | segmentVector *= s_num/denom; |
| 1151 | *clipPoint = umbraPoint + segmentVector; |
| 1152 | return true; |
| 1153 | } |
| 1154 | } |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1155 | fCurrClipPoint = (fCurrClipPoint + 1) % fClipPolygon.count(); |
| 1156 | } while (fCurrClipPoint != startClipPoint); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1157 | |
| 1158 | return false; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1161 | int SkSpotShadowTessellator::getClosestUmbraPoint(const SkPoint& p) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1162 | SkScalar minDistance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[fCurrUmbraPoint]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1163 | int index = fCurrUmbraPoint; |
| 1164 | int dir = 1; |
| 1165 | int next = (index + dir) % fUmbraPolygon.count(); |
| 1166 | |
| 1167 | // init travel direction |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1168 | SkScalar distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1169 | if (distance < minDistance) { |
| 1170 | index = next; |
| 1171 | minDistance = distance; |
| 1172 | } else { |
| 1173 | dir = fUmbraPolygon.count()-1; |
| 1174 | } |
| 1175 | |
| 1176 | // iterate until we find a point that increases the distance |
| 1177 | next = (index + dir) % fUmbraPolygon.count(); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1178 | distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1179 | while (distance < minDistance) { |
| 1180 | index = next; |
| 1181 | minDistance = distance; |
| 1182 | next = (index + dir) % fUmbraPolygon.count(); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1183 | distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | fCurrUmbraPoint = index; |
| 1187 | return index; |
| 1188 | } |
| 1189 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 1190 | void SkSpotShadowTessellator::mapPoints(SkScalar scale, const SkVector& xlate, |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1191 | SkPoint* pts, int count) { |
| 1192 | // TODO: vectorize |
| 1193 | for (int i = 0; i < count; ++i) { |
| 1194 | pts[i] *= scale; |
| 1195 | pts[i] += xlate; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1199 | static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) { |
| 1200 | static constexpr SkScalar kClose = (SK_Scalar1 / 16); |
| 1201 | static constexpr SkScalar kCloseSqd = kClose*kClose; |
| 1202 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1203 | SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1204 | return distSq < kCloseSqd; |
| 1205 | } |
| 1206 | |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1207 | 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] | 1208 | SkVector v0 = p1 - p0; |
| 1209 | SkVector v1 = p2 - p0; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1210 | return v0.cross(v1); |
| 1211 | } |
| 1212 | |
| 1213 | static bool is_collinear(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
| 1214 | return (SkScalarNearlyZero(perp_dot(p0, p1, p2))); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 1217 | void SkSpotShadowTessellator::handleLine(const SkPoint& p) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1218 | // remove coincident points and add to centroid |
| 1219 | if (fPathPolygon.count() > 0) { |
| 1220 | const SkPoint& lastPoint = fPathPolygon[fPathPolygon.count() - 1]; |
| 1221 | if (duplicate_pt(p, lastPoint)) { |
| 1222 | return; |
| 1223 | } |
| 1224 | SkScalar quadArea = lastPoint.cross(p); |
| 1225 | fCentroid.fX += (p.fX + lastPoint.fX) * quadArea; |
| 1226 | fCentroid.fY += (p.fY + lastPoint.fY) * quadArea; |
| 1227 | fArea += quadArea; |
| 1228 | } |
| 1229 | |
| 1230 | // try to remove collinear points |
| 1231 | if (fPathPolygon.count() > 1 && is_collinear(fPathPolygon[fPathPolygon.count()-2], |
| 1232 | fPathPolygon[fPathPolygon.count()-1], |
| 1233 | p)) { |
| 1234 | fPathPolygon[fPathPolygon.count() - 1] = p; |
| 1235 | } else { |
| 1236 | *fPathPolygon.push() = p; |
| 1237 | } |
| 1238 | } |
| 1239 | |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1240 | bool SkSpotShadowTessellator::handlePolyPoint(const SkPoint& p) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1241 | if (fInitPoints.count() < 2) { |
| 1242 | *fInitPoints.push() = p; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1243 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | if (fInitPoints.count() == 2) { |
| 1247 | // determine if cw or ccw |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1248 | SkScalar perpDot = perp_dot(fInitPoints[0], fInitPoints[1], p); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1249 | if (SkScalarNearlyZero(perpDot)) { |
| 1250 | // nearly parallel, just treat as straight line and continue |
| 1251 | fInitPoints[1] = p; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1252 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | // if perpDot > 0, winding is ccw |
| 1256 | fDirection = (perpDot > 0) ? -1 : 1; |
| 1257 | |
| 1258 | // add first quad |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1259 | if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &fFirstOutset)) { |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1260 | // first two points are incident, make the third point the second and continue |
| 1261 | fInitPoints[1] = p; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1262 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1263 | } |
| 1264 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1265 | fFirstOutset *= fRadius; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1266 | fFirstPoint = fInitPoints[0]; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1267 | fFirstVertexIndex = fPositions.count(); |
| 1268 | fPrevOutset = fFirstOutset; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1269 | fPrevPoint = fFirstPoint; |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 1270 | fPrevUmbraIndex = -1; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1271 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1272 | this->addInnerPoint(fFirstPoint); |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1273 | fPrevUmbraIndex = fFirstVertexIndex; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1274 | |
| 1275 | if (!fTransparent) { |
| 1276 | SkPoint clipPoint; |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1277 | bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex], |
| 1278 | fCentroid, &clipPoint); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1279 | if (isOutside) { |
| 1280 | *fPositions.push() = clipPoint; |
| 1281 | *fColors.push() = fUmbraColor; |
| 1282 | } |
| 1283 | fPrevUmbraOutside = isOutside; |
| 1284 | fFirstUmbraOutside = isOutside; |
| 1285 | } |
| 1286 | |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1287 | SkPoint newPoint = fFirstPoint + fFirstOutset; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1288 | *fPositions.push() = newPoint; |
| 1289 | *fColors.push() = fPenumbraColor; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1290 | this->addEdge(fInitPoints[1], fFirstOutset); |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1291 | |
| 1292 | // to ensure we skip this block next time |
| 1293 | *fInitPoints.push() = p; |
| 1294 | } |
| 1295 | |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1296 | // if concave, abort |
| 1297 | SkScalar perpDot = perp_dot(fInitPoints[1], fInitPoints[2], p); |
| 1298 | if (fDirection*perpDot > 0) { |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1302 | SkVector normal; |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 1303 | if (compute_normal(fPrevPoint, p, fDirection, &normal)) { |
| 1304 | normal *= fRadius; |
| 1305 | this->addArc(normal, true); |
| 1306 | this->addEdge(p, normal); |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1307 | fInitPoints[1] = fInitPoints[2]; |
| 1308 | fInitPoints[2] = p; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1309 | } |
Jim Van Verth | b55eb28 | 2017-07-18 14:13:45 -0400 | [diff] [blame] | 1310 | |
| 1311 | return true; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1312 | } |
| 1313 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1314 | bool SkSpotShadowTessellator::addInnerPoint(const SkPoint& pathPoint) { |
| 1315 | SkPoint umbraPoint; |
| 1316 | if (!fValidUmbra) { |
| 1317 | SkVector v = fCentroid - pathPoint; |
| 1318 | v *= 0.95f; |
| 1319 | umbraPoint = pathPoint + v; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1320 | } else { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1321 | umbraPoint = fUmbraPolygon[this->getClosestUmbraPoint(pathPoint)]; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1322 | } |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1323 | |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1324 | fPrevPoint = pathPoint; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1325 | |
| 1326 | // merge "close" points |
Jim Van Verth | e7e1d9d | 2017-05-01 16:06:48 -0400 | [diff] [blame] | 1327 | if (fPrevUmbraIndex == -1 || |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1328 | !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) { |
| 1329 | *fPositions.push() = umbraPoint; |
| 1330 | *fColors.push() = fUmbraColor; |
| 1331 | |
| 1332 | return false; |
| 1333 | } else { |
| 1334 | return true; |
| 1335 | } |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1336 | } |
| 1337 | |
Jim Van Verth | efe3ded | 2017-01-30 13:11:45 -0500 | [diff] [blame] | 1338 | void SkSpotShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1339 | // add next umbra point |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1340 | bool duplicate = this->addInnerPoint(nextPoint); |
| 1341 | int prevPenumbraIndex = duplicate ? fPositions.count()-1 : fPositions.count()-2; |
| 1342 | int currUmbraIndex = duplicate ? fPrevUmbraIndex : fPositions.count()-1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1343 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1344 | if (!duplicate) { |
| 1345 | // add to center fan if transparent or centroid showing |
| 1346 | if (fTransparent) { |
| 1347 | *fIndices.push() = 0; |
| 1348 | *fIndices.push() = fPrevUmbraIndex; |
| 1349 | *fIndices.push() = currUmbraIndex; |
| 1350 | // otherwise add to clip ring |
| 1351 | } else { |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1352 | SkPoint clipPoint; |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1353 | bool isOutside = this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid, |
| 1354 | &clipPoint); |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1355 | if (isOutside) { |
| 1356 | *fPositions.push() = clipPoint; |
| 1357 | *fColors.push() = fUmbraColor; |
| 1358 | |
| 1359 | *fIndices.push() = fPrevUmbraIndex; |
| 1360 | *fIndices.push() = currUmbraIndex; |
| 1361 | *fIndices.push() = currUmbraIndex + 1; |
| 1362 | if (fPrevUmbraOutside) { |
| 1363 | // fill out quad |
| 1364 | *fIndices.push() = fPrevUmbraIndex; |
| 1365 | *fIndices.push() = currUmbraIndex + 1; |
| 1366 | *fIndices.push() = fPrevUmbraIndex + 1; |
| 1367 | } |
| 1368 | } else if (fPrevUmbraOutside) { |
| 1369 | // add tri |
| 1370 | *fIndices.push() = fPrevUmbraIndex; |
| 1371 | *fIndices.push() = currUmbraIndex; |
| 1372 | *fIndices.push() = fPrevUmbraIndex + 1; |
| 1373 | } |
| 1374 | fPrevUmbraOutside = isOutside; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | // add next penumbra point and quad |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1379 | SkPoint newPoint = nextPoint + nextNormal; |
| 1380 | *fPositions.push() = newPoint; |
| 1381 | *fColors.push() = fPenumbraColor; |
| 1382 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 1383 | if (!duplicate) { |
| 1384 | *fIndices.push() = fPrevUmbraIndex; |
| 1385 | *fIndices.push() = prevPenumbraIndex; |
| 1386 | *fIndices.push() = currUmbraIndex; |
| 1387 | } |
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 | *fIndices.push() = prevPenumbraIndex; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1390 | *fIndices.push() = fPositions.count() - 1; |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1391 | *fIndices.push() = currUmbraIndex; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1392 | |
Brian Salomon | 66085ed | 2017-02-02 15:39:34 -0500 | [diff] [blame] | 1393 | fPrevUmbraIndex = currUmbraIndex; |
Jim Van Verth | 7638785 | 2017-05-16 16:55:16 -0400 | [diff] [blame] | 1394 | fPrevOutset = nextNormal; |
Jim Van Verth | 91af727 | 2017-01-27 14:15:54 -0500 | [diff] [blame] | 1395 | } |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1396 | |
| 1397 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1398 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1399 | sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1400 | const SkPoint3& zPlane, bool transparent) { |
| 1401 | SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1402 | return ambientTess.releaseVertices(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1403 | } |
| 1404 | |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1405 | sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm, |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1406 | const SkPoint3& zPlane, const SkPoint3& lightPos, |
Jim Van Verth | 060d982 | 2017-05-04 09:58:17 -0400 | [diff] [blame] | 1407 | SkScalar lightRadius, bool transparent) { |
Jim Van Verth | e308a12 | 2017-05-08 14:19:30 -0400 | [diff] [blame] | 1408 | SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent); |
Brian Salomon | aff27a2 | 2017-02-06 15:47:44 -0500 | [diff] [blame] | 1409 | return spotTess.releaseVertices(); |
Brian Salomon | 958fbc4 | 2017-01-30 17:01:28 -0500 | [diff] [blame] | 1410 | } |