blob: 4619d44f3b9df29d263073ad9711cd7f155f6c24 [file] [log] [blame]
Michael Ludwig0f809022019-06-04 09:14:37 -04001/*
2 * Copyright 2019 Google LLC
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
8#include "src/gpu/geometry/GrQuadUtils.h"
9
Michael Ludwig61328202019-06-19 14:48:58 +000010#include "include/core/SkRect.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040011#include "include/private/GrTypesPriv.h"
Michael Ludwig61328202019-06-19 14:48:58 +000012#include "include/private/SkVx.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040013#include "src/gpu/geometry/GrQuad.h"
14
Michael Ludwig61328202019-06-19 14:48:58 +000015using V4f = skvx::Vec<4, float>;
16using M4f = skvx::Vec<4, int32_t>;
17
Michael Ludwigfb7ba522019-10-29 15:33:34 -040018#define AI SK_ALWAYS_INLINE
19
20static constexpr float kTolerance = 1e-2f;
Michael Ludwigfb7ba522019-10-29 15:33:34 -040021
22// These rotate the points/edge values either clockwise or counterclockwise assuming tri strip
23// order.
24static AI V4f next_cw(const V4f& v) {
25 return skvx::shuffle<2, 0, 3, 1>(v);
26}
27
28static AI V4f next_ccw(const V4f& v) {
29 return skvx::shuffle<1, 3, 0, 2>(v);
30}
31
32// Replaces zero-length 'bad' edge vectors with the reversed opposite edge vector.
33// e3 may be null if only 2D edges need to be corrected for.
34static AI void correct_bad_edges(const M4f& bad, V4f* e1, V4f* e2, V4f* e3) {
35 if (any(bad)) {
36 // Want opposite edges, L B T R -> R T B L but with flipped sign to preserve winding
37 *e1 = if_then_else(bad, -skvx::shuffle<3, 2, 1, 0>(*e1), *e1);
38 *e2 = if_then_else(bad, -skvx::shuffle<3, 2, 1, 0>(*e2), *e2);
39 if (e3) {
40 *e3 = if_then_else(bad, -skvx::shuffle<3, 2, 1, 0>(*e3), *e3);
41 }
42 }
43}
44
45// Replace 'bad' coordinates by rotating CCW to get the next point. c3 may be null for 2D points.
46static AI void correct_bad_coords(const M4f& bad, V4f* c1, V4f* c2, V4f* c3) {
47 if (any(bad)) {
48 *c1 = if_then_else(bad, next_ccw(*c1), *c1);
49 *c2 = if_then_else(bad, next_ccw(*c2), *c2);
50 if (c3) {
51 *c3 = if_then_else(bad, next_ccw(*c3), *c3);
52 }
53 }
54}
55
Michael Ludwig61328202019-06-19 14:48:58 +000056// Since the local quad may not be type kRect, this uses the opposites for each vertex when
57// interpolating, and calculates new ws in addition to new xs, ys.
58static void interpolate_local(float alpha, int v0, int v1, int v2, int v3,
59 float lx[4], float ly[4], float lw[4]) {
60 SkASSERT(v0 >= 0 && v0 < 4);
61 SkASSERT(v1 >= 0 && v1 < 4);
62 SkASSERT(v2 >= 0 && v2 < 4);
63 SkASSERT(v3 >= 0 && v3 < 4);
64
65 float beta = 1.f - alpha;
66 lx[v0] = alpha * lx[v0] + beta * lx[v2];
67 ly[v0] = alpha * ly[v0] + beta * ly[v2];
68 lw[v0] = alpha * lw[v0] + beta * lw[v2];
69
70 lx[v1] = alpha * lx[v1] + beta * lx[v3];
71 ly[v1] = alpha * ly[v1] + beta * ly[v3];
72 lw[v1] = alpha * lw[v1] + beta * lw[v3];
73}
74
75// Crops v0 to v1 based on the clipDevRect. v2 is opposite of v0, v3 is opposite of v1.
76// It is written to not modify coordinates if there's no intersection along the edge.
77// Ideally this would have been detected earlier and the entire draw is skipped.
78static bool crop_rect_edge(const SkRect& clipDevRect, int v0, int v1, int v2, int v3,
79 float x[4], float y[4], float lx[4], float ly[4], float lw[4]) {
80 SkASSERT(v0 >= 0 && v0 < 4);
81 SkASSERT(v1 >= 0 && v1 < 4);
82 SkASSERT(v2 >= 0 && v2 < 4);
83 SkASSERT(v3 >= 0 && v3 < 4);
84
85 if (SkScalarNearlyEqual(x[v0], x[v1])) {
86 // A vertical edge
87 if (x[v0] < clipDevRect.fLeft && x[v2] >= clipDevRect.fLeft) {
88 // Overlapping with left edge of clipDevRect
89 if (lx) {
90 float alpha = (x[v2] - clipDevRect.fLeft) / (x[v2] - x[v0]);
91 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
92 }
93 x[v0] = clipDevRect.fLeft;
94 x[v1] = clipDevRect.fLeft;
95 return true;
96 } else if (x[v0] > clipDevRect.fRight && x[v2] <= clipDevRect.fRight) {
97 // Overlapping with right edge of clipDevRect
98 if (lx) {
99 float alpha = (clipDevRect.fRight - x[v2]) / (x[v0] - x[v2]);
100 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
101 }
102 x[v0] = clipDevRect.fRight;
103 x[v1] = clipDevRect.fRight;
104 return true;
105 }
106 } else {
107 // A horizontal edge
108 SkASSERT(SkScalarNearlyEqual(y[v0], y[v1]));
109 if (y[v0] < clipDevRect.fTop && y[v2] >= clipDevRect.fTop) {
110 // Overlapping with top edge of clipDevRect
111 if (lx) {
112 float alpha = (y[v2] - clipDevRect.fTop) / (y[v2] - y[v0]);
113 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
114 }
115 y[v0] = clipDevRect.fTop;
116 y[v1] = clipDevRect.fTop;
117 return true;
118 } else if (y[v0] > clipDevRect.fBottom && y[v2] <= clipDevRect.fBottom) {
119 // Overlapping with bottom edge of clipDevRect
120 if (lx) {
121 float alpha = (clipDevRect.fBottom - y[v2]) / (y[v0] - y[v2]);
122 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
123 }
124 y[v0] = clipDevRect.fBottom;
125 y[v1] = clipDevRect.fBottom;
126 return true;
127 }
128 }
129
130 // No overlap so don't crop it
131 return false;
132}
133
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000134// Updates x and y to intersect with clipDevRect. lx, ly, and lw are updated appropriately and may
135// be null to skip calculations. Returns bit mask of edges that were clipped.
136static GrQuadAAFlags crop_rect(const SkRect& clipDevRect, float x[4], float y[4],
137 float lx[4], float ly[4], float lw[4]) {
Michael Ludwig61328202019-06-19 14:48:58 +0000138 GrQuadAAFlags clipEdgeFlags = GrQuadAAFlags::kNone;
139
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000140 // The quad's left edge may not align with the SkRect notion of left due to 90 degree rotations
141 // or mirrors. So, this processes the logical edges of the quad and clamps it to the 4 sides of
142 // clipDevRect.
Michael Ludwig61328202019-06-19 14:48:58 +0000143
144 // Quad's left is v0 to v1 (op. v2 and v3)
145 if (crop_rect_edge(clipDevRect, 0, 1, 2, 3, x, y, lx, ly, lw)) {
146 clipEdgeFlags |= GrQuadAAFlags::kLeft;
147 }
148 // Quad's top edge is v0 to v2 (op. v1 and v3)
149 if (crop_rect_edge(clipDevRect, 0, 2, 1, 3, x, y, lx, ly, lw)) {
150 clipEdgeFlags |= GrQuadAAFlags::kTop;
151 }
152 // Quad's right edge is v2 to v3 (op. v0 and v1)
153 if (crop_rect_edge(clipDevRect, 2, 3, 0, 1, x, y, lx, ly, lw)) {
154 clipEdgeFlags |= GrQuadAAFlags::kRight;
155 }
156 // Quad's bottom edge is v1 to v3 (op. v0 and v2)
157 if (crop_rect_edge(clipDevRect, 1, 3, 0, 2, x, y, lx, ly, lw)) {
158 clipEdgeFlags |= GrQuadAAFlags::kBottom;
159 }
160
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000161 return clipEdgeFlags;
162}
163
164// Similar to crop_rect, but assumes that both the device coordinates and optional local coordinates
165// geometrically match the TL, BL, TR, BR vertex ordering, i.e. axis-aligned but not flipped, etc.
166static GrQuadAAFlags crop_simple_rect(const SkRect& clipDevRect, float x[4], float y[4],
167 float lx[4], float ly[4]) {
168 GrQuadAAFlags clipEdgeFlags = GrQuadAAFlags::kNone;
169
170 // Update local coordinates proportionately to how much the device rect edge was clipped
171 const SkScalar dx = lx ? (lx[2] - lx[0]) / (x[2] - x[0]) : 0.f;
172 const SkScalar dy = ly ? (ly[1] - ly[0]) / (y[1] - y[0]) : 0.f;
173 if (clipDevRect.fLeft > x[0]) {
174 if (lx) {
175 lx[0] += (clipDevRect.fLeft - x[0]) * dx;
176 lx[1] = lx[0];
177 }
178 x[0] = clipDevRect.fLeft;
179 x[1] = clipDevRect.fLeft;
180 clipEdgeFlags |= GrQuadAAFlags::kLeft;
Michael Ludwig61328202019-06-19 14:48:58 +0000181 }
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000182 if (clipDevRect.fTop > y[0]) {
183 if (ly) {
184 ly[0] += (clipDevRect.fTop - y[0]) * dy;
185 ly[2] = ly[0];
186 }
187 y[0] = clipDevRect.fTop;
188 y[2] = clipDevRect.fTop;
189 clipEdgeFlags |= GrQuadAAFlags::kTop;
190 }
191 if (clipDevRect.fRight < x[2]) {
192 if (lx) {
193 lx[2] -= (x[2] - clipDevRect.fRight) * dx;
194 lx[3] = lx[2];
195 }
196 x[2] = clipDevRect.fRight;
197 x[3] = clipDevRect.fRight;
198 clipEdgeFlags |= GrQuadAAFlags::kRight;
199 }
200 if (clipDevRect.fBottom < y[1]) {
201 if (ly) {
202 ly[1] -= (y[1] - clipDevRect.fBottom) * dy;
203 ly[3] = ly[1];
204 }
205 y[1] = clipDevRect.fBottom;
206 y[3] = clipDevRect.fBottom;
207 clipEdgeFlags |= GrQuadAAFlags::kBottom;
208 }
209
210 return clipEdgeFlags;
211}
212// Consistent with GrQuad::asRect()'s return value but requires fewer operations since we don't need
213// to calculate the bounds of the quad.
214static bool is_simple_rect(const GrQuad& quad) {
215 if (quad.quadType() != GrQuad::Type::kAxisAligned) {
216 return false;
217 }
218 // v0 at the geometric top-left is unique, so we only need to compare x[0] < x[2] for left
219 // and y[0] < y[1] for top, but add a little padding to protect against numerical precision
220 // on R90 and R270 transforms tricking this check.
221 return ((quad.x(0) + SK_ScalarNearlyZero) < quad.x(2)) &&
222 ((quad.y(0) + SK_ScalarNearlyZero) < quad.y(1));
Michael Ludwig61328202019-06-19 14:48:58 +0000223}
224
225// Calculates barycentric coordinates for each point in (testX, testY) in the triangle formed by
226// (x0,y0) - (x1,y1) - (x2, y2) and stores them in u, v, w.
227static void barycentric_coords(float x0, float y0, float x1, float y1, float x2, float y2,
228 const V4f& testX, const V4f& testY,
229 V4f* u, V4f* v, V4f* w) {
230 // Modeled after SkPathOpsQuad::pointInTriangle() but uses float instead of double, is
231 // vectorized and outputs normalized barycentric coordinates instead of inside/outside test
232 float v0x = x2 - x0;
233 float v0y = y2 - y0;
234 float v1x = x1 - x0;
235 float v1y = y1 - y0;
236 V4f v2x = testX - x0;
237 V4f v2y = testY - y0;
238
239 float dot00 = v0x * v0x + v0y * v0y;
240 float dot01 = v0x * v1x + v0y * v1y;
241 V4f dot02 = v0x * v2x + v0y * v2y;
242 float dot11 = v1x * v1x + v1y * v1y;
243 V4f dot12 = v1x * v2x + v1y * v2y;
244 float invDenom = sk_ieee_float_divide(1.f, dot00 * dot11 - dot01 * dot01);
245 *u = (dot11 * dot02 - dot01 * dot12) * invDenom;
246 *v = (dot00 * dot12 - dot01 * dot02) * invDenom;
247 *w = 1.f - *u - *v;
248}
249
250static M4f inside_triangle(const V4f& u, const V4f& v, const V4f& w) {
251 return ((u >= 0.f) & (u <= 1.f)) & ((v >= 0.f) & (v <= 1.f)) & ((w >= 0.f) & (w <= 1.f));
252}
253
Michael Ludwig0f809022019-06-04 09:14:37 -0400254namespace GrQuadUtils {
255
256void ResolveAAType(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, const GrQuad& quad,
257 GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) {
258 // Most cases will keep the requested types unchanged
259 *outAAType = requestedAAType;
260 *outEdgeFlags = requestedEdgeFlags;
261
262 switch (requestedAAType) {
263 // When aa type is coverage, disable AA if the edge configuration doesn't actually need it
264 case GrAAType::kCoverage:
265 if (requestedEdgeFlags == GrQuadAAFlags::kNone) {
266 // Turn off anti-aliasing
267 *outAAType = GrAAType::kNone;
268 } else {
269 // For coverage AA, if the quad is a rect and it lines up with pixel boundaries
270 // then overall aa and per-edge aa can be completely disabled
271 if (quad.quadType() == GrQuad::Type::kAxisAligned && !quad.aaHasEffectOnRect()) {
272 *outAAType = GrAAType::kNone;
273 *outEdgeFlags = GrQuadAAFlags::kNone;
274 }
275 }
276 break;
277 // For no or msaa anti aliasing, override the edge flags since edge flags only make sense
278 // when coverage aa is being used.
279 case GrAAType::kNone:
280 *outEdgeFlags = GrQuadAAFlags::kNone;
281 break;
282 case GrAAType::kMSAA:
283 *outEdgeFlags = GrQuadAAFlags::kAll;
284 break;
Michael Ludwig0f809022019-06-04 09:14:37 -0400285 }
Michael Ludwig61328202019-06-19 14:48:58 +0000286}
287
288bool CropToRect(const SkRect& cropRect, GrAA cropAA, GrQuadAAFlags* edgeFlags, GrQuad* quad,
289 GrQuad* local) {
Michael Ludwiged71b7e2019-06-21 13:47:02 -0400290 SkASSERT(quad->isFinite());
291
Michael Ludwig61328202019-06-19 14:48:58 +0000292 if (quad->quadType() == GrQuad::Type::kAxisAligned) {
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000293 // crop_rect and crop_rect_simple keep the rectangles as rectangles, so the intersection
294 // of the crop and quad can be calculated exactly. Some care must be taken if the quad
295 // is axis-aligned but does not satisfy asRect() due to flips, etc.
296 GrQuadAAFlags clippedEdges;
Michael Ludwig61328202019-06-19 14:48:58 +0000297 if (local) {
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000298 if (is_simple_rect(*quad) && is_simple_rect(*local)) {
299 clippedEdges = crop_simple_rect(cropRect, quad->xs(), quad->ys(),
300 local->xs(), local->ys());
301 } else {
302 clippedEdges = crop_rect(cropRect, quad->xs(), quad->ys(),
303 local->xs(), local->ys(), local->ws());
304 }
Michael Ludwig61328202019-06-19 14:48:58 +0000305 } else {
Michael Ludwig0a7cab02019-07-09 17:20:08 +0000306 if (is_simple_rect(*quad)) {
307 clippedEdges = crop_simple_rect(cropRect, quad->xs(), quad->ys(), nullptr, nullptr);
308 } else {
309 clippedEdges = crop_rect(cropRect, quad->xs(), quad->ys(),
310 nullptr, nullptr, nullptr);
311 }
312 }
313
314 // Apply the clipped edge updates to the original edge flags
315 if (cropAA == GrAA::kYes) {
316 // Turn on all edges that were clipped
317 *edgeFlags |= clippedEdges;
318 } else {
319 // Turn off all edges that were clipped
320 *edgeFlags &= ~clippedEdges;
Michael Ludwig61328202019-06-19 14:48:58 +0000321 }
322 return true;
323 }
324
325 if (local) {
326 // FIXME (michaelludwig) Calculate cropped local coordinates when not kAxisAligned
327 return false;
328 }
329
330 V4f devX = quad->x4f();
331 V4f devY = quad->y4f();
332 V4f devIW = quad->iw4f();
333 // Project the 3D coordinates to 2D
334 if (quad->quadType() == GrQuad::Type::kPerspective) {
335 devX *= devIW;
336 devY *= devIW;
337 }
338
339 V4f clipX = {cropRect.fLeft, cropRect.fLeft, cropRect.fRight, cropRect.fRight};
340 V4f clipY = {cropRect.fTop, cropRect.fBottom, cropRect.fTop, cropRect.fBottom};
341
342 // Calculate barycentric coordinates for the 4 rect corners in the 2 triangles that the quad
343 // is tessellated into when drawn.
344 V4f u1, v1, w1;
345 barycentric_coords(devX[0], devY[0], devX[1], devY[1], devX[2], devY[2], clipX, clipY,
346 &u1, &v1, &w1);
347 V4f u2, v2, w2;
348 barycentric_coords(devX[1], devY[1], devX[3], devY[3], devX[2], devY[2], clipX, clipY,
349 &u2, &v2, &w2);
350
351 // clipDevRect is completely inside this quad if each corner is in at least one of two triangles
352 M4f inTri1 = inside_triangle(u1, v1, w1);
353 M4f inTri2 = inside_triangle(u2, v2, w2);
354 if (all(inTri1 | inTri2)) {
355 // We can crop to exactly the clipDevRect.
356 // FIXME (michaelludwig) - there are other ways to have determined quad covering the clip
357 // rect, but the barycentric coords will be useful to derive local coordinates in the future
358
359 // Since we are cropped to exactly clipDevRect, we have discarded any perspective and the
360 // type becomes kRect. If updated locals were requested, they will incorporate perspective.
361 // FIXME (michaelludwig) - once we have local coordinates handled, it may be desirable to
362 // keep the draw as perspective so that the hardware does perspective interpolation instead
363 // of pushing it into a local coord w and having the shader do an extra divide.
364 clipX.store(quad->xs());
365 clipY.store(quad->ys());
366 quad->ws()[0] = 1.f;
367 quad->ws()[1] = 1.f;
368 quad->ws()[2] = 1.f;
369 quad->ws()[3] = 1.f;
370 quad->setQuadType(GrQuad::Type::kAxisAligned);
371
372 // Update the edge flags to match the clip setting since all 4 edges have been clipped
373 *edgeFlags = cropAA == GrAA::kYes ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
374
375 return true;
376 }
377
378 // FIXME (michaelludwig) - use the GrQuadPerEdgeAA tessellation inset/outset math to move
379 // edges to the closest clip corner they are outside of
380
381 return false;
382}
Michael Ludwig0f809022019-06-04 09:14:37 -0400383
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400384///////////////////////////////////////////////////////////////////////////////////////////////////
385// TessellationHelper implementation
386///////////////////////////////////////////////////////////////////////////////////////////////////
387
Michael Ludwigce200ac2019-11-04 14:33:32 -0500388TessellationHelper::TessellationHelper(const GrQuad& deviceQuad, const GrQuad* localQuad)
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500389 : fDeviceType(deviceQuad.quadType())
Michael Ludwigce200ac2019-11-04 14:33:32 -0500390 , fLocalType(localQuad ? localQuad->quadType() : GrQuad::Type::kAxisAligned) {
391 fOriginal.fX = deviceQuad.x4f();
392 fOriginal.fY = deviceQuad.y4f();
393 fOriginal.fW = deviceQuad.w4f();
394
395 if (localQuad) {
396 fOriginal.fU = localQuad->x4f();
397 fOriginal.fV = localQuad->y4f();
398 fOriginal.fR = localQuad->w4f();
399 fOriginal.fUVRCount = fLocalType == GrQuad::Type::kPerspective ? 3 : 2;
400 } else {
401 fOriginal.fUVRCount = 0;
402 }
403
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500404 // Calculate all projected edge vector values for this quad.
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400405 if (fDeviceType == GrQuad::Type::kPerspective) {
406 V4f iw = 1.0 / fOriginal.fW;
Michael Ludwigce200ac2019-11-04 14:33:32 -0500407 fEdgeVectors.fX2D = fOriginal.fX * iw;
408 fEdgeVectors.fY2D = fOriginal.fY * iw;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400409 } else {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500410 fEdgeVectors.fX2D = fOriginal.fX;
411 fEdgeVectors.fY2D = fOriginal.fY;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400412 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400413
Michael Ludwigce200ac2019-11-04 14:33:32 -0500414 fEdgeVectors.fDX = next_ccw(fEdgeVectors.fX2D) - fEdgeVectors.fX2D;
415 fEdgeVectors.fDY = next_ccw(fEdgeVectors.fY2D) - fEdgeVectors.fY2D;
416 fEdgeVectors.fInvLengths = rsqrt(mad(fEdgeVectors.fDX, fEdgeVectors.fDX,
417 fEdgeVectors.fDY * fEdgeVectors.fDY));
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400418
419 // Normalize edge vectors
Michael Ludwigce200ac2019-11-04 14:33:32 -0500420 fEdgeVectors.fDX *= fEdgeVectors.fInvLengths;
421 fEdgeVectors.fDY *= fEdgeVectors.fInvLengths;
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500422
423 // Calculate angles between vectors
424 if (fDeviceType <= GrQuad::Type::kRectilinear) {
425 fEdgeVectors.fCosTheta = 0.f;
426 fEdgeVectors.fInvSinTheta = 1.f;
427 } else {
428 fEdgeVectors.fCosTheta = mad(fEdgeVectors.fDX, next_cw(fEdgeVectors.fDX),
429 fEdgeVectors.fDY * next_cw(fEdgeVectors.fDY));
430 // NOTE: if cosTheta is close to 1, inset/outset math will avoid the fast paths that rely
431 // on thefInvSinTheta since it will approach infinity.
432 fEdgeVectors.fInvSinTheta = rsqrt(1.f - fEdgeVectors.fCosTheta * fEdgeVectors.fCosTheta);
433 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400434}
435
Michael Ludwigce200ac2019-11-04 14:33:32 -0500436const TessellationHelper::EdgeEquations& TessellationHelper::getEdgeEquations() {
437 if (!fEdgeEquations.fValid) {
438 V4f dx = fEdgeVectors.fDX;
439 V4f dy = fEdgeVectors.fDY;
440 // Correct for bad edges by copying adjacent edge information into the bad component
441 correct_bad_edges(fEdgeVectors.fInvLengths >= 1.f / kTolerance, &dx, &dy, nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400442
Michael Ludwigce200ac2019-11-04 14:33:32 -0500443 V4f c = mad(dx, fEdgeVectors.fY2D, -dy * fEdgeVectors.fX2D);
444 // Make sure normals point into the shape
445 V4f test = mad(dy, next_cw(fEdgeVectors.fX2D), mad(-dx, next_cw(fEdgeVectors.fY2D), c));
446 if (any(test < -kTolerance)) {
447 fEdgeEquations.fA = -dy;
448 fEdgeEquations.fB = dx;
449 fEdgeEquations.fC = -c;
450 } else {
451 fEdgeEquations.fA = dy;
452 fEdgeEquations.fB = -dx;
453 fEdgeEquations.fC = c;
454 }
455
456 fEdgeEquations.fValid = true;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400457 }
Michael Ludwigce200ac2019-11-04 14:33:32 -0500458 return fEdgeEquations;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400459}
460
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500461const TessellationHelper::OutsetRequest& TessellationHelper::getOutsetRequest(
462 const skvx::Vec<4, float>& edgeDistances) {
463 // Much of the code assumes that we start from positive distances and apply it unmodified to
464 // create an outset; knowing that it's outset simplifies degeneracy checking.
465 SkASSERT(all(edgeDistances >= 0.f));
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400466
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500467 // Rebuild outset request if invalid or if the edge distances have changed.
468 if (!fOutsetRequest.fValid || any(edgeDistances != fOutsetRequest.fEdgeDistances)) {
469 // Based on the edge distances, determine if it's acceptable to use fInvSinTheta to
470 // calculate the inset or outset geometry.
Michael Ludwigce200ac2019-11-04 14:33:32 -0500471 if (fDeviceType <= GrQuad::Type::kRectilinear) {
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500472 // Since it's rectangular, the width (edge[1] or edge[2]) collapses if subtracting
473 // (dist[0] + dist[3]) makes the new width negative (minus for inset, outsetting will
474 // never be degenerate in this case). The same applies for height (edge[0] or edge[3])
475 // and (dist[1] + dist[2]).
476 fOutsetRequest.fOutsetDegenerate = false;
477 float widthChange = edgeDistances[0] + edgeDistances[3];
478 float heightChange = edgeDistances[1] + edgeDistances[2];
479 // (1/len > 1/(edge sum) implies len - edge sum < 0.
480 fOutsetRequest.fInsetDegenerate =
481 (widthChange > 0.f && fEdgeVectors.fInvLengths[1] > 1.f / widthChange) ||
482 (heightChange > 0.f && fEdgeVectors.fInvLengths[0] > 1.f / heightChange);
Michael Ludwigce200ac2019-11-04 14:33:32 -0500483 } else if (any(fEdgeVectors.fInvLengths >= 1.f / kTolerance)) {
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500484 // Have an edge that is effectively length 0, so we're dealing with a triangle, which
485 // must always go through the degenerate code path.
486 fOutsetRequest.fOutsetDegenerate = true;
487 fOutsetRequest.fInsetDegenerate = true;
Michael Ludwig3699a172019-11-01 13:38:32 -0400488 } else {
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500489 // If possible, the corners will move +/-edgeDistances * 1/sin(theta). The entire
490 // request is degenerate if 1/sin(theta) -> infinity (or cos(theta) -> 1).
491 if (any(abs(fEdgeVectors.fCosTheta) >= 0.9f)) {
492 fOutsetRequest.fOutsetDegenerate = true;
493 fOutsetRequest.fInsetDegenerate = true;
Michael Ludwigce200ac2019-11-04 14:33:32 -0500494 } else {
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500495 // With an edge-centric view, an edge's length changes by
496 // edgeDistance * cos(pi - theta) / sin(theta) for each of its corners (the second
497 // corner uses ccw theta value). An edge's length also changes when its adjacent
498 // edges move, in which case it's updated by edgeDistance / sin(theta)
499 // (or cos(theta) for the other edge).
Michael Ludwigce200ac2019-11-04 14:33:32 -0500500
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500501 // cos(pi - theta) = -cos(theta)
502 V4f halfTanTheta = -fEdgeVectors.fCosTheta * fEdgeVectors.fInvSinTheta;
503 V4f edgeAdjust = edgeDistances * (halfTanTheta + next_ccw(halfTanTheta)) +
504 next_ccw(edgeDistances) * next_ccw(fEdgeVectors.fInvSinTheta) +
505 next_cw(edgeDistances) * fEdgeVectors.fInvSinTheta;
506
Michael Ludwigce200ac2019-11-04 14:33:32 -0500507 // If either outsetting (plus edgeAdjust) or insetting (minus edgeAdjust) make
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500508 // the edge lengths negative, then it's degenerate.
Michael Ludwigce200ac2019-11-04 14:33:32 -0500509 V4f threshold = 0.1f - (1.f / fEdgeVectors.fInvLengths);
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500510 fOutsetRequest.fOutsetDegenerate = any(edgeAdjust < threshold);
511 fOutsetRequest.fInsetDegenerate = any(edgeAdjust > -threshold);
Michael Ludwigce200ac2019-11-04 14:33:32 -0500512 }
Michael Ludwig3699a172019-11-01 13:38:32 -0400513 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400514
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500515 fOutsetRequest.fEdgeDistances = edgeDistances;
Michael Ludwigce200ac2019-11-04 14:33:32 -0500516 fOutsetRequest.fValid = true;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400517 }
Michael Ludwigce200ac2019-11-04 14:33:32 -0500518 return fOutsetRequest;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400519}
520
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400521void TessellationHelper::Vertices::moveAlong(const EdgeVectors& edgeVectors,
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500522 const V4f& signedEdgeDistances) {
523 // This shouldn't be called if fInvSinTheta is close to infinity (cosTheta close to 1).
524 SkASSERT(all(abs(edgeVectors.fCosTheta) < 0.9f));
525
526 // When the projected device quad is not degenerate, the vertex corners can move
527 // cornerOutsetLen along their edge and their cw-rotated edge. The vertex's edge points
528 // inwards and the cw-rotated edge points outwards, hence the minus-sign.
529 // The edge distances are rotated compared to the corner outsets and (dx, dy), since if
530 // the edge is "on" both its corners need to be moved along their other edge vectors.
531 V4f signedOutsets = -edgeVectors.fInvSinTheta * next_cw(signedEdgeDistances);
532 V4f signedOutsetsCW = edgeVectors.fInvSinTheta * signedEdgeDistances;
533
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400534 // x = x + outset * mask * next_cw(xdiff) - outset * next_cw(mask) * xdiff
Michael Ludwig3699a172019-11-01 13:38:32 -0400535 fX += mad(signedOutsetsCW, next_cw(edgeVectors.fDX), signedOutsets * edgeVectors.fDX);
536 fY += mad(signedOutsetsCW, next_cw(edgeVectors.fDY), signedOutsets * edgeVectors.fDY);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400537 if (fUVRCount > 0) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400538 // We want to extend the texture coords by the same proportion as the positions.
Michael Ludwig3699a172019-11-01 13:38:32 -0400539 signedOutsets *= edgeVectors.fInvLengths;
540 signedOutsetsCW *= next_cw(edgeVectors.fInvLengths);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400541 V4f du = next_ccw(fU) - fU;
542 V4f dv = next_ccw(fV) - fV;
Michael Ludwig3699a172019-11-01 13:38:32 -0400543 fU += mad(signedOutsetsCW, next_cw(du), signedOutsets * du);
544 fV += mad(signedOutsetsCW, next_cw(dv), signedOutsets * dv);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400545 if (fUVRCount == 3) {
546 V4f dr = next_ccw(fR) - fR;
Michael Ludwig3699a172019-11-01 13:38:32 -0400547 fR += mad(signedOutsetsCW, next_cw(dr), signedOutsets * dr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400548 }
549 }
550}
551
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400552void TessellationHelper::Vertices::moveTo(const V4f& x2d, const V4f& y2d, const M4f& mask) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400553 // Left to right, in device space, for each point
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400554 V4f e1x = skvx::shuffle<2, 3, 2, 3>(fX) - skvx::shuffle<0, 1, 0, 1>(fX);
555 V4f e1y = skvx::shuffle<2, 3, 2, 3>(fY) - skvx::shuffle<0, 1, 0, 1>(fY);
556 V4f e1w = skvx::shuffle<2, 3, 2, 3>(fW) - skvx::shuffle<0, 1, 0, 1>(fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400557 correct_bad_edges(mad(e1x, e1x, e1y * e1y) < kTolerance * kTolerance, &e1x, &e1y, &e1w);
558
559 // // Top to bottom, in device space, for each point
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400560 V4f e2x = skvx::shuffle<1, 1, 3, 3>(fX) - skvx::shuffle<0, 0, 2, 2>(fX);
561 V4f e2y = skvx::shuffle<1, 1, 3, 3>(fY) - skvx::shuffle<0, 0, 2, 2>(fY);
562 V4f e2w = skvx::shuffle<1, 1, 3, 3>(fW) - skvx::shuffle<0, 0, 2, 2>(fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400563 correct_bad_edges(mad(e2x, e2x, e2y * e2y) < kTolerance * kTolerance, &e2x, &e2y, &e2w);
564
565 // Can only move along e1 and e2 to reach the new 2D point, so we have
566 // x2d = (x + a*e1x + b*e2x) / (w + a*e1w + b*e2w) and
567 // y2d = (y + a*e1y + b*e2y) / (w + a*e1w + b*e2w) for some a, b
568 // This can be rewritten to a*c1x + b*c2x + c3x = 0; a * c1y + b*c2y + c3y = 0, where
569 // the cNx and cNy coefficients are:
570 V4f c1x = e1w * x2d - e1x;
571 V4f c1y = e1w * y2d - e1y;
572 V4f c2x = e2w * x2d - e2x;
573 V4f c2y = e2w * y2d - e2y;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400574 V4f c3x = fW * x2d - fX;
575 V4f c3y = fW * y2d - fY;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400576
577 // Solve for a and b
578 V4f a, b, denom;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400579 if (all(mask)) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400580 // When every edge is outset/inset, each corner can use both edge vectors
581 denom = c1x * c2y - c2x * c1y;
582 a = (c2x * c3y - c3x * c2y) / denom;
583 b = (c3x * c1y - c1x * c3y) / denom;
584 } else {
585 // Force a or b to be 0 if that edge cannot be used due to non-AA
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400586 M4f aMask = skvx::shuffle<0, 0, 3, 3>(mask);
587 M4f bMask = skvx::shuffle<2, 1, 2, 1>(mask);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400588
589 // When aMask[i]&bMask[i], then a[i], b[i], denom[i] match the kAll case.
590 // When aMask[i]&!bMask[i], then b[i] = 0, a[i] = -c3x/c1x or -c3y/c1y, using better denom
591 // When !aMask[i]&bMask[i], then a[i] = 0, b[i] = -c3x/c2x or -c3y/c2y, ""
592 // When !aMask[i]&!bMask[i], then both a[i] = 0 and b[i] = 0
593 M4f useC1x = abs(c1x) > abs(c1y);
594 M4f useC2x = abs(c2x) > abs(c2y);
595
596 denom = if_then_else(aMask,
597 if_then_else(bMask,
598 c1x * c2y - c2x * c1y, /* A & B */
599 if_then_else(useC1x, c1x, c1y)), /* A & !B */
600 if_then_else(bMask,
601 if_then_else(useC2x, c2x, c2y), /* !A & B */
602 V4f(1.f))); /* !A & !B */
603
604 a = if_then_else(aMask,
605 if_then_else(bMask,
606 c2x * c3y - c3x * c2y, /* A & B */
607 if_then_else(useC1x, -c3x, -c3y)), /* A & !B */
608 V4f(0.f)) / denom; /* !A */
609 b = if_then_else(bMask,
610 if_then_else(aMask,
611 c3x * c1y - c1x * c3y, /* A & B */
612 if_then_else(useC2x, -c3x, -c3y)), /* !A & B */
613 V4f(0.f)) / denom; /* !B */
614 }
615
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400616 V4f newW = fW + a * e1w + b * e2w;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400617 // If newW < 0, scale a and b such that the point reaches the infinity plane instead of crossing
618 // This breaks orthogonality of inset/outsets, but GPUs don't handle negative Ws well so this
619 // is far less visually disturbing (likely not noticeable since it's at extreme perspective).
620 // The alternative correction (multiply xyw by -1) has the disadvantage of changing how local
621 // coordinates would be interpolated.
622 static const float kMinW = 1e-6f;
623 if (any(newW < 0.f)) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400624 V4f scale = if_then_else(newW < kMinW, (kMinW - fW) / (newW - fW), V4f(1.f));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400625 a *= scale;
626 b *= scale;
627 }
628
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400629 fX += a * e1x + b * e2x;
630 fY += a * e1y + b * e2y;
631 fW += a * e1w + b * e2w;
632 correct_bad_coords(abs(denom) < kTolerance, &fX, &fY, &fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400633
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400634 if (fUVRCount > 0) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400635 // Calculate R here so it can be corrected with U and V in case it's needed later
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400636 V4f e1u = skvx::shuffle<2, 3, 2, 3>(fU) - skvx::shuffle<0, 1, 0, 1>(fU);
637 V4f e1v = skvx::shuffle<2, 3, 2, 3>(fV) - skvx::shuffle<0, 1, 0, 1>(fV);
638 V4f e1r = skvx::shuffle<2, 3, 2, 3>(fR) - skvx::shuffle<0, 1, 0, 1>(fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400639 correct_bad_edges(mad(e1u, e1u, e1v * e1v) < kTolerance * kTolerance, &e1u, &e1v, &e1r);
640
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400641 V4f e2u = skvx::shuffle<1, 1, 3, 3>(fU) - skvx::shuffle<0, 0, 2, 2>(fU);
642 V4f e2v = skvx::shuffle<1, 1, 3, 3>(fV) - skvx::shuffle<0, 0, 2, 2>(fV);
643 V4f e2r = skvx::shuffle<1, 1, 3, 3>(fR) - skvx::shuffle<0, 0, 2, 2>(fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400644 correct_bad_edges(mad(e2u, e2u, e2v * e2v) < kTolerance * kTolerance, &e2u, &e2v, &e2r);
645
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400646 fU += a * e1u + b * e2u;
647 fV += a * e1v + b * e2v;
648 if (fUVRCount == 3) {
649 fR += a * e1r + b * e2r;
650 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, &fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400651 } else {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400652 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400653 }
654 }
655}
656
Michael Ludwig45a22812019-11-04 11:15:30 -0500657V4f TessellationHelper::EdgeEquations::estimateCoverage(const V4f& x2d, const V4f& y2d) const {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400658 // Calculate distance of the 4 inset points (px, py) to the 4 edges
Michael Ludwig45a22812019-11-04 11:15:30 -0500659 V4f d0 = mad(fA[0], x2d, mad(fB[0], y2d, fC[0]));
660 V4f d1 = mad(fA[1], x2d, mad(fB[1], y2d, fC[1]));
661 V4f d2 = mad(fA[2], x2d, mad(fB[2], y2d, fC[2]));
662 V4f d3 = mad(fA[3], x2d, mad(fB[3], y2d, fC[3]));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400663
664 // For each point, pretend that there's a rectangle that touches e0 and e3 on the horizontal
665 // axis, so its width is "approximately" d0 + d3, and it touches e1 and e2 on the vertical axis
666 // so its height is d1 + d2. Pin each of these dimensions to [0, 1] and approximate the coverage
667 // at each point as clamp(d0+d3, 0, 1) x clamp(d1+d2, 0, 1). For rectilinear quads this is an
668 // accurate calculation of its area clipped to an aligned pixel. For arbitrary quads it is not
669 // mathematically accurate but qualitatively provides a stable value proportional to the size of
670 // the shape.
671 V4f w = max(0.f, min(1.f, d0 + d3));
672 V4f h = max(0.f, min(1.f, d1 + d2));
673 return w * h;
674}
675
Michael Ludwigce200ac2019-11-04 14:33:32 -0500676int TessellationHelper::computeDegenerateQuad(const V4f& signedEdgeDistances, V4f* x2d, V4f* y2d) {
Michael Ludwig3699a172019-11-01 13:38:32 -0400677 // Move the edge by the signed edge adjustment.
Michael Ludwigce200ac2019-11-04 14:33:32 -0500678 const EdgeEquations& edges = this->getEdgeEquations();
Michael Ludwig3699a172019-11-01 13:38:32 -0400679 V4f oc = edges.fC + signedEdgeDistances;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400680
681 // There are 6 points that we care about to determine the final shape of the polygon, which
682 // are the intersections between (e0,e2), (e1,e0), (e2,e3), (e3,e1) (corresponding to the
683 // 4 corners), and (e1, e2), (e0, e3) (representing the intersections of opposite edges).
684 V4f denom = edges.fA * next_cw(edges.fB) - edges.fB * next_cw(edges.fA);
685 V4f px = (edges.fB * next_cw(oc) - oc * next_cw(edges.fB)) / denom;
686 V4f py = (oc * next_cw(edges.fA) - edges.fA * next_cw(oc)) / denom;
687 correct_bad_coords(abs(denom) < kTolerance, &px, &py, nullptr);
688
689 // Calculate the signed distances from these 4 corners to the other two edges that did not
690 // define the intersection. So p(0) is compared to e3,e1, p(1) to e3,e2 , p(2) to e0,e1, and
691 // p(3) to e0,e2
692 V4f dists1 = px * skvx::shuffle<3, 3, 0, 0>(edges.fA) +
693 py * skvx::shuffle<3, 3, 0, 0>(edges.fB) +
694 skvx::shuffle<3, 3, 0, 0>(oc);
695 V4f dists2 = px * skvx::shuffle<1, 2, 1, 2>(edges.fA) +
696 py * skvx::shuffle<1, 2, 1, 2>(edges.fB) +
697 skvx::shuffle<1, 2, 1, 2>(oc);
698
699 // If all the distances are >= 0, the 4 corners form a valid quadrilateral, so use them as
700 // the 4 points. If any point is on the wrong side of both edges, the interior has collapsed
701 // and we need to use a central point to represent it. If all four points are only on the
702 // wrong side of 1 edge, one edge has crossed over another and we use a line to represent it.
703 // Otherwise, use a triangle that replaces the bad points with the intersections of
704 // (e1, e2) or (e0, e3) as needed.
705 M4f d1v0 = dists1 < kTolerance;
706 M4f d2v0 = dists2 < kTolerance;
707 M4f d1And2 = d1v0 & d2v0;
708 M4f d1Or2 = d1v0 | d2v0;
709
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400710 if (!any(d1Or2)) {
711 // Every dists1 and dists2 >= kTolerance so it's not degenerate, use all 4 corners as-is
712 // and use full coverage
Michael Ludwig45a22812019-11-04 11:15:30 -0500713 *x2d = px;
714 *y2d = py;
715 return 4;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400716 } else if (any(d1And2)) {
717 // A point failed against two edges, so reduce the shape to a single point, which we take as
718 // the center of the original quad to ensure it is contained in the intended geometry. Since
719 // it has collapsed, we know the shape cannot cover a pixel so update the coverage.
Michael Ludwigba971892019-11-04 11:11:55 -0500720 SkPoint center = {0.25f * ((*x2d)[0] + (*x2d)[1] + (*x2d)[2] + (*x2d)[3]),
721 0.25f * ((*y2d)[0] + (*y2d)[1] + (*y2d)[2] + (*y2d)[3])};
Michael Ludwig45a22812019-11-04 11:15:30 -0500722 *x2d = center.fX;
723 *y2d = center.fY;
724 return 1;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400725 } else if (all(d1Or2)) {
726 // Degenerates to a line. Compare p[2] and p[3] to edge 0. If they are on the wrong side,
727 // that means edge 0 and 3 crossed, and otherwise edge 1 and 2 crossed.
728 if (dists1[2] < kTolerance && dists1[3] < kTolerance) {
729 // Edges 0 and 3 have crossed over, so make the line from average of (p0,p2) and (p1,p3)
Michael Ludwig45a22812019-11-04 11:15:30 -0500730 *x2d = 0.5f * (skvx::shuffle<0, 1, 0, 1>(px) + skvx::shuffle<2, 3, 2, 3>(px));
731 *y2d = 0.5f * (skvx::shuffle<0, 1, 0, 1>(py) + skvx::shuffle<2, 3, 2, 3>(py));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400732 } else {
733 // Edges 1 and 2 have crossed over, so make the line from average of (p0,p1) and (p2,p3)
Michael Ludwig45a22812019-11-04 11:15:30 -0500734 *x2d = 0.5f * (skvx::shuffle<0, 0, 2, 2>(px) + skvx::shuffle<1, 1, 3, 3>(px));
735 *y2d = 0.5f * (skvx::shuffle<0, 0, 2, 2>(py) + skvx::shuffle<1, 1, 3, 3>(py));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400736 }
Michael Ludwig45a22812019-11-04 11:15:30 -0500737 return 2;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400738 } else {
739 // This turns into a triangle. Replace corners as needed with the intersections between
740 // (e0,e3) and (e1,e2), which must now be calculated
741 using V2f = skvx::Vec<2, float>;
742 V2f eDenom = skvx::shuffle<0, 1>(edges.fA) * skvx::shuffle<3, 2>(edges.fB) -
743 skvx::shuffle<0, 1>(edges.fB) * skvx::shuffle<3, 2>(edges.fA);
744 V2f ex = (skvx::shuffle<0, 1>(edges.fB) * skvx::shuffle<3, 2>(oc) -
745 skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(edges.fB)) / eDenom;
746 V2f ey = (skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(edges.fA) -
747 skvx::shuffle<0, 1>(edges.fA) * skvx::shuffle<3, 2>(oc)) / eDenom;
748
749 if (SkScalarAbs(eDenom[0]) > kTolerance) {
750 px = if_then_else(d1v0, V4f(ex[0]), px);
751 py = if_then_else(d1v0, V4f(ey[0]), py);
752 }
753 if (SkScalarAbs(eDenom[1]) > kTolerance) {
754 px = if_then_else(d2v0, V4f(ex[1]), px);
755 py = if_then_else(d2v0, V4f(ey[1]), py);
756 }
757
Michael Ludwig45a22812019-11-04 11:15:30 -0500758 *x2d = px;
759 *y2d = py;
760 return 3;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400761 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400762}
763
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500764int TessellationHelper::adjustVertices(const skvx::Vec<4, float>& edgeDistances, bool inset,
765 Vertices* vertices) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400766 SkASSERT(vertices);
767 SkASSERT(vertices->fUVRCount == 0 || vertices->fUVRCount == 2 || vertices->fUVRCount == 3);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400768
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500769 const OutsetRequest& outsetRequest = this->getOutsetRequest(edgeDistances);
770 // Insets are more likely to become degenerate than outsets, so this allows us to compute the
771 // outer geometry with the fast path and the inner geometry with a slow path if possible.
772 bool degenerate = inset ? outsetRequest.fInsetDegenerate : outsetRequest.fOutsetDegenerate;
773 V4f signedEdgeDistances = outsetRequest.fEdgeDistances;
774 if (inset) {
775 signedEdgeDistances *= -1.f;
776 }
777
778 if (fDeviceType == GrQuad::Type::kPerspective || degenerate) {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500779 Vertices projected = { fEdgeVectors.fX2D, fEdgeVectors.fY2D, /*w*/ 1.f, 0.f, 0.f, 0.f, 0};
Michael Ludwig45a22812019-11-04 11:15:30 -0500780 int vertexCount;
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500781 if (degenerate) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400782 // Must use the slow path to handle numerical issues and self intersecting geometry
Michael Ludwigce200ac2019-11-04 14:33:32 -0500783 vertexCount = computeDegenerateQuad(signedEdgeDistances, &projected.fX, &projected.fY);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400784 } else {
785 // Move the projected quad with the fast path, even though we will reconstruct the
786 // perspective corners afterwards.
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500787 projected.moveAlong(fEdgeVectors, signedEdgeDistances);
Michael Ludwig45a22812019-11-04 11:15:30 -0500788 vertexCount = 4;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400789 }
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500790 vertices->moveTo(projected.fX, projected.fY, signedEdgeDistances != 0.f);
Michael Ludwig45a22812019-11-04 11:15:30 -0500791 return vertexCount;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400792 } else {
793 // Quad is 2D and the inset/outset request does not cause the geometry to self intersect, so
794 // we can directly move the corners along the already calculated edge vectors.
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500795 vertices->moveAlong(fEdgeVectors, signedEdgeDistances);
Michael Ludwig45a22812019-11-04 11:15:30 -0500796 return 4;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400797 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400798}
799
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500800V4f TessellationHelper::inset(const skvx::Vec<4, float>& edgeDistances,
801 GrQuad* deviceInset, GrQuad* localInset) {
802 Vertices inset = fOriginal;
803 int vertexCount = this->adjustVertices(edgeDistances, true, &inset);
804 this->setQuads(inset, deviceInset, localInset);
805
806 if (vertexCount < 3) {
807 // The interior has less than a full pixel's area so estimate reduced coverage using
808 // the distance of the inset's projected corners to the original edges.
809 return this->getEdgeEquations().estimateCoverage(inset.fX / inset.fW,
810 inset.fY / inset.fW);
811 } else {
Michael Ludwig45a22812019-11-04 11:15:30 -0500812 return 1.f;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400813 }
814}
815
Michael Ludwigd84dd4b2019-11-05 12:03:12 -0500816void TessellationHelper::outset(const skvx::Vec<4, float>& edgeDistances,
817 GrQuad* deviceOutset, GrQuad* localOutset) {
818 Vertices outset = fOriginal;
819 this->adjustVertices(edgeDistances, false, &outset);
820 this->setQuads(outset, deviceOutset, localOutset);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400821}
822
823void TessellationHelper::setQuads(const Vertices& vertices,
824 GrQuad* deviceOut, GrQuad* localOut) const {
825 SkASSERT(deviceOut);
826 SkASSERT(vertices.fUVRCount == 0 || localOut);
827
828 vertices.fX.store(deviceOut->xs());
829 vertices.fY.store(deviceOut->ys());
830 if (fDeviceType == GrQuad::Type::kPerspective) {
831 vertices.fW.store(deviceOut->ws());
832 }
833 deviceOut->setQuadType(fDeviceType); // This sets ws == 1 when device type != perspective
834
835 if (vertices.fUVRCount > 0) {
836 vertices.fU.store(localOut->xs());
837 vertices.fV.store(localOut->ys());
838 if (vertices.fUVRCount == 3) {
839 vertices.fR.store(localOut->ws());
840 }
841 localOut->setQuadType(fLocalType);
842 }
843}
844
Michael Ludwig0f809022019-06-04 09:14:37 -0400845}; // namespace GrQuadUtils