blob: 23754292b0a08f065cb428e66f9cf8acfa94737e [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 Verth22526362018-02-28 14:51:19 -0500381 // TODO: support some concave paths
382 if (!path.isConvex()) {
383 return;
384 }
385
Jim Van Verthda965502017-04-11 15:29:14 -0400386 // Set base colors
Jim Van Verth1af03d42017-07-31 09:34:58 -0400387 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(heightFunc(0, 0)));
Jim Van Verthb4366552017-03-27 14:25:29 -0400388 // umbraColor is the interior value, penumbraColor the exterior value.
389 // umbraAlpha is the factor that is linearly interpolated from outside to inside, and
390 // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get
391 // the final alpha.
Jim Van Verth060d9822017-05-04 09:58:17 -0400392 fUmbraColor = SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
393 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400394
Jim Van Verthda965502017-04-11 15:29:14 -0400395 // make sure we're not below the canvas plane
396 this->setZOffset(path.getBounds(), ctm.hasPerspective());
397
Jim Van Verth7c8008c2018-02-07 15:02:50 -0500398 if (!this->setTransformedHeightFunc(ctm)) {
399 return;
400 }
Jim Van Verthda965502017-04-11 15:29:14 -0400401
Jim Van Verthbce74962017-01-25 09:39:46 -0500402 // Outer ring: 3*numPts
403 // Middle ring: numPts
404 fPositions.setReserve(4 * path.countPoints());
405 fColors.setReserve(4 * path.countPoints());
406 // Outer ring: 12*numPts
407 // Middle ring: 0
408 fIndices.setReserve(12 * path.countPoints());
409
Jim Van Verthbce74962017-01-25 09:39:46 -0500410 // walk around the path, tessellate and generate outer ring
411 // if original path is transparent, will accumulate sum of points for centroid
412 SkPath::Iter iter(path, true);
413 SkPoint pts[4];
414 SkPath::Verb verb;
415 if (fTransparent) {
416 *fPositions.push() = SkPoint::Make(0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400417 *fColors.push() = fUmbraColor;
Jim Van Verthbce74962017-01-25 09:39:46 -0500418 fCentroidCount = 0;
419 }
420 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
421 switch (verb) {
422 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500423 this->INHERITED::handleLine(ctm, &pts[1]);
Jim Van Verthbce74962017-01-25 09:39:46 -0500424 break;
425 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500426 this->handleQuad(ctm, pts);
Jim Van Verthbce74962017-01-25 09:39:46 -0500427 break;
428 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500429 this->handleCubic(ctm, pts);
Jim Van Verthbce74962017-01-25 09:39:46 -0500430 break;
431 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500432 this->handleConic(ctm, pts, iter.conicWeight());
Jim Van Verthbce74962017-01-25 09:39:46 -0500433 break;
434 case SkPath::kMove_Verb:
435 case SkPath::kClose_Verb:
436 case SkPath::kDone_Verb:
437 break;
438 }
439 }
440
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500441 if (!this->indexCount()) {
442 return;
443 }
444
Jim Van Verth76387852017-05-16 16:55:16 -0400445 // Finish up
Jim Van Verthbce74962017-01-25 09:39:46 -0500446 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -0400447 if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) {
448 SkScalar z = fTransformedHeightFunc(fPrevPoint);
449 fRadius = this->offset(z);
450 SkVector scaledNormal(normal);
451 scaledNormal *= fRadius;
452 this->addArc(scaledNormal, true);
Jim Van Verthbce74962017-01-25 09:39:46 -0500453
Jim Van Verth76387852017-05-16 16:55:16 -0400454 // fix-up the last and first umbra points
455 SkVector inset = normal;
456 // adding to an average, so multiply by an additional half
457 inset *= 0.5f*kInsetFactor;
458 fPositions[fPrevUmbraIndex] += inset;
459 fPositions[fFirstVertexIndex] += inset;
460 // we multiply by another half because now we're adding to an average of an average
461 inset *= 0.5f;
462 if (fSplitPreviousEdge) {
463 fPositions[fPrevUmbraIndex - 2] += inset;
464 }
465 if (fSplitFirstEdge) {
466 fPositions[fFirstVertexIndex + 2] += inset;
467 }
468
Jim Van Verthda965502017-04-11 15:29:14 -0400469 // set up for final edge
470 z = fTransformedHeightFunc(fFirstPoint);
471 normal *= this->offset(z);
Jim Van Verthbce74962017-01-25 09:39:46 -0500472
Jim Van Verthda965502017-04-11 15:29:14 -0400473 // make sure we don't end up with a sharp alpha edge along the quad diagonal
Jim Van Verth76387852017-05-16 16:55:16 -0400474 if (fColors[fPrevUmbraIndex] != fColors[fFirstVertexIndex] &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500475 SkPointPriv::DistanceToSqd(fFirstPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400476 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + fPositions[fFirstVertexIndex];
Jim Van Verthda965502017-04-11 15:29:14 -0400477 centerPoint *= 0.5f;
478 *fPositions.push() = centerPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400479 *fColors.push() = SkPMLerp(fColors[fFirstVertexIndex], fColors[fPrevUmbraIndex], 128);
480 centerPoint = fPositions[fPositions.count()-2] + fPositions[fFirstVertexIndex+1];
481 centerPoint *= 0.5f;
482 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400483 *fColors.push() = fPenumbraColor;
484
Jim Van Verth76387852017-05-16 16:55:16 -0400485 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
486 *fIndices.push() = fPrevUmbraIndex;
487 *fIndices.push() = fPositions.count() - 3;
488 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthda965502017-04-11 15:29:14 -0400489
Jim Van Verth76387852017-05-16 16:55:16 -0400490 *fIndices.push() = fPositions.count() - 3;
491 *fIndices.push() = fPositions.count() - 1;
492 *fIndices.push() = fPositions.count() - 2;
493 } else {
494 *fIndices.push() = fPrevUmbraIndex;
495 *fIndices.push() = fPositions.count() - 2;
496 *fIndices.push() = fPositions.count() - 1;
497
498 *fIndices.push() = fPrevUmbraIndex;
499 *fIndices.push() = fPositions.count() - 1;
500 *fIndices.push() = fPositions.count() - 3;
501 }
Jim Van Verthda965502017-04-11 15:29:14 -0400502
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400503 // if transparent, add point to first one in array and add to center fan
504 if (fTransparent) {
505 fPositions[0] += centerPoint;
506 ++fCentroidCount;
507
508 *fIndices.push() = 0;
509 *fIndices.push() = fPrevUmbraIndex;
510 *fIndices.push() = fPositions.count() - 2;
511 }
512
Jim Van Verthda965502017-04-11 15:29:14 -0400513 fPrevUmbraIndex = fPositions.count() - 2;
514 }
515
516 // final edge
Jim Van Vertha84898d2017-02-06 13:38:23 -0500517 *fPositions.push() = fFirstPoint + normal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500518 *fColors.push() = fPenumbraColor;
519
Jim Van Verth76387852017-05-16 16:55:16 -0400520 if (fColors[fPrevUmbraIndex] > fColors[fFirstVertexIndex]) {
Jim Van Verthda965502017-04-11 15:29:14 -0400521 *fIndices.push() = fPrevUmbraIndex;
522 *fIndices.push() = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400523 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500524
Jim Van Verthda965502017-04-11 15:29:14 -0400525 *fIndices.push() = fPositions.count() - 2;
526 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400527 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400528 } else {
529 *fIndices.push() = fPrevUmbraIndex;
530 *fIndices.push() = fPositions.count() - 2;
531 *fIndices.push() = fPositions.count() - 1;
532
533 *fIndices.push() = fPrevUmbraIndex;
534 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400535 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400536 }
Jim Van Verth76387852017-05-16 16:55:16 -0400537 fPrevOutset = normal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500538 }
539
540 // finalize centroid
541 if (fTransparent) {
542 fPositions[0] *= SkScalarFastInvert(fCentroidCount);
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400543 fColors[0] = this->umbraColor(fTransformedHeightFunc(fPositions[0]));
Jim Van Verthbce74962017-01-25 09:39:46 -0500544
545 *fIndices.push() = 0;
Brian Salomon66085ed2017-02-02 15:39:34 -0500546 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400547 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500548 }
549
550 // final fan
551 if (fPositions.count() >= 3) {
Jim Van Verth76387852017-05-16 16:55:16 -0400552 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500553 fPrevPoint = fFirstPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400554 fRadius = this->offset(fTransformedHeightFunc(fPrevPoint));
Jim Van Verth76387852017-05-16 16:55:16 -0400555 if (this->addArc(fFirstOutset, false)) {
556 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400557 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400558 *fIndices.push() = fFirstVertexIndex + 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400559 } else {
560 // arc is too small, set the first penumbra point to be the same position
561 // as the last one
Jim Van Verth76387852017-05-16 16:55:16 -0400562 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400563 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500564 }
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500565 fSucceeded = true;
Jim Van Verthbce74962017-01-25 09:39:46 -0500566}
567
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500568void SkAmbientShadowTessellator::handleLine(const SkPoint& p) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500569 if (fInitPoints.count() < 2) {
570 *fInitPoints.push() = p;
571 return;
572 }
573
574 if (fInitPoints.count() == 2) {
575 // determine if cw or ccw
576 SkVector v0 = fInitPoints[1] - fInitPoints[0];
577 SkVector v1 = p - fInitPoints[0];
578 SkScalar perpDot = v0.fX*v1.fY - v0.fY*v1.fX;
579 if (SkScalarNearlyZero(perpDot)) {
580 // nearly parallel, just treat as straight line and continue
581 fInitPoints[1] = p;
582 return;
583 }
584
585 // if perpDot > 0, winding is ccw
586 fDirection = (perpDot > 0) ? -1 : 1;
587
588 // add first quad
Jim Van Verthda965502017-04-11 15:29:14 -0400589 SkVector normal;
590 if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &normal)) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500591 // first two points are incident, make the third point the second and continue
592 fInitPoints[1] = p;
593 return;
594 }
595
Jim Van Vertha84898d2017-02-06 13:38:23 -0500596 fFirstPoint = fInitPoints[0];
Jim Van Verth76387852017-05-16 16:55:16 -0400597 fFirstVertexIndex = fPositions.count();
Jim Van Verthda965502017-04-11 15:29:14 -0400598 SkScalar z = fTransformedHeightFunc(fFirstPoint);
Jim Van Verth76387852017-05-16 16:55:16 -0400599 fFirstOutset = normal;
600 fFirstOutset *= this->offset(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400601
Jim Van Verth76387852017-05-16 16:55:16 -0400602 fPrevOutset = fFirstOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500603 fPrevPoint = fFirstPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400604 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500605
Jim Van Verthda965502017-04-11 15:29:14 -0400606 *fPositions.push() = fFirstPoint;
607 *fColors.push() = this->umbraColor(z);
Jim Van Verth76387852017-05-16 16:55:16 -0400608 *fPositions.push() = fFirstPoint + fFirstOutset;
Jim Van Verthbce74962017-01-25 09:39:46 -0500609 *fColors.push() = fPenumbraColor;
610 if (fTransparent) {
Jim Van Verthda965502017-04-11 15:29:14 -0400611 fPositions[0] += fFirstPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500612 fCentroidCount = 1;
613 }
Jim Van Verthda965502017-04-11 15:29:14 -0400614
615 // add the first quad
616 z = fTransformedHeightFunc(fInitPoints[1]);
617 fRadius = this->offset(z);
618 fUmbraColor = this->umbraColor(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400619 this->addEdge(fInitPoints[1], normal);
Jim Van Verthbce74962017-01-25 09:39:46 -0500620
621 // to ensure we skip this block next time
622 *fInitPoints.push() = p;
623 }
624
625 SkVector normal;
Jim Van Verth76387852017-05-16 16:55:16 -0400626 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400627 SkVector scaledNormal = normal;
628 scaledNormal *= fRadius;
629 this->addArc(scaledNormal, true);
630 SkScalar z = fTransformedHeightFunc(p);
631 fRadius = this->offset(z);
632 fUmbraColor = this->umbraColor(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400633 this->addEdge(p, normal);
Jim Van Verthbce74962017-01-25 09:39:46 -0500634 }
635}
636
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500637void SkAmbientShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) {
Jim Van Verth76387852017-05-16 16:55:16 -0400638 // We compute the inset in two stages: first we inset by half the current normal,
639 // then on the next addEdge() we add half of the next normal to get an average of the two
640 SkVector insetNormal = nextNormal;
641 insetNormal *= 0.5f*kInsetFactor;
642
643 // Adding the other half of the average for the previous edge
644 fPositions[fPrevUmbraIndex] += insetNormal;
645
646 SkPoint umbraPoint = nextPoint + insetNormal;
647 SkVector outsetNormal = nextNormal;
648 outsetNormal *= fRadius;
649 SkPoint penumbraPoint = nextPoint + outsetNormal;
650
651 // For split edges, we're adding an average of two averages, so we multiply by another half
652 if (fSplitPreviousEdge) {
653 insetNormal *= 0.5f;
654 fPositions[fPrevUmbraIndex - 2] += insetNormal;
655 }
656
657 // 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 -0400658 if (fColors[fPrevUmbraIndex] != fUmbraColor &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500659 SkPointPriv::DistanceToSqd(nextPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400660
661 // This is lacking 1/4 of the next inset -- we'll add it the next time we call addEdge()
662 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + umbraPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400663 centerPoint *= 0.5f;
664 *fPositions.push() = centerPoint;
665 *fColors.push() = SkPMLerp(fUmbraColor, fColors[fPrevUmbraIndex], 128);
Jim Van Verth76387852017-05-16 16:55:16 -0400666 centerPoint = fPositions[fPositions.count()-2] + penumbraPoint;
667 centerPoint *= 0.5f;
668 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400669 *fColors.push() = fPenumbraColor;
670
671 // set triangularization to get best interpolation of color
672 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
673 *fIndices.push() = fPrevUmbraIndex;
674 *fIndices.push() = fPositions.count() - 3;
675 *fIndices.push() = fPositions.count() - 2;
676
677 *fIndices.push() = fPositions.count() - 3;
678 *fIndices.push() = fPositions.count() - 1;
679 *fIndices.push() = fPositions.count() - 2;
680 } else {
681 *fIndices.push() = fPrevUmbraIndex;
682 *fIndices.push() = fPositions.count() - 2;
683 *fIndices.push() = fPositions.count() - 1;
684
685 *fIndices.push() = fPrevUmbraIndex;
686 *fIndices.push() = fPositions.count() - 1;
687 *fIndices.push() = fPositions.count() - 3;
688 }
689
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400690 // if transparent, add point to first one in array and add to center fan
691 if (fTransparent) {
692 fPositions[0] += centerPoint;
693 ++fCentroidCount;
694
695 *fIndices.push() = 0;
696 *fIndices.push() = fPrevUmbraIndex;
697 *fIndices.push() = fPositions.count() - 2;
698 }
699
Jim Van Verth76387852017-05-16 16:55:16 -0400700 fSplitPreviousEdge = true;
701 if (fPrevUmbraIndex == fFirstVertexIndex) {
702 fSplitFirstEdge = true;
703 }
Jim Van Verthda965502017-04-11 15:29:14 -0400704 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400705 } else {
706 fSplitPreviousEdge = false;
Jim Van Verthda965502017-04-11 15:29:14 -0400707 }
708
Jim Van Verthbce74962017-01-25 09:39:46 -0500709 // add next quad
Jim Van Verth76387852017-05-16 16:55:16 -0400710 *fPositions.push() = umbraPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500711 *fColors.push() = fUmbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -0400712 *fPositions.push() = penumbraPoint;
Jim Van Verthbce74962017-01-25 09:39:46 -0500713 *fColors.push() = fPenumbraColor;
714
Jim Van Verthda965502017-04-11 15:29:14 -0400715 // set triangularization to get best interpolation of color
716 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
717 *fIndices.push() = fPrevUmbraIndex;
718 *fIndices.push() = fPositions.count() - 3;
719 *fIndices.push() = fPositions.count() - 2;
Jim Van Verthbce74962017-01-25 09:39:46 -0500720
Jim Van Verthda965502017-04-11 15:29:14 -0400721 *fIndices.push() = fPositions.count() - 3;
722 *fIndices.push() = fPositions.count() - 1;
723 *fIndices.push() = fPositions.count() - 2;
724 } else {
725 *fIndices.push() = fPrevUmbraIndex;
726 *fIndices.push() = fPositions.count() - 2;
727 *fIndices.push() = fPositions.count() - 1;
728
729 *fIndices.push() = fPrevUmbraIndex;
730 *fIndices.push() = fPositions.count() - 1;
731 *fIndices.push() = fPositions.count() - 3;
732 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500733
734 // if transparent, add point to first one in array and add to center fan
735 if (fTransparent) {
736 fPositions[0] += nextPoint;
737 ++fCentroidCount;
738
739 *fIndices.push() = 0;
Brian Salomon66085ed2017-02-02 15:39:34 -0500740 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500741 *fIndices.push() = fPositions.count() - 2;
742 }
743
Brian Salomon66085ed2017-02-02 15:39:34 -0500744 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500745 fPrevPoint = nextPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400746 fPrevOutset = outsetNormal;
Jim Van Verthbce74962017-01-25 09:39:46 -0500747}
Jim Van Verth91af7272017-01-27 14:15:54 -0500748
749///////////////////////////////////////////////////////////////////////////////////////////////////
750
Brian Salomonaff27a22017-02-06 15:47:44 -0500751class SkSpotShadowTessellator : public SkBaseShadowTessellator {
Brian Salomon958fbc42017-01-30 17:01:28 -0500752public:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500753 SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400754 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -0400755 SkScalar lightRadius, bool transparent);
Brian Salomon958fbc42017-01-30 17:01:28 -0500756
Brian Salomon958fbc42017-01-30 17:01:28 -0500757private:
Brian Salomonab664fa2017-03-24 16:07:20 +0000758 void computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -0400759 const SkMatrix& shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000760 void computeClipVectorsAndTestCentroid();
Brian Salomon66085ed2017-02-02 15:39:34 -0500761 bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000762 int getClosestUmbraPoint(const SkPoint& point);
Brian Salomon958fbc42017-01-30 17:01:28 -0500763
Jim Van Vertha84898d2017-02-06 13:38:23 -0500764 void handleLine(const SkPoint& p) override;
Jim Van Verthb55eb282017-07-18 14:13:45 -0400765 bool handlePolyPoint(const SkPoint& p);
Brian Salomon958fbc42017-01-30 17:01:28 -0500766
767 void mapPoints(SkScalar scale, const SkVector& xlate, SkPoint* pts, int count);
Brian Salomonab664fa2017-03-24 16:07:20 +0000768 bool addInnerPoint(const SkPoint& pathPoint);
Jim Van Verthda965502017-04-11 15:29:14 -0400769 void addEdge(const SkVector& nextPoint, const SkVector& nextNormal);
770
771 SkScalar offset(SkScalar z) {
772 float zRatio = SkTPin(z / (fLightZ - z), 0.0f, 0.95f);
773 return fLightRadius*zRatio;
774 }
775
776 SkScalar fLightZ;
777 SkScalar fLightRadius;
778 SkScalar fOffsetAdjust;
Brian Salomon958fbc42017-01-30 17:01:28 -0500779
Brian Salomon958fbc42017-01-30 17:01:28 -0500780 SkTDArray<SkPoint> fClipPolygon;
Brian Salomon66085ed2017-02-02 15:39:34 -0500781 SkTDArray<SkVector> fClipVectors;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500782 SkPoint fCentroid;
Brian Salomonab664fa2017-03-24 16:07:20 +0000783 SkScalar fArea;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500784
Brian Salomonab664fa2017-03-24 16:07:20 +0000785 SkTDArray<SkPoint> fPathPolygon;
786 SkTDArray<SkPoint> fUmbraPolygon;
787 int fCurrClipPoint;
788 int fCurrUmbraPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -0500789 bool fPrevUmbraOutside;
790 bool fFirstUmbraOutside;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500791 bool fValidUmbra;
Brian Salomon958fbc42017-01-30 17:01:28 -0500792
Brian Salomonaff27a22017-02-06 15:47:44 -0500793 typedef SkBaseShadowTessellator INHERITED;
Brian Salomon958fbc42017-01-30 17:01:28 -0500794};
795
Jim Van Vertha84898d2017-02-06 13:38:23 -0500796SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400797 const SkPoint3& zPlaneParams,
Jim Van Verthb4366552017-03-27 14:25:29 -0400798 const SkPoint3& lightPos, SkScalar lightRadius,
Jim Van Verth060d9822017-05-04 09:58:17 -0400799 bool transparent)
Jim Van Verthe308a122017-05-08 14:19:30 -0400800 : INHERITED(zPlaneParams, transparent)
Jim Van Verthda965502017-04-11 15:29:14 -0400801 , fLightZ(lightPos.fZ)
802 , fLightRadius(lightRadius)
803 , fOffsetAdjust(0)
804 , fCurrClipPoint(0)
805 , fPrevUmbraOutside(false)
806 , fFirstUmbraOutside(false)
807 , fValidUmbra(true) {
808
Jim Van Verth22526362018-02-28 14:51:19 -0500809 // TODO: support some concave paths
810 if (!path.isConvex()) {
811 return;
812 }
813
Jim Van Verthda965502017-04-11 15:29:14 -0400814 // make sure we're not below the canvas plane
815 if (this->setZOffset(path.getBounds(), ctm.hasPerspective())) {
816 // Adjust light height and radius
817 fLightRadius *= (fLightZ + fZOffset) / fLightZ;
818 fLightZ += fZOffset;
819 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400820
821 // Set radius and colors
Jim Van Verthb4366552017-03-27 14:25:29 -0400822 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verthe308a122017-05-08 14:19:30 -0400823 SkScalar occluderHeight = this->heightFunc(center.fX, center.fY) + fZOffset;
Jim Van Verth060d9822017-05-04 09:58:17 -0400824 fUmbraColor = SkColorSetARGB(255, 0, 0, 0);
825 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400826
Jim Van Verth1af03d42017-07-31 09:34:58 -0400827 // Compute the blur radius, scale and translation for the spot shadow.
828 SkScalar radius;
Jim Van Verthda965502017-04-11 15:29:14 -0400829 SkMatrix shadowTransform;
830 if (!ctm.hasPerspective()) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400831 SkScalar scale;
832 SkVector translate;
833 SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY, fLightZ,
834 lightRadius, &radius, &scale, &translate);
Jim Van Verthda965502017-04-11 15:29:14 -0400835 shadowTransform.setScaleTranslate(scale, scale, translate.fX, translate.fY);
836 } else {
837 // For perspective, we have a scale, a z-shear, and another projective divide --
838 // this varies at each point so we can't use an affine transform.
839 // We'll just apply this to each generated point in turn.
840 shadowTransform.reset();
841 // Also can't cull the center (for now).
842 fTransparent = true;
Jim Van Verth1af03d42017-07-31 09:34:58 -0400843 radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius);
Jim Van Verthda965502017-04-11 15:29:14 -0400844 }
Jim Van Verth1af03d42017-07-31 09:34:58 -0400845 fRadius = radius;
Jim Van Verthda965502017-04-11 15:29:14 -0400846 SkMatrix fullTransform = SkMatrix::Concat(shadowTransform, ctm);
847
848 // Set up our reverse mapping
Jim Van Verth7c8008c2018-02-07 15:02:50 -0500849 if (!this->setTransformedHeightFunc(fullTransform)) {
850 return;
851 }
Jim Van Verthb4366552017-03-27 14:25:29 -0400852
Brian Salomonab664fa2017-03-24 16:07:20 +0000853 // TODO: calculate these reserves better
Brian Salomon66085ed2017-02-02 15:39:34 -0500854 // Penumbra ring: 3*numPts
855 // Umbra ring: numPts
Jim Van Verth91af7272017-01-27 14:15:54 -0500856 // Inner ring: numPts
Brian Salomon66085ed2017-02-02 15:39:34 -0500857 fPositions.setReserve(5 * path.countPoints());
858 fColors.setReserve(5 * path.countPoints());
859 // Penumbra ring: 12*numPts
860 // Umbra ring: 3*numPts
861 fIndices.setReserve(15 * path.countPoints());
Brian Salomone7c85c42017-03-24 16:00:35 +0000862 fClipPolygon.setReserve(path.countPoints());
Brian Salomonab664fa2017-03-24 16:07:20 +0000863
864 // compute rough clip bounds for umbra, plus offset polygon, plus centroid
Jim Van Verthda965502017-04-11 15:29:14 -0400865 this->computeClipAndPathPolygons(path, ctm, shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000866 if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3) {
Brian Salomon66085ed2017-02-02 15:39:34 -0500867 return;
868 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500869
Brian Salomonab664fa2017-03-24 16:07:20 +0000870 // check to see if umbra collapses
Cary Clarkdf429f32017-11-08 11:44:31 -0500871 SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, fPathPolygon[0],
872 fPathPolygon[1]);
Jim Van Verthda965502017-04-11 15:29:14 -0400873 SkRect bounds;
874 bounds.setBounds(&fPathPolygon[0], fPathPolygon.count());
Brian Salomonab664fa2017-03-24 16:07:20 +0000875 for (int i = 1; i < fPathPolygon.count(); ++i) {
876 int j = i + 1;
877 if (i == fPathPolygon.count() - 1) {
878 j = 0;
879 }
880 SkPoint currPoint = fPathPolygon[i];
881 SkPoint nextPoint = fPathPolygon[j];
Cary Clarkdf429f32017-11-08 11:44:31 -0500882 SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint,
883 nextPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000884 if (distSq < minDistSq) {
885 minDistSq = distSq;
886 }
887 }
888 static constexpr auto kTolerance = 1.0e-2f;
889 if (minDistSq < (radius + kTolerance)*(radius + kTolerance)) {
890 // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha
891 SkScalar newRadius = SkScalarSqrt(minDistSq) - kTolerance;
Jim Van Verthda965502017-04-11 15:29:14 -0400892 fOffsetAdjust = newRadius - radius;
Jim Van Verthb6069df2017-04-28 11:00:35 -0400893 SkScalar ratio = 128 * (newRadius + radius) / radius;
Brian Salomonab664fa2017-03-24 16:07:20 +0000894 // they aren't PMColors, but the interpolation algorithm is the same
895 fUmbraColor = SkPMLerp(fUmbraColor, fPenumbraColor, (unsigned)ratio);
896 radius = newRadius;
897 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500898
Brian Salomonab664fa2017-03-24 16:07:20 +0000899 // compute vectors for clip tests
900 this->computeClipVectorsAndTestCentroid();
901
902 // generate inner ring
Jim Van Verthda965502017-04-11 15:29:14 -0400903 if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), radius,
904 &fUmbraPolygon)) {
Brian Salomonab664fa2017-03-24 16:07:20 +0000905 // this shouldn't happen, but just in case we'll inset using the centroid
906 fValidUmbra = false;
907 }
908
909 // walk around the path polygon, generate outer ring and connect to inner ring
Brian Salomon66085ed2017-02-02 15:39:34 -0500910 if (fTransparent) {
911 *fPositions.push() = fCentroid;
912 *fColors.push() = fUmbraColor;
913 }
Brian Salomonab664fa2017-03-24 16:07:20 +0000914 fCurrUmbraPoint = 0;
915 for (int i = 0; i < fPathPolygon.count(); ++i) {
Jim Van Verthb55eb282017-07-18 14:13:45 -0400916 if (!this->handlePolyPoint(fPathPolygon[i])) {
917 return;
918 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500919 }
920
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500921 if (!this->indexCount()) {
922 return;
923 }
924
Brian Salomonab664fa2017-03-24 16:07:20 +0000925 // finish up the final verts
Jim Van Verth91af7272017-01-27 14:15:54 -0500926 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -0400927 if (compute_normal(fPrevPoint, fFirstPoint, fDirection, &normal)) {
928 normal *= fRadius;
929 this->addArc(normal, true);
Jim Van Verth91af7272017-01-27 14:15:54 -0500930
Brian Salomon66085ed2017-02-02 15:39:34 -0500931 // add to center fan
932 if (fTransparent) {
933 *fIndices.push() = 0;
934 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400935 *fIndices.push() = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -0500936 // or to clip ring
937 } else {
938 if (fFirstUmbraOutside) {
939 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400940 *fIndices.push() = fFirstVertexIndex;
941 *fIndices.push() = fFirstVertexIndex + 1;
Brian Salomon66085ed2017-02-02 15:39:34 -0500942 if (fPrevUmbraOutside) {
943 // fill out quad
944 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400945 *fIndices.push() = fFirstVertexIndex + 1;
Brian Salomon66085ed2017-02-02 15:39:34 -0500946 *fIndices.push() = fPrevUmbraIndex + 1;
947 }
948 } else if (fPrevUmbraOutside) {
949 // add tri
950 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400951 *fIndices.push() = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -0500952 *fIndices.push() = fPrevUmbraIndex + 1;
953 }
954 }
955
Jim Van Verth91af7272017-01-27 14:15:54 -0500956 // add final edge
957 *fPositions.push() = fFirstPoint + normal;
958 *fColors.push() = fPenumbraColor;
959
Brian Salomon66085ed2017-02-02 15:39:34 -0500960 *fIndices.push() = fPrevUmbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500961 *fIndices.push() = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400962 *fIndices.push() = fFirstVertexIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500963
964 *fIndices.push() = fPositions.count() - 2;
965 *fIndices.push() = fPositions.count() - 1;
Jim Van Verth76387852017-05-16 16:55:16 -0400966 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthda965502017-04-11 15:29:14 -0400967
Jim Van Verth76387852017-05-16 16:55:16 -0400968 fPrevOutset = normal;
Jim Van Verth91af7272017-01-27 14:15:54 -0500969 }
970
971 // final fan
972 if (fPositions.count() >= 3) {
Jim Van Verth76387852017-05-16 16:55:16 -0400973 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -0500974 fPrevPoint = fFirstPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400975 if (this->addArc(fFirstOutset, false)) {
976 *fIndices.push() = fFirstVertexIndex;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400977 *fIndices.push() = fPositions.count() - 1;
978 if (fFirstUmbraOutside) {
Jim Van Verth76387852017-05-16 16:55:16 -0400979 *fIndices.push() = fFirstVertexIndex + 2;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400980 } else {
Jim Van Verth76387852017-05-16 16:55:16 -0400981 *fIndices.push() = fFirstVertexIndex + 1;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400982 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500983 } else {
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400984 // no arc added, fix up by setting first penumbra point position to last one
985 if (fFirstUmbraOutside) {
Jim Van Verth76387852017-05-16 16:55:16 -0400986 fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400987 } else {
Jim Van Verth76387852017-05-16 16:55:16 -0400988 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400989 }
Brian Salomon66085ed2017-02-02 15:39:34 -0500990 }
Jim Van Verth91af7272017-01-27 14:15:54 -0500991 }
Jim Van Verthda965502017-04-11 15:29:14 -0400992
993 if (ctm.hasPerspective()) {
994 for (int i = 0; i < fPositions.count(); ++i) {
995 SkScalar pathZ = fTransformedHeightFunc(fPositions[i]);
996 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
997 fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor;
998 fPositions[i].fY = (fPositions[i].fY*fLightZ - lightPos.fY*pathZ)*factor;
999 }
1000#ifdef DRAW_CENTROID
1001 SkScalar pathZ = fTransformedHeightFunc(fCentroid);
1002 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
1003 fCentroid.fX = (fCentroid.fX*fLightZ - lightPos.fX*pathZ)*factor;
1004 fCentroid.fY = (fCentroid.fY*fLightZ - lightPos.fY*pathZ)*factor;
1005#endif
1006 }
1007#ifdef DRAW_CENTROID
1008 *fPositions.push() = fCentroid + SkVector::Make(-2, -2);
1009 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1010 *fPositions.push() = fCentroid + SkVector::Make(2, -2);
1011 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1012 *fPositions.push() = fCentroid + SkVector::Make(-2, 2);
1013 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1014 *fPositions.push() = fCentroid + SkVector::Make(2, 2);
1015 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1016
1017 *fIndices.push() = fPositions.count() - 4;
1018 *fIndices.push() = fPositions.count() - 2;
1019 *fIndices.push() = fPositions.count() - 1;
1020
1021 *fIndices.push() = fPositions.count() - 4;
1022 *fIndices.push() = fPositions.count() - 1;
1023 *fIndices.push() = fPositions.count() - 3;
1024#endif
1025
Brian Salomon0dda9cb2017-02-03 10:33:25 -05001026 fSucceeded = true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001027}
1028
Brian Salomonab664fa2017-03-24 16:07:20 +00001029void SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -04001030 const SkMatrix& shadowTransform) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001031
1032 fPathPolygon.setReserve(path.countPoints());
1033
1034 // Walk around the path and compute clip polygon and path polygon.
1035 // Will also accumulate sum of areas for centroid.
1036 // For Bezier curves, we compute additional interior points on curve.
Jim Van Verth91af7272017-01-27 14:15:54 -05001037 SkPath::Iter iter(path, true);
1038 SkPoint pts[4];
1039 SkPath::Verb verb;
1040
Jim Van Verth91af7272017-01-27 14:15:54 -05001041 fClipPolygon.reset();
1042
Brian Salomonab664fa2017-03-24 16:07:20 +00001043 // init centroid
1044 fCentroid = SkPoint::Make(0, 0);
1045 fArea = 0;
1046
Brian Salomon66085ed2017-02-02 15:39:34 -05001047 // coefficients to compute cubic Bezier at t = 5/16
Brian Salomonab664fa2017-03-24 16:07:20 +00001048 static constexpr SkScalar kA = 0.32495117187f;
1049 static constexpr SkScalar kB = 0.44311523437f;
1050 static constexpr SkScalar kC = 0.20141601562f;
1051 static constexpr SkScalar kD = 0.03051757812f;
Brian Salomon66085ed2017-02-02 15:39:34 -05001052
1053 SkPoint curvePoint;
1054 SkScalar w;
Jim Van Verth91af7272017-01-27 14:15:54 -05001055 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
1056 switch (verb) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001057 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001058 ctm.mapPoints(&pts[1], 1);
Jim Van Verth91af7272017-01-27 14:15:54 -05001059 *fClipPolygon.push() = pts[1];
Brian Salomonab664fa2017-03-24 16:07:20 +00001060 this->INHERITED::handleLine(shadowTransform, &pts[1]);
Jim Van Verth91af7272017-01-27 14:15:54 -05001061 break;
1062 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001063 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001064 // point at t = 1/2
1065 curvePoint.fX = 0.25f*pts[0].fX + 0.5f*pts[1].fX + 0.25f*pts[2].fX;
1066 curvePoint.fY = 0.25f*pts[0].fY + 0.5f*pts[1].fY + 0.25f*pts[2].fY;
1067 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001068 *fClipPolygon.push() = pts[2];
Brian Salomonab664fa2017-03-24 16:07:20 +00001069 this->handleQuad(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001070 break;
1071 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001072 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001073 w = iter.conicWeight();
Jim Van Vertha84898d2017-02-06 13:38:23 -05001074 // point at t = 1/2
Brian Salomon66085ed2017-02-02 15:39:34 -05001075 curvePoint.fX = 0.25f*pts[0].fX + w*0.5f*pts[1].fX + 0.25f*pts[2].fX;
1076 curvePoint.fY = 0.25f*pts[0].fY + w*0.5f*pts[1].fY + 0.25f*pts[2].fY;
1077 curvePoint *= SkScalarInvert(0.5f + 0.5f*w);
1078 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001079 *fClipPolygon.push() = pts[2];
Brian Salomonab664fa2017-03-24 16:07:20 +00001080 this->handleConic(shadowTransform, pts, w);
Jim Van Verth91af7272017-01-27 14:15:54 -05001081 break;
1082 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001083 ctm.mapPoints(pts, 4);
Brian Salomon66085ed2017-02-02 15:39:34 -05001084 // point at t = 5/16
1085 curvePoint.fX = kA*pts[0].fX + kB*pts[1].fX + kC*pts[2].fX + kD*pts[3].fX;
1086 curvePoint.fY = kA*pts[0].fY + kB*pts[1].fY + kC*pts[2].fY + kD*pts[3].fY;
1087 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001088 // point at t = 11/16
1089 curvePoint.fX = kD*pts[0].fX + kC*pts[1].fX + kB*pts[2].fX + kA*pts[3].fX;
1090 curvePoint.fY = kD*pts[0].fY + kC*pts[1].fY + kB*pts[2].fY + kA*pts[3].fY;
1091 *fClipPolygon.push() = curvePoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001092 *fClipPolygon.push() = pts[3];
Brian Salomonab664fa2017-03-24 16:07:20 +00001093 this->handleCubic(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001094 break;
Brian Salomonab664fa2017-03-24 16:07:20 +00001095 case SkPath::kMove_Verb:
Jim Van Verth91af7272017-01-27 14:15:54 -05001096 case SkPath::kClose_Verb:
Brian Salomonab664fa2017-03-24 16:07:20 +00001097 case SkPath::kDone_Verb:
Jim Van Verth91af7272017-01-27 14:15:54 -05001098 break;
1099 default:
1100 SkDEBUGFAIL("unknown verb");
1101 }
1102 }
1103
Brian Salomonab664fa2017-03-24 16:07:20 +00001104 // finish centroid
1105 if (fPathPolygon.count() > 0) {
1106 SkPoint currPoint = fPathPolygon[fPathPolygon.count() - 1];
1107 SkPoint nextPoint = fPathPolygon[0];
1108 SkScalar quadArea = currPoint.cross(nextPoint);
1109 fCentroid.fX += (currPoint.fX + nextPoint.fX) * quadArea;
1110 fCentroid.fY += (currPoint.fY + nextPoint.fY) * quadArea;
1111 fArea += quadArea;
1112 fCentroid *= SK_Scalar1 / (3 * fArea);
1113 }
1114
1115 fCurrClipPoint = fClipPolygon.count() - 1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001116}
1117
Brian Salomonab664fa2017-03-24 16:07:20 +00001118void SkSpotShadowTessellator::computeClipVectorsAndTestCentroid() {
Brian Salomon66085ed2017-02-02 15:39:34 -05001119 SkASSERT(fClipPolygon.count() >= 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001120
Brian Salomonab664fa2017-03-24 16:07:20 +00001121 // init clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001122 SkVector v0 = fClipPolygon[1] - fClipPolygon[0];
1123 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001124
1125 // init centroid check
1126 bool hiddenCentroid = true;
Brian Salomonab664fa2017-03-24 16:07:20 +00001127 SkVector v1 = fCentroid - fClipPolygon[0];
Brian Salomon66085ed2017-02-02 15:39:34 -05001128 SkScalar initCross = v0.cross(v1);
1129
1130 for (int p = 1; p < fClipPolygon.count(); ++p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001131 // add to clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001132 v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p];
1133 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001134 // Determine if transformed centroid is inside clipPolygon.
Brian Salomonab664fa2017-03-24 16:07:20 +00001135 v1 = fCentroid - fClipPolygon[p];
Brian Salomon66085ed2017-02-02 15:39:34 -05001136 if (initCross*v0.cross(v1) <= 0) {
1137 hiddenCentroid = false;
1138 }
1139 }
1140 SkASSERT(fClipVectors.count() == fClipPolygon.count());
1141
Brian Salomonab664fa2017-03-24 16:07:20 +00001142 fTransparent = fTransparent || !hiddenCentroid;
Brian Salomon66085ed2017-02-02 15:39:34 -05001143}
1144
1145bool SkSpotShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid,
1146 SkPoint* clipPoint) {
1147 SkVector segmentVector = centroid - umbraPoint;
1148
Brian Salomonab664fa2017-03-24 16:07:20 +00001149 int startClipPoint = fCurrClipPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001150 do {
Brian Salomonab664fa2017-03-24 16:07:20 +00001151 SkVector dp = umbraPoint - fClipPolygon[fCurrClipPoint];
1152 SkScalar denom = fClipVectors[fCurrClipPoint].cross(segmentVector);
Brian Salomon66085ed2017-02-02 15:39:34 -05001153 SkScalar t_num = dp.cross(segmentVector);
1154 // if line segments are nearly parallel
1155 if (SkScalarNearlyZero(denom)) {
1156 // and collinear
1157 if (SkScalarNearlyZero(t_num)) {
1158 return false;
1159 }
1160 // otherwise are separate, will try the next poly segment
1161 // else if crossing lies within poly segment
1162 } else if (t_num >= 0 && t_num <= denom) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001163 SkScalar s_num = dp.cross(fClipVectors[fCurrClipPoint]);
Brian Salomon66085ed2017-02-02 15:39:34 -05001164 // if umbra point is inside the clip polygon
Jim Van Verthda965502017-04-11 15:29:14 -04001165 if (s_num >= 0 && s_num <= denom) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001166 segmentVector *= s_num/denom;
1167 *clipPoint = umbraPoint + segmentVector;
1168 return true;
1169 }
1170 }
Brian Salomonab664fa2017-03-24 16:07:20 +00001171 fCurrClipPoint = (fCurrClipPoint + 1) % fClipPolygon.count();
1172 } while (fCurrClipPoint != startClipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001173
1174 return false;
Jim Van Verth91af7272017-01-27 14:15:54 -05001175}
1176
Brian Salomonab664fa2017-03-24 16:07:20 +00001177int SkSpotShadowTessellator::getClosestUmbraPoint(const SkPoint& p) {
Cary Clarkdf429f32017-11-08 11:44:31 -05001178 SkScalar minDistance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[fCurrUmbraPoint]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001179 int index = fCurrUmbraPoint;
1180 int dir = 1;
1181 int next = (index + dir) % fUmbraPolygon.count();
1182
1183 // init travel direction
Cary Clarkdf429f32017-11-08 11:44:31 -05001184 SkScalar distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001185 if (distance < minDistance) {
1186 index = next;
1187 minDistance = distance;
1188 } else {
1189 dir = fUmbraPolygon.count()-1;
1190 }
1191
1192 // iterate until we find a point that increases the distance
1193 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001194 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001195 while (distance < minDistance) {
1196 index = next;
1197 minDistance = distance;
1198 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001199 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001200 }
1201
1202 fCurrUmbraPoint = index;
1203 return index;
1204}
1205
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001206void SkSpotShadowTessellator::mapPoints(SkScalar scale, const SkVector& xlate,
Jim Van Verth91af7272017-01-27 14:15:54 -05001207 SkPoint* pts, int count) {
1208 // TODO: vectorize
1209 for (int i = 0; i < count; ++i) {
1210 pts[i] *= scale;
1211 pts[i] += xlate;
1212 }
1213}
1214
Brian Salomonab664fa2017-03-24 16:07:20 +00001215static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) {
1216 static constexpr SkScalar kClose = (SK_Scalar1 / 16);
1217 static constexpr SkScalar kCloseSqd = kClose*kClose;
1218
Cary Clarkdf429f32017-11-08 11:44:31 -05001219 SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1);
Brian Salomonab664fa2017-03-24 16:07:20 +00001220 return distSq < kCloseSqd;
1221}
1222
Jim Van Verthb55eb282017-07-18 14:13:45 -04001223static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001224 SkVector v0 = p1 - p0;
1225 SkVector v1 = p2 - p0;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001226 return v0.cross(v1);
1227}
1228
1229static bool is_collinear(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
1230 return (SkScalarNearlyZero(perp_dot(p0, p1, p2)));
Brian Salomonab664fa2017-03-24 16:07:20 +00001231}
1232
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001233void SkSpotShadowTessellator::handleLine(const SkPoint& p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001234 // remove coincident points and add to centroid
1235 if (fPathPolygon.count() > 0) {
1236 const SkPoint& lastPoint = fPathPolygon[fPathPolygon.count() - 1];
1237 if (duplicate_pt(p, lastPoint)) {
1238 return;
1239 }
1240 SkScalar quadArea = lastPoint.cross(p);
1241 fCentroid.fX += (p.fX + lastPoint.fX) * quadArea;
1242 fCentroid.fY += (p.fY + lastPoint.fY) * quadArea;
1243 fArea += quadArea;
1244 }
1245
1246 // try to remove collinear points
1247 if (fPathPolygon.count() > 1 && is_collinear(fPathPolygon[fPathPolygon.count()-2],
1248 fPathPolygon[fPathPolygon.count()-1],
1249 p)) {
1250 fPathPolygon[fPathPolygon.count() - 1] = p;
1251 } else {
1252 *fPathPolygon.push() = p;
1253 }
1254}
1255
Jim Van Verthb55eb282017-07-18 14:13:45 -04001256bool SkSpotShadowTessellator::handlePolyPoint(const SkPoint& p) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001257 if (fInitPoints.count() < 2) {
1258 *fInitPoints.push() = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001259 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001260 }
1261
1262 if (fInitPoints.count() == 2) {
1263 // determine if cw or ccw
Jim Van Verthb55eb282017-07-18 14:13:45 -04001264 SkScalar perpDot = perp_dot(fInitPoints[0], fInitPoints[1], p);
Jim Van Verth91af7272017-01-27 14:15:54 -05001265 if (SkScalarNearlyZero(perpDot)) {
1266 // nearly parallel, just treat as straight line 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
1271 // if perpDot > 0, winding is ccw
1272 fDirection = (perpDot > 0) ? -1 : 1;
1273
1274 // add first quad
Jim Van Verth76387852017-05-16 16:55:16 -04001275 if (!compute_normal(fInitPoints[0], fInitPoints[1], fDirection, &fFirstOutset)) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001276 // first two points are incident, make the third point the second and continue
1277 fInitPoints[1] = p;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001278 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001279 }
1280
Jim Van Verth76387852017-05-16 16:55:16 -04001281 fFirstOutset *= fRadius;
Jim Van Verth91af7272017-01-27 14:15:54 -05001282 fFirstPoint = fInitPoints[0];
Jim Van Verth76387852017-05-16 16:55:16 -04001283 fFirstVertexIndex = fPositions.count();
1284 fPrevOutset = fFirstOutset;
Jim Van Verth91af7272017-01-27 14:15:54 -05001285 fPrevPoint = fFirstPoint;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001286 fPrevUmbraIndex = -1;
Jim Van Verth91af7272017-01-27 14:15:54 -05001287
Brian Salomon66085ed2017-02-02 15:39:34 -05001288 this->addInnerPoint(fFirstPoint);
Jim Van Verth76387852017-05-16 16:55:16 -04001289 fPrevUmbraIndex = fFirstVertexIndex;
Brian Salomon66085ed2017-02-02 15:39:34 -05001290
1291 if (!fTransparent) {
1292 SkPoint clipPoint;
Jim Van Verthb55eb282017-07-18 14:13:45 -04001293 bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex],
1294 fCentroid, &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001295 if (isOutside) {
1296 *fPositions.push() = clipPoint;
1297 *fColors.push() = fUmbraColor;
1298 }
1299 fPrevUmbraOutside = isOutside;
1300 fFirstUmbraOutside = isOutside;
1301 }
1302
Jim Van Verth76387852017-05-16 16:55:16 -04001303 SkPoint newPoint = fFirstPoint + fFirstOutset;
Jim Van Verth91af7272017-01-27 14:15:54 -05001304 *fPositions.push() = newPoint;
1305 *fColors.push() = fPenumbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -04001306 this->addEdge(fInitPoints[1], fFirstOutset);
Jim Van Verth91af7272017-01-27 14:15:54 -05001307
1308 // to ensure we skip this block next time
1309 *fInitPoints.push() = p;
1310 }
1311
Jim Van Verthb55eb282017-07-18 14:13:45 -04001312 // if concave, abort
1313 SkScalar perpDot = perp_dot(fInitPoints[1], fInitPoints[2], p);
1314 if (fDirection*perpDot > 0) {
1315 return false;
1316 }
1317
Jim Van Verth91af7272017-01-27 14:15:54 -05001318 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -04001319 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
1320 normal *= fRadius;
1321 this->addArc(normal, true);
1322 this->addEdge(p, normal);
Jim Van Verthb55eb282017-07-18 14:13:45 -04001323 fInitPoints[1] = fInitPoints[2];
1324 fInitPoints[2] = p;
Jim Van Verth91af7272017-01-27 14:15:54 -05001325 }
Jim Van Verthb55eb282017-07-18 14:13:45 -04001326
1327 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001328}
1329
Brian Salomonab664fa2017-03-24 16:07:20 +00001330bool SkSpotShadowTessellator::addInnerPoint(const SkPoint& pathPoint) {
1331 SkPoint umbraPoint;
1332 if (!fValidUmbra) {
1333 SkVector v = fCentroid - pathPoint;
1334 v *= 0.95f;
1335 umbraPoint = pathPoint + v;
Jim Van Verth91af7272017-01-27 14:15:54 -05001336 } else {
Brian Salomonab664fa2017-03-24 16:07:20 +00001337 umbraPoint = fUmbraPolygon[this->getClosestUmbraPoint(pathPoint)];
Jim Van Verth91af7272017-01-27 14:15:54 -05001338 }
Brian Salomon66085ed2017-02-02 15:39:34 -05001339
Jim Van Verth91af7272017-01-27 14:15:54 -05001340 fPrevPoint = pathPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001341
1342 // merge "close" points
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001343 if (fPrevUmbraIndex == -1 ||
Brian Salomonab664fa2017-03-24 16:07:20 +00001344 !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) {
1345 *fPositions.push() = umbraPoint;
1346 *fColors.push() = fUmbraColor;
1347
1348 return false;
1349 } else {
1350 return true;
1351 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001352}
1353
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001354void SkSpotShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001355 // add next umbra point
Brian Salomonab664fa2017-03-24 16:07:20 +00001356 bool duplicate = this->addInnerPoint(nextPoint);
1357 int prevPenumbraIndex = duplicate ? fPositions.count()-1 : fPositions.count()-2;
1358 int currUmbraIndex = duplicate ? fPrevUmbraIndex : fPositions.count()-1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001359
Brian Salomonab664fa2017-03-24 16:07:20 +00001360 if (!duplicate) {
1361 // add to center fan if transparent or centroid showing
1362 if (fTransparent) {
1363 *fIndices.push() = 0;
1364 *fIndices.push() = fPrevUmbraIndex;
1365 *fIndices.push() = currUmbraIndex;
1366 // otherwise add to clip ring
1367 } else {
Brian Salomon66085ed2017-02-02 15:39:34 -05001368 SkPoint clipPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001369 bool isOutside = this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid,
1370 &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001371 if (isOutside) {
1372 *fPositions.push() = clipPoint;
1373 *fColors.push() = fUmbraColor;
1374
1375 *fIndices.push() = fPrevUmbraIndex;
1376 *fIndices.push() = currUmbraIndex;
1377 *fIndices.push() = currUmbraIndex + 1;
1378 if (fPrevUmbraOutside) {
1379 // fill out quad
1380 *fIndices.push() = fPrevUmbraIndex;
1381 *fIndices.push() = currUmbraIndex + 1;
1382 *fIndices.push() = fPrevUmbraIndex + 1;
1383 }
1384 } else if (fPrevUmbraOutside) {
1385 // add tri
1386 *fIndices.push() = fPrevUmbraIndex;
1387 *fIndices.push() = currUmbraIndex;
1388 *fIndices.push() = fPrevUmbraIndex + 1;
1389 }
1390 fPrevUmbraOutside = isOutside;
1391 }
1392 }
1393
1394 // add next penumbra point and quad
Jim Van Verth91af7272017-01-27 14:15:54 -05001395 SkPoint newPoint = nextPoint + nextNormal;
1396 *fPositions.push() = newPoint;
1397 *fColors.push() = fPenumbraColor;
1398
Brian Salomonab664fa2017-03-24 16:07:20 +00001399 if (!duplicate) {
1400 *fIndices.push() = fPrevUmbraIndex;
1401 *fIndices.push() = prevPenumbraIndex;
1402 *fIndices.push() = currUmbraIndex;
1403 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001404
Brian Salomon66085ed2017-02-02 15:39:34 -05001405 *fIndices.push() = prevPenumbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -05001406 *fIndices.push() = fPositions.count() - 1;
Brian Salomon66085ed2017-02-02 15:39:34 -05001407 *fIndices.push() = currUmbraIndex;
Jim Van Verth91af7272017-01-27 14:15:54 -05001408
Brian Salomon66085ed2017-02-02 15:39:34 -05001409 fPrevUmbraIndex = currUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -04001410 fPrevOutset = nextNormal;
Jim Van Verth91af7272017-01-27 14:15:54 -05001411}
Brian Salomon958fbc42017-01-30 17:01:28 -05001412
1413///////////////////////////////////////////////////////////////////////////////////////////////////
1414
Brian Salomonaff27a22017-02-06 15:47:44 -05001415sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001416 const SkPoint3& zPlane, bool transparent) {
1417 SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001418 return ambientTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001419}
1420
Brian Salomonaff27a22017-02-06 15:47:44 -05001421sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001422 const SkPoint3& zPlane, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -04001423 SkScalar lightRadius, bool transparent) {
Jim Van Verthe308a122017-05-08 14:19:30 -04001424 SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001425 return spotTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001426}