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