blob: 57bf5851fa7edf9523be7a66ab70fbcb2225ab02 [file] [log] [blame]
Jim Van Verthbce74962017-01-25 09:39:46 -05001/*
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 Verthefe3ded2017-01-30 13:11:45 -05008#include "SkShadowTessellator.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
Jim Van Verth1af03d42017-07-31 09:34:58 -040010#include "SkDrawShadowInfo.h"
Jim Van Verth58abc9e2017-01-25 11:05:01 -050011#include "SkGeometry.h"
Brian Salomonab664fa2017-03-24 16:07:20 +000012#include "SkInsetConvexPolygon.h"
Jim Van Verth85dc96b2017-01-30 14:49:21 -050013#include "SkPath.h"
Mike Reed54518ac2017-07-22 08:29:48 -040014#include "SkPoint3.h"
Cary Clarkdf429f32017-11-08 11:44:31 -050015#include "SkPointPriv.h"
Brian Salomonaff27a22017-02-06 15:47:44 -050016#include "SkVertices.h"
Jim Van Verth85dc96b2017-01-30 14:49:21 -050017
18#if SK_SUPPORT_GPU
19#include "GrPathUtils.h"
20#endif
Jim Van Verth58abc9e2017-01-25 11:05:01 -050021
Brian Salomon958fbc42017-01-30 17:01:28 -050022
Jim Van Vertha84898d2017-02-06 13:38:23 -050023/**
24 * Base class
25 */
Brian Salomonaff27a22017-02-06 15:47:44 -050026class SkBaseShadowTessellator {
Brian Salomon958fbc42017-01-30 17:01:28 -050027public:
Jim Van Verthe308a122017-05-08 14:19:30 -040028 SkBaseShadowTessellator(const SkPoint3& zPlaneParams, bool transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -050029 virtual ~SkBaseShadowTessellator() {}
Brian Salomon958fbc42017-01-30 17:01:28 -050030
Brian Salomonaff27a22017-02-06 15:47:44 -050031 sk_sp<SkVertices> releaseVertices() {
32 if (!fSucceeded) {
33 return nullptr;
34 }
Mike Reed887cdf12017-04-03 11:11:09 -040035 return SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, this->vertexCount(),
Mike Reed97eb4fe2017-03-14 12:04:16 -040036 fPositions.begin(), nullptr, fColors.begin(),
37 this->indexCount(), fIndices.begin());
Brian Salomon1a8b79a2017-01-31 11:26:26 -050038 }
Brian Salomon958fbc42017-01-30 17:01:28 -050039
Jim Van Vertha84898d2017-02-06 13:38:23 -050040protected:
Jim Van Verthda965502017-04-11 15:29:14 -040041 static constexpr auto kMinHeight = 0.1f;
42
Brian Salomonaff27a22017-02-06 15:47:44 -050043 int vertexCount() const { return fPositions.count(); }
44 int indexCount() const { return fIndices.count(); }
45
Jim Van Verthda965502017-04-11 15:29:14 -040046 bool setZOffset(const SkRect& bounds, bool perspective);
47
Jim Van Vertha84898d2017-02-06 13:38:23 -050048 virtual void handleLine(const SkPoint& p) = 0;
49 void handleLine(const SkMatrix& m, SkPoint* p);
Brian Salomon958fbc42017-01-30 17:01:28 -050050
51 void handleQuad(const SkPoint pts[3]);
Jim Van Vertha84898d2017-02-06 13:38:23 -050052 void handleQuad(const SkMatrix& m, SkPoint pts[3]);
Brian Salomon958fbc42017-01-30 17:01:28 -050053
Jim Van Vertha84898d2017-02-06 13:38:23 -050054 void handleCubic(const SkMatrix& m, SkPoint pts[4]);
Brian Salomon958fbc42017-01-30 17:01:28 -050055
Jim Van Vertha84898d2017-02-06 13:38:23 -050056 void handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w);
Brian Salomon958fbc42017-01-30 17:01:28 -050057
Jim Van Verthda965502017-04-11 15:29:14 -040058 bool setTransformedHeightFunc(const SkMatrix& ctm);
Brian Salomon958fbc42017-01-30 17:01:28 -050059
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -040060 bool addArc(const SkVector& nextNormal, bool finishArc);
Jim Van Verthb4366552017-03-27 14:25:29 -040061
Jim Van Verthe308a122017-05-08 14:19:30 -040062 SkScalar heightFunc(SkScalar x, SkScalar y) {
63 return fZPlaneParams.fX*x + fZPlaneParams.fY*y + fZPlaneParams.fZ;
64 }
65
66 SkPoint3 fZPlaneParams;
Jim Van Verthda965502017-04-11 15:29:14 -040067 std::function<SkScalar(const SkPoint&)> fTransformedHeightFunc;
68 SkScalar fZOffset;
69 // members for perspective height function
Jim Van Verth4c9b8932017-05-15 13:49:21 -040070 SkPoint3 fTransformedZParams;
Jim Van Verthda965502017-04-11 15:29:14 -040071 SkScalar fPartialDeterminants[3];
72
73 // first two points
Brian Salomon958fbc42017-01-30 17:01:28 -050074 SkTDArray<SkPoint> fInitPoints;
75 // temporary buffer
76 SkTDArray<SkPoint> fPointBuffer;
Brian Salomon0dda9cb2017-02-03 10:33:25 -050077
Jim Van Vertha84898d2017-02-06 13:38:23 -050078 SkTDArray<SkPoint> fPositions;
79 SkTDArray<SkColor> fColors;
80 SkTDArray<uint16_t> fIndices;
81
Jim Van Verth76387852017-05-16 16:55:16 -040082 int fFirstVertexIndex;
83 SkVector fFirstOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -050084 SkPoint fFirstPoint;
85
Brian Salomon0dda9cb2017-02-03 10:33:25 -050086 bool fSucceeded;
Jim Van Vertha84898d2017-02-06 13:38:23 -050087 bool fTransparent;
88
89 SkColor fUmbraColor;
90 SkColor fPenumbraColor;
91
92 SkScalar fRadius;
93 SkScalar fDirection;
94 int fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -040095 SkVector fPrevOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -050096 SkPoint fPrevPoint;
Brian Salomon958fbc42017-01-30 17:01:28 -050097};
98
Jim Van Verthda965502017-04-11 15:29:14 -040099static bool compute_normal(const SkPoint& p0, const SkPoint& p1, SkScalar dir,
Jim Van Verthbce74962017-01-25 09:39:46 -0500100 SkVector* newNormal) {
101 SkVector normal;
102 // compute perpendicular
103 normal.fX = p0.fY - p1.fY;
104 normal.fY = p1.fX - p0.fX;
Jim Van Verthda965502017-04-11 15:29:14 -0400105 normal *= dir;
Jim Van Verthbce74962017-01-25 09:39:46 -0500106 if (!normal.normalize()) {
107 return false;
108 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500109 *newNormal = normal;
110 return true;
111}
112
113static void compute_radial_steps(const SkVector& v1, const SkVector& v2, SkScalar r,
114 SkScalar* rotSin, SkScalar* rotCos, int* n) {
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400115 const SkScalar kRecipPixelsPerArcSegment = 0.125f;
Jim Van Verthbce74962017-01-25 09:39:46 -0500116
117 SkScalar rCos = v1.dot(v2);
118 SkScalar rSin = v1.cross(v2);
119 SkScalar theta = SkScalarATan2(rSin, rCos);
120
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400121 int steps = SkScalarFloorToInt(r*theta*kRecipPixelsPerArcSegment);
Jim Van Verthbce74962017-01-25 09:39:46 -0500122
123 SkScalar dTheta = theta / steps;
124 *rotSin = SkScalarSinCos(dTheta, rotCos);
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400125 *n = steps;
Jim Van Verthbce74962017-01-25 09:39:46 -0500126}
127
Jim Van Verthe308a122017-05-08 14:19:30 -0400128SkBaseShadowTessellator::SkBaseShadowTessellator(const SkPoint3& zPlaneParams, bool transparent)
129 : fZPlaneParams(zPlaneParams)
Jim Van Verthda965502017-04-11 15:29:14 -0400130 , fZOffset(0)
Jim Van Verth76387852017-05-16 16:55:16 -0400131 , fFirstVertexIndex(-1)
Brian Salomonaff27a22017-02-06 15:47:44 -0500132 , fSucceeded(false)
133 , fTransparent(transparent)
Brian Salomonaff27a22017-02-06 15:47:44 -0500134 , fDirection(1)
135 , fPrevUmbraIndex(-1) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500136 fInitPoints.setReserve(3);
137
138 // child classes will set reserve for positions, colors and indices
139}
140
Jim Van Verthda965502017-04-11 15:29:14 -0400141bool SkBaseShadowTessellator::setZOffset(const SkRect& bounds, bool perspective) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400142 SkScalar minZ = this->heightFunc(bounds.fLeft, bounds.fTop);
Jim Van Verthda965502017-04-11 15:29:14 -0400143 if (perspective) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400144 SkScalar z = this->heightFunc(bounds.fLeft, bounds.fBottom);
Jim Van Verthda965502017-04-11 15:29:14 -0400145 if (z < minZ) {
146 minZ = z;
147 }
Jim Van Verthe308a122017-05-08 14:19:30 -0400148 z = this->heightFunc(bounds.fRight, bounds.fTop);
Jim Van Verthda965502017-04-11 15:29:14 -0400149 if (z < minZ) {
150 minZ = z;
151 }
Jim Van Verthe308a122017-05-08 14:19:30 -0400152 z = this->heightFunc(bounds.fRight, bounds.fBottom);
Jim Van Verthda965502017-04-11 15:29:14 -0400153 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 Vertha84898d2017-02-06 13:38:23 -0500166// tesselation tolerance values, in device space pixels
Mike Kleinb8b51e62017-02-09 15:22:53 -0500167#if SK_SUPPORT_GPU
Jim Van Vertha84898d2017-02-06 13:38:23 -0500168static const SkScalar kQuadTolerance = 0.2f;
169static const SkScalar kCubicTolerance = 0.2f;
Mike Kleinb8b51e62017-02-09 15:22:53 -0500170#endif
Jim Van Vertha84898d2017-02-06 13:38:23 -0500171static const SkScalar kConicTolerance = 0.5f;
172
Brian Salomonaff27a22017-02-06 15:47:44 -0500173void SkBaseShadowTessellator::handleLine(const SkMatrix& m, SkPoint* p) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500174 m.mapPoints(p, 1);
175 this->handleLine(*p);
176}
177
Brian Salomonaff27a22017-02-06 15:47:44 -0500178void SkBaseShadowTessellator::handleQuad(const SkPoint pts[3]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500179#if SK_SUPPORT_GPU
Jim Van Vertha947e292018-02-26 13:54:34 -0500180 // check for degeneracy
181 SkVector v0 = pts[1] - pts[0];
182 SkVector v1 = pts[2] - pts[0];
183 if (SkScalarNearlyZero(v0.cross(v1))) {
184 return;
185 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500186 // TODO: Pull PathUtils out of Ganesh?
187 int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance);
188 fPointBuffer.setReserve(maxCount);
189 SkPoint* target = fPointBuffer.begin();
190 int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
191 kQuadTolerance, &target, maxCount);
192 fPointBuffer.setCount(count);
193 for (int i = 0; i < count; i++) {
194 this->handleLine(fPointBuffer[i]);
195 }
196#else
197 // for now, just to draw something
198 this->handleLine(pts[1]);
199 this->handleLine(pts[2]);
200#endif
201}
202
Brian Salomonaff27a22017-02-06 15:47:44 -0500203void SkBaseShadowTessellator::handleQuad(const SkMatrix& m, SkPoint pts[3]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500204 m.mapPoints(pts, 3);
205 this->handleQuad(pts);
206}
207
Brian Salomonaff27a22017-02-06 15:47:44 -0500208void SkBaseShadowTessellator::handleCubic(const SkMatrix& m, SkPoint pts[4]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500209 m.mapPoints(pts, 4);
210#if SK_SUPPORT_GPU
211 // TODO: Pull PathUtils out of Ganesh?
212 int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance);
213 fPointBuffer.setReserve(maxCount);
214 SkPoint* target = fPointBuffer.begin();
215 int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
216 kCubicTolerance, &target, maxCount);
217 fPointBuffer.setCount(count);
218 for (int i = 0; i < count; i++) {
219 this->handleLine(fPointBuffer[i]);
220 }
221#else
222 // for now, just to draw something
223 this->handleLine(pts[1]);
224 this->handleLine(pts[2]);
225 this->handleLine(pts[3]);
226#endif
227}
228
Brian Salomonaff27a22017-02-06 15:47:44 -0500229void SkBaseShadowTessellator::handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w) {
Jim Van Verthda965502017-04-11 15:29:14 -0400230 if (m.hasPerspective()) {
231 w = SkConic::TransformW(pts, w, m);
232 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500233 m.mapPoints(pts, 3);
234 SkAutoConicToQuads quadder;
235 const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance);
236 SkPoint lastPoint = *(quads++);
237 int count = quadder.countQuads();
238 for (int i = 0; i < count; ++i) {
239 SkPoint quadPts[3];
240 quadPts[0] = lastPoint;
241 quadPts[1] = quads[0];
242 quadPts[2] = i == count - 1 ? pts[2] : quads[1];
243 this->handleQuad(quadPts);
244 lastPoint = quadPts[2];
245 quads += 2;
246 }
247}
248
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400249bool SkBaseShadowTessellator::addArc(const SkVector& nextNormal, bool finishArc) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500250 // fill in fan from previous quad
251 SkScalar rotSin, rotCos;
252 int numSteps;
Jim Van Verth76387852017-05-16 16:55:16 -0400253 compute_radial_steps(fPrevOutset, nextNormal, fRadius, &rotSin, &rotCos, &numSteps);
254 SkVector prevNormal = fPrevOutset;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400255 for (int i = 0; i < numSteps-1; ++i) {
Jim Van Verthda965502017-04-11 15:29:14 -0400256 SkVector currNormal;
257 currNormal.fX = prevNormal.fX*rotCos - prevNormal.fY*rotSin;
258 currNormal.fY = prevNormal.fY*rotCos + prevNormal.fX*rotSin;
259 *fPositions.push() = fPrevPoint + currNormal;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500260 *fColors.push() = fPenumbraColor;
261 *fIndices.push() = fPrevUmbraIndex;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500262 *fIndices.push() = fPositions.count() - 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400263 *fIndices.push() = fPositions.count() - 2;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500264
Jim Van Verthda965502017-04-11 15:29:14 -0400265 prevNormal = currNormal;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500266 }
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400267 if (finishArc && numSteps) {
Jim Van Verthda965502017-04-11 15:29:14 -0400268 *fPositions.push() = fPrevPoint + nextNormal;
269 *fColors.push() = fPenumbraColor;
270 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400271 *fIndices.push() = fPositions.count() - 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400272 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthda965502017-04-11 15:29:14 -0400273 }
Jim Van Verth76387852017-05-16 16:55:16 -0400274 fPrevOutset = nextNormal;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400275
276 return (numSteps > 0);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500277}
278
Jim Van Verthda965502017-04-11 15:29:14 -0400279bool SkBaseShadowTessellator::setTransformedHeightFunc(const SkMatrix& ctm) {
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400280 if (SkScalarNearlyZero(fZPlaneParams.fX) && SkScalarNearlyZero(fZPlaneParams.fY)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400281 fTransformedHeightFunc = [this](const SkPoint& p) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400282 return fZPlaneParams.fZ;
Jim Van Verthda965502017-04-11 15:29:14 -0400283 };
284 } else {
285 SkMatrix ctmInverse;
Jim Van Vertha947e292018-02-26 13:54:34 -0500286 if (!ctm.invert(&ctmInverse) || !ctmInverse.isFinite()) {
Jim Van Verthda965502017-04-11 15:29:14 -0400287 return false;
288 }
Jim Van Verthda965502017-04-11 15:29:14 -0400289 // multiply by transpose
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400290 fTransformedZParams = SkPoint3::Make(
Jim Van Verthe308a122017-05-08 14:19:30 -0400291 ctmInverse[SkMatrix::kMScaleX] * fZPlaneParams.fX +
292 ctmInverse[SkMatrix::kMSkewY] * fZPlaneParams.fY +
293 ctmInverse[SkMatrix::kMPersp0] * fZPlaneParams.fZ,
294
295 ctmInverse[SkMatrix::kMSkewX] * fZPlaneParams.fX +
296 ctmInverse[SkMatrix::kMScaleY] * fZPlaneParams.fY +
297 ctmInverse[SkMatrix::kMPersp1] * fZPlaneParams.fZ,
298
299 ctmInverse[SkMatrix::kMTransX] * fZPlaneParams.fX +
300 ctmInverse[SkMatrix::kMTransY] * fZPlaneParams.fY +
301 ctmInverse[SkMatrix::kMPersp2] * fZPlaneParams.fZ
302 );
Jim Van Verthda965502017-04-11 15:29:14 -0400303
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400304 if (ctm.hasPerspective()) {
305 // We use Cramer's rule to solve for the W value for a given post-divide X and Y,
306 // so pre-compute those values that are independent of X and Y.
307 // W is det(ctmInverse)/(PD[0]*X + PD[1]*Y + PD[2])
308 fPartialDeterminants[0] = ctm[SkMatrix::kMSkewY] * ctm[SkMatrix::kMPersp1] -
309 ctm[SkMatrix::kMScaleY] * ctm[SkMatrix::kMPersp0];
310 fPartialDeterminants[1] = ctm[SkMatrix::kMPersp0] * ctm[SkMatrix::kMSkewX] -
311 ctm[SkMatrix::kMPersp1] * ctm[SkMatrix::kMScaleX];
312 fPartialDeterminants[2] = ctm[SkMatrix::kMScaleX] * ctm[SkMatrix::kMScaleY] -
313 ctm[SkMatrix::kMSkewX] * ctm[SkMatrix::kMSkewY];
314 SkScalar ctmDeterminant = ctm[SkMatrix::kMTransX] * fPartialDeterminants[0] +
315 ctm[SkMatrix::kMTransY] * fPartialDeterminants[1] +
316 ctm[SkMatrix::kMPersp2] * fPartialDeterminants[2];
Jim Van Verthda965502017-04-11 15:29:14 -0400317
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400318 // Pre-bake the numerator of Cramer's rule into the zParams to avoid another multiply.
319 // TODO: this may introduce numerical instability, but I haven't seen any issues yet.
320 fTransformedZParams.fX *= ctmDeterminant;
321 fTransformedZParams.fY *= ctmDeterminant;
322 fTransformedZParams.fZ *= ctmDeterminant;
Jim Van Verthda965502017-04-11 15:29:14 -0400323
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400324 fTransformedHeightFunc = [this](const SkPoint& p) {
325 SkScalar denom = p.fX * fPartialDeterminants[0] +
326 p.fY * fPartialDeterminants[1] +
327 fPartialDeterminants[2];
328 SkScalar w = SkScalarFastInvert(denom);
329 return fZOffset + w*(fTransformedZParams.fX * p.fX +
330 fTransformedZParams.fY * p.fY +
331 fTransformedZParams.fZ);
332 };
333 } else {
334 fTransformedHeightFunc = [this](const SkPoint& p) {
335 return fZOffset + fTransformedZParams.fX * p.fX +
336 fTransformedZParams.fY * p.fY + fTransformedZParams.fZ;
337 };
338 }
Jim Van Verthda965502017-04-11 15:29:14 -0400339 }
340
341 return true;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500342}
343
344
345//////////////////////////////////////////////////////////////////////////////////////////////////
346
Brian Salomonaff27a22017-02-06 15:47:44 -0500347class SkAmbientShadowTessellator : public SkBaseShadowTessellator {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500348public:
349 SkAmbientShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400350 const SkPoint3& zPlaneParams, bool transparent);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500351
352private:
353 void handleLine(const SkPoint& p) override;
Jim Van Verthda965502017-04-11 15:29:14 -0400354 void addEdge(const SkVector& nextPoint, const SkVector& nextNormal);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500355
Jim Van Verthda965502017-04-11 15:29:14 -0400356 static constexpr auto kMaxEdgeLenSqr = 20 * 20;
Jim Van Verth76387852017-05-16 16:55:16 -0400357 static constexpr auto kInsetFactor = -0.5f;
Jim Van Verthda965502017-04-11 15:29:14 -0400358
359 SkScalar offset(SkScalar z) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400360 return SkDrawShadowMetrics::AmbientBlurRadius(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400361 }
362 SkColor umbraColor(SkScalar z) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400363 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(z));
Jim Van Verth060d9822017-05-04 09:58:17 -0400364 return SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
Jim Van Verthda965502017-04-11 15:29:14 -0400365 }
366
Jim Van Vertha84898d2017-02-06 13:38:23 -0500367 int fCentroidCount;
Jim Van Verth76387852017-05-16 16:55:16 -0400368 bool fSplitFirstEdge;
369 bool fSplitPreviousEdge;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500370
Brian Salomonaff27a22017-02-06 15:47:44 -0500371 typedef SkBaseShadowTessellator INHERITED;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500372};
373
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500374SkAmbientShadowTessellator::SkAmbientShadowTessellator(const SkPath& path,
Jim Van Vertha84898d2017-02-06 13:38:23 -0500375 const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400376 const SkPoint3& zPlaneParams,
Jim Van Verthbce74962017-01-25 09:39:46 -0500377 bool transparent)
Jim Van Verth76387852017-05-16 16:55:16 -0400378 : INHERITED(zPlaneParams, transparent)
379 , fSplitFirstEdge(false)
380 , fSplitPreviousEdge(false) {
Jim Van Verthda965502017-04-11 15:29:14 -0400381 // Set base colors
Jim Van Verth1af03d42017-07-31 09:34:58 -0400382 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(heightFunc(0, 0)));
Jim Van Verthb4366552017-03-27 14:25:29 -0400383 // umbraColor is the interior value, penumbraColor the exterior value.
384 // umbraAlpha is the factor that is linearly interpolated from outside to inside, and
385 // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get
386 // the final alpha.
Jim Van Verth060d9822017-05-04 09:58:17 -0400387 fUmbraColor = SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
388 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400389
Jim Van Verthda965502017-04-11 15:29:14 -0400390 // make sure we're not below the canvas plane
391 this->setZOffset(path.getBounds(), ctm.hasPerspective());
392
Jim Van Verth7c8008c2018-02-07 15:02:50 -0500393 if (!this->setTransformedHeightFunc(ctm)) {
394 return;
395 }
Jim Van Verthda965502017-04-11 15:29:14 -0400396
Jim Van Verthbce74962017-01-25 09:39:46 -0500397 // Outer ring: 3*numPts
398 // Middle ring: numPts
399 fPositions.setReserve(4 * path.countPoints());
400 fColors.setReserve(4 * path.countPoints());
401 // Outer ring: 12*numPts
402 // Middle ring: 0
403 fIndices.setReserve(12 * path.countPoints());
404
Jim Van Verthbce74962017-01-25 09:39:46 -0500405 // walk around the path, tessellate and generate outer ring
406 // if original path is transparent, will accumulate sum of points for centroid
407 SkPath::Iter iter(path, true);
408 SkPoint pts[4];
409 SkPath::Verb verb;
410 if (fTransparent) {
411 *fPositions.push() = SkPoint::Make(0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400412 *fColors.push() = fUmbraColor;
Jim Van Verthbce74962017-01-25 09:39:46 -0500413 fCentroidCount = 0;
414 }
415 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
416 switch (verb) {
417 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500418 this->INHERITED::handleLine(ctm, &pts[1]);
Jim Van Verthbce74962017-01-25 09:39:46 -0500419 break;
420 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500421 this->handleQuad(ctm, pts);
Jim Van Verthbce74962017-01-25 09:39:46 -0500422 break;
423 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500424 this->handleCubic(ctm, pts);
Jim Van Verthbce74962017-01-25 09:39:46 -0500425 break;
426 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500427 this->handleConic(ctm, pts, iter.conicWeight());
Jim Van Verthbce74962017-01-25 09:39:46 -0500428 break;
429 case SkPath::kMove_Verb:
430 case SkPath::kClose_Verb:
431 case SkPath::kDone_Verb:
432 break;
433 }
434 }
435
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500436 if (!this->indexCount()) {
437 return;
438 }
439
Jim Van Verth76387852017-05-16 16:55:16 -0400440 // Finish up
Jim Van Verthbce74962017-01-25 09:39:46 -0500441 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -0400442 if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) {
443 SkScalar z = fTransformedHeightFunc(fPrevPoint);
444 fRadius = this->offset(z);
445 SkVector scaledNormal(normal);
446 scaledNormal *= fRadius;
447 this->addArc(scaledNormal, true);
Jim Van Verthbce74962017-01-25 09:39:46 -0500448
Jim Van Verth76387852017-05-16 16:55:16 -0400449 // fix-up the last and first umbra points
450 SkVector inset = normal;
451 // adding to an average, so multiply by an additional half
452 inset *= 0.5f*kInsetFactor;
453 fPositions[fPrevUmbraIndex] += inset;
454 fPositions[fFirstVertexIndex] += inset;
455 // we multiply by another half because now we're adding to an average of an average
456 inset *= 0.5f;
457 if (fSplitPreviousEdge) {
458 fPositions[fPrevUmbraIndex - 2] += inset;
459 }
460 if (fSplitFirstEdge) {
461 fPositions[fFirstVertexIndex + 2] += inset;
462 }
463
Jim Van Verthda965502017-04-11 15:29:14 -0400464 // set up for final edge
465 z = fTransformedHeightFunc(fFirstPoint);
466 normal *= this->offset(z);
Jim Van Verthbce74962017-01-25 09:39:46 -0500467
Jim Van Verthda965502017-04-11 15:29:14 -0400468 // make sure we don't end up with a sharp alpha edge along the quad diagonal
Jim Van Verth76387852017-05-16 16:55:16 -0400469 if (fColors[fPrevUmbraIndex] != fColors[fFirstVertexIndex] &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500470 SkPointPriv::DistanceToSqd(fFirstPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400471 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + fPositions[fFirstVertexIndex];
Jim Van Verthda965502017-04-11 15:29:14 -0400472 centerPoint *= 0.5f;
473 *fPositions.push() = centerPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400474 *fColors.push() = SkPMLerp(fColors[fFirstVertexIndex], fColors[fPrevUmbraIndex], 128);
475 centerPoint = fPositions[fPositions.count()-2] + fPositions[fFirstVertexIndex+1];
476 centerPoint *= 0.5f;
477 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400478 *fColors.push() = fPenumbraColor;
479
Jim Van Verth76387852017-05-16 16:55:16 -0400480 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
481 *fIndices.push() = fPrevUmbraIndex;
482 *fIndices.push() = fPositions.count() - 3;
483 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthda965502017-04-11 15:29:14 -0400484
Jim Van Verth76387852017-05-16 16:55:16 -0400485 *fIndices.push() = fPositions.count() - 3;
486 *fIndices.push() = fPositions.count() - 1;
487 *fIndices.push() = fPositions.count() - 2;
488 } else {
489 *fIndices.push() = fPrevUmbraIndex;
490 *fIndices.push() = fPositions.count() - 2;
491 *fIndices.push() = fPositions.count() - 1;
492
493 *fIndices.push() = fPrevUmbraIndex;
494 *fIndices.push() = fPositions.count() - 1;
495 *fIndices.push() = fPositions.count() - 3;
496 }
Jim Van Verthda965502017-04-11 15:29:14 -0400497
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400498 // if transparent, add point to first one in array and add to center fan
499 if (fTransparent) {
500 fPositions[0] += centerPoint;
501 ++fCentroidCount;
502
503 *fIndices.push() = 0;
504 *fIndices.push() = fPrevUmbraIndex;
505 *fIndices.push() = fPositions.count() - 2;
506 }
507
Jim Van Verthda965502017-04-11 15:29:14 -0400508 fPrevUmbraIndex = fPositions.count() - 2;
509 }
510
511 // final edge
Jim Van Vertha84898d2017-02-06 13:38:23 -0500512 *fPositions.push() = fFirstPoint + normal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500513 *fColors.push() = fPenumbraColor;
514
Jim Van Verth76387852017-05-16 16:55:16 -0400515 if (fColors[fPrevUmbraIndex] > fColors[fFirstVertexIndex]) {
Jim Van Verthda965502017-04-11 15:29:14 -0400516 *fIndices.push() = fPrevUmbraIndex;
517 *fIndices.push() = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400518 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500519
Jim Van Verthda965502017-04-11 15:29:14 -0400520 *fIndices.push() = fPositions.count() - 2;
521 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400522 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400523 } else {
524 *fIndices.push() = fPrevUmbraIndex;
525 *fIndices.push() = fPositions.count() - 2;
526 *fIndices.push() = fPositions.count() - 1;
527
528 *fIndices.push() = fPrevUmbraIndex;
529 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400530 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400531 }
Jim Van Verth76387852017-05-16 16:55:16 -0400532 fPrevOutset = normal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500533 }
534
535 // finalize centroid
536 if (fTransparent) {
537 fPositions[0] *= SkScalarFastInvert(fCentroidCount);
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400538 fColors[0] = this->umbraColor(fTransformedHeightFunc(fPositions[0]));
Jim Van Verthbce74962017-01-25 09:39:46 -0500539
540 *fIndices.push() = 0;
Brian Salomon66085ed2017-02-02 15:39:34 -0500541 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400542 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500543 }
544
545 // final fan
546 if (fPositions.count() >= 3) {
Jim Van Verth76387852017-05-16 16:55:16 -0400547 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500548 fPrevPoint = fFirstPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400549 fRadius = this->offset(fTransformedHeightFunc(fPrevPoint));
Jim Van Verth76387852017-05-16 16:55:16 -0400550 if (this->addArc(fFirstOutset, false)) {
551 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400552 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400553 *fIndices.push() = fFirstVertexIndex + 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400554 } else {
555 // arc is too small, set the first penumbra point to be the same position
556 // as the last one
Jim Van Verth76387852017-05-16 16:55:16 -0400557 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400558 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500559 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500560 fSucceeded = true;
Jim Van Verthbce74962017-01-25 09:39:46 -0500561}
562
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500563void SkAmbientShadowTessellator::handleLine(const SkPoint& p) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500564 if (fInitPoints.count() < 2) {
565 *fInitPoints.push() = p;
566 return;
567 }
568
569 if (fInitPoints.count() == 2) {
570 // determine if cw or ccw
571 SkVector v0 = fInitPoints[1] - fInitPoints[0];
572 SkVector v1 = p - fInitPoints[0];
573 SkScalar perpDot = v0.fX*v1.fY - v0.fY*v1.fX;
574 if (SkScalarNearlyZero(perpDot)) {
575 // nearly parallel, just treat as straight line and continue
576 fInitPoints[1] = p;
577 return;
578 }
579
580 // if perpDot > 0, winding is ccw
581 fDirection = (perpDot > 0) ? -1 : 1;
582
583 // add first quad
Jim Van Verthda965502017-04-11 15:29:14 -0400584 SkVector normal;
585 if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &normal)) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500586 // first two points are incident, make the third point the second and continue
587 fInitPoints[1] = p;
588 return;
589 }
590
Jim Van Vertha84898d2017-02-06 13:38:23 -0500591 fFirstPoint = fInitPoints[0];
Jim Van Verth76387852017-05-16 16:55:16 -0400592 fFirstVertexIndex = fPositions.count();
Jim Van Verthda965502017-04-11 15:29:14 -0400593 SkScalar z = fTransformedHeightFunc(fFirstPoint);
Jim Van Verth76387852017-05-16 16:55:16 -0400594 fFirstOutset = normal;
595 fFirstOutset *= this->offset(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400596
Jim Van Verth76387852017-05-16 16:55:16 -0400597 fPrevOutset = fFirstOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500598 fPrevPoint = fFirstPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400599 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500600
Jim Van Verthda965502017-04-11 15:29:14 -0400601 *fPositions.push() = fFirstPoint;
602 *fColors.push() = this->umbraColor(z);
Jim Van Verth76387852017-05-16 16:55:16 -0400603 *fPositions.push() = fFirstPoint + fFirstOutset;
Jim Van Verthbce74962017-01-25 09:39:46 -0500604 *fColors.push() = fPenumbraColor;
605 if (fTransparent) {
Jim Van Verthda965502017-04-11 15:29:14 -0400606 fPositions[0] += fFirstPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500607 fCentroidCount = 1;
608 }
Jim Van Verthda965502017-04-11 15:29:14 -0400609
610 // add the first quad
611 z = fTransformedHeightFunc(fInitPoints[1]);
612 fRadius = this->offset(z);
613 fUmbraColor = this->umbraColor(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400614 this->addEdge(fInitPoints[1], normal);
Jim Van Verthbce74962017-01-25 09:39:46 -0500615
616 // to ensure we skip this block next time
617 *fInitPoints.push() = p;
618 }
619
620 SkVector normal;
Jim Van Verth76387852017-05-16 16:55:16 -0400621 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400622 SkVector scaledNormal = normal;
623 scaledNormal *= fRadius;
624 this->addArc(scaledNormal, true);
625 SkScalar z = fTransformedHeightFunc(p);
626 fRadius = this->offset(z);
627 fUmbraColor = this->umbraColor(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400628 this->addEdge(p, normal);
Jim Van Verthbce74962017-01-25 09:39:46 -0500629 }
630}
631
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500632void SkAmbientShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) {
Jim Van Verth76387852017-05-16 16:55:16 -0400633 // We compute the inset in two stages: first we inset by half the current normal,
634 // then on the next addEdge() we add half of the next normal to get an average of the two
635 SkVector insetNormal = nextNormal;
636 insetNormal *= 0.5f*kInsetFactor;
637
638 // Adding the other half of the average for the previous edge
639 fPositions[fPrevUmbraIndex] += insetNormal;
640
641 SkPoint umbraPoint = nextPoint + insetNormal;
642 SkVector outsetNormal = nextNormal;
643 outsetNormal *= fRadius;
644 SkPoint penumbraPoint = nextPoint + outsetNormal;
645
646 // For split edges, we're adding an average of two averages, so we multiply by another half
647 if (fSplitPreviousEdge) {
648 insetNormal *= 0.5f;
649 fPositions[fPrevUmbraIndex - 2] += insetNormal;
650 }
651
652 // Split the edge to make sure we don't end up with a sharp alpha edge along the quad diagonal
Jim Van Verthda965502017-04-11 15:29:14 -0400653 if (fColors[fPrevUmbraIndex] != fUmbraColor &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500654 SkPointPriv::DistanceToSqd(nextPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400655
656 // This is lacking 1/4 of the next inset -- we'll add it the next time we call addEdge()
657 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + umbraPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400658 centerPoint *= 0.5f;
659 *fPositions.push() = centerPoint;
660 *fColors.push() = SkPMLerp(fUmbraColor, fColors[fPrevUmbraIndex], 128);
Jim Van Verth76387852017-05-16 16:55:16 -0400661 centerPoint = fPositions[fPositions.count()-2] + penumbraPoint;
662 centerPoint *= 0.5f;
663 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400664 *fColors.push() = fPenumbraColor;
665
666 // set triangularization to get best interpolation of color
667 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
668 *fIndices.push() = fPrevUmbraIndex;
669 *fIndices.push() = fPositions.count() - 3;
670 *fIndices.push() = fPositions.count() - 2;
671
672 *fIndices.push() = fPositions.count() - 3;
673 *fIndices.push() = fPositions.count() - 1;
674 *fIndices.push() = fPositions.count() - 2;
675 } else {
676 *fIndices.push() = fPrevUmbraIndex;
677 *fIndices.push() = fPositions.count() - 2;
678 *fIndices.push() = fPositions.count() - 1;
679
680 *fIndices.push() = fPrevUmbraIndex;
681 *fIndices.push() = fPositions.count() - 1;
682 *fIndices.push() = fPositions.count() - 3;
683 }
684
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400685 // if transparent, add point to first one in array and add to center fan
686 if (fTransparent) {
687 fPositions[0] += centerPoint;
688 ++fCentroidCount;
689
690 *fIndices.push() = 0;
691 *fIndices.push() = fPrevUmbraIndex;
692 *fIndices.push() = fPositions.count() - 2;
693 }
694
Jim Van Verth76387852017-05-16 16:55:16 -0400695 fSplitPreviousEdge = true;
696 if (fPrevUmbraIndex == fFirstVertexIndex) {
697 fSplitFirstEdge = true;
698 }
Jim Van Verthda965502017-04-11 15:29:14 -0400699 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400700 } else {
701 fSplitPreviousEdge = false;
Jim Van Verthda965502017-04-11 15:29:14 -0400702 }
703
Jim Van Verthbce74962017-01-25 09:39:46 -0500704 // add next quad
Jim Van Verth76387852017-05-16 16:55:16 -0400705 *fPositions.push() = umbraPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500706 *fColors.push() = fUmbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -0400707 *fPositions.push() = penumbraPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500708 *fColors.push() = fPenumbraColor;
709
Jim Van Verthda965502017-04-11 15:29:14 -0400710 // set triangularization to get best interpolation of color
711 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
712 *fIndices.push() = fPrevUmbraIndex;
713 *fIndices.push() = fPositions.count() - 3;
714 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthbce74962017-01-25 09:39:46 -0500715
Jim Van Verthda965502017-04-11 15:29:14 -0400716 *fIndices.push() = fPositions.count() - 3;
717 *fIndices.push() = fPositions.count() - 1;
718 *fIndices.push() = fPositions.count() - 2;
719 } else {
720 *fIndices.push() = fPrevUmbraIndex;
721 *fIndices.push() = fPositions.count() - 2;
722 *fIndices.push() = fPositions.count() - 1;
723
724 *fIndices.push() = fPrevUmbraIndex;
725 *fIndices.push() = fPositions.count() - 1;
726 *fIndices.push() = fPositions.count() - 3;
727 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500728
729 // if transparent, add point to first one in array and add to center fan
730 if (fTransparent) {
731 fPositions[0] += nextPoint;
732 ++fCentroidCount;
733
734 *fIndices.push() = 0;
Brian Salomon66085ed2017-02-02 15:39:34 -0500735 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500736 *fIndices.push() = fPositions.count() - 2;
737 }
738
Brian Salomon66085ed2017-02-02 15:39:34 -0500739 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500740 fPrevPoint = nextPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400741 fPrevOutset = outsetNormal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500742}
Jim Van Verth91af7272017-01-27 14:15:54 -0500743
744///////////////////////////////////////////////////////////////////////////////////////////////////
745
Brian Salomonaff27a22017-02-06 15:47:44 -0500746class SkSpotShadowTessellator : public SkBaseShadowTessellator {
Brian Salomon958fbc42017-01-30 17:01:28 -0500747public:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500748 SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400749 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -0400750 SkScalar lightRadius, bool transparent);
Brian Salomon958fbc42017-01-30 17:01:28 -0500751
Brian Salomon958fbc42017-01-30 17:01:28 -0500752private:
Brian Salomonab664fa2017-03-24 16:07:20 +0000753 void computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -0400754 const SkMatrix& shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000755 void computeClipVectorsAndTestCentroid();
Brian Salomon66085ed2017-02-02 15:39:34 -0500756 bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000757 int getClosestUmbraPoint(const SkPoint& point);
Brian Salomon958fbc42017-01-30 17:01:28 -0500758
Jim Van Vertha84898d2017-02-06 13:38:23 -0500759 void handleLine(const SkPoint& p) override;
Jim Van Verthb55eb282017-07-18 14:13:45 -0400760 bool handlePolyPoint(const SkPoint& p);
Brian Salomon958fbc42017-01-30 17:01:28 -0500761
762 void mapPoints(SkScalar scale, const SkVector& xlate, SkPoint* pts, int count);
Brian Salomonab664fa2017-03-24 16:07:20 +0000763 bool addInnerPoint(const SkPoint& pathPoint);
Jim Van Verthda965502017-04-11 15:29:14 -0400764 void addEdge(const SkVector& nextPoint, const SkVector& nextNormal);
765
766 SkScalar offset(SkScalar z) {
767 float zRatio = SkTPin(z / (fLightZ - z), 0.0f, 0.95f);
768 return fLightRadius*zRatio;
769 }
770
771 SkScalar fLightZ;
772 SkScalar fLightRadius;
773 SkScalar fOffsetAdjust;
Brian Salomon958fbc42017-01-30 17:01:28 -0500774
Brian Salomon958fbc42017-01-30 17:01:28 -0500775 SkTDArray<SkPoint> fClipPolygon;
Brian Salomon66085ed2017-02-02 15:39:34 -0500776 SkTDArray<SkVector> fClipVectors;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500777 SkPoint fCentroid;
Brian Salomonab664fa2017-03-24 16:07:20 +0000778 SkScalar fArea;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500779
Brian Salomonab664fa2017-03-24 16:07:20 +0000780 SkTDArray<SkPoint> fPathPolygon;
781 SkTDArray<SkPoint> fUmbraPolygon;
782 int fCurrClipPoint;
783 int fCurrUmbraPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -0500784 bool fPrevUmbraOutside;
785 bool fFirstUmbraOutside;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500786 bool fValidUmbra;
Brian Salomon958fbc42017-01-30 17:01:28 -0500787
Brian Salomonaff27a22017-02-06 15:47:44 -0500788 typedef SkBaseShadowTessellator INHERITED;
Brian Salomon958fbc42017-01-30 17:01:28 -0500789};
790
Jim Van Vertha84898d2017-02-06 13:38:23 -0500791SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400792 const SkPoint3& zPlaneParams,
Jim Van Verthb4366552017-03-27 14:25:29 -0400793 const SkPoint3& lightPos, SkScalar lightRadius,
Jim Van Verth060d9822017-05-04 09:58:17 -0400794 bool transparent)
Jim Van Verthe308a122017-05-08 14:19:30 -0400795 : INHERITED(zPlaneParams, transparent)
Jim Van Verthda965502017-04-11 15:29:14 -0400796 , fLightZ(lightPos.fZ)
797 , fLightRadius(lightRadius)
798 , fOffsetAdjust(0)
799 , fCurrClipPoint(0)
800 , fPrevUmbraOutside(false)
801 , fFirstUmbraOutside(false)
802 , fValidUmbra(true) {
803
804 // make sure we're not below the canvas plane
805 if (this->setZOffset(path.getBounds(), ctm.hasPerspective())) {
806 // Adjust light height and radius
807 fLightRadius *= (fLightZ + fZOffset) / fLightZ;
808 fLightZ += fZOffset;
809 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400810
811 // Set radius and colors
Jim Van Verthb4366552017-03-27 14:25:29 -0400812 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verthe308a122017-05-08 14:19:30 -0400813 SkScalar occluderHeight = this->heightFunc(center.fX, center.fY) + fZOffset;
Jim Van Verth060d9822017-05-04 09:58:17 -0400814 fUmbraColor = SkColorSetARGB(255, 0, 0, 0);
815 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400816
Jim Van Verth1af03d42017-07-31 09:34:58 -0400817 // Compute the blur radius, scale and translation for the spot shadow.
818 SkScalar radius;
Jim Van Verthda965502017-04-11 15:29:14 -0400819 SkMatrix shadowTransform;
820 if (!ctm.hasPerspective()) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400821 SkScalar scale;
822 SkVector translate;
823 SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY, fLightZ,
824 lightRadius, &radius, &scale, &translate);
Jim Van Verthda965502017-04-11 15:29:14 -0400825 shadowTransform.setScaleTranslate(scale, scale, translate.fX, translate.fY);
826 } else {
827 // For perspective, we have a scale, a z-shear, and another projective divide --
828 // this varies at each point so we can't use an affine transform.
829 // We'll just apply this to each generated point in turn.
830 shadowTransform.reset();
831 // Also can't cull the center (for now).
832 fTransparent = true;
Jim Van Verth1af03d42017-07-31 09:34:58 -0400833 radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius);
Jim Van Verthda965502017-04-11 15:29:14 -0400834 }
Jim Van Verth1af03d42017-07-31 09:34:58 -0400835 fRadius = radius;
Jim Van Verthda965502017-04-11 15:29:14 -0400836 SkMatrix fullTransform = SkMatrix::Concat(shadowTransform, ctm);
837
838 // Set up our reverse mapping
Jim Van Verth7c8008c2018-02-07 15:02:50 -0500839 if (!this->setTransformedHeightFunc(fullTransform)) {
840 return;
841 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400842
Brian Salomonab664fa2017-03-24 16:07:20 +0000843 // TODO: calculate these reserves better
Brian Salomon66085ed2017-02-02 15:39:34 -0500844 // Penumbra ring: 3*numPts
845 // Umbra ring: numPts
Jim Van Verth91af7272017-01-27 14:15:54 -0500846 // Inner ring: numPts
Brian Salomon66085ed2017-02-02 15:39:34 -0500847 fPositions.setReserve(5 * path.countPoints());
848 fColors.setReserve(5 * path.countPoints());
849 // Penumbra ring: 12*numPts
850 // Umbra ring: 3*numPts
851 fIndices.setReserve(15 * path.countPoints());
Brian Salomone7c85c42017-03-24 16:00:35 +0000852 fClipPolygon.setReserve(path.countPoints());
Brian Salomonab664fa2017-03-24 16:07:20 +0000853
854 // compute rough clip bounds for umbra, plus offset polygon, plus centroid
Jim Van Verthda965502017-04-11 15:29:14 -0400855 this->computeClipAndPathPolygons(path, ctm, shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000856 if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3) {
Brian Salomon66085ed2017-02-02 15:39:34 -0500857 return;
858 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500859
Brian Salomonab664fa2017-03-24 16:07:20 +0000860 // check to see if umbra collapses
Cary Clarkdf429f32017-11-08 11:44:31 -0500861 SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, fPathPolygon[0],
862 fPathPolygon[1]);
Jim Van Verthda965502017-04-11 15:29:14 -0400863 SkRect bounds;
864 bounds.setBounds(&fPathPolygon[0], fPathPolygon.count());
Brian Salomonab664fa2017-03-24 16:07:20 +0000865 for (int i = 1; i < fPathPolygon.count(); ++i) {
866 int j = i + 1;
867 if (i == fPathPolygon.count() - 1) {
868 j = 0;
869 }
870 SkPoint currPoint = fPathPolygon[i];
871 SkPoint nextPoint = fPathPolygon[j];
Cary Clarkdf429f32017-11-08 11:44:31 -0500872 SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint,
873 nextPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000874 if (distSq < minDistSq) {
875 minDistSq = distSq;
876 }
877 }
878 static constexpr auto kTolerance = 1.0e-2f;
879 if (minDistSq < (radius + kTolerance)*(radius + kTolerance)) {
880 // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha
881 SkScalar newRadius = SkScalarSqrt(minDistSq) - kTolerance;
Jim Van Verthda965502017-04-11 15:29:14 -0400882 fOffsetAdjust = newRadius - radius;
Jim Van Verthb6069df2017-04-28 11:00:35 -0400883 SkScalar ratio = 128 * (newRadius + radius) / radius;
Brian Salomonab664fa2017-03-24 16:07:20 +0000884 // they aren't PMColors, but the interpolation algorithm is the same
885 fUmbraColor = SkPMLerp(fUmbraColor, fPenumbraColor, (unsigned)ratio);
886 radius = newRadius;
887 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500888
Brian Salomonab664fa2017-03-24 16:07:20 +0000889 // compute vectors for clip tests
890 this->computeClipVectorsAndTestCentroid();
891
892 // generate inner ring
Jim Van Verthda965502017-04-11 15:29:14 -0400893 if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), radius,
894 &fUmbraPolygon)) {
Brian Salomonab664fa2017-03-24 16:07:20 +0000895 // this shouldn't happen, but just in case we'll inset using the centroid
896 fValidUmbra = false;
897 }
898
899 // walk around the path polygon, generate outer ring and connect to inner ring
Brian Salomon66085ed2017-02-02 15:39:34 -0500900 if (fTransparent) {
901 *fPositions.push() = fCentroid;
902 *fColors.push() = fUmbraColor;
903 }
Brian Salomonab664fa2017-03-24 16:07:20 +0000904 fCurrUmbraPoint = 0;
905 for (int i = 0; i < fPathPolygon.count(); ++i) {
Jim Van Verthb55eb282017-07-18 14:13:45 -0400906 if (!this->handlePolyPoint(fPathPolygon[i])) {
907 return;
908 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500909 }
910
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500911 if (!this->indexCount()) {
912 return;
913 }
914
Brian Salomonab664fa2017-03-24 16:07:20 +0000915 // finish up the final verts
Jim Van Verth91af7272017-01-27 14:15:54 -0500916 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -0400917 if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) {
918 normal *= fRadius;
919 this->addArc(normal, true);
Jim Van Verth91af7272017-01-27 14:15:54 -0500920
Brian Salomon66085ed2017-02-02 15:39:34 -0500921 // add to center fan
922 if (fTransparent) {
923 *fIndices.push() = 0;
924 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400925 *fIndices.push() = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -0500926 // or to clip ring
927 } else {
928 if (fFirstUmbraOutside) {
929 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400930 *fIndices.push() = fFirstVertexIndex;
931 *fIndices.push() = fFirstVertexIndex + 1;
Brian Salomon66085ed2017-02-02 15:39:34 -0500932 if (fPrevUmbraOutside) {
933 // fill out quad
934 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400935 *fIndices.push() = fFirstVertexIndex + 1;
Brian Salomon66085ed2017-02-02 15:39:34 -0500936 *fIndices.push() = fPrevUmbraIndex + 1;
937 }
938 } else if (fPrevUmbraOutside) {
939 // add tri
940 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400941 *fIndices.push() = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -0500942 *fIndices.push() = fPrevUmbraIndex + 1;
943 }
944 }
945
Jim Van Verth91af7272017-01-27 14:15:54 -0500946 // add final edge
947 *fPositions.push() = fFirstPoint + normal;
948 *fColors.push() = fPenumbraColor;
949
Brian Salomon66085ed2017-02-02 15:39:34 -0500950 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500951 *fIndices.push() = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400952 *fIndices.push() = fFirstVertexIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500953
954 *fIndices.push() = fPositions.count() - 2;
955 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400956 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400957
Jim Van Verth76387852017-05-16 16:55:16 -0400958 fPrevOutset = normal;
Jim Van Verth91af7272017-01-27 14:15:54 -0500959 }
960
961 // final fan
962 if (fPositions.count() >= 3) {
Jim Van Verth76387852017-05-16 16:55:16 -0400963 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500964 fPrevPoint = fFirstPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400965 if (this->addArc(fFirstOutset, false)) {
966 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400967 *fIndices.push() = fPositions.count() - 1;
968 if (fFirstUmbraOutside) {
Jim Van Verth76387852017-05-16 16:55:16 -0400969 *fIndices.push() = fFirstVertexIndex + 2;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400970 } else {
Jim Van Verth76387852017-05-16 16:55:16 -0400971 *fIndices.push() = fFirstVertexIndex + 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400972 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500973 } else {
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400974 // no arc added, fix up by setting first penumbra point position to last one
975 if (fFirstUmbraOutside) {
Jim Van Verth76387852017-05-16 16:55:16 -0400976 fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400977 } else {
Jim Van Verth76387852017-05-16 16:55:16 -0400978 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400979 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500980 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500981 }
Jim Van Verthda965502017-04-11 15:29:14 -0400982
983 if (ctm.hasPerspective()) {
984 for (int i = 0; i < fPositions.count(); ++i) {
985 SkScalar pathZ = fTransformedHeightFunc(fPositions[i]);
986 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
987 fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor;
988 fPositions[i].fY = (fPositions[i].fY*fLightZ - lightPos.fY*pathZ)*factor;
989 }
990#ifdef DRAW_CENTROID
991 SkScalar pathZ = fTransformedHeightFunc(fCentroid);
992 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
993 fCentroid.fX = (fCentroid.fX*fLightZ - lightPos.fX*pathZ)*factor;
994 fCentroid.fY = (fCentroid.fY*fLightZ - lightPos.fY*pathZ)*factor;
995#endif
996 }
997#ifdef DRAW_CENTROID
998 *fPositions.push() = fCentroid + SkVector::Make(-2, -2);
999 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1000 *fPositions.push() = fCentroid + SkVector::Make(2, -2);
1001 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1002 *fPositions.push() = fCentroid + SkVector::Make(-2, 2);
1003 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1004 *fPositions.push() = fCentroid + SkVector::Make(2, 2);
1005 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1006
1007 *fIndices.push() = fPositions.count() - 4;
1008 *fIndices.push() = fPositions.count() - 2;
1009 *fIndices.push() = fPositions.count() - 1;
1010
1011 *fIndices.push() = fPositions.count() - 4;
1012 *fIndices.push() = fPositions.count() - 1;
1013 *fIndices.push() = fPositions.count() - 3;
1014#endif
1015
Brian Salomon0dda9cb2017-02-03 10:33:25 -05001016 fSucceeded = true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001017}
1018
Brian Salomonab664fa2017-03-24 16:07:20 +00001019void SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -04001020 const SkMatrix& shadowTransform) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001021
1022 fPathPolygon.setReserve(path.countPoints());
1023
1024 // Walk around the path and compute clip polygon and path polygon.
1025 // Will also accumulate sum of areas for centroid.
1026 // For Bezier curves, we compute additional interior points on curve.
Jim Van Verth91af7272017-01-27 14:15:54 -05001027 SkPath::Iter iter(path, true);
1028 SkPoint pts[4];
1029 SkPath::Verb verb;
1030
Jim Van Verth91af7272017-01-27 14:15:54 -05001031 fClipPolygon.reset();
1032
Brian Salomonab664fa2017-03-24 16:07:20 +00001033 // init centroid
1034 fCentroid = SkPoint::Make(0, 0);
1035 fArea = 0;
1036
Brian Salomon66085ed2017-02-02 15:39:34 -05001037 // coefficients to compute cubic Bezier at t = 5/16
Brian Salomonab664fa2017-03-24 16:07:20 +00001038 static constexpr SkScalar kA = 0.32495117187f;
1039 static constexpr SkScalar kB = 0.44311523437f;
1040 static constexpr SkScalar kC = 0.20141601562f;
1041 static constexpr SkScalar kD = 0.03051757812f;
Brian Salomon66085ed2017-02-02 15:39:34 -05001042
1043 SkPoint curvePoint;
1044 SkScalar w;
Jim Van Verth91af7272017-01-27 14:15:54 -05001045 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
1046 switch (verb) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001047 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001048 ctm.mapPoints(&pts[1], 1);
Jim Van Verth91af7272017-01-27 14:15:54 -05001049 *fClipPolygon.push() = pts[1];
Brian Salomonab664fa2017-03-24 16:07:20 +00001050 this->INHERITED::handleLine(shadowTransform, &pts[1]);
Jim Van Verth91af7272017-01-27 14:15:54 -05001051 break;
1052 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001053 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001054 // point at t = 1/2
1055 curvePoint.fX = 0.25f*pts[0].fX + 0.5f*pts[1].fX + 0.25f*pts[2].fX;
1056 curvePoint.fY = 0.25f*pts[0].fY + 0.5f*pts[1].fY + 0.25f*pts[2].fY;
1057 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001058 *fClipPolygon.push() = pts[2];
Brian Salomonab664fa2017-03-24 16:07:20 +00001059 this->handleQuad(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001060 break;
1061 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001062 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001063 w = iter.conicWeight();
Jim Van Vertha84898d2017-02-06 13:38:23 -05001064 // point at t = 1/2
Brian Salomon66085ed2017-02-02 15:39:34 -05001065 curvePoint.fX = 0.25f*pts[0].fX + w*0.5f*pts[1].fX + 0.25f*pts[2].fX;
1066 curvePoint.fY = 0.25f*pts[0].fY + w*0.5f*pts[1].fY + 0.25f*pts[2].fY;
1067 curvePoint *= SkScalarInvert(0.5f + 0.5f*w);
1068 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001069 *fClipPolygon.push() = pts[2];
Brian Salomonab664fa2017-03-24 16:07:20 +00001070 this->handleConic(shadowTransform, pts, w);
Jim Van Verth91af7272017-01-27 14:15:54 -05001071 break;
1072 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001073 ctm.mapPoints(pts, 4);
Brian Salomon66085ed2017-02-02 15:39:34 -05001074 // point at t = 5/16
1075 curvePoint.fX = kA*pts[0].fX + kB*pts[1].fX + kC*pts[2].fX + kD*pts[3].fX;
1076 curvePoint.fY = kA*pts[0].fY + kB*pts[1].fY + kC*pts[2].fY + kD*pts[3].fY;
1077 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001078 // point at t = 11/16
1079 curvePoint.fX = kD*pts[0].fX + kC*pts[1].fX + kB*pts[2].fX + kA*pts[3].fX;
1080 curvePoint.fY = kD*pts[0].fY + kC*pts[1].fY + kB*pts[2].fY + kA*pts[3].fY;
1081 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001082 *fClipPolygon.push() = pts[3];
Brian Salomonab664fa2017-03-24 16:07:20 +00001083 this->handleCubic(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001084 break;
Brian Salomonab664fa2017-03-24 16:07:20 +00001085 case SkPath::kMove_Verb:
Jim Van Verth91af7272017-01-27 14:15:54 -05001086 case SkPath::kClose_Verb:
Brian Salomonab664fa2017-03-24 16:07:20 +00001087 case SkPath::kDone_Verb:
Jim Van Verth91af7272017-01-27 14:15:54 -05001088 break;
1089 default:
1090 SkDEBUGFAIL("unknown verb");
1091 }
1092 }
1093
Brian Salomonab664fa2017-03-24 16:07:20 +00001094 // finish centroid
1095 if (fPathPolygon.count() > 0) {
1096 SkPoint currPoint = fPathPolygon[fPathPolygon.count() - 1];
1097 SkPoint nextPoint = fPathPolygon[0];
1098 SkScalar quadArea = currPoint.cross(nextPoint);
1099 fCentroid.fX += (currPoint.fX + nextPoint.fX) * quadArea;
1100 fCentroid.fY += (currPoint.fY + nextPoint.fY) * quadArea;
1101 fArea += quadArea;
1102 fCentroid *= SK_Scalar1 / (3 * fArea);
1103 }
1104
1105 fCurrClipPoint = fClipPolygon.count() - 1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001106}
1107
Brian Salomonab664fa2017-03-24 16:07:20 +00001108void SkSpotShadowTessellator::computeClipVectorsAndTestCentroid() {
Brian Salomon66085ed2017-02-02 15:39:34 -05001109 SkASSERT(fClipPolygon.count() >= 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001110
Brian Salomonab664fa2017-03-24 16:07:20 +00001111 // init clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001112 SkVector v0 = fClipPolygon[1] - fClipPolygon[0];
1113 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001114
1115 // init centroid check
1116 bool hiddenCentroid = true;
Brian Salomonab664fa2017-03-24 16:07:20 +00001117 SkVector v1 = fCentroid - fClipPolygon[0];
Brian Salomon66085ed2017-02-02 15:39:34 -05001118 SkScalar initCross = v0.cross(v1);
1119
1120 for (int p = 1; p < fClipPolygon.count(); ++p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001121 // add to clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001122 v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p];
1123 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001124 // Determine if transformed centroid is inside clipPolygon.
Brian Salomonab664fa2017-03-24 16:07:20 +00001125 v1 = fCentroid - fClipPolygon[p];
Brian Salomon66085ed2017-02-02 15:39:34 -05001126 if (initCross*v0.cross(v1) <= 0) {
1127 hiddenCentroid = false;
1128 }
1129 }
1130 SkASSERT(fClipVectors.count() == fClipPolygon.count());
1131
Brian Salomonab664fa2017-03-24 16:07:20 +00001132 fTransparent = fTransparent || !hiddenCentroid;
Brian Salomon66085ed2017-02-02 15:39:34 -05001133}
1134
1135bool SkSpotShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid,
1136 SkPoint* clipPoint) {
1137 SkVector segmentVector = centroid - umbraPoint;
1138
Brian Salomonab664fa2017-03-24 16:07:20 +00001139 int startClipPoint = fCurrClipPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001140 do {
Brian Salomonab664fa2017-03-24 16:07:20 +00001141 SkVector dp = umbraPoint - fClipPolygon[fCurrClipPoint];
1142 SkScalar denom = fClipVectors[fCurrClipPoint].cross(segmentVector);
Brian Salomon66085ed2017-02-02 15:39:34 -05001143 SkScalar t_num = dp.cross(segmentVector);
1144 // if line segments are nearly parallel
1145 if (SkScalarNearlyZero(denom)) {
1146 // and collinear
1147 if (SkScalarNearlyZero(t_num)) {
1148 return false;
1149 }
1150 // otherwise are separate, will try the next poly segment
1151 // else if crossing lies within poly segment
1152 } else if (t_num >= 0 && t_num <= denom) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001153 SkScalar s_num = dp.cross(fClipVectors[fCurrClipPoint]);
Brian Salomon66085ed2017-02-02 15:39:34 -05001154 // if umbra point is inside the clip polygon
Jim Van Verthda965502017-04-11 15:29:14 -04001155 if (s_num >= 0 && s_num <= denom) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001156 segmentVector *= s_num/denom;
1157 *clipPoint = umbraPoint + segmentVector;
1158 return true;
1159 }
1160 }
Brian Salomonab664fa2017-03-24 16:07:20 +00001161 fCurrClipPoint = (fCurrClipPoint + 1) % fClipPolygon.count();
1162 } while (fCurrClipPoint != startClipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001163
1164 return false;
Jim Van Verth91af7272017-01-27 14:15:54 -05001165}
1166
Brian Salomonab664fa2017-03-24 16:07:20 +00001167int SkSpotShadowTessellator::getClosestUmbraPoint(const SkPoint& p) {
Cary Clarkdf429f32017-11-08 11:44:31 -05001168 SkScalar minDistance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[fCurrUmbraPoint]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001169 int index = fCurrUmbraPoint;
1170 int dir = 1;
1171 int next = (index + dir) % fUmbraPolygon.count();
1172
1173 // init travel direction
Cary Clarkdf429f32017-11-08 11:44:31 -05001174 SkScalar distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001175 if (distance < minDistance) {
1176 index = next;
1177 minDistance = distance;
1178 } else {
1179 dir = fUmbraPolygon.count()-1;
1180 }
1181
1182 // iterate until we find a point that increases the distance
1183 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001184 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001185 while (distance < minDistance) {
1186 index = next;
1187 minDistance = distance;
1188 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001189 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001190 }
1191
1192 fCurrUmbraPoint = index;
1193 return index;
1194}
1195
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001196void SkSpotShadowTessellator::mapPoints(SkScalar scale, const SkVector& xlate,
Jim Van Verth91af7272017-01-27 14:15:54 -05001197 SkPoint* pts, int count) {
1198 // TODO: vectorize
1199 for (int i = 0; i < count; ++i) {
1200 pts[i] *= scale;
1201 pts[i] += xlate;
1202 }
1203}
1204
Brian Salomonab664fa2017-03-24 16:07:20 +00001205static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) {
1206 static constexpr SkScalar kClose = (SK_Scalar1 / 16);
1207 static constexpr SkScalar kCloseSqd = kClose*kClose;
1208
Cary Clarkdf429f32017-11-08 11:44:31 -05001209 SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1);
Brian Salomonab664fa2017-03-24 16:07:20 +00001210 return distSq < kCloseSqd;
1211}
1212
Jim Van Verthb55eb282017-07-18 14:13:45 -04001213static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001214 SkVector v0 = p1 - p0;
1215 SkVector v1 = p2 - p0;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001216 return v0.cross(v1);
1217}
1218
1219static bool is_collinear(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
1220 return (SkScalarNearlyZero(perp_dot(p0, p1, p2)));
Brian Salomonab664fa2017-03-24 16:07:20 +00001221}
1222
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001223void SkSpotShadowTessellator::handleLine(const SkPoint& p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001224 // remove coincident points and add to centroid
1225 if (fPathPolygon.count() > 0) {
1226 const SkPoint& lastPoint = fPathPolygon[fPathPolygon.count() - 1];
1227 if (duplicate_pt(p, lastPoint)) {
1228 return;
1229 }
1230 SkScalar quadArea = lastPoint.cross(p);
1231 fCentroid.fX += (p.fX + lastPoint.fX) * quadArea;
1232 fCentroid.fY += (p.fY + lastPoint.fY) * quadArea;
1233 fArea += quadArea;
1234 }
1235
1236 // try to remove collinear points
1237 if (fPathPolygon.count() > 1 && is_collinear(fPathPolygon[fPathPolygon.count()-2],
1238 fPathPolygon[fPathPolygon.count()-1],
1239 p)) {
1240 fPathPolygon[fPathPolygon.count() - 1] = p;
1241 } else {
1242 *fPathPolygon.push() = p;
1243 }
1244}
1245
Jim Van Verthb55eb282017-07-18 14:13:45 -04001246bool SkSpotShadowTessellator::handlePolyPoint(const SkPoint& p) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001247 if (fInitPoints.count() < 2) {
1248 *fInitPoints.push() = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001249 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001250 }
1251
1252 if (fInitPoints.count() == 2) {
1253 // determine if cw or ccw
Jim Van Verthb55eb282017-07-18 14:13:45 -04001254 SkScalar perpDot = perp_dot(fInitPoints[0], fInitPoints[1], p);
Jim Van Verth91af7272017-01-27 14:15:54 -05001255 if (SkScalarNearlyZero(perpDot)) {
1256 // nearly parallel, just treat as straight line and continue
1257 fInitPoints[1] = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001258 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001259 }
1260
1261 // if perpDot > 0, winding is ccw
1262 fDirection = (perpDot > 0) ? -1 : 1;
1263
1264 // add first quad
Jim Van Verth76387852017-05-16 16:55:16 -04001265 if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &fFirstOutset)) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001266 // first two points are incident, make the third point the second and continue
1267 fInitPoints[1] = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001268 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001269 }
1270
Jim Van Verth76387852017-05-16 16:55:16 -04001271 fFirstOutset *= fRadius;
Jim Van Verth91af7272017-01-27 14:15:54 -05001272 fFirstPoint = fInitPoints[0];
Jim Van Verth76387852017-05-16 16:55:16 -04001273 fFirstVertexIndex = fPositions.count();
1274 fPrevOutset = fFirstOutset;
Jim Van Verth91af7272017-01-27 14:15:54 -05001275 fPrevPoint = fFirstPoint;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001276 fPrevUmbraIndex = -1;
Jim Van Verth91af7272017-01-27 14:15:54 -05001277
Brian Salomon66085ed2017-02-02 15:39:34 -05001278 this->addInnerPoint(fFirstPoint);
Jim Van Verth76387852017-05-16 16:55:16 -04001279 fPrevUmbraIndex = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -05001280
1281 if (!fTransparent) {
1282 SkPoint clipPoint;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001283 bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex],
1284 fCentroid, &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001285 if (isOutside) {
1286 *fPositions.push() = clipPoint;
1287 *fColors.push() = fUmbraColor;
1288 }
1289 fPrevUmbraOutside = isOutside;
1290 fFirstUmbraOutside = isOutside;
1291 }
1292
Jim Van Verth76387852017-05-16 16:55:16 -04001293 SkPoint newPoint = fFirstPoint + fFirstOutset;
Jim Van Verth91af7272017-01-27 14:15:54 -05001294 *fPositions.push() = newPoint;
1295 *fColors.push() = fPenumbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -04001296 this->addEdge(fInitPoints[1], fFirstOutset);
Jim Van Verth91af7272017-01-27 14:15:54 -05001297
1298 // to ensure we skip this block next time
1299 *fInitPoints.push() = p;
1300 }
1301
Jim Van Verthb55eb282017-07-18 14:13:45 -04001302 // if concave, abort
1303 SkScalar perpDot = perp_dot(fInitPoints[1], fInitPoints[2], p);
1304 if (fDirection*perpDot > 0) {
1305 return false;
1306 }
1307
Jim Van Verth91af7272017-01-27 14:15:54 -05001308 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -04001309 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
1310 normal *= fRadius;
1311 this->addArc(normal, true);
1312 this->addEdge(p, normal);
Jim Van Verthb55eb282017-07-18 14:13:45 -04001313 fInitPoints[1] = fInitPoints[2];
1314 fInitPoints[2] = p;
Jim Van Verth91af7272017-01-27 14:15:54 -05001315 }
Jim Van Verthb55eb282017-07-18 14:13:45 -04001316
1317 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001318}
1319
Brian Salomonab664fa2017-03-24 16:07:20 +00001320bool SkSpotShadowTessellator::addInnerPoint(const SkPoint& pathPoint) {
1321 SkPoint umbraPoint;
1322 if (!fValidUmbra) {
1323 SkVector v = fCentroid - pathPoint;
1324 v *= 0.95f;
1325 umbraPoint = pathPoint + v;
Jim Van Verth91af7272017-01-27 14:15:54 -05001326 } else {
Brian Salomonab664fa2017-03-24 16:07:20 +00001327 umbraPoint = fUmbraPolygon[this->getClosestUmbraPoint(pathPoint)];
Jim Van Verth91af7272017-01-27 14:15:54 -05001328 }
Brian Salomon66085ed2017-02-02 15:39:34 -05001329
Jim Van Verth91af7272017-01-27 14:15:54 -05001330 fPrevPoint = pathPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001331
1332 // merge "close" points
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001333 if (fPrevUmbraIndex == -1 ||
Brian Salomonab664fa2017-03-24 16:07:20 +00001334 !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) {
1335 *fPositions.push() = umbraPoint;
1336 *fColors.push() = fUmbraColor;
1337
1338 return false;
1339 } else {
1340 return true;
1341 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001342}
1343
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001344void SkSpotShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001345 // add next umbra point
Brian Salomonab664fa2017-03-24 16:07:20 +00001346 bool duplicate = this->addInnerPoint(nextPoint);
1347 int prevPenumbraIndex = duplicate ? fPositions.count()-1 : fPositions.count()-2;
1348 int currUmbraIndex = duplicate ? fPrevUmbraIndex : fPositions.count()-1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001349
Brian Salomonab664fa2017-03-24 16:07:20 +00001350 if (!duplicate) {
1351 // add to center fan if transparent or centroid showing
1352 if (fTransparent) {
1353 *fIndices.push() = 0;
1354 *fIndices.push() = fPrevUmbraIndex;
1355 *fIndices.push() = currUmbraIndex;
1356 // otherwise add to clip ring
1357 } else {
Brian Salomon66085ed2017-02-02 15:39:34 -05001358 SkPoint clipPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001359 bool isOutside = this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid,
1360 &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001361 if (isOutside) {
1362 *fPositions.push() = clipPoint;
1363 *fColors.push() = fUmbraColor;
1364
1365 *fIndices.push() = fPrevUmbraIndex;
1366 *fIndices.push() = currUmbraIndex;
1367 *fIndices.push() = currUmbraIndex + 1;
1368 if (fPrevUmbraOutside) {
1369 // fill out quad
1370 *fIndices.push() = fPrevUmbraIndex;
1371 *fIndices.push() = currUmbraIndex + 1;
1372 *fIndices.push() = fPrevUmbraIndex + 1;
1373 }
1374 } else if (fPrevUmbraOutside) {
1375 // add tri
1376 *fIndices.push() = fPrevUmbraIndex;
1377 *fIndices.push() = currUmbraIndex;
1378 *fIndices.push() = fPrevUmbraIndex + 1;
1379 }
1380 fPrevUmbraOutside = isOutside;
1381 }
1382 }
1383
1384 // add next penumbra point and quad
Jim Van Verth91af7272017-01-27 14:15:54 -05001385 SkPoint newPoint = nextPoint + nextNormal;
1386 *fPositions.push() = newPoint;
1387 *fColors.push() = fPenumbraColor;
1388
Brian Salomonab664fa2017-03-24 16:07:20 +00001389 if (!duplicate) {
1390 *fIndices.push() = fPrevUmbraIndex;
1391 *fIndices.push() = prevPenumbraIndex;
1392 *fIndices.push() = currUmbraIndex;
1393 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001394
Brian Salomon66085ed2017-02-02 15:39:34 -05001395 *fIndices.push() = prevPenumbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -05001396 *fIndices.push() = fPositions.count() - 1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001397 *fIndices.push() = currUmbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -05001398
Brian Salomon66085ed2017-02-02 15:39:34 -05001399 fPrevUmbraIndex = currUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -04001400 fPrevOutset = nextNormal;
Jim Van Verth91af7272017-01-27 14:15:54 -05001401}
Brian Salomon958fbc42017-01-30 17:01:28 -05001402
1403///////////////////////////////////////////////////////////////////////////////////////////////////
1404
Brian Salomonaff27a22017-02-06 15:47:44 -05001405sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001406 const SkPoint3& zPlane, bool transparent) {
1407 SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001408 return ambientTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001409}
1410
Brian Salomonaff27a22017-02-06 15:47:44 -05001411sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001412 const SkPoint3& zPlane, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -04001413 SkScalar lightRadius, bool transparent) {
Jim Van Verthe308a122017-05-08 14:19:30 -04001414 SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001415 return spotTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001416}