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