Flutter Engine
The Flutter Engine
GrQuadUtils.cpp
Go to the documentation of this file.
1/*
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 */
8
10#include "include/core/SkRect.h"
13#include "include/gpu/GrTypes.h"
16#include "src/base/SkVx.h"
17#include "src/core/SkPathPriv.h"
19
20#include <algorithm>
21#include <cmath>
22
24using mask4 = skvx::int4; // aliased to 'mask' to emphasize that it will hold boolean SIMD masks.
25
26#define AI SK_ALWAYS_INLINE
27
28// General tolerance used for denominators, checking div-by-0
29static constexpr float kTolerance = 1e-9f;
30// Increased slop when comparing signed distances / lengths
31static constexpr float kDistTolerance = 1e-2f;
33static constexpr float kInvDistTolerance = 1.f / kDistTolerance;
34
35// These rotate the points/edge values either clockwise or counterclockwise assuming tri strip
36// order.
37template<typename T>
39 return skvx::shuffle<2, 0, 3, 1>(v);
40}
41
42template<typename T>
44 return skvx::shuffle<1, 3, 0, 2>(v);
45}
46
47static AI float4 next_diag(const float4& v) {
48 // Same as next_ccw(next_ccw(v)), or next_cw(next_cw(v)), e.g. two rotations either direction.
49 return skvx::shuffle<3, 2, 1, 0>(v);
50}
51
52// Replaces zero-length 'bad' edge vectors with the reversed opposite edge vector.
53// e3 may be null if only 2D edges need to be corrected for.
54static AI void correct_bad_edges(const mask4& bad, float4* e1, float4* e2, float4* e3) {
55 if (any(bad)) {
56 // Want opposite edges, L B T R -> R T B L but with flipped sign to preserve winding
57 *e1 = if_then_else(bad, -next_diag(*e1), *e1);
58 *e2 = if_then_else(bad, -next_diag(*e2), *e2);
59 if (e3) {
60 *e3 = if_then_else(bad, -next_diag(*e3), *e3);
61 }
62 }
63}
64
65// Replace 'bad' coordinates by rotating CCW to get the next point. c3 may be null for 2D points.
66static AI void correct_bad_coords(const mask4& bad, float4* c1, float4* c2, float4* c3) {
67 if (any(bad)) {
68 *c1 = if_then_else(bad, next_ccw(*c1), *c1);
69 *c2 = if_then_else(bad, next_ccw(*c2), *c2);
70 if (c3) {
71 *c3 = if_then_else(bad, next_ccw(*c3), *c3);
72 }
73 }
74}
75
76// Since the local quad may not be type kRect, this uses the opposites for each vertex when
77// interpolating, and calculates new ws in addition to new xs, ys.
78static void interpolate_local(float alpha, int v0, int v1, int v2, int v3,
79 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 float beta = 1.f - alpha;
86 lx[v0] = alpha * lx[v0] + beta * lx[v2];
87 ly[v0] = alpha * ly[v0] + beta * ly[v2];
88 lw[v0] = alpha * lw[v0] + beta * lw[v2];
89
90 lx[v1] = alpha * lx[v1] + beta * lx[v3];
91 ly[v1] = alpha * ly[v1] + beta * ly[v3];
92 lw[v1] = alpha * lw[v1] + beta * lw[v3];
93}
94
95// Crops v0 to v1 based on the clipDevRect. v2 is opposite of v0, v3 is opposite of v1.
96// It is written to not modify coordinates if there's no intersection along the edge.
97// Ideally this would have been detected earlier and the entire draw is skipped.
98static bool crop_rect_edge(const SkRect& clipDevRect, int v0, int v1, int v2, int v3,
99 float x[4], float y[4], float lx[4], float ly[4], float lw[4]) {
100 SkASSERT(v0 >= 0 && v0 < 4);
101 SkASSERT(v1 >= 0 && v1 < 4);
102 SkASSERT(v2 >= 0 && v2 < 4);
103 SkASSERT(v3 >= 0 && v3 < 4);
104
105 if (SkScalarNearlyEqual(x[v0], x[v1])) {
106 // A vertical edge
107 if (x[v0] < clipDevRect.fLeft && x[v2] >= clipDevRect.fLeft) {
108 // Overlapping with left edge of clipDevRect
109 if (lx) {
110 float alpha = (x[v2] - clipDevRect.fLeft) / (x[v2] - x[v0]);
111 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
112 }
113 x[v0] = clipDevRect.fLeft;
114 x[v1] = clipDevRect.fLeft;
115 return true;
116 } else if (x[v0] > clipDevRect.fRight && x[v2] <= clipDevRect.fRight) {
117 // Overlapping with right edge of clipDevRect
118 if (lx) {
119 float alpha = (clipDevRect.fRight - x[v2]) / (x[v0] - x[v2]);
120 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
121 }
122 x[v0] = clipDevRect.fRight;
123 x[v1] = clipDevRect.fRight;
124 return true;
125 }
126 } else {
127 // A horizontal edge
128 SkASSERT(SkScalarNearlyEqual(y[v0], y[v1]));
129 if (y[v0] < clipDevRect.fTop && y[v2] >= clipDevRect.fTop) {
130 // Overlapping with top edge of clipDevRect
131 if (lx) {
132 float alpha = (y[v2] - clipDevRect.fTop) / (y[v2] - y[v0]);
133 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
134 }
135 y[v0] = clipDevRect.fTop;
136 y[v1] = clipDevRect.fTop;
137 return true;
138 } else if (y[v0] > clipDevRect.fBottom && y[v2] <= clipDevRect.fBottom) {
139 // Overlapping with bottom edge of clipDevRect
140 if (lx) {
141 float alpha = (clipDevRect.fBottom - y[v2]) / (y[v0] - y[v2]);
142 interpolate_local(alpha, v0, v1, v2, v3, lx, ly, lw);
143 }
144 y[v0] = clipDevRect.fBottom;
145 y[v1] = clipDevRect.fBottom;
146 return true;
147 }
148 }
149
150 // No overlap so don't crop it
151 return false;
152}
153
154// Updates x and y to intersect with clipDevRect. lx, ly, and lw are updated appropriately and may
155// be null to skip calculations. Returns bit mask of edges that were clipped.
156static GrQuadAAFlags crop_rect(const SkRect& clipDevRect, float x[4], float y[4],
157 float lx[4], float ly[4], float lw[4]) {
158 GrQuadAAFlags clipEdgeFlags = GrQuadAAFlags::kNone;
159
160 // The quad's left edge may not align with the SkRect notion of left due to 90 degree rotations
161 // or mirrors. So, this processes the logical edges of the quad and clamps it to the 4 sides of
162 // clipDevRect.
163
164 // Quad's left is v0 to v1 (op. v2 and v3)
165 if (crop_rect_edge(clipDevRect, 0, 1, 2, 3, x, y, lx, ly, lw)) {
166 clipEdgeFlags |= GrQuadAAFlags::kLeft;
167 }
168 // Quad's top edge is v0 to v2 (op. v1 and v3)
169 if (crop_rect_edge(clipDevRect, 0, 2, 1, 3, x, y, lx, ly, lw)) {
170 clipEdgeFlags |= GrQuadAAFlags::kTop;
171 }
172 // Quad's right edge is v2 to v3 (op. v0 and v1)
173 if (crop_rect_edge(clipDevRect, 2, 3, 0, 1, x, y, lx, ly, lw)) {
174 clipEdgeFlags |= GrQuadAAFlags::kRight;
175 }
176 // Quad's bottom edge is v1 to v3 (op. v0 and v2)
177 if (crop_rect_edge(clipDevRect, 1, 3, 0, 2, x, y, lx, ly, lw)) {
178 clipEdgeFlags |= GrQuadAAFlags::kBottom;
179 }
180
181 return clipEdgeFlags;
182}
183
184// Similar to crop_rect, but assumes that both the device coordinates and optional local coordinates
185// geometrically match the TL, BL, TR, BR vertex ordering, i.e. axis-aligned but not flipped, etc.
186static GrQuadAAFlags crop_simple_rect(const SkRect& clipDevRect, float x[4], float y[4],
187 float lx[4], float ly[4]) {
188 GrQuadAAFlags clipEdgeFlags = GrQuadAAFlags::kNone;
189
190 // Update local coordinates proportionately to how much the device rect edge was clipped
191 const SkScalar dx = lx ? (lx[2] - lx[0]) / (x[2] - x[0]) : 0.f;
192 const SkScalar dy = ly ? (ly[1] - ly[0]) / (y[1] - y[0]) : 0.f;
193 if (clipDevRect.fLeft > x[0]) {
194 if (lx) {
195 lx[0] += (clipDevRect.fLeft - x[0]) * dx;
196 lx[1] = lx[0];
197 }
198 x[0] = clipDevRect.fLeft;
199 x[1] = clipDevRect.fLeft;
200 clipEdgeFlags |= GrQuadAAFlags::kLeft;
201 }
202 if (clipDevRect.fTop > y[0]) {
203 if (ly) {
204 ly[0] += (clipDevRect.fTop - y[0]) * dy;
205 ly[2] = ly[0];
206 }
207 y[0] = clipDevRect.fTop;
208 y[2] = clipDevRect.fTop;
209 clipEdgeFlags |= GrQuadAAFlags::kTop;
210 }
211 if (clipDevRect.fRight < x[2]) {
212 if (lx) {
213 lx[2] -= (x[2] - clipDevRect.fRight) * dx;
214 lx[3] = lx[2];
215 }
216 x[2] = clipDevRect.fRight;
217 x[3] = clipDevRect.fRight;
218 clipEdgeFlags |= GrQuadAAFlags::kRight;
219 }
220 if (clipDevRect.fBottom < y[1]) {
221 if (ly) {
222 ly[1] -= (y[1] - clipDevRect.fBottom) * dy;
223 ly[3] = ly[1];
224 }
225 y[1] = clipDevRect.fBottom;
226 y[3] = clipDevRect.fBottom;
227 clipEdgeFlags |= GrQuadAAFlags::kBottom;
228 }
229
230 return clipEdgeFlags;
231}
232// Consistent with GrQuad::asRect()'s return value but requires fewer operations since we don't need
233// to calculate the bounds of the quad.
234static bool is_simple_rect(const GrQuad& quad) {
235 if (quad.quadType() != GrQuad::Type::kAxisAligned) {
236 return false;
237 }
238 // v0 at the geometric top-left is unique, so we only need to compare x[0] < x[2] for left
239 // and y[0] < y[1] for top, but add a little padding to protect against numerical precision
240 // on R90 and R270 transforms tricking this check.
241 return ((quad.x(0) + SK_ScalarNearlyZero) < quad.x(2)) &&
242 ((quad.y(0) + SK_ScalarNearlyZero) < quad.y(1));
243}
244
245// Calculates barycentric coordinates for each point in (testX, testY) in the triangle formed by
246// (x0,y0) - (x1,y1) - (x2, y2) and stores them in u, v, w.
247static bool barycentric_coords(float x0, float y0, float x1, float y1, float x2, float y2,
248 const float4& testX, const float4& testY,
249 float4* u, float4* v, float4* w) {
250 // The 32-bit calculations can have catastrophic cancellation if the device-space coordinates
251 // are really big, and this code needs to handle that because we evaluate barycentric coords
252 // pre-cropping to the render target bounds. This preserves some precision by shrinking the
253 // coordinate space if the bounds are large.
254 static constexpr float kCoordLimit = 1e7f; // Big but somewhat arbitrary, fixes crbug:10141204
255 float scaleX = std::max(std::max(x0, x1), x2) - std::min(std::min(x0, x1), x2);
256 float scaleY = std::max(std::max(y0, y1), y2) - std::min(std::min(y0, y1), y2);
257 if (scaleX > kCoordLimit) {
258 scaleX = kCoordLimit / scaleX;
259 x0 *= scaleX;
260 x1 *= scaleX;
261 x2 *= scaleX;
262 } else {
263 // Don't scale anything
264 scaleX = 1.f;
265 }
266 if (scaleY > kCoordLimit) {
267 scaleY = kCoordLimit / scaleY;
268 y0 *= scaleY;
269 y1 *= scaleY;
270 y2 *= scaleY;
271 } else {
272 scaleY = 1.f;
273 }
274
275 // Modeled after SkPathOpsQuad::pointInTriangle() but uses float instead of double, is
276 // vectorized and outputs normalized barycentric coordinates instead of inside/outside test
277 float v0x = x2 - x0;
278 float v0y = y2 - y0;
279 float v1x = x1 - x0;
280 float v1y = y1 - y0;
281
282 float dot00 = v0x * v0x + v0y * v0y;
283 float dot01 = v0x * v1x + v0y * v1y;
284 float dot11 = v1x * v1x + v1y * v1y;
285
286 // Not yet 1/d, first check d != 0 with a healthy tolerance (worst case is we end up not
287 // cropping something we could have, which is better than cropping something we shouldn't have).
288 // The tolerance is partly so large because these comparisons operate in device px^4 units,
289 // with plenty of subtractions thrown in. The SkPathOpsQuad code's use of doubles helped, and
290 // because it only needed to return "inside triangle", it could compare against [0, denom] and
291 // skip the normalization entirely.
292 float invDenom = dot00 * dot11 - dot01 * dot01;
293 static constexpr SkScalar kEmptyTriTolerance = SK_Scalar1 / (1 << 5);
294 if (SkScalarNearlyZero(invDenom, kEmptyTriTolerance)) {
295 // The triangle was degenerate/empty, which can cause the following UVW calculations to
296 // return (0,0,1) for every test point. This in turn makes the cropping code think that the
297 // empty triangle contains the crop rect and we turn the draw into a fullscreen clear, which
298 // is definitely the utter opposite of what we'd expect for an empty shape.
299 return false;
300 } else {
301 // Safe to divide
302 invDenom = sk_ieee_float_divide(1.f, invDenom);
303 }
304
305 float4 v2x = (scaleX * testX) - x0;
306 float4 v2y = (scaleY * testY) - y0;
307
308 float4 dot02 = v0x * v2x + v0y * v2y;
309 float4 dot12 = v1x * v2x + v1y * v2y;
310
311 // These are relative to the vertices, so there's no need to undo the scale factor
312 *u = (dot11 * dot02 - dot01 * dot12) * invDenom;
313 *v = (dot00 * dot12 - dot01 * dot02) * invDenom;
314 *w = 1.f - *u - *v;
315
316 return true;
317}
318
319static mask4 inside_triangle(const float4& u, const float4& v, const float4& w) {
320 return ((u >= 0.f) & (u <= 1.f)) & ((v >= 0.f) & (v <= 1.f)) & ((w >= 0.f) & (w <= 1.f));
321}
322
323///////////////////////////////////////////////////////////////////////////////////////////////////
324
325SkRect GrQuad::projectedBounds() const {
326 float4 xs = this->x4f();
327 float4 ys = this->y4f();
328 float4 ws = this->w4f();
330 if (any(clipW)) {
331 float4 x2d = xs / ws;
332 float4 y2d = ys / ws;
333 // Bounds of just the projected points in front of w = epsilon
334 SkRect frontBounds = {
339 };
340 // Calculate clipped coordinates by following CCW edges, only keeping points where the w
341 // actually changes sign between the vertices.
343 x2d = (t * next_ccw(xs) + (1.f - t) * xs) / SkPathPriv::kW0PlaneDistance;
344 y2d = (t * next_ccw(ys) + (1.f - t) * ys) / SkPathPriv::kW0PlaneDistance;
345 // True if (w < e) xor (ccw(w) < e), i.e. crosses the w = epsilon plane
346 clipW = clipW ^ (next_ccw(ws) < SkPathPriv::kW0PlaneDistance);
347 return {
348 min(if_then_else(clipW, x2d, float4(frontBounds.fLeft))),
349 min(if_then_else(clipW, y2d, float4(frontBounds.fTop))),
350 max(if_then_else(clipW, x2d, float4(frontBounds.fRight))),
351 max(if_then_else(clipW, y2d, float4(frontBounds.fBottom)))
352 };
353 } else {
354 // Nothing is behind the viewer, so the projection is straight forward and valid
355 ws = 1.f / ws;
356 float4 x2d = xs * ws;
357 float4 y2d = ys * ws;
358 return {min(x2d), min(y2d), max(x2d), max(y2d)};
359 }
360}
361
362///////////////////////////////////////////////////////////////////////////////////////////////////
363
364namespace GrQuadUtils {
365
366void ResolveAAType(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, const GrQuad& quad,
367 GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) {
368 // Most cases will keep the requested types unchanged
369 *outAAType = requestedAAType;
370 *outEdgeFlags = requestedEdgeFlags;
371
372 switch (requestedAAType) {
373 // When aa type is coverage, disable AA if the edge configuration doesn't actually need it
375 if (requestedEdgeFlags == GrQuadAAFlags::kNone) {
376 // This can happen when quads are drawn in bulk, where the requestedAAType was
377 // conservatively enabled and the edge flags are per-entry.
378 *outAAType = GrAAType::kNone;
379 } else if (quad.quadType() == GrQuad::Type::kAxisAligned &&
380 !quad.aaHasEffectOnRect(requestedEdgeFlags)) {
381 // For coverage AA, if the quad is a rect and AA-enabled edges line up with pixel
382 // boundaries, then overall AA and per-edge AA can be completely disabled.
383 *outAAType = GrAAType::kNone;
384 *outEdgeFlags = GrQuadAAFlags::kNone;
385 }
386
387 break;
388 // For no or msaa anti aliasing, override the edge flags since edge flags only make sense
389 // when coverage aa is being used.
390 case GrAAType::kNone:
391 *outEdgeFlags = GrQuadAAFlags::kNone;
392 break;
393 case GrAAType::kMSAA:
394 *outEdgeFlags = GrQuadAAFlags::kAll;
395 break;
396 }
397}
398
399int ClipToW0(DrawQuad* quad, DrawQuad* extraVertices) {
400 using Vertices = TessellationHelper::Vertices;
401
402 SkASSERT(quad && extraVertices);
403
405 // W implicitly 1s for each vertex, so nothing to do but draw unmodified 'quad'
406 return 1;
407 }
408
409 mask4 validW = quad->fDevice.w4f() >= SkPathPriv::kW0PlaneDistance;
410 if (all(validW)) {
411 // Nothing to clip, can proceed normally drawing just 'quad'
412 return 1;
413 } else if (!any(validW)) {
414 // Everything is clipped, so draw nothing
415 return 0;
416 }
417
418 // The clipped local coordinates will most likely not remain rectilinear
419 GrQuad::Type localType = quad->fLocal.quadType();
420 if (localType < GrQuad::Type::kGeneral) {
421 localType = GrQuad::Type::kGeneral;
422 }
423
424 // If we got here, there are 1, 2, or 3 points behind the w = 0 plane. If 2 or 3 points are
425 // clipped we can define a new quad that covers the clipped shape directly. If there's 1 clipped
426 // out, the new geometry is a pentagon.
427 Vertices v;
428 v.reset(quad->fDevice, &quad->fLocal);
429
430 int clipCount = (validW[0] ? 0 : 1) + (validW[1] ? 0 : 1) +
431 (validW[2] ? 0 : 1) + (validW[3] ? 0 : 1);
432 SkASSERT(clipCount >= 1 && clipCount <= 3);
433
434 // FIXME de-duplicate from the projectedBounds() calculations.
435 float4 t = (SkPathPriv::kW0PlaneDistance - v.fW) / (next_ccw(v.fW) - v.fW);
436
437 Vertices clip;
438 clip.fX = (t * next_ccw(v.fX) + (1.f - t) * v.fX);
439 clip.fY = (t * next_ccw(v.fY) + (1.f - t) * v.fY);
441
442 clip.fU = (t * next_ccw(v.fU) + (1.f - t) * v.fU);
443 clip.fV = (t * next_ccw(v.fV) + (1.f - t) * v.fV);
444 clip.fR = (t * next_ccw(v.fR) + (1.f - t) * v.fR);
445
446 mask4 ccwValid = next_ccw(v.fW) >= SkPathPriv::kW0PlaneDistance;
447 mask4 cwValid = next_cw(v.fW) >= SkPathPriv::kW0PlaneDistance;
448
449 if (clipCount != 1) {
450 // Simplest case, replace behind-w0 points with their clipped points by following CCW edge
451 // or CW edge, depending on if the edge crosses from neg. to pos. w or pos. to neg.
452 SkASSERT(clipCount == 2 || clipCount == 3);
453
454 // NOTE: when 3 vertices are clipped, this results in a degenerate quad where one vertex
455 // is replicated. This is preferably to inserting a 3rd vertex on the w = 0 intersection
456 // line because two parallel edges make inset/outset math unstable for large quads.
457 v.fX = if_then_else(validW, v.fX,
458 if_then_else((!ccwValid) & (!cwValid), next_ccw(clip.fX),
459 if_then_else(ccwValid, clip.fX, /* cwValid */ next_cw(clip.fX))));
460 v.fY = if_then_else(validW, v.fY,
461 if_then_else((!ccwValid) & (!cwValid), next_ccw(clip.fY),
462 if_then_else(ccwValid, clip.fY, /* cwValid */ next_cw(clip.fY))));
463 v.fW = if_then_else(validW, v.fW, clip.fW);
464
465 v.fU = if_then_else(validW, v.fU,
466 if_then_else((!ccwValid) & (!cwValid), next_ccw(clip.fU),
467 if_then_else(ccwValid, clip.fU, /* cwValid */ next_cw(clip.fU))));
468 v.fV = if_then_else(validW, v.fV,
469 if_then_else((!ccwValid) & (!cwValid), next_ccw(clip.fV),
470 if_then_else(ccwValid, clip.fV, /* cwValid */ next_cw(clip.fV))));
471 v.fR = if_then_else(validW, v.fR,
472 if_then_else((!ccwValid) & (!cwValid), next_ccw(clip.fR),
473 if_then_else(ccwValid, clip.fR, /* cwValid */ next_cw(clip.fR))));
474
475 // For 2 or 3 clipped vertices, the resulting shape is a quad or a triangle, so it can be
476 // entirely represented in 'quad'.
477 v.asGrQuads(&quad->fDevice, GrQuad::Type::kPerspective,
478 &quad->fLocal, localType);
479 return 1;
480 } else {
481 // The clipped geometry is a pentagon, so it will be represented as two quads connected by
482 // a new non-AA edge. Use the midpoint along one of the unclipped edges as a split vertex.
483 Vertices mid;
484 mid.fX = 0.5f * (v.fX + next_ccw(v.fX));
485 mid.fY = 0.5f * (v.fY + next_ccw(v.fY));
486 mid.fW = 0.5f * (v.fW + next_ccw(v.fW));
487
488 mid.fU = 0.5f * (v.fU + next_ccw(v.fU));
489 mid.fV = 0.5f * (v.fV + next_ccw(v.fV));
490 mid.fR = 0.5f * (v.fR + next_ccw(v.fR));
491
492 // Make a quad formed by the 2 clipped points, the inserted mid point, and the good vertex
493 // that is CCW rotated from the clipped vertex.
494 Vertices v2;
495 v2.fUVRCount = v.fUVRCount;
496 v2.fX = if_then_else((!validW) | (!ccwValid), clip.fX,
497 if_then_else(cwValid, next_cw(mid.fX), v.fX));
498 v2.fY = if_then_else((!validW) | (!ccwValid), clip.fY,
499 if_then_else(cwValid, next_cw(mid.fY), v.fY));
500 v2.fW = if_then_else((!validW) | (!ccwValid), clip.fW,
501 if_then_else(cwValid, next_cw(mid.fW), v.fW));
502
503 v2.fU = if_then_else((!validW) | (!ccwValid), clip.fU,
504 if_then_else(cwValid, next_cw(mid.fU), v.fU));
505 v2.fV = if_then_else((!validW) | (!ccwValid), clip.fV,
506 if_then_else(cwValid, next_cw(mid.fV), v.fV));
507 v2.fR = if_then_else((!validW) | (!ccwValid), clip.fR,
508 if_then_else(cwValid, next_cw(mid.fR), v.fR));
509 // The non-AA edge for this quad is the opposite of the clipped vertex's edge
510 GrQuadAAFlags v2EdgeFlag = (!validW[0] ? GrQuadAAFlags::kRight : // left clipped -> right
511 (!validW[1] ? GrQuadAAFlags::kTop : // bottom clipped -> top
512 (!validW[2] ? GrQuadAAFlags::kBottom : // top clipped -> bottom
513 GrQuadAAFlags::kLeft))); // right clipped -> left
514 extraVertices->fEdgeFlags = quad->fEdgeFlags & ~v2EdgeFlag;
515
516 // Make a quad formed by the remaining two good vertices, one clipped point, and the
517 // inserted mid point.
518 v.fX = if_then_else(!validW, next_cw(clip.fX),
519 if_then_else(!cwValid, mid.fX, v.fX));
520 v.fY = if_then_else(!validW, next_cw(clip.fY),
521 if_then_else(!cwValid, mid.fY, v.fY));
522 v.fW = if_then_else(!validW, clip.fW,
523 if_then_else(!cwValid, mid.fW, v.fW));
524
525 v.fU = if_then_else(!validW, next_cw(clip.fU),
526 if_then_else(!cwValid, mid.fU, v.fU));
527 v.fV = if_then_else(!validW, next_cw(clip.fV),
528 if_then_else(!cwValid, mid.fV, v.fV));
529 v.fR = if_then_else(!validW, next_cw(clip.fR),
530 if_then_else(!cwValid, mid.fR, v.fR));
531 // The non-AA edge for this quad is the clipped vertex's edge
532 GrQuadAAFlags v1EdgeFlag = (!validW[0] ? GrQuadAAFlags::kLeft :
533 (!validW[1] ? GrQuadAAFlags::kBottom :
534 (!validW[2] ? GrQuadAAFlags::kTop :
536
537 v.asGrQuads(&quad->fDevice, GrQuad::Type::kPerspective,
538 &quad->fLocal, localType);
539 quad->fEdgeFlags &= ~v1EdgeFlag;
540
541 v2.asGrQuads(&extraVertices->fDevice, GrQuad::Type::kPerspective,
542 &extraVertices->fLocal, localType);
543 // Caller must draw both 'quad' and 'extraVertices' to cover the clipped geometry
544 return 2;
545 }
546}
547
548bool CropToRect(const SkRect& cropRect, GrAA cropAA, DrawQuad* quad, bool computeLocal) {
549 SkASSERT(quad->fDevice.isFinite());
550
552 // crop_rect and crop_rect_simple keep the rectangles as rectangles, so the intersection
553 // of the crop and quad can be calculated exactly. Some care must be taken if the quad
554 // is axis-aligned but does not satisfy asRect() due to flips, etc.
555 GrQuadAAFlags clippedEdges;
556 if (computeLocal) {
557 if (is_simple_rect(quad->fDevice) && is_simple_rect(quad->fLocal)) {
558 clippedEdges = crop_simple_rect(cropRect, quad->fDevice.xs(), quad->fDevice.ys(),
559 quad->fLocal.xs(), quad->fLocal.ys());
560 } else {
561 clippedEdges = crop_rect(cropRect, quad->fDevice.xs(), quad->fDevice.ys(),
562 quad->fLocal.xs(), quad->fLocal.ys(), quad->fLocal.ws());
563 }
564 } else {
565 if (is_simple_rect(quad->fDevice)) {
566 clippedEdges = crop_simple_rect(cropRect, quad->fDevice.xs(), quad->fDevice.ys(),
567 nullptr, nullptr);
568 } else {
569 clippedEdges = crop_rect(cropRect, quad->fDevice.xs(), quad->fDevice.ys(),
570 nullptr, nullptr, nullptr);
571 }
572 }
573
574 // Apply the clipped edge updates to the original edge flags
575 if (cropAA == GrAA::kYes) {
576 // Turn on all edges that were clipped
577 quad->fEdgeFlags |= clippedEdges;
578 } else {
579 // Turn off all edges that were clipped
580 quad->fEdgeFlags &= ~clippedEdges;
581 }
582 return true;
583 }
584
585 if (computeLocal || quad->fDevice.quadType() == GrQuad::Type::kPerspective) {
586 // FIXME (michaelludwig) Calculate cropped local coordinates when not kAxisAligned
587 // FIXME (michaelludwig) crbug.com/1204347 and skbug.com/9906 - disable this when there's
588 // perspective; it does not prove numerical robust enough in the wild and should be
589 // revisited.
590 return false;
591 }
592
593 float4 devX = quad->fDevice.x4f();
594 float4 devY = quad->fDevice.y4f();
595
596 float4 clipX = {cropRect.fLeft, cropRect.fLeft, cropRect.fRight, cropRect.fRight};
597 float4 clipY = {cropRect.fTop, cropRect.fBottom, cropRect.fTop, cropRect.fBottom};
598
599 // Calculate barycentric coordinates for the 4 rect corners in the 2 triangles that the quad
600 // is tessellated into when drawn.
601 float4 u1, v1, w1;
602 float4 u2, v2, w2;
603 if (!barycentric_coords(devX[0], devY[0], devX[1], devY[1], devX[2], devY[2], clipX, clipY,
604 &u1, &v1, &w1) ||
605 !barycentric_coords(devX[1], devY[1], devX[3], devY[3], devX[2], devY[2], clipX, clipY,
606 &u2, &v2, &w2)) {
607 // Bad triangles, skip cropping
608 return false;
609 }
610
611 // clipDevRect is completely inside this quad if each corner is in at least one of two triangles
612 mask4 inTri1 = inside_triangle(u1, v1, w1);
613 mask4 inTri2 = inside_triangle(u2, v2, w2);
614 if (all(inTri1 | inTri2)) {
615 // We can crop to exactly the clipDevRect.
616 // FIXME (michaelludwig) - there are other ways to have determined quad covering the clip
617 // rect, but the barycentric coords will be useful to derive local coordinates in the future
618
619 // Since we are cropped to exactly clipDevRect, we have discarded any perspective and the
620 // type becomes kRect. If updated locals were requested, they will incorporate perspective.
621 // FIXME (michaelludwig) - once we have local coordinates handled, it may be desirable to
622 // keep the draw as perspective so that the hardware does perspective interpolation instead
623 // of pushing it into a local coord w and having the shader do an extra divide.
624 clipX.store(quad->fDevice.xs());
625 clipY.store(quad->fDevice.ys());
627
628 // Update the edge flags to match the clip setting since all 4 edges have been clipped
630
631 return true;
632 }
633
634 // FIXME (michaelludwig) - use TessellationHelper's inset/outset math to move
635 // edges to the closest clip corner they are outside of
636
637 return false;
638}
639
640bool WillUseHairline(const GrQuad& quad, GrAAType aaType, GrQuadAAFlags edgeFlags) {
641 if (aaType != GrAAType::kCoverage || edgeFlags != GrQuadAAFlags::kAll) {
642 // Non-aa or msaa don't do any outsetting so they will not be hairlined; mixed edge flags
643 // could be hairlined in theory, but applying hairline bloat would extend beyond the
644 // original tiled shape.
645 return false;
646 }
647
648 if (quad.quadType() == GrQuad::Type::kAxisAligned) {
649 // Fast path that avoids computing edge properties via TessellationHelper.
650 // Taking the absolute value of the diagonals always produces the minimum of width or
651 // height given that this is axis-aligned, regardless of mirror or 90/180-degree rotations.
652 float d = std::min(std::abs(quad.x(3) - quad.x(0)), std::abs(quad.y(3) - quad.y(0)));
653 return d < 1.f;
654 } else {
655 TessellationHelper helper;
656 helper.reset(quad, nullptr);
657 return helper.isSubpixel();
658 }
659}
660
661///////////////////////////////////////////////////////////////////////////////////////////////////
662// TessellationHelper implementation and helper struct implementations
663///////////////////////////////////////////////////////////////////////////////////////////////////
664
665//** EdgeVectors implementation
666
668 const skvx::Vec<4, float>& ys,
669 const skvx::Vec<4, float>& ws,
670 GrQuad::Type quadType) {
671 // Calculate all projected edge vector values for this quad.
672 if (quadType == GrQuad::Type::kPerspective) {
673 float4 iw = 1.f / ws;
674 fX2D = xs * iw;
675 fY2D = ys * iw;
676 } else {
677 fX2D = xs;
678 fY2D = ys;
679 }
680
681 fDX = next_ccw(fX2D) - fX2D;
682 fDY = next_ccw(fY2D) - fY2D;
683 fInvLengths = 1.f / sqrt(fDX*fDX + fDY*fDY);
684
685 // Normalize edge vectors
686 fDX *= fInvLengths;
687 fDY *= fInvLengths;
688
689 // Calculate angles between vectors
690 if (quadType <= GrQuad::Type::kRectilinear) {
691 fCosTheta = 0.f;
692 fInvSinTheta = 1.f;
693 } else {
694 fCosTheta = fDX*next_cw(fDX) + fDY*next_cw(fDY);
695 // NOTE: if cosTheta is close to 1, inset/outset math will avoid the fast paths that rely
696 // on thefInvSinTheta since it will approach infinity.
697 fInvSinTheta = 1.f / sqrt(1.f - fCosTheta * fCosTheta);
698 }
699}
700
701//** EdgeEquations implementation
702
703void TessellationHelper::EdgeEquations::reset(const EdgeVectors& edgeVectors) {
704 float4 dx = edgeVectors.fDX;
705 float4 dy = edgeVectors.fDY;
706 // Correct for bad edges by copying adjacent edge information into the bad component
707 correct_bad_edges(edgeVectors.fInvLengths >= kInvDistTolerance, &dx, &dy, nullptr);
708
709 float4 c = dx*edgeVectors.fY2D - dy*edgeVectors.fX2D;
710 // Make sure normals point into the shape
711 float4 test = dy * next_cw(edgeVectors.fX2D) + (-dx * next_cw(edgeVectors.fY2D) + c);
712 if (any(test < -kDistTolerance)) {
713 fA = -dy;
714 fB = dx;
715 fC = -c;
716 } else {
717 fA = dy;
718 fB = -dx;
719 fC = c;
720 }
721}
722
723float4 TessellationHelper::EdgeEquations::estimateCoverage(const float4& x2d,
724 const float4& y2d) const {
725 // Calculate distance of the 4 inset points (px, py) to the 4 edges
726 float4 d0 = fA[0]*x2d + (fB[0]*y2d + fC[0]);
727 float4 d1 = fA[1]*x2d + (fB[1]*y2d + fC[1]);
728 float4 d2 = fA[2]*x2d + (fB[2]*y2d + fC[2]);
729 float4 d3 = fA[3]*x2d + (fB[3]*y2d + fC[3]);
730
731 // For each point, pretend that there's a rectangle that touches e0 and e3 on the horizontal
732 // axis, so its width is "approximately" d0 + d3, and it touches e1 and e2 on the vertical axis
733 // so its height is d1 + d2. Pin each of these dimensions to [0, 1] and approximate the coverage
734 // at each point as clamp(d0+d3, 0, 1) x clamp(d1+d2, 0, 1). For rectilinear quads this is an
735 // accurate calculation of its area clipped to an aligned pixel. For arbitrary quads it is not
736 // mathematically accurate but qualitatively provides a stable value proportional to the size of
737 // the shape.
738 float4 w = max(0.f, min(1.f, d0 + d3));
739 float4 h = max(0.f, min(1.f, d1 + d2));
740 return w * h;
741}
742
743bool TessellationHelper::EdgeEquations::isSubpixel(const float4& x2d, const float4& y2d) const {
744 // Compute the minimum distances from vertices to opposite edges. If all 4 minimum distances
745 // are less than 1px, then the inset geometry would be a point or line and quad rendering
746 // will switch to hairline mode.
747 float4 d = min(x2d * skvx::shuffle<1,2,1,2>(fA) + y2d * skvx::shuffle<1,2,1,2>(fB)
748 + skvx::shuffle<1,2,1,2>(fC),
749 x2d * skvx::shuffle<3,3,0,0>(fA) + y2d * skvx::shuffle<3,3,0,0>(fB)
750 + skvx::shuffle<3,3,0,0>(fC));
751 return all(d < 1.f);
752}
753
754int TessellationHelper::EdgeEquations::computeDegenerateQuad(const float4& signedEdgeDistances,
755 float4* x2d, float4* y2d,
756 mask4* aaMask) const {
757 // If the original points form a line in the 2D projection then give up on antialiasing.
758 for (int i = 0; i < 4; ++i) {
759 float4 d = (*x2d)*fA[i] + (*y2d)*fB[i] + fC[i];
760 if (all(abs(d) < kDistTolerance)) {
761 *aaMask = mask4(0);
762 return 4;
763 }
764 }
765
766 *aaMask = signedEdgeDistances != 0.f;
767
768 // Move the edge by the signed edge adjustment.
769 float4 oc = fC + signedEdgeDistances;
770
771 // There are 6 points that we care about to determine the final shape of the polygon, which
772 // are the intersections between (e0,e2), (e1,e0), (e2,e3), (e3,e1) (corresponding to the
773 // 4 corners), and (e1, e2), (e0, e3) (representing the intersections of opposite edges).
774 float4 denom = fA * next_cw(fB) - fB * next_cw(fA);
775 float4 px = (fB * next_cw(oc) - oc * next_cw(fB)) / denom;
776 float4 py = (oc * next_cw(fA) - fA * next_cw(oc)) / denom;
777 correct_bad_coords(abs(denom) < kTolerance, &px, &py, nullptr);
778
779 // Calculate the signed distances from these 4 corners to the other two edges that did not
780 // define the intersection. So p(0) is compared to e3,e1, p(1) to e3,e2 , p(2) to e0,e1, and
781 // p(3) to e0,e2
782 float4 dists1 = px * skvx::shuffle<3, 3, 0, 0>(fA) +
783 py * skvx::shuffle<3, 3, 0, 0>(fB) +
784 skvx::shuffle<3, 3, 0, 0>(oc);
785 float4 dists2 = px * skvx::shuffle<1, 2, 1, 2>(fA) +
786 py * skvx::shuffle<1, 2, 1, 2>(fB) +
787 skvx::shuffle<1, 2, 1, 2>(oc);
788
789 // If all the distances are >= 0, the 4 corners form a valid quadrilateral, so use them as
790 // the 4 points. If any point is on the wrong side of both edges, the interior has collapsed
791 // and we need to use a central point to represent it. If all four points are only on the
792 // wrong side of 1 edge, one edge has crossed over another and we use a line to represent it.
793 // Otherwise, use a triangle that replaces the bad points with the intersections of
794 // (e1, e2) or (e0, e3) as needed.
795 mask4 d1v0 = dists1 < kDistTolerance;
796 mask4 d2v0 = dists2 < kDistTolerance;
797 mask4 d1And2 = d1v0 & d2v0;
798 mask4 d1Or2 = d1v0 | d2v0;
799
800 if (!any(d1Or2)) {
801 // Every dists1 and dists2 >= kTolerance so it's not degenerate, use all 4 corners as-is
802 // and use full coverage
803 *x2d = px;
804 *y2d = py;
805 return 4;
806 } else if (any(d1And2)) {
807 // A point failed against two edges, so reduce the shape to a single point, which we take as
808 // the center of the original quad to ensure it is contained in the intended geometry. Since
809 // it has collapsed, we know the shape cannot cover a pixel so update the coverage.
810 SkPoint center = {0.25f * ((*x2d)[0] + (*x2d)[1] + (*x2d)[2] + (*x2d)[3]),
811 0.25f * ((*y2d)[0] + (*y2d)[1] + (*y2d)[2] + (*y2d)[3])};
812 *x2d = center.fX;
813 *y2d = center.fY;
814 *aaMask = any(*aaMask);
815 return 1;
816 } else if (all(d1Or2)) {
817 // Degenerates to a line. Compare p[2] and p[3] to edge 0. If they are on the wrong side,
818 // that means edge 0 and 3 crossed, and otherwise edge 1 and 2 crossed.
819 if (dists1[2] < kDistTolerance && dists1[3] < kDistTolerance) {
820 // Edges 0 and 3 have crossed over, so make the line from average of (p0,p2) and (p1,p3)
821 *x2d = 0.5f * (skvx::shuffle<0, 1, 0, 1>(px) + skvx::shuffle<2, 3, 2, 3>(px));
822 *y2d = 0.5f * (skvx::shuffle<0, 1, 0, 1>(py) + skvx::shuffle<2, 3, 2, 3>(py));
823 // If edges 0 and 3 crossed then one must have AA but we moved both 2D points on the
824 // edge so we need moveTo() to be able to move both 3D points along the shared edge. So
825 // ensure both have AA.
826 *aaMask = *aaMask | mask4({1, 0, 0, 1});
827 } else {
828 // Edges 1 and 2 have crossed over, so make the line from average of (p0,p1) and (p2,p3)
829 *x2d = 0.5f * (skvx::shuffle<0, 0, 2, 2>(px) + skvx::shuffle<1, 1, 3, 3>(px));
830 *y2d = 0.5f * (skvx::shuffle<0, 0, 2, 2>(py) + skvx::shuffle<1, 1, 3, 3>(py));
831 *aaMask = *aaMask | mask4({0, 1, 1, 0});
832 }
833 return 2;
834 } else {
835 // This turns into a triangle. Replace corners as needed with the intersections between
836 // (e0,e3) and (e1,e2), which must now be calculated. Because of kDistTolarance we can
837 // have cases where the intersection lies far outside the quad. For example, consider top
838 // and bottom edges that are nearly parallel and their intersections with the right edge are
839 // nearly but not quite swapped (top edge intersection is barely above bottom edge
840 // intersection). In this case we replace the point with the average of itself and the point
841 // calculated using the edge equation it failed (in the example case this would be the
842 // average of the points calculated by the top and bottom edges intersected with the right
843 // edge.)
844 using V2f = skvx::Vec<2, float>;
845 V2f eDenom = skvx::shuffle<0, 1>(fA) * skvx::shuffle<3, 2>(fB) -
846 skvx::shuffle<0, 1>(fB) * skvx::shuffle<3, 2>(fA);
847 V2f ex = (skvx::shuffle<0, 1>(fB) * skvx::shuffle<3, 2>(oc) -
848 skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(fB)) / eDenom;
849 V2f ey = (skvx::shuffle<0, 1>(oc) * skvx::shuffle<3, 2>(fA) -
850 skvx::shuffle<0, 1>(fA) * skvx::shuffle<3, 2>(oc)) / eDenom;
851
852 float4 avgX = 0.5f * (skvx::shuffle<0, 1, 0, 2>(px) + skvx::shuffle<2, 3, 1, 3>(px));
853 float4 avgY = 0.5f * (skvx::shuffle<0, 1, 0, 2>(py) + skvx::shuffle<2, 3, 1, 3>(py));
854 for (int i = 0; i < 4; ++i) {
855 // Note that we would not have taken this branch if any point failed both of its edges
856 // tests. That is, it can't be the case that d1v0[i] and d2v0[i] are both true.
857 if (dists1[i] < -kDistTolerance && std::abs(eDenom[0]) > kTolerance) {
858 px[i] = ex[0];
859 py[i] = ey[0];
860 } else if (d1v0[i]) {
861 px[i] = avgX[i % 2];
862 py[i] = avgY[i % 2];
863 } else if (dists2[i] < -kDistTolerance && std::abs(eDenom[1]) > kTolerance) {
864 px[i] = ex[1];
865 py[i] = ey[1];
866 } else if (d2v0[i]) {
867 px[i] = avgX[i / 2 + 2];
868 py[i] = avgY[i / 2 + 2];
869 }
870 }
871
872 // If we replace a vertex with an intersection then it will not fall along the
873 // edges that intersect at the original vertex. When we apply AA later to the
874 // original points we move along the original 3d edges to move towards the 2d
875 // points we're computing here. If we have an AA edge and a non-AA edge we
876 // can only move along 1 edge, but now the point we're moving toward isn't
877 // on that edge. Thus, we provide an additional degree of freedom by turning
878 // AA on for both edges if either edge is AA at each point.
879 *aaMask = *aaMask | (d1Or2 & next_cw(*aaMask)) | (next_ccw(d1Or2) & next_ccw(*aaMask));
880 *x2d = px;
881 *y2d = py;
882 return 3;
883 }
884}
885
886//** OutsetRequest implementation
887
888void TessellationHelper::OutsetRequest::reset(const EdgeVectors& edgeVectors, GrQuad::Type quadType,
889 const skvx::Vec<4, float>& edgeDistances) {
890 fEdgeDistances = edgeDistances;
891
892 // Based on the edge distances, determine if it's acceptable to use fInvSinTheta to
893 // calculate the inset or outset geometry.
894 if (quadType <= GrQuad::Type::kRectilinear) {
895 // Since it's rectangular, the width (edge[1] or edge[2]) collapses if subtracting
896 // (dist[0] + dist[3]) makes the new width negative (minus for inset, outsetting will
897 // never be degenerate in this case). The same applies for height (edge[0] or edge[3])
898 // and (dist[1] + dist[2]).
899 fOutsetDegenerate = false;
900 float widthChange = edgeDistances[0] + edgeDistances[3];
901 float heightChange = edgeDistances[1] + edgeDistances[2];
902 // (1/len > 1/(edge sum) implies len - edge sum < 0.
903 fInsetDegenerate =
904 (widthChange > 0.f && edgeVectors.fInvLengths[1] > 1.f / widthChange) ||
905 (heightChange > 0.f && edgeVectors.fInvLengths[0] > 1.f / heightChange);
906 } else if (any(edgeVectors.fInvLengths >= kInvDistTolerance)) {
907 // Have an edge that is effectively length 0, so we're dealing with a triangle, which
908 // must always go through the degenerate code path.
909 fOutsetDegenerate = true;
910 fInsetDegenerate = true;
911 } else {
912 // If possible, the corners will move +/-edgeDistances * 1/sin(theta). The entire
913 // request is degenerate if 1/sin(theta) -> infinity (or cos(theta) -> 1).
914 if (any(abs(edgeVectors.fCosTheta) >= 0.9f)) {
915 fOutsetDegenerate = true;
916 fInsetDegenerate = true;
917 } else {
918 // With an edge-centric view, an edge's length changes by
919 // edgeDistance * cos(pi - theta) / sin(theta) for each of its corners (the second
920 // corner uses ccw theta value). An edge's length also changes when its adjacent
921 // edges move, in which case it's updated by edgeDistance / sin(theta)
922 // (or cos(theta) for the other edge).
923
924 // cos(pi - theta) = -cos(theta)
925 float4 halfTanTheta = -edgeVectors.fCosTheta * edgeVectors.fInvSinTheta;
926 float4 edgeAdjust = edgeDistances * (halfTanTheta + next_ccw(halfTanTheta)) +
927 next_ccw(edgeDistances) * next_ccw(edgeVectors.fInvSinTheta) +
928 next_cw(edgeDistances) * edgeVectors.fInvSinTheta;
929
930 // If either outsetting (plus edgeAdjust) or insetting (minus edgeAdjust) make
931 // the edge lengths negative, then it's degenerate.
932 float4 threshold = 0.1f - (1.f / edgeVectors.fInvLengths);
933 fOutsetDegenerate = any(edgeAdjust < threshold);
934 fInsetDegenerate = any(edgeAdjust > -threshold);
935 }
936 }
937}
938
939//** Vertices implementation
940
941void TessellationHelper::Vertices::reset(const GrQuad& deviceQuad, const GrQuad* localQuad) {
942 // Set vertices to match the device and local quad
943 fX = deviceQuad.x4f();
944 fY = deviceQuad.y4f();
945 fW = deviceQuad.w4f();
946
947 if (localQuad) {
948 fU = localQuad->x4f();
949 fV = localQuad->y4f();
950 fR = localQuad->w4f();
951 fUVRCount = localQuad->hasPerspective() ? 3 : 2;
952 } else {
953 fUVRCount = 0;
954 }
955}
956
957void TessellationHelper::Vertices::asGrQuads(GrQuad* deviceOut, GrQuad::Type deviceType,
958 GrQuad* localOut, GrQuad::Type localType) const {
959 SkASSERT(deviceOut);
960 SkASSERT(fUVRCount == 0 || localOut);
961
962 fX.store(deviceOut->xs());
963 fY.store(deviceOut->ys());
964 if (deviceType == GrQuad::Type::kPerspective) {
965 fW.store(deviceOut->ws());
966 }
967 deviceOut->setQuadType(deviceType); // This sets ws == 1 when device type != perspective
968
969 if (fUVRCount > 0) {
970 fU.store(localOut->xs());
971 fV.store(localOut->ys());
972 if (fUVRCount == 3) {
973 fR.store(localOut->ws());
974 }
975 localOut->setQuadType(localType);
976 }
977}
978
979void TessellationHelper::Vertices::moveAlong(const EdgeVectors& edgeVectors,
980 const float4& signedEdgeDistances) {
981 // This shouldn't be called if fInvSinTheta is close to infinity (cosTheta close to 1).
982 // FIXME (michaelludwig) - Temporarily allow NaNs on debug builds here, for crbug:224618's GM
983 // Once W clipping is implemented, shouldn't see NaNs unless it's actually time to fail.
984 SkASSERT(all(abs(edgeVectors.fCosTheta) < 0.9f) ||
985 any(edgeVectors.fCosTheta != edgeVectors.fCosTheta));
986
987 // When the projected device quad is not degenerate, the vertex corners can move
988 // cornerOutsetLen along their edge and their cw-rotated edge. The vertex's edge points
989 // inwards and the cw-rotated edge points outwards, hence the minus-sign.
990 // The edge distances are rotated compared to the corner outsets and (dx, dy), since if
991 // the edge is "on" both its corners need to be moved along their other edge vectors.
992 float4 signedOutsets = -edgeVectors.fInvSinTheta * next_cw(signedEdgeDistances);
993 float4 signedOutsetsCW = edgeVectors.fInvSinTheta * signedEdgeDistances;
994
995 // x = x + outset * mask * next_cw(xdiff) - outset * next_cw(mask) * xdiff
996 fX += signedOutsetsCW * next_cw(edgeVectors.fDX) + signedOutsets * edgeVectors.fDX;
997 fY += signedOutsetsCW * next_cw(edgeVectors.fDY) + signedOutsets * edgeVectors.fDY;
998 if (fUVRCount > 0) {
999 // We want to extend the texture coords by the same proportion as the positions.
1000 signedOutsets *= edgeVectors.fInvLengths;
1001 signedOutsetsCW *= next_cw(edgeVectors.fInvLengths);
1002 float4 du = next_ccw(fU) - fU;
1003 float4 dv = next_ccw(fV) - fV;
1004 fU += signedOutsetsCW * next_cw(du) + signedOutsets * du;
1005 fV += signedOutsetsCW * next_cw(dv) + signedOutsets * dv;
1006 if (fUVRCount == 3) {
1007 float4 dr = next_ccw(fR) - fR;
1008 fR += signedOutsetsCW * next_cw(dr) + signedOutsets * dr;
1009 }
1010 }
1011}
1012
1013void TessellationHelper::Vertices::moveTo(const float4& x2d, const float4& y2d, const mask4& mask) {
1014 // Left to right, in device space, for each point
1015 float4 e1x = skvx::shuffle<2, 3, 2, 3>(fX) - skvx::shuffle<0, 1, 0, 1>(fX);
1016 float4 e1y = skvx::shuffle<2, 3, 2, 3>(fY) - skvx::shuffle<0, 1, 0, 1>(fY);
1017 float4 e1w = skvx::shuffle<2, 3, 2, 3>(fW) - skvx::shuffle<0, 1, 0, 1>(fW);
1018 mask4 e1Bad = e1x*e1x + e1y*e1y < kDist2Tolerance;
1019 correct_bad_edges(e1Bad, &e1x, &e1y, &e1w);
1020
1021 // // Top to bottom, in device space, for each point
1022 float4 e2x = skvx::shuffle<1, 1, 3, 3>(fX) - skvx::shuffle<0, 0, 2, 2>(fX);
1023 float4 e2y = skvx::shuffle<1, 1, 3, 3>(fY) - skvx::shuffle<0, 0, 2, 2>(fY);
1024 float4 e2w = skvx::shuffle<1, 1, 3, 3>(fW) - skvx::shuffle<0, 0, 2, 2>(fW);
1025 mask4 e2Bad = e2x*e2x + e2y*e2y < kDist2Tolerance;
1026 correct_bad_edges(e2Bad, &e2x, &e2y, &e2w);
1027
1028 // Can only move along e1 and e2 to reach the new 2D point, so we have
1029 // x2d = (x + a*e1x + b*e2x) / (w + a*e1w + b*e2w) and
1030 // y2d = (y + a*e1y + b*e2y) / (w + a*e1w + b*e2w) for some a, b
1031 // This can be rewritten to a*c1x + b*c2x + c3x = 0; a * c1y + b*c2y + c3y = 0, where
1032 // the cNx and cNy coefficients are:
1033 float4 c1x = e1w * x2d - e1x;
1034 float4 c1y = e1w * y2d - e1y;
1035 float4 c2x = e2w * x2d - e2x;
1036 float4 c2y = e2w * y2d - e2y;
1037 float4 c3x = fW * x2d - fX;
1038 float4 c3y = fW * y2d - fY;
1039
1040 // Solve for a and b
1041 float4 a, b, denom;
1042 if (all(mask)) {
1043 // When every edge is outset/inset, each corner can use both edge vectors
1044 denom = c1x * c2y - c2x * c1y;
1045 a = (c2x * c3y - c3x * c2y) / denom;
1046 b = (c3x * c1y - c1x * c3y) / denom;
1047 } else {
1048 // Force a or b to be 0 if that edge cannot be used due to non-AA
1049 mask4 aMask = skvx::shuffle<0, 0, 3, 3>(mask);
1050 mask4 bMask = skvx::shuffle<2, 1, 2, 1>(mask);
1051
1052 // When aMask[i]&bMask[i], then a[i], b[i], denom[i] match the kAll case.
1053 // When aMask[i]&!bMask[i], then b[i] = 0, a[i] = -c3x/c1x or -c3y/c1y, using better denom
1054 // When !aMask[i]&bMask[i], then a[i] = 0, b[i] = -c3x/c2x or -c3y/c2y, ""
1055 // When !aMask[i]&!bMask[i], then both a[i] = 0 and b[i] = 0
1056 mask4 useC1x = abs(c1x) > abs(c1y);
1057 mask4 useC2x = abs(c2x) > abs(c2y);
1058
1059 denom = if_then_else(aMask,
1060 if_then_else(bMask,
1061 c1x * c2y - c2x * c1y, /* A & B */
1062 if_then_else(useC1x, c1x, c1y)), /* A & !B */
1063 if_then_else(bMask,
1064 if_then_else(useC2x, c2x, c2y), /* !A & B */
1065 float4(1.f))); /* !A & !B */
1066
1067 a = if_then_else(aMask,
1068 if_then_else(bMask,
1069 c2x * c3y - c3x * c2y, /* A & B */
1070 if_then_else(useC1x, -c3x, -c3y)), /* A & !B */
1071 float4(0.f)) / denom; /* !A */
1072 b = if_then_else(bMask,
1073 if_then_else(aMask,
1074 c3x * c1y - c1x * c3y, /* A & B */
1075 if_then_else(useC2x, -c3x, -c3y)), /* !A & B */
1076 float4(0.f)) / denom; /* !B */
1077 }
1078
1079 fX += a * e1x + b * e2x;
1080 fY += a * e1y + b * e2y;
1081 fW += a * e1w + b * e2w;
1082
1083 // If fW has gone negative, flip the point to the other side of w=0. This only happens if the
1084 // edge was approaching a vanishing point and it was physically impossible to outset 1/2px in
1085 // screen space w/o going behind the viewer and being mirrored. Scaling by -1 preserves the
1086 // computed screen space position but moves the 3D point off of the original quad. So far, this
1087 // seems to be a reasonable compromise.
1088 if (any(fW < 0.f)) {
1089 float4 scale = if_then_else(fW < 0.f, float4(-1.f), float4(1.f));
1090 fX *= scale;
1091 fY *= scale;
1092 fW *= scale;
1093 }
1094
1095 correct_bad_coords(abs(denom) < kTolerance, &fX, &fY, &fW);
1096
1097 if (fUVRCount > 0) {
1098 // Calculate R here so it can be corrected with U and V in case it's needed later
1099 float4 e1u = skvx::shuffle<2, 3, 2, 3>(fU) - skvx::shuffle<0, 1, 0, 1>(fU);
1100 float4 e1v = skvx::shuffle<2, 3, 2, 3>(fV) - skvx::shuffle<0, 1, 0, 1>(fV);
1101 float4 e1r = skvx::shuffle<2, 3, 2, 3>(fR) - skvx::shuffle<0, 1, 0, 1>(fR);
1102 correct_bad_edges(e1Bad, &e1u, &e1v, &e1r);
1103
1104 float4 e2u = skvx::shuffle<1, 1, 3, 3>(fU) - skvx::shuffle<0, 0, 2, 2>(fU);
1105 float4 e2v = skvx::shuffle<1, 1, 3, 3>(fV) - skvx::shuffle<0, 0, 2, 2>(fV);
1106 float4 e2r = skvx::shuffle<1, 1, 3, 3>(fR) - skvx::shuffle<0, 0, 2, 2>(fR);
1107 correct_bad_edges(e2Bad, &e2u, &e2v, &e2r);
1108
1109 fU += a * e1u + b * e2u;
1110 fV += a * e1v + b * e2v;
1111 if (fUVRCount == 3) {
1112 fR += a * e1r + b * e2r;
1113 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, &fR);
1114 } else {
1115 correct_bad_coords(abs(denom) < kTolerance, &fU, &fV, nullptr);
1116 }
1117 }
1118}
1119
1120//** TessellationHelper implementation
1121
1122void TessellationHelper::reset(const GrQuad& deviceQuad, const GrQuad* localQuad) {
1123 // Record basic state that isn't recorded on the Vertices struct itself
1124 fDeviceType = deviceQuad.quadType();
1125 fLocalType = localQuad ? localQuad->quadType() : GrQuad::Type::kAxisAligned;
1126
1127 // Reset metadata validity
1128 fOutsetRequestValid = false;
1129 fEdgeEquationsValid = false;
1130
1131 // Compute vertex properties that are always needed for a quad, so no point in doing it lazily.
1132 fOriginal.reset(deviceQuad, localQuad);
1133 fEdgeVectors.reset(fOriginal.fX, fOriginal.fY, fOriginal.fW, fDeviceType);
1134
1135 fVerticesValid = true;
1136}
1137
1139 GrQuad* deviceInset, GrQuad* localInset) {
1140 SkASSERT(fVerticesValid);
1141
1142 Vertices inset = fOriginal;
1143 const OutsetRequest& request = this->getOutsetRequest(edgeDistances);
1144 int vertexCount;
1145 if (request.fInsetDegenerate) {
1146 vertexCount = this->adjustDegenerateVertices(-request.fEdgeDistances, &inset);
1147 } else {
1148 this->adjustVertices(-request.fEdgeDistances, &inset);
1149 vertexCount = 4;
1150 }
1151
1152 inset.asGrQuads(deviceInset, fDeviceType, localInset, fLocalType);
1153 if (vertexCount < 3) {
1154 // The interior has less than a full pixel's area so estimate reduced coverage using
1155 // the distance of the inset's projected corners to the original edges.
1156 return this->getEdgeEquations().estimateCoverage(inset.fX / inset.fW,
1157 inset.fY / inset.fW);
1158 } else {
1159 return 1.f;
1160 }
1161}
1162
1164 GrQuad* deviceOutset, GrQuad* localOutset) {
1165 SkASSERT(fVerticesValid);
1166
1167 Vertices outset = fOriginal;
1168 const OutsetRequest& request = this->getOutsetRequest(edgeDistances);
1169 if (request.fOutsetDegenerate) {
1170 this->adjustDegenerateVertices(request.fEdgeDistances, &outset);
1171 } else {
1172 this->adjustVertices(request.fEdgeDistances, &outset);
1173 }
1174
1175 outset.asGrQuads(deviceOutset, fDeviceType, localOutset, fLocalType);
1176}
1177
1178void TessellationHelper::getEdgeEquations(skvx::Vec<4, float>* a,
1181 SkASSERT(a && b && c);
1182 SkASSERT(fVerticesValid);
1183 const EdgeEquations& eq = this->getEdgeEquations();
1184 *a = eq.fA;
1185 *b = eq.fB;
1186 *c = eq.fC;
1187}
1188
1190 SkASSERT(fVerticesValid);
1191 return 1.f / fEdgeVectors.fInvLengths;
1192}
1193
1194const TessellationHelper::OutsetRequest& TessellationHelper::getOutsetRequest(
1195 const skvx::Vec<4, float>& edgeDistances) {
1196 // Much of the code assumes that we start from positive distances and apply it unmodified to
1197 // create an outset; knowing that it's outset simplifies degeneracy checking.
1198 SkASSERT(all(edgeDistances >= 0.f));
1199
1200 // Rebuild outset request if invalid or if the edge distances have changed.
1201 if (!fOutsetRequestValid || any(edgeDistances != fOutsetRequest.fEdgeDistances)) {
1202 fOutsetRequest.reset(fEdgeVectors, fDeviceType, edgeDistances);
1203 fOutsetRequestValid = true;
1204 }
1205 return fOutsetRequest;
1206}
1207
1209 SkASSERT(fVerticesValid);
1210 if (fDeviceType <= GrQuad::Type::kRectilinear) {
1211 // Check the edge lengths, if the shortest is less than 1px it's degenerate, which is the
1212 // same as if the max 1/length is greater than 1px.
1213 return any(fEdgeVectors.fInvLengths > 1.f);
1214 } else {
1215 // Compute edge equations and then distance from each vertex to the opposite edges.
1216 return this->getEdgeEquations().isSubpixel(fEdgeVectors.fX2D, fEdgeVectors.fY2D);
1217 }
1218}
1219
1220const TessellationHelper::EdgeEquations& TessellationHelper::getEdgeEquations() {
1221 if (!fEdgeEquationsValid) {
1222 fEdgeEquations.reset(fEdgeVectors);
1223 fEdgeEquationsValid = true;
1224 }
1225 return fEdgeEquations;
1226}
1227
1228void TessellationHelper::adjustVertices(const skvx::Vec<4, float>& signedEdgeDistances,
1229 Vertices* vertices) {
1230 SkASSERT(vertices);
1231 SkASSERT(vertices->fUVRCount == 0 || vertices->fUVRCount == 2 || vertices->fUVRCount == 3);
1232
1233 if (fDeviceType < GrQuad::Type::kPerspective) {
1234 // For non-perspective, non-degenerate quads, moveAlong is correct and most efficient
1235 vertices->moveAlong(fEdgeVectors, signedEdgeDistances);
1236 } else {
1237 // For perspective, non-degenerate quads, use moveAlong for the projected points and then
1238 // reconstruct Ws with moveTo.
1239 Vertices projected = { fEdgeVectors.fX2D, fEdgeVectors.fY2D, /*w*/ 1.f, 0.f, 0.f, 0.f, 0 };
1240 projected.moveAlong(fEdgeVectors, signedEdgeDistances);
1241 vertices->moveTo(projected.fX, projected.fY, signedEdgeDistances != 0.f);
1242 }
1243}
1244
1245int TessellationHelper::adjustDegenerateVertices(const skvx::Vec<4, float>& signedEdgeDistances,
1246 Vertices* vertices) {
1247 SkASSERT(vertices);
1248 SkASSERT(vertices->fUVRCount == 0 || vertices->fUVRCount == 2 || vertices->fUVRCount == 3);
1249
1250 if (fDeviceType <= GrQuad::Type::kRectilinear) {
1251 // For rectilinear, degenerate quads, can use moveAlong if the edge distances are adjusted
1252 // to not cross over each other.
1253 SkASSERT(all(signedEdgeDistances <= 0.f)); // Only way rectilinear can degenerate is insets
1254 float4 halfLengths = -0.5f / next_cw(fEdgeVectors.fInvLengths); // Negate to inset
1255 mask4 crossedEdges = halfLengths > signedEdgeDistances;
1256 float4 safeInsets = if_then_else(crossedEdges, halfLengths, signedEdgeDistances);
1257 vertices->moveAlong(fEdgeVectors, safeInsets);
1258
1259 // A degenerate rectilinear quad is either a point (both w and h crossed), or a line
1260 return all(crossedEdges) ? 1 : 2;
1261 } else {
1262 // Degenerate non-rectangular shape, must go through slowest path (which automatically
1263 // handles perspective).
1264 float4 x2d = fEdgeVectors.fX2D;
1265 float4 y2d = fEdgeVectors.fY2D;
1266
1267 mask4 aaMask;
1268 int vertexCount = this->getEdgeEquations().computeDegenerateQuad(signedEdgeDistances,
1269 &x2d, &y2d, &aaMask);
1270 vertices->moveTo(x2d, y2d, aaMask);
1271 return vertexCount;
1272 }
1273}
1274
1275} // namespace GrQuadUtils
m reset()
static GrQuadAAFlags crop_rect(const SkRect &clipDevRect, float x[4], float y[4], float lx[4], float ly[4], float lw[4])
static void interpolate_local(float alpha, int v0, int v1, int v2, int v3, float lx[4], float ly[4], float lw[4])
Definition: GrQuadUtils.cpp:78
static bool barycentric_coords(float x0, float y0, float x1, float y1, float x2, float y2, const float4 &testX, const float4 &testY, float4 *u, float4 *v, float4 *w)
static AI void correct_bad_edges(const mask4 &bad, float4 *e1, float4 *e2, float4 *e3)
Definition: GrQuadUtils.cpp:54
static constexpr float kDistTolerance
Definition: GrQuadUtils.cpp:31
static constexpr float kDist2Tolerance
Definition: GrQuadUtils.cpp:32
static AI skvx::Vec< 4, T > next_ccw(const skvx::Vec< 4, T > &v)
Definition: GrQuadUtils.cpp:43
static constexpr float kTolerance
Definition: GrQuadUtils.cpp:29
static bool crop_rect_edge(const SkRect &clipDevRect, int v0, int v1, int v2, int v3, float x[4], float y[4], float lx[4], float ly[4], float lw[4])
Definition: GrQuadUtils.cpp:98
static GrQuadAAFlags crop_simple_rect(const SkRect &clipDevRect, float x[4], float y[4], float lx[4], float ly[4])
static mask4 inside_triangle(const float4 &u, const float4 &v, const float4 &w)
static AI void correct_bad_coords(const mask4 &bad, float4 *c1, float4 *c2, float4 *c3)
Definition: GrQuadUtils.cpp:66
static AI skvx::Vec< 4, T > next_cw(const skvx::Vec< 4, T > &v)
Definition: GrQuadUtils.cpp:38
static AI float4 next_diag(const float4 &v)
Definition: GrQuadUtils.cpp:47
skvx::float4 float4
Definition: GrQuadUtils.cpp:23
#define AI
Definition: GrQuadUtils.cpp:26
skvx::int4 mask4
Definition: GrQuadUtils.cpp:24
static bool is_simple_rect(const GrQuad &quad)
static constexpr float kInvDistTolerance
Definition: GrQuadUtils.cpp:33
GrQuadAAFlags
Definition: GrTypesPriv.h:247
GrAAType
Definition: GrTypesPriv.h:200
GrAA
Definition: GrTypesPriv.h:173
static bool eq(const SkM44 &a, const SkM44 &b, float tol)
Definition: M44Test.cpp:18
float e1
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr float sk_ieee_float_divide(float numer, float denom)
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:101
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:107
#define SK_Scalar1
Definition: SkScalar.h:18
#define SK_ScalarNearlyZero
Definition: SkScalar.h:99
#define SK_ScalarInfinity
Definition: SkScalar.h:26
#define SK_ScalarNegativeInfinity
Definition: SkScalar.h:27
SI T if_then_else(C cond, T t, T e)
Vec2Value v2
void getEdgeEquations(skvx::float4 *a, skvx::float4 *b, skvx::float4 *c)
void reset(const GrQuad &deviceQuad, const GrQuad *localQuad)
void outset(const skvx::float4 &edgeDistances, GrQuad *deviceOutset, GrQuad *localOutset)
skvx::float4 inset(const skvx::float4 &edgeDistances, GrQuad *deviceInset, GrQuad *localInset)
Definition: GrQuad.h:30
const float * xs() const
Definition: GrQuad.h:132
const float * ys() const
Definition: GrQuad.h:134
float y(int i) const
Definition: GrQuad.h:109
Type quadType() const
Definition: GrQuad.h:118
skvx::Vec< 4, float > y4f() const
Definition: GrQuad.h:114
bool aaHasEffectOnRect(GrQuadAAFlags edgeFlags) const
Definition: GrQuad.cpp:135
bool hasPerspective() const
Definition: GrQuad.h:120
float x(int i) const
Definition: GrQuad.h:108
skvx::Vec< 4, float > x4f() const
Definition: GrQuad.h:113
void setQuadType(Type newType)
Definition: GrQuad.h:140
bool isFinite() const
Definition: GrQuad.h:95
Type
Definition: GrQuad.h:35
const float * ws() const
Definition: GrQuad.h:136
skvx::Vec< 4, float > w4f() const
Definition: GrQuad.h:115
static constexpr SkScalar kW0PlaneDistance
Definition: SkPathPriv.h:41
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
bool WillUseHairline(const GrQuad &quad, GrAAType aaType, GrQuadAAFlags edgeFlags)
bool CropToRect(const SkRect &cropRect, GrAA cropAA, DrawQuad *quad, bool computeLocal)
void ResolveAAType(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags, const GrQuad &quad, GrAAType *outAAType, GrQuadAAFlags *outEdgeFlags)
int ClipToW0(DrawQuad *quad, DrawQuad *extraVertices)
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
SIN Vec< N, float > abs(const Vec< N, float > &x)
Definition: SkVx.h:707
Vec< 4, float > float4
Definition: SkVx.h:1146
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition: SkVx.h:706
Vec< 4, int32_t > int4
Definition: SkVx.h:1159
SIT bool all(const Vec< 1, T > &x)
Definition: SkVx.h:582
SIT bool any(const Vec< 1, T > &x)
Definition: SkVx.h:530
SkScalar w
SkScalar h
const Scalar scale
GrQuad fLocal
Definition: GrQuad.h:186
GrQuad fDevice
Definition: GrQuad.h:185
GrQuadAAFlags fEdgeFlags
Definition: GrQuad.h:187
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15
Definition: SkVx.h:83
SKVX_ALWAYS_INLINE void store(void *ptr) const
Definition: SkVx.h:112