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 #ifndef GrQuadUtils_DEFINED
9 #define GrQuadUtils_DEFINED
10
11 #include "include/private/SkVx.h"
12 #include "src/gpu/geometry/GrQuad.h"
13
14 enum class GrQuadAAFlags;
15 enum class GrAA : bool;
16 enum class GrAAType : unsigned;
17 struct SkRect;
18
19 namespace GrQuadUtils {
20
21 // Resolve disagreements between the overall requested AA type and the per-edge quad AA flags.
22 // Both outAAType and outEdgeFlags will be updated.
23 void ResolveAAType(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags,
24 const GrQuad& quad, GrAAType* outAAtype, GrQuadAAFlags* outEdgeFlags);
25
26 /**
27 * Clip the device vertices of 'quad' to be in front of the W = 0 plane (w/in epsilon). The
28 * local coordinates will be updated to match the new clipped vertices. This returns the number
29 * of clipped quads that need to be drawn: 0 if 'quad' was entirely behind the plane, 1 if
30 * 'quad' did not need to be clipped or if 2 or 3 vertices were clipped, or 2 if 'quad' had one
31 * vertex clipped (producing a pentagonal shape spanned by 'quad' and 'extraVertices').
32 */
33 int ClipToW0(DrawQuad* quad, DrawQuad* extraVertices);
34
35 /**
36 * Crops quad to the provided device-space axis-aligned rectangle. If the intersection of this
37 * quad (projected) and cropRect results in a quadrilateral, this returns true. If not, this
38 * quad may be updated to be a smaller quad of the same type such that its intersection with
39 * cropRect is visually the same. This function assumes that the 'quad' coordinates are finite.
40 *
41 * The provided edge flags are updated to reflect edges clipped by cropRect (toggling on or off
42 * based on cropAA policy). If provided, the local coordinates will be updated to reflect the
43 * updated device coordinates of this quad.
44 *
45 * If 'computeLocal' is false, the local coordinates in 'quad' will not be modified.
46 */
47 bool CropToRect(const SkRect& cropRect, GrAA cropAA, DrawQuad* quad, bool computeLocal=true);
48
49 inline void Outset(const skvx::Vec<4, float>& edgeDistances, GrQuad* quad);
50
51 class TessellationHelper {
52 public:
53 // Set the original device and (optional) local coordinates that are inset or outset
54 // by the requested edge distances. Use nullptr if there are no local coordinates to update.
55 // This assumes all device coordinates have been clipped to W > 0.
56 void reset(const GrQuad& deviceQuad, const GrQuad* localQuad);
57
58 // Calculates a new quadrilateral with edges parallel to the original except that they
59 // have been moved inwards by edgeDistances (which should be positive). Distances are
60 // ordered L, B, T, R to match CCW tristrip ordering of GrQuad vertices. Edges that are
61 // not moved (i.e. distance == 0) will not be used in calculations and the corners will
62 // remain on that edge.
63 //
64 // The per-vertex coverage will be returned. When the inset geometry does not collapse to
65 // a point or line, this will be 1.0 for every vertex. When it does collapse, the per-vertex
66 // coverages represent estimated pixel coverage to simulate drawing the subpixel-sized
67 // original quad.
68 //
69 // Note: the edge distances are in device pixel units, so after rendering the new quad
70 // edge's shortest distance to the original quad's edge would be equal to provided edge dist
71 skvx::Vec<4, float> inset(const skvx::Vec<4, float>& edgeDistances,
72 GrQuad* deviceInset, GrQuad* localInset);
73
74 // Calculates a new quadrilateral that outsets the original edges by the given distances.
75 // Other than moving edges outwards, this function is equivalent to inset(). If the exact
76 // same edge distances are provided, certain internal computations can be reused across
77 // consecutive calls to inset() and outset() (in any order).
78 void outset(const skvx::Vec<4, float>& edgeDistances,
79 GrQuad* deviceOutset, GrQuad* localOutset);
80
81 // Compute the edge equations of the original device space quad passed to 'reset()'. The
82 // coefficients are stored per-edge in 'a', 'b', and 'c', such that ax + by + c = 0, and
83 // a positive distance indicates the interior of the quad. Edges are ordered L, B, T, R,
84 // matching edge distances passed to inset() and outset().
85 void getEdgeEquations(skvx::Vec<4, float>* a,
86 skvx::Vec<4, float>* b,
87 skvx::Vec<4, float>* c);
88
89 // Compute the edge lengths of the original device space quad passed to 'reset()'. The
90 // edge lengths are ordered LBTR to match distances passed to inset() and outset().
91 skvx::Vec<4, float> getEdgeLengths();
92
93 private:
94 // NOTE: This struct is named 'EdgeVectors' because it holds a lot of cached calculations
95 // pertaining to the edge vectors of the input quad, projected into 2D device coordinates.
96 // While they are not direction vectors, this struct represents a convenient storage space
97 // for the projected corners of the quad.
98 struct EdgeVectors {
99 // Projected corners (x/w and y/w); these are the 2D coordinates that determine the
100 // actual edge direction vectors, dx, dy, and invLengths
101 skvx::Vec<4, float> fX2D, fY2D;
102 // Normalized edge vectors of the device space quad, ordered L, B, T, R
103 // (i.e. next_ccw(x) - x).
104 skvx::Vec<4, float> fDX, fDY;
105 // Reciprocal of edge length of the device space quad, i.e. 1 / sqrt(dx*dx + dy*dy)
106 skvx::Vec<4, float> fInvLengths;
107 // Theta represents the angle formed by the two edges connected at each corner.
108 skvx::Vec<4, float> fCosTheta;
109 skvx::Vec<4, float> fInvSinTheta; // 1 / sin(theta)
110
111 void reset(const skvx::Vec<4, float>& xs, const skvx::Vec<4, float>& ys,
112 const skvx::Vec<4, float>& ws, GrQuad::Type quadType);
113 };
114
115 struct EdgeEquations {
116 // a * x + b * y + c = 0; positive distance is inside the quad; ordered LBTR.
117 skvx::Vec<4, float> fA, fB, fC;
118
119 void reset(const EdgeVectors& edgeVectors);
120
121 skvx::Vec<4, float> estimateCoverage(const skvx::Vec<4, float>& x2d,
122 const skvx::Vec<4, float>& y2d) const;
123
124 // Outsets or insets 'x2d' and 'y2d' in place. To be used when the interior is very
125 // small, edges are near parallel, or edges are very short/zero-length. Returns number
126 // of effective vertices in the degenerate quad.
127 int computeDegenerateQuad(const skvx::Vec<4, float>& signedEdgeDistances,
128 skvx::Vec<4, float>* x2d, skvx::Vec<4, float>* y2d,
129 skvx::Vec<4, int32_t>* aaMask) const;
130 };
131
132 struct OutsetRequest {
133 // Positive edge distances to move each edge of the quad. These distances represent the
134 // shortest (perpendicular) distance between the original edge and the inset or outset
135 // edge. If the distance is 0, then the edge will not move.
136 skvx::Vec<4, float> fEdgeDistances;
137 // True if the new corners cannot be calculated by simply adding scaled edge vectors.
138 // The quad may be degenerate because of the original geometry (near colinear edges), or
139 // be because of the requested edge distances (collapse of inset, etc.)
140 bool fInsetDegenerate;
141 bool fOutsetDegenerate;
142
143 void reset(const EdgeVectors& edgeVectors, GrQuad::Type quadType,
144 const skvx::Vec<4, float>& edgeDistances);
145 };
146
147 struct Vertices {
148 // X, Y, and W coordinates in device space. If not perspective, w should be set to 1.f
149 skvx::Vec<4, float> fX, fY, fW;
150 // U, V, and R coordinates representing local quad.
151 // Ignored depending on uvrCount (0, 1, 2).
152 skvx::Vec<4, float> fU, fV, fR;
153 int fUVRCount;
154
155 void reset(const GrQuad& deviceQuad, const GrQuad* localQuad);
156
157 void asGrQuads(GrQuad* deviceOut, GrQuad::Type deviceType,
158 GrQuad* localOut, GrQuad::Type localType) const;
159
160 // Update the device and optional local coordinates by moving the corners along their
161 // edge vectors such that the new edges have moved 'signedEdgeDistances' from their
162 // original lines. This should only be called if the 'edgeVectors' fInvSinTheta data is
163 // numerically sound.
164 void moveAlong(const EdgeVectors& edgeVectors,
165 const skvx::Vec<4, float>& signedEdgeDistances);
166
167 // Update the device coordinates by deriving (x,y,w) that project to (x2d, y2d), with
168 // optional local coordinates updated to match the new vertices. It is assumed that
169 // 'mask' was respected when determining (x2d, y2d), but it is used to ensure that only
170 // unmasked unprojected edge vectors are used when computing device and local coords.
171 void moveTo(const skvx::Vec<4, float>& x2d,
172 const skvx::Vec<4, float>& y2d,
173 const skvx::Vec<4, int32_t>& mask);
174 };
175
176 Vertices fOriginal;
177 EdgeVectors fEdgeVectors;
178 GrQuad::Type fDeviceType;
179 GrQuad::Type fLocalType;
180
181 // Lazily computed as needed; use accessor functions instead of direct access.
182 OutsetRequest fOutsetRequest;
183 EdgeEquations fEdgeEquations;
184
185 // Validity of Vertices/EdgeVectors (always true after first call to set()).
186 bool fVerticesValid = false;
187 // Validity of outset request (true after calling getOutsetRequest() until next set() call
188 // or next inset/outset() with different edge distances).
189 bool fOutsetRequestValid = false;
190 // Validity of edge equations (true after calling getEdgeEquations() until next set() call).
191 bool fEdgeEquationsValid = false;
192
193 // The requested edge distances must be positive so that they can be reused between inset
194 // and outset calls.
195 const OutsetRequest& getOutsetRequest(const skvx::Vec<4, float>& edgeDistances);
196 const EdgeEquations& getEdgeEquations();
197
198 // Outsets or insets 'vertices' by the given perpendicular 'signedEdgeDistances' (inset or
199 // outset is determined implicitly by the sign of the distances).
200 void adjustVertices(const skvx::Vec<4, float>& signedEdgeDistances, Vertices* vertices);
201 // Like adjustVertices() but handles empty edges, collapsed quads, numerical issues, and
202 // returns the number of effective vertices in the adjusted shape.
203 int adjustDegenerateVertices(const skvx::Vec<4, float>& signedEdgeDistances,
204 Vertices* vertices);
205
206 friend int ClipToW0(DrawQuad*, DrawQuad*); // To reuse Vertices struct
207 };
208
209 }; // namespace GrQuadUtils
210
Outset(const skvx::Vec<4,float> & edgeDistances,GrQuad * quad)211 void GrQuadUtils::Outset(const skvx::Vec<4, float>& edgeDistances, GrQuad* quad) {
212 TessellationHelper outsetter;
213 outsetter.reset(*quad, nullptr);
214 outsetter.outset(edgeDistances, quad, nullptr);
215 }
216
217 #endif
218