• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
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 "GrQuad.h"
9 
10 #include "GrTypesPriv.h"
11 
12 ///////////////////////////////////////////////////////////////////////////////////////////////////
13 // Functions for identifying the quad type from its coordinates, which are kept debug-only since
14 // production code should rely on the matrix to derive the quad type more efficiently. These are
15 // useful in asserts that the quad type is as expected.
16 ///////////////////////////////////////////////////////////////////////////////////////////////////
17 
18 #ifdef SK_DEBUG
19 // Allow some tolerance from floating point matrix transformations, but SkScalarNearlyEqual doesn't
20 // support comparing infinity, and coords_form_rect should return true for infinite edges
21 #define NEARLY_EQUAL(f1, f2) (f1 == f2 || SkScalarNearlyEqual(f1, f2, 1e-5f))
22 // Similarly, support infinite rectangles by looking at the sign of infinities
dot_nearly_zero(const SkVector & e1,const SkVector & e2)23 static bool dot_nearly_zero(const SkVector& e1, const SkVector& e2) {
24     static constexpr auto dot = SkPoint::DotProduct;
25     static constexpr auto sign = SkScalarSignAsScalar;
26 
27     SkScalar dotValue = dot(e1, e2);
28     if (SkScalarIsNaN(dotValue)) {
29         // Form vectors from the signs of infinities, and check their dot product
30         dotValue = dot({sign(e1.fX), sign(e1.fY)}, {sign(e2.fX), sign(e2.fY)});
31     }
32 
33     return SkScalarNearlyZero(dotValue, 1e-3f);
34 }
35 
36 // This is not the most performance critical function; code using GrQuad should rely on the faster
37 // quad type from matrix path, so this will only be called as part of SkASSERT.
coords_form_rect(const float xs[4],const float ys[4])38 static bool coords_form_rect(const float xs[4], const float ys[4]) {
39     return (NEARLY_EQUAL(xs[0], xs[1]) && NEARLY_EQUAL(xs[2], xs[3]) &&
40             NEARLY_EQUAL(ys[0], ys[2]) && NEARLY_EQUAL(ys[1], ys[3])) ||
41            (NEARLY_EQUAL(xs[0], xs[2]) && NEARLY_EQUAL(xs[1], xs[3]) &&
42             NEARLY_EQUAL(ys[0], ys[1]) && NEARLY_EQUAL(ys[2], ys[3]));
43 }
44 
coords_rectilinear(const float xs[4],const float ys[4])45 static bool coords_rectilinear(const float xs[4], const float ys[4]) {
46     SkVector e0{xs[1] - xs[0], ys[1] - ys[0]}; // connects to e1 and e2(repeat)
47     SkVector e1{xs[3] - xs[1], ys[3] - ys[1]}; // connects to e0(repeat) and e3
48     SkVector e2{xs[0] - xs[2], ys[0] - ys[2]}; // connects to e0 and e3(repeat)
49     SkVector e3{xs[2] - xs[3], ys[2] - ys[3]}; // connects to e1(repeat) and e2
50 
51     e0.normalize();
52     e1.normalize();
53     e2.normalize();
54     e3.normalize();
55 
56     return dot_nearly_zero(e0, e1) && dot_nearly_zero(e1, e3) &&
57            dot_nearly_zero(e2, e0) && dot_nearly_zero(e3, e2);
58 }
59 
quadType() const60 GrQuadType GrQuad::quadType() const {
61     // Since GrQuad applies any perspective information at construction time, there's only two
62     // types to choose from.
63     if (coords_form_rect(fX, fY)) {
64         return GrQuadType::kRect;
65     } else if (coords_rectilinear(fX, fY)) {
66         return GrQuadType::kRectilinear;
67     } else {
68         return GrQuadType::kStandard;
69     }
70 }
71 
quadType() const72 GrQuadType GrPerspQuad::quadType() const {
73     if (this->hasPerspective()) {
74         return GrQuadType::kPerspective;
75     } else {
76         // Rect or standard quad, can ignore w since they are all ones
77         if (coords_form_rect(fX, fY)) {
78             return GrQuadType::kRect;
79         } else if (coords_rectilinear(fX, fY)) {
80             return GrQuadType::kRectilinear;
81         } else {
82             return GrQuadType::kStandard;
83         }
84     }
85 }
86 #endif
87 
88 ///////////////////////////////////////////////////////////////////////////////////////////////////
89 
aa_affects_rect(float ql,float qt,float qr,float qb)90 static bool aa_affects_rect(float ql, float qt, float qr, float qb) {
91     return !SkScalarIsInt(ql) || !SkScalarIsInt(qr) || !SkScalarIsInt(qt) || !SkScalarIsInt(qb);
92 }
93 
94 template <typename Q>
GrResolveAATypeForQuad(GrAAType requestedAAType,GrQuadAAFlags requestedEdgeFlags,const Q & quad,GrQuadType knownType,GrAAType * outAAType,GrQuadAAFlags * outEdgeFlags)95 void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags,
96                             const Q& quad, GrQuadType knownType,
97                             GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) {
98     // Most cases will keep the requested types unchanged
99     *outAAType = requestedAAType;
100     *outEdgeFlags = requestedEdgeFlags;
101 
102     switch (requestedAAType) {
103         // When aa type is coverage, disable AA if the edge configuration doesn't actually need it
104         case GrAAType::kCoverage:
105             if (requestedEdgeFlags == GrQuadAAFlags::kNone) {
106                 // Turn off anti-aliasing
107                 *outAAType = GrAAType::kNone;
108             } else {
109                 // For coverage AA, if the quad is a rect and it lines up with pixel boundaries
110                 // then overall aa and per-edge aa can be completely disabled
111                 if (knownType == GrQuadType::kRect && !quad.aaHasEffectOnRect()) {
112                     *outAAType = GrAAType::kNone;
113                     *outEdgeFlags = GrQuadAAFlags::kNone;
114                 }
115             }
116             break;
117         // For no or msaa anti aliasing, override the edge flags since edge flags only make sense
118         // when coverage aa is being used.
119         case GrAAType::kNone:
120             *outEdgeFlags = GrQuadAAFlags::kNone;
121             break;
122         case GrAAType::kMSAA:
123             *outEdgeFlags = GrQuadAAFlags::kAll;
124             break;
125         case GrAAType::kMixedSamples:
126             SK_ABORT("Should not use mixed sample AA with edge AA flags");
127             break;
128     }
129 };
130 
131 // Instantiate GrResolve... for GrQuad and GrPerspQuad
132 template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrQuad&, GrQuadType,
133                                      GrAAType*, GrQuadAAFlags*);
134 template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrPerspQuad&, GrQuadType,
135                                      GrAAType*, GrQuadAAFlags*);
136 
GrQuadTypeForTransformedRect(const SkMatrix & matrix)137 GrQuadType GrQuadTypeForTransformedRect(const SkMatrix& matrix) {
138     if (matrix.rectStaysRect()) {
139         return GrQuadType::kRect;
140     } else if (matrix.preservesRightAngles()) {
141         return GrQuadType::kRectilinear;
142     } else if (matrix.hasPerspective()) {
143         return GrQuadType::kPerspective;
144     } else {
145         return GrQuadType::kStandard;
146     }
147 }
148 
GrQuad(const SkRect & rect,const SkMatrix & m)149 GrQuad::GrQuad(const SkRect& rect, const SkMatrix& m) {
150     SkMatrix::TypeMask tm = m.getType();
151     if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
152         auto r = Sk4f::Load(&rect);
153         const Sk4f t(m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY());
154         if (tm <= SkMatrix::kTranslate_Mask) {
155             r += t;
156         } else {
157             const Sk4f s(m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY());
158             r = r * s + t;
159         }
160         SkNx_shuffle<0, 0, 2, 2>(r).store(fX);
161         SkNx_shuffle<1, 3, 1, 3>(r).store(fY);
162     } else {
163         Sk4f rx(rect.fLeft, rect.fLeft, rect.fRight, rect.fRight);
164         Sk4f ry(rect.fTop, rect.fBottom, rect.fTop, rect.fBottom);
165         Sk4f sx(m.getScaleX());
166         Sk4f kx(m.getSkewX());
167         Sk4f tx(m.getTranslateX());
168         Sk4f ky(m.getSkewY());
169         Sk4f sy(m.getScaleY());
170         Sk4f ty(m.getTranslateY());
171         auto x = SkNx_fma(sx, rx, SkNx_fma(kx, ry, tx));
172         auto y = SkNx_fma(ky, rx, SkNx_fma(sy, ry, ty));
173         if (m.hasPerspective()) {
174             Sk4f w0(m.getPerspX());
175             Sk4f w1(m.getPerspY());
176             Sk4f w2(m.get(SkMatrix::kMPersp2));
177             auto iw = SkNx_fma(w0, rx, SkNx_fma(w1, ry, w2)).invert();
178             x *= iw;
179             y *= iw;
180         }
181         x.store(fX);
182         y.store(fY);
183     }
184 }
185 
aaHasEffectOnRect() const186 bool GrQuad::aaHasEffectOnRect() const {
187     SkASSERT(this->quadType() == GrQuadType::kRect);
188     return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]);
189 }
190 
GrPerspQuad(const SkRect & rect,const SkMatrix & m)191 GrPerspQuad::GrPerspQuad(const SkRect& rect, const SkMatrix& m) {
192     SkMatrix::TypeMask tm = m.getType();
193     if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
194         auto r = Sk4f::Load(&rect);
195         const Sk4f t(m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY());
196         if (tm <= SkMatrix::kTranslate_Mask) {
197             r += t;
198         } else {
199             const Sk4f s(m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY());
200             r = r * s + t;
201         }
202         SkNx_shuffle<0, 0, 2, 2>(r).store(fX);
203         SkNx_shuffle<1, 3, 1, 3>(r).store(fY);
204         fW[0] = fW[1] = fW[2] = fW[3] = 1.f;
205     } else {
206         Sk4f rx(rect.fLeft, rect.fLeft, rect.fRight, rect.fRight);
207         Sk4f ry(rect.fTop, rect.fBottom, rect.fTop, rect.fBottom);
208         Sk4f sx(m.getScaleX());
209         Sk4f kx(m.getSkewX());
210         Sk4f tx(m.getTranslateX());
211         Sk4f ky(m.getSkewY());
212         Sk4f sy(m.getScaleY());
213         Sk4f ty(m.getTranslateY());
214         SkNx_fma(sx, rx, SkNx_fma(kx, ry, tx)).store(fX);
215         SkNx_fma(ky, rx, SkNx_fma(sy, ry, ty)).store(fY);
216         if (m.hasPerspective()) {
217             Sk4f w0(m.getPerspX());
218             Sk4f w1(m.getPerspY());
219             Sk4f w2(m.get(SkMatrix::kMPersp2));
220             auto w = SkNx_fma(w0, rx, SkNx_fma(w1, ry, w2));
221             w.store(fW);
222         } else {
223             fW[0] = fW[1] = fW[2] = fW[3] = 1.f;
224         }
225     }
226 }
227 
228 // Private constructor used by GrQuadList to quickly fill in a quad's values from the channel arrays
GrPerspQuad(const float * xs,const float * ys,const float * ws)229 GrPerspQuad::GrPerspQuad(const float* xs, const float* ys, const float* ws) {
230     memcpy(fX, xs, 4 * sizeof(float));
231     memcpy(fY, ys, 4 * sizeof(float));
232     memcpy(fW, ws, 4 * sizeof(float));
233 }
234 
aaHasEffectOnRect() const235 bool GrPerspQuad::aaHasEffectOnRect() const {
236     SkASSERT(this->quadType() == GrQuadType::kRect);
237     // If rect, ws must all be 1s so no need to divide
238     return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]);
239 }
240