blob: 1a714076e7e4e606e7a69fb630398f24a455d104 [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
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 Salomonaff27a22017-02-06 15:47:44 -0500197void SkBaseShadowTessellator::handleQuad(const SkMatrix& m, SkPoint pts[3]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500198 m.mapPoints(pts, 3);
199 this->handleQuad(pts);
200}
201
Brian Salomonaff27a22017-02-06 15:47:44 -0500202void SkBaseShadowTessellator::handleCubic(const SkMatrix& m, SkPoint pts[4]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500203 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 Salomonaff27a22017-02-06 15:47:44 -0500223void SkBaseShadowTessellator::handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w) {
Jim Van Verthda965502017-04-11 15:29:14 -0400224 if (m.hasPerspective()) {
225 w = SkConic::TransformW(pts, w, m);
226 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500227 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 Verthe7e1d9d2017-05-01 16:06:48 -0400243bool SkBaseShadowTessellator::addArc(const SkVector& nextNormal, bool finishArc) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500244 // fill in fan from previous quad
245 SkScalar rotSin, rotCos;
246 int numSteps;
Jim Van Verth76387852017-05-16 16:55:16 -0400247 compute_radial_steps(fPrevOutset, nextNormal, fRadius, &rotSin, &rotCos, &numSteps);
248 SkVector prevNormal = fPrevOutset;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400249 for (int i = 0; i < numSteps-1; ++i) {
Jim Van Verthda965502017-04-11 15:29:14 -0400250 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 Vertha84898d2017-02-06 13:38:23 -0500254 *fColors.push() = fPenumbraColor;
255 *fIndices.push() = fPrevUmbraIndex;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500256 *fIndices.push() = fPositions.count() - 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400257 *fIndices.push() = fPositions.count() - 2;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500258
Jim Van Verthda965502017-04-11 15:29:14 -0400259 prevNormal = currNormal;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500260 }
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400261 if (finishArc && numSteps) {
Jim Van Verthda965502017-04-11 15:29:14 -0400262 *fPositions.push() = fPrevPoint + nextNormal;
263 *fColors.push() = fPenumbraColor;
264 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400265 *fIndices.push() = fPositions.count() - 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400266 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthda965502017-04-11 15:29:14 -0400267 }
Jim Van Verth76387852017-05-16 16:55:16 -0400268 fPrevOutset = nextNormal;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400269
270 return (numSteps > 0);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500271}
272
Jim Van Verthda965502017-04-11 15:29:14 -0400273bool SkBaseShadowTessellator::setTransformedHeightFunc(const SkMatrix& ctm) {
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400274 if (SkScalarNearlyZero(fZPlaneParams.fX) && SkScalarNearlyZero(fZPlaneParams.fY)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400275 fTransformedHeightFunc = [this](const SkPoint& p) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400276 return fZPlaneParams.fZ;
Jim Van Verthda965502017-04-11 15:29:14 -0400277 };
278 } else {
279 SkMatrix ctmInverse;
280 if (!ctm.invert(&ctmInverse)) {
281 return false;
282 }
Jim Van Verthda965502017-04-11 15:29:14 -0400283 // multiply by transpose
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400284 fTransformedZParams = SkPoint3::Make(
Jim Van Verthe308a122017-05-08 14:19:30 -0400285 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 Verthda965502017-04-11 15:29:14 -0400297
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400298 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 Verthda965502017-04-11 15:29:14 -0400311
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400312 // 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 Verthda965502017-04-11 15:29:14 -0400317
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400318 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 Verthda965502017-04-11 15:29:14 -0400333 }
334
335 return true;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500336}
337
338
339//////////////////////////////////////////////////////////////////////////////////////////////////
340
Brian Salomonaff27a22017-02-06 15:47:44 -0500341class SkAmbientShadowTessellator : public SkBaseShadowTessellator {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500342public:
343 SkAmbientShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400344 const SkPoint3& zPlaneParams, bool transparent);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500345
346private:
347 void handleLine(const SkPoint& p) override;
Jim Van Verthda965502017-04-11 15:29:14 -0400348 void addEdge(const SkVector& nextPoint, const SkVector& nextNormal);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500349
Jim Van Verthda965502017-04-11 15:29:14 -0400350 static constexpr auto kMaxEdgeLenSqr = 20 * 20;
Jim Van Verth76387852017-05-16 16:55:16 -0400351 static constexpr auto kInsetFactor = -0.5f;
Jim Van Verthda965502017-04-11 15:29:14 -0400352
353 SkScalar offset(SkScalar z) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400354 return SkDrawShadowMetrics::AmbientBlurRadius(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400355 }
356 SkColor umbraColor(SkScalar z) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400357 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(z));
Jim Van Verth060d9822017-05-04 09:58:17 -0400358 return SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
Jim Van Verthda965502017-04-11 15:29:14 -0400359 }
360
Jim Van Vertha84898d2017-02-06 13:38:23 -0500361 int fCentroidCount;
Jim Van Verth76387852017-05-16 16:55:16 -0400362 bool fSplitFirstEdge;
363 bool fSplitPreviousEdge;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500364
Brian Salomonaff27a22017-02-06 15:47:44 -0500365 typedef SkBaseShadowTessellator INHERITED;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500366};
367
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500368SkAmbientShadowTessellator::SkAmbientShadowTessellator(const SkPath& path,
Jim Van Vertha84898d2017-02-06 13:38:23 -0500369 const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400370 const SkPoint3& zPlaneParams,
Jim Van Verthbce74962017-01-25 09:39:46 -0500371 bool transparent)
Jim Van Verth76387852017-05-16 16:55:16 -0400372 : INHERITED(zPlaneParams, transparent)
373 , fSplitFirstEdge(false)
374 , fSplitPreviousEdge(false) {
Jim Van Verthda965502017-04-11 15:29:14 -0400375 // Set base colors
Jim Van Verth1af03d42017-07-31 09:34:58 -0400376 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(heightFunc(0, 0)));
Jim Van Verthb4366552017-03-27 14:25:29 -0400377 // 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 Verth060d9822017-05-04 09:58:17 -0400381 fUmbraColor = SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
382 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400383
Jim Van Verthda965502017-04-11 15:29:14 -0400384 // make sure we're not below the canvas plane
385 this->setZOffset(path.getBounds(), ctm.hasPerspective());
386
387 this->setTransformedHeightFunc(ctm);
388
Jim Van Verthbce74962017-01-25 09:39:46 -0500389 // Outer ring: 3*numPts
390 // Middle ring: numPts
391 fPositions.setReserve(4 * path.countPoints());
392 fColors.setReserve(4 * path.countPoints());
393 // Outer ring: 12*numPts
394 // Middle ring: 0
395 fIndices.setReserve(12 * path.countPoints());
396
Jim Van Verthbce74962017-01-25 09:39:46 -0500397 // walk around the path, tessellate and generate outer ring
398 // if original path is transparent, will accumulate sum of points for centroid
399 SkPath::Iter iter(path, true);
400 SkPoint pts[4];
401 SkPath::Verb verb;
402 if (fTransparent) {
403 *fPositions.push() = SkPoint::Make(0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400404 *fColors.push() = fUmbraColor;
Jim Van Verthbce74962017-01-25 09:39:46 -0500405 fCentroidCount = 0;
406 }
407 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
408 switch (verb) {
409 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500410 this->INHERITED::handleLine(ctm, &pts[1]);
Jim Van Verthbce74962017-01-25 09:39:46 -0500411 break;
412 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500413 this->handleQuad(ctm, pts);
Jim Van Verthbce74962017-01-25 09:39:46 -0500414 break;
415 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500416 this->handleCubic(ctm, pts);
Jim Van Verthbce74962017-01-25 09:39:46 -0500417 break;
418 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500419 this->handleConic(ctm, pts, iter.conicWeight());
Jim Van Verthbce74962017-01-25 09:39:46 -0500420 break;
421 case SkPath::kMove_Verb:
422 case SkPath::kClose_Verb:
423 case SkPath::kDone_Verb:
424 break;
425 }
426 }
427
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500428 if (!this->indexCount()) {
429 return;
430 }
431
Jim Van Verth76387852017-05-16 16:55:16 -0400432 // Finish up
Jim Van Verthbce74962017-01-25 09:39:46 -0500433 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -0400434 if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) {
435 SkScalar z = fTransformedHeightFunc(fPrevPoint);
436 fRadius = this->offset(z);
437 SkVector scaledNormal(normal);
438 scaledNormal *= fRadius;
439 this->addArc(scaledNormal, true);
Jim Van Verthbce74962017-01-25 09:39:46 -0500440
Jim Van Verth76387852017-05-16 16:55:16 -0400441 // fix-up the last and first umbra points
442 SkVector inset = normal;
443 // adding to an average, so multiply by an additional half
444 inset *= 0.5f*kInsetFactor;
445 fPositions[fPrevUmbraIndex] += inset;
446 fPositions[fFirstVertexIndex] += inset;
447 // we multiply by another half because now we're adding to an average of an average
448 inset *= 0.5f;
449 if (fSplitPreviousEdge) {
450 fPositions[fPrevUmbraIndex - 2] += inset;
451 }
452 if (fSplitFirstEdge) {
453 fPositions[fFirstVertexIndex + 2] += inset;
454 }
455
Jim Van Verthda965502017-04-11 15:29:14 -0400456 // set up for final edge
457 z = fTransformedHeightFunc(fFirstPoint);
458 normal *= this->offset(z);
Jim Van Verthbce74962017-01-25 09:39:46 -0500459
Jim Van Verthda965502017-04-11 15:29:14 -0400460 // make sure we don't end up with a sharp alpha edge along the quad diagonal
Jim Van Verth76387852017-05-16 16:55:16 -0400461 if (fColors[fPrevUmbraIndex] != fColors[fFirstVertexIndex] &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500462 SkPointPriv::DistanceToSqd(fFirstPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400463 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + fPositions[fFirstVertexIndex];
Jim Van Verthda965502017-04-11 15:29:14 -0400464 centerPoint *= 0.5f;
465 *fPositions.push() = centerPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400466 *fColors.push() = SkPMLerp(fColors[fFirstVertexIndex], fColors[fPrevUmbraIndex], 128);
467 centerPoint = fPositions[fPositions.count()-2] + fPositions[fFirstVertexIndex+1];
468 centerPoint *= 0.5f;
469 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400470 *fColors.push() = fPenumbraColor;
471
Jim Van Verth76387852017-05-16 16:55:16 -0400472 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
473 *fIndices.push() = fPrevUmbraIndex;
474 *fIndices.push() = fPositions.count() - 3;
475 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthda965502017-04-11 15:29:14 -0400476
Jim Van Verth76387852017-05-16 16:55:16 -0400477 *fIndices.push() = fPositions.count() - 3;
478 *fIndices.push() = fPositions.count() - 1;
479 *fIndices.push() = fPositions.count() - 2;
480 } else {
481 *fIndices.push() = fPrevUmbraIndex;
482 *fIndices.push() = fPositions.count() - 2;
483 *fIndices.push() = fPositions.count() - 1;
484
485 *fIndices.push() = fPrevUmbraIndex;
486 *fIndices.push() = fPositions.count() - 1;
487 *fIndices.push() = fPositions.count() - 3;
488 }
Jim Van Verthda965502017-04-11 15:29:14 -0400489
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400490 // if transparent, add point to first one in array and add to center fan
491 if (fTransparent) {
492 fPositions[0] += centerPoint;
493 ++fCentroidCount;
494
495 *fIndices.push() = 0;
496 *fIndices.push() = fPrevUmbraIndex;
497 *fIndices.push() = fPositions.count() - 2;
498 }
499
Jim Van Verthda965502017-04-11 15:29:14 -0400500 fPrevUmbraIndex = fPositions.count() - 2;
501 }
502
503 // final edge
Jim Van Vertha84898d2017-02-06 13:38:23 -0500504 *fPositions.push() = fFirstPoint + normal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500505 *fColors.push() = fPenumbraColor;
506
Jim Van Verth76387852017-05-16 16:55:16 -0400507 if (fColors[fPrevUmbraIndex] > fColors[fFirstVertexIndex]) {
Jim Van Verthda965502017-04-11 15:29:14 -0400508 *fIndices.push() = fPrevUmbraIndex;
509 *fIndices.push() = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400510 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500511
Jim Van Verthda965502017-04-11 15:29:14 -0400512 *fIndices.push() = fPositions.count() - 2;
513 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400514 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400515 } else {
516 *fIndices.push() = fPrevUmbraIndex;
517 *fIndices.push() = fPositions.count() - 2;
518 *fIndices.push() = fPositions.count() - 1;
519
520 *fIndices.push() = fPrevUmbraIndex;
521 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400522 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400523 }
Jim Van Verth76387852017-05-16 16:55:16 -0400524 fPrevOutset = normal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500525 }
526
527 // finalize centroid
528 if (fTransparent) {
529 fPositions[0] *= SkScalarFastInvert(fCentroidCount);
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400530 fColors[0] = this->umbraColor(fTransformedHeightFunc(fPositions[0]));
Jim Van Verthbce74962017-01-25 09:39:46 -0500531
532 *fIndices.push() = 0;
Brian Salomon66085ed2017-02-02 15:39:34 -0500533 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400534 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500535 }
536
537 // final fan
538 if (fPositions.count() >= 3) {
Jim Van Verth76387852017-05-16 16:55:16 -0400539 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500540 fPrevPoint = fFirstPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400541 fRadius = this->offset(fTransformedHeightFunc(fPrevPoint));
Jim Van Verth76387852017-05-16 16:55:16 -0400542 if (this->addArc(fFirstOutset, false)) {
543 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400544 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400545 *fIndices.push() = fFirstVertexIndex + 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400546 } else {
547 // arc is too small, set the first penumbra point to be the same position
548 // as the last one
Jim Van Verth76387852017-05-16 16:55:16 -0400549 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400550 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500551 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500552 fSucceeded = true;
Jim Van Verthbce74962017-01-25 09:39:46 -0500553}
554
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500555void SkAmbientShadowTessellator::handleLine(const SkPoint& p) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500556 if (fInitPoints.count() < 2) {
557 *fInitPoints.push() = p;
558 return;
559 }
560
561 if (fInitPoints.count() == 2) {
562 // determine if cw or ccw
563 SkVector v0 = fInitPoints[1] - fInitPoints[0];
564 SkVector v1 = p - fInitPoints[0];
565 SkScalar perpDot = v0.fX*v1.fY - v0.fY*v1.fX;
566 if (SkScalarNearlyZero(perpDot)) {
567 // nearly parallel, just treat as straight line and continue
568 fInitPoints[1] = p;
569 return;
570 }
571
572 // if perpDot > 0, winding is ccw
573 fDirection = (perpDot > 0) ? -1 : 1;
574
575 // add first quad
Jim Van Verthda965502017-04-11 15:29:14 -0400576 SkVector normal;
577 if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &normal)) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500578 // first two points are incident, make the third point the second and continue
579 fInitPoints[1] = p;
580 return;
581 }
582
Jim Van Vertha84898d2017-02-06 13:38:23 -0500583 fFirstPoint = fInitPoints[0];
Jim Van Verth76387852017-05-16 16:55:16 -0400584 fFirstVertexIndex = fPositions.count();
Jim Van Verthda965502017-04-11 15:29:14 -0400585 SkScalar z = fTransformedHeightFunc(fFirstPoint);
Jim Van Verth76387852017-05-16 16:55:16 -0400586 fFirstOutset = normal;
587 fFirstOutset *= this->offset(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400588
Jim Van Verth76387852017-05-16 16:55:16 -0400589 fPrevOutset = fFirstOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500590 fPrevPoint = fFirstPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400591 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500592
Jim Van Verthda965502017-04-11 15:29:14 -0400593 *fPositions.push() = fFirstPoint;
594 *fColors.push() = this->umbraColor(z);
Jim Van Verth76387852017-05-16 16:55:16 -0400595 *fPositions.push() = fFirstPoint + fFirstOutset;
Jim Van Verthbce74962017-01-25 09:39:46 -0500596 *fColors.push() = fPenumbraColor;
597 if (fTransparent) {
Jim Van Verthda965502017-04-11 15:29:14 -0400598 fPositions[0] += fFirstPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500599 fCentroidCount = 1;
600 }
Jim Van Verthda965502017-04-11 15:29:14 -0400601
602 // add the first quad
603 z = fTransformedHeightFunc(fInitPoints[1]);
604 fRadius = this->offset(z);
605 fUmbraColor = this->umbraColor(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400606 this->addEdge(fInitPoints[1], normal);
Jim Van Verthbce74962017-01-25 09:39:46 -0500607
608 // to ensure we skip this block next time
609 *fInitPoints.push() = p;
610 }
611
612 SkVector normal;
Jim Van Verth76387852017-05-16 16:55:16 -0400613 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400614 SkVector scaledNormal = normal;
615 scaledNormal *= fRadius;
616 this->addArc(scaledNormal, true);
617 SkScalar z = fTransformedHeightFunc(p);
618 fRadius = this->offset(z);
619 fUmbraColor = this->umbraColor(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400620 this->addEdge(p, normal);
Jim Van Verthbce74962017-01-25 09:39:46 -0500621 }
622}
623
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500624void SkAmbientShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) {
Jim Van Verth76387852017-05-16 16:55:16 -0400625 // We compute the inset in two stages: first we inset by half the current normal,
626 // then on the next addEdge() we add half of the next normal to get an average of the two
627 SkVector insetNormal = nextNormal;
628 insetNormal *= 0.5f*kInsetFactor;
629
630 // Adding the other half of the average for the previous edge
631 fPositions[fPrevUmbraIndex] += insetNormal;
632
633 SkPoint umbraPoint = nextPoint + insetNormal;
634 SkVector outsetNormal = nextNormal;
635 outsetNormal *= fRadius;
636 SkPoint penumbraPoint = nextPoint + outsetNormal;
637
638 // For split edges, we're adding an average of two averages, so we multiply by another half
639 if (fSplitPreviousEdge) {
640 insetNormal *= 0.5f;
641 fPositions[fPrevUmbraIndex - 2] += insetNormal;
642 }
643
644 // Split the edge to make sure we don't end up with a sharp alpha edge along the quad diagonal
Jim Van Verthda965502017-04-11 15:29:14 -0400645 if (fColors[fPrevUmbraIndex] != fUmbraColor &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500646 SkPointPriv::DistanceToSqd(nextPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400647
648 // This is lacking 1/4 of the next inset -- we'll add it the next time we call addEdge()
649 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + umbraPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400650 centerPoint *= 0.5f;
651 *fPositions.push() = centerPoint;
652 *fColors.push() = SkPMLerp(fUmbraColor, fColors[fPrevUmbraIndex], 128);
Jim Van Verth76387852017-05-16 16:55:16 -0400653 centerPoint = fPositions[fPositions.count()-2] + penumbraPoint;
654 centerPoint *= 0.5f;
655 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400656 *fColors.push() = fPenumbraColor;
657
658 // set triangularization to get best interpolation of color
659 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
660 *fIndices.push() = fPrevUmbraIndex;
661 *fIndices.push() = fPositions.count() - 3;
662 *fIndices.push() = fPositions.count() - 2;
663
664 *fIndices.push() = fPositions.count() - 3;
665 *fIndices.push() = fPositions.count() - 1;
666 *fIndices.push() = fPositions.count() - 2;
667 } else {
668 *fIndices.push() = fPrevUmbraIndex;
669 *fIndices.push() = fPositions.count() - 2;
670 *fIndices.push() = fPositions.count() - 1;
671
672 *fIndices.push() = fPrevUmbraIndex;
673 *fIndices.push() = fPositions.count() - 1;
674 *fIndices.push() = fPositions.count() - 3;
675 }
676
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400677 // if transparent, add point to first one in array and add to center fan
678 if (fTransparent) {
679 fPositions[0] += centerPoint;
680 ++fCentroidCount;
681
682 *fIndices.push() = 0;
683 *fIndices.push() = fPrevUmbraIndex;
684 *fIndices.push() = fPositions.count() - 2;
685 }
686
Jim Van Verth76387852017-05-16 16:55:16 -0400687 fSplitPreviousEdge = true;
688 if (fPrevUmbraIndex == fFirstVertexIndex) {
689 fSplitFirstEdge = true;
690 }
Jim Van Verthda965502017-04-11 15:29:14 -0400691 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400692 } else {
693 fSplitPreviousEdge = false;
Jim Van Verthda965502017-04-11 15:29:14 -0400694 }
695
Jim Van Verthbce74962017-01-25 09:39:46 -0500696 // add next quad
Jim Van Verth76387852017-05-16 16:55:16 -0400697 *fPositions.push() = umbraPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500698 *fColors.push() = fUmbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -0400699 *fPositions.push() = penumbraPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500700 *fColors.push() = fPenumbraColor;
701
Jim Van Verthda965502017-04-11 15:29:14 -0400702 // set triangularization to get best interpolation of color
703 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
704 *fIndices.push() = fPrevUmbraIndex;
705 *fIndices.push() = fPositions.count() - 3;
706 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthbce74962017-01-25 09:39:46 -0500707
Jim Van Verthda965502017-04-11 15:29:14 -0400708 *fIndices.push() = fPositions.count() - 3;
709 *fIndices.push() = fPositions.count() - 1;
710 *fIndices.push() = fPositions.count() - 2;
711 } else {
712 *fIndices.push() = fPrevUmbraIndex;
713 *fIndices.push() = fPositions.count() - 2;
714 *fIndices.push() = fPositions.count() - 1;
715
716 *fIndices.push() = fPrevUmbraIndex;
717 *fIndices.push() = fPositions.count() - 1;
718 *fIndices.push() = fPositions.count() - 3;
719 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500720
721 // if transparent, add point to first one in array and add to center fan
722 if (fTransparent) {
723 fPositions[0] += nextPoint;
724 ++fCentroidCount;
725
726 *fIndices.push() = 0;
Brian Salomon66085ed2017-02-02 15:39:34 -0500727 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500728 *fIndices.push() = fPositions.count() - 2;
729 }
730
Brian Salomon66085ed2017-02-02 15:39:34 -0500731 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500732 fPrevPoint = nextPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400733 fPrevOutset = outsetNormal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500734}
Jim Van Verth91af7272017-01-27 14:15:54 -0500735
736///////////////////////////////////////////////////////////////////////////////////////////////////
737
Brian Salomonaff27a22017-02-06 15:47:44 -0500738class SkSpotShadowTessellator : public SkBaseShadowTessellator {
Brian Salomon958fbc42017-01-30 17:01:28 -0500739public:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500740 SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400741 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -0400742 SkScalar lightRadius, bool transparent);
Brian Salomon958fbc42017-01-30 17:01:28 -0500743
Brian Salomon958fbc42017-01-30 17:01:28 -0500744private:
Brian Salomonab664fa2017-03-24 16:07:20 +0000745 void computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -0400746 const SkMatrix& shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000747 void computeClipVectorsAndTestCentroid();
Brian Salomon66085ed2017-02-02 15:39:34 -0500748 bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000749 int getClosestUmbraPoint(const SkPoint& point);
Brian Salomon958fbc42017-01-30 17:01:28 -0500750
Jim Van Vertha84898d2017-02-06 13:38:23 -0500751 void handleLine(const SkPoint& p) override;
Jim Van Verthb55eb282017-07-18 14:13:45 -0400752 bool handlePolyPoint(const SkPoint& p);
Brian Salomon958fbc42017-01-30 17:01:28 -0500753
754 void mapPoints(SkScalar scale, const SkVector& xlate, SkPoint* pts, int count);
Brian Salomonab664fa2017-03-24 16:07:20 +0000755 bool addInnerPoint(const SkPoint& pathPoint);
Jim Van Verthda965502017-04-11 15:29:14 -0400756 void addEdge(const SkVector& nextPoint, const SkVector& nextNormal);
757
758 SkScalar offset(SkScalar z) {
759 float zRatio = SkTPin(z / (fLightZ - z), 0.0f, 0.95f);
760 return fLightRadius*zRatio;
761 }
762
763 SkScalar fLightZ;
764 SkScalar fLightRadius;
765 SkScalar fOffsetAdjust;
Brian Salomon958fbc42017-01-30 17:01:28 -0500766
Brian Salomon958fbc42017-01-30 17:01:28 -0500767 SkTDArray<SkPoint> fClipPolygon;
Brian Salomon66085ed2017-02-02 15:39:34 -0500768 SkTDArray<SkVector> fClipVectors;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500769 SkPoint fCentroid;
Brian Salomonab664fa2017-03-24 16:07:20 +0000770 SkScalar fArea;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500771
Brian Salomonab664fa2017-03-24 16:07:20 +0000772 SkTDArray<SkPoint> fPathPolygon;
773 SkTDArray<SkPoint> fUmbraPolygon;
774 int fCurrClipPoint;
775 int fCurrUmbraPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -0500776 bool fPrevUmbraOutside;
777 bool fFirstUmbraOutside;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500778 bool fValidUmbra;
Brian Salomon958fbc42017-01-30 17:01:28 -0500779
Brian Salomonaff27a22017-02-06 15:47:44 -0500780 typedef SkBaseShadowTessellator INHERITED;
Brian Salomon958fbc42017-01-30 17:01:28 -0500781};
782
Jim Van Vertha84898d2017-02-06 13:38:23 -0500783SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400784 const SkPoint3& zPlaneParams,
Jim Van Verthb4366552017-03-27 14:25:29 -0400785 const SkPoint3& lightPos, SkScalar lightRadius,
Jim Van Verth060d9822017-05-04 09:58:17 -0400786 bool transparent)
Jim Van Verthe308a122017-05-08 14:19:30 -0400787 : INHERITED(zPlaneParams, transparent)
Jim Van Verthda965502017-04-11 15:29:14 -0400788 , fLightZ(lightPos.fZ)
789 , fLightRadius(lightRadius)
790 , fOffsetAdjust(0)
791 , fCurrClipPoint(0)
792 , fPrevUmbraOutside(false)
793 , fFirstUmbraOutside(false)
794 , fValidUmbra(true) {
795
796 // make sure we're not below the canvas plane
797 if (this->setZOffset(path.getBounds(), ctm.hasPerspective())) {
798 // Adjust light height and radius
799 fLightRadius *= (fLightZ + fZOffset) / fLightZ;
800 fLightZ += fZOffset;
801 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400802
803 // Set radius and colors
Jim Van Verthb4366552017-03-27 14:25:29 -0400804 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verthe308a122017-05-08 14:19:30 -0400805 SkScalar occluderHeight = this->heightFunc(center.fX, center.fY) + fZOffset;
Jim Van Verth060d9822017-05-04 09:58:17 -0400806 fUmbraColor = SkColorSetARGB(255, 0, 0, 0);
807 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400808
Jim Van Verth1af03d42017-07-31 09:34:58 -0400809 // Compute the blur radius, scale and translation for the spot shadow.
810 SkScalar radius;
Jim Van Verthda965502017-04-11 15:29:14 -0400811 SkMatrix shadowTransform;
812 if (!ctm.hasPerspective()) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400813 SkScalar scale;
814 SkVector translate;
815 SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY, fLightZ,
816 lightRadius, &radius, &scale, &translate);
Jim Van Verthda965502017-04-11 15:29:14 -0400817 shadowTransform.setScaleTranslate(scale, scale, translate.fX, translate.fY);
818 } else {
819 // For perspective, we have a scale, a z-shear, and another projective divide --
820 // this varies at each point so we can't use an affine transform.
821 // We'll just apply this to each generated point in turn.
822 shadowTransform.reset();
823 // Also can't cull the center (for now).
824 fTransparent = true;
Jim Van Verth1af03d42017-07-31 09:34:58 -0400825 radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius);
Jim Van Verthda965502017-04-11 15:29:14 -0400826 }
Jim Van Verth1af03d42017-07-31 09:34:58 -0400827 fRadius = radius;
Jim Van Verthda965502017-04-11 15:29:14 -0400828 SkMatrix fullTransform = SkMatrix::Concat(shadowTransform, ctm);
829
830 // Set up our reverse mapping
831 this->setTransformedHeightFunc(fullTransform);
Jim Van Verthb4366552017-03-27 14:25:29 -0400832
Brian Salomonab664fa2017-03-24 16:07:20 +0000833 // TODO: calculate these reserves better
Brian Salomon66085ed2017-02-02 15:39:34 -0500834 // Penumbra ring: 3*numPts
835 // Umbra ring: numPts
Jim Van Verth91af7272017-01-27 14:15:54 -0500836 // Inner ring: numPts
Brian Salomon66085ed2017-02-02 15:39:34 -0500837 fPositions.setReserve(5 * path.countPoints());
838 fColors.setReserve(5 * path.countPoints());
839 // Penumbra ring: 12*numPts
840 // Umbra ring: 3*numPts
841 fIndices.setReserve(15 * path.countPoints());
Brian Salomone7c85c42017-03-24 16:00:35 +0000842 fClipPolygon.setReserve(path.countPoints());
Brian Salomonab664fa2017-03-24 16:07:20 +0000843
844 // compute rough clip bounds for umbra, plus offset polygon, plus centroid
Jim Van Verthda965502017-04-11 15:29:14 -0400845 this->computeClipAndPathPolygons(path, ctm, shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000846 if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3) {
Brian Salomon66085ed2017-02-02 15:39:34 -0500847 return;
848 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500849
Brian Salomonab664fa2017-03-24 16:07:20 +0000850 // check to see if umbra collapses
Cary Clarkdf429f32017-11-08 11:44:31 -0500851 SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, fPathPolygon[0],
852 fPathPolygon[1]);
Jim Van Verthda965502017-04-11 15:29:14 -0400853 SkRect bounds;
854 bounds.setBounds(&fPathPolygon[0], fPathPolygon.count());
Brian Salomonab664fa2017-03-24 16:07:20 +0000855 for (int i = 1; i < fPathPolygon.count(); ++i) {
856 int j = i + 1;
857 if (i == fPathPolygon.count() - 1) {
858 j = 0;
859 }
860 SkPoint currPoint = fPathPolygon[i];
861 SkPoint nextPoint = fPathPolygon[j];
Cary Clarkdf429f32017-11-08 11:44:31 -0500862 SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint,
863 nextPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000864 if (distSq < minDistSq) {
865 minDistSq = distSq;
866 }
867 }
868 static constexpr auto kTolerance = 1.0e-2f;
869 if (minDistSq < (radius + kTolerance)*(radius + kTolerance)) {
870 // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha
871 SkScalar newRadius = SkScalarSqrt(minDistSq) - kTolerance;
Jim Van Verthda965502017-04-11 15:29:14 -0400872 fOffsetAdjust = newRadius - radius;
Jim Van Verthb6069df2017-04-28 11:00:35 -0400873 SkScalar ratio = 128 * (newRadius + radius) / radius;
Brian Salomonab664fa2017-03-24 16:07:20 +0000874 // they aren't PMColors, but the interpolation algorithm is the same
875 fUmbraColor = SkPMLerp(fUmbraColor, fPenumbraColor, (unsigned)ratio);
876 radius = newRadius;
877 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500878
Brian Salomonab664fa2017-03-24 16:07:20 +0000879 // compute vectors for clip tests
880 this->computeClipVectorsAndTestCentroid();
881
882 // generate inner ring
Jim Van Verthda965502017-04-11 15:29:14 -0400883 if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), radius,
884 &fUmbraPolygon)) {
Brian Salomonab664fa2017-03-24 16:07:20 +0000885 // this shouldn't happen, but just in case we'll inset using the centroid
886 fValidUmbra = false;
887 }
888
889 // walk around the path polygon, generate outer ring and connect to inner ring
Brian Salomon66085ed2017-02-02 15:39:34 -0500890 if (fTransparent) {
891 *fPositions.push() = fCentroid;
892 *fColors.push() = fUmbraColor;
893 }
Brian Salomonab664fa2017-03-24 16:07:20 +0000894 fCurrUmbraPoint = 0;
895 for (int i = 0; i < fPathPolygon.count(); ++i) {
Jim Van Verthb55eb282017-07-18 14:13:45 -0400896 if (!this->handlePolyPoint(fPathPolygon[i])) {
897 return;
898 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500899 }
900
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500901 if (!this->indexCount()) {
902 return;
903 }
904
Brian Salomonab664fa2017-03-24 16:07:20 +0000905 // finish up the final verts
Jim Van Verth91af7272017-01-27 14:15:54 -0500906 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -0400907 if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) {
908 normal *= fRadius;
909 this->addArc(normal, true);
Jim Van Verth91af7272017-01-27 14:15:54 -0500910
Brian Salomon66085ed2017-02-02 15:39:34 -0500911 // add to center fan
912 if (fTransparent) {
913 *fIndices.push() = 0;
914 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400915 *fIndices.push() = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -0500916 // or to clip ring
917 } else {
918 if (fFirstUmbraOutside) {
919 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400920 *fIndices.push() = fFirstVertexIndex;
921 *fIndices.push() = fFirstVertexIndex + 1;
Brian Salomon66085ed2017-02-02 15:39:34 -0500922 if (fPrevUmbraOutside) {
923 // fill out quad
924 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400925 *fIndices.push() = fFirstVertexIndex + 1;
Brian Salomon66085ed2017-02-02 15:39:34 -0500926 *fIndices.push() = fPrevUmbraIndex + 1;
927 }
928 } else if (fPrevUmbraOutside) {
929 // add tri
930 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400931 *fIndices.push() = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -0500932 *fIndices.push() = fPrevUmbraIndex + 1;
933 }
934 }
935
Jim Van Verth91af7272017-01-27 14:15:54 -0500936 // add final edge
937 *fPositions.push() = fFirstPoint + normal;
938 *fColors.push() = fPenumbraColor;
939
Brian Salomon66085ed2017-02-02 15:39:34 -0500940 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500941 *fIndices.push() = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400942 *fIndices.push() = fFirstVertexIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500943
944 *fIndices.push() = fPositions.count() - 2;
945 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400946 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400947
Jim Van Verth76387852017-05-16 16:55:16 -0400948 fPrevOutset = normal;
Jim Van Verth91af7272017-01-27 14:15:54 -0500949 }
950
951 // final fan
952 if (fPositions.count() >= 3) {
Jim Van Verth76387852017-05-16 16:55:16 -0400953 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500954 fPrevPoint = fFirstPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400955 if (this->addArc(fFirstOutset, false)) {
956 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400957 *fIndices.push() = fPositions.count() - 1;
958 if (fFirstUmbraOutside) {
Jim Van Verth76387852017-05-16 16:55:16 -0400959 *fIndices.push() = fFirstVertexIndex + 2;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400960 } else {
Jim Van Verth76387852017-05-16 16:55:16 -0400961 *fIndices.push() = fFirstVertexIndex + 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400962 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500963 } else {
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400964 // no arc added, fix up by setting first penumbra point position to last one
965 if (fFirstUmbraOutside) {
Jim Van Verth76387852017-05-16 16:55:16 -0400966 fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400967 } else {
Jim Van Verth76387852017-05-16 16:55:16 -0400968 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400969 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500970 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500971 }
Jim Van Verthda965502017-04-11 15:29:14 -0400972
973 if (ctm.hasPerspective()) {
974 for (int i = 0; i < fPositions.count(); ++i) {
975 SkScalar pathZ = fTransformedHeightFunc(fPositions[i]);
976 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
977 fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor;
978 fPositions[i].fY = (fPositions[i].fY*fLightZ - lightPos.fY*pathZ)*factor;
979 }
980#ifdef DRAW_CENTROID
981 SkScalar pathZ = fTransformedHeightFunc(fCentroid);
982 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
983 fCentroid.fX = (fCentroid.fX*fLightZ - lightPos.fX*pathZ)*factor;
984 fCentroid.fY = (fCentroid.fY*fLightZ - lightPos.fY*pathZ)*factor;
985#endif
986 }
987#ifdef DRAW_CENTROID
988 *fPositions.push() = fCentroid + SkVector::Make(-2, -2);
989 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
990 *fPositions.push() = fCentroid + SkVector::Make(2, -2);
991 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
992 *fPositions.push() = fCentroid + SkVector::Make(-2, 2);
993 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
994 *fPositions.push() = fCentroid + SkVector::Make(2, 2);
995 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
996
997 *fIndices.push() = fPositions.count() - 4;
998 *fIndices.push() = fPositions.count() - 2;
999 *fIndices.push() = fPositions.count() - 1;
1000
1001 *fIndices.push() = fPositions.count() - 4;
1002 *fIndices.push() = fPositions.count() - 1;
1003 *fIndices.push() = fPositions.count() - 3;
1004#endif
1005
Brian Salomon0dda9cb2017-02-03 10:33:25 -05001006 fSucceeded = true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001007}
1008
Brian Salomonab664fa2017-03-24 16:07:20 +00001009void SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -04001010 const SkMatrix& shadowTransform) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001011
1012 fPathPolygon.setReserve(path.countPoints());
1013
1014 // Walk around the path and compute clip polygon and path polygon.
1015 // Will also accumulate sum of areas for centroid.
1016 // For Bezier curves, we compute additional interior points on curve.
Jim Van Verth91af7272017-01-27 14:15:54 -05001017 SkPath::Iter iter(path, true);
1018 SkPoint pts[4];
1019 SkPath::Verb verb;
1020
Jim Van Verth91af7272017-01-27 14:15:54 -05001021 fClipPolygon.reset();
1022
Brian Salomonab664fa2017-03-24 16:07:20 +00001023 // init centroid
1024 fCentroid = SkPoint::Make(0, 0);
1025 fArea = 0;
1026
Brian Salomon66085ed2017-02-02 15:39:34 -05001027 // coefficients to compute cubic Bezier at t = 5/16
Brian Salomonab664fa2017-03-24 16:07:20 +00001028 static constexpr SkScalar kA = 0.32495117187f;
1029 static constexpr SkScalar kB = 0.44311523437f;
1030 static constexpr SkScalar kC = 0.20141601562f;
1031 static constexpr SkScalar kD = 0.03051757812f;
Brian Salomon66085ed2017-02-02 15:39:34 -05001032
1033 SkPoint curvePoint;
1034 SkScalar w;
Jim Van Verth91af7272017-01-27 14:15:54 -05001035 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
1036 switch (verb) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001037 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001038 ctm.mapPoints(&pts[1], 1);
Jim Van Verth91af7272017-01-27 14:15:54 -05001039 *fClipPolygon.push() = pts[1];
Brian Salomonab664fa2017-03-24 16:07:20 +00001040 this->INHERITED::handleLine(shadowTransform, &pts[1]);
Jim Van Verth91af7272017-01-27 14:15:54 -05001041 break;
1042 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001043 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001044 // point at t = 1/2
1045 curvePoint.fX = 0.25f*pts[0].fX + 0.5f*pts[1].fX + 0.25f*pts[2].fX;
1046 curvePoint.fY = 0.25f*pts[0].fY + 0.5f*pts[1].fY + 0.25f*pts[2].fY;
1047 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001048 *fClipPolygon.push() = pts[2];
Brian Salomonab664fa2017-03-24 16:07:20 +00001049 this->handleQuad(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001050 break;
1051 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001052 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001053 w = iter.conicWeight();
Jim Van Vertha84898d2017-02-06 13:38:23 -05001054 // point at t = 1/2
Brian Salomon66085ed2017-02-02 15:39:34 -05001055 curvePoint.fX = 0.25f*pts[0].fX + w*0.5f*pts[1].fX + 0.25f*pts[2].fX;
1056 curvePoint.fY = 0.25f*pts[0].fY + w*0.5f*pts[1].fY + 0.25f*pts[2].fY;
1057 curvePoint *= SkScalarInvert(0.5f + 0.5f*w);
1058 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001059 *fClipPolygon.push() = pts[2];
Brian Salomonab664fa2017-03-24 16:07:20 +00001060 this->handleConic(shadowTransform, pts, w);
Jim Van Verth91af7272017-01-27 14:15:54 -05001061 break;
1062 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001063 ctm.mapPoints(pts, 4);
Brian Salomon66085ed2017-02-02 15:39:34 -05001064 // point at t = 5/16
1065 curvePoint.fX = kA*pts[0].fX + kB*pts[1].fX + kC*pts[2].fX + kD*pts[3].fX;
1066 curvePoint.fY = kA*pts[0].fY + kB*pts[1].fY + kC*pts[2].fY + kD*pts[3].fY;
1067 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001068 // point at t = 11/16
1069 curvePoint.fX = kD*pts[0].fX + kC*pts[1].fX + kB*pts[2].fX + kA*pts[3].fX;
1070 curvePoint.fY = kD*pts[0].fY + kC*pts[1].fY + kB*pts[2].fY + kA*pts[3].fY;
1071 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001072 *fClipPolygon.push() = pts[3];
Brian Salomonab664fa2017-03-24 16:07:20 +00001073 this->handleCubic(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001074 break;
Brian Salomonab664fa2017-03-24 16:07:20 +00001075 case SkPath::kMove_Verb:
Jim Van Verth91af7272017-01-27 14:15:54 -05001076 case SkPath::kClose_Verb:
Brian Salomonab664fa2017-03-24 16:07:20 +00001077 case SkPath::kDone_Verb:
Jim Van Verth91af7272017-01-27 14:15:54 -05001078 break;
1079 default:
1080 SkDEBUGFAIL("unknown verb");
1081 }
1082 }
1083
Brian Salomonab664fa2017-03-24 16:07:20 +00001084 // finish centroid
1085 if (fPathPolygon.count() > 0) {
1086 SkPoint currPoint = fPathPolygon[fPathPolygon.count() - 1];
1087 SkPoint nextPoint = fPathPolygon[0];
1088 SkScalar quadArea = currPoint.cross(nextPoint);
1089 fCentroid.fX += (currPoint.fX + nextPoint.fX) * quadArea;
1090 fCentroid.fY += (currPoint.fY + nextPoint.fY) * quadArea;
1091 fArea += quadArea;
1092 fCentroid *= SK_Scalar1 / (3 * fArea);
1093 }
1094
1095 fCurrClipPoint = fClipPolygon.count() - 1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001096}
1097
Brian Salomonab664fa2017-03-24 16:07:20 +00001098void SkSpotShadowTessellator::computeClipVectorsAndTestCentroid() {
Brian Salomon66085ed2017-02-02 15:39:34 -05001099 SkASSERT(fClipPolygon.count() >= 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001100
Brian Salomonab664fa2017-03-24 16:07:20 +00001101 // init clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001102 SkVector v0 = fClipPolygon[1] - fClipPolygon[0];
1103 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001104
1105 // init centroid check
1106 bool hiddenCentroid = true;
Brian Salomonab664fa2017-03-24 16:07:20 +00001107 SkVector v1 = fCentroid - fClipPolygon[0];
Brian Salomon66085ed2017-02-02 15:39:34 -05001108 SkScalar initCross = v0.cross(v1);
1109
1110 for (int p = 1; p < fClipPolygon.count(); ++p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001111 // add to clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001112 v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p];
1113 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001114 // Determine if transformed centroid is inside clipPolygon.
Brian Salomonab664fa2017-03-24 16:07:20 +00001115 v1 = fCentroid - fClipPolygon[p];
Brian Salomon66085ed2017-02-02 15:39:34 -05001116 if (initCross*v0.cross(v1) <= 0) {
1117 hiddenCentroid = false;
1118 }
1119 }
1120 SkASSERT(fClipVectors.count() == fClipPolygon.count());
1121
Brian Salomonab664fa2017-03-24 16:07:20 +00001122 fTransparent = fTransparent || !hiddenCentroid;
Brian Salomon66085ed2017-02-02 15:39:34 -05001123}
1124
1125bool SkSpotShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid,
1126 SkPoint* clipPoint) {
1127 SkVector segmentVector = centroid - umbraPoint;
1128
Brian Salomonab664fa2017-03-24 16:07:20 +00001129 int startClipPoint = fCurrClipPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001130 do {
Brian Salomonab664fa2017-03-24 16:07:20 +00001131 SkVector dp = umbraPoint - fClipPolygon[fCurrClipPoint];
1132 SkScalar denom = fClipVectors[fCurrClipPoint].cross(segmentVector);
Brian Salomon66085ed2017-02-02 15:39:34 -05001133 SkScalar t_num = dp.cross(segmentVector);
1134 // if line segments are nearly parallel
1135 if (SkScalarNearlyZero(denom)) {
1136 // and collinear
1137 if (SkScalarNearlyZero(t_num)) {
1138 return false;
1139 }
1140 // otherwise are separate, will try the next poly segment
1141 // else if crossing lies within poly segment
1142 } else if (t_num >= 0 && t_num <= denom) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001143 SkScalar s_num = dp.cross(fClipVectors[fCurrClipPoint]);
Brian Salomon66085ed2017-02-02 15:39:34 -05001144 // if umbra point is inside the clip polygon
Jim Van Verthda965502017-04-11 15:29:14 -04001145 if (s_num >= 0 && s_num <= denom) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001146 segmentVector *= s_num/denom;
1147 *clipPoint = umbraPoint + segmentVector;
1148 return true;
1149 }
1150 }
Brian Salomonab664fa2017-03-24 16:07:20 +00001151 fCurrClipPoint = (fCurrClipPoint + 1) % fClipPolygon.count();
1152 } while (fCurrClipPoint != startClipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001153
1154 return false;
Jim Van Verth91af7272017-01-27 14:15:54 -05001155}
1156
Brian Salomonab664fa2017-03-24 16:07:20 +00001157int SkSpotShadowTessellator::getClosestUmbraPoint(const SkPoint& p) {
Cary Clarkdf429f32017-11-08 11:44:31 -05001158 SkScalar minDistance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[fCurrUmbraPoint]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001159 int index = fCurrUmbraPoint;
1160 int dir = 1;
1161 int next = (index + dir) % fUmbraPolygon.count();
1162
1163 // init travel direction
Cary Clarkdf429f32017-11-08 11:44:31 -05001164 SkScalar distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001165 if (distance < minDistance) {
1166 index = next;
1167 minDistance = distance;
1168 } else {
1169 dir = fUmbraPolygon.count()-1;
1170 }
1171
1172 // iterate until we find a point that increases the distance
1173 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001174 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001175 while (distance < minDistance) {
1176 index = next;
1177 minDistance = distance;
1178 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001179 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001180 }
1181
1182 fCurrUmbraPoint = index;
1183 return index;
1184}
1185
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001186void SkSpotShadowTessellator::mapPoints(SkScalar scale, const SkVector& xlate,
Jim Van Verth91af7272017-01-27 14:15:54 -05001187 SkPoint* pts, int count) {
1188 // TODO: vectorize
1189 for (int i = 0; i < count; ++i) {
1190 pts[i] *= scale;
1191 pts[i] += xlate;
1192 }
1193}
1194
Brian Salomonab664fa2017-03-24 16:07:20 +00001195static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) {
1196 static constexpr SkScalar kClose = (SK_Scalar1 / 16);
1197 static constexpr SkScalar kCloseSqd = kClose*kClose;
1198
Cary Clarkdf429f32017-11-08 11:44:31 -05001199 SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1);
Brian Salomonab664fa2017-03-24 16:07:20 +00001200 return distSq < kCloseSqd;
1201}
1202
Jim Van Verthb55eb282017-07-18 14:13:45 -04001203static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001204 SkVector v0 = p1 - p0;
1205 SkVector v1 = p2 - p0;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001206 return v0.cross(v1);
1207}
1208
1209static bool is_collinear(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
1210 return (SkScalarNearlyZero(perp_dot(p0, p1, p2)));
Brian Salomonab664fa2017-03-24 16:07:20 +00001211}
1212
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001213void SkSpotShadowTessellator::handleLine(const SkPoint& p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001214 // remove coincident points and add to centroid
1215 if (fPathPolygon.count() > 0) {
1216 const SkPoint& lastPoint = fPathPolygon[fPathPolygon.count() - 1];
1217 if (duplicate_pt(p, lastPoint)) {
1218 return;
1219 }
1220 SkScalar quadArea = lastPoint.cross(p);
1221 fCentroid.fX += (p.fX + lastPoint.fX) * quadArea;
1222 fCentroid.fY += (p.fY + lastPoint.fY) * quadArea;
1223 fArea += quadArea;
1224 }
1225
1226 // try to remove collinear points
1227 if (fPathPolygon.count() > 1 && is_collinear(fPathPolygon[fPathPolygon.count()-2],
1228 fPathPolygon[fPathPolygon.count()-1],
1229 p)) {
1230 fPathPolygon[fPathPolygon.count() - 1] = p;
1231 } else {
1232 *fPathPolygon.push() = p;
1233 }
1234}
1235
Jim Van Verthb55eb282017-07-18 14:13:45 -04001236bool SkSpotShadowTessellator::handlePolyPoint(const SkPoint& p) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001237 if (fInitPoints.count() < 2) {
1238 *fInitPoints.push() = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001239 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001240 }
1241
1242 if (fInitPoints.count() == 2) {
1243 // determine if cw or ccw
Jim Van Verthb55eb282017-07-18 14:13:45 -04001244 SkScalar perpDot = perp_dot(fInitPoints[0], fInitPoints[1], p);
Jim Van Verth91af7272017-01-27 14:15:54 -05001245 if (SkScalarNearlyZero(perpDot)) {
1246 // nearly parallel, just treat as straight line and continue
1247 fInitPoints[1] = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001248 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001249 }
1250
1251 // if perpDot > 0, winding is ccw
1252 fDirection = (perpDot > 0) ? -1 : 1;
1253
1254 // add first quad
Jim Van Verth76387852017-05-16 16:55:16 -04001255 if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &fFirstOutset)) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001256 // first two points are incident, make the third point the second 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
Jim Van Verth76387852017-05-16 16:55:16 -04001261 fFirstOutset *= fRadius;
Jim Van Verth91af7272017-01-27 14:15:54 -05001262 fFirstPoint = fInitPoints[0];
Jim Van Verth76387852017-05-16 16:55:16 -04001263 fFirstVertexIndex = fPositions.count();
1264 fPrevOutset = fFirstOutset;
Jim Van Verth91af7272017-01-27 14:15:54 -05001265 fPrevPoint = fFirstPoint;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001266 fPrevUmbraIndex = -1;
Jim Van Verth91af7272017-01-27 14:15:54 -05001267
Brian Salomon66085ed2017-02-02 15:39:34 -05001268 this->addInnerPoint(fFirstPoint);
Jim Van Verth76387852017-05-16 16:55:16 -04001269 fPrevUmbraIndex = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -05001270
1271 if (!fTransparent) {
1272 SkPoint clipPoint;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001273 bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex],
1274 fCentroid, &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001275 if (isOutside) {
1276 *fPositions.push() = clipPoint;
1277 *fColors.push() = fUmbraColor;
1278 }
1279 fPrevUmbraOutside = isOutside;
1280 fFirstUmbraOutside = isOutside;
1281 }
1282
Jim Van Verth76387852017-05-16 16:55:16 -04001283 SkPoint newPoint = fFirstPoint + fFirstOutset;
Jim Van Verth91af7272017-01-27 14:15:54 -05001284 *fPositions.push() = newPoint;
1285 *fColors.push() = fPenumbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -04001286 this->addEdge(fInitPoints[1], fFirstOutset);
Jim Van Verth91af7272017-01-27 14:15:54 -05001287
1288 // to ensure we skip this block next time
1289 *fInitPoints.push() = p;
1290 }
1291
Jim Van Verthb55eb282017-07-18 14:13:45 -04001292 // if concave, abort
1293 SkScalar perpDot = perp_dot(fInitPoints[1], fInitPoints[2], p);
1294 if (fDirection*perpDot > 0) {
1295 return false;
1296 }
1297
Jim Van Verth91af7272017-01-27 14:15:54 -05001298 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -04001299 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
1300 normal *= fRadius;
1301 this->addArc(normal, true);
1302 this->addEdge(p, normal);
Jim Van Verthb55eb282017-07-18 14:13:45 -04001303 fInitPoints[1] = fInitPoints[2];
1304 fInitPoints[2] = p;
Jim Van Verth91af7272017-01-27 14:15:54 -05001305 }
Jim Van Verthb55eb282017-07-18 14:13:45 -04001306
1307 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001308}
1309
Brian Salomonab664fa2017-03-24 16:07:20 +00001310bool SkSpotShadowTessellator::addInnerPoint(const SkPoint& pathPoint) {
1311 SkPoint umbraPoint;
1312 if (!fValidUmbra) {
1313 SkVector v = fCentroid - pathPoint;
1314 v *= 0.95f;
1315 umbraPoint = pathPoint + v;
Jim Van Verth91af7272017-01-27 14:15:54 -05001316 } else {
Brian Salomonab664fa2017-03-24 16:07:20 +00001317 umbraPoint = fUmbraPolygon[this->getClosestUmbraPoint(pathPoint)];
Jim Van Verth91af7272017-01-27 14:15:54 -05001318 }
Brian Salomon66085ed2017-02-02 15:39:34 -05001319
Jim Van Verth91af7272017-01-27 14:15:54 -05001320 fPrevPoint = pathPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001321
1322 // merge "close" points
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001323 if (fPrevUmbraIndex == -1 ||
Brian Salomonab664fa2017-03-24 16:07:20 +00001324 !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) {
1325 *fPositions.push() = umbraPoint;
1326 *fColors.push() = fUmbraColor;
1327
1328 return false;
1329 } else {
1330 return true;
1331 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001332}
1333
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001334void SkSpotShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001335 // add next umbra point
Brian Salomonab664fa2017-03-24 16:07:20 +00001336 bool duplicate = this->addInnerPoint(nextPoint);
1337 int prevPenumbraIndex = duplicate ? fPositions.count()-1 : fPositions.count()-2;
1338 int currUmbraIndex = duplicate ? fPrevUmbraIndex : fPositions.count()-1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001339
Brian Salomonab664fa2017-03-24 16:07:20 +00001340 if (!duplicate) {
1341 // add to center fan if transparent or centroid showing
1342 if (fTransparent) {
1343 *fIndices.push() = 0;
1344 *fIndices.push() = fPrevUmbraIndex;
1345 *fIndices.push() = currUmbraIndex;
1346 // otherwise add to clip ring
1347 } else {
Brian Salomon66085ed2017-02-02 15:39:34 -05001348 SkPoint clipPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001349 bool isOutside = this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid,
1350 &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001351 if (isOutside) {
1352 *fPositions.push() = clipPoint;
1353 *fColors.push() = fUmbraColor;
1354
1355 *fIndices.push() = fPrevUmbraIndex;
1356 *fIndices.push() = currUmbraIndex;
1357 *fIndices.push() = currUmbraIndex + 1;
1358 if (fPrevUmbraOutside) {
1359 // fill out quad
1360 *fIndices.push() = fPrevUmbraIndex;
1361 *fIndices.push() = currUmbraIndex + 1;
1362 *fIndices.push() = fPrevUmbraIndex + 1;
1363 }
1364 } else if (fPrevUmbraOutside) {
1365 // add tri
1366 *fIndices.push() = fPrevUmbraIndex;
1367 *fIndices.push() = currUmbraIndex;
1368 *fIndices.push() = fPrevUmbraIndex + 1;
1369 }
1370 fPrevUmbraOutside = isOutside;
1371 }
1372 }
1373
1374 // add next penumbra point and quad
Jim Van Verth91af7272017-01-27 14:15:54 -05001375 SkPoint newPoint = nextPoint + nextNormal;
1376 *fPositions.push() = newPoint;
1377 *fColors.push() = fPenumbraColor;
1378
Brian Salomonab664fa2017-03-24 16:07:20 +00001379 if (!duplicate) {
1380 *fIndices.push() = fPrevUmbraIndex;
1381 *fIndices.push() = prevPenumbraIndex;
1382 *fIndices.push() = currUmbraIndex;
1383 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001384
Brian Salomon66085ed2017-02-02 15:39:34 -05001385 *fIndices.push() = prevPenumbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -05001386 *fIndices.push() = fPositions.count() - 1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001387 *fIndices.push() = currUmbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -05001388
Brian Salomon66085ed2017-02-02 15:39:34 -05001389 fPrevUmbraIndex = currUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -04001390 fPrevOutset = nextNormal;
Jim Van Verth91af7272017-01-27 14:15:54 -05001391}
Brian Salomon958fbc42017-01-30 17:01:28 -05001392
1393///////////////////////////////////////////////////////////////////////////////////////////////////
1394
Brian Salomonaff27a22017-02-06 15:47:44 -05001395sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001396 const SkPoint3& zPlane, bool transparent) {
1397 SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001398 return ambientTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001399}
1400
Brian Salomonaff27a22017-02-06 15:47:44 -05001401sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001402 const SkPoint3& zPlane, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -04001403 SkScalar lightRadius, bool transparent) {
Jim Van Verthe308a122017-05-08 14:19:30 -04001404 SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001405 return spotTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001406}