blob: bd01a68edbcf7c28621704468da41df6cb10e55f [file] [log] [blame]
Brian Salomona33b67c2018-05-17 10:42:14 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrQuad.h"
Brian Salomona33b67c2018-05-17 10:42:14 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrTypesPriv.h"
Michael Ludwig6bee7762018-10-19 09:50:36 -040011
Michael Ludwigb3461fa2019-04-30 11:50:55 -040012using V4f = skvx::Vec<4, float>;
13
Michael Ludwig1f7e4382018-10-19 09:36:57 -040014static bool aa_affects_rect(float ql, float qt, float qr, float qb) {
15 return !SkScalarIsInt(ql) || !SkScalarIsInt(qr) || !SkScalarIsInt(qt) || !SkScalarIsInt(qb);
16}
17
Michael Ludwige9c57d32019-02-13 13:39:39 -050018static void map_rect_translate_scale(const SkRect& rect, const SkMatrix& m,
Michael Ludwigb3461fa2019-04-30 11:50:55 -040019 V4f* xs, V4f* ys) {
Michael Ludwige9c57d32019-02-13 13:39:39 -050020 SkMatrix::TypeMask tm = m.getType();
21 SkASSERT(tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
22
Michael Ludwigb3461fa2019-04-30 11:50:55 -040023 V4f r = V4f::Load(&rect);
Michael Ludwige9c57d32019-02-13 13:39:39 -050024 if (tm > SkMatrix::kIdentity_Mask) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -040025 const V4f t{m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()};
Michael Ludwige9c57d32019-02-13 13:39:39 -050026 if (tm <= SkMatrix::kTranslate_Mask) {
27 r += t;
28 } else {
Michael Ludwigb3461fa2019-04-30 11:50:55 -040029 const V4f s{m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()};
Michael Ludwige9c57d32019-02-13 13:39:39 -050030 r = r * s + t;
31 }
32 }
Michael Ludwigb3461fa2019-04-30 11:50:55 -040033 *xs = skvx::shuffle<0, 0, 2, 2>(r);
34 *ys = skvx::shuffle<1, 3, 1, 3>(r);
Michael Ludwige9c57d32019-02-13 13:39:39 -050035}
36
Michael Ludwigb3461fa2019-04-30 11:50:55 -040037static void map_quad_general(const V4f& qx, const V4f& qy, const SkMatrix& m,
38 V4f* xs, V4f* ys, V4f* ws) {
39 *xs = mad(m.getScaleX(), qx, mad(m.getSkewX(), qy, m.getTranslateX()));
40 *ys = mad(m.getSkewY(), qx, mad(m.getScaleY(), qy, m.getTranslateY()));
Michael Ludwige9c57d32019-02-13 13:39:39 -050041 if (m.hasPerspective()) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -040042 V4f w = mad(m.getPerspX(), qx,
43 mad(m.getPerspY(), qy, m.get(SkMatrix::kMPersp2)));
Michael Ludwige9c57d32019-02-13 13:39:39 -050044 if (ws) {
45 // Output the calculated w coordinates
46 *ws = w;
47 } else {
48 // Apply perspective division immediately
Michael Ludwigb3461fa2019-04-30 11:50:55 -040049 V4f iw = 1.f / w;
Michael Ludwige9c57d32019-02-13 13:39:39 -050050 *xs *= iw;
51 *ys *= iw;
52 }
53 } else if (ws) {
54 *ws = 1.f;
55 }
56}
57
58static void map_rect_general(const SkRect& rect, const SkMatrix& matrix,
Michael Ludwigb3461fa2019-04-30 11:50:55 -040059 V4f* xs, V4f* ys, V4f* ws) {
60 V4f rx{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight};
61 V4f ry{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom};
Michael Ludwige9c57d32019-02-13 13:39:39 -050062 map_quad_general(rx, ry, matrix, xs, ys, ws);
63}
64
Michael Ludwig009b92e2019-02-15 16:03:53 -050065// Rearranges (top-left, top-right, bottom-right, bottom-left) ordered skQuadPts into xs and ys
66// ordered (top-left, bottom-left, top-right, bottom-right)
Michael Ludwigb3461fa2019-04-30 11:50:55 -040067static void rearrange_sk_to_gr_points(const SkPoint skQuadPts[4], V4f* xs, V4f* ys) {
68 *xs = V4f{skQuadPts[0].fX, skQuadPts[3].fX, skQuadPts[1].fX, skQuadPts[2].fX};
69 *ys = V4f{skQuadPts[0].fY, skQuadPts[3].fY, skQuadPts[1].fY, skQuadPts[2].fY};
Michael Ludwig009b92e2019-02-15 16:03:53 -050070}
71
Michael Ludwig41f395d2019-05-23 13:59:45 -040072// If an SkRect is transformed by this matrix, what class of quad is required to represent it.
73static GrQuadType quad_type_for_transformed_rect(const SkMatrix& matrix) {
74 if (matrix.rectStaysRect()) {
75 return GrQuadType::kRect;
76 } else if (matrix.preservesRightAngles()) {
77 return GrQuadType::kRectilinear;
78 } else if (matrix.hasPerspective()) {
79 return GrQuadType::kPerspective;
80 } else {
81 return GrQuadType::kStandard;
82 }
83}
84
85// Perform minimal analysis of 'pts' (which are suitable for MakeFromSkQuad), and determine a
86// quad type that will be as minimally general as possible.
87static GrQuadType quad_type_for_points(const SkPoint pts[4], const SkMatrix& matrix) {
88 if (matrix.hasPerspective()) {
89 return GrQuadType::kPerspective;
90 }
91 // If 'pts' was formed by SkRect::toQuad() and not transformed further, it is safe to use the
92 // quad type derived from 'matrix'. Otherwise don't waste any more time and assume kStandard
93 // (most general 2D quad).
94 if ((pts[0].fX == pts[3].fX && pts[1].fX == pts[2].fX) &&
95 (pts[0].fY == pts[1].fY && pts[2].fY == pts[3].fY)) {
96 return quad_type_for_transformed_rect(matrix);
97 } else {
98 return GrQuadType::kStandard;
99 }
100}
101
Michael Ludwig6bee7762018-10-19 09:50:36 -0400102template <typename Q>
103void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags,
Michael Ludwig41f395d2019-05-23 13:59:45 -0400104 const Q& quad, GrAAType* outAAType, GrQuadAAFlags* outEdgeFlags) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400105 // Most cases will keep the requested types unchanged
106 *outAAType = requestedAAType;
107 *outEdgeFlags = requestedEdgeFlags;
108
109 switch (requestedAAType) {
110 // When aa type is coverage, disable AA if the edge configuration doesn't actually need it
111 case GrAAType::kCoverage:
112 if (requestedEdgeFlags == GrQuadAAFlags::kNone) {
113 // Turn off anti-aliasing
114 *outAAType = GrAAType::kNone;
115 } else {
116 // For coverage AA, if the quad is a rect and it lines up with pixel boundaries
117 // then overall aa and per-edge aa can be completely disabled
Michael Ludwig41f395d2019-05-23 13:59:45 -0400118 if (quad.quadType() == GrQuadType::kRect && !quad.aaHasEffectOnRect()) {
Michael Ludwig6bee7762018-10-19 09:50:36 -0400119 *outAAType = GrAAType::kNone;
120 *outEdgeFlags = GrQuadAAFlags::kNone;
121 }
122 }
123 break;
124 // For no or msaa anti aliasing, override the edge flags since edge flags only make sense
125 // when coverage aa is being used.
126 case GrAAType::kNone:
127 *outEdgeFlags = GrQuadAAFlags::kNone;
128 break;
129 case GrAAType::kMSAA:
130 *outEdgeFlags = GrQuadAAFlags::kAll;
131 break;
132 case GrAAType::kMixedSamples:
133 SK_ABORT("Should not use mixed sample AA with edge AA flags");
134 break;
135 }
136};
137
138// Instantiate GrResolve... for GrQuad and GrPerspQuad
Michael Ludwig41f395d2019-05-23 13:59:45 -0400139template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrQuad&,
Michael Ludwig6bee7762018-10-19 09:50:36 -0400140 GrAAType*, GrQuadAAFlags*);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400141template void GrResolveAATypeForQuad(GrAAType, GrQuadAAFlags, const GrPerspQuad&,
Michael Ludwig6bee7762018-10-19 09:50:36 -0400142 GrAAType*, GrQuadAAFlags*);
143
Michael Ludwige9c57d32019-02-13 13:39:39 -0500144GrQuad GrQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400145 V4f x, y;
Brian Salomona33b67c2018-05-17 10:42:14 -0400146 SkMatrix::TypeMask tm = m.getType();
Michael Ludwig41f395d2019-05-23 13:59:45 -0400147 GrQuadType type;
Brian Salomona33b67c2018-05-17 10:42:14 -0400148 if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500149 map_rect_translate_scale(rect, m, &x, &y);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400150 type = GrQuadType::kRect;
Brian Salomona33b67c2018-05-17 10:42:14 -0400151 } else {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500152 map_rect_general(rect, m, &x, &y, nullptr);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400153 type = quad_type_for_transformed_rect(m);
154 if (type == GrQuadType::kPerspective) {
155 // While the matrix created perspective, the coordinates were projected to a 2D quad
156 // in map_rect_general since no w V4f was provided.
157 type = GrQuadType::kStandard;
158 }
Brian Salomona33b67c2018-05-17 10:42:14 -0400159 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400160 return GrQuad(x, y, type);
Brian Salomona33b67c2018-05-17 10:42:14 -0400161}
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400162
Michael Ludwig009b92e2019-02-15 16:03:53 -0500163GrQuad GrQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400164 V4f xs, ys;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500165 rearrange_sk_to_gr_points(pts, &xs, &ys);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400166 GrQuadType type = quad_type_for_points(pts, matrix);
167 if (type == GrQuadType::kPerspective) {
168 // While the matrix created perspective, the coordinates were projected to a 2D quad
169 // in map_rect_general since no w V4f was provided.
170 type = GrQuadType::kStandard;
171 }
172
Michael Ludwig009b92e2019-02-15 16:03:53 -0500173 if (matrix.isIdentity()) {
Michael Ludwig41f395d2019-05-23 13:59:45 -0400174 return GrQuad(xs, ys, type);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500175 } else {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400176 V4f mx, my;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500177 map_quad_general(xs, ys, matrix, &mx, &my, nullptr);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400178 return GrQuad(mx, my, type);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500179 }
180}
181
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400182bool GrQuad::aaHasEffectOnRect() const {
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400183 return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]);
184}
185
Michael Ludwigc96fc372019-01-08 15:46:15 -0500186// Private constructor used by GrQuadList to quickly fill in a quad's values from the channel arrays
Michael Ludwig41f395d2019-05-23 13:59:45 -0400187GrPerspQuad::GrPerspQuad(const float* xs, const float* ys, const float* ws, GrQuadType type)
188 : fType(type) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500189 memcpy(fX, xs, 4 * sizeof(float));
190 memcpy(fY, ys, 4 * sizeof(float));
191 memcpy(fW, ws, 4 * sizeof(float));
192}
193
Michael Ludwige9c57d32019-02-13 13:39:39 -0500194GrPerspQuad GrPerspQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400195 V4f x, y, w;
Michael Ludwige9c57d32019-02-13 13:39:39 -0500196 SkMatrix::TypeMask tm = m.getType();
Michael Ludwig41f395d2019-05-23 13:59:45 -0400197 GrQuadType type;
Michael Ludwige9c57d32019-02-13 13:39:39 -0500198 if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
199 map_rect_translate_scale(rect, m, &x, &y);
200 w = 1.f;
Michael Ludwig41f395d2019-05-23 13:59:45 -0400201 type = GrQuadType::kRect;
Michael Ludwige9c57d32019-02-13 13:39:39 -0500202 } else {
203 map_rect_general(rect, m, &x, &y, &w);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400204 type = quad_type_for_transformed_rect(m);
Michael Ludwige9c57d32019-02-13 13:39:39 -0500205 }
Michael Ludwig41f395d2019-05-23 13:59:45 -0400206 return GrPerspQuad(x, y, w, type);
Michael Ludwige9c57d32019-02-13 13:39:39 -0500207}
208
Michael Ludwig009b92e2019-02-15 16:03:53 -0500209GrPerspQuad GrPerspQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400210 V4f xs, ys;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500211 rearrange_sk_to_gr_points(pts, &xs, &ys);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400212 GrQuadType type = quad_type_for_points(pts, matrix);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500213 if (matrix.isIdentity()) {
Michael Ludwig41f395d2019-05-23 13:59:45 -0400214 return GrPerspQuad(xs, ys, 1.f, type);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500215 } else {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400216 V4f mx, my, mw;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500217 map_quad_general(xs, ys, matrix, &mx, &my, &mw);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400218 return GrPerspQuad(mx, my, mw, type);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500219 }
220}
221
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400222bool GrPerspQuad::aaHasEffectOnRect() const {
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400223 // If rect, ws must all be 1s so no need to divide
224 return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]);
225}