blob: b525e5171d0319471c96ef07ac0ec3d1643d5633 [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 Ludwige64c8bf2019-11-01 13:29:44 -0400388TessellationHelper::EdgeVectors TessellationHelper::getEdgeVectors() const {
389 EdgeVectors v;
390 if (fDeviceType == GrQuad::Type::kPerspective) {
391 V4f iw = 1.0 / fOriginal.fW;
392 v.fX2D = fOriginal.fX * iw;
393 v.fY2D = fOriginal.fY * iw;
394 } else {
395 v.fX2D = fOriginal.fX;
396 v.fY2D = fOriginal.fY;
397 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400398
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400399 v.fDX = next_ccw(v.fX2D) - v.fX2D;
400 v.fDY = next_ccw(v.fY2D) - v.fY2D;
401 v.fInvLengths = rsqrt(mad(v.fDX, v.fDX, v.fDY * v.fDY));
402
403 // Normalize edge vectors
404 v.fDX *= v.fInvLengths;
405 v.fDY *= v.fInvLengths;
406 return v;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400407}
408
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400409TessellationHelper::EdgeEquations TessellationHelper::getEdgeEquations(
410 const EdgeVectors& edgeVectors) const {
411 V4f dx = edgeVectors.fDX;
412 V4f dy = edgeVectors.fDY;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400413 // Correct for bad edges by copying adjacent edge information into the bad component
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400414 correct_bad_edges(edgeVectors.fInvLengths >= 1.f / kTolerance, &dx, &dy, nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400415
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400416 V4f c = mad(dx, edgeVectors.fY2D, -dy * edgeVectors.fX2D);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400417 // Make sure normals point into the shape
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400418 V4f test = mad(dy, next_cw(edgeVectors.fX2D), mad(-dx, next_cw(edgeVectors.fY2D), c));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400419 if (any(test < -kTolerance)) {
420 return {-dy, dx, -c, true};
421 } else {
422 return {dy, -dx, c, false};
423 }
424}
425
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400426TessellationHelper::OutsetRequest TessellationHelper::getOutsetRequest(
427 const EdgeVectors& edgeVectors) const {
428 OutsetRequest r;
429 r.fOutsets = 0.5f; // Half a pixel for AA
430 r.fMask = fAAFlags == GrQuadAAFlags::kAll ? V4f(1.f) :
431 V4f{(GrQuadAAFlags::kLeft & fAAFlags) ? 1.f : 0.f,
432 (GrQuadAAFlags::kBottom & fAAFlags) ? 1.f : 0.f,
433 (GrQuadAAFlags::kTop & fAAFlags) ? 1.f : 0.f,
434 (GrQuadAAFlags::kRight & fAAFlags) ? 1.f : 0.f};
435
436 if (fDeviceType <= GrQuad::Type::kRectilinear) {
437 // While it's still rectangular, must use the degenerate path when the quad is less
438 // than a pixel along a side since the coverage must be updated. (len < 1 implies 1/len > 1)
439 r.fDegenerate = any(edgeVectors.fInvLengths > 1.f);
440 return r;
441 } else if (any(edgeVectors.fInvLengths >= 1.f / kTolerance)) {
442 // Have an edge that is effectively length 0, so we're dealing with a triangle. Skip
443 // computing corner outsets, since degenerate path won't use them.
444 r.fDegenerate = true;
445 return r;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400446 }
447
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400448 // Must scale corner distance by 1/2sin(theta), where theta is the angle between the two
449 // edges at that corner. cos(theta) is equal to dot(dXY, next_cw(dXY)),
450 // and sin(theta) = sqrt(1 - cos(theta)^2)
451 V4f cosTheta = mad(edgeVectors.fDX, next_cw(edgeVectors.fDX),
452 edgeVectors.fDY * next_cw(edgeVectors.fDY));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400453
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400454 // If the angle is too shallow between edges, go through the degenerate path, otherwise
455 // adding and subtracting very large vectors in almost opposite directions leads to float
456 // errors (and skip updating the outsets since degenerate code path doesn't rely on that).
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400457 if (any(abs(cosTheta) >= 0.9f)) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400458 r.fDegenerate = true;
459 return r;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400460 }
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400461
462 r.fOutsets *= rsqrt(1.f - cosTheta * cosTheta); // 1/2sin(theta)
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400463
464 // When outsetting or insetting, the current edge's AA adds to the length:
465 // cos(pi - theta)/2sin(theta) + cos(pi-ccw(theta))/2sin(ccw(theta))
466 // Moving an adjacent edge updates the length by 1/2sin(theta|ccw(theta))
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400467 V4f halfTanTheta = -cosTheta * r.fOutsets; // cos(pi - theta) = -cos(theta)
468 V4f edgeAdjust = r.fMask * (halfTanTheta + next_ccw(halfTanTheta)) +
469 next_ccw(r.fMask) * next_ccw(r.fOutsets) +
470 next_cw(r.fMask) * r.fOutsets;
471 // If either outsetting (plus edgeAdjust) or insetting (minus edgeAdjust) make edgeLen
472 // negative then it's degenerate
473 V4f threshold = 0.1f - (1.f / edgeVectors.fInvLengths);
474 r.fDegenerate = any(edgeAdjust < threshold) || any(edgeAdjust > -threshold);
475 return r;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400476}
477
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400478void TessellationHelper::Vertices::moveAlong(const EdgeVectors& edgeVectors,
479 const V4f& signedOutsets,
480 const V4f& mask) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400481 // The mask is rotated compared to the outsets and edge vectors, since if the edge is "on"
482 // both its points need to be moved along their other edge vectors.
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400483 auto maskedOutset = -signedOutsets * next_cw(mask);
484 auto maskedOutsetCW = signedOutsets * mask;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400485 // x = x + outset * mask * next_cw(xdiff) - outset * next_cw(mask) * xdiff
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400486 fX += mad(maskedOutsetCW, next_cw(edgeVectors.fDX), maskedOutset * edgeVectors.fDX);
487 fY += mad(maskedOutsetCW, next_cw(edgeVectors.fDY), maskedOutset * edgeVectors.fDY);
488 if (fUVRCount > 0) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400489 // We want to extend the texture coords by the same proportion as the positions.
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400490 maskedOutset *= edgeVectors.fInvLengths;
491 maskedOutsetCW *= next_cw(edgeVectors.fInvLengths);
492 V4f du = next_ccw(fU) - fU;
493 V4f dv = next_ccw(fV) - fV;
494 fU += mad(maskedOutsetCW, next_cw(du), maskedOutset * du);
495 fV += mad(maskedOutsetCW, next_cw(dv), maskedOutset * dv);
496 if (fUVRCount == 3) {
497 V4f dr = next_ccw(fR) - fR;
498 fR += mad(maskedOutsetCW, next_cw(dr), maskedOutset * dr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400499 }
500 }
501}
502
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400503void TessellationHelper::Vertices::moveTo(const V4f& x2d, const V4f& y2d, const M4f& mask) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400504 // Left to right, in device space, for each point
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400505 V4f e1x = skvx::shuffle<2, 3, 2, 3>(fX) - skvx::shuffle<0, 1, 0, 1>(fX);
506 V4f e1y = skvx::shuffle<2, 3, 2, 3>(fY) - skvx::shuffle<0, 1, 0, 1>(fY);
507 V4f e1w = skvx::shuffle<2, 3, 2, 3>(fW) - skvx::shuffle<0, 1, 0, 1>(fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400508 correct_bad_edges(mad(e1x, e1x, e1y * e1y) < kTolerance * kTolerance, &e1x, &e1y, &e1w);
509
510 // // Top to bottom, in device space, for each point
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400511 V4f e2x = skvx::shuffle<1, 1, 3, 3>(fX) - skvx::shuffle<0, 0, 2, 2>(fX);
512 V4f e2y = skvx::shuffle<1, 1, 3, 3>(fY) - skvx::shuffle<0, 0, 2, 2>(fY);
513 V4f e2w = skvx::shuffle<1, 1, 3, 3>(fW) - skvx::shuffle<0, 0, 2, 2>(fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400514 correct_bad_edges(mad(e2x, e2x, e2y * e2y) < kTolerance * kTolerance, &e2x, &e2y, &e2w);
515
516 // Can only move along e1 and e2 to reach the new 2D point, so we have
517 // x2d = (x + a*e1x + b*e2x) / (w + a*e1w + b*e2w) and
518 // y2d = (y + a*e1y + b*e2y) / (w + a*e1w + b*e2w) for some a, b
519 // This can be rewritten to a*c1x + b*c2x + c3x = 0; a * c1y + b*c2y + c3y = 0, where
520 // the cNx and cNy coefficients are:
521 V4f c1x = e1w * x2d - e1x;
522 V4f c1y = e1w * y2d - e1y;
523 V4f c2x = e2w * x2d - e2x;
524 V4f c2y = e2w * y2d - e2y;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400525 V4f c3x = fW * x2d - fX;
526 V4f c3y = fW * y2d - fY;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400527
528 // Solve for a and b
529 V4f a, b, denom;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400530 if (all(mask)) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400531 // When every edge is outset/inset, each corner can use both edge vectors
532 denom = c1x * c2y - c2x * c1y;
533 a = (c2x * c3y - c3x * c2y) / denom;
534 b = (c3x * c1y - c1x * c3y) / denom;
535 } else {
536 // Force a or b to be 0 if that edge cannot be used due to non-AA
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400537 M4f aMask = skvx::shuffle<0, 0, 3, 3>(mask);
538 M4f bMask = skvx::shuffle<2, 1, 2, 1>(mask);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400539
540 // When aMask[i]&bMask[i], then a[i], b[i], denom[i] match the kAll case.
541 // When aMask[i]&!bMask[i], then b[i] = 0, a[i] = -c3x/c1x or -c3y/c1y, using better denom
542 // When !aMask[i]&bMask[i], then a[i] = 0, b[i] = -c3x/c2x or -c3y/c2y, ""
543 // When !aMask[i]&!bMask[i], then both a[i] = 0 and b[i] = 0
544 M4f useC1x = abs(c1x) > abs(c1y);
545 M4f useC2x = abs(c2x) > abs(c2y);
546
547 denom = if_then_else(aMask,
548 if_then_else(bMask,
549 c1x * c2y - c2x * c1y, /* A & B */
550 if_then_else(useC1x, c1x, c1y)), /* A & !B */
551 if_then_else(bMask,
552 if_then_else(useC2x, c2x, c2y), /* !A & B */
553 V4f(1.f))); /* !A & !B */
554
555 a = if_then_else(aMask,
556 if_then_else(bMask,
557 c2x * c3y - c3x * c2y, /* A & B */
558 if_then_else(useC1x, -c3x, -c3y)), /* A & !B */
559 V4f(0.f)) / denom; /* !A */
560 b = if_then_else(bMask,
561 if_then_else(aMask,
562 c3x * c1y - c1x * c3y, /* A & B */
563 if_then_else(useC2x, -c3x, -c3y)), /* !A & B */
564 V4f(0.f)) / denom; /* !B */
565 }
566
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400567 V4f newW = fW + a * e1w + b * e2w;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400568 // If newW < 0, scale a and b such that the point reaches the infinity plane instead of crossing
569 // This breaks orthogonality of inset/outsets, but GPUs don't handle negative Ws well so this
570 // is far less visually disturbing (likely not noticeable since it's at extreme perspective).
571 // The alternative correction (multiply xyw by -1) has the disadvantage of changing how local
572 // coordinates would be interpolated.
573 static const float kMinW = 1e-6f;
574 if (any(newW < 0.f)) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400575 V4f scale = if_then_else(newW < kMinW, (kMinW - fW) / (newW - fW), V4f(1.f));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400576 a *= scale;
577 b *= scale;
578 }
579
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400580 fX += a * e1x + b * e2x;
581 fY += a * e1y + b * e2y;
582 fW += a * e1w + b * e2w;
583 correct_bad_coords(abs(denom) < kTolerance, &fX, &fY, &fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400584
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400585 if (fUVRCount > 0) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400586 // 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 -0400587 V4f e1u = skvx::shuffle<2, 3, 2, 3>(fU) - skvx::shuffle<0, 1, 0, 1>(fU);
588 V4f e1v = skvx::shuffle<2, 3, 2, 3>(fV) - skvx::shuffle<0, 1, 0, 1>(fV);
589 V4f e1r = skvx::shuffle<2, 3, 2, 3>(fR) - skvx::shuffle<0, 1, 0, 1>(fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400590 correct_bad_edges(mad(e1u, e1u, e1v * e1v) < kTolerance * kTolerance, &e1u, &e1v, &e1r);
591
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400592 V4f e2u = skvx::shuffle<1, 1, 3, 3>(fU) - skvx::shuffle<0, 0, 2, 2>(fU);
593 V4f e2v = skvx::shuffle<1, 1, 3, 3>(fV) - skvx::shuffle<0, 0, 2, 2>(fV);
594 V4f e2r = skvx::shuffle<1, 1, 3, 3>(fR) - skvx::shuffle<0, 0, 2, 2>(fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400595 correct_bad_edges(mad(e2u, e2u, e2v * e2v) < kTolerance * kTolerance, &e2u, &e2v, &e2r);
596
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400597 fU += a * e1u + b * e2u;
598 fV += a * e1v + b * e2v;
599 if (fUVRCount == 3) {
600 fR += a * e1r + b * e2r;
601 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, &fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400602 } else {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400603 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400604 }
605 }
606}
607
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400608V4f TessellationHelper::getDegenerateCoverage(const V4f& px, const V4f& py,
609 const EdgeEquations& edges) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400610 // Calculate distance of the 4 inset points (px, py) to the 4 edges
611 V4f d0 = mad(edges.fA[0], px, mad(edges.fB[0], py, edges.fC[0]));
612 V4f d1 = mad(edges.fA[1], px, mad(edges.fB[1], py, edges.fC[1]));
613 V4f d2 = mad(edges.fA[2], px, mad(edges.fB[2], py, edges.fC[2]));
614 V4f d3 = mad(edges.fA[3], px, mad(edges.fB[3], py, edges.fC[3]));
615
616 // For each point, pretend that there's a rectangle that touches e0 and e3 on the horizontal
617 // axis, so its width is "approximately" d0 + d3, and it touches e1 and e2 on the vertical axis
618 // so its height is d1 + d2. Pin each of these dimensions to [0, 1] and approximate the coverage
619 // at each point as clamp(d0+d3, 0, 1) x clamp(d1+d2, 0, 1). For rectilinear quads this is an
620 // accurate calculation of its area clipped to an aligned pixel. For arbitrary quads it is not
621 // mathematically accurate but qualitatively provides a stable value proportional to the size of
622 // the shape.
623 V4f w = max(0.f, min(1.f, d0 + d3));
624 V4f h = max(0.f, min(1.f, d1 + d2));
625 return w * h;
626}
627
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400628V4f TessellationHelper::computeDegenerateQuad(const V4f& signedEdgeDistances, const V4f& mask,
629 const EdgeEquations& edges, Vertices* quad) {
630 // Move the edge by the signed edge adjustment, respecting mask.
631 V4f oc = edges.fC + mask * signedEdgeDistances;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400632
633 // There are 6 points that we care about to determine the final shape of the polygon, which
634 // are the intersections between (e0,e2), (e1,e0), (e2,e3), (e3,e1) (corresponding to the
635 // 4 corners), and (e1, e2), (e0, e3) (representing the intersections of opposite edges).
636 V4f denom = edges.fA * next_cw(edges.fB) - edges.fB * next_cw(edges.fA);
637 V4f px = (edges.fB * next_cw(oc) - oc * next_cw(edges.fB)) / denom;
638 V4f py = (oc * next_cw(edges.fA) - edges.fA * next_cw(oc)) / denom;
639 correct_bad_coords(abs(denom) < kTolerance, &px, &py, nullptr);
640
641 // Calculate the signed distances from these 4 corners to the other two edges that did not
642 // define the intersection. So p(0) is compared to e3,e1, p(1) to e3,e2 , p(2) to e0,e1, and
643 // p(3) to e0,e2
644 V4f dists1 = px * skvx::shuffle<3, 3, 0, 0>(edges.fA) +
645 py * skvx::shuffle<3, 3, 0, 0>(edges.fB) +
646 skvx::shuffle<3, 3, 0, 0>(oc);
647 V4f dists2 = px * skvx::shuffle<1, 2, 1, 2>(edges.fA) +
648 py * skvx::shuffle<1, 2, 1, 2>(edges.fB) +
649 skvx::shuffle<1, 2, 1, 2>(oc);
650
651 // If all the distances are >= 0, the 4 corners form a valid quadrilateral, so use them as
652 // the 4 points. If any point is on the wrong side of both edges, the interior has collapsed
653 // and we need to use a central point to represent it. If all four points are only on the
654 // wrong side of 1 edge, one edge has crossed over another and we use a line to represent it.
655 // Otherwise, use a triangle that replaces the bad points with the intersections of
656 // (e1, e2) or (e0, e3) as needed.
657 M4f d1v0 = dists1 < kTolerance;
658 M4f d2v0 = dists2 < kTolerance;
659 M4f d1And2 = d1v0 & d2v0;
660 M4f d1Or2 = d1v0 | d2v0;
661
662 V4f coverage;
663 if (!any(d1Or2)) {
664 // Every dists1 and dists2 >= kTolerance so it's not degenerate, use all 4 corners as-is
665 // and use full coverage
666 coverage = 1.f;
667 } else if (any(d1And2)) {
668 // A point failed against two edges, so reduce the shape to a single point, which we take as
669 // the center of the original quad to ensure it is contained in the intended geometry. Since
670 // it has collapsed, we know the shape cannot cover a pixel so update the coverage.
671 SkPoint center = {0.25f * (quad->fX[0] + quad->fX[1] + quad->fX[2] + quad->fX[3]),
672 0.25f * (quad->fY[0] + quad->fY[1] + quad->fY[2] + quad->fY[3])};
673 px = center.fX;
674 py = center.fY;
675 coverage = getDegenerateCoverage(px, py, edges);
676 } else if (all(d1Or2)) {
677 // Degenerates to a line. Compare p[2] and p[3] to edge 0. If they are on the wrong side,
678 // that means edge 0 and 3 crossed, and otherwise edge 1 and 2 crossed.
679 if (dists1[2] < kTolerance && dists1[3] < kTolerance) {
680 // Edges 0 and 3 have crossed over, so make the line from average of (p0,p2) and (p1,p3)
681 px = 0.5f * (skvx::shuffle<0, 1, 0, 1>(px) + skvx::shuffle<2, 3, 2, 3>(px));
682 py = 0.5f * (skvx::shuffle<0, 1, 0, 1>(py) + skvx::shuffle<2, 3, 2, 3>(py));
683 } else {
684 // Edges 1 and 2 have crossed over, so make the line from average of (p0,p1) and (p2,p3)
685 px = 0.5f * (skvx::shuffle<0, 0, 2, 2>(px) + skvx::shuffle<1, 1, 3, 3>(px));
686 py = 0.5f * (skvx::shuffle<0, 0, 2, 2>(py) + skvx::shuffle<1, 1, 3, 3>(py));
687 }
688 coverage = getDegenerateCoverage(px, py, edges);
689 } else {
690 // This turns into a triangle. Replace corners as needed with the intersections between
691 // (e0,e3) and (e1,e2), which must now be calculated
692 using V2f = skvx::Vec<2, float>;
693 V2f eDenom = skvx::shuffle<0, 1>(edges.fA) * skvx::shuffle<3, 2>(edges.fB) -
694 skvx::shuffle<0, 1>(edges.fB) * skvx::shuffle<3, 2>(edges.fA);
695 V2f ex = (skvx::shuffle<0, 1>(edges.fB) * skvx::shuffle<3, 2>(oc) -
696 skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(edges.fB)) / eDenom;
697 V2f ey = (skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(edges.fA) -
698 skvx::shuffle<0, 1>(edges.fA) * skvx::shuffle<3, 2>(oc)) / eDenom;
699
700 if (SkScalarAbs(eDenom[0]) > kTolerance) {
701 px = if_then_else(d1v0, V4f(ex[0]), px);
702 py = if_then_else(d1v0, V4f(ey[0]), py);
703 }
704 if (SkScalarAbs(eDenom[1]) > kTolerance) {
705 px = if_then_else(d2v0, V4f(ex[1]), px);
706 py = if_then_else(d2v0, V4f(ey[1]), py);
707 }
708
709 coverage = 1.f;
710 }
711
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400712 quad->moveTo(px, py, mask != 0.f);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400713 return coverage;
714}
715
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400716V4f TessellationHelper::adjustVertices(const OutsetRequest& outsetRequest,
717 bool inset,
718 const EdgeVectors& edgeVectors,
719 const EdgeEquations* edgeEquations,
720 Vertices* vertices) {
721 SkASSERT(vertices);
722 SkASSERT(vertices->fUVRCount == 0 || vertices->fUVRCount == 2 || vertices->fUVRCount == 3);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400723
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400724 // Get signed outsets from cached outset request (which are positive values)
725 V4f signedOutsets = outsetRequest.fOutsets;
726 if (inset) {
727 signedOutsets *= -1.f;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400728 }
729
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400730 if (fDeviceType == GrQuad::Type::kPerspective || outsetRequest.fDegenerate) {
731 Vertices projected = { edgeVectors.fX2D, edgeVectors.fY2D, /*w*/ 1.f, 0.f, 0.f, 0.f, 0};
732 V4f coverage = 1.f;
733 if (outsetRequest.fDegenerate) {
734 // Must use the slow path to handle numerical issues and self intersecting geometry
735 SkASSERT(edgeEquations);
736 V4f signedEdgeDistances = 0.5f; // Half a pixel for AA
737 if (inset) {
738 signedEdgeDistances *= -1.f;
739 }
740 coverage = computeDegenerateQuad(signedEdgeDistances, outsetRequest.fMask,
741 *edgeEquations, &projected);
742 } else {
743 // Move the projected quad with the fast path, even though we will reconstruct the
744 // perspective corners afterwards.
745 projected.moveAlong(edgeVectors, signedOutsets, outsetRequest.fMask);
746 }
747 vertices->moveTo(projected.fX, projected.fY, outsetRequest.fMask != 0.f);
748 return coverage;
749 } else {
750 // Quad is 2D and the inset/outset request does not cause the geometry to self intersect, so
751 // we can directly move the corners along the already calculated edge vectors.
752 vertices->moveAlong(edgeVectors, signedOutsets, outsetRequest.fMask);
753 return 1.f;
754 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400755}
756
757TessellationHelper::TessellationHelper(const GrQuad& deviceQuad, const GrQuad* localQuad)
758 : fAAFlags(GrQuadAAFlags::kNone)
759 , fCoverage(1.f)
760 , fDeviceType(deviceQuad.quadType())
761 , fLocalType(localQuad ? localQuad->quadType() : GrQuad::Type::kAxisAligned) {
762 fOriginal.fX = deviceQuad.x4f();
763 fOriginal.fY = deviceQuad.y4f();
764 fOriginal.fW = deviceQuad.w4f();
765
766 if (localQuad) {
767 fOriginal.fU = localQuad->x4f();
768 fOriginal.fV = localQuad->y4f();
769 fOriginal.fR = localQuad->w4f();
770 fOriginal.fUVRCount = fLocalType == GrQuad::Type::kPerspective ? 3 : 2;
771 } else {
772 fOriginal.fUVRCount = 0;
773 }
774}
775
776V4f TessellationHelper::pixelCoverage() {
777 // When there are no AA edges, insetting and outsetting is skipped since the original geometry
778 // can just be reported directly (in which case fCoverage may be stale).
779 return fAAFlags == GrQuadAAFlags::kNone ? 1.f : fCoverage;
780}
781
782void TessellationHelper::inset(GrQuadAAFlags aaFlags, GrQuad* deviceInset, GrQuad* localInset) {
783 if (aaFlags != fAAFlags) {
784 fAAFlags = aaFlags;
785 if (aaFlags != GrQuadAAFlags::kNone) {
786 this->recomputeInsetAndOutset();
787 }
788 }
789 if (fAAFlags == GrQuadAAFlags::kNone) {
790 this->setQuads(fOriginal, deviceInset, localInset);
791 } else {
792 this->setQuads(fInset, deviceInset, localInset);
793 }
794}
795
796void TessellationHelper::outset(GrQuadAAFlags aaFlags, GrQuad* deviceOutset, GrQuad* localOutset) {
797 if (aaFlags != fAAFlags) {
798 fAAFlags = aaFlags;
799 if (aaFlags != GrQuadAAFlags::kNone) {
800 this->recomputeInsetAndOutset();
801 }
802 }
803 if (fAAFlags == GrQuadAAFlags::kNone) {
804 this->setQuads(fOriginal, deviceOutset, localOutset);
805 } else {
806 this->setQuads(fOutset, deviceOutset, localOutset);
807 }
808}
809
810void TessellationHelper::recomputeInsetAndOutset() {
811 // Start from the original geometry
812 fInset = fOriginal;
813 fOutset = fOriginal;
814
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400815 // Calculate state that can be shared between both inset and outset quads
816 EdgeVectors edgeVectors = this->getEdgeVectors();
817 OutsetRequest outsetRequest = this->getOutsetRequest(edgeVectors);
818
819 // Adjust inset and outset vertices to match the request
820 if (outsetRequest.fDegenerate) {
821 // adjustVertices requires edge equations too
822 EdgeEquations edgeEquations = this->getEdgeEquations(edgeVectors);
823 this->adjustVertices(outsetRequest, false, edgeVectors, &edgeEquations, &fOutset);
824 fCoverage = this->adjustVertices(outsetRequest, true, edgeVectors, &edgeEquations, &fInset);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400825 } else {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400826 // skip calculating edge equations
827 this->adjustVertices(outsetRequest, false, edgeVectors, nullptr, &fOutset);
828 fCoverage = this->adjustVertices(outsetRequest, true, edgeVectors, nullptr, &fInset);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400829 }
830}
831
832void TessellationHelper::setQuads(const Vertices& vertices,
833 GrQuad* deviceOut, GrQuad* localOut) const {
834 SkASSERT(deviceOut);
835 SkASSERT(vertices.fUVRCount == 0 || localOut);
836
837 vertices.fX.store(deviceOut->xs());
838 vertices.fY.store(deviceOut->ys());
839 if (fDeviceType == GrQuad::Type::kPerspective) {
840 vertices.fW.store(deviceOut->ws());
841 }
842 deviceOut->setQuadType(fDeviceType); // This sets ws == 1 when device type != perspective
843
844 if (vertices.fUVRCount > 0) {
845 vertices.fU.store(localOut->xs());
846 vertices.fV.store(localOut->ys());
847 if (vertices.fUVRCount == 3) {
848 vertices.fR.store(localOut->ws());
849 }
850 localOut->setQuadType(fLocalType);
851 }
852}
853
Michael Ludwig0f809022019-06-04 09:14:37 -0400854}; // namespace GrQuadUtils