blob: b4be529f66702d7e5c07ea6072973be8f8d8af56 [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)
389 : fAAFlags(GrQuadAAFlags::kNone)
390 , fDeviceType(deviceQuad.quadType())
391 , fLocalType(localQuad ? localQuad->quadType() : GrQuad::Type::kAxisAligned) {
392 fOriginal.fX = deviceQuad.x4f();
393 fOriginal.fY = deviceQuad.y4f();
394 fOriginal.fW = deviceQuad.w4f();
395
396 if (localQuad) {
397 fOriginal.fU = localQuad->x4f();
398 fOriginal.fV = localQuad->y4f();
399 fOriginal.fR = localQuad->w4f();
400 fOriginal.fUVRCount = fLocalType == GrQuad::Type::kPerspective ? 3 : 2;
401 } else {
402 fOriginal.fUVRCount = 0;
403 }
404
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 Ludwigfb7ba522019-10-29 15:33:34 -0400422}
423
Michael Ludwigce200ac2019-11-04 14:33:32 -0500424const TessellationHelper::EdgeEquations& TessellationHelper::getEdgeEquations() {
425 if (!fEdgeEquations.fValid) {
426 V4f dx = fEdgeVectors.fDX;
427 V4f dy = fEdgeVectors.fDY;
428 // Correct for bad edges by copying adjacent edge information into the bad component
429 correct_bad_edges(fEdgeVectors.fInvLengths >= 1.f / kTolerance, &dx, &dy, nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400430
Michael Ludwigce200ac2019-11-04 14:33:32 -0500431 V4f c = mad(dx, fEdgeVectors.fY2D, -dy * fEdgeVectors.fX2D);
432 // Make sure normals point into the shape
433 V4f test = mad(dy, next_cw(fEdgeVectors.fX2D), mad(-dx, next_cw(fEdgeVectors.fY2D), c));
434 if (any(test < -kTolerance)) {
435 fEdgeEquations.fA = -dy;
436 fEdgeEquations.fB = dx;
437 fEdgeEquations.fC = -c;
438 } else {
439 fEdgeEquations.fA = dy;
440 fEdgeEquations.fB = -dx;
441 fEdgeEquations.fC = c;
442 }
443
444 fEdgeEquations.fValid = true;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400445 }
Michael Ludwigce200ac2019-11-04 14:33:32 -0500446 return fEdgeEquations;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400447}
448
Michael Ludwigce200ac2019-11-04 14:33:32 -0500449const TessellationHelper::OutsetRequest& TessellationHelper::getOutsetRequest() {
450 if (!fOutsetRequest.fValid) {
451 V4f mask = fAAFlags == GrQuadAAFlags::kAll ? V4f(1.f) :
452 V4f{(GrQuadAAFlags::kLeft & fAAFlags) ? 1.f : 0.f,
453 (GrQuadAAFlags::kBottom & fAAFlags) ? 1.f : 0.f,
454 (GrQuadAAFlags::kTop & fAAFlags) ? 1.f : 0.f,
455 (GrQuadAAFlags::kRight & fAAFlags) ? 1.f : 0.f};
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400456
Michael Ludwigce200ac2019-11-04 14:33:32 -0500457 // Calculate the lengths of the outset/inset edge vectors per corner, before applying the
458 // mask
459 bool degenerate;
460 V4f cornerOutsetLen;
461 if (fDeviceType <= GrQuad::Type::kRectilinear) {
462 // Since it's rectangular, the corners move the same distance as the edge lines would
463 cornerOutsetLen = 0.5f;
464 // While it's still rectangular, must use the degenerate path when the quad is less than
465 // a pixel along a side since the coverage must be updated. (len < 1 implies 1/len > 1)
466 degenerate = any(fEdgeVectors.fInvLengths > 1.f);
467 } else if (any(fEdgeVectors.fInvLengths >= 1.f / kTolerance)) {
468 // Have an edge that is effectively length 0, so we're dealing with a triangle. Skip
469 // computing corner outsets, since degenerate path won't use them.
Michael Ludwig3699a172019-11-01 13:38:32 -0400470 degenerate = true;
471 } else {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500472 // Must scale corner distance by 1/2sin(theta), where theta is the angle between the two
473 // edges at that corner. cos(theta) is equal to dot(dXY, next_cw(dXY)),
474 // and sin(theta) = sqrt(1 - cos(theta)^2)
475 V4f cosTheta = mad(fEdgeVectors.fDX, next_cw(fEdgeVectors.fDX),
476 fEdgeVectors.fDY * next_cw(fEdgeVectors.fDY));
Michael Ludwig3699a172019-11-01 13:38:32 -0400477
Michael Ludwigce200ac2019-11-04 14:33:32 -0500478 // If the angle is too shallow between edges, go through the degenerate path, otherwise
479 // adding and subtracting very large vectors in almost opposite directions leads to
480 // float errors.
481 if (any(abs(cosTheta) >= 0.9f)) {
482 // Skip updating the outsets since degenerate code path doesn't rely on that
483 degenerate = true;
484 } else {
485 cornerOutsetLen = 0.5f * rsqrt(1.f - cosTheta * cosTheta); // 1/2sin(theta)
486
487 // When outsetting or insetting, the current edge's AA adds to the length:
488 // cos(pi - theta)/2sin(theta) + cos(pi-ccw(theta))/2sin(ccw(theta))
489 // Moving an adjacent edge updates the length by 1/2sin(theta|ccw(theta))
490 V4f halfTanTheta = -cosTheta * cornerOutsetLen; // cos(pi - theta) = -cos(theta)
491 V4f edgeAdjust = mask * (halfTanTheta + next_ccw(halfTanTheta)) +
492 next_ccw(mask) * next_ccw(cornerOutsetLen) +
493 next_cw(mask) * cornerOutsetLen;
494 // If either outsetting (plus edgeAdjust) or insetting (minus edgeAdjust) make
495 // edgeLen negative then it's degenerate
496 V4f threshold = 0.1f - (1.f / fEdgeVectors.fInvLengths);
497 degenerate = any(edgeAdjust < threshold) || any(edgeAdjust > -threshold);
498 }
Michael Ludwig3699a172019-11-01 13:38:32 -0400499 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400500
Michael Ludwigce200ac2019-11-04 14:33:32 -0500501 fOutsetRequest.fEdgeDistances = 0.5f * mask; // Half a pixel for AA on edges that can move
502 fOutsetRequest.fDegenerate = degenerate;
503 if (!degenerate) {
504 // When the projected device quad is not degenerate, the vertex corners can move
505 // cornerOutsetLen along their edge and their cw-rotated edge. The vertex's edge points
506 // inwards and the cw-rotated edge points outwards, hence the minus-sign.
507 // The mask is rotated compared to the outsets and edge vectors, since if the edge is
508 // "on" both its points need to be moved along their other edge vectors.
509 fOutsetRequest.fOutsets = -cornerOutsetLen * next_cw(mask); // scales dx, dy
510 fOutsetRequest.fOutsetsCW = cornerOutsetLen * mask; // scales next_cw(dx, dy)
511 }
512 fOutsetRequest.fValid = true;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400513 }
Michael Ludwigce200ac2019-11-04 14:33:32 -0500514 return fOutsetRequest;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400515}
516
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400517void TessellationHelper::Vertices::moveAlong(const EdgeVectors& edgeVectors,
Michael Ludwig3699a172019-11-01 13:38:32 -0400518 const OutsetRequest& outsetRequest,
519 bool inset) {
520 SkASSERT(!outsetRequest.fDegenerate);
521 V4f signedOutsets = outsetRequest.fOutsets;
522 V4f signedOutsetsCW = outsetRequest.fOutsetsCW;
523 if (inset) {
524 signedOutsets *= -1.f;
525 signedOutsetsCW *= -1.f;
526 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400527 // x = x + outset * mask * next_cw(xdiff) - outset * next_cw(mask) * xdiff
Michael Ludwig3699a172019-11-01 13:38:32 -0400528 fX += mad(signedOutsetsCW, next_cw(edgeVectors.fDX), signedOutsets * edgeVectors.fDX);
529 fY += mad(signedOutsetsCW, next_cw(edgeVectors.fDY), signedOutsets * edgeVectors.fDY);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400530 if (fUVRCount > 0) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400531 // We want to extend the texture coords by the same proportion as the positions.
Michael Ludwig3699a172019-11-01 13:38:32 -0400532 signedOutsets *= edgeVectors.fInvLengths;
533 signedOutsetsCW *= next_cw(edgeVectors.fInvLengths);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400534 V4f du = next_ccw(fU) - fU;
535 V4f dv = next_ccw(fV) - fV;
Michael Ludwig3699a172019-11-01 13:38:32 -0400536 fU += mad(signedOutsetsCW, next_cw(du), signedOutsets * du);
537 fV += mad(signedOutsetsCW, next_cw(dv), signedOutsets * dv);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400538 if (fUVRCount == 3) {
539 V4f dr = next_ccw(fR) - fR;
Michael Ludwig3699a172019-11-01 13:38:32 -0400540 fR += mad(signedOutsetsCW, next_cw(dr), signedOutsets * dr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400541 }
542 }
543}
544
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400545void TessellationHelper::Vertices::moveTo(const V4f& x2d, const V4f& y2d, const M4f& mask) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400546 // Left to right, in device space, for each point
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400547 V4f e1x = skvx::shuffle<2, 3, 2, 3>(fX) - skvx::shuffle<0, 1, 0, 1>(fX);
548 V4f e1y = skvx::shuffle<2, 3, 2, 3>(fY) - skvx::shuffle<0, 1, 0, 1>(fY);
549 V4f e1w = skvx::shuffle<2, 3, 2, 3>(fW) - skvx::shuffle<0, 1, 0, 1>(fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400550 correct_bad_edges(mad(e1x, e1x, e1y * e1y) < kTolerance * kTolerance, &e1x, &e1y, &e1w);
551
552 // // Top to bottom, in device space, for each point
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400553 V4f e2x = skvx::shuffle<1, 1, 3, 3>(fX) - skvx::shuffle<0, 0, 2, 2>(fX);
554 V4f e2y = skvx::shuffle<1, 1, 3, 3>(fY) - skvx::shuffle<0, 0, 2, 2>(fY);
555 V4f e2w = skvx::shuffle<1, 1, 3, 3>(fW) - skvx::shuffle<0, 0, 2, 2>(fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400556 correct_bad_edges(mad(e2x, e2x, e2y * e2y) < kTolerance * kTolerance, &e2x, &e2y, &e2w);
557
558 // Can only move along e1 and e2 to reach the new 2D point, so we have
559 // x2d = (x + a*e1x + b*e2x) / (w + a*e1w + b*e2w) and
560 // y2d = (y + a*e1y + b*e2y) / (w + a*e1w + b*e2w) for some a, b
561 // This can be rewritten to a*c1x + b*c2x + c3x = 0; a * c1y + b*c2y + c3y = 0, where
562 // the cNx and cNy coefficients are:
563 V4f c1x = e1w * x2d - e1x;
564 V4f c1y = e1w * y2d - e1y;
565 V4f c2x = e2w * x2d - e2x;
566 V4f c2y = e2w * y2d - e2y;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400567 V4f c3x = fW * x2d - fX;
568 V4f c3y = fW * y2d - fY;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400569
570 // Solve for a and b
571 V4f a, b, denom;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400572 if (all(mask)) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400573 // When every edge is outset/inset, each corner can use both edge vectors
574 denom = c1x * c2y - c2x * c1y;
575 a = (c2x * c3y - c3x * c2y) / denom;
576 b = (c3x * c1y - c1x * c3y) / denom;
577 } else {
578 // Force a or b to be 0 if that edge cannot be used due to non-AA
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400579 M4f aMask = skvx::shuffle<0, 0, 3, 3>(mask);
580 M4f bMask = skvx::shuffle<2, 1, 2, 1>(mask);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400581
582 // When aMask[i]&bMask[i], then a[i], b[i], denom[i] match the kAll case.
583 // When aMask[i]&!bMask[i], then b[i] = 0, a[i] = -c3x/c1x or -c3y/c1y, using better denom
584 // When !aMask[i]&bMask[i], then a[i] = 0, b[i] = -c3x/c2x or -c3y/c2y, ""
585 // When !aMask[i]&!bMask[i], then both a[i] = 0 and b[i] = 0
586 M4f useC1x = abs(c1x) > abs(c1y);
587 M4f useC2x = abs(c2x) > abs(c2y);
588
589 denom = if_then_else(aMask,
590 if_then_else(bMask,
591 c1x * c2y - c2x * c1y, /* A & B */
592 if_then_else(useC1x, c1x, c1y)), /* A & !B */
593 if_then_else(bMask,
594 if_then_else(useC2x, c2x, c2y), /* !A & B */
595 V4f(1.f))); /* !A & !B */
596
597 a = if_then_else(aMask,
598 if_then_else(bMask,
599 c2x * c3y - c3x * c2y, /* A & B */
600 if_then_else(useC1x, -c3x, -c3y)), /* A & !B */
601 V4f(0.f)) / denom; /* !A */
602 b = if_then_else(bMask,
603 if_then_else(aMask,
604 c3x * c1y - c1x * c3y, /* A & B */
605 if_then_else(useC2x, -c3x, -c3y)), /* !A & B */
606 V4f(0.f)) / denom; /* !B */
607 }
608
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400609 V4f newW = fW + a * e1w + b * e2w;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400610 // If newW < 0, scale a and b such that the point reaches the infinity plane instead of crossing
611 // This breaks orthogonality of inset/outsets, but GPUs don't handle negative Ws well so this
612 // is far less visually disturbing (likely not noticeable since it's at extreme perspective).
613 // The alternative correction (multiply xyw by -1) has the disadvantage of changing how local
614 // coordinates would be interpolated.
615 static const float kMinW = 1e-6f;
616 if (any(newW < 0.f)) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400617 V4f scale = if_then_else(newW < kMinW, (kMinW - fW) / (newW - fW), V4f(1.f));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400618 a *= scale;
619 b *= scale;
620 }
621
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400622 fX += a * e1x + b * e2x;
623 fY += a * e1y + b * e2y;
624 fW += a * e1w + b * e2w;
625 correct_bad_coords(abs(denom) < kTolerance, &fX, &fY, &fW);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400626
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400627 if (fUVRCount > 0) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400628 // 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 -0400629 V4f e1u = skvx::shuffle<2, 3, 2, 3>(fU) - skvx::shuffle<0, 1, 0, 1>(fU);
630 V4f e1v = skvx::shuffle<2, 3, 2, 3>(fV) - skvx::shuffle<0, 1, 0, 1>(fV);
631 V4f e1r = skvx::shuffle<2, 3, 2, 3>(fR) - skvx::shuffle<0, 1, 0, 1>(fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400632 correct_bad_edges(mad(e1u, e1u, e1v * e1v) < kTolerance * kTolerance, &e1u, &e1v, &e1r);
633
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400634 V4f e2u = skvx::shuffle<1, 1, 3, 3>(fU) - skvx::shuffle<0, 0, 2, 2>(fU);
635 V4f e2v = skvx::shuffle<1, 1, 3, 3>(fV) - skvx::shuffle<0, 0, 2, 2>(fV);
636 V4f e2r = skvx::shuffle<1, 1, 3, 3>(fR) - skvx::shuffle<0, 0, 2, 2>(fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400637 correct_bad_edges(mad(e2u, e2u, e2v * e2v) < kTolerance * kTolerance, &e2u, &e2v, &e2r);
638
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400639 fU += a * e1u + b * e2u;
640 fV += a * e1v + b * e2v;
641 if (fUVRCount == 3) {
642 fR += a * e1r + b * e2r;
643 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, &fR);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400644 } else {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400645 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, nullptr);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400646 }
647 }
648}
649
Michael Ludwig45a22812019-11-04 11:15:30 -0500650V4f TessellationHelper::EdgeEquations::estimateCoverage(const V4f& x2d, const V4f& y2d) const {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400651 // Calculate distance of the 4 inset points (px, py) to the 4 edges
Michael Ludwig45a22812019-11-04 11:15:30 -0500652 V4f d0 = mad(fA[0], x2d, mad(fB[0], y2d, fC[0]));
653 V4f d1 = mad(fA[1], x2d, mad(fB[1], y2d, fC[1]));
654 V4f d2 = mad(fA[2], x2d, mad(fB[2], y2d, fC[2]));
655 V4f d3 = mad(fA[3], x2d, mad(fB[3], y2d, fC[3]));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400656
657 // For each point, pretend that there's a rectangle that touches e0 and e3 on the horizontal
658 // axis, so its width is "approximately" d0 + d3, and it touches e1 and e2 on the vertical axis
659 // so its height is d1 + d2. Pin each of these dimensions to [0, 1] and approximate the coverage
660 // at each point as clamp(d0+d3, 0, 1) x clamp(d1+d2, 0, 1). For rectilinear quads this is an
661 // accurate calculation of its area clipped to an aligned pixel. For arbitrary quads it is not
662 // mathematically accurate but qualitatively provides a stable value proportional to the size of
663 // the shape.
664 V4f w = max(0.f, min(1.f, d0 + d3));
665 V4f h = max(0.f, min(1.f, d1 + d2));
666 return w * h;
667}
668
Michael Ludwigce200ac2019-11-04 14:33:32 -0500669int TessellationHelper::computeDegenerateQuad(const V4f& signedEdgeDistances, V4f* x2d, V4f* y2d) {
Michael Ludwig3699a172019-11-01 13:38:32 -0400670 // Move the edge by the signed edge adjustment.
Michael Ludwigce200ac2019-11-04 14:33:32 -0500671 const EdgeEquations& edges = this->getEdgeEquations();
Michael Ludwig3699a172019-11-01 13:38:32 -0400672 V4f oc = edges.fC + signedEdgeDistances;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400673
674 // There are 6 points that we care about to determine the final shape of the polygon, which
675 // are the intersections between (e0,e2), (e1,e0), (e2,e3), (e3,e1) (corresponding to the
676 // 4 corners), and (e1, e2), (e0, e3) (representing the intersections of opposite edges).
677 V4f denom = edges.fA * next_cw(edges.fB) - edges.fB * next_cw(edges.fA);
678 V4f px = (edges.fB * next_cw(oc) - oc * next_cw(edges.fB)) / denom;
679 V4f py = (oc * next_cw(edges.fA) - edges.fA * next_cw(oc)) / denom;
680 correct_bad_coords(abs(denom) < kTolerance, &px, &py, nullptr);
681
682 // Calculate the signed distances from these 4 corners to the other two edges that did not
683 // define the intersection. So p(0) is compared to e3,e1, p(1) to e3,e2 , p(2) to e0,e1, and
684 // p(3) to e0,e2
685 V4f dists1 = px * skvx::shuffle<3, 3, 0, 0>(edges.fA) +
686 py * skvx::shuffle<3, 3, 0, 0>(edges.fB) +
687 skvx::shuffle<3, 3, 0, 0>(oc);
688 V4f dists2 = px * skvx::shuffle<1, 2, 1, 2>(edges.fA) +
689 py * skvx::shuffle<1, 2, 1, 2>(edges.fB) +
690 skvx::shuffle<1, 2, 1, 2>(oc);
691
692 // If all the distances are >= 0, the 4 corners form a valid quadrilateral, so use them as
693 // the 4 points. If any point is on the wrong side of both edges, the interior has collapsed
694 // and we need to use a central point to represent it. If all four points are only on the
695 // wrong side of 1 edge, one edge has crossed over another and we use a line to represent it.
696 // Otherwise, use a triangle that replaces the bad points with the intersections of
697 // (e1, e2) or (e0, e3) as needed.
698 M4f d1v0 = dists1 < kTolerance;
699 M4f d2v0 = dists2 < kTolerance;
700 M4f d1And2 = d1v0 & d2v0;
701 M4f d1Or2 = d1v0 | d2v0;
702
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400703 if (!any(d1Or2)) {
704 // Every dists1 and dists2 >= kTolerance so it's not degenerate, use all 4 corners as-is
705 // and use full coverage
Michael Ludwig45a22812019-11-04 11:15:30 -0500706 *x2d = px;
707 *y2d = py;
708 return 4;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400709 } else if (any(d1And2)) {
710 // A point failed against two edges, so reduce the shape to a single point, which we take as
711 // the center of the original quad to ensure it is contained in the intended geometry. Since
712 // it has collapsed, we know the shape cannot cover a pixel so update the coverage.
Michael Ludwigba971892019-11-04 11:11:55 -0500713 SkPoint center = {0.25f * ((*x2d)[0] + (*x2d)[1] + (*x2d)[2] + (*x2d)[3]),
714 0.25f * ((*y2d)[0] + (*y2d)[1] + (*y2d)[2] + (*y2d)[3])};
Michael Ludwig45a22812019-11-04 11:15:30 -0500715 *x2d = center.fX;
716 *y2d = center.fY;
717 return 1;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400718 } else if (all(d1Or2)) {
719 // Degenerates to a line. Compare p[2] and p[3] to edge 0. If they are on the wrong side,
720 // that means edge 0 and 3 crossed, and otherwise edge 1 and 2 crossed.
721 if (dists1[2] < kTolerance && dists1[3] < kTolerance) {
722 // 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 -0500723 *x2d = 0.5f * (skvx::shuffle<0, 1, 0, 1>(px) + skvx::shuffle<2, 3, 2, 3>(px));
724 *y2d = 0.5f * (skvx::shuffle<0, 1, 0, 1>(py) + skvx::shuffle<2, 3, 2, 3>(py));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400725 } else {
726 // 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 -0500727 *x2d = 0.5f * (skvx::shuffle<0, 0, 2, 2>(px) + skvx::shuffle<1, 1, 3, 3>(px));
728 *y2d = 0.5f * (skvx::shuffle<0, 0, 2, 2>(py) + skvx::shuffle<1, 1, 3, 3>(py));
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400729 }
Michael Ludwig45a22812019-11-04 11:15:30 -0500730 return 2;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400731 } else {
732 // This turns into a triangle. Replace corners as needed with the intersections between
733 // (e0,e3) and (e1,e2), which must now be calculated
734 using V2f = skvx::Vec<2, float>;
735 V2f eDenom = skvx::shuffle<0, 1>(edges.fA) * skvx::shuffle<3, 2>(edges.fB) -
736 skvx::shuffle<0, 1>(edges.fB) * skvx::shuffle<3, 2>(edges.fA);
737 V2f ex = (skvx::shuffle<0, 1>(edges.fB) * skvx::shuffle<3, 2>(oc) -
738 skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(edges.fB)) / eDenom;
739 V2f ey = (skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(edges.fA) -
740 skvx::shuffle<0, 1>(edges.fA) * skvx::shuffle<3, 2>(oc)) / eDenom;
741
742 if (SkScalarAbs(eDenom[0]) > kTolerance) {
743 px = if_then_else(d1v0, V4f(ex[0]), px);
744 py = if_then_else(d1v0, V4f(ey[0]), py);
745 }
746 if (SkScalarAbs(eDenom[1]) > kTolerance) {
747 px = if_then_else(d2v0, V4f(ex[1]), px);
748 py = if_then_else(d2v0, V4f(ey[1]), py);
749 }
750
Michael Ludwig45a22812019-11-04 11:15:30 -0500751 *x2d = px;
752 *y2d = py;
753 return 3;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400754 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400755}
756
Michael Ludwigce200ac2019-11-04 14:33:32 -0500757int TessellationHelper::adjustVertices(bool inset, Vertices* vertices) {
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400758 SkASSERT(vertices);
759 SkASSERT(vertices->fUVRCount == 0 || vertices->fUVRCount == 2 || vertices->fUVRCount == 3);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400760
Michael Ludwigce200ac2019-11-04 14:33:32 -0500761 const OutsetRequest& outsetRequest = this->getOutsetRequest();
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400762 if (fDeviceType == GrQuad::Type::kPerspective || outsetRequest.fDegenerate) {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500763 Vertices projected = { fEdgeVectors.fX2D, fEdgeVectors.fY2D, /*w*/ 1.f, 0.f, 0.f, 0.f, 0};
Michael Ludwig45a22812019-11-04 11:15:30 -0500764 int vertexCount;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400765 if (outsetRequest.fDegenerate) {
766 // Must use the slow path to handle numerical issues and self intersecting geometry
Michael Ludwig3699a172019-11-01 13:38:32 -0400767 V4f signedEdgeDistances = outsetRequest.fEdgeDistances;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400768 if (inset) {
769 signedEdgeDistances *= -1.f;
770 }
Michael Ludwigce200ac2019-11-04 14:33:32 -0500771 vertexCount = computeDegenerateQuad(signedEdgeDistances, &projected.fX, &projected.fY);
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400772 } else {
773 // Move the projected quad with the fast path, even though we will reconstruct the
774 // perspective corners afterwards.
Michael Ludwigce200ac2019-11-04 14:33:32 -0500775 projected.moveAlong(fEdgeVectors, outsetRequest, inset);
Michael Ludwig45a22812019-11-04 11:15:30 -0500776 vertexCount = 4;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400777 }
Michael Ludwig3699a172019-11-01 13:38:32 -0400778 vertices->moveTo(projected.fX, projected.fY, outsetRequest.fEdgeDistances != 0.f);
Michael Ludwig45a22812019-11-04 11:15:30 -0500779 return vertexCount;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400780 } else {
781 // Quad is 2D and the inset/outset request does not cause the geometry to self intersect, so
782 // we can directly move the corners along the already calculated edge vectors.
Michael Ludwigce200ac2019-11-04 14:33:32 -0500783 vertices->moveAlong(fEdgeVectors, outsetRequest, inset);
Michael Ludwig45a22812019-11-04 11:15:30 -0500784 return 4;
Michael Ludwige64c8bf2019-11-01 13:29:44 -0400785 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400786}
787
Michael Ludwig45a22812019-11-04 11:15:30 -0500788V4f TessellationHelper::inset(GrQuadAAFlags aaFlags, GrQuad* deviceInset, GrQuad* localInset) {
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400789 if (aaFlags != fAAFlags) {
790 fAAFlags = aaFlags;
Michael Ludwigce200ac2019-11-04 14:33:32 -0500791 this->reset();
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400792 }
793 if (fAAFlags == GrQuadAAFlags::kNone) {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500794 // No need to calculate anything since none of the edges are allowed to move. Since it will
795 // be drawn without anti-aliasing, can just return full coverage.
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400796 this->setQuads(fOriginal, deviceInset, localInset);
Michael Ludwig45a22812019-11-04 11:15:30 -0500797 return 1.f;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400798 } else {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500799 Vertices inset = fOriginal;
800 int vertexCount = this->adjustVertices(true, &inset);
801 this->setQuads(inset, deviceInset, localInset);
802
803 if (vertexCount < 3) {
804 // The interior has less than a full pixel's area so estimate reduced coverage using
805 // the distance of the inset's projected corners to the original edges.
806 return this->getEdgeEquations().estimateCoverage(inset.fX / inset.fW,
807 inset.fY / inset.fW);
808 } else {
809 return 1.f;
810 }
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400811 }
812}
813
814void TessellationHelper::outset(GrQuadAAFlags aaFlags, GrQuad* deviceOutset, GrQuad* localOutset) {
815 if (aaFlags != fAAFlags) {
816 fAAFlags = aaFlags;
Michael Ludwigce200ac2019-11-04 14:33:32 -0500817 this->reset();
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400818 }
819 if (fAAFlags == GrQuadAAFlags::kNone) {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500820 // No need to calculate anything since none of the edges are allowed to move
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400821 this->setQuads(fOriginal, deviceOutset, localOutset);
822 } else {
Michael Ludwigce200ac2019-11-04 14:33:32 -0500823 Vertices outset = fOriginal;
824 this->adjustVertices(false, &outset);
825 this->setQuads(outset, deviceOutset, localOutset);
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400826 }
827}
828
Michael Ludwigce200ac2019-11-04 14:33:32 -0500829void TessellationHelper::reset() {
830 fOutsetRequest.fValid = false;
831 fEdgeEquations.fValid = false;
Michael Ludwigfb7ba522019-10-29 15:33:34 -0400832}
833
834void TessellationHelper::setQuads(const Vertices& vertices,
835 GrQuad* deviceOut, GrQuad* localOut) const {
836 SkASSERT(deviceOut);
837 SkASSERT(vertices.fUVRCount == 0 || localOut);
838
839 vertices.fX.store(deviceOut->xs());
840 vertices.fY.store(deviceOut->ys());
841 if (fDeviceType == GrQuad::Type::kPerspective) {
842 vertices.fW.store(deviceOut->ws());
843 }
844 deviceOut->setQuadType(fDeviceType); // This sets ws == 1 when device type != perspective
845
846 if (vertices.fUVRCount > 0) {
847 vertices.fU.store(localOut->xs());
848 vertices.fV.store(localOut->ys());
849 if (vertices.fUVRCount == 3) {
850 vertices.fR.store(localOut->ws());
851 }
852 localOut->setQuadType(fLocalType);
853 }
854}
855
Michael Ludwig0f809022019-06-04 09:14:37 -0400856}; // namespace GrQuadUtils