blob: 873e6d2687e2f64fa28fe332d0ab3e76b2fce5b8 [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"
Jim Van Verth8664a1d2018-06-28 16:26:50 -040012#include "SkPolyUtils.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 Verthcdaf6612018-06-05 15:21:13 -040048 bool accumulateCentroid(const SkPoint& c, const SkPoint& n);
49 bool checkConvexity(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2);
50 void finishPathPolygon();
Jim Van Verth8760e2f2018-06-12 14:21:38 -040051 void stitchConcaveRings(const SkTDArray<SkPoint>& umbraPolygon,
52 SkTDArray<int>* umbraIndices,
53 const SkTDArray<SkPoint>& penumbraPolygon,
54 SkTDArray<int>* penumbraIndices);
Jim Van Verthcdaf6612018-06-05 15:21:13 -040055
56 void handleLine(const SkPoint& p);
Jim Van Vertha84898d2017-02-06 13:38:23 -050057 void handleLine(const SkMatrix& m, SkPoint* p);
Brian Salomon958fbc42017-01-30 17:01:28 -050058
59 void handleQuad(const SkPoint pts[3]);
Jim Van Vertha84898d2017-02-06 13:38:23 -050060 void handleQuad(const SkMatrix& m, SkPoint pts[3]);
Brian Salomon958fbc42017-01-30 17:01:28 -050061
Jim Van Vertha84898d2017-02-06 13:38:23 -050062 void handleCubic(const SkMatrix& m, SkPoint pts[4]);
Brian Salomon958fbc42017-01-30 17:01:28 -050063
Jim Van Vertha84898d2017-02-06 13:38:23 -050064 void handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w);
Brian Salomon958fbc42017-01-30 17:01:28 -050065
Jim Van Verthda965502017-04-11 15:29:14 -040066 bool setTransformedHeightFunc(const SkMatrix& ctm);
Brian Salomon958fbc42017-01-30 17:01:28 -050067
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -040068 bool addArc(const SkVector& nextNormal, bool finishArc);
Jim Van Verthb4366552017-03-27 14:25:29 -040069
Jim Van Verth872da6b2018-04-10 11:24:11 -040070 void appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2);
71 void appendQuad(uint16_t index0, uint16_t index1, uint16_t index2, uint16_t index3);
72
Jim Van Verthe308a122017-05-08 14:19:30 -040073 SkScalar heightFunc(SkScalar x, SkScalar y) {
74 return fZPlaneParams.fX*x + fZPlaneParams.fY*y + fZPlaneParams.fZ;
75 }
76
77 SkPoint3 fZPlaneParams;
Jim Van Verthda965502017-04-11 15:29:14 -040078 std::function<SkScalar(const SkPoint&)> fTransformedHeightFunc;
79 SkScalar fZOffset;
80 // members for perspective height function
Jim Van Verth4c9b8932017-05-15 13:49:21 -040081 SkPoint3 fTransformedZParams;
Jim Van Verthda965502017-04-11 15:29:14 -040082 SkScalar fPartialDeterminants[3];
83
Brian Salomon958fbc42017-01-30 17:01:28 -050084 // temporary buffer
85 SkTDArray<SkPoint> fPointBuffer;
Brian Salomon0dda9cb2017-02-03 10:33:25 -050086
Jim Van Vertha84898d2017-02-06 13:38:23 -050087 SkTDArray<SkPoint> fPositions;
88 SkTDArray<SkColor> fColors;
89 SkTDArray<uint16_t> fIndices;
90
Jim Van Verthcdaf6612018-06-05 15:21:13 -040091 SkTDArray<SkPoint> fPathPolygon;
92 SkPoint fCentroid;
93 SkScalar fArea;
94 SkScalar fLastArea;
Jim Van Verthcdaf6612018-06-05 15:21:13 -040095 SkScalar fLastCross;
96
Jim Van Verth76387852017-05-16 16:55:16 -040097 int fFirstVertexIndex;
98 SkVector fFirstOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -050099 SkPoint fFirstPoint;
100
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500101 bool fSucceeded;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500102 bool fTransparent;
Jim Van Verthf507c282018-05-11 10:48:20 -0400103 bool fIsConvex;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500104
105 SkColor fUmbraColor;
106 SkColor fPenumbraColor;
107
108 SkScalar fRadius;
109 SkScalar fDirection;
110 int fPrevUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -0400111 SkVector fPrevOutset;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500112 SkPoint fPrevPoint;
Brian Salomon958fbc42017-01-30 17:01:28 -0500113};
114
Jim Van Verthda965502017-04-11 15:29:14 -0400115static bool compute_normal(const SkPoint& p0, const SkPoint& p1, SkScalar dir,
Jim Van Verthbce74962017-01-25 09:39:46 -0500116 SkVector* newNormal) {
117 SkVector normal;
118 // compute perpendicular
119 normal.fX = p0.fY - p1.fY;
120 normal.fY = p1.fX - p0.fX;
Jim Van Verthda965502017-04-11 15:29:14 -0400121 normal *= dir;
Jim Van Verthbce74962017-01-25 09:39:46 -0500122 if (!normal.normalize()) {
123 return false;
124 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500125 *newNormal = normal;
126 return true;
127}
128
Jim Van Verthf507c282018-05-11 10:48:20 -0400129static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) {
130 static constexpr SkScalar kClose = (SK_Scalar1 / 16);
131 static constexpr SkScalar kCloseSqd = kClose * kClose;
132
133 SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1);
134 return distSq < kCloseSqd;
135}
136
137static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
138 SkVector v0 = p1 - p0;
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400139 SkVector v1 = p2 - p1;
Jim Van Verthf507c282018-05-11 10:48:20 -0400140 return v0.cross(v1);
141}
142
Jim Van Verthe308a122017-05-08 14:19:30 -0400143SkBaseShadowTessellator::SkBaseShadowTessellator(const SkPoint3& zPlaneParams, bool transparent)
144 : fZPlaneParams(zPlaneParams)
Jim Van Verthda965502017-04-11 15:29:14 -0400145 , fZOffset(0)
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400146 , fCentroid({0, 0})
147 , fArea(0)
148 , fLastArea(0)
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400149 , fLastCross(0)
Jim Van Verth76387852017-05-16 16:55:16 -0400150 , fFirstVertexIndex(-1)
Brian Salomonaff27a22017-02-06 15:47:44 -0500151 , fSucceeded(false)
152 , fTransparent(transparent)
Jim Van Verthf507c282018-05-11 10:48:20 -0400153 , fIsConvex(true)
Brian Salomonaff27a22017-02-06 15:47:44 -0500154 , fDirection(1)
155 , fPrevUmbraIndex(-1) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500156 // child classes will set reserve for positions, colors and indices
157}
158
Jim Van Verthda965502017-04-11 15:29:14 -0400159bool SkBaseShadowTessellator::setZOffset(const SkRect& bounds, bool perspective) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400160 SkScalar minZ = this->heightFunc(bounds.fLeft, bounds.fTop);
Jim Van Verthda965502017-04-11 15:29:14 -0400161 if (perspective) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400162 SkScalar z = this->heightFunc(bounds.fLeft, bounds.fBottom);
Jim Van Verthda965502017-04-11 15:29:14 -0400163 if (z < minZ) {
164 minZ = z;
165 }
Jim Van Verthe308a122017-05-08 14:19:30 -0400166 z = this->heightFunc(bounds.fRight, bounds.fTop);
Jim Van Verthda965502017-04-11 15:29:14 -0400167 if (z < minZ) {
168 minZ = z;
169 }
Jim Van Verthe308a122017-05-08 14:19:30 -0400170 z = this->heightFunc(bounds.fRight, bounds.fBottom);
Jim Van Verthda965502017-04-11 15:29:14 -0400171 if (z < minZ) {
172 minZ = z;
173 }
174 }
175
176 if (minZ < kMinHeight) {
177 fZOffset = -minZ + kMinHeight;
178 return true;
179 }
180
181 return false;
182}
183
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400184bool SkBaseShadowTessellator::accumulateCentroid(const SkPoint& curr, const SkPoint& next) {
185 if (duplicate_pt(curr, next)) {
186 return false;
187 }
188
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400189 SkASSERT(fPathPolygon.count() > 0);
190 SkVector v0 = curr - fPathPolygon[0];
191 SkVector v1 = next - fPathPolygon[0];
192 SkScalar quadArea = v0.cross(v1);
193 fCentroid.fX += (v0.fX + v1.fX) * quadArea;
194 fCentroid.fY += (v0.fY + v1.fY) * quadArea;
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400195 fArea += quadArea;
196 // convexity check
Jim Van Verth3645bb02018-06-26 14:58:58 -0400197 if (quadArea*fLastArea < 0) {
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400198 fIsConvex = false;
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400199 }
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400200 if (0 != quadArea) {
201 fLastArea = quadArea;
202 }
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400203
204 return true;
205}
206
207bool SkBaseShadowTessellator::checkConvexity(const SkPoint& p0,
208 const SkPoint& p1,
209 const SkPoint& p2) {
210 SkScalar cross = perp_dot(p0, p1, p2);
211 // skip collinear point
212 if (SkScalarNearlyZero(cross)) {
213 return false;
214 }
215
216 // check for convexity
217 if (fLastCross*cross < 0) {
218 fIsConvex = false;
219 }
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400220 if (0 != cross) {
221 fLastCross = cross;
222 }
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400223
224 return true;
225}
226
227void SkBaseShadowTessellator::finishPathPolygon() {
228 if (fPathPolygon.count() > 1) {
229 if (!this->accumulateCentroid(fPathPolygon[fPathPolygon.count() - 1], fPathPolygon[0])) {
230 // remove coincident point
231 fPathPolygon.pop();
232 }
233 }
234
235 if (fPathPolygon.count() > 2) {
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400236 // do this before the final convexity check, so we use the correct fPathPolygon[0]
237 fCentroid *= sk_ieee_float_divide(1, 3 * fArea);
238 fCentroid += fPathPolygon[0];
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400239 if (!checkConvexity(fPathPolygon[fPathPolygon.count() - 2],
240 fPathPolygon[fPathPolygon.count() - 1],
241 fPathPolygon[0])) {
242 // remove collinear point
243 fPathPolygon[0] = fPathPolygon[fPathPolygon.count() - 1];
244 fPathPolygon.pop();
245 }
246 }
247
Jim Van Verth7deacf42018-06-08 15:13:25 -0400248 // if area is positive, winding is ccw
249 fDirection = fArea > 0 ? -1 : 1;
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400250}
251
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400252void SkBaseShadowTessellator::stitchConcaveRings(const SkTDArray<SkPoint>& umbraPolygon,
253 SkTDArray<int>* umbraIndices,
254 const SkTDArray<SkPoint>& penumbraPolygon,
255 SkTDArray<int>* penumbraIndices) {
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400256 // TODO: only create and fill indexMap when fTransparent is true?
257 SkAutoSTMalloc<64, uint16_t> indexMap(umbraPolygon.count());
258
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400259 // find minimum indices
260 int minIndex = 0;
261 int min = (*penumbraIndices)[0];
262 for (int i = 1; i < (*penumbraIndices).count(); ++i) {
263 if ((*penumbraIndices)[i] < min) {
264 min = (*penumbraIndices)[i];
265 minIndex = i;
266 }
267 }
268 int currPenumbra = minIndex;
269
270 minIndex = 0;
271 min = (*umbraIndices)[0];
272 for (int i = 1; i < (*umbraIndices).count(); ++i) {
273 if ((*umbraIndices)[i] < min) {
274 min = (*umbraIndices)[i];
275 minIndex = i;
276 }
277 }
278 int currUmbra = minIndex;
279
280 // now find a case where the indices are equal (there should be at least one)
281 int maxPenumbraIndex = fPathPolygon.count() - 1;
282 int maxUmbraIndex = fPathPolygon.count() - 1;
283 while ((*penumbraIndices)[currPenumbra] != (*umbraIndices)[currUmbra]) {
284 if ((*penumbraIndices)[currPenumbra] < (*umbraIndices)[currUmbra]) {
285 (*penumbraIndices)[currPenumbra] += fPathPolygon.count();
286 maxPenumbraIndex = (*penumbraIndices)[currPenumbra];
287 currPenumbra = (currPenumbra + 1) % penumbraPolygon.count();
288 } else {
289 (*umbraIndices)[currUmbra] += fPathPolygon.count();
290 maxUmbraIndex = (*umbraIndices)[currUmbra];
291 currUmbra = (currUmbra + 1) % umbraPolygon.count();
292 }
293 }
294
295 *fPositions.push() = penumbraPolygon[currPenumbra];
296 *fColors.push() = fPenumbraColor;
297 int prevPenumbraIndex = 0;
298 *fPositions.push() = umbraPolygon[currUmbra];
299 *fColors.push() = fUmbraColor;
300 fPrevUmbraIndex = 1;
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400301 indexMap[currUmbra] = 1;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400302
303 int nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count();
304 int nextUmbra = (currUmbra + 1) % umbraPolygon.count();
305 while ((*penumbraIndices)[nextPenumbra] <= maxPenumbraIndex ||
306 (*umbraIndices)[nextUmbra] <= maxUmbraIndex) {
307
308 if ((*umbraIndices)[nextUmbra] == (*penumbraIndices)[nextPenumbra]) {
309 // advance both one step
310 *fPositions.push() = penumbraPolygon[nextPenumbra];
311 *fColors.push() = fPenumbraColor;
312 int currPenumbraIndex = fPositions.count() - 1;
313
314 *fPositions.push() = umbraPolygon[nextUmbra];
315 *fColors.push() = fUmbraColor;
316 int currUmbraIndex = fPositions.count() - 1;
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400317 indexMap[nextUmbra] = currUmbraIndex;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400318
319 this->appendQuad(prevPenumbraIndex, currPenumbraIndex,
320 fPrevUmbraIndex, currUmbraIndex);
321
322 prevPenumbraIndex = currPenumbraIndex;
323 (*penumbraIndices)[currPenumbra] += fPathPolygon.count();
324 currPenumbra = nextPenumbra;
325 nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count();
326
327 fPrevUmbraIndex = currUmbraIndex;
328 (*umbraIndices)[currUmbra] += fPathPolygon.count();
329 currUmbra = nextUmbra;
330 nextUmbra = (currUmbra + 1) % umbraPolygon.count();
331 }
332
333 while ((*penumbraIndices)[nextPenumbra] < (*umbraIndices)[nextUmbra] &&
334 (*penumbraIndices)[nextPenumbra] <= maxPenumbraIndex) {
335 // fill out penumbra arc
336 *fPositions.push() = penumbraPolygon[nextPenumbra];
337 *fColors.push() = fPenumbraColor;
338 int currPenumbraIndex = fPositions.count() - 1;
339
340 this->appendTriangle(prevPenumbraIndex, currPenumbraIndex, fPrevUmbraIndex);
341
342 prevPenumbraIndex = currPenumbraIndex;
343 // this ensures the ordering when we wrap around
344 (*penumbraIndices)[currPenumbra] += fPathPolygon.count();
345 currPenumbra = nextPenumbra;
346 nextPenumbra = (currPenumbra + 1) % penumbraPolygon.count();
347 }
348
349 while ((*umbraIndices)[nextUmbra] < (*penumbraIndices)[nextPenumbra] &&
350 (*umbraIndices)[nextUmbra] <= maxUmbraIndex) {
351 // fill out umbra arc
352 *fPositions.push() = umbraPolygon[nextUmbra];
353 *fColors.push() = fUmbraColor;
354 int currUmbraIndex = fPositions.count() - 1;
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400355 indexMap[nextUmbra] = currUmbraIndex;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400356
357 this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex);
358
359 fPrevUmbraIndex = currUmbraIndex;
360 // this ensures the ordering when we wrap around
361 (*umbraIndices)[currUmbra] += fPathPolygon.count();
362 currUmbra = nextUmbra;
363 nextUmbra = (currUmbra + 1) % umbraPolygon.count();
364 }
365 }
366 // finish up by advancing both one step
367 *fPositions.push() = penumbraPolygon[nextPenumbra];
368 *fColors.push() = fPenumbraColor;
369 int currPenumbraIndex = fPositions.count() - 1;
370
371 *fPositions.push() = umbraPolygon[nextUmbra];
372 *fColors.push() = fUmbraColor;
373 int currUmbraIndex = fPositions.count() - 1;
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400374 indexMap[nextUmbra] = currUmbraIndex;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400375
376 this->appendQuad(prevPenumbraIndex, currPenumbraIndex,
377 fPrevUmbraIndex, currUmbraIndex);
378
379 if (fTransparent) {
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400380 SkTriangulateSimplePolygon(umbraPolygon.begin(), indexMap, umbraPolygon.count(),
381 &fIndices);
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400382 }
383}
384
385
Jim Van Vertha84898d2017-02-06 13:38:23 -0500386// tesselation tolerance values, in device space pixels
Mike Kleinb8b51e62017-02-09 15:22:53 -0500387#if SK_SUPPORT_GPU
Jim Van Vertha84898d2017-02-06 13:38:23 -0500388static const SkScalar kQuadTolerance = 0.2f;
389static const SkScalar kCubicTolerance = 0.2f;
Mike Kleinb8b51e62017-02-09 15:22:53 -0500390#endif
Jim Van Vertha84898d2017-02-06 13:38:23 -0500391static const SkScalar kConicTolerance = 0.5f;
392
Jim Van Verth3645bb02018-06-26 14:58:58 -0400393// clamps the point to the nearest 16th of a pixel
394static void sanitize_point(const SkPoint& in, SkPoint* out) {
395 out->fX = SkScalarRoundToScalar(16.f*in.fX)*0.0625f;
396 out->fY = SkScalarRoundToScalar(16.f*in.fY)*0.0625f;
397}
398
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400399void SkBaseShadowTessellator::handleLine(const SkPoint& p) {
Jim Van Verth3645bb02018-06-26 14:58:58 -0400400 SkPoint pSanitized;
401 sanitize_point(p, &pSanitized);
402
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400403 if (fPathPolygon.count() > 0) {
Jim Van Verth3645bb02018-06-26 14:58:58 -0400404 if (!this->accumulateCentroid(fPathPolygon[fPathPolygon.count() - 1], pSanitized)) {
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400405 // skip coincident point
406 return;
407 }
408 }
409
410 if (fPathPolygon.count() > 1) {
411 if (!checkConvexity(fPathPolygon[fPathPolygon.count() - 2],
412 fPathPolygon[fPathPolygon.count() - 1],
Jim Van Verth3645bb02018-06-26 14:58:58 -0400413 pSanitized)) {
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400414 // remove collinear point
415 fPathPolygon.pop();
416 }
417 }
418
Jim Van Verth3645bb02018-06-26 14:58:58 -0400419 *fPathPolygon.push() = pSanitized;
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400420}
421
Brian Salomonaff27a22017-02-06 15:47:44 -0500422void SkBaseShadowTessellator::handleLine(const SkMatrix& m, SkPoint* p) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500423 m.mapPoints(p, 1);
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400424
Jim Van Vertha84898d2017-02-06 13:38:23 -0500425 this->handleLine(*p);
426}
427
Brian Salomonaff27a22017-02-06 15:47:44 -0500428void SkBaseShadowTessellator::handleQuad(const SkPoint pts[3]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500429#if SK_SUPPORT_GPU
Jim Van Vertha947e292018-02-26 13:54:34 -0500430 // check for degeneracy
431 SkVector v0 = pts[1] - pts[0];
432 SkVector v1 = pts[2] - pts[0];
433 if (SkScalarNearlyZero(v0.cross(v1))) {
434 return;
435 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500436 // TODO: Pull PathUtils out of Ganesh?
437 int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance);
Mike Kleincc9856c2018-04-19 09:18:33 -0400438 fPointBuffer.setCount(maxCount);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500439 SkPoint* target = fPointBuffer.begin();
440 int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
441 kQuadTolerance, &target, maxCount);
442 fPointBuffer.setCount(count);
443 for (int i = 0; i < count; i++) {
444 this->handleLine(fPointBuffer[i]);
445 }
446#else
447 // for now, just to draw something
448 this->handleLine(pts[1]);
449 this->handleLine(pts[2]);
450#endif
451}
452
Brian Salomonaff27a22017-02-06 15:47:44 -0500453void SkBaseShadowTessellator::handleQuad(const SkMatrix& m, SkPoint pts[3]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500454 m.mapPoints(pts, 3);
455 this->handleQuad(pts);
456}
457
Brian Salomonaff27a22017-02-06 15:47:44 -0500458void SkBaseShadowTessellator::handleCubic(const SkMatrix& m, SkPoint pts[4]) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500459 m.mapPoints(pts, 4);
460#if SK_SUPPORT_GPU
461 // TODO: Pull PathUtils out of Ganesh?
462 int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance);
Mike Kleincc9856c2018-04-19 09:18:33 -0400463 fPointBuffer.setCount(maxCount);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500464 SkPoint* target = fPointBuffer.begin();
465 int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
466 kCubicTolerance, &target, maxCount);
467 fPointBuffer.setCount(count);
468 for (int i = 0; i < count; i++) {
469 this->handleLine(fPointBuffer[i]);
470 }
471#else
472 // for now, just to draw something
473 this->handleLine(pts[1]);
474 this->handleLine(pts[2]);
475 this->handleLine(pts[3]);
476#endif
477}
478
Brian Salomonaff27a22017-02-06 15:47:44 -0500479void SkBaseShadowTessellator::handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w) {
Jim Van Verthda965502017-04-11 15:29:14 -0400480 if (m.hasPerspective()) {
481 w = SkConic::TransformW(pts, w, m);
482 }
Jim Van Vertha84898d2017-02-06 13:38:23 -0500483 m.mapPoints(pts, 3);
484 SkAutoConicToQuads quadder;
485 const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance);
486 SkPoint lastPoint = *(quads++);
487 int count = quadder.countQuads();
488 for (int i = 0; i < count; ++i) {
489 SkPoint quadPts[3];
490 quadPts[0] = lastPoint;
491 quadPts[1] = quads[0];
492 quadPts[2] = i == count - 1 ? pts[2] : quads[1];
493 this->handleQuad(quadPts);
494 lastPoint = quadPts[2];
495 quads += 2;
496 }
497}
498
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400499bool SkBaseShadowTessellator::addArc(const SkVector& nextNormal, bool finishArc) {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500500 // fill in fan from previous quad
501 SkScalar rotSin, rotCos;
502 int numSteps;
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400503 SkComputeRadialSteps(fPrevOutset, nextNormal, fRadius, &rotSin, &rotCos, &numSteps);
Jim Van Verth76387852017-05-16 16:55:16 -0400504 SkVector prevNormal = fPrevOutset;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400505 for (int i = 0; i < numSteps-1; ++i) {
Jim Van Verthda965502017-04-11 15:29:14 -0400506 SkVector currNormal;
507 currNormal.fX = prevNormal.fX*rotCos - prevNormal.fY*rotSin;
508 currNormal.fY = prevNormal.fY*rotCos + prevNormal.fX*rotSin;
509 *fPositions.push() = fPrevPoint + currNormal;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500510 *fColors.push() = fPenumbraColor;
Jim Van Verth872da6b2018-04-10 11:24:11 -0400511 this->appendTriangle(fPrevUmbraIndex, fPositions.count() - 1, fPositions.count() - 2);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500512
Jim Van Verthda965502017-04-11 15:29:14 -0400513 prevNormal = currNormal;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500514 }
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400515 if (finishArc && numSteps) {
Jim Van Verthda965502017-04-11 15:29:14 -0400516 *fPositions.push() = fPrevPoint + nextNormal;
517 *fColors.push() = fPenumbraColor;
Jim Van Verth872da6b2018-04-10 11:24:11 -0400518 this->appendTriangle(fPrevUmbraIndex, fPositions.count() - 1, fPositions.count() - 2);
Jim Van Verthda965502017-04-11 15:29:14 -0400519 }
Jim Van Verth76387852017-05-16 16:55:16 -0400520 fPrevOutset = nextNormal;
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -0400521
522 return (numSteps > 0);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500523}
524
Jim Van Verth872da6b2018-04-10 11:24:11 -0400525void SkBaseShadowTessellator::appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2) {
526 auto indices = fIndices.append(3);
527
528 indices[0] = index0;
529 indices[1] = index1;
530 indices[2] = index2;
531}
532
533void SkBaseShadowTessellator::appendQuad(uint16_t index0, uint16_t index1,
534 uint16_t index2, uint16_t index3) {
535 auto indices = fIndices.append(6);
536
537 indices[0] = index0;
538 indices[1] = index1;
539 indices[2] = index2;
540
541 indices[3] = index2;
542 indices[4] = index1;
543 indices[5] = index3;
544}
545
Jim Van Verthda965502017-04-11 15:29:14 -0400546bool SkBaseShadowTessellator::setTransformedHeightFunc(const SkMatrix& ctm) {
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400547 if (SkScalarNearlyZero(fZPlaneParams.fX) && SkScalarNearlyZero(fZPlaneParams.fY)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400548 fTransformedHeightFunc = [this](const SkPoint& p) {
Jim Van Verthe308a122017-05-08 14:19:30 -0400549 return fZPlaneParams.fZ;
Jim Van Verthda965502017-04-11 15:29:14 -0400550 };
551 } else {
552 SkMatrix ctmInverse;
Jim Van Vertha947e292018-02-26 13:54:34 -0500553 if (!ctm.invert(&ctmInverse) || !ctmInverse.isFinite()) {
Jim Van Verthda965502017-04-11 15:29:14 -0400554 return false;
555 }
Jim Van Verthda965502017-04-11 15:29:14 -0400556 // multiply by transpose
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400557 fTransformedZParams = SkPoint3::Make(
Jim Van Verthe308a122017-05-08 14:19:30 -0400558 ctmInverse[SkMatrix::kMScaleX] * fZPlaneParams.fX +
559 ctmInverse[SkMatrix::kMSkewY] * fZPlaneParams.fY +
560 ctmInverse[SkMatrix::kMPersp0] * fZPlaneParams.fZ,
561
562 ctmInverse[SkMatrix::kMSkewX] * fZPlaneParams.fX +
563 ctmInverse[SkMatrix::kMScaleY] * fZPlaneParams.fY +
564 ctmInverse[SkMatrix::kMPersp1] * fZPlaneParams.fZ,
565
566 ctmInverse[SkMatrix::kMTransX] * fZPlaneParams.fX +
567 ctmInverse[SkMatrix::kMTransY] * fZPlaneParams.fY +
568 ctmInverse[SkMatrix::kMPersp2] * fZPlaneParams.fZ
569 );
Jim Van Verthda965502017-04-11 15:29:14 -0400570
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400571 if (ctm.hasPerspective()) {
572 // We use Cramer's rule to solve for the W value for a given post-divide X and Y,
573 // so pre-compute those values that are independent of X and Y.
574 // W is det(ctmInverse)/(PD[0]*X + PD[1]*Y + PD[2])
575 fPartialDeterminants[0] = ctm[SkMatrix::kMSkewY] * ctm[SkMatrix::kMPersp1] -
576 ctm[SkMatrix::kMScaleY] * ctm[SkMatrix::kMPersp0];
577 fPartialDeterminants[1] = ctm[SkMatrix::kMPersp0] * ctm[SkMatrix::kMSkewX] -
578 ctm[SkMatrix::kMPersp1] * ctm[SkMatrix::kMScaleX];
579 fPartialDeterminants[2] = ctm[SkMatrix::kMScaleX] * ctm[SkMatrix::kMScaleY] -
580 ctm[SkMatrix::kMSkewX] * ctm[SkMatrix::kMSkewY];
581 SkScalar ctmDeterminant = ctm[SkMatrix::kMTransX] * fPartialDeterminants[0] +
582 ctm[SkMatrix::kMTransY] * fPartialDeterminants[1] +
583 ctm[SkMatrix::kMPersp2] * fPartialDeterminants[2];
Jim Van Verthda965502017-04-11 15:29:14 -0400584
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400585 // Pre-bake the numerator of Cramer's rule into the zParams to avoid another multiply.
586 // TODO: this may introduce numerical instability, but I haven't seen any issues yet.
587 fTransformedZParams.fX *= ctmDeterminant;
588 fTransformedZParams.fY *= ctmDeterminant;
589 fTransformedZParams.fZ *= ctmDeterminant;
Jim Van Verthda965502017-04-11 15:29:14 -0400590
Jim Van Verth4c9b8932017-05-15 13:49:21 -0400591 fTransformedHeightFunc = [this](const SkPoint& p) {
592 SkScalar denom = p.fX * fPartialDeterminants[0] +
593 p.fY * fPartialDeterminants[1] +
594 fPartialDeterminants[2];
595 SkScalar w = SkScalarFastInvert(denom);
596 return fZOffset + w*(fTransformedZParams.fX * p.fX +
597 fTransformedZParams.fY * p.fY +
598 fTransformedZParams.fZ);
599 };
600 } else {
601 fTransformedHeightFunc = [this](const SkPoint& p) {
602 return fZOffset + fTransformedZParams.fX * p.fX +
603 fTransformedZParams.fY * p.fY + fTransformedZParams.fZ;
604 };
605 }
Jim Van Verthda965502017-04-11 15:29:14 -0400606 }
607
608 return true;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500609}
610
611
612//////////////////////////////////////////////////////////////////////////////////////////////////
613
Brian Salomonaff27a22017-02-06 15:47:44 -0500614class SkAmbientShadowTessellator : public SkBaseShadowTessellator {
Jim Van Vertha84898d2017-02-06 13:38:23 -0500615public:
616 SkAmbientShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400617 const SkPoint3& zPlaneParams, bool transparent);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500618
619private:
Jim Van Verth3645bb02018-06-26 14:58:58 -0400620 bool computePathPolygon(const SkPath& path, const SkMatrix& ctm);
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400621 bool computeConvexShadow();
622 bool computeConcaveShadow();
623
Jim Van Verth7deacf42018-06-08 15:13:25 -0400624 void handlePolyPoint(const SkPoint& p, bool finalPoint);
625 void addEdge(const SkPoint& nextPoint, const SkVector& nextNormal, bool finalEdge);
626 void splitEdge(const SkPoint& nextPoint, const SkVector& insetNormal,
627 const SkPoint& penumbraPoint, const SkPoint& umbraPoint, SkColor umbraColor);
Jim Van Vertha84898d2017-02-06 13:38:23 -0500628
Jim Van Verthda965502017-04-11 15:29:14 -0400629 static constexpr auto kMaxEdgeLenSqr = 20 * 20;
Jim Van Verth76387852017-05-16 16:55:16 -0400630 static constexpr auto kInsetFactor = -0.5f;
Jim Van Verthda965502017-04-11 15:29:14 -0400631
632 SkScalar offset(SkScalar z) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400633 return SkDrawShadowMetrics::AmbientBlurRadius(z);
Jim Van Verthda965502017-04-11 15:29:14 -0400634 }
635 SkColor umbraColor(SkScalar z) {
Jim Van Verth1af03d42017-07-31 09:34:58 -0400636 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(z));
Jim Van Verth060d9822017-05-04 09:58:17 -0400637 return SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
Jim Van Verthda965502017-04-11 15:29:14 -0400638 }
639
Jim Van Verth76387852017-05-16 16:55:16 -0400640 bool fSplitFirstEdge;
641 bool fSplitPreviousEdge;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500642
Brian Salomonaff27a22017-02-06 15:47:44 -0500643 typedef SkBaseShadowTessellator INHERITED;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500644};
645
Jim Van Verthefe3ded2017-01-30 13:11:45 -0500646SkAmbientShadowTessellator::SkAmbientShadowTessellator(const SkPath& path,
Jim Van Vertha84898d2017-02-06 13:38:23 -0500647 const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400648 const SkPoint3& zPlaneParams,
Jim Van Verthbce74962017-01-25 09:39:46 -0500649 bool transparent)
Jim Van Verth76387852017-05-16 16:55:16 -0400650 : INHERITED(zPlaneParams, transparent)
651 , fSplitFirstEdge(false)
652 , fSplitPreviousEdge(false) {
Jim Van Verthda965502017-04-11 15:29:14 -0400653 // Set base colors
Jim Van Verth1af03d42017-07-31 09:34:58 -0400654 SkScalar umbraAlpha = SkScalarInvert(SkDrawShadowMetrics::AmbientRecipAlpha(heightFunc(0, 0)));
Jim Van Verthb4366552017-03-27 14:25:29 -0400655 // umbraColor is the interior value, penumbraColor the exterior value.
656 // umbraAlpha is the factor that is linearly interpolated from outside to inside, and
657 // then "blurred" by the GrBlurredEdgeFP. It is then multiplied by fAmbientAlpha to get
658 // the final alpha.
Jim Van Verth060d9822017-05-04 09:58:17 -0400659 fUmbraColor = SkColorSetARGB(umbraAlpha * 255.9999f, 0, 0, 0);
660 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -0400661
Jim Van Verthda965502017-04-11 15:29:14 -0400662 // make sure we're not below the canvas plane
663 this->setZOffset(path.getBounds(), ctm.hasPerspective());
664
Jim Van Verth7c8008c2018-02-07 15:02:50 -0500665 if (!this->setTransformedHeightFunc(ctm)) {
666 return;
667 }
Jim Van Verthda965502017-04-11 15:29:14 -0400668
Jim Van Verth3645bb02018-06-26 14:58:58 -0400669 if (!this->computePathPolygon(path, ctm)) {
670 return;
671 }
672 if (fPathPolygon.count() < 3 || !SkScalarIsFinite(fArea)) {
673 fSucceeded = true; // We don't want to try to blur these cases, so we will
674 // return an empty SkVertices instead.
675 return;
676 }
677
Jim Van Verthbce74962017-01-25 09:39:46 -0500678 // Outer ring: 3*numPts
679 // Middle ring: numPts
680 fPositions.setReserve(4 * path.countPoints());
681 fColors.setReserve(4 * path.countPoints());
682 // Outer ring: 12*numPts
683 // Middle ring: 0
684 fIndices.setReserve(12 * path.countPoints());
685
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400686 if (fIsConvex) {
687 fSucceeded = this->computeConvexShadow();
688 } else {
689 fSucceeded = this->computeConcaveShadow();
690 }
691}
692
Jim Van Verth3645bb02018-06-26 14:58:58 -0400693bool SkAmbientShadowTessellator::computePathPolygon(const SkPath& path, const SkMatrix& ctm) {
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400694 fPathPolygon.setReserve(path.countPoints());
695
696 // walk around the path, tessellate and generate outer ring
697 // if original path is transparent, will accumulate sum of points for centroid
698 SkPath::Iter iter(path, true);
699 SkPoint pts[4];
700 SkPath::Verb verb;
Jim Van Verth3645bb02018-06-26 14:58:58 -0400701 bool verbSeen = false;
702 bool closeSeen = false;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400703 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Jim Van Verth3645bb02018-06-26 14:58:58 -0400704 if (closeSeen) {
705 return false;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400706 }
Jim Van Verth3645bb02018-06-26 14:58:58 -0400707 switch (verb) {
708 case SkPath::kLine_Verb:
709 this->handleLine(ctm, &pts[1]);
710 break;
711 case SkPath::kQuad_Verb:
712 this->handleQuad(ctm, pts);
713 break;
714 case SkPath::kCubic_Verb:
715 this->handleCubic(ctm, pts);
716 break;
717 case SkPath::kConic_Verb:
718 this->handleConic(ctm, pts, iter.conicWeight());
719 break;
720 case SkPath::kMove_Verb:
721 if (verbSeen) {
722 return false;
723 }
724 break;
725 case SkPath::kClose_Verb:
726 case SkPath::kDone_Verb:
727 closeSeen = true;
728 break;
729 }
730 verbSeen = true;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400731 }
732
733 this->finishPathPolygon();
Jim Van Verth3645bb02018-06-26 14:58:58 -0400734 return true;
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400735}
736
737bool SkAmbientShadowTessellator::computeConvexShadow() {
Jim Van Verth7deacf42018-06-08 15:13:25 -0400738 int polyCount = fPathPolygon.count();
739 if (polyCount < 3) {
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400740 return false;
Brian Salomon0dda9cb2017-02-03 10:33:25 -0500741 }
742
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400743 // Add center point for fan if needed
744 if (fTransparent) {
745 *fPositions.push() = fCentroid;
746 *fColors.push() = this->umbraColor(fTransformedHeightFunc(fCentroid));
747 }
748
Jim Van Verth7deacf42018-06-08 15:13:25 -0400749 // Initialize
750 SkVector normal;
751 if (!compute_normal(fPathPolygon[polyCount-1], fPathPolygon[0], fDirection, &normal)) {
752 // the polygon should be sanitized, so any issues at this point are unrecoverable
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400753 return false;
Jim Van Verthf507c282018-05-11 10:48:20 -0400754 }
Jim Van Verth7deacf42018-06-08 15:13:25 -0400755 fFirstPoint = fPathPolygon[polyCount - 1];
756 fFirstVertexIndex = fPositions.count();
757 SkScalar z = fTransformedHeightFunc(fFirstPoint);
758 fFirstOutset = normal;
759 fFirstOutset *= this->offset(z);
Jim Van Verthf507c282018-05-11 10:48:20 -0400760
Jim Van Verth7deacf42018-06-08 15:13:25 -0400761 fPrevOutset = fFirstOutset;
762 fPrevPoint = fFirstPoint;
763 fPrevUmbraIndex = fFirstVertexIndex;
Jim Van Verthbce74962017-01-25 09:39:46 -0500764
Jim Van Verth7deacf42018-06-08 15:13:25 -0400765 // Add the first quad
766 *fPositions.push() = fFirstPoint;
767 *fColors.push() = this->umbraColor(z);
768 *fPositions.push() = fFirstPoint + fFirstOutset;
769 *fColors.push() = fPenumbraColor;
Jim Van Verth76387852017-05-16 16:55:16 -0400770
Jim Van Verth7deacf42018-06-08 15:13:25 -0400771 z = fTransformedHeightFunc(fPathPolygon[0]);
772 fRadius = this->offset(z);
773 fUmbraColor = this->umbraColor(z);
774 this->addEdge(fPathPolygon[0], normal, false);
Jim Van Verthbce74962017-01-25 09:39:46 -0500775
Jim Van Verth7deacf42018-06-08 15:13:25 -0400776 // Process the remaining points
777 for (int i = 1; i < fPathPolygon.count(); ++i) {
778 this->handlePolyPoint(fPathPolygon[i], i == fPathPolygon.count()-1);
779 }
780 SkASSERT(this->indexCount());
Jim Van Verthda965502017-04-11 15:29:14 -0400781
Jim Van Verth7deacf42018-06-08 15:13:25 -0400782 // Final fan
783 SkASSERT(fPositions.count() >= 3);
784 if (this->addArc(fFirstOutset, false)) {
785 this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1, fFirstVertexIndex + 1);
786 } else {
787 // arc is too small, set the first penumbra point to be the same position
788 // as the last one
789 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verthbce74962017-01-25 09:39:46 -0500790 }
791
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400792 return true;
Jim Van Verthbce74962017-01-25 09:39:46 -0500793}
794
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400795bool SkAmbientShadowTessellator::computeConcaveShadow() {
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400796 if (!SkIsSimplePolygon(&fPathPolygon[0], fPathPolygon.count())) {
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400797 return false;
Jim Van Verthf507c282018-05-11 10:48:20 -0400798 }
799
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400800 // generate inner ring
801 SkTDArray<SkPoint> umbraPolygon;
802 SkTDArray<int> umbraIndices;
803 umbraIndices.setReserve(fPathPolygon.count());
804 if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), 0.5f,
805 &umbraPolygon, &umbraIndices)) {
806 // TODO: figure out how to handle this case
807 return false;
808 }
809
810 // generate outer ring
811 SkTDArray<SkPoint> penumbraPolygon;
812 SkTDArray<int> penumbraIndices;
813 penumbraPolygon.setReserve(umbraPolygon.count());
814 penumbraIndices.setReserve(umbraPolygon.count());
Jim Van Verthe39bc9f2018-06-14 14:43:00 -0400815
816 auto offsetFunc = [this](const SkPoint& p) { return -this->offset(fTransformedHeightFunc(p)); };
817 if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), offsetFunc,
Jim Van Verth8760e2f2018-06-12 14:21:38 -0400818 &penumbraPolygon, &penumbraIndices)) {
819 // TODO: figure out how to handle this case
820 return false;
821 }
822
823 if (!umbraPolygon.count() || !penumbraPolygon.count()) {
824 return false;
825 }
826
827 // attach the rings together
828 this->stitchConcaveRings(umbraPolygon, &umbraIndices, penumbraPolygon, &penumbraIndices);
829
830 return true;
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400831}
832
Jim Van Verth7deacf42018-06-08 15:13:25 -0400833void SkAmbientShadowTessellator::handlePolyPoint(const SkPoint& p, bool finalPoint) {
Jim Van Verthbce74962017-01-25 09:39:46 -0500834 SkVector normal;
Jim Van Verth76387852017-05-16 16:55:16 -0400835 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
Jim Van Verthda965502017-04-11 15:29:14 -0400836 SkVector scaledNormal = normal;
837 scaledNormal *= fRadius;
838 this->addArc(scaledNormal, true);
839 SkScalar z = fTransformedHeightFunc(p);
840 fRadius = this->offset(z);
841 fUmbraColor = this->umbraColor(z);
Jim Van Verth7deacf42018-06-08 15:13:25 -0400842 this->addEdge(p, normal, finalPoint);
Jim Van Verthbce74962017-01-25 09:39:46 -0500843 }
844}
845
Jim Van Verth7deacf42018-06-08 15:13:25 -0400846void SkAmbientShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal,
847 bool finalEdge) {
Jim Van Verth76387852017-05-16 16:55:16 -0400848 // We compute the inset in two stages: first we inset by half the current normal,
849 // then on the next addEdge() we add half of the next normal to get an average of the two
850 SkVector insetNormal = nextNormal;
851 insetNormal *= 0.5f*kInsetFactor;
852
853 // Adding the other half of the average for the previous edge
854 fPositions[fPrevUmbraIndex] += insetNormal;
855
Jim Van Verth7deacf42018-06-08 15:13:25 -0400856 SkPoint umbraPoint;
857 if (finalEdge) {
858 // Again, adding the other half of the average for the previous edge
859 fPositions[fFirstVertexIndex] += insetNormal;
860 // we multiply by another half because now we're adding to an average of an average
861 if (fSplitFirstEdge) {
862 fPositions[fFirstVertexIndex + 2] += insetNormal * 0.5f;
863 }
864 umbraPoint = fPositions[fFirstVertexIndex];
865 } else {
866 umbraPoint = nextPoint + insetNormal;
867 }
Jim Van Verth76387852017-05-16 16:55:16 -0400868 SkVector outsetNormal = nextNormal;
869 outsetNormal *= fRadius;
870 SkPoint penumbraPoint = nextPoint + outsetNormal;
871
Jim Van Verth7deacf42018-06-08 15:13:25 -0400872 // make sure we don't end up with a sharp alpha edge along the quad diagonal
873 this->splitEdge(nextPoint, insetNormal, penumbraPoint, umbraPoint, fUmbraColor);
874
875 // add next quad
876 int prevPenumbraIndex;
877 int currUmbraIndex;
878 if (finalEdge) {
879 prevPenumbraIndex = fPositions.count() - 1;
880 currUmbraIndex = fFirstVertexIndex;
881 } else {
882 prevPenumbraIndex = fPositions.count() - 1;
883 *fPositions.push() = umbraPoint;
884 *fColors.push() = fUmbraColor;
885 currUmbraIndex = fPositions.count() - 1;
886 }
887
888 *fPositions.push() = penumbraPoint;
889 *fColors.push() = fPenumbraColor;
890
891 // set triangularization to get best interpolation of color
892 if (fColors[fPrevUmbraIndex] > fUmbraColor) {
893 this->appendQuad(fPrevUmbraIndex, prevPenumbraIndex,
894 currUmbraIndex, fPositions.count() - 1);
895 } else {
896 this->appendQuad(currUmbraIndex, fPositions.count() - 1,
897 fPrevUmbraIndex, prevPenumbraIndex);
898 }
899
900 // if transparent, add to center fan
901 if (fTransparent) {
902 this->appendTriangle(0, fPrevUmbraIndex, currUmbraIndex);
903 }
904
905 fPrevUmbraIndex = currUmbraIndex;
906 fPrevPoint = nextPoint;
907 fPrevOutset = outsetNormal;
908}
909
910void SkAmbientShadowTessellator::splitEdge(const SkPoint& nextPoint, const SkVector& insetNormal,
911 const SkPoint& penumbraPoint, const SkPoint& umbraPoint,
912 SkColor umbraColor) {
Jim Van Verth76387852017-05-16 16:55:16 -0400913 // For split edges, we're adding an average of two averages, so we multiply by another half
914 if (fSplitPreviousEdge) {
Jim Van Verth7deacf42018-06-08 15:13:25 -0400915 fPositions[fPrevUmbraIndex - 2] += insetNormal*SK_ScalarHalf;
Jim Van Verth76387852017-05-16 16:55:16 -0400916 }
917
918 // Split the edge to make sure we don't end up with a sharp alpha edge along the quad diagonal
Jim Van Verth7deacf42018-06-08 15:13:25 -0400919 if (fColors[fPrevUmbraIndex] != umbraColor &&
Cary Clarkdf429f32017-11-08 11:44:31 -0500920 SkPointPriv::DistanceToSqd(nextPoint, fPositions[fPrevUmbraIndex]) > kMaxEdgeLenSqr) {
Jim Van Verth76387852017-05-16 16:55:16 -0400921
922 // This is lacking 1/4 of the next inset -- we'll add it the next time we call addEdge()
923 SkPoint centerPoint = fPositions[fPrevUmbraIndex] + umbraPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400924 centerPoint *= 0.5f;
925 *fPositions.push() = centerPoint;
Jim Van Verth7deacf42018-06-08 15:13:25 -0400926 *fColors.push() = SkPMLerp(umbraColor, fColors[fPrevUmbraIndex], 128);
927 centerPoint = fPositions[fPositions.count() - 2] + penumbraPoint;
Jim Van Verth76387852017-05-16 16:55:16 -0400928 centerPoint *= 0.5f;
929 *fPositions.push() = centerPoint;
Jim Van Verthda965502017-04-11 15:29:14 -0400930 *fColors.push() = fPenumbraColor;
931
932 // set triangularization to get best interpolation of color
933 if (fColors[fPrevUmbraIndex] > fColors[fPositions.count() - 2]) {
Jim Van Verth872da6b2018-04-10 11:24:11 -0400934 this->appendQuad(fPrevUmbraIndex, fPositions.count() - 3,
935 fPositions.count() - 2, fPositions.count() - 1);
Jim Van Verthda965502017-04-11 15:29:14 -0400936 } else {
Jim Van Verth872da6b2018-04-10 11:24:11 -0400937 this->appendQuad(fPositions.count() - 2, fPositions.count() - 1,
938 fPrevUmbraIndex, fPositions.count() - 3);
Jim Van Verthda965502017-04-11 15:29:14 -0400939 }
940
Jim Van Verthcdaf6612018-06-05 15:21:13 -0400941 // if transparent, add to center fan
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400942 if (fTransparent) {
Jim Van Verth872da6b2018-04-10 11:24:11 -0400943 this->appendTriangle(0, fPrevUmbraIndex, fPositions.count() - 2);
Jim Van Verth1c4c1142017-05-11 10:23:53 -0400944 }
945
Jim Van Verth76387852017-05-16 16:55:16 -0400946 fSplitPreviousEdge = true;
947 if (fPrevUmbraIndex == fFirstVertexIndex) {
948 fSplitFirstEdge = true;
949 }
Jim Van Verthda965502017-04-11 15:29:14 -0400950 fPrevUmbraIndex = fPositions.count() - 2;
Jim Van Verth76387852017-05-16 16:55:16 -0400951 } else {
952 fSplitPreviousEdge = false;
Jim Van Verthda965502017-04-11 15:29:14 -0400953 }
Jim Van Verthbce74962017-01-25 09:39:46 -0500954}
Jim Van Verth91af7272017-01-27 14:15:54 -0500955
Jim Van Verth7deacf42018-06-08 15:13:25 -0400956
Jim Van Verth91af7272017-01-27 14:15:54 -0500957///////////////////////////////////////////////////////////////////////////////////////////////////
958
Brian Salomonaff27a22017-02-06 15:47:44 -0500959class SkSpotShadowTessellator : public SkBaseShadowTessellator {
Brian Salomon958fbc42017-01-30 17:01:28 -0500960public:
Jim Van Vertha84898d2017-02-06 13:38:23 -0500961 SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -0400962 const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -0400963 SkScalar lightRadius, bool transparent);
Brian Salomon958fbc42017-01-30 17:01:28 -0500964
Brian Salomon958fbc42017-01-30 17:01:28 -0500965private:
Jim Van Verth3645bb02018-06-26 14:58:58 -0400966 bool computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -0400967 const SkMatrix& shadowTransform);
Brian Salomonab664fa2017-03-24 16:07:20 +0000968 void computeClipVectorsAndTestCentroid();
Brian Salomon66085ed2017-02-02 15:39:34 -0500969 bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint);
Brian Salomonab664fa2017-03-24 16:07:20 +0000970 int getClosestUmbraPoint(const SkPoint& point);
Brian Salomon958fbc42017-01-30 17:01:28 -0500971
Jim Van Verth872da6b2018-04-10 11:24:11 -0400972 bool computeConvexShadow(SkScalar radius);
973 bool computeConcaveShadow(SkScalar radius);
974
Jim Van Verth7deacf42018-06-08 15:13:25 -0400975 bool handlePolyPoint(const SkPoint& p, bool lastPoint);
Brian Salomon958fbc42017-01-30 17:01:28 -0500976
977 void mapPoints(SkScalar scale, const SkVector& xlate, SkPoint* pts, int count);
Jim Van Verth8c2de8f2018-05-14 11:20:58 -0400978 bool addInnerPoint(const SkPoint& pathPoint, int* currUmbraIndex);
Jim Van Verth7deacf42018-06-08 15:13:25 -0400979 void addEdge(const SkVector& nextPoint, const SkVector& nextNormal, bool lastEdge);
Jim Van Verthf507c282018-05-11 10:48:20 -0400980 void addToClip(const SkVector& nextPoint);
Jim Van Verthda965502017-04-11 15:29:14 -0400981
982 SkScalar offset(SkScalar z) {
983 float zRatio = SkTPin(z / (fLightZ - z), 0.0f, 0.95f);
984 return fLightRadius*zRatio;
985 }
986
987 SkScalar fLightZ;
988 SkScalar fLightRadius;
989 SkScalar fOffsetAdjust;
Brian Salomon958fbc42017-01-30 17:01:28 -0500990
Brian Salomon958fbc42017-01-30 17:01:28 -0500991 SkTDArray<SkPoint> fClipPolygon;
Brian Salomon66085ed2017-02-02 15:39:34 -0500992 SkTDArray<SkVector> fClipVectors;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500993
Brian Salomonab664fa2017-03-24 16:07:20 +0000994 SkTDArray<SkPoint> fUmbraPolygon;
995 int fCurrClipPoint;
996 int fCurrUmbraPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -0500997 bool fPrevUmbraOutside;
998 bool fFirstUmbraOutside;
Jim Van Vertha84898d2017-02-06 13:38:23 -0500999 bool fValidUmbra;
Brian Salomon958fbc42017-01-30 17:01:28 -05001000
Brian Salomonaff27a22017-02-06 15:47:44 -05001001 typedef SkBaseShadowTessellator INHERITED;
Brian Salomon958fbc42017-01-30 17:01:28 -05001002};
1003
Jim Van Vertha84898d2017-02-06 13:38:23 -05001004SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001005 const SkPoint3& zPlaneParams,
Jim Van Verthb4366552017-03-27 14:25:29 -04001006 const SkPoint3& lightPos, SkScalar lightRadius,
Jim Van Verth060d9822017-05-04 09:58:17 -04001007 bool transparent)
Jim Van Verthe308a122017-05-08 14:19:30 -04001008 : INHERITED(zPlaneParams, transparent)
Jim Van Verthda965502017-04-11 15:29:14 -04001009 , fLightZ(lightPos.fZ)
1010 , fLightRadius(lightRadius)
1011 , fOffsetAdjust(0)
1012 , fCurrClipPoint(0)
1013 , fPrevUmbraOutside(false)
1014 , fFirstUmbraOutside(false)
1015 , fValidUmbra(true) {
1016
1017 // make sure we're not below the canvas plane
1018 if (this->setZOffset(path.getBounds(), ctm.hasPerspective())) {
1019 // Adjust light height and radius
1020 fLightRadius *= (fLightZ + fZOffset) / fLightZ;
1021 fLightZ += fZOffset;
1022 }
Jim Van Verthb4366552017-03-27 14:25:29 -04001023
1024 // Set radius and colors
Jim Van Verthb4366552017-03-27 14:25:29 -04001025 SkPoint center = SkPoint::Make(path.getBounds().centerX(), path.getBounds().centerY());
Jim Van Verthe308a122017-05-08 14:19:30 -04001026 SkScalar occluderHeight = this->heightFunc(center.fX, center.fY) + fZOffset;
Jim Van Verth060d9822017-05-04 09:58:17 -04001027 fUmbraColor = SkColorSetARGB(255, 0, 0, 0);
1028 fPenumbraColor = SkColorSetARGB(0, 0, 0, 0);
Jim Van Verthb4366552017-03-27 14:25:29 -04001029
Jim Van Verth1af03d42017-07-31 09:34:58 -04001030 // Compute the blur radius, scale and translation for the spot shadow.
1031 SkScalar radius;
Jim Van Verthda965502017-04-11 15:29:14 -04001032 SkMatrix shadowTransform;
1033 if (!ctm.hasPerspective()) {
Jim Van Verth1af03d42017-07-31 09:34:58 -04001034 SkScalar scale;
1035 SkVector translate;
1036 SkDrawShadowMetrics::GetSpotParams(occluderHeight, lightPos.fX, lightPos.fY, fLightZ,
1037 lightRadius, &radius, &scale, &translate);
Jim Van Verthda965502017-04-11 15:29:14 -04001038 shadowTransform.setScaleTranslate(scale, scale, translate.fX, translate.fY);
1039 } else {
1040 // For perspective, we have a scale, a z-shear, and another projective divide --
1041 // this varies at each point so we can't use an affine transform.
1042 // We'll just apply this to each generated point in turn.
1043 shadowTransform.reset();
1044 // Also can't cull the center (for now).
1045 fTransparent = true;
Jim Van Verth1af03d42017-07-31 09:34:58 -04001046 radius = SkDrawShadowMetrics::SpotBlurRadius(occluderHeight, lightPos.fZ, lightRadius);
Jim Van Verthda965502017-04-11 15:29:14 -04001047 }
Jim Van Verth1af03d42017-07-31 09:34:58 -04001048 fRadius = radius;
Jim Van Verthda965502017-04-11 15:29:14 -04001049 SkMatrix fullTransform = SkMatrix::Concat(shadowTransform, ctm);
1050
1051 // Set up our reverse mapping
Jim Van Verth7c8008c2018-02-07 15:02:50 -05001052 if (!this->setTransformedHeightFunc(fullTransform)) {
1053 return;
1054 }
Jim Van Verthb4366552017-03-27 14:25:29 -04001055
Brian Salomonab664fa2017-03-24 16:07:20 +00001056 // compute rough clip bounds for umbra, plus offset polygon, plus centroid
Jim Van Verth3645bb02018-06-26 14:58:58 -04001057 if (!this->computeClipAndPathPolygons(path, ctm, shadowTransform)) {
1058 return;
1059 }
1060 if (fClipPolygon.count() < 3 || fPathPolygon.count() < 3 || !SkScalarIsFinite(fArea)) {
1061 fSucceeded = true; // We don't want to try to blur these cases, so we will
1062 // return an empty SkVertices instead.
Brian Salomon66085ed2017-02-02 15:39:34 -05001063 return;
1064 }
Brian Salomon66085ed2017-02-02 15:39:34 -05001065
Jim Van Verthf507c282018-05-11 10:48:20 -04001066 // compute vectors for clip tests
1067 this->computeClipVectorsAndTestCentroid();
1068
Brian Salomonab664fa2017-03-24 16:07:20 +00001069 // check to see if umbra collapses
Jim Van Verthf507c282018-05-11 10:48:20 -04001070 if (fIsConvex) {
Jim Van Verth872da6b2018-04-10 11:24:11 -04001071 SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid,
1072 fPathPolygon[0],
1073 fPathPolygon[1]);
1074 SkRect bounds;
1075 bounds.setBounds(&fPathPolygon[0], fPathPolygon.count());
1076 for (int i = 1; i < fPathPolygon.count(); ++i) {
1077 int j = i + 1;
1078 if (i == fPathPolygon.count() - 1) {
1079 j = 0;
1080 }
1081 SkPoint currPoint = fPathPolygon[i];
1082 SkPoint nextPoint = fPathPolygon[j];
1083 SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint,
1084 nextPoint);
1085 if (distSq < minDistSq) {
1086 minDistSq = distSq;
1087 }
Brian Salomonab664fa2017-03-24 16:07:20 +00001088 }
Jim Van Verth872da6b2018-04-10 11:24:11 -04001089 static constexpr auto kTolerance = 1.0e-2f;
1090 if (minDistSq < (radius + kTolerance)*(radius + kTolerance)) {
1091 // if the umbra would collapse, we back off a bit on inner blur and adjust the alpha
1092 SkScalar newRadius = SkScalarSqrt(minDistSq) - kTolerance;
1093 fOffsetAdjust = newRadius - radius;
1094 SkScalar ratio = 128 * (newRadius + radius) / radius;
1095 // they aren't PMColors, but the interpolation algorithm is the same
1096 fUmbraColor = SkPMLerp(fUmbraColor, fPenumbraColor, (unsigned)ratio);
1097 radius = newRadius;
Brian Salomonab664fa2017-03-24 16:07:20 +00001098 }
1099 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001100
Jim Van Verth3645bb02018-06-26 14:58:58 -04001101 // TODO: calculate these reserves better
1102 // Penumbra ring: 3*numPts
1103 // Umbra ring: numPts
1104 // Inner ring: numPts
1105 fPositions.setReserve(5 * path.countPoints());
1106 fColors.setReserve(5 * path.countPoints());
1107 // Penumbra ring: 12*numPts
1108 // Umbra ring: 3*numPts
1109 fIndices.setReserve(15 * path.countPoints());
1110
Jim Van Verthf507c282018-05-11 10:48:20 -04001111 if (fIsConvex) {
Jim Van Verth8760e2f2018-06-12 14:21:38 -04001112 fSucceeded = this->computeConvexShadow(radius);
Jim Van Verth872da6b2018-04-10 11:24:11 -04001113 } else {
Jim Van Verth8760e2f2018-06-12 14:21:38 -04001114 fSucceeded = this->computeConcaveShadow(radius);
1115 }
1116
1117 if (!fSucceeded) {
Jim Van Verthf507c282018-05-11 10:48:20 -04001118 return;
Jim Van Verth91af7272017-01-27 14:15:54 -05001119 }
Jim Van Verthda965502017-04-11 15:29:14 -04001120
1121 if (ctm.hasPerspective()) {
1122 for (int i = 0; i < fPositions.count(); ++i) {
1123 SkScalar pathZ = fTransformedHeightFunc(fPositions[i]);
1124 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
1125 fPositions[i].fX = (fPositions[i].fX*fLightZ - lightPos.fX*pathZ)*factor;
1126 fPositions[i].fY = (fPositions[i].fY*fLightZ - lightPos.fY*pathZ)*factor;
1127 }
1128#ifdef DRAW_CENTROID
1129 SkScalar pathZ = fTransformedHeightFunc(fCentroid);
1130 SkScalar factor = SkScalarInvert(fLightZ - pathZ);
1131 fCentroid.fX = (fCentroid.fX*fLightZ - lightPos.fX*pathZ)*factor;
1132 fCentroid.fY = (fCentroid.fY*fLightZ - lightPos.fY*pathZ)*factor;
1133#endif
1134 }
1135#ifdef DRAW_CENTROID
1136 *fPositions.push() = fCentroid + SkVector::Make(-2, -2);
1137 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1138 *fPositions.push() = fCentroid + SkVector::Make(2, -2);
1139 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1140 *fPositions.push() = fCentroid + SkVector::Make(-2, 2);
1141 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1142 *fPositions.push() = fCentroid + SkVector::Make(2, 2);
1143 *fColors.push() = SkColorSetARGB(255, 0, 255, 255);
1144
Jim Van Verth872da6b2018-04-10 11:24:11 -04001145 this->appendQuad(fPositions.count() - 2, fPositions.count() - 1,
1146 fPositions.count() - 4, fPositions.count() - 3);
Jim Van Verthda965502017-04-11 15:29:14 -04001147#endif
1148
Brian Salomon0dda9cb2017-02-03 10:33:25 -05001149 fSucceeded = true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001150}
1151
Jim Van Verthf507c282018-05-11 10:48:20 -04001152void SkSpotShadowTessellator::addToClip(const SkPoint& point) {
1153 if (fClipPolygon.isEmpty() || !duplicate_pt(point, fClipPolygon[fClipPolygon.count()-1])) {
1154 *fClipPolygon.push() = point;
1155 }
1156}
1157
Jim Van Verth3645bb02018-06-26 14:58:58 -04001158bool SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthda965502017-04-11 15:29:14 -04001159 const SkMatrix& shadowTransform) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001160
1161 fPathPolygon.setReserve(path.countPoints());
Jim Van Verth3645bb02018-06-26 14:58:58 -04001162 fClipPolygon.setReserve(path.countPoints());
Brian Salomonab664fa2017-03-24 16:07:20 +00001163
1164 // Walk around the path and compute clip polygon and path polygon.
1165 // Will also accumulate sum of areas for centroid.
1166 // For Bezier curves, we compute additional interior points on curve.
Jim Van Verth91af7272017-01-27 14:15:54 -05001167 SkPath::Iter iter(path, true);
1168 SkPoint pts[4];
1169 SkPath::Verb verb;
1170
Brian Salomon66085ed2017-02-02 15:39:34 -05001171 // coefficients to compute cubic Bezier at t = 5/16
Brian Salomonab664fa2017-03-24 16:07:20 +00001172 static constexpr SkScalar kA = 0.32495117187f;
1173 static constexpr SkScalar kB = 0.44311523437f;
1174 static constexpr SkScalar kC = 0.20141601562f;
1175 static constexpr SkScalar kD = 0.03051757812f;
Brian Salomon66085ed2017-02-02 15:39:34 -05001176
1177 SkPoint curvePoint;
1178 SkScalar w;
Jim Van Verth3645bb02018-06-26 14:58:58 -04001179 bool closeSeen = false;
1180 bool verbSeen = false;
Jim Van Verth91af7272017-01-27 14:15:54 -05001181 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Jim Van Verth3645bb02018-06-26 14:58:58 -04001182 if (closeSeen) {
1183 return false;
1184 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001185 switch (verb) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001186 case SkPath::kLine_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001187 ctm.mapPoints(&pts[1], 1);
Jim Van Verthf507c282018-05-11 10:48:20 -04001188 this->addToClip(pts[1]);
Jim Van Verthcdaf6612018-06-05 15:21:13 -04001189 this->handleLine(shadowTransform, &pts[1]);
Jim Van Verth91af7272017-01-27 14:15:54 -05001190 break;
1191 case SkPath::kQuad_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001192 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001193 // point at t = 1/2
1194 curvePoint.fX = 0.25f*pts[0].fX + 0.5f*pts[1].fX + 0.25f*pts[2].fX;
1195 curvePoint.fY = 0.25f*pts[0].fY + 0.5f*pts[1].fY + 0.25f*pts[2].fY;
Jim Van Verthf507c282018-05-11 10:48:20 -04001196 this->addToClip(curvePoint);
1197 this->addToClip(pts[2]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001198 this->handleQuad(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001199 break;
1200 case SkPath::kConic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001201 ctm.mapPoints(pts, 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001202 w = iter.conicWeight();
Jim Van Vertha84898d2017-02-06 13:38:23 -05001203 // point at t = 1/2
Brian Salomon66085ed2017-02-02 15:39:34 -05001204 curvePoint.fX = 0.25f*pts[0].fX + w*0.5f*pts[1].fX + 0.25f*pts[2].fX;
1205 curvePoint.fY = 0.25f*pts[0].fY + w*0.5f*pts[1].fY + 0.25f*pts[2].fY;
1206 curvePoint *= SkScalarInvert(0.5f + 0.5f*w);
Jim Van Verthf507c282018-05-11 10:48:20 -04001207 this->addToClip(curvePoint);
1208 this->addToClip(pts[2]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001209 this->handleConic(shadowTransform, pts, w);
Jim Van Verth91af7272017-01-27 14:15:54 -05001210 break;
1211 case SkPath::kCubic_Verb:
Jim Van Vertha84898d2017-02-06 13:38:23 -05001212 ctm.mapPoints(pts, 4);
Brian Salomon66085ed2017-02-02 15:39:34 -05001213 // point at t = 5/16
1214 curvePoint.fX = kA*pts[0].fX + kB*pts[1].fX + kC*pts[2].fX + kD*pts[3].fX;
1215 curvePoint.fY = kA*pts[0].fY + kB*pts[1].fY + kC*pts[2].fY + kD*pts[3].fY;
Jim Van Verthf507c282018-05-11 10:48:20 -04001216 this->addToClip(curvePoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001217 // point at t = 11/16
1218 curvePoint.fX = kD*pts[0].fX + kC*pts[1].fX + kB*pts[2].fX + kA*pts[3].fX;
1219 curvePoint.fY = kD*pts[0].fY + kC*pts[1].fY + kB*pts[2].fY + kA*pts[3].fY;
Jim Van Verthf507c282018-05-11 10:48:20 -04001220 this->addToClip(curvePoint);
1221 this->addToClip(pts[3]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001222 this->handleCubic(shadowTransform, pts);
Jim Van Verth91af7272017-01-27 14:15:54 -05001223 break;
Brian Salomonab664fa2017-03-24 16:07:20 +00001224 case SkPath::kMove_Verb:
Jim Van Verth3645bb02018-06-26 14:58:58 -04001225 if (verbSeen) {
1226 return false;
1227 }
1228 break;
Jim Van Verth91af7272017-01-27 14:15:54 -05001229 case SkPath::kClose_Verb:
Brian Salomonab664fa2017-03-24 16:07:20 +00001230 case SkPath::kDone_Verb:
Jim Van Verth3645bb02018-06-26 14:58:58 -04001231 closeSeen = true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001232 break;
1233 default:
1234 SkDEBUGFAIL("unknown verb");
1235 }
Jim Van Verth3645bb02018-06-26 14:58:58 -04001236 verbSeen = true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001237 }
1238
Jim Van Verthcdaf6612018-06-05 15:21:13 -04001239 this->finishPathPolygon();
Brian Salomonab664fa2017-03-24 16:07:20 +00001240 fCurrClipPoint = fClipPolygon.count() - 1;
Jim Van Verth3645bb02018-06-26 14:58:58 -04001241 return true;
Brian Salomon66085ed2017-02-02 15:39:34 -05001242}
1243
Brian Salomonab664fa2017-03-24 16:07:20 +00001244void SkSpotShadowTessellator::computeClipVectorsAndTestCentroid() {
Brian Salomon66085ed2017-02-02 15:39:34 -05001245 SkASSERT(fClipPolygon.count() >= 3);
Brian Salomon66085ed2017-02-02 15:39:34 -05001246
Brian Salomonab664fa2017-03-24 16:07:20 +00001247 // init clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001248 SkVector v0 = fClipPolygon[1] - fClipPolygon[0];
Jim Van Verthf507c282018-05-11 10:48:20 -04001249 SkVector v1 = fClipPolygon[2] - fClipPolygon[0];
Brian Salomon66085ed2017-02-02 15:39:34 -05001250 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001251
1252 // init centroid check
1253 bool hiddenCentroid = true;
Jim Van Verthf507c282018-05-11 10:48:20 -04001254 v1 = fCentroid - fClipPolygon[0];
Brian Salomon66085ed2017-02-02 15:39:34 -05001255 SkScalar initCross = v0.cross(v1);
1256
1257 for (int p = 1; p < fClipPolygon.count(); ++p) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001258 // add to clip vectors
Brian Salomon66085ed2017-02-02 15:39:34 -05001259 v0 = fClipPolygon[(p + 1) % fClipPolygon.count()] - fClipPolygon[p];
1260 *fClipVectors.push() = v0;
Brian Salomon66085ed2017-02-02 15:39:34 -05001261 // Determine if transformed centroid is inside clipPolygon.
Brian Salomonab664fa2017-03-24 16:07:20 +00001262 v1 = fCentroid - fClipPolygon[p];
Brian Salomon66085ed2017-02-02 15:39:34 -05001263 if (initCross*v0.cross(v1) <= 0) {
1264 hiddenCentroid = false;
1265 }
1266 }
1267 SkASSERT(fClipVectors.count() == fClipPolygon.count());
1268
Brian Salomonab664fa2017-03-24 16:07:20 +00001269 fTransparent = fTransparent || !hiddenCentroid;
Brian Salomon66085ed2017-02-02 15:39:34 -05001270}
1271
1272bool SkSpotShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid,
1273 SkPoint* clipPoint) {
1274 SkVector segmentVector = centroid - umbraPoint;
1275
Brian Salomonab664fa2017-03-24 16:07:20 +00001276 int startClipPoint = fCurrClipPoint;
Brian Salomon66085ed2017-02-02 15:39:34 -05001277 do {
Brian Salomonab664fa2017-03-24 16:07:20 +00001278 SkVector dp = umbraPoint - fClipPolygon[fCurrClipPoint];
1279 SkScalar denom = fClipVectors[fCurrClipPoint].cross(segmentVector);
Brian Salomon66085ed2017-02-02 15:39:34 -05001280 SkScalar t_num = dp.cross(segmentVector);
1281 // if line segments are nearly parallel
1282 if (SkScalarNearlyZero(denom)) {
1283 // and collinear
1284 if (SkScalarNearlyZero(t_num)) {
1285 return false;
1286 }
1287 // otherwise are separate, will try the next poly segment
1288 // else if crossing lies within poly segment
1289 } else if (t_num >= 0 && t_num <= denom) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001290 SkScalar s_num = dp.cross(fClipVectors[fCurrClipPoint]);
Brian Salomon66085ed2017-02-02 15:39:34 -05001291 // if umbra point is inside the clip polygon
Jim Van Verthda965502017-04-11 15:29:14 -04001292 if (s_num >= 0 && s_num <= denom) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001293 segmentVector *= s_num/denom;
1294 *clipPoint = umbraPoint + segmentVector;
1295 return true;
1296 }
1297 }
Brian Salomonab664fa2017-03-24 16:07:20 +00001298 fCurrClipPoint = (fCurrClipPoint + 1) % fClipPolygon.count();
1299 } while (fCurrClipPoint != startClipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001300
1301 return false;
Jim Van Verth91af7272017-01-27 14:15:54 -05001302}
1303
Brian Salomonab664fa2017-03-24 16:07:20 +00001304int SkSpotShadowTessellator::getClosestUmbraPoint(const SkPoint& p) {
Cary Clarkdf429f32017-11-08 11:44:31 -05001305 SkScalar minDistance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[fCurrUmbraPoint]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001306 int index = fCurrUmbraPoint;
1307 int dir = 1;
1308 int next = (index + dir) % fUmbraPolygon.count();
1309
1310 // init travel direction
Cary Clarkdf429f32017-11-08 11:44:31 -05001311 SkScalar distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001312 if (distance < minDistance) {
1313 index = next;
1314 minDistance = distance;
1315 } else {
1316 dir = fUmbraPolygon.count()-1;
1317 }
1318
1319 // iterate until we find a point that increases the distance
1320 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001321 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001322 while (distance < minDistance) {
1323 index = next;
1324 minDistance = distance;
1325 next = (index + dir) % fUmbraPolygon.count();
Cary Clarkdf429f32017-11-08 11:44:31 -05001326 distance = SkPointPriv::DistanceToSqd(p, fUmbraPolygon[next]);
Brian Salomonab664fa2017-03-24 16:07:20 +00001327 }
1328
1329 fCurrUmbraPoint = index;
1330 return index;
1331}
1332
Jim Van Verth872da6b2018-04-10 11:24:11 -04001333bool SkSpotShadowTessellator::computeConvexShadow(SkScalar radius) {
1334 // generate inner ring
1335 if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.count(), radius,
1336 &fUmbraPolygon)) {
1337 // this shouldn't happen, but just in case we'll inset using the centroid
1338 fValidUmbra = false;
1339 }
1340
1341 // walk around the path polygon, generate outer ring and connect to inner ring
1342 if (fTransparent) {
1343 *fPositions.push() = fCentroid;
1344 *fColors.push() = fUmbraColor;
1345 }
1346 fCurrUmbraPoint = 0;
Jim Van Verth872da6b2018-04-10 11:24:11 -04001347
Jim Van Verth7deacf42018-06-08 15:13:25 -04001348 // initial setup
1349 // add first quad
1350 int polyCount = fPathPolygon.count();
1351 if (!compute_normal(fPathPolygon[polyCount-1], fPathPolygon[0], fDirection, &fFirstOutset)) {
1352 // polygon should be sanitized by this point, so this is unrecoverable
Jim Van Verth872da6b2018-04-10 11:24:11 -04001353 return false;
1354 }
1355
Jim Van Verth7deacf42018-06-08 15:13:25 -04001356 fFirstOutset *= fRadius;
1357 fFirstPoint = fPathPolygon[polyCount - 1];
1358 fFirstVertexIndex = fPositions.count();
1359 fPrevOutset = fFirstOutset;
1360 fPrevPoint = fFirstPoint;
1361 fPrevUmbraIndex = -1;
Jim Van Verth872da6b2018-04-10 11:24:11 -04001362
Jim Van Verth7deacf42018-06-08 15:13:25 -04001363 this->addInnerPoint(fFirstPoint, &fPrevUmbraIndex);
1364
1365 if (!fTransparent) {
1366 SkPoint clipPoint;
1367 bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex],
1368 fCentroid, &clipPoint);
1369 if (isOutside) {
1370 *fPositions.push() = clipPoint;
1371 *fColors.push() = fUmbraColor;
Jim Van Verth872da6b2018-04-10 11:24:11 -04001372 }
Jim Van Verth7deacf42018-06-08 15:13:25 -04001373 fPrevUmbraOutside = isOutside;
1374 fFirstUmbraOutside = isOutside;
Jim Van Verth872da6b2018-04-10 11:24:11 -04001375 }
1376
Jim Van Verth7deacf42018-06-08 15:13:25 -04001377 SkPoint newPoint = fFirstPoint + fFirstOutset;
1378 *fPositions.push() = newPoint;
1379 *fColors.push() = fPenumbraColor;
1380 this->addEdge(fPathPolygon[0], fFirstOutset, false);
1381
1382 for (int i = 1; i < polyCount; ++i) {
1383 if (!this->handlePolyPoint(fPathPolygon[i], i == polyCount-1)) {
1384 return false;
1385 }
1386 }
1387 SkASSERT(this->indexCount());
1388
Jim Van Verth872da6b2018-04-10 11:24:11 -04001389 // final fan
Jim Van Verth7deacf42018-06-08 15:13:25 -04001390 SkASSERT(fPositions.count() >= 3);
1391 if (this->addArc(fFirstOutset, false)) {
1392 if (fFirstUmbraOutside) {
1393 this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1,
1394 fFirstVertexIndex + 2);
Jim Van Verth872da6b2018-04-10 11:24:11 -04001395 } else {
Jim Van Verth7deacf42018-06-08 15:13:25 -04001396 this->appendTriangle(fFirstVertexIndex, fPositions.count() - 1,
1397 fFirstVertexIndex + 1);
1398 }
1399 } else {
1400 // no arc added, fix up by setting first penumbra point position to last one
1401 if (fFirstUmbraOutside) {
1402 fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.count() - 1];
1403 } else {
1404 fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.count() - 1];
Jim Van Verth872da6b2018-04-10 11:24:11 -04001405 }
1406 }
1407
1408 return true;
1409}
1410
1411bool SkSpotShadowTessellator::computeConcaveShadow(SkScalar radius) {
Jim Van Verth8664a1d2018-06-28 16:26:50 -04001412 if (!SkIsSimplePolygon(&fPathPolygon[0], fPathPolygon.count())) {
Jim Van Verth872da6b2018-04-10 11:24:11 -04001413 return false;
1414 }
1415
1416 // generate inner ring
1417 SkTDArray<int> umbraIndices;
1418 umbraIndices.setReserve(fPathPolygon.count());
1419 if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), radius,
1420 &fUmbraPolygon, &umbraIndices)) {
1421 // TODO: figure out how to handle this case
1422 return false;
1423 }
1424
1425 // generate outer ring
1426 SkTDArray<SkPoint> penumbraPolygon;
1427 SkTDArray<int> penumbraIndices;
1428 penumbraPolygon.setReserve(fUmbraPolygon.count());
1429 penumbraIndices.setReserve(fUmbraPolygon.count());
1430 if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.count(), -radius,
1431 &penumbraPolygon, &penumbraIndices)) {
1432 // TODO: figure out how to handle this case
1433 return false;
1434 }
1435
1436 if (!fUmbraPolygon.count() || !penumbraPolygon.count()) {
1437 return false;
1438 }
1439
1440 // attach the rings together
Jim Van Verth8760e2f2018-06-12 14:21:38 -04001441 this->stitchConcaveRings(fUmbraPolygon, &umbraIndices, penumbraPolygon, &penumbraIndices);
Jim Van Verth872da6b2018-04-10 11:24:11 -04001442
1443 return true;
1444}
1445
Jim Van Verthefe3ded2017-01-30 13:11:45 -05001446void SkSpotShadowTessellator::mapPoints(SkScalar scale, const SkVector& xlate,
Jim Van Verth91af7272017-01-27 14:15:54 -05001447 SkPoint* pts, int count) {
1448 // TODO: vectorize
1449 for (int i = 0; i < count; ++i) {
1450 pts[i] *= scale;
1451 pts[i] += xlate;
1452 }
1453}
1454
Jim Van Verth7deacf42018-06-08 15:13:25 -04001455bool SkSpotShadowTessellator::handlePolyPoint(const SkPoint& p, bool lastPoint) {
Jim Van Verth91af7272017-01-27 14:15:54 -05001456 SkVector normal;
Jim Van Verthda965502017-04-11 15:29:14 -04001457 if (compute_normal(fPrevPoint, p, fDirection, &normal)) {
1458 normal *= fRadius;
1459 this->addArc(normal, true);
Jim Van Verth7deacf42018-06-08 15:13:25 -04001460 this->addEdge(p, normal, lastPoint);
Jim Van Verth91af7272017-01-27 14:15:54 -05001461 }
Jim Van Verthb55eb282017-07-18 14:13:45 -04001462
1463 return true;
Jim Van Verth91af7272017-01-27 14:15:54 -05001464}
1465
Jim Van Verth8c2de8f2018-05-14 11:20:58 -04001466bool SkSpotShadowTessellator::addInnerPoint(const SkPoint& pathPoint, int* currUmbraIndex) {
Brian Salomonab664fa2017-03-24 16:07:20 +00001467 SkPoint umbraPoint;
1468 if (!fValidUmbra) {
1469 SkVector v = fCentroid - pathPoint;
1470 v *= 0.95f;
1471 umbraPoint = pathPoint + v;
Jim Van Verth91af7272017-01-27 14:15:54 -05001472 } else {
Brian Salomonab664fa2017-03-24 16:07:20 +00001473 umbraPoint = fUmbraPolygon[this->getClosestUmbraPoint(pathPoint)];
Jim Van Verth91af7272017-01-27 14:15:54 -05001474 }
Brian Salomon66085ed2017-02-02 15:39:34 -05001475
Jim Van Verth91af7272017-01-27 14:15:54 -05001476 fPrevPoint = pathPoint;
Brian Salomonab664fa2017-03-24 16:07:20 +00001477
1478 // merge "close" points
Jim Van Verthe7e1d9d2017-05-01 16:06:48 -04001479 if (fPrevUmbraIndex == -1 ||
Brian Salomonab664fa2017-03-24 16:07:20 +00001480 !duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) {
Jim Van Verth8c2de8f2018-05-14 11:20:58 -04001481 // if we've wrapped around, don't add a new point
1482 if (fPrevUmbraIndex >= 0 && duplicate_pt(umbraPoint, fPositions[fFirstVertexIndex])) {
1483 *currUmbraIndex = fFirstVertexIndex;
1484 } else {
1485 *currUmbraIndex = fPositions.count();
1486 *fPositions.push() = umbraPoint;
1487 *fColors.push() = fUmbraColor;
1488 }
Brian Salomonab664fa2017-03-24 16:07:20 +00001489 return false;
1490 } else {
Jim Van Verth8c2de8f2018-05-14 11:20:58 -04001491 *currUmbraIndex = fPrevUmbraIndex;
Brian Salomonab664fa2017-03-24 16:07:20 +00001492 return true;
1493 }
Jim Van Verth91af7272017-01-27 14:15:54 -05001494}
1495
Jim Van Verth7deacf42018-06-08 15:13:25 -04001496void SkSpotShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal,
1497 bool lastEdge) {
Brian Salomon66085ed2017-02-02 15:39:34 -05001498 // add next umbra point
Jim Van Verth8c2de8f2018-05-14 11:20:58 -04001499 int currUmbraIndex;
Jim Van Verth7deacf42018-06-08 15:13:25 -04001500 bool duplicate;
1501 if (lastEdge) {
1502 duplicate = false;
1503 currUmbraIndex = fFirstVertexIndex;
1504 fPrevPoint = nextPoint;
1505 } else {
1506 duplicate = this->addInnerPoint(nextPoint, &currUmbraIndex);
1507 }
Jim Van Verth8c2de8f2018-05-14 11:20:58 -04001508 int prevPenumbraIndex = duplicate || (currUmbraIndex == fFirstVertexIndex)
1509 ? fPositions.count()-1
1510 : fPositions.count()-2;
Brian Salomonab664fa2017-03-24 16:07:20 +00001511 if (!duplicate) {
1512 // add to center fan if transparent or centroid showing
1513 if (fTransparent) {
Jim Van Verth872da6b2018-04-10 11:24:11 -04001514 this->appendTriangle(0, fPrevUmbraIndex, currUmbraIndex);
Brian Salomonab664fa2017-03-24 16:07:20 +00001515 // otherwise add to clip ring
1516 } else {
Brian Salomon66085ed2017-02-02 15:39:34 -05001517 SkPoint clipPoint;
Jim Van Verth7deacf42018-06-08 15:13:25 -04001518 bool isOutside = lastEdge ? fFirstUmbraOutside
1519 : this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid,
1520 &clipPoint);
Brian Salomon66085ed2017-02-02 15:39:34 -05001521 if (isOutside) {
Jim Van Verth7deacf42018-06-08 15:13:25 -04001522 if (!lastEdge) {
1523 *fPositions.push() = clipPoint;
1524 *fColors.push() = fUmbraColor;
1525 }
Jim Van Verth872da6b2018-04-10 11:24:11 -04001526 this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, currUmbraIndex + 1);
Brian Salomon66085ed2017-02-02 15:39:34 -05001527 if (fPrevUmbraOutside) {
1528 // fill out quad
Jim Van Verth872da6b2018-04-10 11:24:11 -04001529 this->appendTriangle(fPrevUmbraIndex, currUmbraIndex + 1,
1530 fPrevUmbraIndex + 1);
Brian Salomon66085ed2017-02-02 15:39:34 -05001531 }
1532 } else if (fPrevUmbraOutside) {
1533 // add tri
Jim Van Verth872da6b2018-04-10 11:24:11 -04001534 this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, fPrevUmbraIndex + 1);
Brian Salomon66085ed2017-02-02 15:39:34 -05001535 }
Jim Van Verth872da6b2018-04-10 11:24:11 -04001536
Brian Salomon66085ed2017-02-02 15:39:34 -05001537 fPrevUmbraOutside = isOutside;
1538 }
1539 }
1540
1541 // add next penumbra point and quad
Jim Van Verth91af7272017-01-27 14:15:54 -05001542 SkPoint newPoint = nextPoint + nextNormal;
1543 *fPositions.push() = newPoint;
1544 *fColors.push() = fPenumbraColor;
1545
Brian Salomonab664fa2017-03-24 16:07:20 +00001546 if (!duplicate) {
Jim Van Verth872da6b2018-04-10 11:24:11 -04001547 this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex);
Brian Salomonab664fa2017-03-24 16:07:20 +00001548 }
Jim Van Verth872da6b2018-04-10 11:24:11 -04001549 this->appendTriangle(prevPenumbraIndex, fPositions.count() - 1, currUmbraIndex);
Jim Van Verth91af7272017-01-27 14:15:54 -05001550
Brian Salomon66085ed2017-02-02 15:39:34 -05001551 fPrevUmbraIndex = currUmbraIndex;
Jim Van Verth76387852017-05-16 16:55:16 -04001552 fPrevOutset = nextNormal;
Jim Van Verth91af7272017-01-27 14:15:54 -05001553}
Brian Salomon958fbc42017-01-30 17:01:28 -05001554
1555///////////////////////////////////////////////////////////////////////////////////////////////////
1556
Brian Salomonaff27a22017-02-06 15:47:44 -05001557sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001558 const SkPoint3& zPlane, bool transparent) {
Mike Reed27575e82018-05-17 10:11:31 -04001559 if (!ctm.mapRect(path.getBounds()).isFinite() || !zPlane.isFinite()) {
Mike Reed9d5c6742018-03-06 10:31:27 -05001560 return nullptr;
1561 }
Jim Van Verthe308a122017-05-08 14:19:30 -04001562 SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001563 return ambientTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001564}
1565
Brian Salomonaff27a22017-02-06 15:47:44 -05001566sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm,
Jim Van Verthe308a122017-05-08 14:19:30 -04001567 const SkPoint3& zPlane, const SkPoint3& lightPos,
Jim Van Verth060d9822017-05-04 09:58:17 -04001568 SkScalar lightRadius, bool transparent) {
Mike Reed27575e82018-05-17 10:11:31 -04001569 if (!ctm.mapRect(path.getBounds()).isFinite() || !zPlane.isFinite() ||
Jim Van Verth1989c492018-05-31 13:15:16 -04001570 !lightPos.isFinite() || !(lightPos.fZ >= SK_ScalarNearlyZero) ||
1571 !SkScalarIsFinite(lightRadius) || !(lightRadius >= SK_ScalarNearlyZero)) {
Mike Reed9d5c6742018-03-06 10:31:27 -05001572 return nullptr;
1573 }
Jim Van Verthe308a122017-05-08 14:19:30 -04001574 SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent);
Brian Salomonaff27a22017-02-06 15:47:44 -05001575 return spotTess.releaseVertices();
Brian Salomon958fbc42017-01-30 17:01:28 -05001576}