blob: 6ed72325b7793e44ea24f92c9baf44df8de9f605 [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
Michael Ludwigfd4f4df2019-05-29 09:51:09 -04008#include "src/gpu/geometry/GrQuad.h"
Brian Salomona33b67c2018-05-17 10:42:14 -04009
Michael Ludwigde4c58c2019-06-04 09:12:59 -040010#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrTypesPriv.h"
Michael Ludwig6bee7762018-10-19 09:50:36 -040012
Michael Ludwigb3461fa2019-04-30 11:50:55 -040013using V4f = skvx::Vec<4, float>;
14
Michael Ludwig1f7e4382018-10-19 09:36:57 -040015static bool aa_affects_rect(float ql, float qt, float qr, float qb) {
16 return !SkScalarIsInt(ql) || !SkScalarIsInt(qr) || !SkScalarIsInt(qt) || !SkScalarIsInt(qb);
17}
18
Michael Ludwige9c57d32019-02-13 13:39:39 -050019static void map_rect_translate_scale(const SkRect& rect, const SkMatrix& m,
Michael Ludwigb3461fa2019-04-30 11:50:55 -040020 V4f* xs, V4f* ys) {
Michael Ludwige9c57d32019-02-13 13:39:39 -050021 SkMatrix::TypeMask tm = m.getType();
22 SkASSERT(tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask));
23
Michael Ludwigb3461fa2019-04-30 11:50:55 -040024 V4f r = V4f::Load(&rect);
Michael Ludwige9c57d32019-02-13 13:39:39 -050025 if (tm > SkMatrix::kIdentity_Mask) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -040026 const V4f t{m.getTranslateX(), m.getTranslateY(), m.getTranslateX(), m.getTranslateY()};
Michael Ludwige9c57d32019-02-13 13:39:39 -050027 if (tm <= SkMatrix::kTranslate_Mask) {
28 r += t;
29 } else {
Michael Ludwigb3461fa2019-04-30 11:50:55 -040030 const V4f s{m.getScaleX(), m.getScaleY(), m.getScaleX(), m.getScaleY()};
Michael Ludwige9c57d32019-02-13 13:39:39 -050031 r = r * s + t;
32 }
33 }
Michael Ludwigb3461fa2019-04-30 11:50:55 -040034 *xs = skvx::shuffle<0, 0, 2, 2>(r);
35 *ys = skvx::shuffle<1, 3, 1, 3>(r);
Michael Ludwige9c57d32019-02-13 13:39:39 -050036}
37
Michael Ludwigb3461fa2019-04-30 11:50:55 -040038static void map_quad_general(const V4f& qx, const V4f& qy, const SkMatrix& m,
39 V4f* xs, V4f* ys, V4f* ws) {
40 *xs = mad(m.getScaleX(), qx, mad(m.getSkewX(), qy, m.getTranslateX()));
41 *ys = mad(m.getSkewY(), qx, mad(m.getScaleY(), qy, m.getTranslateY()));
Michael Ludwige9c57d32019-02-13 13:39:39 -050042 if (m.hasPerspective()) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -040043 V4f w = mad(m.getPerspX(), qx,
44 mad(m.getPerspY(), qy, m.get(SkMatrix::kMPersp2)));
Michael Ludwige9c57d32019-02-13 13:39:39 -050045 if (ws) {
46 // Output the calculated w coordinates
47 *ws = w;
48 } else {
49 // Apply perspective division immediately
Michael Ludwigb3461fa2019-04-30 11:50:55 -040050 V4f iw = 1.f / w;
Michael Ludwige9c57d32019-02-13 13:39:39 -050051 *xs *= iw;
52 *ys *= iw;
53 }
54 } else if (ws) {
55 *ws = 1.f;
56 }
57}
58
59static void map_rect_general(const SkRect& rect, const SkMatrix& matrix,
Michael Ludwigb3461fa2019-04-30 11:50:55 -040060 V4f* xs, V4f* ys, V4f* ws) {
61 V4f rx{rect.fLeft, rect.fLeft, rect.fRight, rect.fRight};
62 V4f ry{rect.fTop, rect.fBottom, rect.fTop, rect.fBottom};
Michael Ludwige9c57d32019-02-13 13:39:39 -050063 map_quad_general(rx, ry, matrix, xs, ys, ws);
64}
65
Michael Ludwig009b92e2019-02-15 16:03:53 -050066// Rearranges (top-left, top-right, bottom-right, bottom-left) ordered skQuadPts into xs and ys
67// ordered (top-left, bottom-left, top-right, bottom-right)
Michael Ludwigb3461fa2019-04-30 11:50:55 -040068static void rearrange_sk_to_gr_points(const SkPoint skQuadPts[4], V4f* xs, V4f* ys) {
69 *xs = V4f{skQuadPts[0].fX, skQuadPts[3].fX, skQuadPts[1].fX, skQuadPts[2].fX};
70 *ys = V4f{skQuadPts[0].fY, skQuadPts[3].fY, skQuadPts[1].fY, skQuadPts[2].fY};
Michael Ludwig009b92e2019-02-15 16:03:53 -050071}
72
Michael Ludwig41f395d2019-05-23 13:59:45 -040073// If an SkRect is transformed by this matrix, what class of quad is required to represent it.
Michael Ludwigde4c58c2019-06-04 09:12:59 -040074static GrQuad::Type quad_type_for_transformed_rect(const SkMatrix& matrix) {
Michael Ludwig41f395d2019-05-23 13:59:45 -040075 if (matrix.rectStaysRect()) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -040076 return GrQuad::Type::kAxisAligned;
Michael Ludwig41f395d2019-05-23 13:59:45 -040077 } else if (matrix.preservesRightAngles()) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -040078 return GrQuad::Type::kRectilinear;
Michael Ludwig41f395d2019-05-23 13:59:45 -040079 } else if (matrix.hasPerspective()) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -040080 return GrQuad::Type::kPerspective;
Michael Ludwig41f395d2019-05-23 13:59:45 -040081 } else {
Michael Ludwigde4c58c2019-06-04 09:12:59 -040082 return GrQuad::Type::kGeneral;
Michael Ludwig41f395d2019-05-23 13:59:45 -040083 }
84}
85
86// Perform minimal analysis of 'pts' (which are suitable for MakeFromSkQuad), and determine a
87// quad type that will be as minimally general as possible.
Michael Ludwigde4c58c2019-06-04 09:12:59 -040088static GrQuad::Type quad_type_for_points(const SkPoint pts[4], const SkMatrix& matrix) {
Michael Ludwig41f395d2019-05-23 13:59:45 -040089 if (matrix.hasPerspective()) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -040090 return GrQuad::Type::kPerspective;
Michael Ludwig41f395d2019-05-23 13:59:45 -040091 }
92 // If 'pts' was formed by SkRect::toQuad() and not transformed further, it is safe to use the
93 // quad type derived from 'matrix'. Otherwise don't waste any more time and assume kStandard
94 // (most general 2D quad).
95 if ((pts[0].fX == pts[3].fX && pts[1].fX == pts[2].fX) &&
96 (pts[0].fY == pts[1].fY && pts[2].fY == pts[3].fY)) {
97 return quad_type_for_transformed_rect(matrix);
98 } else {
Michael Ludwigde4c58c2019-06-04 09:12:59 -040099 return GrQuad::Type::kGeneral;
Michael Ludwig41f395d2019-05-23 13:59:45 -0400100 }
101}
102
Michael Ludwig6bee7762018-10-19 09:50:36 -0400103void GrResolveAATypeForQuad(GrAAType requestedAAType, GrQuadAAFlags requestedEdgeFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400104 const GrQuad& 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 Ludwigde4c58c2019-06-04 09:12:59 -0400118 if (quad.quadType() == GrQuad::Type::kAxisAligned && !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
Michael Ludwige9c57d32019-02-13 13:39:39 -0500138GrQuad GrQuad::MakeFromRect(const SkRect& rect, const SkMatrix& m) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400139 V4f x, y, w;
Brian Salomona33b67c2018-05-17 10:42:14 -0400140 SkMatrix::TypeMask tm = m.getType();
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400141 Type type;
Brian Salomona33b67c2018-05-17 10:42:14 -0400142 if (tm <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500143 map_rect_translate_scale(rect, m, &x, &y);
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400144 w = 1.f;
145 type = Type::kAxisAligned;
Brian Salomona33b67c2018-05-17 10:42:14 -0400146 } else {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400147 map_rect_general(rect, m, &x, &y, &w);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400148 type = quad_type_for_transformed_rect(m);
Brian Salomona33b67c2018-05-17 10:42:14 -0400149 }
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400150 return GrQuad(x, y, w, type);
Brian Salomona33b67c2018-05-17 10:42:14 -0400151}
Brian Salomonbe3c1d22018-05-21 12:54:39 -0400152
Michael Ludwig009b92e2019-02-15 16:03:53 -0500153GrQuad GrQuad::MakeFromSkQuad(const SkPoint pts[4], const SkMatrix& matrix) {
Michael Ludwigb3461fa2019-04-30 11:50:55 -0400154 V4f xs, ys;
Michael Ludwig009b92e2019-02-15 16:03:53 -0500155 rearrange_sk_to_gr_points(pts, &xs, &ys);
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400156 Type type = quad_type_for_points(pts, matrix);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500157 if (matrix.isIdentity()) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400158 return GrQuad(xs, ys, 1.f, type);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500159 } else {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400160 V4f mx, my, mw;
161 map_quad_general(xs, ys, matrix, &mx, &my, &mw);
162 return GrQuad(mx, my, mw, type);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500163 }
164}
165
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400166bool GrQuad::aaHasEffectOnRect() const {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400167 SkASSERT(this->quadType() == Type::kAxisAligned);
Michael Ludwig1f7e4382018-10-19 09:36:57 -0400168 // If rect, ws must all be 1s so no need to divide
169 return aa_affects_rect(fX[0], fY[0], fX[3], fY[3]);
170}